<?php 
/**
 *----------------------------------------------------------------------------*
 * 版权所有 2010-2016 HuCart.COM，并保留所有权利。
 * 网站地址: http://Www.HuCart.Com；
 * 作者: HuCart.COM
 *----------------------------------------------------------------------------*
**/
if (!defined('HUCART_COM')) exit('HuCart.COM No direct script access allowed');
class Sys_config extends HC_Controller
{
	private $master_act 	= 'sys_config';
	private $info_prefix 	= 'sys_';
	function __construct()
	{
		parent::__construct();	
		$this -> load_lang('adminsys/'.$this -> master_act );
		$this -> view -> assign('master_act', $this -> master_act);
		$this -> view -> assign('info_prefix',$this -> info_prefix);
		$this -> load('admin');
		$this -> admin -> online_admin();
		$this -> load_db();
		$this -> load('helper');     //加载辅助类

	}
	
	function info_list(  )
	{

		$sql_query = " SELECT sys_code, sys_value FROM ".$this -> db -> prefix."sys_config WHERE sys_type ='web' or sys_type ='mail' GROUP BY sys_id ORDER BY `sys_id` ";
		$info = $this -> db -> select_info($sql_query);
		
		foreach((array)$info as $key => $val )
		{
			if($val["sys_code"]=='web_count_code')
			{
				$re_arr[$val["sys_code"]]["sys_code"] = $val["sys_code"];
				$re_arr[$val["sys_code"]]["sys_value"] = base64_decode($val["sys_value"]);
			}else{
				 $re_arr[$val["sys_code"]]["sys_code"]  = $val["sys_code"];
				 $re_arr[$val["sys_code"]]["sys_value"] = $val["sys_value"];
			}
		}
		
		
		$r =  $this -> helper -> checkPowered( $re_arr["web_powered"]["sys_value"] );
 
		$re_arr["powered_true"] = $r["poweredtrue"];
		//var_dump($re_arr);
		$ckeditorLoad 	= $this -> helper -> load_ckeditor( 'web_description',$re_arr["web_description"]["sys_value"],'Basic' );//加载文本编辑器
		
		$this -> view -> assign( 'ckeditorLoad', $ckeditorLoad);

		$close_ckeditorLoad 	= $this -> helper -> load_ckeditor( 'web_close_val',$re_arr["web_close_val"]["sys_value"],'Basic' );//加载文本编辑器
		
		$this -> view -> assign( 'close_ckeditorLoad', $close_ckeditorLoad);
		
		$this -> view -> assign('edit_submit',	$this->lang['edit']);
		$this -> view -> assign('txt_title',	$this->lang['title']);
		$this -> view -> assign('re_arr',$re_arr);
		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=".$this -> master_act."&act=edit_info&act_type=edit");
		$this -> view -> display('sys_config.tpl');
		
	}
	
	//<div class="css_powered_right">&copy; 2005-2013 HuCart.COM 版权所有 Powered by <a href="http://Www.HuCart.Com/"  target="_blank">HuCart.COM</a></div>
	
	function edit_info()
	{	
	
		$web_name 			= $this -> input ->  funpost("web_name");
		$web_title 			= $this -> input ->  funpost("web_title");
		$sys_sign 			= $this -> input ->  funpost("sys_sign");
		//$web_description 	= $this -> input ->  funpost("web_description");
		$web_description 	= html_entity_decode($_POST["web_description"]);
		
		$web_keyword 		= $this -> input ->  funpost("web_keyword");
		$web_qq 			= $this -> input ->  funpost("web_qq");
		$web_phote 			= $this -> input ->  funpost("web_phote");
		$web_close 			= $this -> input ->  funpost("sys_enabled");
		//$web_close_val 	= $this -> input ->  funpost("web_close_val");
		$web_close_val 		= html_entity_decode($_POST["web_close_val"]);
		
		$web_logo 			= $this -> input ->  funpost("web_logo");
		$web_close_reg 		= $this -> input ->  funpost("web_close_reg");
		
		$web_certificate 		= $this -> input ->  funpost("web_certificate");
		$web_serach_key 		= $this -> input ->  funpost("web_serach_key");
		$web_linkman 			= $this -> input ->  funpost("web_linkman");
		$web_address 			= $this -> input ->  funpost("web_address");
		$web_language 			= $this -> input ->  funpost("web_language");
		$web_powered 			= $this -> input ->  funpost("web_powered");
		
		//$web_count_code 		= $this -> input ->  funpost("web_count_code");
		$web_count_code			= base64_encode(trim($_POST["web_count_code"]));
		
		
		$web_tmp 				= $this -> input ->  funpost("web_tmp");
		$web_tmp_time 			= $this -> input ->  funpost("web_tmp_time");
		$mail_localhost 		= $this -> input ->  funpost("mail_localhost");
		$mail_user_name 		= $this -> input ->  funpost("mail_user_name");
		$mail_passwd 			= $this -> input ->  funpost("mail_passwd");
		$mail_reply 			= $this -> input ->  funpost("mail_reply");
		$mail_reply_name 		= $this -> input ->  funpost("mail_reply_name");
		$mail_from_name 		= $this -> input ->  funpost("mail_from_name");
		$mail_type_mail 		= $this -> input ->  funpost("mail_type_mail");
		$mail_from 				= $this -> input ->  funpost("mail_from");

		
		
		if($web_name !='' and $web_title !='' )
		{

			$up_arr = array('web_name' => $web_name,'web_title'=>$web_title,'sys_sign'=>$sys_sign,
								'web_description'=>$web_description,'web_keyword'=>$web_keyword,'web_qq'=>$web_qq,
								'web_phote'=>$web_phote,'web_close'=>$web_close,'web_close_val'=>$web_close_val,'web_logo'=>$web_logo,
								'web_close_reg'=>$web_close_reg,
								'web_certificate'=>$web_certificate,'web_serach_key'=>$web_serach_key,'web_linkman'=>$web_serach_key,
								'web_address'=>$web_address,'web_language'=>$web_language,'web_powered'=>$web_powered,
								'web_count_code'=>$web_count_code,'web_tmp'=>$web_tmp,'web_tmp_time'=>$web_tmp_time,
								'mail_localhost'=>$mail_localhost,'mail_user_name'=>$mail_user_name,'mail_passwd'=>$mail_passwd,
								'mail_reply'=>$mail_reply,'mail_reply_name'=>$mail_reply_name,'mail_from_name'=>$mail_from_name,
								'mail_type_mail'=>$mail_type_mail,'mail_from'=>$mail_from);



			foreach($up_arr as $key => $val)
			{
				if($val!='')
				{
					$upsql = '';
					$upsql = "UPDATE ".$this -> db -> prefix."sys_config  set  sys_value  = '".$val."'   WHERE  sys_code = '".$key."' and ( sys_type ='web' or  sys_type ='mail' ) ";
					$this -> db -> query_sql( $upsql );
				}
			}
			
			$this -> view ->  assign('info_title',$this->lang['edit_success']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=".$this -> master_act."&act=info_list");
			$this -> view ->  display('suggestion.tpl');
				
		}else{
		
			$this -> view ->  assign('info_title',$this->lang['err_info_missing']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=".$this -> master_act."&act=info_list");
			$this -> view ->  display('suggestion.tpl');	
		}
		
	}
	
	function tem_list()
	{
		$sql_query = " SELECT sys_name,sys_value  FROM ".$this -> db -> prefix."sys_config WHERE sys_type ='web' and sys_code ='temp' ORDER BY `sys_id` ";
		$info_temp = $this -> db -> select_one($sql_query);
		//var_dump( $info_temp );
		//json_decode
		$info_user['sys_name'] =$info_temp["sys_name"];
		
		$temp_val = json_decode($info_temp["sys_value"]);
		//var_dump($temp_val);
		$info_user['sys_css'] = $temp_val->style ;
		$info_user['sys_code'] =$temp_val->dir ;
		//var_dump($temp_val->dir);
		//exit();
		
		//查询目录下所有文件夹
		$dir = DOCUMENT_DIR."templates/www";
		$dirlist = $this -> helper -> getdirs( $dir );
		$info = array();
		foreach((array)$dirlist as $dkey => $dval)
		{	
			//可添加目录，不存在库中模板
			if(is_file($dir."/".$dval."/default.xml"))
			{
				$re_sys = $this -> read_xml($dir."/".$dval."/default.xml");
				
				if($re_sys["sys_title"]!='' and $re_sys["sys_version"]!='' and $re_sys["sys_image"]!='' and $re_sys["sys_css"][0]!='')
				{
					$info[$dkey]["sys_dir"] 	= $dval;


					if($temp_val->dir==$dval)
					{ 
						$info[$dkey]["sys_css_checked"] 	= $temp_val->style; 
						$info[$dkey]["sys_checked"] 	= true; 
					}else{ 
						$info[$dkey]["sys_checked"] 	= false; 
					}
					
					$info[$dkey]["sys_title"] 	= $re_sys["sys_title"];
					$info[$dkey]["sys_image"] 	= $re_sys["sys_image"];
					$info[$dkey]["sys_css"] 	= $re_sys["sys_css"];
					$info[$dkey]["sys_css_cout"] = count($re_sys["sys_css"]);
					$info[$dkey]["sys_version"] = $re_sys["sys_version"];
					$info[$dkey]["sys_link"] 	= $re_sys["sys_link"];
					
				}	
			}
		}

 

		$this -> view -> assign('txt_title',	$this->lang['tmp_title']);
		$this -> view -> assign('edit_submit',	$this->lang['edit']);
		
		//var_dump($temp_val);
		
		//echo '<br>';
		//var_dump($info);
		
		
		$this -> view -> assign('info_user',$info_user);
		$this -> view -> assign('info',$info);
		$this -> view -> display('temp.tpl');	
	}
	
	
	//添加新查询模板
	function add_tem()
	{
		//查询数据库中模板数量，及查询目录下文件是否存在。
		exit();
		$sql_query = " SELECT *  FROM ".$this -> db -> prefix."sys_config WHERE sys_type ='temp' GROUP BY sys_id ORDER BY `sys_id`  ";
		
		$info = $this -> db -> select_info($sql_query);
		
		$iskeyArray = array();
		foreach( (array)$info as $ikey => $ival )
		{
			$iskeyArray[] = $ival["sys_value"];
			
		}
		//查询目录下所有文件夹
		$dir = DOCUMENT_DIR."templates/www";
		
		$dirlist = $this -> helper -> getdirs( $dir );
 
 		$isnew = false ;
		
		foreach((array)$dirlist as $dkey => $dval)
		{	
			if(in_array($dval , $iskeyArray))
			{
				 
			
			}else{
				$isnew = true ; 
				//可添加目录，不存在库中模板
				if(is_file($dir."/".$dval."/default.xml"))
				{
					//echo $dir."/".$dval."/default.xml";
					$re_sys = $this -> read_xml($dir."/".$dval."/default.xml");
					//var_dump($re_sys );
					//exit();
					if($re_sys["sys_name"]!='')
					{
						echo $sqlinsert = "INSERT INTO `".$this -> db -> prefix."sys_config` ( `sys_name` , `sys_code` , `sys_value` , `sys_type` )VALUES (  '".$re_sys["sys_name"]."', 'template', '".$dval."', 'temp') ";
						
						$this -> db -> query_sql( $sqlinsert );
					}
					
				}
				//echo 'out'.$dval;
			
			}
			//echo '<br>';
		}
		
		if($isnew == false)
		{
			$_LANG['bd_tempsssusse']	= $_LANG['bd_tempnone'] .$dir."/<br>"; 
		}
		
		$this -> view -> assign('info',$info);
		
		$this -> view ->  assign('info_title',$_LANG['bd_tempsssusse']);
		$this -> view ->  assign('URL_TIME',URL_TIME);
		$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=".$this -> master_act."&act=tem_list");
		$this -> view ->  display('suggestion.tpl');
		
	}
	
	
	function read_xml($fileName = '')
	{
	
		$file = fopen($fileName, 'r'); 
		while( $data = fread( $file, 4096) )
		{ 
			$xml .= $data; 
		} 
		fclose( $file );
		// 上面读取数据 
		preg_match_all( "/\<copyright\>(.*?)\<\/copyright\>/s", $xml, $humans ); //匹配最外层标签里面的内容 

		preg_match_all( '/\<title\>(.*?)\<\/title\>/', $humans[1][0], $title ); //匹配出名称标题 
		preg_match_all( "/\<link\>(.*?)\<\/link\>/", $humans[1][0], $link ); //匹配出发布地址 
		preg_match_all( "/\<version\>(.*?)\<\/version\>/", $humans[1][0], $version ); //匹配版本号
		preg_match_all( "/\<author\>(.*?)\<\/author\>/", $humans[1][0], $author ); //匹配出出品单位/作者
		preg_match_all( "/\<image\>(.*?)\<\/image\>/", $humans[1][0], $image ); //匹配出出品单位/作者
		
		preg_match_all( "/\<css\>(.*?)\<\/css\>/", $humans[1][0], $css ); //匹配出颜色组 

	 	$re_sys["sys_title"] 	= $title[1][0] ;
		$re_sys["sys_link"] 	= $link[1][0] ;
		$re_sys["sys_version"] 	= $version[1][0] ;
		$re_sys["sys_author"] 	= $author[1][0] ;
		$re_sys["sys_image"] 	= $image[1][0] ;
		$re_sys["sys_css"] 		= $css[1];
		return $re_sys;	
	}

	
	
	function edit_temp()
	{

		$sys_nameval 		= $this -> input ->  funpost("sys_name");
		$sys_code['dir'] 	= $this -> input ->  funpost("sys_code");
		$sys_code['style'] 	= $this -> input ->  funpost("sys_css");
	
		if($sys_nameval !='' && $this -> input ->  funpost("sys_code") !='' && $this -> input ->  funpost("sys_css") ){
			
			$sys_value = json_encode($sys_code);
			//var_dump($sys_value);
			//exit();
			$upsql = "UPDATE ".$this -> db -> prefix."sys_config set sys_code  = 'temp',sys_value   = '$sys_value', sys_name  = '$sys_nameval' WHERE sys_code = 'temp' and  sys_type = 'web'";
			//exit();
			$this -> db -> query_sql( $upsql );
		}
		$this -> view ->  assign('info_title',$this->lang['edit_success']);
		$this -> view ->  assign('URL_TIME',URL_TIME);
		$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=".$this -> master_act."&act=tem_list");
		$this -> view ->  display('suggestion.tpl');
		
		
	}
	
	function user_config()
	{
		$sql_query = " SELECT sys_code,sys_value  FROM ".$this -> db -> prefix."sys_config WHERE sys_type ='reg' GROUP BY sys_id ORDER BY `sys_id`  ";
		
		$info = $this -> db -> select_info($sql_query);
		
		foreach( $info as $key => $val )
		{
			$re_arr[$val["sys_code"]] = explode('|',$val["sys_value"]);
		}
		//var_dump($re_arr);
		
		$this -> view -> assign('re_arr',$re_arr);
		$this -> view -> assign('txt_title',$this->lang['reg_config_title']);
		$this -> view -> assign('edit_submit',$this->lang['edit']);
		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=".$this -> master_act."&act=edit_reg_config");
		$this -> view -> display('user_reg_config.tpl');			
	}
	
	
	function edit_reg_config( )
	{	

		$reg_msn 		= $this -> input ->  funpost("reg_msn");
		$reg_qq 		= $this -> input ->  funpost("reg_qq");
		$reg_phote 		= $this -> input ->  funpost("reg_phote");
		$reg_home_phote	= $this -> input ->  funpost("reg_home_phote");
		$reg_tel 		= $this -> input ->  funpost("reg_tel");
		//var_dump( $reg_msn );

		$uparray = array('reg_msn'=>$reg_msn, 'reg_qq'=>$reg_qq, 'reg_phote'=>$reg_phote,'reg_home_phote'=>$reg_home_phote,'reg_tel'=>$reg_tel );

			foreach($uparray as $key => $val)
			{
 
					$upsql = '';
					if($val[0]==''){$val[0] = '0';}
					if($val[1]==''){$val[1] = '0';}
					if($val[2]==''){$val[2] = '0';}
					
					$upsql = "UPDATE ".$this -> db -> prefix."sys_config  set  sys_value  = '".$val[0]."|".$val[1]."|".$val[2]."'   WHERE  sys_code = '".$key."' and  sys_type ='reg' ";
					$this -> db -> query_sql( $upsql );

			}
			
			//exit();
			$this -> view ->  assign('info_title',$this->lang['edit_success']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=sys_config&act=user_config&type=reg");
			$this -> view ->  display('suggestion.tpl');
				
 
		
	}

		
}

