<?php
/**
 * 入口路由
 * - 根路径: 重定向到官网首页
 * - 静态资源: 直接放 web 目录由 nginx/apache 处理
 * 
 * 宝塔部署:
 *   网站根目录 -> /www/wwwroot/shengmufan
 *   运行目录 -> /public (推荐, 但为简单起见, 直接根目录也可)
 * 
 * 默认结构: 将整个项目根目录当作 web 根
 */
require_once __DIR__ . '/config/config.php';

$path = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH);
$path = '/' . trim($path, '/');

// 资源目录直出 (PHP-FPM 接管前可由 nginx 静态处理)
if (preg_match('#^/uploads/#', $path) && file_exists(ROOT_PATH . $path)) {
    return false; // 让 PHP 内置服务器 / FPM 透传
}

// sitemap / robots
if ($path === '/sitemap.xml') {
    require __DIR__ . '/web/sitemap.php';
    exit;
}
if ($path === '/robots.txt') {
    header('Content-Type: text/plain; charset=utf-8');
    echo cfg('robots_content', "User-agent: *\nAllow: /");
    exit;
}

// 后台 (v1.4.5: 入口改为 /fmsmf)
if (strpos($path, '/admin') === 0 || strpos($path, '/fmsmf') === 0) {
    if ($path === '/admin' || $path === '/admin/' || $path === '/fmsmf' || $path === '/fmsmf/') {
        header('Location: /fmsmf/login.php');
        exit;
    }
    // 其余后台请求由 nginx alias 转给对应 php
    // 此处已经直接访问了具体 php
}

// 前端官网首页
if ($path === '/' || $path === '/index.php' || $path === '/home') {
    require __DIR__ . '/web/index.php';
    exit;
}

// 前端内容页
if (strpos($path, '/web/') === 0 || file_exists(ROOT_PATH . '/web' . $path)) {
    $file = __DIR__ . '/web' . $path;
    if (is_file($file)) {
        require $file;
        exit;
    }
}

// 兜底: 404
http_response_code(404);
echo '<h1>404 Not Found</h1>';
