<?php

// 如果已经通过 Turnstile，直接放行
if (!empty($_COOKIE['turnstile_pass'])) {
    require 'bpage/index.html';
    exit;
}
if (!empty($_COOKIE['turnstile_no_pass'])) {
    require 'apage/index.html';
    exit;
}


//引用配置文件
require __DIR__ . '/tretrerf/fangyu_config.php';

//请求网络接口（空或0无返回；1有返回）
$result = fangyuApiRequestExecutor(1);


//返回码；请求异常：-1；被拦截：0；被允许：1；
$resultCode = $result['code'];
// 是否开启真人验证
$realPersonVerifyEnabled = $result['realPersonVerifyEnabled'] ?? false;
// 设置 HTTP 状态码（可选，如果你想保持 200，可以不用设置）

//判断
if ($resultCode == 1) {

//***********************************************//
//******************* 被允许  *******************//
//***********************************************//

    // 保存用户最初访问的完整 URL
    $currentUrl = $_SERVER['REQUEST_URI'];  // 只包含 path + query
    setcookie("original_url", $currentUrl, time()+600, "/");

    // 根据 realPersonVerifyEnabled 决定是否进行真人验证
    if ($realPersonVerifyEnabled) {
        // 开启真人验证 → 进入Turnstile验证页
        header("Location: /turnstile.php");
        exit;
    } else {
        // 未开启真人验证 → 直接放行
        require 'bpage/index.html';
        exit;
    }

	//方式1：引用本地文件模式
    // require 'index_fdfdjjfhodfo.html';

	//方式2：【单个页面】跳转模式
	//header('Location: https://www.baidu.com/');
	//exit; 

    //方式3：【普通多页面随机轮询】跳转模式
	/*
   $pages = [
			"http://example.com/page1",
			"http://example.com/page2",
			"http://example.com/page3",
			"http://example.com/page4",
			"http://example.com/page5"
			];
	$randomKey = array_rand($pages);
	$randomPage = $pages[$randomKey];
	header('Location: '.$randomPage);
	exit; 
	*/

	//方式4：【均匀多页面随机轮询】跳转模式
	/*
   $pages = [
			"http://example.com/page1",
			"http://example.com/page2",
			"http://example.com/page3",
			"http://example.com/page4",
			"http://example.com/page5"
			];
	shuffle($pages);
	$randomPage = $pages[0];
	header('Location: '.$randomPage);
	exit; 
	*/

} else {

//**********************************************//
//*********** 被拦截 和 请求异常 等  ***********//
//**********************************************//

	//方式1：引用本地文件模式
    //require '安全页_[随机字符串].html';

	//方式2：跳转模式
// 	header('Location: https://www.facebook.com/');
    require 'apage/index.html';
	exit; 

	//方式3：返回404错误(页面不存在)
	//header("HTTP/1.1 404 Not Found");
	//exit; 

	//方式4：返回403错误(页面无权访问)
	//header("HTTP/1.1 403 Forbidden");
	//exit; 
}

?>