<?php

// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// [ 应用入口文件 ]
// 定义应用目录
define('APP_PATH', __DIR__ . '/../application/');

// 判断是否安装（open_basedir 仅允许 public 时无法读 application，视为已安装继续）
$lockFile = APP_PATH . 'admin/command/Install/install.lock';
$installed = @is_file($lockFile);
if (!$installed && @is_dir(APP_PATH)) {
    header("location:./install.php");
    exit;
}

// 伪静态误入本入口时转后台隐藏入口（与 public/OlLYSXvpwx.php 绑定 admin 一致）
// 否则菜单生成 /payorder/index 被 nginx 转到 index.php 时无法加载 admin 模块 → 404
$s = isset($_GET['s']) ? (string)$_GET['s'] : '';
if ($s === '' && !empty($_SERVER['PATH_INFO'])) {
    $s = (string)$_SERVER['PATH_INFO'];
}
if ($s === '' && !empty($_SERVER['REQUEST_URI'])) {
    $p = (string)parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
    $p = preg_replace('#^/index\.php#i', '', $p);
    $p = trim(str_replace('\\', '/', $p), '/');
    if ($p !== '' && strpos($p, '.') === false) {
        $s = $p;
    }
}
$s = trim(str_replace('\\', '/', $s), '/');
if ($s !== '') {
    $parts = explode('/', $s);
    $first = strtolower($parts[0]);
    if ($first !== 'api') {
        static $faAdminOnly = null;
        if ($faAdminOnly === null) {
            $idx = [];
            foreach (glob(APP_PATH . 'index/controller/*.php') ?: [] as $f) {
                $idx[] = strtolower(basename($f, '.php'));
            }
            $apiNames = [];
            foreach (glob(APP_PATH . 'api/controller/*.php') ?: [] as $f) {
                $apiNames[] = strtolower(basename($f, '.php'));
            }
            $faAdminOnly = [];
            foreach (glob(APP_PATH . 'admin/controller/*.php') ?: [] as $f) {
                $n = strtolower(basename($f, '.php'));
                if (!in_array($n, $idx, true) && !in_array($n, $apiNames, true)) {
                    $faAdminOnly[$n] = true;
                }
            }
            foreach (glob(APP_PATH . 'admin/controller/*', GLOB_ONLYDIR) ?: [] as $dir) {
                $n = strtolower(basename($dir));
                if ($n !== '' && !in_array($n, $idx, true) && !in_array($n, $apiNames, true)) {
                    $faAdminOnly[$n] = true;
                }
            }
        }
        if (isset($faAdminOnly[$first])) {
            $qs = $_GET;
            $qs['s'] = '/' . $s;
            $qstr = http_build_query($qs);
            $host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost';
            $https = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
                || (isset($_SERVER['SERVER_PORT']) && (int)$_SERVER['SERVER_PORT'] === 443)
                || (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https');
            $scheme = $https ? 'https' : 'http';
            $dir = dirname($_SERVER['SCRIPT_NAME'] ?? '/');
            if ($dir === '\\' || $dir === '.') {
                $dir = '';
            }
            $dir = rtrim(str_replace('\\', '/', $dir), '/');
            $target = $scheme . '://' . $host . ($dir === '' ? '' : $dir) . '/OlLYSXvpwx.php';
            header('Location: ' . $target . ($qstr !== '' ? '?' . $qstr : ''), true, 302);
            exit;
        }
    }
}

// 加载框架引导文件
require __DIR__ . '/../thinkphp/start.php';
