<?php
// +----------------------------------------------------------------------
// | newcms 入口文件 (ThinkPHP 8)
// | 以原版 newcms 程序为核心，框架升级为 ThinkPHP 8.1
// | 入口文件位于项目根目录（与原版 newcms 保持一致）
// +----------------------------------------------------------------------

// PHP 版本检查（沿用原版逻辑，门槛上调到 7.4）
if (!version_compare(PHP_VERSION, '7.4.0', 'ge')) {
    header("Content-type: text/html; charset=utf-8");
    echo '您当前使用的PHP版本为：' . PHP_VERSION . '，本程序基于 ThinkPHP 8 升级，最低要求 PHP7.4，建议使用 PHP8.0+';
    exit;
}

// 301 跳转（无 www 自动补 www，仅 2 段域名时）
// [BUG修复] 禁用此逻辑。当站点主域本身就是 efusc.com（无 www）时，
// 此 301 会把用户强跳到不存在的 www.efusc.com，导致：
//   1) 浏览器永久缓存 301，后续访问全部失效
//   2) cookie 域名变化，captcha session 对不上 → 「验证码错误」
// 如需启用，请在数据库 Settings 中配置 www 域名后再打开。
$is301 = 0;
if ($is301) {
    $pageURL = 'http';
    if (@$_SERVER["HTTPS"] == "on") {
        $pageURL .= "s";
    }
    $httpType  = $pageURL . "://";
    $hostArray = explode('.', $_SERVER["HTTP_HOST"] ?? '');
    if (count($hostArray) == 2) {
        Header("HTTP/1.1 301 Moved Permanently");
        Header("Location: " . $httpType . 'www.' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
        exit;
    }
}

// [ 业务常量 - 与原版 newcms 完全一致 ]
define('MIP_HOST', true);
define('SITE_HOST', true);
define('BAIDU', false);

// [ 路径常量 - 与原版 newcms 完全一致（入口在根目录，无需 ../） ]
define('APP_PATH',     __DIR__ . '/app/');
define('ADDONS_PATH',  __DIR__ . '/addons/');
define('ZHANQUN_PATH', __DIR__ . '/zhanqun/');
define('ZQADDONS_PATH',__DIR__ . '/zqAddons/');
define('SITE_PATH',    __DIR__ . '/');
defined('MIP_ROOT') or define('MIP_ROOT', __DIR__ . '/');
define('PUBLIC_PATH',  __DIR__ . '/public/');
define('VENDOR_PATH',  __DIR__ . '/vendor/');

// PHP 8 兼容：屏蔽 NOTICE/WARNING 级警告（与原版行为一致）
error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR);

// 加载框架引导文件
// 原版：require __DIR__ . '/system/thinkphp/start.php';
// TP8 ：通过 Composer vendor/autoload.php + 标准启动四行
require __DIR__ . '/vendor/autoload.php';

// [对齐旧版 newcms 行为] 通过 runtime/debug.txt 开关控制 debug 模式
// 旧版在 app/config.php 里读取该文件设置 $app_debug + $exception_tmpl，
// TP8 不再从 config 读 app_debug（只读 env），所以在入口显式调用 debug(true)
// 同时写入 putenv 以保证 env('APP_DEBUG') / env('app_debug') 在后续链路中亦可取
if (is_file(__DIR__ . '/runtime/debug.txt')
    && trim((string) @file_get_contents(__DIR__ . '/runtime/debug.txt'))) {
    // 部分主机 disable_functions 中禁用了 putenv，需要守卫调用
    if (function_exists('putenv') && !in_array('putenv', array_map('trim', explode(',', (string) ini_get('disable_functions'))))) {
        @putenv('APP_DEBUG=1');
    }
    $_ENV['APP_DEBUG']    = '1';
    $_SERVER['APP_DEBUG'] = '1';
}

$app = new \think\App();
if (!empty($_ENV['APP_DEBUG']) || (function_exists('getenv') && getenv('APP_DEBUG'))) {
    $app->debug(true);
}
$http     = $app->http;
$response = $http->run();
$response->send();
$http->end($response);
