<?php
//前台入口文件
define('IN_T', true);
require 'source/include/init.php';

// 统一追加公安备案信息，避免不同模板页脚不一致导致前台不显示
ob_start(function ($html) {
    $policeBeian = '<a href="https://beian.mps.gov.cn/#/query/webSearch?code=42011302001079" rel="noreferrer" target="_blank" style="color:#ccc;margin-left:8px;display:inline-flex;align-items:center;gap:4px;text-decoration:none;"><img src="https://www.beian.gov.cn/img/new/gongan.png" alt="公安备案图标" style="width:16px;height:16px;vertical-align:middle;border:0;"><span>鄂公网安备42011302001079号</span></a>';

    if (strpos($html, '42011302001079') !== false) {
        return $html;
    }

    if (strpos($html, '{$_lang.icp}') !== false) {
        return str_replace('{$_lang.icp}', '{$_lang.icp} ' . $policeBeian, $html);
    }

    if (preg_match('/(<a[^>]+beian\.miit\.gov\.cn[^>]*>.*?<\/a>)/is', $html)) {
        return preg_replace('/(<a[^>]+beian\.miit\.gov\.cn[^>]*>.*?<\/a>)/is', '$1 ' . $policeBeian, $html, 1);
    }

    if (preg_match('/(鄂ICP备[^<\s]*)/u', $html)) {
        return preg_replace('/(鄂ICP备[^<\s]*)/u', '$1 ' . $policeBeian, $html, 1);
    }

    return str_ireplace('</body>', '<div style="text-align:center;padding:10px 0;background:#333;font-size:12px;">' . $policeBeian . '</div></body>', $html);
});

//请求模块
$ismobile = isMobile();
if ($ismobile) {
    //$site_url = strtolower('http://'.$_SERVER['HTTP_HOST'].'/mobile/index');
    //@header('Location: '.$site_url);
}
$module = isset($_REQUEST['module']) ? Common::sfilter($_REQUEST['module']) : 'index';
$newIndex = 'N';
if (isset($_REQUEST['module']) === false) {
    $newIndex = 'Y';
}

//查询导航链接
$navlinkParentList = $Db->query("SELECT * FROM " . $Base->table('navlink') . " where nav_parent_id=0");
foreach ($navlinkParentList as $navKey => $navInfo) {
    $navlinkChildList = $Db->query("SELECT * FROM " . $Base->table('navlink') . " where nav_parent_id=" . $navInfo['nav_id']);
    if (count($navlinkChildList) > 0) {
        $navlinkParentList[$navKey]['child_list'] = $navlinkChildList;
    }
}

$tp->assign('navlinkParentList', $navlinkParentList);

//前台的module模块在模板目录下
if (file_exists($module_file = 'template/' . $_lang['moban'] . '/module/' . $module . '.php')) {
    require $module_file;
    $tp->assign('module', $module);

    if ($newIndex === 'Y') {
        // $tp->display($_lang['moban'].'/new_index.tpl');
        $tp->display($_lang['moban'] . '/index.tpl');
    } else {
        $tp->display($_lang['moban'] . '/index.tpl');
    }
} else {
    die('hacking attempt');
}
function isMobile()
{
    $useragent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
    $useragent_commentsblock = preg_match('|\(.*?\)|', $useragent, $matches) > 0 ? $matches[0] : '';
    function CheckSubstrs($substrs, $text)
    {
        foreach ($substrs as $substr)
            if (false !== strpos($text, $substr)) {
                return true;
            }
        return false;
    }

    $mobile_os_list = array('Google Wireless Transcoder', 'iPad', 'Windows CE', 'WindowsCE', 'Symbian', 'Android', 'armv6l', 'armv5', 'Mobile', 'CentOS', 'mowser', 'AvantGo', 'Opera Mobi', 'J2ME/MIDP', 'Smartphone', 'Go.Web', 'Palm', 'iPAQ');
    $mobile_token_list = array('Profile/MIDP', 'Configuration/CLDC-', '160×160', '176×220', '240×240', '240×320', '320×240', 'UP.Browser', 'UP.Link', 'SymbianOS', 'PalmOS', 'PocketPC', 'SonyEricsson', 'Nokia', 'BlackBerry', 'Vodafone', 'BenQ', 'Novarra-Vision', 'Iris', 'NetFront', 'HTC_', 'Xda_', 'SAMSUNG-SGH', 'Wapaka', 'DoCoMo', 'iPhone', 'iPod');
    $found_mobile = CheckSubstrs($mobile_os_list, $useragent_commentsblock) || CheckSubstrs($mobile_token_list, $useragent);
    if ($found_mobile) {
        return true;
    } else {
        return false;
    }
}

?>
