<?php
/**
 * 五十九秒网络购物cms 系统设置实体类
 * ============================================================================
 * 版权所有 2009-2012 五十九秒网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.59miao.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: longzhang
 * 
*/
if (!defined('IN_59MIAO'))
{
	exit('Access Defined');
}
class SettingbasicModel extends Model {
	public function __construct(){
		parent::__construct();
		$this->_fields = array('id','site_title','site_url','site_titleb','site_keywords','site_description','site_logo','login_checkcode','adminpath','site_cache_dir','site_upload_dir','site_email','site_address','site_worktime','site_qq','site_benan','site_tel','site_copyright','site_tongji');
		$this->_tables = array(DB_FREFIX.'websetting');
		$this->_check = new SettingCheck();		
	}
	//获取一条数据
	public function findOne() {
		$_where = array("id='1'");
		if (!$this->_check->oneCheck($this, $_where)) $this->_check->error();
		return parent::select(array('id','site_title','site_url','site_titleb','site_keywords','site_description','site_logo','login_checkcode','adminpath','site_cache_dir','site_upload_dir','site_email','site_address','site_worktime','site_qq','site_benan','site_tel','site_copyright','site_tongji'),
											array('where'=>$_where, 'limit'=>'1'));
	}	
	//修改系统设置
	public function update(){
		$_where=array("id='1'");		
		if (!$this->_check->oneCheck($this, $_where)) $this->_check->error();		
		if (!$this->_check->updateCheck($this)) $this->_check->error();			
		$_updateData = $this->getRequest()->update($this->_fields);		
		$_updateData['site_tongji']=stripslashes($_updateData['site_tongji']);
		
		return parent::update($_where, $_updateData);
	}
	//高级配置
	public function high(){		
		if(!is_numeric(trim($_POST['page_num'])) || !is_numeric(trim($_POST['front_page_num'])) || !is_numeric(trim($_POST['upload_max'])) || !is_numeric(trim($_POST['cache_time']))){
			return false;
		}
		$page_num=trim($_POST['page_num']);
		$front_page_num=trim($_POST['front_page_num']);		
		$upload_dir=trim($_POST['upload_dir']);
		$upload_max=trim($_POST['upload_max']*1024);	
		$cache=trim($_POST['cache']);
		$cache_time=$_POST['cache_time']*60*60;		
		$_br = "\r\n";
		$_tab = "\t";		
		$_profile = '<?php'.$_br;
		$_profile .= $_tab."//系统配置文件".$_br;	
		$_profile .= $_tab."if (!defined('IN_59MIAO'))
		{
			exit('Access Defined');
		}".$_br;
		$_profile .= $_tab."define('PAGE_SIZE','{$page_num}');".$_br;
		$_profile .= $_tab."define('FRONT_PAGE_SIZE','{$front_page_num}');".$_br;
		$_profile .= $_tab."define('UPDIR','{$upload_dir}');".$_br;
		$_profile .= $_tab."define('SOURCE_PATH','souse_path/');".$_br;
		$_profile .= $_tab."define('MIN_PATH','min_path/');".$_br;	
		$_profile .= $_tab."define('MAX_FILE_SIZE','{$upload_max}');".$_br;

		$_profile .= $_br;				
		
		$_profile .= $_tab."define('SMARTY_ADMIN', 'admin/');".$_br;
		$_profile .= $_tab."define('SMARTY_TEMPLATE_DIR', ROOT_PATH.'/view/');".$_br;
		$_profile .= $_tab."define('SMARTY_COMPILE_DIR', ROOT_PATH.'/data/smarty_compile/');".$_br;
		$_profile .= $_tab."define('SMARTY_CONFIG_DIR', ROOT_PATH.'/configs/');".$_br;
		$_profile .= $_tab."define('SMARTY_CACHE_DIR', ROOT_PATH.'/data/smarty_cache/');".$_br;
		$_profile .= $_tab."define('SMARTY_CACHING',{$cache});".$_br;
		$_profile .= $_tab."define('SMARTY_CACHE_LIFETIME',{$cache_time});".$_br;
		$_profile .= $_tab."define('SMARTY_LEFT_DELIMITER', '{');".$_br;
		$_profile .= $_tab."define('SMARTY_RIGHT_DELIMITER', '}');".$_br;		
		$_profile .= $_br;	
		$_profile .= '?>'.$_br;		
		if (!file_put_contents('./configs/profile.inc.php', $_profile)) {
			return false;
		}
		else{
			return true;
		}
	}
	public function check(){
		$filelist=array('configs','cache','data','data/smarty_cache','data/smarty_compile','install','data/uploads','data/adjs');		
		$str='<table>';		
		 foreach ($filelist AS $dir){		
		    	
		        if (!file_exists(ROOT_PATH.'/'.$dir))
		        {
					$str.= '<tr class="error"><td>'.$dir.'目录<font style="color:#ff6600" class="nw">文件不存在</font><input type="hidden" name="error[]" value="1"/></td></tr>';
		            continue;
		        }
		
		        if (Tool::file_mode_info(ROOT_PATH .'/'. $dir) < 2)
		        {
		            $str.='<tr class="error"><td>'.$dir.'目录<font style="color:#ff6600" class="nw">不可写</font><input type="hidden" name="error[]" value="1"/></td></tr>';
		        }
		        else
		        {
		            $str.='<tr class="correct"><td>'.$dir.'目录<font style="color:#339900" class="w">可写</font></td></tr>';
		        }
		 
		    }
		 $url = 'http://gw.api.59miao.com';
		 $array_url = get_headers($url,1);
		 if(preg_match('/200/',$array_url[0])){
			 $str.='<tr class="correct"><td>与五十九秒Api服务器通信<font style="color:#339900" class="w">成功</font></td></tr>';						   
		  }
		  else
		  {
			$str.='<tr class="error"><td>与五十九秒Api服务器通信<font style="color:#ff6600" class="nw">不成功</font></td></tr>';
						    
		  } 	
		$urltao= 'http://gw.api.taobao.com/router/rest';
		$array_url = get_headers($urltao,1);
		if(preg_match('/200/',$array_url[0])){
			 $str.='<tr class="correct"><td>与淘宝Api服务器通信<font style="color:#339900" class="w">成功</font></td></tr>';	   
		}else{
				$str.='<tr class="error"><td>与淘宝Api服务器通信<font style="color:#ff6600" class="nw">不成功</font></td></tr>';
						    
		} 
		$str.='</table>';
		
		return $str;
	}	
}
?>