<?php

if (!defined('ROOT'))
    exit('Can\'t Access !');
class config_admin extends admin {
    function init() {
    }
    
    function getcnzz_action(){
        $cnzz = new cnzz();
        $infos = $cnzz->getinfo();
        config::modify(array('cnzz_user' => $infos[0]));
        config::modify(array('cnzz_pass' => $infos[1]));
        front::redirect(url('config/system/set/cnzz'));
    }
    
    function system_action() {
        $type=front::get('set');
        if($type == 'template'){
        	chkpw('template_set');
        }
        if (!$type)
            $type='[^-]+';
        $source=file_get_contents(config::$path);
        $tpl_str='';
        foreach (front::$view->default_tpl_list() as $tpl) if (!preg_match('/admin/',$tpl) &&!preg_match('/\./',$tpl))
                $tpl_str.="$tpl/$tpl/";
        $tpl_str=rtrim($tpl_str,'/');
        $source=str_replace('[默认default]',"=>$tpl_str",$source);
        preg_match_all("%//$type-(\S+?)\{(.+?)//\}%s",$source,$result,PREG_PATTERN_ORDER);
        $this->view->units=$result[1];
        //var_dump($result);exit;
        foreach ($result[2] as $order=>$source2) {
            preg_match_all('%\'(\w+)\'=>\'(.*?)\',\s*//([^=\n\[]+)(\[(.+)\])?(=>(.+))?%',$source2,$result2,PREG_SET_ORDER);
            foreach ($result2 as $key=>$res) {
                $item=array();
                $item['name']=$res[1];
                $item['value']=$res[2];
                $item['title']=$res[3];
                if (isset($res[5])){
                    $item['intro']=$res[5];
                }
                if (isset($res[7]) &&!strstr($res[7],"image")) {
                    if (strstr($res[7],'$mF')){
                        $arr = array_combine(config::get('$mF'),config::get('$mF'));
                        $item['select'] = $arr;
                    }else{
                        $item['select']=url::toarray($res[7]);
                    }
                }elseif (isset($res[7]) && strstr($res[7],"image")) {
                    $item['image']=true;
                }
                $this->view->items[$order][]=$item;
            }
        }
		
		foreach (front::$view->default_tpl_list() as $tpl){
			if (preg_match('/admin/',$tpl) && !preg_match('/\./',$tpl)){
				$admin_str.="$tpl@";
			}
		}
		$admin_str = rtrim($admin_str,"@");
		$tmp = explode('@',$admin_str);
		$this->view->admintpllist = array_combine($tmp,$tmp);
        if (front::post('submit')) {
        	chkpw('system');
            if (front::post('admin_dir') &&front::post('admin_dir') != config::get('admin_dir')) {
                $new_dir=ROOT.'/'.front::post('admin_dir');
                if (ADMIN_DIRNAME != $new_dir) {
                    rename(ADMIN_DIRNAME,$new_dir);
                    if (is_dir($new_dir)) {
                        front::flash('后台目录更改成功！&nbsp;&nbsp;');
                    }
                    else
                        unset(front::$post['admin_dir']);
                }
            }
			config::modify(front::$post);
            if(front::post('cnzz_user') && front::post('cnzz_pass')){
            	$content = "user:".config::get('cnzz_user')."\tpass:".config::get('cnzz_pass')."\tdate:".date('Y-m-d H:i:s')."\n";
            	$file = ROOT.'/data/cnzz.txt';
            	$fp = fopen($file,'ab');
            	fwrite($fp,$content);
            }
            event::log('修改网站配置','成功');
            config::modify(front::$post);
            front::flash('设置成功！');
            if (!empty($new_dir) ||!front::post('submit')) {
                front::$view->sysVar();
                $new_url=front::$view->admin_url;
                front::redirect($new_url);
            }
        }
    }
    function _url($action = 'getmoinf') {
        form_admin::init();
        $this->__table=new defind($this->table);
        $pars=array(
                'action'=>$action,
                'tel'=>config::get('tel'),
                'sitename'=>config::get('sitename'),
                'site_url'=>config::get('site_url'),
                'encoding'=>'utf-8',
                'host'=>$_SERVER['HTTP_HOST'],
                'version'=>'cmseasy3.0',
                'release'=>config::get('version'),
                'os'=>PHP_OS,
                'php'=>phpversion(),
                'mysql'=>$this->__table->verison(),
                'browser'=>urlencode($_SERVER['HTTP_USER_AGENT']),
                'address'=>config::get('address'),
                'email'=>config::get('email'),
                'serverip'=>$_SERVER['REMOTE_ADDR'],
                'serverfilename'=>urlencode($_SERVER['SCRIPT_FILENAME']),
        );
        $data=http_build_query($pars);
        $verify=md5($data.config::get('email'));
        return 'http://info.cmseasy.cn/server/upgrade.php?'.$data.'&verify='.$verify;
    }

	function config_info() {
      //return '<script type="text/javascript" src="'.$this->_url('getmoinf').'"></script>';
    }
 
    function remove_action() {
    	chkpw('cache_update');
        front::remove(ROOT.'/cache/data');
        front::remove(ROOT.'/cache/template');
        $user=new user();
        $user->close_db();
        cookie::del('passinfo');
        front::flash('缓存更新成功！');
        front::redirect(front::$from);
    }

    function sms_action() {

    }
    function hottag_action(){
    	$set=settings::getInstance();

    	if(front::post('submit')){
    		$var=front::$post;
            unset($var['submit']);
            $set->rec_replace(array('value'=>serialize($var),'tag'=>'table-hottag','array'=>var_export($var,true)));
            event::log("标签修改",'成功');
            front::flash('标签修改成功！');
        }
        $sets=$set->getrow(array('tag'=>'table-hottag'));
        if(empty($sets)){
        	$set->rec_insert(array('tag'=>'table-hottag'));
        	$sets=$set->getrow(array('tag'=>'table-hottag'));
        }
        if(empty($sets['value']))
           $this->view->hottags = NULL;
        else
           $this->view->hottags =  unserialize($sets['value']);
    }

    function end() {
        $this->render('index.php');
        //echo $this->config_info();
    }
}