<?php

final class view {
    public $_var;
    public $lang=array();
    function __construct(act $act) {
        $this->_var=new stdClass();
        if (isset($act->style))
            $this->_style=$act->style;
        $this->setTemplate();
        $this->sysVar();
        new template();
        templatetag::_getVer();
    }
    function setTemplate() {
        if (front::$admin &&!front::$html) {
            $this->_style=config::get('admin_template_dir') ?config::get('admin_template_dir') : 'admin';
            $this->_tpl_ext='.php';
        }
        else {
            $this->_style=ltrim(THIS_URL,'/');
            if (!$this->_style ||front::$html)
                $this->_style=config::get('template_dir');
            $this->_tpl_ext='.html';
        }
        $this->_tpl=front::$case.'/'.front::$act.$this->_tpl_ext;
    }
    function archive_tpl_list($type='') {
        $dir=preg_replace('%\/.*%','',$type);
        $_tpls=front::scan_all(TEMPLATE.'/'.config::get('template_dir').'/'.$dir,$dir.'/');
        $tpls=array('0'=>'继承');
        foreach ($_tpls as $tpl) {
            if (preg_match('/\.htm(l)?$/',$tpl) &&!preg_match('/#/',$tpl)) {
                if ($type)
                    if (!preg_match("%^$type%",$tpl))
                        continue;
                $_tpl=str_replace('.','_',$tpl);
                $_tpl=help::tpl_name($_tpl);
                if ($_tpl)
                    $_tpl=$_tpl."($tpl)";
                else
                    $_tpl=$tpl;
                $tpls[$tpl]=$_tpl;
            }
        }
        return $tpls;
    }
    function show($string,$whole=false) {
        return $string;
    }
    function default_tpl_list() {
        return front::scan(TEMPLATE);
    }
    function myform_tpl_list() {
        $_tpls=front::scan_all(TEMPLATE.'/'.config::get('template_dir').'/myform','myform/');
        $tpls=array();
        foreach ($_tpls as $tpl) {
            if (preg_match('/\.htm(l)?$/',$tpl) &&!preg_match('/#/',$tpl)) {
                $_tpl=str_replace('.','_',$tpl);
                $_tpl=help::tpl_name($_tpl);
                if ($_tpl)
                    $_tpl=$_tpl."($tpl)";
                else
                    $_tpl=$tpl;
                $tpls[$tpl]=$_tpl;
            }
        }
        return $tpls;
    }
    function sysVar() {
        $this->base_url=config::get('base_url');
        $this->skin_path=$this->base_url.'/template/'.$this->_style.'/skin';
        $this->skin_url=$this->skin_path;
        $this->template_path=$this->base_url.'/template/'.$this->_style.'';
        $this->admin_url=config::get('base_url').'/index.php?admin_dir='.config::get('admin_dir');
        $this->roles=session::get('roles');
    }
    function fetch($tpl=null) {
        if (!$tpl &&get('spid') &&front::$case == 'table'&&front::$act == 'list') {
            $_tpl='table/special/manage.php';
            $tpl=$_tpl;
        }
        if (!$tpl &&get('spid') &&front::$case == 'table'&&front::$act == 'list') {
            $_tpl='table/spider/manage.php';
            $tpl=$_tpl;
        }
       
        if (front::$case == 'table'&&!$tpl &&preg_match('/^(htmlrule|viewcnzz|send|mail|list|add|edit|setting|show|manage)$/',front::$act) &&front::get('table') &&front::$admin &&preg_match('/^my_/',
                front::get('table'))) {
            $_tpl='myform/'.front::$act.'.php';
            $tpl=$_tpl;
        }
        elseif (front::$case == 'table'&&!$tpl &&preg_match('/^(htmlrule|viewcnzz|send|mail|list|add|edit|setting|show|manage)$/',front::$act) &&front::get('table') &&front::$admin) {
        	if (front::$get['table'] == 'category' && front::$act == 'list') {
        		if(!chkpower('category_list')){
        			return '无权限操作';
        		}
        	}
        	if (front::$get['table'] == 'archive' && front::$act == 'list') {
        		if(!chkpower('archive_list')){
        			return '无权限操作';
        		}
        	}
        	if (front::$get['table'] == 'type' && front::$act == 'list') {
        		if(!chkpower('type_list')){
        			return '无权限操作';
        		}
        	}
        	if (front::$get['table'] == 'special' && front::$act == 'list') {
        		if(!chkpower('special_list')){
        			return '无权限操作';
        		}
        	}
        	if (front::$get['table'] == 'user' && front::$act == 'list') {
        		if(!chkpower('user_list')){
        			return '无权限操作';
        		}
        	}
        	if (front::$get['table'] == 'usergroup' && front::$act == 'list') {
        		if(!chkpower('usergroup_list')){
        			return '无权限操作';
        		}
        	}
        	if (front::$get['table'] == 'orders' && front::$act == 'list') {
        		if(!chkpower('order_list')){
        			return '无权限操作';
        		}
        	}
        	if (front::$get['table'] == 'ballot' && front::$act == 'list') {
        		if(!chkpower('func_ballot_list')){
        			return '无权限操作';
        		}
        	}
        	if (front::$get['table'] == 'comment' && front::$act == 'list') {
        		if(!chkpower('func_book_pllist')){
        			return '无权限操作';
        		}
        	}
        	if (front::$get['table'] == 'guestbook' && front::$act == 'list') {
        		if(!chkpower('func_book_list')){
        			return '无权限操作';
        		}
        	}
        	if (front::$get['table'] == 'announcement' && front::$act == 'list') {
        		if(!chkpower('func_announc_list')){
        			return '无权限操作';
        		}
        	}
        	if($this->table == 'templatetag' && front::get('tagfrom') == 'define' && front::$act == 'list'){
        		if(!chkpower('templatetag_list_define')){
        			return '无权限操作';
        		}
        	}
        	if($this->table == 'templatetag' && front::get('tagfrom') == 'category' && front::$act == 'list'){
        		if(!chkpower('templatetag_list_category')){
        			return '无权限操作';
        		}
        	}
        	if($this->table == 'templatetag' && front::get('tagfrom') == 'content' && front::$act == 'list'){
        		if(!chkpower('templatetag_list_content')){
        			return '无权限操作';
        		}
        	}
        	if($this->table == 'templatetag' && front::get('tagfrom') == 'system' && front::$act == 'list'){
        		if(!chkpower('templatetag_list_system')){
        			return '无权限操作';
        		}
        	}
        	if($this->table == 'templatetag' && front::get('tagfrom') == 'function' && front::$act == 'list'){
        		if(!chkpower('templatetag_list_function')){
        			return '无权限操作';
        		}
        	}
        	if(front::$get['table'] == 'linkword' && front::$act == 'list') {
        		if(!chkpower('seo_linkword_list')){
        			return '无权限操作';
        		}
        	}
        	if(front::$get['table'] == 'friendlink' && front::$act == 'list') {
        		if(!chkpower('seo_friendlink_list')){
        			return '无权限操作';
        		}
        	}
        	
        	$_tpl='table/'.front::get('table').'/'.front::$act.'.php';
            if (file_exists(TEMPLATE.'/'.$this->_style.'/'.$_tpl)){
                $tpl=$_tpl;
            }
        }elseif(front::$case == 'stats'&&!$tpl&&front::get('table') == 'stats' && front::$act == 'list'){
        		if(!chkpower('seo_status_spider')){
        			return '无权限操作';
        		}
        }elseif(front::$case == 'field'&&!$tpl&&front::get('table') == 'archive' && front::$act == 'list'){
        		if(!chkpower('defined_field_content')){
        			return '无权限操作';
        		}
        }
        
        if (!isset($tpl))
            $tpl=$this->_tpl;
        $file=TEMPLATE.'/'.$this->_style.'/'.$tpl;
        if (!file_exists($file)) {
            exit("模板不存在：$file");
            return;
        }
        $tFile=preg_replace('/([\w-]+)\.(\w+)$/','#$1.$2',preg_replace('/\.html?$/ix','.php',$tpl));
        $cacheFile=ROOT.'/cache/template/'.$this->_style.'/'.$tFile;
        tool::mkdir(dirname($cacheFile));
        run::_time();
        if (!file_exists($cacheFile) ||filemtime($cacheFile) <filemtime($file) ||front::$admin &&!cookie::get('passinfo')) {
            $source=$this->compile(file_get_contents($file));
            file_put_contents($cacheFile,$source);
        }
        else
            $source=file_get_contents($cacheFile);
        $content=$this->_eval($source,$cacheFile);
        if (front::$admin)
            return $this->show($content);
        $rs=config::get('filter_word');
        $re=str_replace('，',',',$rs);
        $rs=explode(',',$rs);
        if (is_array($rs))
            foreach ($rs as $rp) {
                if ($rp)
                    $content=str_replace(trim($rp),config::get('filter_x'),$content);
            }
        return $this->show($content);
    }
    function render($tpl=null) {
        echo $this->fetch($tpl);
    }
    function _eval($content,$file=null) {
        foreach ($this as $var=>$value) if (!preg_match('/^_/',$var))
                $$var=$value;
        if (is_object($this->_var))
            foreach ($this->_var as $var=>$value) $$var=$value;
        ob_start();
        if ($file)
            include $file;
        else
            eval('?'.'>'.trim($content));
        $content=ob_get_contents();
        ob_end_clean();
        $this->_var=new stdClass();
        return $content;
    }
    function compile($source) {
        $source=admin_system_::_compile_($source);
        if (front::$act == 'visual') {
            $source=preg_replace('/(\{tag_([^}]+)\})/se'," '<span class=tagedit tagid='.templatetag::id('$2').'>$1</span>' ",$source);
            $source=preg_replace('/(\{js_([^}]+)\})/se'," '<span class=tagedit tagid='.templatetag::id('$2').'>$1</span>' ",$source);
        }
        else {
            $source=preg_replace('/\{tag_([^}]+)\}/s','{templatetag::tag(\'$1\')}',$source);
            $source=preg_replace('/\{js_([^}]+)\}/s','{templatetag::js(\'$1\')}',$source);
        }
        $source=preg_replace("/([\n\r]+)\t+/s","\\1",$source);
        $source=preg_replace("%\/\/\{(.+?)\}%","",$source);
        $source=preg_replace("/\{template\s+(.+)\}/","<?php echo template(\\1); ?>",$source);
        $source=preg_replace("/\{=(.+?)\}/","<?php echo \\1;?>",$source);
        $source=preg_replace("/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}/","<?php echo \\1;?>",$source);
        $source=preg_replace("/\{\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}/","<?php echo \\1;?>",$source);
        $source=preg_replace("/\{(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/","<?php echo \\1;?>",$source);
        $source=preg_replace("/\{(\\$[a-zA-Z0-9_\[\]\'\"\$\x7f-\xff]+)\}/es","\$this->addquote('<?php echo \\1;?>')",$source);
        $source=preg_replace("/\{([A-Z_\x7f-\xff][A-Z0-9_\x7f-\xff]*)\}/s","<?php echo \\1;?>",$source);
        $source=preg_replace("/\{\\$([a-zA-Z_]+)\.([a-zA-Z_]+)\}/s","<?php echo \$\\1['\\2'];?>",$source);
        $source=preg_replace("/\{\\$([a-zA-Z_]+)\.(\\$[a-zA-Z_]+)\}/s","<?php echo \$\\1[\\2];?>",$source);
        $source=preg_replace("/\{\\$([a-zA-Z_]+)\.(\\$[a-zA-Z_]+)\.([a-zA-Z_]+)\}/s","<?php echo \$\\1[\\2]['\\3'];?>",$source);
        $source=preg_replace('/\{(\\$[a-zA-Z_]+)\.(\\$[a-zA-Z_]+)\|([^,}]+)(.*?)\}/i','<?php echo \\3(\\1[\\2]\\4);?>',$source);
        $source=preg_replace('/\{(\\$[a-zA-Z_]+)\.([a-zA-Z_]+)\|([^,}]+)(.*?)\}/i',"<?php echo \\3(\\1['\\2']\\4);?>",$source);
        $source=preg_replace('/\{(\\$[a-zA-Z_]+)\|([^,}]+)(.*?)\}/i',"<?php echo \\2(\\1\\3);?>",$source);
        $source=preg_replace("/\\$([a-zA-Z0-9_]+)\[([a-zA-Z0-9_]+)\]/s","\$\\1['\\2']",$source);
        $source="<?php defined('ROOT') or exit('Can\'t Access !'); ?>\r\n".$source;
        return $source;
	}
    function addquote($var) {
        return str_replace("\\\"","\"",preg_replace("/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s","['\\1']",$var));
    }
}