<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site_config extends Controller
{
	function Site_config()
	{
		parent::Controller();
		check_is_login();
		$this->load->library('form_validation');
	}
	function index()
	{
		$this->load->config("shop_site_config");
		$this->load->config("shop_email_config");
		$this->load->config("top_config");
		$data=array(
			'edit_data' => $this->config->config
		);
		$this->load->view(TPL_FOLDER."site_config",$data);
	}
	
	function save_record()
	{
		$this->form_validation->set_rules("sys_site_name","网站名称","trim|require");
		$this->form_validation->set_rules("sys_site_title","网站标题","trim|require");
		$this->form_validation->set_rules("sys_site_keyword","META关键字","trim|require");
		$this->form_validation->set_rules("sys_site_description","META描述","trim|require");
		$this->form_validation->set_rules("sys_site_copyright","网站版权","trim|require");
		$this->form_validation->set_rules('smtp_email','发信邮箱','required|valid_email');
		$this->form_validation->set_rules('nick','淘宝账号','required');
		$this->form_validation->set_rules('pid','淘客PID','required');
		$this->form_validation->set_rules('cache_time','数据缓存周期','required');
		$this->form_validation->set_rules('page_size','每页商品数','required');
		$this->form_validation->set_rules('start_commission_rate','起始佣金比率','required');
		$this->form_validation->set_rules('end_commission_rate','结束佣金比率','required');
		$this->form_validation->set_rules('start_credit','起始卖家信用','required');
		$this->form_validation->set_rules('end_credit','结束卖家信用','required');
		
		$tx_msg="";
		if($this->form_validation->run()==FALSE)
		{
			$tx_msg.=validation_errors();
		}
		$sys_site_logo=do_upload(array('up_path'=>UP_IMAGES_PATH,'form_name'=>"sys_site_logo",'suffix'=>UP_IMAGES_EXT));
		if(!$sys_site_logo['status'])
		{
			$tx_msg.=$sys_site_logo['upload_errors'];
		}
		
		$pid = strip_quotes($this->input->post("pid"));
		if($pid)
		{
			$pid_arr = explode('_',$pid);
			if(isset($pid_arr[1]))
			{
				$pid = $pid_arr[1];
			}
			else
			{
				$tx_msg .= '<li>淘客PID 有误</li>';
			}
		}
		else
		{
			$tx_msg .= '<li>请填写淘客PID</li>';
		}
		$appkey = $this->input->post('appkey');
		$secretkey = $this->input->post('secretkey');
		if( ! is_array($appkey) || ! is_array($secretkey))
		{
			$tx_msg .= '<li>appkey 或secretkey 填写有误</li>';
		}
		
		if($tx_msg!="")
		{
			echo_msg($tx_msg);
		}
		$this->load->helper('file');
		$this->load->helper('string');
		$string=read_file(APPPATH.'config/shop_site_config'.EXT);
		$config['sys_site_name'] = strip_quotes($this->input->post("sys_site_name"));
		$config['sys_site_title'] = strip_quotes($this->input->post("sys_site_title"));
		if($sys_site_logo['file_path'])
		{
			$config['sys_site_logo'] = $sys_site_logo['file_path'];
		}
		else
		{
			$config['sys_site_logo'] = strip_quotes($this->input->post("pic_path1"));
		}
		$config['sys_site_keyword'] = strip_quotes($this->input->post("sys_site_keyword"));
		$config['sys_site_description'] = strip_quotes($this->input->post("sys_site_description"));
		$config['sys_site_copyright'] = strip_quotes($this->input->post("sys_site_copyright"));
		$reg ='/\$config\[\'(.+?)\'\].+?;/is';
		preg_match_all( $reg ,$string , $out  );
		if( $out[1] )
		{
			$old = $new = array();
			foreach( $out[1] as $k => $v )
			{
				if(isset($config[strtolower($v)]))
				{
					$old[] = $out[0][$k];
					$new[] = '$config[\''.strtolower($v).'\'] = "'.$config[strtolower($v)].'";';
				}				
			}
			if( $new )
			{
				$string = str_replace( $old , $new , $string );
				write_file(APPPATH.'config/shop_site_config'.EXT, $string);
			}
		}
		unset($config); unset($out);unset($old); unset($new);
		
		$string=read_file(APPPATH.'config/shop_email_config'.EXT);
		$config['smtp_host'] = strip_quotes($this->input->post("smtp_host"));
		$config['smtp_user'] = strip_quotes($this->input->post("smtp_user"));
		$config['smtp_pass'] = strip_quotes($this->input->post("smtp_pass"));
		$config['smtp_email'] = strip_quotes($this->input->post("smtp_email"));
		$reg ='/\$config\[\'(.+?)\'\].+?;/is';
		preg_match_all( $reg ,$string , $out  );
		if( $out[1] )
		{
			//$old = $new = array();
			foreach( $out[1] as $k => $v )
			{
				if(isset($config[strtolower($v)]))
				{
					$old[] = $out[0][$k];
					$new[] = '$config[\''.strtolower($v).'\'] = "'.$config[strtolower($v)].'";';
				}			
			}
			if( $new )
			{
				$string = str_replace( $old , $new , $string );
				write_file(APPPATH.'config/shop_email_config'.EXT, $string);
			}
		}
		unset($config); unset($out);unset($old); unset($new);
		
		$string=read_file(APPPATH.'config/top_config'.EXT);
		$config['nick'] = strip_quotes($this->input->post("nick"));
		$config['cache_time'] = strip_quotes($this->input->post("cache_time"));
		$config['page_size'] = strip_quotes($this->input->post("page_size"));
		$config['start_commission_rate'] = strip_quotes($this->input->post("start_commission_rate"));
		$config['end_commission_rate'] = strip_quotes($this->input->post("end_commission_rate"));
		$config['start_credit'] = strip_quotes($this->input->post("start_credit"));
		$config['end_credit'] = strip_quotes($this->input->post("end_credit"));
		$config['pid'] = $pid;
		$reg ='/\$config\[\'(.+?)\'\].+?;/is';
		preg_match_all( $reg ,$string , $out  );
		if( $out[1] )
		{
			//$old = $new = array();
			foreach( $out[1] as $k => $v )
			{
				if(isset($config[strtolower($v)]))
				{
					$old[] = $out[0][$k];
					$new[] = '$config[\''.strtolower($v).'\'] = "'.$config[strtolower($v)].'";';
				}			
			}
			if( $new)
			{
				$string = str_replace( $old , $new , $string );
			}
		}
		unset($config); unset($out);unset($old); unset($new);
		
		$mp100 = strip_quotes($this->input->post("mp100"));
		$mp1000 = strip_quotes($this->input->post("mp1000"));
		$mp3000 = strip_quotes($this->input->post("mp3000"));
		$mp5000 = strip_quotes($this->input->post("mp5000"));
		$mp10000 = strip_quotes($this->input->post("mp10000"));
		$mp100000 = strip_quotes($this->input->post("mp100000"));
		$reg = '/\$config\[\"market_price\"\][^\r\n]+;/is';
		preg_match_all( $reg ,$string , $out  );
		if(isset($out[0][0]))
		{
			$str = '$config["market_price"] = array(';
			$str .= "'0-100'=>'{$mp100}','100-1000'=>'{$mp1000}','1000-3000'=>'{$mp3000}','3000-5000'=>'{$mp5000}','5000-10000'=>'{$mp10000}','10000-0'=>'{$mp100000}'";
			$str .= ');';
			$string = str_replace($out[0][0],$str,$string);
		}
		
		$reg = '/\$config\[\"appkey\"\][^\r\n]+;/is';
		preg_match_all( $reg ,$string , $out  );
		if(isset($out[0][0]))
		{
			$str = '$config["appkey"] = array(';
			foreach($appkey as $k=>$v)
			{
				$str .= "array('appkey'=>'{$v}','secretkey'=>'{$secretkey[$k]}'),";
			}
			$str = rtrim($str,',');
			$str .= ');';
			$string = str_replace($out[0][0],$str,$string);
		}
		write_file(APPPATH.'config/top_config'.EXT, $string);
		echo_msg('<li>配置修改成功</li>','','yes');
	}

}
?>