<?php

/*

'**************************************************************************************************

' 装B舞曲PHP版，讨论Q群199607802 装BQQ2682376396

' 装B网 www.zhuangbi.tk

'**************************************************************************************************

*/

require_once("config.php");

require_once("config.inc.php");

require_once("install.php");

require_once("pinyin.php");

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("Can't pConnect MySQL Server!");

		}

		

		mysql_query("SET NAMES " . $charset, $this->link_id);

		

		if ($dbname)

		{

			if (@mysql_select_db($dbname, $this->link_id) === false )

			{

				$this->ErrorMsg("Can't select MySQL database($dbname)!");

				

				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:";

			print_r($this->error_message);

		}

		

		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);

	}



}

        //连接数据库开始

	$cache_opt = new Cache_Lite(array('_dir'=>_CHSHDJ_ROOT_."/".S_CacheFolder."/",'_time'=>S_CacheTime));

	$db	= new cls_mysql(S_Sqlservername,S_Sqluserid,S_Sqlpwd,S_Sqldbname);

?>

