<?php
ini_set("display_errors", "On"); 
error_reporting(E_ALL | E_STRICT);
//echo phpinfo();die;
include("./data/config.inc.php");

include("./data/db.php");

include("./global/db.inc.php");

include("./global/session.class.php");

$po = $_SERVER['SERVER_PORT'];

// 写死手机端和后台的域名
$mobile_domain = 'h5.138.138.ptuscq.com';
$admin_domain = 'admin.138.138.ptuscq.com';

// 判断当前请求的域名
if (substr($po, -2) == '01') {
    header("Location:/login");
    exit;
}

$url  = $_SERVER['SERVER_NAME'];
$msql->query("select logincode,loginfs from `$tb_config`");
$msql->next_record();
$loginfs = $msql->f('loginfs');

if ($msql->f('logincode') == 1) {
    $mobi = $_REQUEST['mobi'];
    $type = base64_decode(substr($_REQUEST['type'], 5));
    $type = substr($type, 0, 1);

    if ($_SESSION['login'] == 1 && $type != '') {
        if ($loginfs == 'dk') {
            $msql->query("select mdi,udi,adi,hdi,mpo,upo,apo,hpo,wid from `$tb_web` where upo='$po' or apo='$po' or hpo='$po' limit 1");
        } else {
            $msql->query("select mdi,udi,adi,hdi,murl,uurl,aurl,hurl,wid from `$tb_web` where uurl='$url' or aurl='$url' or hurl='$url' limit 1");
        }
        $msql->next_record();
        $_SESSION['wid'] = $msql->f('wid');

        if ($mobi == 1) {
            $_SESSION['mobi'] = 1;
            $floder = $msql->f('mdi');
            header("Location:$floder/?com=" . rand(1000, 9999));
            exit;
        } else if ($type == 'u') {
            unset($_SESSION['mobi']);
            $floder = $msql->f('udi');
        } else if ($type == 'a') {
            unset($_SESSION['mobi']);
            $floder = $msql->f('adi');
        } else {
            echo "<script language='javascript'>window.location.href='http://baidu.com/s?wd=abc';</script>";
            exit;
        }

        echo "
        <html><head><title></title><meta http-equiv='Content-Type' content='text/html; charset=utf-8'></head><frameset rows='0,*' frameborder='NO' border='0' framespacing='0'>
        <frame src='about:blank' name='topFrame' scrolling='NO' noresize ><frame src='$floder/?com=" . rand(1000, 9999) . "' name='indexFrame'></frameset><noframes><body></body></noframes></html>";
        exit;
    }

    if ($_POST['code']) {
        $code = $_POST['code'];

        if ($loginfs == 'dk') {
            $msql->query("select mdi,udi,adi,hdi,mpo,upo,apo,hpo,wid,webname,ucode,acode,hcode from `$tb_web` where upo='$po' or apo='$po' or hpo='$po' limit 1");
        } else {
            $msql->query("select mdi,udi,adi,hdi,murl,uurl,aurl,hurl,wid,webname,ucode,acode,hcode from `$tb_web` where uurl='$url' or aurl='$url' or hurl='$url' limit 1");
        }
        $msql->next_record();

        if ($loginfs == 'dk') {
            if ($po == $msql->f('hpo') && $code == $msql->f('hcode')) {
                $_SESSION['login'] = 1;
                $_SESSION['wid']   = 100;
                $floder            = $msql->f('hdi');
                echo "
                <html><head><title></title><meta http-equiv='Content-Type' content='text/html; charset=utf-8'></head><frameset rows='0,*' frameborder='NO' border='0' framespacing='0'>
                <frame src='about:blank' name='topFrame' scrolling='NO' noresize ><frame src='$floder/?com=" . rand(1000, 9999) . "' name='indexFrame'></frameset><noframes><body></body></noframes></html>";
                exit;
            } else if (($po == $msql->f('apo') && $code == $msql->f('acode')) || ($po == $msql->f('upo') && $code == $msql->f('ucode'))) {
                $_SESSION['login'] = 1;
                if (ismobi()) {
                    include("navmobi.php");
                } else {
                    include("navcomputer.php");
                }
            } else {
                echo "<script language='javascript'>window.location.href='http://baidu.com/?wd=" . $code . "';</script>";
                exit;
            }
        } else if ($loginfs == 'url') {
            if ($url == $msql->f('hurl') && $code == $msql->f('hcode')) {
                $_SESSION['login'] = 1;
                $_SESSION['wid']   = 100;
                $floder            = $msql->f('hdi');
                echo "
                <html><head><title></title><meta http-equiv='Content-Type' content='text/html; charset=utf-8'></head><frameset rows='0,*' frameborder='NO' border='0' framespacing='0'>
                <frame src='about:blank' name='topFrame' scrolling='NO' noresize ><frame src='$floder/?com=" . rand(1000, 9999) . "' name='indexFrame'></frameset><noframes><body></body></noframes></html>";
                exit;
            } else if (($url == $msql->f('aurl') && $code == $msql->f('acode')) || ($url == $msql->f('uurl') && $code == $msql->f('ucode'))) {
                $_SESSION['login'] = 1;
                if (ismobi()) {
                    include("navmobi.php");
                } else {
                    include("navcomputer.php");
                }
            } else {
                echo "<script language='javascript'>window.location.href='http://baidu.com/?wd=" . $code . "';</script>";
                exit;
            }
        } else {
            echo "<script language='javascript'>window.location.href='http://baidu.com/s?wd=" . $code . "';</script>";
            exit;
        }
    } else {
        include("./nav.php");
    }
} else {
    if ($loginfs == 'dk') {
        $msql->query("select mdi,udi,adi,hdi,mpo,upo,apo,hpo,wid from `$tb_web` where upo='$po' or apo='$po' or hpo='$po' limit 1");
    } else {
        $msql->query("select mdi,udi,adi,hdi,murl,uurl,aurl,hurl,wid from `$tb_web` where uurl='$url' or aurl='$url' or hurl='$url' limit 1");
    }
    $msql->next_record();

    if ($loginfs == 'dk') {
        if ($po == $msql->f('upo')) {
            $_SESSION['wid'] = $msql->f('wid');
            if (ismobi()) {
                $floder = $msql->f('mdi');
                $_SESSION['mobi'] = 1;
                header("Location:/creditmobile/login");
                exit;
            } else {
                $floder = $msql->f('udi');
            }
        } else if ($po == $msql->f('apo')) {
            $_SESSION['wid'] = $msql->f('wid');
            $floder = $msql->f('adi');
        } else if ($po == $msql->f('hpo')) {
            $_SESSION['wid'] = 100;
            $floder = $msql->f('hdi');
        } else {
            echo "<script>window.location.href='http://google.com';</script>";
            exit;
        }
    } else {
        if ($url == $msql->f('uurl')) {
            $_SESSION['wid'] = $msql->f('wid');
            if (ismobi()) {
                $floder = $msql->f('mdi');
                $_SESSION['mobi'] = 1;
                header("Location:/creditmobile/login");
                exit;
            } else {
                $floder = $msql->f('udi');
            }
        } else if ($url == $msql->f('aurl')) {
            $_SESSION['wid'] = $msql->f('wid');
            $floder = $msql->f('adi');
        } else if ($url == $msql->f('hurl')) {
            $_SESSION['wid'] = 100;
            $floder = $msql->f('hdi');
        } else {
            echo "<script>window.location.href='http://google.com';</script>";
            exit;
        }
    }

    if ($_REQUEST['wai'] == 'code') {
        header("Location:login.php");
    } else if ($_REQUEST['wai'] == 'guest') {
        header("Location:guest.php");
    } else {
        header("Location:$floder/login.php");
    }
    exit;
}

function ismobi()
{
    if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
        return true;
    }
    if (isset($_SERVER['HTTP_VIA'])) {
        return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
    }
    if (isset($_SERVER['HTTP_USER_AGENT'])) {
        $clientkeywords = array(
            'nokia', 'sony', 'ericsson', 'mot', 'samsung', 'htc', 'sgh', 'lg', 'sharp', 
            'sie-', 'philips', 'panasonic', 'alcatel', 'lenovo', 'iphone', 'ipod', 'blackberry',
            'meizu', 'android', 'netfront', 'symbian', 'ucweb', 'windowsce', 'palm', 'operamini',
            'operamobi', 'openwave', 'nexusone', 'cldc', 'midp', 'wap', 'mobile'
        );
        if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
            return true;
        }
    }
    return false;
}
?>
