<?php
/*
'**************************************************************************************************
' 软件名称: 搜一次 Content Management System
' 版本编号: Version 1.X
' 官方网站: http://www.syccms.com
' 官方论坛：http://bbs.syccms.com
' 版权所有: 搜一次运营团队
' 法律顾问: 杭州市元茂律师事务所 严飞律师
' 郑重声明:
'    1、任何个人或组织不得在未经授权的情况下删除、修改、拷贝本软件及其他副本上一切关于版权的信息；
'    2、搜一次网络事业部保留此软件的法律追究权利
'**************************************************************************************************
*/
require 'config.inc.php';
require 'config.php';
require 'install.php';
if (S_IsInstall==0) {
	exit('您好，无法连接数据库或者您尚未执行安装!<a href="./install/install.php">执行安装</a>');
}
class cls_mysql{
	protected $link_id;
	public function __construct($dbhost, $dbuser, $dbpw, $dbname = '', $charset = 'gbk'){
		if(!($this->link_id = @mysql_connect($dbhost, $dbuser, $dbpw))){
			$this->ErrorMsg('您好，无法连接数据库或者您尚未执行安装!<a href="./install/install.php">执行安装</a>');
		}
		
		mysql_query("SET NAMES " . $charset, $this->link_id);
		if ($dbname){
			if (@mysql_select_db($dbname, $this->link_id) === false ){
				$this->ErrorMsg('您好，没有找到您选择的数据库名或者您尚未执行安装!<a href="./install/install.php">执行安装</a>');	
				return false;
			}else{
				return true;
			}
		}
	}
	public function select_database($dbname){
		return mysql_select_db($dbname, $this->link_id);
	}
	
	public function list_tables($dbname){
		return mysql_list_tables($dbname,$this->link_id);
	}
	
	public function list_fields($dbname,$tbname){
		return  mysql_list_fields($dbname,$tbname,$this->link_id);
	}
	
	public function fetch_array($query, $result_type = MYSQL_ASSOC){
		return mysql_fetch_array($query, $result_type);
	}
	
	public function query($sql){
		return mysql_query($sql, $this->link_id);
	}
	
	public function affected_rows(){
		return mysql_affected_rows($this->link_id);
	}
	
	public function num_rows($query){
		return mysql_num_rows($query);
	}
	
	public function insert_id(){
		return mysql_insert_id($this->link_id);
	}

	public function selectLimit($sql, $num, $start = 0){
		if ($start == 0){
			$sql .= ' LIMIT ' . $num;
		}else{
			$sql .= ' LIMIT ' . $start . ', ' . $num;
		}
		return $this->query($sql);
	}

	public function getOne($sql, $limited = false){
		if ($limited == true){
			$sql = trim($sql . ' LIMIT 1');
		}
		
		$res = $this->query($sql);
		if ($res !== false){
			$row = mysql_fetch_row($res);
			return $row[0];
		}else{
			return false;
		}
	}
	
	public function getrow($sql){
		$res = $this->query($sql);
		if ($res !== false){
			return mysql_fetch_assoc($res);
		}else{
			return false;
		}
	}

	public function getAll($sql){
		$res = $this->query($sql);
		if ($res !== false){
			$arr = array();
			while ($row = mysql_fetch_assoc($res)){
				$arr[] = $row;
			}
		
			return $arr;
		}else{
			return false;
		}
	}
	

		
	function ErrorMsg($message = '', $sql = ''){
		if ($message){
			echo "<b>error info</b>: $message\n\n";
		}else{
			echo "<b>MySQL server error report:";
		}
		exit;
	}
}

class Cache_Lite{

	var $_dir  = '/cache/';
	var $_time = 60;
	var $_id;

	function __construct($options=array(NULL)){
		if(is_array($options)){
			$available_options = array('_dir','_time');
			foreach($options as $key => $value){
				if(in_array($key,$available_options)){
					$this->$key = $value;
				}
			}
		}
	}

	function get($id){
		$this->_id = md5(md5($id));
		if(file_exists($this->_dir.$this->_id) && ((time() - filemtime($this->_dir.$this->_id)) < $this->_time)){
			if(PHP_VERSION >= '4.3.0'){
				$data = file_get_contents($this->_dir.$this->_id);
			}else{
				$handle = fopen($this->_dir.$this->_id,'rb');
				$data = fread($handle,filesize($this->_dir.$this->_id));
				fclose($handle);
			}
			return $data;
		}else{
			return false;
		}
	}

	function save($data){
		if(!is_writable($this->_dir)){
			if(!@mkdir($this->_dir,0777,true)){
				echo 'Cache directory not writable';
				exit;
			}
		}
		if(PHP_VERSION >= '5'){
			file_put_contents($this->_dir.$this->_id,$data);
		}else{
			$handle = fopen($this->_dir.$this->_id,'wb');
			fwrite($handle,$data);
			fclose($handle);
		}
		return true;
	}

	function start($id){
		$data = $this->get($id);
		if($data !== false && S_IsCache==1){
			echo($data);
			return true;
		}
		ob_start();
		ob_implicit_flush(false);
		return false;
	}

	function end(){
		$data = ob_get_contents();
		ob_end_clean();
		if(S_IsCache==1) $this->save($data);
		echo($data);
	}

}
$db	= new cls_mysql(S_Sqlservername,S_Sqluserid,S_Sqlpwd,S_Sqldbname);
$cache_opt = new Cache_Lite(array('_dir'=>_SYC_ROOT_."/".S_CacheFolder."/",'_time'=>S_CacheTime));
?>
