<?php
/*
+---------------------------------------------------
* APP 类 系统关键类
+---------------------------------------------------
* 进行网站标识符识别已经系统错误检查等功能
* 此类不可进行任何未经授权的修改
+---------------------------------------------------
* By ding.cn QQ:2207134109 
+---------------------------------------------------
*/
class APP
{
    public $host;
    public $thishost;
    public $siteinfo = array();
    
	
	public function __construct()
    {
        $this->host = $_SERVER['HTTP_HOST'];
        $this->thishost = cfg('URL');
    }
    public function run()
    {
        $this->key();
        $filename = root_path . '/temp/siteid/' . base64_encode($this->host) . '.tmp';
        if (!is_file($filename)) {
            $siteid = $this->getSiteId();
            if (!is_dir(root_path . '/temp/siteid')) {
                mkdir(root_path . '/temp/siteid');
            }
            file_put_contents($filename, $siteid);
        } else {
            $siteid = file_get_contents($filename);
        }
        if (!$this->getinfo($siteid)) {
            die('对应网站尚未开通');
        }
        $this -> Error($this->siteinfo);
		$this -> sitelink($siteid);
        return $siteid;
    }
    public function isSysHost()
    {
        $array = explode('|', $this->thishost);
        foreach ($array as $value) {
            if ($this->host == $value) {
                return TRUE;
            }
        }
        return FALSE;
    }
    public function getSiteId()
    {
        $site = '1000';
        if (!$this->isSysHost()) {
            $result = mysql_query("SELECT id,siteid,url,zhuanfa,go_url FROM urlzf WHERE url='{$this->host}'");
            if ($result && mysql_num_rows($result) > 0) {
                list($id, $siteid, $url, $zhuanfa, $go_url) = mysql_fetch_row($result);
                if ($zhuanfa == 'YES') {
                    header('location:' . $go_url);
                    die;
                }
                $site = $siteid;
            } else {
                die('对应的网站尚未开通(error:没有找到相关记录)!');
            }
        }
        return $site;
    }
    public function getinfo($siteid)
    {
        $sql = "SELECT id,stitle,qsid,name,site_u FROM user WHERE id='{$siteid}' AND site='YES'";
        $result = mysql_query($sql);
        if ($result && mysql_num_rows($result) > 0) {
            $this->siteinfo = mysql_fetch_array($result);
            return TRUE;
        }
        return FALSE;
    }
    public function Error($info)
    {
        if ($info['site_stop'] == '1') {
            die('抱歉，' . $_SERVER['HTTP_HOST'] . ' 已经被锁定!');
        }
        $cid = $_GET['cid'];
        $wapid = $info['id'];
        if ($cid != 0 && $cid != NULL) {
            $sql = "SELECT cid FROM class WHERE siteid='{$wapid}' AND cid='{$cid}'";
            $result = mysql_query($sql);
            if (!$result || mysql_num_rows($result) <= 0) {
                die('亲！你迷路了吧！');
            }
        }
    }
    public function key()
    {
        $key = root_path . '/' . MD5($this->thishost) . '.key';
        if (!is_file($key)) {
            //die('秘钥文件不存在!');
        }
        if (file_get_contents($key) != $this->syscode($this->thishost)) {
            //die('<a href="http://dingd.cn">请使用正版软件!尊重开发者的劳动成果就是尊重自己!</a>');
        }
    }
    public function syscode($str)
    {	
	$OOO000000=urldecode('%74%68%36%73%62%65%68%71%6c%61%34%63%6f%5f%73%61%64%66%70%6e%72');
	$OOO0000O0=$OOO000000{4}.$OOO000000{9}.$OOO000000{3}.$OOO000000{5};
	$OOO0000O0.=$OOO000000{2}.$OOO000000{10}.$OOO000000{13}.$OOO000000{16};
	$OOO0000O0.=$OOO0000O0{3}.$OOO000000{11}.$OOO000000{12}.$OOO0000O0{7}.$OOO000000{5};
		eval($OOO0000O0('JHN0ciA9IG1kNShiYXNlNjRfZW5jb2RlKG1kNSgkc3RyKSkpOw=='));
		return $str;
    }
	
	public function sitelink($wapid)
	{
	/*回链访问系统*/
		if($_GET['link']!=NULL && $_GET['link']!='')
		{
			$vurl = url_come($_GET['link']);
			$vurl = $vurl+1;
			mysql_query("UPDATE sitelink SET come='{$vurl}' WHERE urlid='{$_GET['link']}' AND siteid='{$wapid}'");
		}
	}
	
/*函数结束End*/
}