HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/PvdBoogaard/indoorski.nl/backup/oude-site/cms/lib/mysqldb/db.php
<?php
/**
* This file only has the base Db class in it. This sets up class variables and basic constructs only.
* Each subclass (different database type) overrides most functions (except logging) and handles database specific instances.
*
* @version     $Id$
* @author Chris <chris@interspire.com>
*
* @package Library
* @subpackage Db
*/

/**
* Make sure our error reporting levels are set properly.
*/
if (!defined('INTERSPIRE_ERROR_FATAL')) define('INTERSPIRE_ERROR_FATAL', E_USER_ERROR);
if (!defined('INTERSPIRE_ERROR_ERROR')) define('INTERSPIRE_ERROR_ERROR', E_USER_WARNING);
if (!defined('INTERSPIRE_ERROR_WARNING')) define('INTERSPIRE_ERROR_WARNING', E_USER_NOTICE);

/**
* This is the base class for the database system.
* Almost all methods are overwritten in subclasses, except for logging and for the 'FetchOne' method.
*
* @package Library
* @subpackage Db
*/
abstract class Db {

	/**
	* The global database connection.
	*
	* @see Connect
	*
	* @var Resource
	*/
	public $connection = null;


	/**
	* Where any database errors are stored.
	*
	* @see GetError
	* @see SetError
	*
	* @access private
	*
	* @var String
	*/
	public $_Error = null;

	/**
	* Are we in the error callback function ? Used to avoid logging errors that happen inside
	* the callback function
	*
	* @var Boolean
	*/
	public $_InErrorCallback = false;

	/**
	* What type of error this is.
	*
	* @see GetError
	* @see SetError
	*
	* @access private
	*
	* @var String
	*/
	public $_ErrorLevel = E_USER_ERROR;


	/**
	* Determines whether a query will be logged or not. If it's false or null it won't log, if it's a filename (or path to a file) it will log.
	* Useful for debug / development purposes to see all the queries being run.
	*
	* @see LogQuery
	*
	* @var String
	*/
	public $QueryLog = null;

	/**
	* Determines whether a time query will be logged or not. If it's false or null it won't log, if it's a filename (or path to a file) it will log.
	* Useful for debug / development purposes to find slow queries.
	*
	* @see TimeQuery
	*
	* @var String
	*/
	public $TimeLog = null;

	/**
	* Determines whether an error will be logged or not. If it's false or null it won't log, if it's a filename (or path to a file) it will log.
	* Useful for debug / development purposes to find queries that create errors.
	*
	* @see LogError
	*
	* @var String
	*/
	public $ErrorLog = null;
	/**
	* Determines whether a query will be logged or not. If it's false or null it won't log, if it's a direcotry it will log.
	*
	* @see QueryLogByDateDir
	*
	* @var String
	*/
	public $QueryLogByDateDir = null;

	/**
	* The number of queries that have been executed
	*
	* @var Integer
	*/
	public $NumQueries = 0;

	/**
	* The database table prefix that is being used.
	*
	* @var String
	*/
	public $TablePrefix = '';
	/**
	* The last query sent to the database
	*
	* @var string
	*/
	public $LastQuery = null;

	/**
	* Constructor
	*
	* Sets up the database connection.
	* Since this is the parent class the others inherit from, this returns null when called directly.
	*
	* @return Null This will always return null in the base class.
	*/
	public function __construct() {
		return null;
	}


	/**
	* Connect
	*
	* This function will connect to the database based on the details passed in.
	* Since this is the parent class the others inherit from, this returns false when called directly.
	*
	* @return False Will always return false when called in the base class.
	*/
	function Connect() {
		$this->SetError('Cannot call base class method Connect directly');
		return false;
	}


	/**
	* Disconnect
	*
	* This function will disconnect from the database resource passed in.
	* Since this is the parent class the others inherit from, this returns false when called directly.
	*
	* @return false
	*/
	function Disconnect() {
		$this->SetError('Cannot call base class method Disconnect directly');
		return false;
	}


	/**
	* SetError
	*
	* Stores the error in the _error var for retrieval.
	*
	* @param String $error The error you wish to store for retrieval.
	* @param String $errorlevel The error level you wish to store.
	*
	* @return Void Doesn't return anything, only sets the values and leaves it at that.
	*/
	function SetError($error='', $errorlevel=E_USER_ERROR)
	{
		$this->_Error = $error;
		$this->_ErrorLevel = $errorlevel;
	}


	/**
	* GetError
	*
	* This simply returns the $_Error var and it's error level.
	*
	* @access public
	*
	* @see SetError
	*
	* @return Array Returns the error and it's error level.
	*/
	function GetError()
	{
		return array($this->_Error, $this->_ErrorLevel);
	}


	/**
	* GetErrorMsg
	*
	* This simply returns the $_Error var
	*
	* @access public
	*
	* @see SetError
	*
	* @return string Returns the error
	*/
	function GetErrorMsg() {
		return $this->_Error;
	}


	/**
	* Query
	*
	* This runs a query against the database and returns the resource result.
	*
	* @access public
	*
	* @return false Will always return false when called in the base class.
	*/
	function Query() {
		$this->SetError('Cannot call base class method Query directly');
		return false;
	}


	/**
	* Fetch
	* This function will fetch a result from the result set passed in.
	*
	* @see Query
	* @see SetError
	*
	* @access public
	*
	* @return false Will always return false when called in the base class.
	*/
	function Fetch() {
		$this->SetError('Cannot call base class method Fetch directly');
		return false;
	}

	/**
	* LogError
	* This will log all errors if ErrorLog is not false or null. Is called from Query.
	*
	* @param String $query The query to log.
	* @param String $error The error message to log.
	*
	* @see ErrorLog
	* @see Query
	*
	* @return Boolean Returns whether the query & error are logged or not. Will return false if there is no query, or if the ErrorLog variable is set to false or null.
	*/
	function LogError($query='', $error=false)
	{
		if (!$query) {
			return false;
		}

		if (!$this->ErrorLog || is_null($this->ErrorLog)) {
			return false;
		}

		if (!$fp = fopen($this->ErrorLog, 'a+')) {
			return false;
		}

		$backtrace = '';
		if (function_exists('debug_backtrace')) {
			$backtrace = debug_backtrace();
			$called_from = $backtrace[1];
			$backtrace = $called_from['file'] . ' (' . $called_from['line'] . ')'. "\t";
		}
		$line = date('M d H:i:s') . "\t" . getmypid() . "\t" . $backtrace . "\t" . "Error!: " . $error . "\t" . $query . "\n";
		fputs($fp, $line, strlen($line));
		fclose($fp);
		return true;
	}

	/**
	* LogQuery
	* This will log all queries if QueryLog is not false or null. Is called from Query.
	*
	* @param string The query to log.
	*
	* @see QueryLog
	* @see Query
	* @see SetError
	*
	* @return boolean Returns whether the query is logged or not. Will return false if there is no query or if the QueryLog variable is set to false or null.
	*/
	function LogQuery($query='') {
		if (!$query) {
			return false;
		}

		if (!$this->QueryLog || is_null($this->QueryLog)) {
			return false;
		}
		if (!$fp = fopen($this->QueryLog, 'a+')) {
			return false;
		}
		$backtrace = '';
		if (function_exists('debug_backtrace')) {
			$backtrace = debug_backtrace();
			$called_from = $backtrace[1];
			$backtrace = $called_from['file'] . ' (' . $called_from['line'] . ')'. "\t";
		}
		$line = date('M d H:i:s') . "\t" . getmypid() . "\t" . $backtrace . $query . "\n";
		fputs($fp, $line, strlen($line));
		fclose($fp);
		return true;
	}

	function TimeQuery($query='', $timestart=0, $timeend=0)
	{
		if (!$this->TimeLog || is_null($this->TimeLog)) {
			return false;
		}

		if (!$query) {
			return false;
		}

		if (!$fp = fopen($this->TimeLog, 'a+')) {
			return false;
		}

		$backtrace = '';
		if (function_exists('debug_backtrace')) {
			$backtrace = debug_backtrace();
			$called_from = $backtrace[1];
			$backtrace = $called_from['file'] . ' (' . $called_from['line'] . ')'. "\t";
		}
		$line = date('M d H:i:s') . "\t" . getmypid() . "\t" . sprintf("%01.2f", ($timeend - $timestart)) . "\t" . $query . "\t" . $backtrace . "\n";
		fputs($fp, $line, strlen($line));
		fclose($fp);
		return true;
	}
	/**
	* LogQueryByDate
	* This will log all queries if QueryLogByDateDir is not false or null. Is called from Query.
	* It stores the queries into files based upon the unix timestamp for 00:00:00 each day.
	*
	* @param string The query to log.
	*
	* @see QueryLog
	* @see Query
	* @see SetError
	*
	* @return boolean Returns whether the query is logged or not. Will return false if there is no query or if the QueryLogByDateDir variable is set to false or null.
	*/
	function LogQueryByDate($query='', $timer = '', $error='', $backtrace='') {
		if (!$query) {
			return false;
		}

		if (!$this->QueryLogByDateDir || is_null($this->QueryLogByDateDir)) {
			return false;
		}

		// determine the filename to use
		$filename = 'querylog_' . mktime(0,0,0) . '.txt';

		if(substr($this->QueryLogByDateDir, -1) != "/"){
			$this->QueryLogByDateDir .= "/";
		}

		if (is_array($backtrace)) {
			$newBacktrace = array();
			foreach ($backtrace as $k => $trace) {
				$newTrace = array();
				$newTrace[] = @$trace['class'];
				$newTrace[] = @$trace['type'];
				$newTrace[] = @$trace['function'];
				$newTrace[] = @$trace['line'];
				$newTrace[] = @$trace['file'];
				$newBacktrace[] = $newTrace;
			}
			$backtrace = serialize($newBacktrace);
		} else {
			$backtrace = '(not an array)';
		}

		$query = str_replace(array("\\", "\r", "\n"), array("\\\\", "\\r", "\\n"), $query);

		$timer = floor($timer * 1000);

		file_put_contents($this->QueryLogByDateDir . $filename, date('Y-m-d H:i:s') ."\t". $_SERVER['REMOTE_ADDR'] ."\t". $_SERVER['REQUEST_URI'] ."\t". $timer ."\t". $error ."\t". $query ."\t". $backtrace ."\n", FILE_APPEND);

		return true;
	}

	/**
	* FreeResult
	* Frees the result from memory. The base class always returns false.
	*
	* @see Query
	* @see SetError
	*
	* @return boolean Whether freeing the result worked or not.
	*/
	function FreeResult() {
		$this->SetError('Can\'t call FreeResult from the base class.');
		return false;
	}

	/**
	* CountResult
	* Returns the number of rows returned for the resource passed in. The base class always returns 0.
	*
	* @see Query
	* @see SetError
	*
	* @return false Always returns false in the base class.
	*/
	function CountResult() {
		$this->SetError('Can\'t call CountResult from the base class.');
		return false;
	}

	/**
	* FetchOne
	* Fetches one item from a result and returns it.
	*
	* @param string Result to fetch the item from.
	* @param string The item to look for and return.
	*
	* @see Fetch
	*
	* @return mixed Returns false if there is no result or item, or if the item doesn't exist in the result. Otherwise returns the item's value.
	*/
	function FetchOne($result=null, $item=null) {
		if (is_null($result)) {
			return false;
		}
		if (!is_resource($result)) {
			$result = $this->Query($result);
		}
		$row = $this->Fetch($result);
		if (!$row) {
			return false;
		}
		if (is_null($item)) {
			$item = key($row);
		}
		if (!isset($row[$item])) {
			return false;
		}
		return (get_magic_quotes_runtime()) ? stripslashes($row[$item]) : $row[$item];
	}

	/**
	* Concat
	* Concatentates multiple strings together. The base class returns nothing - it needs to be overridden for each database type.
	*
	* @return false Always returns false in the base class.
	*/
	function Concat() {
		return false;
	}

	/**
	* stripslashes_array
	* Strips the slashes from all the elements in an entire (multidimensional) array
	*
	* @param mixed Item you want to strip the slashes from
	*
	* @return mixed Returns the same type as passed in
	*/
	function stripslashes_array($value)
	{
		$value = is_array($value) ? array_map($this->{'stripslashes_array'}, $value) : stripslashes($value);
		return $value;
	}

	/**
	* Quote
	*
	* Quotes the string ready for database queries. The subclasses should use their db specific methods. The base class (here) runs addslashes.
	*
	* @return String String with quotes!
	*/
	function Quote($string='') {
		return addslashes($string);
	}
	/**
	* LastId
	*
	* Returns the last insert id
	*
	* @param String $seq The sequence name to fetch the last id from.
	*
	* @return False The base class always returns false.
	*/
	function LastId($seq='')
	{
		$this->SetError('Can\'t call LastId from the base class.');
		return false;
	}
	/**
	* NextId
	*
	* Returns the next insert id
	*
	* @param String $seq The sequence name to fetch the next id from.
	*
	* @return False The base class always returns false.
	*/
	function NextId($seq='')
	{
		$this->SetError('Can\'t call NextId from the base class.');
		return false;
	}

	/**
	* CheckSequence
	*
	* Checks to make sure a sequence doesn't have multiple entries.
	*
	* @return False The base class always returns false.
	*/
	function CheckSequence()
	{
		$this->SetError('Can\'t call CheckSequence from the base class.');
		return false;
	}

	/**
	* ResetSequence
	*
	* Resets a field in a sequence table.
	*
	* @return False The base class always returns false.
	*/
	function ResetSequence()
	{
		$this->SetError('Can\'t call ResetSequence from the base class.');
		return false;
	}

	/**
	* OptimizeTable
	*
	* Runs "optimize" or "analyze" over the tablename passed in. This is useful to keep the database reasonably speedy.
	*
	* @param String $tablename The tablename to optimize.
	*
	* @return False The base class always returns false.
	*/
	function OptimizeTable($tablename='')
	{
		$this->SetError('Can\'t call OptimizeTable from the base class.');
		return false;
	}

	/**
	* GetTime
	*
	* Returns a float microtime so we can record how long it took to get a result from the database.
	*
	* @see TimeQuery
	* @see MySQLDB::Query
	* @see PgSQLDB::Query
	*
	* @return Float Returns a float microtime.
	*/
	function GetTime()
	{
		return microtime(true);
	}

	function SetTablePrefix($prefix){
		$this->TablePrefix = $prefix;
	}
}

?>