<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

// [ 应用入口文件 ]
namespace think;

/*
 * 不在tp加载后判断，为了安全的使用exit()
 * 使用绝对路径，确保花里胡哨的url均能正确判定和跳转
 */
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    header("Access-Control-Allow-Credentials: true");
    header("Access-Control-Max-Age: 86400");
    header("Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS");
    header("Access-Control-Allow-Headers: " . $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']);
    header("Access-Control-Allow-Origin: *");
    exit();
}

$rootPath = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR;
$server = isset($_REQUEST['server']) ||
    isset($_SERVER['HTTP_SERVER']) ||
    substr($_SERVER['REQUEST_URI'], 1, 9) == 'index.php' ||
    strpos($_SERVER['REQUEST_URI'], 'notify') ||
    strpos($_SERVER['REQUEST_URI'], 'pay/payment') ||
    strpos($_SERVER['REQUEST_URI'], 'alipaycashier') ||
    strpos($_SERVER['REQUEST_URI'], 'pay/cashier') ||
    strpos($_SERVER['REQUEST_URI'], 'getPay');
if (!$server) {
    // 用户访问前端

    // 未安装swoole
    if (!extension_loaded("swoole_loader")) {
        header("location:" . DIRECTORY_SEPARATOR . 'help' . DIRECTORY_SEPARATOR . 'swoole-compiler-loader.php');
        exit();
    }

    // 安装检测-s
    if (!file_exists($rootPath . 'install.lock')) {
        header("location:" . DIRECTORY_SEPARATOR . 'admin.html#/install');
        exit();
    }
    // 安装检测-e

    if (is_file($rootPath . 'index.html')) {
        header("location:" . DIRECTORY_SEPARATOR . 'index.html');
        exit();
    }

    if (is_file($rootPath . 'admin.html')) {
        header("location:" . DIRECTORY_SEPARATOR . 'admin.html');
        exit();
    }
    // 检测是否已编译前端-e
} else {
    // 访问后端
    $requestUri = $_SERVER['REQUEST_URI'];

    // 判断当前访问路径是不是/api/install开头
    if (
        !str_starts_with($requestUri, '/api/install') && // 注意，这里是“从开头”匹配
        !file_exists($rootPath . 'install.lock')     // 且 install.lock 文件不存在
    ) {
        header("location:" . DIRECTORY_SEPARATOR . 'admin.html#/install');
        exit();
    }
}
if (!extension_loaded("swoole_loader")) {
    header("location:" . DIRECTORY_SEPARATOR . 'help' . DIRECTORY_SEPARATOR . 'swoole-compiler-loader.php');
//    echo json_encode([
//        'code' => 3001,
//        'msg' => 'swoole_loader未安装',
//        'url' => DIRECTORY_SEPARATOR . 'help' . DIRECTORY_SEPARATOR . 'swoole-compiler-loader.php',
//    ]);
    exit();
}


require __DIR__ . '/../vendor/autoload.php';

// 执行HTTP应用并响应
$app = (new App());
$http = $app->http;
if (strpos($_SERVER['REQUEST_URI'], 'pay/payment')) {
    $app->route->rule('/', '/pay/submit');
    $http->name('api');
}
if (strpos($_SERVER['REQUEST_URI'], 'alipaycashier')) {
    $app->route->rule('/', '/pay/alipaycashier');
    $http->name('api');
}
if (strpos($_SERVER['REQUEST_URI'], 'pay/cashier')) {
    $app->route->rule('/', '/pay/cashier');
    $http->name('api');
}
if (preg_match('#/getPay(\?|$)#', $_SERVER['REQUEST_URI'])) {
    $app->route->rule('/', '/pay/getPay');
    $http->name('api');
}

$response = $http->run();

$response->send();

$http->end($response);