<?php
error_reporting(0);
ini_set('display_errors', 0);
@set_time_limit(0);

define('IN_CRONLITE', true);
define('SYSTEM_ROOT', dirname(__FILE__).'/');
define('ROOT', dirname(dirname(__FILE__)).'/');
define('SYS_KEY', 'BT_HOST_SYSTEM');
define('CC_Defender', false);

date_default_timezone_set('PRC');

if(file_exists(SYSTEM_ROOT.'360safe/360webscan.php')){
    include_once SYSTEM_ROOT.'360safe/360webscan.php';
}

// 设置session cookie参数，确保跨目录共享
if (session_status() === PHP_SESSION_NONE) {
    ini_set('session.cookie_path', '/');
    ini_set('session.cookie_domain', '');
    ini_set('session.cookie_secure', false);
    ini_set('session.cookie_httponly', true);
    ini_set('session.cookie_samesite', 'Lax');
    session_start();
}

$siteurl = 'http://'.$_SERVER['HTTP_HOST'];

if(!file_exists(ROOT.'config.php')){
    header('Location: /install/');
    exit;
}

$config = @include(ROOT.'config.php');

if(!$config || !is_array($config)){
    header('Location: /install/');
    exit;
}

include_once SYSTEM_ROOT.'db.class.php';

$DB = new db($config['host'], $config['user'], $config['pwd'], $config['dbname'], $config['port']);
$DB->connect();

$conf = $DB->get_row("SELECT * FROM `config` WHERE `id`=1");

if(!$conf){
    header('Location: /install/');
    exit;
}

$alipay_config = array(
    'partner' => isset($conf['hxr']) ? $conf['hxr'] : '',
    'key' => isset($conf['hxt']) ? $conf['hxt'] : '',
    'apiurl' => isset($conf['hxe']) ? $conf['hxe'] : '',
    'notify_url' => $siteurl.'/user/notify_url.php',
    'return_url' => $siteurl.'/user/return_url.php',
    'input_charset' => 'UTF-8',
    'transport' => 'http',
    'sign_type' => 'MD5'
);

include_once SYSTEM_ROOT.'function.php';
include_once SYSTEM_ROOT.'member.php';
include_once SYSTEM_ROOT.'security.php';
include_once SYSTEM_ROOT.'json_helper.php';
include_once ROOT.'class.php';

if(CC_Defender){
    cc_defender();
}

/**
 * 检查管理员邮箱验证
 * 如果开启了邮箱验证且未验证，则跳转到验证页面
 */
function check_admin_email_verify() {
    global $conf;
    
    // 获取当前脚本路径
    $current_script = basename($_SERVER['SCRIPT_NAME']);
    $current_dir = basename(dirname($_SERVER['SCRIPT_NAME']));
    
    // 只在admin目录下检查，且排除verify.php和login.php
    if ($current_dir !== 'admin' || in_array($current_script, ['verify.php', 'login.php', 'ajax.php'])) {
        return;
    }
    
    // 检查是否已登录
    if (!is_admin_login()) {
        return;
    }
    
    // 检查是否开启了邮箱验证
    if (!isset($conf['admin_email_verify']) || $conf['admin_email_verify'] !== 'true') {
        return;
    }
    
    // 检查是否设置了管理员邮箱
    if (empty($conf['admin_email'])) {
        return;
    }
    
    // 检查是否已通过验证
    if (isset($_SESSION['admin_email_verified']) && $_SESSION['admin_email_verified'] === true) {
        return;
    }
    
    // 未验证，跳转到验证页面
    header('Location: verify.php');
    exit;
}

// 执行邮箱验证检查
check_admin_email_verify();

$password_hash = isset($conf['password_hash']) ? $conf['password_hash'] : 'bt_host_system_2026';

$lang = array();
$lang_code = 'zh_CN';

if (isset($_COOKIE['lang'])) {
    $lang_code = $_COOKIE['lang'];
} elseif (isset($_SESSION['lang'])) {
    $lang_code = $_SESSION['lang'];
}

$lang_file = ROOT . 'lang/' . $lang_code . '.php';
if (file_exists($lang_file)) {
    include_once $lang_file;
}

// 加载语言扩展文件
$lang_ext_file = ROOT . 'lang/' . $lang_code . '_extensions.php';
if (file_exists($lang_ext_file)) {
    include_once $lang_ext_file;
}

function L($key, $category = 'common') {
    global $lang;
    if (isset($lang[$category][$key])) {
        return $lang[$category][$key];
    }
    return $key;
}

function setLanguage($lang_code) {
    $allowed_langs = array('zh_CN', 'en_US');
    if (in_array($lang_code, $allowed_langs)) {
        $_SESSION['lang'] = $lang_code;
        setcookie('lang', $lang_code, time() + 86400 * 30, '/');
        return true;
    }
    return false;
}
?>
