<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
class SettingController extends AdminbaseController{
	
	protected $options_model;
	
	function _initialize() {
		parent::_initialize();
		$this->options_model = D("Common/Options");
	}
	
	function site(){
	    C(S('sp_dynamic_config'));
		$option=$this->options_model->where("option_name='site_options'")->find();
		$cmf_settings=$this->options_model->where("option_name='cmf_settings'")->getField("option_value");
		$tpls=sp_scan_dir(C("SP_TMPL_PATH")."*",GLOB_ONLYDIR);
		$noneed=array(".","..",".svn");
		$tpls=array_diff($tpls, $noneed);
		$this->assign("templates",$tpls);
	    $alipayconfig=array(
	      seller_email=>C('seller_email'),
	      notify_url=>C('notify_url'),
	      return_url=>C('return_url'),
	      successpage=>C('successpage'),
	      errorpage=>C('errorpage'),
	      partner=>C('partner'),
	      key=>C('key'),
	    );
		 $wxconfig=array(
	      wx_APPID=>C('wx_APPID'),
	      wx_MCHID=>C('wx_MCHID'),
	      wx_KEY=>C('wx_KEY'),
	      wx_APPSECRET=>C('wx_APPSECRET'),
	    );
	    $tencent=array(
	     APP_ID=>C(APP_ID),
	     SECRET_ID=>C(SECRET_ID),
	     SECRET_KEY=>C(SECRET_KEY),
	     DOMAIN=>C(DOMAIN),
		 SecretKey=>C(SecretKey),
		 SecretId=>C(SecretId),
		 AppId=>C(AppId),
		 DOCUMENT=>C(DOCUMENT),
		 DOCUMENTDOMAIN=>C(DOCUMENTDOMAIN)
    
	    );
		$radio=array(
		  livetype=>C('livetype'),
		  yuntype=>C('yuntype'),
		  mobileverify=>C('mobileverify'),
		  appon=>C('BJ_APP'),
		  dianboIsOpen=>C('dianboIsOpen'),
		  zhiboIsOpen=>C('zhiboIsOpen'),
		  xianxiaIsOpen=>C('xianxiaIsOpen'),
		  mingshiIsOpen=>C('mingshiIsOpen'),
		  cmfIsOpen=>C('cmfIsOpen'),
		  forumIsOpen=>C('forumIsOpen'),
		  tikuIsOpen=>C('tikuIsOpen')
		);
		$yuntongxun=array(
		  UCPAAS_accountsid=>C('UCPAAS_accountsid'),
		  UCPAAS_token=>C('UCPAAS_token'),
		  UCPAAS_very_appId=>C('UCPAAS_very_appId'),
		  UCPAAS_MSN_TEMPLATE_ID=>C('UCPAAS_MSN_TEMPLATE_ID'),
		  UCPAAS_FIND_TEMPLATE_ID=>C('UCPAAS_FIND_TEMPLATE_ID'),
		);
		
		$adminstyles=sp_scan_dir("public/simpleboot/themes/*",GLOB_ONLYDIR);
		$adminstyles=array_diff($adminstyles, $noneed);
		$this->assign("adminstyles",$adminstyles);
		$homestyle=sp_scan_dir("themes/classics/Public/themes/*",GLOB_ONLYDIR);
		$homestyle=array_diff($homestyle, $noneed);
		$this->assign("homestyles",$homestyle);
		if($option){
			$this->assign((array)json_decode($option['option_value']));
			$this->assign("option_id",$option['option_id']);
		}
		$this->assign("alipayconfig",$alipayconfig);
		$this->assign("tencent",$tencent);
		$this->assign("wxconfig",$wxconfig);
		$this->assign("yuntongxun",$yuntongxun);
		$this->assign("radio",$radio);
		$this->assign("cmf_settings",json_decode($cmf_settings,true));
		$this->display();
	}
	
	function site_post(){
		if (IS_POST) {
			if(isset($_POST['option_id'])){
				$data['option_id']=intval($_POST['option_id']);
			}
			
			$configs["SP_SITE_ADMIN_URL_PASSWORD"]=empty($_POST['options']['site_admin_url_password'])?"":md5(md5(C("AUTHCODE").$_POST['options']['site_admin_url_password']));
			$configs["SP_DEFAULT_THEME"]=$_POST['options']['site_tpl'];
			$configs["DEFAULT_THEME"]=$_POST['options']['site_tpl'];
			$configs["SP_ADMIN_STYLE"]=$_POST['options']['site_adminstyle'];
			$configs["URL_MODEL"]=$_POST['options']['urlmode'];
			$configs["URL_HTML_SUFFIX"]=$_POST['options']['html_suffix'];
			$configs["UCENTER_ENABLED"]=empty($_POST['options']['ucenter_enabled'])?0:1;
			$configs["COMMENT_NEED_CHECK"]=empty($_POST['options']['comment_need_check'])?0:1;
			$comment_time_interval=intval($_POST['options']['comment_time_interval']);
			$configs["COMMENT_TIME_INTERVAL"]=$comment_time_interval;
			$_POST['options']['comment_time_interval']=$comment_time_interval;
			$configs["MOBILE_TPL_ENABLED"]=empty($_POST['options']['mobile_tpl_enabled'])?0:1;
			$configs["HTML_CACHE_ON"]=empty($_POST['options']['html_cache_on'])?false:true;
			$configs["kefu_tel"]=$_POST['options']['kefu_tel'];	
			$configs["kefu_QQ"]=$_POST['options']['kefu_QQ'];	
			$configs["seller_email"]=$_POST['options']['seller_email'];	
			$configs["notify_url"]=$_POST['options']['notify_url'];	
			$configs["return_url"]=$_POST['options']['return_url'];	
			$configs["successpage"]=$_POST['options']['successpage'];	
			$configs["errorpage"]=$_POST['options']['errorpage'];	
			$configs["partner"]=$_POST['options']['partner'];	
			$configs["key"]=$_POST['options']['key'];
			$configs["AccessKeyID"]=$_POST['options']['AccessKeyID'];
			$configs["AccessKeySecret"]=$_POST['options']['AccessKeySecret'];
			$configs["wx_APPID"]=$_POST['options']['wx_APPID'];
			$configs["wx_MCHID"]=$_POST['options']['wx_MCHID'];
			$configs["wx_KEY"]=$_POST['options']['wx_KEY'];
			$configs["wx_APPSECRET"]=$_POST['options']['wx_APPSECRET'];
				
			$configs["APP_ID"]=$_POST['options']['APP_ID'];
			$configs["SECRET_ID"]=$_POST['options']['SECRET_ID'];
			$configs["SECRET_KEY"]=$_POST['options']['SECRET_KEY'];
			$configs["DOMAIN"]=$_POST['options']['DOMAIN'];
			$configs["DOCUMENT"]=$_POST['options']['DOCUMENT'];
			
            $configs["DOCUMENTDOMAIN"]=$_POST['options']['DOCUMENTDOMAIN'];

			$configs["mobileverify"]=$_POST['radio3'];
			$configs["UCPAAS_accountsid"]=$_POST['options']['UCPAAS_accountsid'];
			$configs["UCPAAS_token"]=$_POST['options']['UCPAAS_token'];
			$configs["UCPAAS_very_appId"]=$_POST['options']['UCPAAS_very_appId'];
			$configs["UCPAAS_MSN_TEMPLATE_ID"]=$_POST['options']['UCPAAS_MSN_TEMPLATE_ID'];
			$configs["UCPAAS_FIND_TEMPLATE_ID"]=$_POST['options']['UCPAAS_FIND_TEMPLATE_ID'];
			
			$configs["SITEKEY"]=$_POST['options']['SITEKEY'];
			$configs["SITEID"]=$_POST['options']['SITEID'];
			$configs["xueDianURL"]=$_POST['options']['xueDianURL'];
			
			$configs["dianboIsOpen"]=$_POST['options']['dianboIsOpen'];
			$configs["zhiboIsOpen"]=$_POST['options']['zhiboIsOpen'];
			$configs["xianxiaIsOpen"]=$_POST['options']['xianxiaIsOpen'];
			$configs["mingshiIsOpen"]=$_POST['options']['mingshiIsOpen'];
			$configs["forumIsOpen"]=$_POST['options']['forumIsOpen'];
			$configs["cmfIsOpen"]=$_POST['options']['cmfIsOpen'];
			$configs["tikuIsOpen"]=$_POST['options']['tikuIsOpen'];
			$configs["VIPPRICE"]=$_POST['options']['VIPPRICE'];
			
			sp_set_dynamic_config($configs);
			$data['option_name']="site_options";
			$data['option_value']=json_encode($_POST['options']);
			if($this->options_model->where("option_name='site_options'")->find()){
				$r=$this->options_model->where("option_name='site_options'")->save($data);
			}else{
				$r=$this->options_model->add($data);
			}
			
			$banned_usernames=preg_replace("/[^0-9A-Za-z_\x{4e00}-\x{9fa5}-]/u", ",", $_POST['cmf_settings']['banned_usernames']);
			$_POST['cmf_settings']['banned_usernames']=$banned_usernames;
			sp_set_cmf_setting($_POST['cmf_settings']);
			if ($r!==false) {
				$this->success("保存成功！");
			} else {
				$this->error("保存失败！");
			}
			
		}
	}
	function alipay(){
	    $this->display();
	}
	
	function pay(){

    }
	function password(){
		$this->display();
	}
	
	function password_post(){
		if (IS_POST) {
			if(empty($_POST['old_password'])){
				$this->error("原始密码不能为空！");
			}
			if(empty($_POST['password'])){
				$this->error("新密码不能为空！");
			}
			$user_obj = D("Common/Users");
			$uid=get_current_admin_id();
			$admin=$user_obj->where(array("id"=>$uid))->find();
			$old_password=$_POST['old_password'];
			$password=$_POST['password'];
			if(sp_compare_password($old_password,$admin['user_pass'])){
				if($_POST['password']==$_POST['repassword']){
					if(sp_compare_password($password,$admin['user_pass'])){
						$this->error("新密码不能和原始密码相同！");
					}else{
						$data['user_pass']=sp_password($password);
						$data['id']=$uid;
						$r=$user_obj->save($data);
						if ($r!==false) {
							$this->success("修改成功！");
						} else {
							$this->error("修改失败！");
						}
					}
				}else{
					$this->error("密码输入不一致！");
				}
	
			}else{
				$this->error("原始密码不正确！");
			}
		}
	}
	
	//清除缓存
	function clearcache(){
			
		sp_clear_cache();
		$this->display();
	}
	function updata(){
		$this->display();
		
	}
	
	function getFile($url, $save_dir = '', $filename = '', $type = 0) {
    if (trim($url) == '') {
        return false;
    }
    if (trim($save_dir) == '') {
        $save_dir = './';
    }
    if (0 !== strrpos($save_dir, '/')) {
        $save_dir.= '/';
    }
    if (!file_exists($save_dir) && !mkdir($save_dir, 0777, true)) {
        return false;
    }
    if ($type) {
        $ch = curl_init();
        $timeout = 5;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $content = curl_exec($ch);
        curl_close($ch);
    } else {
        ob_start();
        readfile($url);
        $content = ob_get_contents();
        ob_end_clean();
    }
    $size = strlen($content);
    $fp2 = @fopen($save_dir . $filename, 'a');
    fwrite($fp2, $content);
    fclose($fp2);
    unset($content, $url);
    return array(
        'file_name' => $filename,
        'save_path' => $save_dir . $filename
    );
}
	
}