<?php
    // 广告来源限制（黑白名单之后、临时链接 / API 之前）
    if (empty($_SESSION['check_result'])) {
        $__ad_guard_cfg = [
            'ad_fb'     => defined('CLOAK_AD_FB')     && strtoupper(CLOAK_AD_FB)     === 'ON',
            'ad_google' => defined('CLOAK_AD_GOOGLE') && strtoupper(CLOAK_AD_GOOGLE) === 'ON',
            'ad_tiktok' => defined('CLOAK_AD_TIKTOK') && strtoupper(CLOAK_AD_TIKTOK) === 'ON',
            'ad_strict' => defined('CLOAK_AD_STRICT') && strtoupper(CLOAK_AD_STRICT) === 'ON',
            'ad_spm_id' => defined('CLOAK_AD_SPM_ID') ? (string) CLOAK_AD_SPM_ID : '',
        ];

        if (CloakAdSourceGuard::anyPlatformEnabled($__ad_guard_cfg)) {
            if ($__cloak_debug_on) {
                cloak_dbg_step(__LINE__, '广告来源限制', 'info', '已启用平台检测，开始 evaluate()');
            }

            $__ad_ref_raw = isset($_SERVER['HTTP_REFERER']) ? (string) $_SERVER['HTTP_REFERER'] : '';
            $__ad_ref_parts = parse_url($__ad_ref_raw);
            $__ad_ref_q = [];
            if (!empty($__ad_ref_parts['query'])) {
                parse_str($__ad_ref_parts['query'], $__ad_ref_q);
            }
            $__ad_query = array_merge($__ad_ref_q, $_GET);

            $__ad_result = CloakAdSourceGuard::evaluate($__ad_guard_cfg, [
                'referer'    => $__ad_ref_raw,
                'query'      => $__ad_query,
                'user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? (string) $_SERVER['HTTP_USER_AGENT'] : '',
            ]);

            if ($__ad_result['passed']) {
                if ($__cloak_debug_on) {
                    cloak_dbg_step(__LINE__, '广告来源限制', 'pass', 'matched_by=' . $__ad_result['matched_by']);
                    cloak_dbg_record(__LINE__, null, '', [
                        'stage'      => 'ad_source_guard',
                        'matched_by' => $__ad_result['matched_by'],
                    ]);
                }
            } elseif ($__ad_guard_cfg['ad_strict']) {
                $_SESSION['check_result'] = 'false';
                $reason = $__ad_result['reason'];
                if ($__cloak_debug_on) {
                    cloak_dbg_record(__LINE__, 'false', $reason, [
                        'stage'      => 'ad_source_guard',
                        'matched_by' => $__ad_result['matched_by'],
                        'strict'     => true,
                    ]);
                }
            } else {
                $reason = cloak_reason_append($reason, $__ad_result['reason']);
                if ($__cloak_debug_on) {
                    cloak_dbg_step(__LINE__, '广告来源限制', 'warn', $reason, [
                        'matched_by' => $__ad_result['matched_by'],
                        'strict'     => false,
                    ]);
                    cloak_dbg_record(__LINE__, null, $reason, [
                        'stage'      => 'ad_source_guard_uncertain',
                        'matched_by' => $__ad_result['matched_by'],
                    ]);
                }
            }

            unset($__ad_ref_raw, $__ad_ref_parts, $__ad_ref_q, $__ad_query, $__ad_result);
        } elseif ($__cloak_debug_on) {
            cloak_dbg_step(__LINE__, '广告来源限制', 'skip', '未开启任何广告平台');
        }

        unset($__ad_guard_cfg);
    } elseif ($__cloak_debug_on) {
        cloak_dbg_step(__LINE__, '广告来源限制', 'skip', 'check_result 已由前置步骤写入: ' . ($_SESSION['check_result'] ?? ''));
    }
