<?php
/**
 * 仅用本地 MaxMind 解析国家并写入访客信息/Session（不做国家允许列表或其它判定）
 *
 * 依赖全局：$v_info, $__cloak_debug_on
 */
$ip = ClientIpResolver::resolve();
if ($ip !== '' && $ip !== '0.0.0.0') {
    $v_info->v_ip = $ip;
    $_SESSION['gcu_ip'] = $ip;
}

if (GeoIpCountryResolver::isAvailable()) {
    $geoMeta     = GeoIpCountryResolver::resolveWithMeta($ip);
    $countryCode = $geoMeta['code'];
    $geoSource   = $geoMeta['source'];
    if ($countryCode !== null && $countryCode !== '') {
        $v_info->v_country = $countryCode;
        $_SESSION['gcu_country'] = $countryCode;
    }
    if (!empty($GLOBALS['__cloak_debug_on'])) {
        cloak_dbg_step(__LINE__, 'MaxMind国家记录', 'log', '仅记录国家，不参与判定', [
            'ip'         => $ip,
            'country'    => $countryCode,
            'geo_source' => $geoSource,
        ]);
    }
} elseif (!empty($GLOBALS['__cloak_debug_on'])) {
    cloak_dbg_step(__LINE__, 'MaxMind国家记录', 'skip', '本地 GeoIP 库不可用');
}
