<?php
if (defined('SQLDB')) {
	return;
}
define('SQLDB', true);

class sqldb {
	var $querynum = 0;
	var $mLinkId = false;
	var $mClosed = null;
	var $connectSilent = false;
	var $mDbCharSet = '';

	function connect($dbhost, $dbuser, $dbpw, $dbname = '', $dbcharset = '', $pconnect = 0) {
		if ($pconnect AND function_exists('mysql_pconnect')) {
			if(!($this->mLinkId = @mysql_pconnect($dbhost, $dbuser, $dbpw))) {
				(false === $this->connectSilent) AND $this->halt('Can not connect to MySQL server');
				return false;
			}
		} elseif (function_exists('mysql_connect')) {
			if(!($this->mLinkId = @mysql_connect($dbhost, $dbuser, $dbpw))) {
				(false === $this->connectSilent) AND $this->halt('Can not connect to MySQL server');
				return false;
			}
		} else {
			(false === $this->connectSilent) AND $this->halt('Please enable your mysql library!');
			return false;
		}

		$dbversion = $this->version();
		if ('' !== $dbcharset AND $dbversion >= '4.1') {
			$dbcharset = str_replace('-', '', $dbcharset);
			mysql_query("SET character_set_client='binary', character_set_connection='".$dbcharset."', character_set_results='".$dbcharset."'", $this->mLinkId);
		}

		if($dbversion > '5.0.1') {
			mysql_query("SET sql_mode=''", $this->mLinkId);
		}

		if($dbname) {
			mysql_select_db($dbname, $this->mLinkId);
		}
		$this->mClosed = false;
		function_exists('track_errors') AND @ini_set('track_errors', 1);
		return true;

	}

	function select_db($dbname) {
		return mysql_select_db($dbname, $this->mLinkId);
	}

	function &fetch_array($query, $result_type = MYSQL_ASSOC) {
		$dbinfo = false;
		if (!($dbinfo = mysql_fetch_array($query, $result_type))) {
			$dbinfo = false;
		}
		return $dbinfo;
	}

	function query( $stmt, $opts = array('type'=>'', 'rowOffset'=>null, 'absolutePage'=>0, 'pageSize'=>0) ) {
		if( $stmt == '' ) return null;
		$func = (isset($opts['type']) AND $opts['type'] == 'UNBUFFERED') && @function_exists('mysql_unbuffered_query') ? 'mysql_unbuffered_query' : 'mysql_query';
		if(!isset($opts['pageSize']) OR !is_numeric($opts['pageSize']) ){
			$pageSize = 0;
		} else{
			$pageSize = intval($opts['pageSize']);
		}
		$rowOffset = -1;
		if(isset($opts['pageSize']) AND isset($opts['rowOffset']) AND is_numeric($opts['rowOffset']) && $opts['rowOffset'] >= 0 ){
			$rowOffset = $opts['rowOffset'];
		}
		if( $rowOffset === -1 ){
			if(isset($opts['absolutePage']) AND  is_numeric($opts['absolutePage']) && $opts['absolutePage'] > 0 ){
				$rowOffset = ($opts['absolutePage']-1)*$pageSize;
			}
		}
		if( $rowOffset > 0 && $pageSize > 0 ){
			$stmt .= " LIMIT {$rowOffset},{$pageSize}";
		} elseif( $pageSize > 0 ){
			$stmt .= " LIMIT {$pageSize}";
		}
		if(isset($opts['debug']) AND $opts['debug'] === true ){
			print "\$stmt:\r\n".$stmt."\r\n<br />\r\n";
			return false;
		}
		if (isset($opts['type'])) {
			$type = trim($opts['type']);
		} else {
			$type = '';
		}
		if(!($query = $func($stmt, $this->mLinkId)) && $type != 'SILENT') {
			$this->halt('MySQL Query Error', $stmt);
		}
		$this->querynum++;
		return $query;
	}

	function scalar( $stmt, $opts = array('type'=>'') ){
		if( $stmt == '' ) return false;
		if( $query = $this->query( $stmt , $opts ) ){
			if( $result = $this->fetch_row($query) ){
				$this->free_result($query);
				return $result[0];
			} else{
				return false;
			}
		} else{
			return false;
		}
	}

	function affected_rows() {
		return mysql_affected_rows($this->mLinkId);
	}

	function error() {
		if ($this->mLinkId) {
			return mysql_error($this->mLinkId);
		} else {
			return mysql_error();
		}
	}

	function errno() {
		if ($this->mLinkId) {
			return mysql_errno($this->mLinkId);
		} else {
			return mysql_errno();
		}
	}

	function result($result, $row, $field = null) {
		if (null === $field) {
			$result = @mysql_result($result, $row);
		} else {
			$result = @mysql_result($result, $row, $field);
		}
		return $result;
	}

	function num_rows($result) {
		return mysql_num_rows($result);
	}

	function num_fields($result) {
		return mysql_num_fields($result);
	}

	function free_result($result) {
		return mysql_free_result($result);
	}

	function insert_id() {
		$id = mysql_insert_id($this->mLinkId);
		return $id;
	}

	function &fetch_row($result) {
		$dbinfo = false;
		if (!($dbinfo = mysql_fetch_row($result))) {
			$dbinfo = false;
		}
		return $dbinfo;
	}

	function version() {
		return mysql_get_server_info($this->mLinkId);
	}

	function close() {
		$returnval = true;
		(false === $this->mClosed) AND ($this->mClosed = true) AND ($returnval = mysql_close($this->mLinkId));
		return $returnval;
	}

	function IsClosed()
	{
		return $this->mClosed;
	}//end function IsClosed

	function halt($message = '', $sql = '') {
		global $lang;
		if($this->errno() == 1114) {
			if($GLOBALS['charset'] == 'gbk') {
				print $lang['dv_clssql.str1'];
				exit();
			} else {
				print 'Forum onlines reached the upper limit,please wait for someone else going offline or visit us in idle hours.';
				exit();
			}
		} else {
			empty($message) AND ($message = $this->error());
			$errmsg = "<br><b>Dvbbs.php info</b>: $message";
			print $errmsg;
			print "<br><b>The URL Is</b>:&nbsp;http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];
			print "<br><b>Errno</b>: ".$this->errno();
			print "<br><b>Error</b>: ".$this->error();
			if($sql) {
				print "<br><b>SQL</b>: ".htmlspecialchars($sql);
			}
			print "<br><b>You Can Get Help In:</b>&nbsp;<a href=\"http://p.dvbbs.net\"><b><span style=\"text-decoration: none;color: blue;\">http://p.dvbbs.net</span></b></a>";
			exit();
		}
	}

	function data_seek($query,$rowNumber){
		return mysql_data_seek($query,$rowNumber);
	}

	function escape_string($unescaped_string){
		if( $unescaped_string == '' ){
			return '';
		}

		return mysql_escape_string($unescaped_string);
	}

	function &getResultSet($stmt,$opts=array('resultType'=>MYSQL_ASSOC)){
		$resultSet = false;
		if( $query = $this->query($stmt,$opts) ){
			if( empty($opts['resultType']) ){
				$result_type = MYSQL_ASSOC;
			} else{
				$result_type = $opts['resultType'];
			}
			while( $result =& $this->fetch_array($query,$result_type) ) {
				$resultSet[] =& $result;
			}
			$this->free_result($query);
			if( empty($resultSet) ) return $resultSet;
		}
		return $resultSet;
	}

	function fetch_lengths($query){
		return mysql_fetch_lengths($query);
	}

	function fetch_field($query,$field_offset=null){
		if( is_numeric($field_offset) ){
			return mysql_fetch_field($query,$field_offset);
		} else{
			return mysql_fetch_field($query);
		}
	}
}
?>