<?php
/**
 * Copyright (C) LJCMS All rights reserved.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2016.03.19
 */
class control extends adminbase {
    
    public function action_run() {
        $this->checkAuth('run');
        $model = parent::model('setting', 'am');
        $optionname='site_base';
        $lang= parent::$lang;   
            // echo $lang;
        if($lang!='cn'){
            $optionname=$optionname."_".$_SESSION['lang'];
        }
   
        $data = $model->getOptions($optionname);
        unset($model);
        $var_array = array();
        if (!empty($data)) {
            parent::loadLib('mod');
            $data['tjcode'] = stripslashes($data['tjcode']);
            $data['timezone_select'] = XMod::selectTimezone($data['timezone'], 'timezone');
            $var_array = $data;
        }
        TPL::assign($var_array);
        TPL::display($this->cptpl.'setting.tpl');
	}
    
    
    public function action_footer() {
        $this->checkAuth('footer');
        $model = parent::model('setting', 'am');
        $data = $model->getOptions('site_footer');
        unset($model);
        $var_array = array('content'=>stripslashes($data));
        TPL::assign($var_array);
        TPL::display($this->cptpl.'setting.tpl');
    }
    
    
    public function action_rewrite() {
         $this->checkAuth('rewrite');
        $model = parent::model('setting', 'am');
        $data = $model->getOptions('site_rewrite');
        unset($model);
        
        $model = parent::model('index', 'am');
        $sysdata = $model->doGetSysData();
        unset($model);
        
         $var_array = $data;
        if (is_array($var_array)) {
            $var_array = array_merge($var_array, array('sysdata'=>$sysdata));
        }
        
        TPL::assign($var_array);
        TPL::display($this->cptpl.'setting.tpl');
    }
    
    
    public function action_upload() {
            $this->checkAuth('upload');
        $php_upload_maxsize = ini_get('upload_max_filesize');
        
        if (function_exists("imagecreate")){
        	if(function_exists('gd_info')){
        		$gd_ver_info = gd_info();
        		$gd_ver = $gd_ver_info['GD Version'];
        	}else{
        		$gd_ver = '支持';
        	}
        }else{
        	$gd_ver = '不支持';
        }
        $var_array = array();
        parent::loadLib('option');
        $data = XOption::get('upload_config');
        if (!empty($data)) {
            $var_array = $data;
        }
        $var_array = array_merge($var_array, 
            array(
            'php_upload_maxsize'=>$php_upload_maxsize, 
            'gd_version'=>$gd_ver
        ));
        TPL::assign($var_array);
        TPL::display($this->cptpl.'setting.tpl');       
    }
    
    
    
    public function action_index_style() {
              $this->checkAuth('index_style');
        $model = parent::model('setting', 'am');
        $data = $model->getOptions('index_style');
        unset($model);
        $var_array = array();
        if (!empty($data)) {
            $var_array = $data;
        }
        TPL::assign($var_array);
        TPL::display($this->cptpl.'setting.tpl');
    }
    
    public function action_main_style() {
        $this->checkAuth('main_style');
        $model = parent::model('setting', 'am');
        $data = $model->getOptions('main_style');
        unset($model);
        $var_array = array();
        if (!empty($data)) {
            $var_array = $data;
        }
        TPL::assign($var_array);
        TPL::display($this->cptpl.'setting.tpl');
    }
    
    
    
    public function action_savebase() {
        $this->checkAuth('savebase');
        $args = array();
        $args = $this->_validBase();
        $model = parent::model('setting', 'am');
      //  print_r($args);die;
                $optionname='site_base';
        $lang= parent::$lang;      
        if($lang!='cn'){
            $optionname=$optionname."_".$_SESSION['lang'];
        }
        $tjcode=$args['tjcode'];
        unset($args['tjcode']);
        $result = $model->doSave($optionname, $args,$tjcode);
        unset($model);
        if (true === $result) {
            $this->log('setting', '站点设置', 1);
            XHandle::halt('保存成功', $this->cpfile.'?c=setting', 0);
        }
        else {
            $this->log('setting', '站点设置', 0);
            XHandle::halt('保存失败', '', 1);
        }  
    }
    
    
    public function action_savefooter() {
        $this->checkAuth('savefooter');
        $server_name=$_SERVER['SERVER_NAME'];
        $content = $this->_validFooter();
        $sql = "SELECT * FROM ".DB_PREFIX."options WHERE optionname='author'";
        $data= parent::$obj->fetch_first($sql);
        $code=$data['optionvalue'];
        $url="https://www.8cms.cn/api.php?a=is_code&code=$code";
        $curl = curl_init(); // 启动一个CURL会话
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_HEADER, 0);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);  
            // 从证书中检查SSL加密算法是否存在
            $tmpInfo = curl_exec($curl);     //返回api的json对象
            //关闭URL请求
            curl_close($curl);
        $res1= json_decode($tmpInfo,TRUE);    
        $is_author = $res1['slay'];
        if($is_author!='1'){
          //   print_r($res1['data']);die;
             $needle ='LJCMS';//判断是否包含a这个字符
             $tmparray = explode($needle,$content);
             if(count($tmparray)>1){
                    $content =$content;
             } else{
                   $content =$content."  Powered by "."<a href='http://www.8cms.cn/' target='_blank'>LJCMS</a>";
             }

        }elseif ($server_name!=$res1['data']['domain']) {
             $needle ='LJCMS';//判断是否包含a这个字符
             $tmparray = explode($needle,$content);
             if(count($tmparray)>1){
                    $content =$content;
             } else{
                   $content =$content."  Powered by "."<a href='http://www.8cms.cn/' target='_blank'>LJCMS</a>";
             }           
        }
        $model = parent::model('setting', 'am');
        $result = $model->doUpdate('site_footer', $content);

        unset($model);
        if (true === $result) {
            $this->log('setting', '底部信息', 1);
            XHandle::halt('保存成功', $this->cpfile.'?c=setting&a=footer', 0);
        }else {
            $this->log('setting', '底部信息', 0);
            XHandle::halt('保存失败', '', 1);
        }     
    }
    
    
    public function action_saverewrite() {
         $this->checkAuth('saverewrite');
        list($urlsuffix, $rewriterule) = $this->_validRewrite();
        
        $model = parent::model('setting', 'am');
        $result = $model->doSave('site_rewrite', array('urlsuffix'=>$urlsuffix, 'rewriterule'=>$rewriterule),'');
        if (true === $result) {
            
            if ($urlsuffix == 'php') {
                $rewriterule = '';
            }
            $model->doRewrite($rewriterule);
            $this->log('setting', '伪静态设置', 1);
            XHandle::halt('保存成功', $this->cpfile.'?c=setting&a=rewrite', 0);
        }
        else {
            $this->log('setting', '伪静态设置', 0);
            XHandle::halt('保存失败', '', 1);
        }   
        unset($model);  
    }
    
    
    public function action_saveupload() {
         $this->checkAuth('saveupload');
        $args = array();
        $args = $this->_validUpload();
        $model = parent::model('setting', 'am');
        $result = $model->doSave('upload_config', $args,'');
        unset($model);
        if (true === $result) {
            $this->log('setting', '图片设置', 1);
            XHandle::halt('保存成功', $this->cpfile.'?c=setting&a=upload', 0);
        }
        else {
            $this->log('setting', '图片设置', 0);
            XHandle::halt('保存失败', '', 1);
        }
    }
    
    
    
    public function action_save_index_style() {
         $this->checkAuth('save_index_style');
        $args = array();
        $args = $this->_validIndexStyle();
        $model = parent::model('setting', 'am');
        $result = $model->doSave('index_style', $args,'');
        unset($model);
        if (true === $result) {
            $this->log('setting', '首页配置', 1);
            XHandle::halt('保存成功', $this->cpfile.'?c=setting&a=index_style', 0);
        }
        else {
            $this->log('setting', '首页配置', 0);
            XHandle::halt('保存失败', '', 1);
        }  
    }
    
    public function action_save_main_style() {
         $this->checkAuth('save_main_style');
        $args = array();
        $args = $this->_validMainStyle();
        $model = parent::model('setting', 'am');
        $result = $model->doSave('main_style', $args,'');
        unset($model);
        if (true === $result) {
            $this->log('setting', '界面配置', 1);
            XHandle::halt('保存成功', $this->cpfile.'?c=setting&a=main_style', 0);
        }
        else {
            $this->log('setting', '界面配置', 0);
            XHandle::halt('保存失败', '', 1);
        }  
    }
    
    
    public function action_clearcache() {
        TPL::clearComplied();
        $type=$_REQUEST['type'];
        if($type==1){
            XHandle::halt('清除成功', $this->cpfile.'?c=ui_set', 0);
        }else{
            XHandle::halt('清除成功', $this->cpfile.'?c=setting', 0);
        }

    }
    
    
    public function action_updatecache() {
        $model = parent::model('setting', 'am');
        $model->doUpdateCache();
        unset($model);
        XHandle::halt('更新成功', $this->cpfile.'?c=setting', 0);
    }

    
    public function action_wap() {
        $this->checkAuth('wap');
        TPL::display($this->cptpl."setting.tpl");
    }
    
    public function action_savewap() {
        $this->checkAuth('savewap');
        $args = $this->_validWap();
       
        $result = M("setting", "am")->doSave("wap_config", $args,'');
        if (true === $result) {
            $this->log('setting', 'WAP手机版设置', 1);
            XHandle::halt('保存成功', $this->cpfile.'?c=setting&a=wap', 0);
        }
        else {
            $this->log('setting', 'WAP手机版设置', 0);
            XHandle::halt('保存失败', '', 1);
        }
    }
    
    
    private function _validBase() {
        $args = array();
        $args = XRequest::getGpc(array(
            'sitename', 'siteurl', 'logo', 'logowidth', 'logoheight',
            'timezone', 'icpcode','sitephone','siteaddress','siteemail',
            'siteqq','qrcode','longitude','latitude'
        ));
        $tjcode = XRequest::getArgs('tjcode', '', false);
        if (empty($args['sitename'])) {
            XHandle::halt('网站名称不能为空', '', 1);
        }
        if (empty($args['siteurl'])) {
            XHandle::halt('网站URL地址不能为空', '', 1);
        }
        else {
     		if (substr($args['siteurl'], -1) != '/') {
    			$args['siteurl'] = $args['siteurl'].'/';
    		}
        }
        $args['timezone'] = trim($args['timezone']);
        if (false == XValid::isNumber($args['timezone'])) {
            $args['timezone'] = 8;
        }
        $args['logowidth'] = intval($args['logowidth']);
        $args['logoheight'] = intval($args['logoheight']);
        $args = array_merge($args, array('tjcode'=>$tjcode));
        return $args;
    }
    
    
    private function _validFooter() {
        $content = XRequest::getArgs('content', '', false);
        return $content; 
    }
    
    
    private function _validUpload(){
        $php_upload_maxsize = ini_get('upload_max_filesize');
        $args = XRequest::getGpc(
            array(
                'uploadmaxsize', 'maxthumbwidth', 'maxthumbheight', 'thumbwidth',
                'thumbheight', 'productthumbwidth', 'productthumbheight',
                'photothumbwidth', 'photothumbheight', 'watermarkflag',
                'watermarkfile', 'watermarkpos'
            )
        );
        $args['uploadmaxsize'] = floatval($args['uploadmaxsize']);
        if ($args['uploadmaxsize']>$php_upload_maxsize){
            $args['uploadmaxsize'] = $php_upload_maxsize;
        }
        $args['maxthumbwidth'] = intval($args['maxthumbwidth']);
        $args['maxthumbheight'] = intval($args['maxthumbheight']);
        $args['thumbwidth'] = intval($args['thumbwidth']);
        $args['thumbheight'] = intval($args['thumbheight']);
        $args['productthumbwidth'] = intval($args['productthumbwidth']);
        $args['productthumbheight'] = intval($args['productthumbheight']);
        $args['photothumbwidth'] = intval($args['photothumbwidth']);
        $args['photothumbheight'] = intval($args['photothumbheight']);
        $args['watermarkflag'] = intval($args['watermarkflag']);
        $args['watermarkpos'] = intval($args['watermarkpos']);
        return $args;
    }
    
    
    private function _validSeo() {
        $args = XRequest::getGpc(array(
            'sitetitle', 'metadescription', 'metakeyword'
        ));
        return $args;
    }
    
    
    private function _validRewrite() {
        $urlsuffix = XRequest::getArgs('urlsuffix');
        $rewriterule = XRequest::getArgs('rewriterule');
        if (empty($urlsuffix)) {
            $urlsuffix = 'php';
        }
        return array($urlsuffix, $rewriterule);
    }
    
     
    private function _validSafety() {
        $args = array();
        $args = XRequest::getGpc(array(
            'admincode', 'commentcode', 'commentaudit',
            'regcode', 'logincode', 'forgetcode', 'forbidargs',
        ));
        $args['admincode'] = intval($args['admincode']);
        $args['commentcode'] = intval($args['commentcode']);
        $args['commentaudit'] = intval($args['commentaudit']);
        $args['regcode'] = intval($args['regcode']);
        $args['logincode'] = intval($args['logincode']);
		$args['forgetcode'] = intval($args['forgetcode']);
        return $args;
    }
    
    
    private function _validOrder() {
        $args = array();
        $args = XRequest::getGpc(array(
            'buynon', 'currency', 'symbol', 'taxrate', 'orderpagesize',
        ));
        $args['buynon'] = intval($args['buynon']);
        $args['taxrate'] = floatval($args['taxrate']);
        $args['orderpagesize'] = intval($args['orderpagesize']);
        if (empty($args['currency'])) {
            $args['currency'] = 'CNY';
        }
        if (empty($args['symbol'])) {
            $args['symbol'] = '￥';
        }
        if ($args['taxrate']<0) {
            $args['taxrate'] = 0;
        }
        return $args;
    }
    
    
    private function _validIndexStyle() {
        $args = XRequest::getGpc(array(
            'articlenum', 'articlelen',
            'productnum', 'productlen',
            'photonum', 'photolen',
            'downnum', 'downlen',
            'hrnum', 'hrlen',
        ));
        $args['articlenum'] = intval($args['articlenum']);
        $args['articlelen'] = intval($args['articlelen']);
        $args['productnum'] = intval($args['productnum']);
        $args['productlen'] = intval($args['productlen']);
        $args['photonum'] = intval($args['photonum']);
        $args['photolen'] = intval($args['photolen']);
        $args['downnum'] = intval($args['downnum']);
        $args['downlen'] = intval($args['downlen']);
        $args['hrnum'] = intval($args['hrnum']);
        $args['hrlen'] = intval($args['hrlen']);
        return $args;
    }
    
    
    private function _validMainStyle() {
        $args = XRequest::getGpc(array(
            'articlepagesize', 'productpagesize',
            'photopagesize', 'downpagesize',
            'hrpagesize',
        ));
        $args['articlepagesize'] = intval($args['articlepagesize']);
        $args['productpagesize'] = intval($args['productpagesize']);
        $args['photopagesize'] = intval($args['photopagesize']);
        $args['downpagesize'] = intval($args['downpagesize']);
        $args['hrpagesize'] = intval($args['hrpagesize']);
        return $args;
    }
    
    
    private function _validUser() {
        $args = XRequest::getGpc(array(
            'usercpflag', 'userreg', 'usergrid', 'lockusers', 'useraudit',
        ));
        $args['usercpflag'] = intval($args['usercpflag']);
        $args['userreg'] = intval($args['userreg']);
        $args['usergrid'] = intval($args['usergrid']);
        $args['useraudit'] = intval($args['useraudit']);
        return $args;
    }

    
    private function _validWap() {
        $args = GPC(array(
            "wap_name", "wap_productpagesize"=>"int", "wap_articlepagesize"=>"int",
            "wap_photopagesize"=>"int", "wap_hrpagesize"=>"int", "wap_downpagesize"=>"int",
            "wap_skin",
        ));
        return $args;
    }
         public function action_ajax_get_action()
     {     
     	$advContrl = get_class_methods('control');
   
        
     	$html = '';
     	foreach ($advContrl as $val){
            if(strpos($val,'action') !== false){
                   
               $html .= "<option value='".$val."'>".$val."</option>";
                    
            } else {
               
            }
     		
     	}
     	exit($html);
     }
     
    public function action_author() {
        $this->checkAuth('run');
        $model = parent::model('setting', 'am');
        $optionname='author';

        $data = $model->getcode($optionname);
        unset($model);
        $var_array = array(
            'code'=>$data['optionvalue'],
           
        );
        TPL::assign($var_array);
        TPL::display($this->cptpl.'setting.tpl');
	}
    public function action_savecode(){
        $this->checkAuth('run');
        $server_name=$_SERVER['SERVER_NAME'];
        $rest = substr($server_name, 0, 3);
        if($rest=='www'){
            $server_name=substr($server_name, 4);
        }
        $code=$_REQUEST['code'];
        $url= "https://www.8cms.cn/api.php?a=isdelegated&code=$code";
      //  echo $url;die;
        $res     = $this->curl_get($url);
        $data    = json_decode($res, TRUE);
      //  print_r($data);die;
        if(empty($data['data'])){
            XHandle::halt('授权码不存在，请到官网购买', '', 1);
            die;
        }
        if($data['data']['domain']!=$server_name){
            XHandle::halt('授权码与当前域名不对应，无法授权', '', 1);
            die;
        }
        $optionname='author';
        $model = parent::model('setting', 'am');
        $data = $model->getcode($optionname);
        if(empty($data)){
            $arr['id']='20';
            $arr['optionname']=$optionname;
            $arr['optionvalue']=$code;
            $arr['optiondesc']="商业授权码";
            $result=$model->addcode($arr);
        }else{
            $arr['optionvalue']=$code;
            $result=$model->savecode($optionname,$arr);
        }
        
        if (true === $result) {
            XHandle::halt('设置成功', $this->cpfile.'?c=setting&a=author', 0);
        }
        else {
            XHandle::halt('设置失败', '', 1);
        }
    }
        private function curl_get($url)
    {
        //初始化
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);  // 从证书中检查SSL加密算法是否存在
        $tmpInfo = curl_exec($curl);     //返回api的json对象
        //关闭URL请求
        curl_close($curl);
        return $tmpInfo;    //返回json对象
    }
    public function action_setup(){
        $this->checkAuth('run');
        $model = parent::model('setting', 'am');
        $data = $model->getsetup();
        $arr=array();
        foreach ($data as $Key=>$value){
            $arr[$value['setname']]=$value['setvalue'];
        }
        unset($model);
        $var_array = array(
            'data'=>$arr,
        );
        TPL::assign($var_array);
        TPL::display($this->cptpl.'setup.tpl');
    }
    public function action_savesetup() {
        $content = $this->_validsetup();
        $model = parent::model('setting', 'am');
        foreach ($content as $k=>$v){
            $result = $model->doUpdatesetup($k,$v);
        }

        unset($model);
        if (true === $result) {
            $this->log('setting', '分销信息', 1);
            XHandle::halt('保存成功', $this->cpfile.'?c=setting&a=setup', 0);
        }else {
            $this->log('setting', '分销信息', 0);
            XHandle::halt('保存失败', '', 1);
        }
    }
    private function _validsetup() {
        $args = array();
        $args = XRequest::getGpc(array(
            'integral_ratio', 'integral','one_ratio','two_ratio','three_ratio'
        ));
        if (empty($args['integral_ratio'])) {
            XHandle::halt('积分比例不能为空', '', 1);
        }
        if (empty($args['integral'])) {
            XHandle::halt('注册积分不能为空', '', 1);
        }
        return $args;
    }
}
?>
