⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mssql_driver.inc

📁 基础原形来自 Ourplus,修补跨站脚本攻击漏洞 采用PHP+Mysql+Xml开发的网站流量统计分析系统 ,不影响页面下载速度。 本流量统计系统采用了高效的程序算法和精心优化的数据库结构
💻 INC
字号:
<?php

/**
 * ADOdb Lite is a PHP class to encapsulate multiple database APIs and is compatible with 
 * a subset of the ADODB Command Syntax. 
 * Currently supports Frontbase, MaxDB, miniSQL, MSSQL, MSSQL Pro, MySQLi, MySQLt, MySQL, PostgresSQL,
 * PostgresSQL64, PostgresSQL7, SqLite and Sybase.
 * 
 */

class mssql_ADOConnection extends ADOConnection
{
	function mssql_ADOConnection( $dbtype )
	{
		$this->dbtype = strtolower( $dbtype );
	}

	/**
	 * Connection to database server and selected database
	 * 
	 * @access private 
	 */

	function _connect($host = "", $username = "", $password = "", $database = "", $persistent, $forcenew)
	{
		if (!function_exists('mssql_pconnect')) return false;

		if ($host != "") $this->host = $host;
		if ($username != "") $this->username = $username;
		if ($password != "") $this->password = $password;
		if ($database != "") $this->database = $database;		
		$this->persistent = $persistent;
		$this->forcenewconnection = $forcenew;

		if($this->persistent == 1)
		{
			$this->connectionId = @mssql_pconnect( $this->host, $this->username, $this->password );
		}
		else
		{
			$this->connectionId = @mssql_connect( $this->host, $this->username, $this->password );
		}

		if ($this->connectionId === false) return false;
		if (!empty($this->database)) return $this->SelectDB( $this->database );
		return true;
	} 

	/**
	 * Choose a database to connect.
	 *
	 * @param dbname 	is the name of the database to select
	 * @return 		true or false
	 * @access public
	 */

	function SelectDB($dbname)
	{
		$this->database = $dbname;

		if ($this->connectionId === false)
		{
			$this->connectionId = false;
			return false;
		}
		else
		{
			$result = @mssql_select_db( $this->database, $this->connectionId );

			if($result === false)
			{
				if($this->createdatabase == true)
				{
					$result = @mssql_query( "CREATE DATABASE " . $this->database, $this->connectionId );
					if ($result === false) { // error handling if query fails
						return false;
					} 
					$result = @mssql_select_db( $this->database, $this->connectionId );
					if($result === false)
					{
						return false;
					}
				}
				else
				{
					return false;
				}
			}
			return true;
		}
	} 

	/**
	 * Return database error message
	 * Usage: $errormessage =& $db->ErrorMsg();
	 * 
	 * @access public
	 */

	function ErrorMsg()
	{
		return @mssql_get_last_message();
	}

	/**
	 * Return database error number
	 * Usage: $errorbo =& $db->ErrorNo();
	 * 
	 * @access public
	 */

	function ErrorNo()
	{
		$result = @mssql_query("select @@ERROR",$this->connectionId);
		if (!$result) return false;
		$array = mssql_fetch_array($result);
		@mssql_free_result($result);
		if (is_array($array)) return $array[0];
		else return false;
	}

	/**
	* PEAR DB Compat - do not use internally
	*/
	function ErrorNative()
	{
		return $this->ErrorNo();
	}

	/**
	 * Returns # of affected rows from insert/delete/update query
	 * 
	 * @access public 
	 * @return integer Affected rows
	 */

	function Affected_Rows()
	{
		return @mssql_rows_affected($this->connectionId);
	} 

	/**
	 * Returns the last record id of an inserted item
	 * Usage: $db->Insert_ID();
	 * 
	 * @access public 
	 */

	function Insert_ID()
	{
		return false;
	}

	/**
	 * Correctly quotes a string so that all strings are escape coded.
	 * An example is  $db->qstr("Haven't a clue.");
	 * 
	 * @param string			the string to quote
	 * @param [magic_quotes]	if $s is GET/POST var, set to get_magic_quotes_gpc().
	 *
	 * @return  single-quoted string IE: 'Haven\'t a clue.'
	 */

	function qstr($string, $magic_quotes=false)
	{	
		if (!$magic_quotes) {
			return  "'".str_replace("'", "''", $string)."'";
		}
		$string = str_replace("\\'", "''", str_replace('\\\\', '\\', str_replace('\\"', '"', $string)));
		return "'" . $string . "'";
	}

	/**
	* PEAR DB Compat - do not use internally
	*/
	function Quote($string)
	{
		return $this->qstr($string, false);
	}

	function QMagic($string)
	{
		return $this->qstr($string, get_magic_quotes_gpc());
	}

	/**
	 * Returns concatenated string
	 * Usage: $db->Concat($str1,$str2);
	 * 
	 * @return concatenated string
	 */
	function Concat()
	{
		$s = "";
		$arr = func_get_args();

		if (sizeof($arr) == 1) {
			foreach ($arr as $arg) {
				$args = explode(',', $arg);
			}
			$arr = $args;
		}

		array_walk($arr, create_function('&$v', '$v = "CAST(" . $v . " AS VARCHAR(255))";'));
		$s = implode('+',$arr);
		if (sizeof($arr) > 0) return "$s";
		return '';
	}

	function IfNull( $field, $ifNull ) 
	{
		return " ISNULL($field, $ifNull) ";
	}

	/**
	 * Closes database connection
	 * Usage: $db->close();
	 * 
	 * @access public 
	 */

	function Close()
	{
		@mssql_close( $this->connectionId );
		$this->connectionId = false;
	}

}

class mssqlStatement
{
	var $connection;
	var $offset;
	var $nrows;
	var $sql;
	var $inputarr;
	var $extention;

	/**
	 * mysqlStatement Constructor
	 * 
	 * @access private 
	 * @param string $connection 
	 * @param string $parameters 
	 * @param string $sql 
	 */

	function mssqlStatement( $sql, &$connection, $offset, $nrows, $inputarr=false )
	{
		$this->connection = &$connection;
		$this->offset = $offset;
		$this->nrows = $nrows;
		$this->sql = $sql;
		$this->inputarr = $inputarr;
	} 

	/**
	 * Executes SQL query and instantiates resultset methods
	 * 
	 * @access private 
	 * @return mixed Resultset methods
	 */

	function &do_query()
	{
		global $ADODB_FETCH_MODE;

		$false = false;

		if ($this->inputarr && is_array($this->inputarr)) {
			$sqlarr = explode('?', $this->sql);
			if (!is_array(reset($this->inputarr))) $this->inputarr = array($this->inputarr);
			foreach($this->inputarr as $arr) {
				$sql = ''; $i = 0;
				foreach($arr as $v) {
					$sql .= $sqlarr[$i];
					switch(gettype($v)){
						case 'string':
							$sql .= $this->extention->qstr($v);
							break;
						case 'double':
							$sql .= str_replace(',', '.', $v);
							break;
						case 'boolean':
							$sql .= $v ? 1 : 0;
							break;
						default:
							if ($v === null)
								$sql .= 'NULL';
							else $sql .= $v;
					}
					$i += 1;
				}
				$sql .= $sqlarr[$i];
				if ($i+1 != sizeof($sqlarr))	
					return $false;
				$resultId = @mssql_query( $sql );
				if($this->extention->debug)
				{
					$this->extention->debugoutput($sql);
				}
				if ($resultId === false) return $false;
			}
		}
		else
		{
				$resultId = @mssql_query( $this->sql );
				if($this->extention->debug)
				{
					$this->extention->debugoutput($this->sql);
				}
		}

		if ($resultId === false) { // error handling if query fails
			return $false;
		} 

		if ($resultId === true) { // return simplified recordset for inserts/updates/deletes with lower overhead
			$rs =& new ADORecordSet_empty();
			return $rs;
		}

		$recordset = new mssqlResultSet( $resultId, $this->connection );

		$recordset->_currentRow = 0;

		switch ($ADODB_FETCH_MODE)
		{
			case ADODB_FETCH_NUM: $recordset->fetchMode = MSSQL_NUM; break;
			case ADODB_FETCH_ASSOC:$recordset->fetchMode = MSSQL_ASSOC; break;
			default:
			case ADODB_FETCH_DEFAULT:
			case ADODB_FETCH_BOTH:$recordset->fetchMode = MSSQL_BOTH; break;
		}

		$recordset->_numOfRows = @mssql_num_rows( $resultId );
		$recordset->_numOfFields = @mssql_num_fields( $resultId );

		if ($this->offset != -1 || $this->nrows != -1)
		{
			if($this->offset == -1 || ($this->offset == 0 && $this->nrows != -1))
			{
				$recordset->_numOfRows = ($this->nrows < $recordset->_numOfRows) ? $this->nrows : $recordset->_numOfRows;
				$recordset->_fetch();
			}
			else
			{
				if($this->offset > $recordset->_numOfRows)
				{
					$rs =& new ADORecordSet_empty();
					return $rs;
				}

				$recordset->_fetch();
				for($i = 0; $i < $this->offset; $i++)
				{
					$recordset->MoveNext();
				}

				$recordset->_currentRow = 0;
				if($this->nrows != -1)
				{
					$recordset->_numOfRows = ($this->nrows < ($recordset->_numOfRows - $this->offset)) ? $this->nrows : $recordset->_numOfRows - $this->offset;
				}
				else
				{
					$recordset->_numOfRows -= $this->offset;
				}
			}
		}
		else
		{
			$recordset->_fetch();
		}

		return $recordset;
	} 
} 

/**
 * Empty result record set for updates, inserts, ect
 * 
 * @access private 
 */

class ADORecordSet_empty
{
	var $fields = false;
	var $EOF = true;
	function MoveNext() {return;}
	function RecordCount() {return 0;}
	function FieldCount() {return 0;}
	function EOF(){return TRUE;}
	function Close(){return true;}
}

class mssqlResultSet
{
	var $connectionId;
	var $fields;
	var $resultId;
	var $_currentRow = 0;
	var $_numOfRows = -1;
	var $_numOfFields = -1;
	var $fetchMode;
	var $EOF;

	/**
	 * mssqlResultSet Constructor
	 * 
	 * @access private 
	 * @param string $record 
	 * @param string $resultId 
	 */

	function mssqlResultSet( $resultId, $connectionId )
	{
		$this->fields = array();
		$this->connectionId = $connectionId;
		$this->record = array();
		$this->resultId = $resultId;
		$this->EOF = false;
	} 

	/**
	 * Frees resultset
	 * 
	 * @access public 
	 */

	function close()
	{
		@mssql_free_result( $this->resultId );
		$this->fields = array();
		$this->resultId = false;
	} 

	/**
	* PEAR DB Compatable Command
	*/
	function Free()
	{
		return $this->Close();
	}

	/**
	 * Returns field name from select query
	 * 
	 * @access public 
	 * @param string $field
	 * @return string Field name
	 */

	function fields( $field )
	{
		return $this->fields[$field];
	} 

	/**
	 * Returns numrows from select query
	 * 
	 * @access public 
	 * @return integer Numrows
	 */

	function RecordCount()
	{
		return $this->_numOfRows;
	} 

	/**
	* PEAR DB Compatable Command
	*/
	function NumRows()
	{
		return $this->_numOfRows;
	}

	/**
	 * Returns num of fields from select query
	 * 
	 * @access public 
	 * @return integer numfields
	 */

	function FieldCount()
	{
		return $this->_numOfFields;
	} 

	/**
	* PEAR DB Compatable Command
	*/
	function NumCols()
	{
		return $this->_numOfFields;
	}

	/**
	 * Returns next record
	 * 
	 * @access public 
	 */

	function MoveNext()
	{
		if (@$this->fields = mssql_fetch_array($this->resultId,$this->fetchMode)) {
			$this->_currentRow += 1;
			return true;
		}
		if (!$this->EOF) {
			$this->_currentRow += 1;
			$this->EOF = true;
		}
		return false;
	} 

	/**
	 * Move to the first row in the recordset. Many databases do NOT support this.
	 *
	 * @return true or false
	 */

	function MoveFirst() 
	{
		if ($this->_currentRow == 0) return true;
		return $this->Move(0);			
	}			

	/**
	 * Returns the Last Record
	 * 
	 * @access public 
	 */

	function MoveLast()
	{
		if ($this->EOF) return false;
		while (!$this->EOF) {
			$fields = $this->fields;
			$this->MoveNext();
		}
		$this->fields = $fields;
		return true;
	} 

	/**
	 * Random access to a specific row in the recordset. Some databases do not support
	 * access to previous rows in the databases (no scrolling backwards).
	 *
	 * @param rowNumber is the row to move to (0-based)
	 *
	 * @return true if there still rows available, or false if there are no more rows (EOF).
	 */

	function Move($rowNumber = 0) 
	{
		$this->EOF = false;
		if ($rowNumber == $this->_currentRow) return true;
		if ($rowNumber >= $this->_numOfRows)
	   		if ($this->_numOfRows != -1) $rowNumber = $this->_numOfRows-2;
  				
		if ($this->_seek($rowNumber)) {
			$this->_currentRow = $rowNumber;
			if ($this->_fetch()) {
				return true;
			}
		} else {
			$this->EOF = true;
			return false;
		}
	}

	/**
	 * Perform Seek to specific row
	 * 
	 * @access private 
	 */

	function _seek($row)
	{
		if ($this->_numOfRows == 0) return false;
		return @mssql_data_seek($this->resultId,$row);
	}

	/**
	 * Fills field array with first database element when query initially executed
	 * 
	 * @access private 
	 */

	function _fetch()
	{
		$this->fields = @mssql_fetch_array($this->resultId,$this->fetchMode);
		if( $this->_numOfRows == 0)
		{
			$this->EOF = true;
		}
		return is_array($this->fields);
	}

	/**
	 * Check to see if last record reached
	 * 
	 * @access public 
	 */

	function EOF()
	{
		if( $this->_currentRow < $this->_numOfRows)
		{
			return false;
		}
		else
		{
			$this->EOF = true;
			return true;
		}
	} 

	/**
	 * Returns All Records in an array
	 * 
	 * @access public 
	 * @param [nRows]  is the number of rows to return. -1 means every row.
	 */

	function GetArray($nRows = -1)
	{
		$results = array();
		$cnt = 0;
		while (!$this->EOF && $nRows != $cnt) {
			$results[] = $this->fields;
			$this->MoveNext();
			$cnt++;
		}
		return $results;
	} 

	function &GetRows($nRows = -1) 
	{
		$arr =& $this->GetArray($nRows);
		return $arr;
	}

	function &GetAll($nRows = -1)
	{
		$arr =& $this->GetArray($nRows);
		return $arr;
	}
	/**
	* Fetch a row, returning false if no more rows. 
	* PEAR DB Compatable Command
	*
	* @return false or array containing the current record
	*/
	function FetchRow()
	{
		if ($this->EOF) {
			$false = false;
			return $false;
		}
		$arr = $this->fields;
		$this->_currentRow++;
		if (!$this->_fetch()) $this->EOF = true;
		return $arr;
	}

	/**
	* Fetch a row, returning PEAR_Error if no more rows. 
	* PEAR DB Compatable Command
	*
	*/
	function FetchInto(&$arr)
	{
		$false = false;
		$true = 1;
		if ($this->EOF) return $false;
		$arr = $this->fields;
		$this->MoveNext();
		return $true;
	}

	/**
	* Fetch field information for a table. 
	*
	* @return object containing the name, type and max_length
	*/
	function FetchField($fieldOffset = -1) 
	{
		if ($fieldOffset != -1) {
			$fieldObject = @mssql_fetch_field($this->resultId, $fieldOffset);
		}
		else
		{
			$fieldObject = @mssql_fetch_field($this->resultId);
		}
		$false = false;
		if (empty($fieldObject)) return $false;
		return $fieldObject;
	}
}
?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -