<?php
    //指定ip显示仿品（优先数据库白名单组，其次兼容旧版 SHOW_SITE_IP 逗号列表）
    if (defined('WHITELIST_GROUP_ID') && (int) WHITELIST_GROUP_ID > 0) {
        $ips_fp_ary = ip_group_list_addresses((int) WHITELIST_GROUP_ID);
    } else {
        $ips_fp_ary = array_filter(array_map('trim', explode(',', SHOW_SITE_IP)), static function ($v) {
            return $v !== '';
        });
    }
    $reason = "";  // 判断理由
    
    $whiteParamsMatched = false;
    if( !empty(WHITE_PARAMS) ) {
        $wparams = explode('=', WHITE_PARAMS, 2);
        
        if(count($wparams) == 2) {
            $pk = $wparams[0];
            $pv = $wparams[1];
        }
        
        if(isset($_GET[$pk]) && strip_tags($_GET[$pk]) == $pv) {
            $whiteParamsMatched = true;
            
        }
    }
    
    $match = false;
    if (defined('BLACKLIST_GROUP_ID') && (int) BLACKLIST_GROUP_ID > 0) {
        $blEntries = ip_group_list_addresses((int) BLACKLIST_GROUP_ID);
        $match = ip_visitor_in_blacklist_entries($v_info->v_ip, $blEntries);
    }
    if ($__cloak_debug_on) {
        cloak_dbg_step(__LINE__,'黑名单检查', $match ? 'fail' : 'pass', $match ? 'IP 在黑名单组内' : '未命中黑名单', [
            'ip' => $v_info->v_ip,
            'group_id' => defined('BLACKLIST_GROUP_ID') ? BLACKLIST_GROUP_ID : 0,
        ]);
    }
    if ($match) {
        $_SESSION["check_result"] = "false";
        $reason = "黑名单";
        if ($__cloak_debug_on) {
            cloak_dbg_record(__LINE__,'false', $reason, ['stage' => 'blacklist']);
        }
    }
    
    if(in_array($v_info->v_ip, $ips_fp_ary) ) {
        $_SESSION["check_result"] = "true";
        $reason = "白名单";
        if ($__cloak_debug_on) {
            cloak_dbg_step(__LINE__,'IP 白名单', 'pass', 'IP 在白名单组/列表中');
            cloak_dbg_record(__LINE__,'true', $reason, ['stage' => 'whitelist_ip']);
        }
    } elseif( !empty(WHITE_PARAMS) ) {
        $wparams = explode('=', WHITE_PARAMS, 2);
        if(count($wparams) == 2) {
            $pk = $wparams[0];
            $pv = $wparams[1];
        }
        if(isset($_GET[$pk]) && strip_tags($_GET[$pk]) == $pv) {
            $_SESSION["check_result"] = "true";
            $reason = "白名单链接参数";
            if ($__cloak_debug_on) {
                cloak_dbg_step(__LINE__,'链接参数白名单', 'pass', WHITE_PARAMS);
                cloak_dbg_record(__LINE__,'true', $reason, ['stage' => 'whitelist_params']);
            }
        }
    } elseif ($__cloak_debug_on && !empty(WHITE_PARAMS)) {
        cloak_dbg_step(__LINE__,'链接参数白名单', 'skip', '参数不匹配: ' . WHITE_PARAMS);
    }
