<?php

if (!defined('ROOT'))
    exit('Can\'t Access !');
class admin_system_ {
    static function _compile_($source) {
		//文件自校验部分代码开始
		include(ROOT.'/lib/admin/template_.php');
		$md5_file_check = md5_file(ROOT.'/lib/inc/view.php');
		if ($md5_file_check != $view_phpcheck){
			exit('系统文件被破坏，请勿修改系统核心文件！请重新上传系统文件或与客服联系。');
		} else { //文件自校验部分
        $tsource=$source;
        $source=@file_get_contents(ROOT.'/lib/inc/cdkey.dll');
        $start=0;
        $sources=array();
        if (!strpos($source,'s*s'))
            $sources[]=$source;
        else {
            while ($end=strpos($source,'s*s')) {
                $source=substr($source,$start,$end -$start +3);
                $sources[]=$source;
                $start=$end +3;
            }
            $sources[]=$source;
        }
        $names=array(
                front::$domain,
        );
        $pass=false;
        foreach ($sources as $source) {
            $authkey=self::_getauthkey_($source);
            $authdate=self::_getauthdate_($source);
            $authperiod=self::_getauthperiod_($source);
            if ($authdate +$authperiod <time()) {
                break;
            }
            foreach ($names as $name) {
                if ($authkey == self::md5tocdkey($source,$name)) {
                    $pass=true;
                    break;
                }
                if ($pass)
                    break;
            }
        }
        $source=$tsource;
        $domain=front::$domain;
        $soft_type=null;
        if (!$pass)
            $passinfo='免费版 <a href="http://www.cmseasy.cn/service_1.html" target="_blank"><font color="green">(购买授权)</font></a>';
        else
            $passinfo='<span id="__edition">商业版</span> <a href=http://info.cmseasy.cn/help/vip.php?domain='.$domain.'&type='.$soft_type.'" target="_blank"><font color="green">(查询服务有效期)</font></a>';
        cookie::set('passinfo',$passinfo);
        if (!$pass) {
            preg_match_all('/<title>(.*) - (.*)<\/title>/',$source,$out);
            if (!$out[2][0] ||$out[2][0] != 'Powered by CmsEasy') {
                $source=str_replace('</title>',' - Powered by CmsEasy</title>',$source);
            }
            $source=preg_replace('/<head>/i',"<head>\r\n<meta name=\"Generator\" content=\"".SYSTEMNAME.' '._VERSION."\" />",$source);
            $pos=strpos($source,'</body>');
            if ($pos === false) {
                $source=str_replace('</html>','</body></html>',$source);
            }
            else {
                $pos=strpos($source,'<a href="http://www.cmseasy.cn" title="Powered by CmsEasy" target="_blank">Powered by CmsEasy</a>');
                if ($pos === false) {
                    $source=str_replace('</head>','<a href="http://www.cmseasy.cn" title="Powered by CmsEasy" target="_blank">Powered by CmsEasy</a>',$source);
                }
            }
        }
        $source=preg_replace("/\{php\s+(.+)\}/","<?php \\1?>",$source);
        $source=preg_replace("/\{if\s+(.+?)\}/","<?php if(\\1) { ?>",$source);
        $source=preg_replace("/\{else\}/","<?php } else { ?>",$source);
        $source=preg_replace("/\{elseif\s+(.+?)\}/","<?php } elseif (\\1) { ?>",$source);
        $source=preg_replace("/\{\/if\}/","<?php } ?>",$source);
        $source=preg_replace("/\{loop\s+(\\$\w+)\s+(\S+)\}/","<?php foreach(\\1 as \\2) { ?>",$source);
        $source=preg_replace("/\{loop\s+(\\$\w+)\s+(\S+)\s+(\S+)\}/","<?php foreach(\\1 as \\2 => \\3) { ?>",$source);
        $source=preg_replace("/\{loop\s+(\S+)\s+(\S+)\}/","<?php foreach(\\1 as \\2) { ?>",$source);
        $source=preg_replace("/\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}/","<?php foreach(\\1 as \\2 => \\3) { ?>",$source);
        $source=preg_replace("/\{\/loop\}/","<?php } ?>",$source);
        return $source;
		}//文件自校验部分结束，以上代码为通过验证后的部分
	}
    static function _getauthdate_($source) {
        if (strlen($source) <0)
            return '0';
        preg_match_all('/\\:(.*):\//',$source,$outd0);
        preg_match_all('/\':(.*);\'/',$source,$outd1);
        preg_match_all('/;:(.*):\'/',$source,$outd2);
        return $outd0[1][0].$outd1[1][0].$outd2[1][0];
    }
    static function _getauthperiod_($source) {
        if (strlen($source) <0)
            return '0';
        preg_match_all('/.];(.*);]./',$source,$outp0);
        preg_match_all('/\)\)(.*)\(\(/',$source,$outp1);
        return $outp0[1][0].$outp1[1][0];
    }
    static function _getauthkey_($source) {
        if (strlen($source) <0)
            return '';
        preg_match_all('/#!=(.*)=!#/',$source,$out);
        preg_match_all('/#\$=(.*)=\$#/',$source,$out1);
        preg_match_all('/#\^=(.*)=\^#/',$source,$out2);
        preg_match_all('/#%=(.*)=%#/',$source,$out3);
        preg_match_all('/#\*=(.*)=\*#/',$source,$out4);
        preg_match_all('/#\(=(.*)=\)#/',$source,$out5);
        preg_match_all('/#\-=(.*)=\-#/',$source,$out6);
        preg_match_all('/#\?=(.*)=\?#/',$source,$out7);
        preg_match_all('/#`=(.*)`##/',$source,$out8);
        return $out[1][0].'-'.$out1[1][0].'-'.$out2[1][0].'-'.$out3[1][0].'-'.$out4[1][0].'-'.$out5[1][0].'-'.$out6[1][0].'-'.$out7[1][0].'-'.$out8[1][0];
    }
    static function md5tocdkey($source,$name) {
        $md5str=md5(gzdeflate($name));
        if (strlen($source) <0)
            return 'nocdkeymd5str';
        $str='a`b`c`d`e`f`0`1`2`3`4`5`6`7`8`9';
        preg_match_all('/\[\[=(.*)\*%/',$source,$outa);
        preg_match_all('/%%=(.*)\/=/',$source,$outa1);
        preg_match_all('/\/\/(.*)\*\*=/',$source,$outa2);
        preg_match_all('/\*\*=(.*)=\*/',$source,$outa3);
        preg_match_all('/\$%(.*)%\$/',$source,$outa4);
        preg_match_all('/\-=\-(.*)\(\)/',$source,$outa5);
        preg_match_all('/#\/(.*)\/#/',$source,$outa6);
        preg_match_all('/!%(.*)=\]\]/',$source,$outa7);
        $cdkeystr=$outa[1][0].$outa1[1][0].$outa2[1][0].$outa3[1][0].$outa4[1][0].$outa5[1][0].$outa6[1][0].$outa7[1][0];
        $srtarr=explode('`',$str);
        $cdkeyarr=explode('`',$cdkeystr);
        $cdkey='';
        for ($i=0;$i <32;$i++) {
            $md5word=substr($md5str,$i,1);
            foreach ($srtarr as $key=>$val) {
                if ($md5word == $val) {
                    foreach ($cdkeyarr as $key1=>$val1) {
                        if ($key == $key1) {
                            if ($i %4 == 0) {
                                $cdkey.=$val1.'-';
                            }
                            else {
                                $cdkey.=$val1;
                            }
                        }
                    }
                }
            }
        }
        return $cdkey;
    }
}