<?php
// ==========================================
// 🚀 泛目录智能解析引擎 (完美拟合所有严格路由版)
// ==========================================
if (isset($_GET['mac_wd'])) {
    $wd = $_GET['mac_wd'];

    // 1. 强行清空原始请求，让系统完全听我们的
    $_SERVER['PATH_INFO'] = '';
    $_SERVER['REQUEST_URI'] = '';

    if (isset($_GET['mac_actor'])) {
        // 【核心修复】：明星页伪装 -> 完美还原 vodsearch 的 13 个短横线！
        $actor_enc = urlencode($wd);
        $search_arr = [
            '',         // [0] <wd> 关键字
            $actor_enc, // [1] <actor> 演员 (把周星驰放这里)
            '',         // [2] <area> 地区
            '',         // [3] <by> 排序
            '',         // [4] <class> 剧情
            '',         // [5] <director> 导演
            '',         // [6] <lang> 语言
            '',         // [7] <letter> 字母
            '',         // [8] <level> 推荐
            '',         // [9] <order> 排序方式
            '',         // [10] <page> 页码
            '',         // [11] <state> 状态
            '',         // [12] <tag> 标签
            ''          // [13] <year> 年份
        ];
        // 拼接成 /vodsearch/-zhouxingchi------------.html
        $_SERVER['PATH_INFO'] = '/vodsearch/' . implode('-', $search_arr) . '.html';
        
    } else {
        // 2. 智能解析长尾词
        $id = ''; $year = ''; $class = '';

        if (preg_match('/(20[0-2][0-9])/', $wd, $matches)) { $year = $matches[1]; }
        if (strpos($wd, 'dianying') !== false) { $id = '1'; }
        if (strpos($wd, 'dianshiju') !== false || strpos($wd, 'lianxuju') !== false) { $id = '2'; }
        
        if (strpos($wd, 'dongzuo') !== false) { $id = '1'; $class = '动作'; }
        if (strpos($wd, 'aiqing') !== false)  { $id = '1'; $class = '爱情'; }
        if (strpos($wd, 'kehuan') !== false)  { $id = '1'; $class = '科幻'; }
        if (strpos($wd, 'xiju') !== false)    { $id = '1'; $class = '喜剧'; }
        if (strpos($wd, 'kongbu') !== false)  { $id = '1'; $class = '恐怖'; }
        if (strpos($wd, 'juqing') !== false)  { $id = '1'; $class = '剧情'; }

        if ($id != '') {
            // 完美还原 vodshow 的 11 个短横线
            $class_enc = $class ? urlencode($class) : '';
            $year_enc = $year ? urlencode($year) : '';
            
            $route_arr = [
                $id, '', '', $class_enc, '', '', '', '', '', '', '', $year_enc
            ];
            $_SERVER['PATH_INFO'] = '/vodshow/' . implode('-', $route_arr) . '.html';
            
        } else {
            // 如果没匹配到任何分类，按纯关键字搜索（也是 13 个短横线，关键字放第一位）
            $wd_enc = urlencode($wd);
            $search_arr = [
                $wd_enc, '', '', '', '', '', '', '', '', '', '', '', '', ''
            ];
            $_SERVER['PATH_INFO'] = '/vodsearch/' . implode('-', $search_arr) . '.html';
        }
    }
    
    // 覆盖全局请求，彻底骗过 TP5 路由器
    $_SERVER['REQUEST_URI'] = $_SERVER['PATH_INFO'];
    
    // 销毁我们的暗号
    unset($_GET['mac_wd'], $_GET['mac_actor']);
}
// ==========================================

// ↓↓↓ 以下是你 index.php 原本的代码，千万别动它 ↓↓↓
define('MAC_APP_PATH', __DIR__ . '/application/');
/*
'软件名称：苹果CMS 源码库：https://github.com/magicblack
'--------------------------------------------------------
'Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
'遵循Apache2开源协议发布，并提供免费使用。
'--------------------------------------------------------
*/
header('Content-Type:text/html;charset=utf-8');
// 检测PHP环境
if(version_compare(PHP_VERSION,'5.5.0','<'))  die('PHP版本需要>=5.5，请升级【PHP version requires > = 5.5，please upgrade】');
//超时时间
@ini_set('max_execution_time', '0');
//内存限制 取消内存限制
@ini_set("memory_limit",'-1');
// 定义应用目录
define('ROOT_PATH', __DIR__ . '/');
define('APP_PATH', __DIR__ . '/application/');
define('MAC_COMM', __DIR__.'/application/common/common/');
define('MAC_HOME_COMM', __DIR__.'/application/index/common/');
define('MAC_ADMIN_COMM', __DIR__.'/application/admin/common/');
define('MAC_START_TIME', microtime(true) );
define('BIND_MODULE','index');
define('ENTRANCE', 'index');
$in_file = rtrim($_SERVER['SCRIPT_NAME'],'/');
if(substr($in_file,strlen($in_file)-4)!=='.php'){
    $in_file = substr($in_file,0,strpos($in_file,'.php')) .'.php';
}
define('IN_FILE',$in_file);
if(!is_file('./application/data/install/install.lock')) {
    header("Location: ./install.php");
    exit;
}
if (!@mb_check_encoding($_SERVER['PATH_INFO'], 'utf-8')){
    $_SERVER['PATH_INFO']=@mb_convert_encoding($_SERVER['PATH_INFO'], 'UTF-8', 'GBK');
}

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

