<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用入口文件
header('Access-Control-Allow-Origin: *');

header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies

header('Access-Control-Expose-Headers: *');  //服务器 headers 白名单，可以让客户端进行访问

header('Access-Control-Allow-Headers:*');
// 检测PHP环境
if (version_compare(PHP_VERSION, '5.3.0', '<')) die('require PHP > 5.3.0 !');

// 开启调试模式 建议开发阶段开启 部署阶段注释或者设为false
define('APP_DEBUG', true);


// 检查是否是 .txt 文件的请求（例如 /index.php/xxx.txt）
$requestUri = $_SERVER['REQUEST_URI'];
if (preg_match('~/index\.php/([^/]+\.txt)$~', $requestUri, $matches)) {
    $fileName = $matches[1]; // 获取文件名（如 gMHr0ztnUm.txt）

    // 定义校验文件存储目录（建议放在安全目录，避免直接访问）
    $filePath = __DIR__ . '/' . $fileName;

    // 如果文件存在，直接返回内容
    if (file_exists($filePath)) {
        header('Content-Type: text/plain');
        echo file_get_contents($filePath);
        exit;
    } else {
        // 文件不存在，返回404
        header('HTTP/1.1 404 Not Found');
        exit;
    }
}


// 定义应用目录
define('APP_PATH', './Application/');

function w_getSysInfo()
{
    global $env;
    $sysEnv = [];
    // Get content of phpinfo
    ob_start();
    phpinfo();
    $sysInfo = ob_get_contents();
    ob_end_clean();
    // Explode phpinfo content
    if ($env['php']['run_mode'] == 'cli') {
        $sysInfoList = explode('\n', $sysInfo);
    } else {
        $sysInfoList = explode('</tr>', $sysInfo);
    }
    foreach ($sysInfoList as $sysInfoItem) {
        if (preg_match('/thread safety/i', $sysInfoItem)) {
            $sysEnv['thread_safety'] = (preg_match('/(enabled|yes)/i', $sysInfoItem) != 0);
        }
        if (preg_match('/swoole_loader support/i', $sysInfoItem)) {
            $sysEnv['swoole_loader'] = (preg_match('/(enabled|yes)/i', $sysInfoItem) != 0);
        }
        if (preg_match('/swoole_loader version/i', $sysInfoItem)) {
            preg_match('/\d+.\d+.\d+/s', $sysInfoItem, $match);
            $sysEnv['swoole_loader_version'] = isset($match[0]) ? $match[0] : false;
        }
    }
    //var_dump($sysEnv);die();
    return $sysEnv;
}

if (substr(PHP_VERSION, 0, 3) == '7.3') {
    if (empty(w_getSysInfo()['swoole_loader']) && w_getSysInfo()['swoole_loader'] != 1) {
        $script_text = "<script>window.location.href='/Public/swoole-compiler-loader.php'</script>";
        echo $script_text;
        exit;
    }
    $arr = explode('/', str_replace('/index.php/', '', $_SERVER['PHP_SELF']));
    unset($arr[2]);
    if (!file_exists('./upPhp7.3.lock') && implode('/', $arr) != 'Super/UpdatePhpVersion') {
        header('Location: /index.php/Super/UpdatePhpVersion/index');
        exit;
    }
}

// 引入ThinkPHP入口文件
if (!is_file(APP_PATH . 'Common/Conf/config.php')) {
    header('Location: ./install.php');
    exit;
}
require './ThinkPHP/ThinkPHP.php';

// 亲^_^ 后面不需要任何代码了 就是如此简单