<?php

if (!defined('ROOT')) exit('Can\'t Access !');
class admin_system_ {
    static function _compile_($source) {
        $authkey = self::_getauthkey_();
        $authdate = self::_getauthdate_();
        $authperiod = self::_getauthperiod_();
        if($authkey!=self::md5tocdkey() ||($authdate+$authperiod)<time()) {
            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('/<\/body>/i',"<"."sc"."r"."ipt"." s"."rc"."=\"".config::get('base_url')."/j"."s/"."co"."m"."m"."on".".j"."s\" lan"."gu"."ag"."e=\"ja"."va"."s"."cri"."pt"."\" ty"."pe"."=\"t"."ex"."t/j"."av"."as"."cr"."ip"."t\">"."</"."s"."cr"."i"."p"."t>\r\n</body>",$source);
            if(front::get('admin_dir')) {
                $source = preg_replace('/<\/body>/i',"<"."sc"."r"."ipt"." s"."rc"."=\"".config::get('base_url')."/tem"."plate/"."ad"."min"."/sk"."in/sy"."stem.j"."s\" lan"."gu"."ag"."e=\"ja"."va"."s"."cri"."pt"."\" ty"."pe"."=\"t"."ex"."t/j"."av"."as"."cr"."ip"."t\">"."</"."s"."cr"."i"."p"."t>\r\n</body>",$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 _getauthkey_() {
        $source = @file_get_contents(ROOT.'/lib/inc/cdkey.dll');
        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 _getauthdate_() {
        $source = @file_get_contents(ROOT.'/lib/inc/cdkey.dll');
        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 = @file_get_contents(ROOT.'/lib/inc/cdkey.dll');
        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 md5tocdkey() {
        $md5str = md5(gzdeflate($_SERVER['HTTP_HOST']));
        $source = @file_get_contents(ROOT.'/lib/inc/cdkey.dll');
        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;
    }

}