Blame Identity/Models/Html/phpBB/3.0.4/includes/db/dbal.php

d6e8d8
d6e8d8
/**
d6e8d8
*
d6e8d8
* @package dbal
d6e8d8
* @version $Id: dbal.php 9178 2008-12-06 11:11:10Z acydburn $
d6e8d8
* @copyright (c) 2005 phpBB Group
d6e8d8
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
d6e8d8
*
d6e8d8
*/
d6e8d8
d6e8d8
/**
d6e8d8
* @ignore
d6e8d8
*/
d6e8d8
if (!defined('IN_PHPBB'))
d6e8d8
{
d6e8d8
	exit;
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* Database Abstraction Layer
d6e8d8
* @package dbal
d6e8d8
*/
d6e8d8
class dbal
d6e8d8
{
d6e8d8
	var $db_connect_id;
d6e8d8
	var $query_result;
d6e8d8
	var $return_on_error = false;
d6e8d8
	var $transaction = false;
d6e8d8
	var $sql_time = 0;
d6e8d8
	var $num_queries = array();
d6e8d8
	var $open_queries = array();
d6e8d8
d6e8d8
	var $curtime = 0;
d6e8d8
	var $query_hold = '';
d6e8d8
	var $html_hold = '';
d6e8d8
	var $sql_report = '';
d6e8d8
d6e8d8
	var $persistency = false;
d6e8d8
	var $user = '';
d6e8d8
	var $server = '';
d6e8d8
	var $dbname = '';
d6e8d8
d6e8d8
	// Set to true if error triggered
d6e8d8
	var $sql_error_triggered = false;
d6e8d8
d6e8d8
	// Holding the last sql query on sql error
d6e8d8
	var $sql_error_sql = '';
d6e8d8
	// Holding the error information - only populated if sql_error_triggered is set
d6e8d8
	var $sql_error_returned = array();
d6e8d8
d6e8d8
	// Holding transaction count
d6e8d8
	var $transactions = 0;
d6e8d8
d6e8d8
	// Supports multi inserts?
d6e8d8
	var $multi_insert = false;
d6e8d8
d6e8d8
	/**
d6e8d8
	* Current sql layer
d6e8d8
	*/
d6e8d8
	var $sql_layer = '';
d6e8d8
d6e8d8
	/**
d6e8d8
	* Wildcards for matching any (%) or exactly one (_) character within LIKE expressions
d6e8d8
	*/
d6e8d8
	var $any_char;
d6e8d8
	var $one_char;
d6e8d8
d6e8d8
	/**
d6e8d8
	* Exact version of the DBAL, directly queried
d6e8d8
	*/
d6e8d8
	var $sql_server_version = false;
d6e8d8
d6e8d8
	/**
d6e8d8
	* Constructor
d6e8d8
	*/
d6e8d8
	function dbal()
d6e8d8
	{
d6e8d8
		$this->num_queries = array(
d6e8d8
			'cached'		=> 0,
d6e8d8
			'normal'		=> 0,
d6e8d8
			'total'			=> 0,
d6e8d8
		);
d6e8d8
d6e8d8
		// Fill default sql layer based on the class being called.
d6e8d8
		// This can be changed by the specified layer itself later if needed.
d6e8d8
		$this->sql_layer = substr(get_class($this), 5);
d6e8d8
d6e8d8
		// Do not change this please! This variable is used to easy the use of it - and is hardcoded.
d6e8d8
		$this->any_char = chr(0) . '%';
d6e8d8
		$this->one_char = chr(0) . '_';
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* return on error or display error message
d6e8d8
	*/
d6e8d8
	function sql_return_on_error($fail = false)
d6e8d8
	{
d6e8d8
		$this->sql_error_triggered = false;
d6e8d8
		$this->sql_error_sql = '';
d6e8d8
d6e8d8
		$this->return_on_error = $fail;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Return number of sql queries and cached sql queries used
d6e8d8
	*/
d6e8d8
	function sql_num_queries($cached = false)
d6e8d8
	{
d6e8d8
		return ($cached) ? $this->num_queries['cached'] : $this->num_queries['normal'];
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Add to query count
d6e8d8
	*/
d6e8d8
	function sql_add_num_queries($cached = false)
d6e8d8
	{
d6e8d8
		$this->num_queries['cached'] += ($cached !== false) ? 1 : 0;
d6e8d8
		$this->num_queries['normal'] += ($cached !== false) ? 0 : 1;
d6e8d8
		$this->num_queries['total'] += 1;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* DBAL garbage collection, close sql connection
d6e8d8
	*/
d6e8d8
	function sql_close()
d6e8d8
	{
d6e8d8
		if (!$this->db_connect_id)
d6e8d8
		{
d6e8d8
			return false;
d6e8d8
		}
d6e8d8
d6e8d8
		if ($this->transaction)
d6e8d8
		{
d6e8d8
			do
d6e8d8
			{
d6e8d8
				$this->sql_transaction('commit');
d6e8d8
			}
d6e8d8
			while ($this->transaction);
d6e8d8
		}
d6e8d8
d6e8d8
		foreach ($this->open_queries as $query_id)
d6e8d8
		{
d6e8d8
			$this->sql_freeresult($query_id);
d6e8d8
		}
d6e8d8
d6e8d8
		// Connection closed correctly. Set db_connect_id to false to prevent errors
d6e8d8
		if ($result = $this->_sql_close())
d6e8d8
		{
d6e8d8
			$this->db_connect_id = false;
d6e8d8
		}
d6e8d8
d6e8d8
		return $result;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Build LIMIT query
d6e8d8
	* Doing some validation here.
d6e8d8
	*/
d6e8d8
	function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
d6e8d8
	{
d6e8d8
		if (empty($query))
d6e8d8
		{
d6e8d8
			return false;
d6e8d8
		}
d6e8d8
d6e8d8
		// Never use a negative total or offset
d6e8d8
		$total = ($total < 0) ? 0 : $total;
d6e8d8
		$offset = ($offset < 0) ? 0 : $offset;
d6e8d8
d6e8d8
		return $this->_sql_query_limit($query, $total, $offset, $cache_ttl);
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Fetch all rows
d6e8d8
	*/
d6e8d8
	function sql_fetchrowset($query_id = false)
d6e8d8
	{
d6e8d8
		if ($query_id === false)
d6e8d8
		{
d6e8d8
			$query_id = $this->query_result;
d6e8d8
		}
d6e8d8
d6e8d8
		if ($query_id !== false)
d6e8d8
		{
d6e8d8
			$result = array();
d6e8d8
			while ($row = $this->sql_fetchrow($query_id))
d6e8d8
			{
d6e8d8
				$result[] = $row;
d6e8d8
			}
d6e8d8
d6e8d8
			return $result;
d6e8d8
		}
d6e8d8
d6e8d8
		return false;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Fetch field
d6e8d8
	* if rownum is false, the current row is used, else it is pointing to the row (zero-based)
d6e8d8
	*/
d6e8d8
	function sql_fetchfield($field, $rownum = false, $query_id = false)
d6e8d8
	{
d6e8d8
		global $cache;
d6e8d8
d6e8d8
		if ($query_id === false)
d6e8d8
		{
d6e8d8
			$query_id = $this->query_result;
d6e8d8
		}
d6e8d8
d6e8d8
		if ($query_id !== false)
d6e8d8
		{
d6e8d8
			if ($rownum !== false)
d6e8d8
			{
d6e8d8
				$this->sql_rowseek($rownum, $query_id);
d6e8d8
			}
d6e8d8
d6e8d8
			if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
d6e8d8
			{
d6e8d8
				return $cache->sql_fetchfield($query_id, $field);
d6e8d8
			}
d6e8d8
d6e8d8
			$row = $this->sql_fetchrow($query_id);
d6e8d8
			return (isset($row[$field])) ? $row[$field] : false;
d6e8d8
		}
d6e8d8
d6e8d8
		return false;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Correctly adjust LIKE expression for special characters
d6e8d8
	* Some DBMS are handling them in a different way
d6e8d8
	*
d6e8d8
	* @param string $expression The expression to use. Every wildcard is escaped, except $this->any_char and $this->one_char
d6e8d8
	* @return string LIKE expression including the keyword!
d6e8d8
	*/
d6e8d8
	function sql_like_expression($expression)
d6e8d8
	{
d6e8d8
		$expression = str_replace(array('_', '%'), array("\_", "\%"), $expression);
d6e8d8
		$expression = str_replace(array(chr(0) . "\_", chr(0) . "\%"), array('_', '%'), $expression);
d6e8d8
d6e8d8
		return $this->_sql_like_expression('LIKE \'' . $this->sql_escape($expression) . '\'');
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* SQL Transaction
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function sql_transaction($status = 'begin')
d6e8d8
	{
d6e8d8
		switch ($status)
d6e8d8
		{
d6e8d8
			case 'begin':
d6e8d8
				// If we are within a transaction we will not open another one, but enclose the current one to not loose data (prevening auto commit)
d6e8d8
				if ($this->transaction)
d6e8d8
				{
d6e8d8
					$this->transactions++;
d6e8d8
					return true;
d6e8d8
				}
d6e8d8
d6e8d8
				$result = $this->_sql_transaction('begin');
d6e8d8
d6e8d8
				if (!$result)
d6e8d8
				{
d6e8d8
					$this->sql_error();
d6e8d8
				}
d6e8d8
d6e8d8
				$this->transaction = true;
d6e8d8
			break;
d6e8d8
d6e8d8
			case 'commit':
d6e8d8
				// If there was a previously opened transaction we do not commit yet... but count back the number of inner transactions
d6e8d8
				if ($this->transaction && $this->transactions)
d6e8d8
				{
d6e8d8
					$this->transactions--;
d6e8d8
					return true;
d6e8d8
				}
d6e8d8
d6e8d8
				// Check if there is a transaction (no transaction can happen if there was an error, with a combined rollback and error returning enabled)
d6e8d8
				// This implies we have transaction always set for autocommit db's
d6e8d8
				if (!$this->transaction)
d6e8d8
				{
d6e8d8
					return false;
d6e8d8
				}
d6e8d8
d6e8d8
				$result = $this->_sql_transaction('commit');
d6e8d8
d6e8d8
				if (!$result)
d6e8d8
				{
d6e8d8
					$this->sql_error();
d6e8d8
				}
d6e8d8
d6e8d8
				$this->transaction = false;
d6e8d8
				$this->transactions = 0;
d6e8d8
			break;
d6e8d8
d6e8d8
			case 'rollback':
d6e8d8
				$result = $this->_sql_transaction('rollback');
d6e8d8
				$this->transaction = false;
d6e8d8
				$this->transactions = 0;
d6e8d8
			break;
d6e8d8
d6e8d8
			default:
d6e8d8
				$result = $this->_sql_transaction($status);
d6e8d8
			break;
d6e8d8
		}
d6e8d8
d6e8d8
		return $result;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Build sql statement from array for insert/update/select statements
d6e8d8
	*
d6e8d8
	* Idea for this from Ikonboard
d6e8d8
	* Possible query values: INSERT, INSERT_SELECT, UPDATE, SELECT
d6e8d8
	*
d6e8d8
	*/
d6e8d8
	function sql_build_array($query, $assoc_ary = false)
d6e8d8
	{
d6e8d8
		if (!is_array($assoc_ary))
d6e8d8
		{
d6e8d8
			return false;
d6e8d8
		}
d6e8d8
d6e8d8
		$fields = $values = array();
d6e8d8
d6e8d8
		if ($query == 'INSERT' || $query == 'INSERT_SELECT')
d6e8d8
		{
d6e8d8
			foreach ($assoc_ary as $key => $var)
d6e8d8
			{
d6e8d8
				$fields[] = $key;
d6e8d8
d6e8d8
				if (is_array($var) && is_string($var[0]))
d6e8d8
				{
d6e8d8
					// This is used for INSERT_SELECT(s)
d6e8d8
					$values[] = $var[0];
d6e8d8
				}
d6e8d8
				else
d6e8d8
				{
d6e8d8
					$values[] = $this->_sql_validate_value($var);
d6e8d8
				}
d6e8d8
			}
d6e8d8
d6e8d8
			$query = ($query == 'INSERT') ? ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')' : ' (' . implode(', ', $fields) . ') SELECT ' . implode(', ', $values) . ' ';
d6e8d8
		}
d6e8d8
		else if ($query == 'MULTI_INSERT')
d6e8d8
		{
d6e8d8
			trigger_error('The MULTI_INSERT query value is no longer supported. Please use sql_multi_insert() instead.', E_USER_ERROR);
d6e8d8
		}
d6e8d8
		else if ($query == 'UPDATE' || $query == 'SELECT')
d6e8d8
		{
d6e8d8
			$values = array();
d6e8d8
			foreach ($assoc_ary as $key => $var)
d6e8d8
			{
d6e8d8
				$values[] = "$key = " . $this->_sql_validate_value($var);
d6e8d8
			}
d6e8d8
			$query = implode(($query == 'UPDATE') ? ', ' : ' AND ', $values);
d6e8d8
		}
d6e8d8
d6e8d8
		return $query;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Build IN or NOT IN sql comparison string, uses <> or = on single element
d6e8d8
	* arrays to improve comparison speed
d6e8d8
	*
d6e8d8
	* @access public
d6e8d8
	* @param	string	$field				name of the sql column that shall be compared
d6e8d8
	* @param	array	$array				array of values that are allowed (IN) or not allowed (NOT IN)
d6e8d8
	* @param	bool	$negate				true for NOT IN (), false for IN () (default)
d6e8d8
	* @param	bool	$allow_empty_set	If true, allow $array to be empty, this function will return 1=1 or 1=0 then. Default to false.
d6e8d8
	*/
d6e8d8
	function sql_in_set($field, $array, $negate = false, $allow_empty_set = false)
d6e8d8
	{
d6e8d8
		if (!sizeof($array))
d6e8d8
		{
d6e8d8
			if (!$allow_empty_set)
d6e8d8
			{
d6e8d8
				// Print the backtrace to help identifying the location of the problematic code
d6e8d8
				$this->sql_error('No values specified for SQL IN comparison');
d6e8d8
			}
d6e8d8
			else
d6e8d8
			{
d6e8d8
				// NOT IN () actually means everything so use a tautology
d6e8d8
				if ($negate)
d6e8d8
				{
d6e8d8
					return '1=1';
d6e8d8
				}
d6e8d8
				// IN () actually means nothing so use a contradiction
d6e8d8
				else
d6e8d8
				{
d6e8d8
					return '1=0';
d6e8d8
				}
d6e8d8
			}
d6e8d8
		}
d6e8d8
d6e8d8
		if (!is_array($array))
d6e8d8
		{
d6e8d8
			$array = array($array);
d6e8d8
		}
d6e8d8
d6e8d8
		if (sizeof($array) == 1)
d6e8d8
		{
d6e8d8
			@reset($array);
d6e8d8
			$var = current($array);
d6e8d8
d6e8d8
			return $field . ($negate ? ' <> ' : ' = ') . $this->_sql_validate_value($var);
d6e8d8
		}
d6e8d8
		else
d6e8d8
		{
d6e8d8
			return $field . ($negate ? ' NOT IN ' : ' IN ') . '(' . implode(', ', array_map(array($this, '_sql_validate_value'), $array)) . ')';
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Run more than one insert statement.
d6e8d8
	*
d6e8d8
	* @param string $table table name to run the statements on
d6e8d8
	* @param array &$sql_ary multi-dimensional array holding the statement data.
d6e8d8
	*
d6e8d8
	* @return bool false if no statements were executed.
d6e8d8
	* @access public
d6e8d8
	*/
d6e8d8
	function sql_multi_insert($table, &$sql_ary)
d6e8d8
	{
d6e8d8
		if (!sizeof($sql_ary))
d6e8d8
		{
d6e8d8
			return false;
d6e8d8
		}
d6e8d8
d6e8d8
		if ($this->multi_insert)
d6e8d8
		{
d6e8d8
			$ary = array();
d6e8d8
			foreach ($sql_ary as $id => $_sql_ary)
d6e8d8
			{
d6e8d8
				// If by accident the sql array is only one-dimensional we build a normal insert statement
d6e8d8
				if (!is_array($_sql_ary))
d6e8d8
				{
d6e8d8
					$this->sql_query('INSERT INTO ' . $table . ' ' . $this->sql_build_array('INSERT', $sql_ary));
d6e8d8
					return true;
d6e8d8
				}
d6e8d8
d6e8d8
				$values = array();
d6e8d8
				foreach ($_sql_ary as $key => $var)
d6e8d8
				{
d6e8d8
					$values[] = $this->_sql_validate_value($var);
d6e8d8
				}
d6e8d8
				$ary[] = '(' . implode(', ', $values) . ')';
d6e8d8
			}
d6e8d8
d6e8d8
			$this->sql_query('INSERT INTO ' . $table . ' ' . ' (' . implode(', ', array_keys($sql_ary[0])) . ') VALUES ' . implode(', ', $ary));
d6e8d8
		}
d6e8d8
		else
d6e8d8
		{
d6e8d8
			foreach ($sql_ary as $ary)
d6e8d8
			{
d6e8d8
				if (!is_array($ary))
d6e8d8
				{
d6e8d8
					return false;
d6e8d8
				}
d6e8d8
d6e8d8
				$this->sql_query('INSERT INTO ' . $table . ' ' . $this->sql_build_array('INSERT', $ary));
d6e8d8
			}
d6e8d8
		}
d6e8d8
d6e8d8
		return true;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Function for validating values
d6e8d8
	* @access private
d6e8d8
	*/
d6e8d8
	function _sql_validate_value($var)
d6e8d8
	{
d6e8d8
		if (is_null($var))
d6e8d8
		{
d6e8d8
			return 'NULL';
d6e8d8
		}
d6e8d8
		else if (is_string($var))
d6e8d8
		{
d6e8d8
			return "'" . $this->sql_escape($var) . "'";
d6e8d8
		}
d6e8d8
		else
d6e8d8
		{
d6e8d8
			return (is_bool($var)) ? intval($var) : $var;
d6e8d8
		}
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Build sql statement from array for select and select distinct statements
d6e8d8
	*
d6e8d8
	* Possible query values: SELECT, SELECT_DISTINCT
d6e8d8
	*/
d6e8d8
	function sql_build_query($query, $array)
d6e8d8
	{
d6e8d8
		$sql = '';
d6e8d8
		switch ($query)
d6e8d8
		{
d6e8d8
			case 'SELECT':
d6e8d8
			case 'SELECT_DISTINCT';
d6e8d8
d6e8d8
				$sql = str_replace('_', ' ', $query) . ' ' . $array['SELECT'] . ' FROM ';
d6e8d8
d6e8d8
				// Build table array. We also build an alias array for later checks.
d6e8d8
				$table_array = $aliases = array();
d6e8d8
				$used_multi_alias = false;
d6e8d8
d6e8d8
				foreach ($array['FROM'] as $table_name => $alias)
d6e8d8
				{
d6e8d8
					if (is_array($alias))
d6e8d8
					{
d6e8d8
						$used_multi_alias = true;
d6e8d8
d6e8d8
						foreach ($alias as $multi_alias)
d6e8d8
						{
d6e8d8
							$table_array[] = $table_name . ' ' . $multi_alias;
d6e8d8
							$aliases[] = $multi_alias;
d6e8d8
						}
d6e8d8
					}
d6e8d8
					else
d6e8d8
					{
d6e8d8
						$table_array[] = $table_name . ' ' . $alias;
d6e8d8
						$aliases[] = $alias;
d6e8d8
					}
d6e8d8
				}
d6e8d8
d6e8d8
				// We run the following code to determine if we need to re-order the table array. ;)
d6e8d8
				// The reason for this is that for multi-aliased tables (two equal tables) in the FROM statement the last table need to match the first comparison.
d6e8d8
				// DBMS who rely on this: Oracle, PostgreSQL and MSSQL. For all other DBMS it makes absolutely no difference in which order the table is.
d6e8d8
				if (!empty($array['LEFT_JOIN']) && sizeof($array['FROM']) > 1 && $used_multi_alias !== false)
d6e8d8
				{
d6e8d8
					// Take first LEFT JOIN
d6e8d8
					$join = current($array['LEFT_JOIN']);
d6e8d8
d6e8d8
					// Determine the table used there (even if there are more than one used, we only want to have one
d6e8d8
					preg_match('/(' . implode('|', $aliases) . ')\.[^\s]+/U', str_replace(array('(', ')', 'AND', 'OR', ' '), '', $join['ON']), $matches);
d6e8d8
d6e8d8
					// If there is a first join match, we need to make sure the table order is correct
d6e8d8
					if (!empty($matches[1]))
d6e8d8
					{
d6e8d8
						$first_join_match = trim($matches[1]);
d6e8d8
						$table_array = $last = array();
d6e8d8
d6e8d8
						foreach ($array['FROM'] as $table_name => $alias)
d6e8d8
						{
d6e8d8
							if (is_array($alias))
d6e8d8
							{
d6e8d8
								foreach ($alias as $multi_alias)
d6e8d8
								{
d6e8d8
									($multi_alias === $first_join_match) ? $last[] = $table_name . ' ' . $multi_alias : $table_array[] = $table_name . ' ' . $multi_alias;
d6e8d8
								}
d6e8d8
							}
d6e8d8
							else
d6e8d8
							{
d6e8d8
								($alias === $first_join_match) ? $last[] = $table_name . ' ' . $alias : $table_array[] = $table_name . ' ' . $alias;
d6e8d8
							}
d6e8d8
						}
d6e8d8
d6e8d8
						$table_array = array_merge($table_array, $last);
d6e8d8
					}
d6e8d8
				}
d6e8d8
d6e8d8
				$sql .= $this->_sql_custom_build('FROM', implode(', ', $table_array));
d6e8d8
d6e8d8
				if (!empty($array['LEFT_JOIN']))
d6e8d8
				{
d6e8d8
					foreach ($array['LEFT_JOIN'] as $join)
d6e8d8
					{
d6e8d8
						$sql .= ' LEFT JOIN ' . key($join['FROM']) . ' ' . current($join['FROM']) . ' ON (' . $join['ON'] . ')';
d6e8d8
					}
d6e8d8
				}
d6e8d8
d6e8d8
				if (!empty($array['WHERE']))
d6e8d8
				{
d6e8d8
					$sql .= ' WHERE ' . $this->_sql_custom_build('WHERE', $array['WHERE']);
d6e8d8
				}
d6e8d8
d6e8d8
				if (!empty($array['GROUP_BY']))
d6e8d8
				{
d6e8d8
					$sql .= ' GROUP BY ' . $array['GROUP_BY'];
d6e8d8
				}
d6e8d8
d6e8d8
				if (!empty($array['ORDER_BY']))
d6e8d8
				{
d6e8d8
					$sql .= ' ORDER BY ' . $array['ORDER_BY'];
d6e8d8
				}
d6e8d8
d6e8d8
			break;
d6e8d8
		}
d6e8d8
d6e8d8
		return $sql;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* display sql error page
d6e8d8
	*/
d6e8d8
	function sql_error($sql = '')
d6e8d8
	{
d6e8d8
		global $auth, $user, $config;
d6e8d8
d6e8d8
		// Set var to retrieve errored status
d6e8d8
		$this->sql_error_triggered = true;
d6e8d8
		$this->sql_error_sql = $sql;
d6e8d8
d6e8d8
		$this->sql_error_returned = $this->_sql_error();
d6e8d8
d6e8d8
		if (!$this->return_on_error)
d6e8d8
		{
d6e8d8
			$message = 'SQL ERROR [ ' . $this->sql_layer . ' ]

' . $this->sql_error_returned['message'] . ' [' . $this->sql_error_returned['code'] . ']';
d6e8d8
d6e8d8
			// Show complete SQL error and path to administrators only
d6e8d8
			// Additionally show complete error on installation or if extended debug mode is enabled
d6e8d8
			// The DEBUG_EXTRA constant is for development only!
d6e8d8
			if ((isset($auth) && $auth->acl_get('a_')) || defined('IN_INSTALL') || defined('DEBUG_EXTRA'))
d6e8d8
			{
d6e8d8
				// Print out a nice backtrace...
d6e8d8
				$backtrace = get_backtrace();
d6e8d8
d6e8d8
				$message .= ($sql) ? '

SQL

' . htmlspecialchars($sql) : '';
d6e8d8
				$message .= ($backtrace) ? '

BACKTRACE
' . $backtrace : '';
d6e8d8
				$message .= '
';
d6e8d8
			}
d6e8d8
			else
d6e8d8
			{
d6e8d8
				// If error occurs in initiating the session we need to use a pre-defined language string
d6e8d8
				// This could happen if the connection could not be established for example (then we are not able to grab the default language)
d6e8d8
				if (!isset($user->lang['SQL_ERROR_OCCURRED']))
d6e8d8
				{
d6e8d8
					$message .= '

An sql error occurred while fetching this page. Please contact an administrator if this problem persists.';
d6e8d8
				}
d6e8d8
				else
d6e8d8
				{
d6e8d8
					if (!empty($config['board_contact']))
d6e8d8
					{
d6e8d8
						$message .= '

' . sprintf($user->lang['SQL_ERROR_OCCURRED'], '', '');
d6e8d8
					}
d6e8d8
					else
d6e8d8
					{
d6e8d8
						$message .= '

' . sprintf($user->lang['SQL_ERROR_OCCURRED'], '', '');
d6e8d8
					}
d6e8d8
				}
d6e8d8
			}
d6e8d8
d6e8d8
			if ($this->transaction)
d6e8d8
			{
d6e8d8
				$this->sql_transaction('rollback');
d6e8d8
			}
d6e8d8
d6e8d8
			if (strlen($message) > 1024)
d6e8d8
			{
d6e8d8
				// We need to define $msg_long_text here to circumvent text stripping.
d6e8d8
				global $msg_long_text;
d6e8d8
				$msg_long_text = $message;
d6e8d8
d6e8d8
				trigger_error(false, E_USER_ERROR);
d6e8d8
			}
d6e8d8
d6e8d8
			trigger_error($message, E_USER_ERROR);
d6e8d8
		}
d6e8d8
d6e8d8
		if ($this->transaction)
d6e8d8
		{
d6e8d8
			$this->sql_transaction('rollback');
d6e8d8
		}
d6e8d8
d6e8d8
		return $this->sql_error_returned;
d6e8d8
	}
d6e8d8
d6e8d8
	/**
d6e8d8
	* Explain queries
d6e8d8
	*/
d6e8d8
	function sql_report($mode, $query = '')
d6e8d8
	{
d6e8d8
		global $cache, $starttime, $phpbb_root_path, $user;
d6e8d8
d6e8d8
		if (empty($_REQUEST['explain']))
d6e8d8
		{
d6e8d8
			return false;
d6e8d8
		}
d6e8d8
d6e8d8
		if (!$query && $this->query_hold != '')
d6e8d8
		{
d6e8d8
			$query = $this->query_hold;
d6e8d8
		}
d6e8d8
d6e8d8
		switch ($mode)
d6e8d8
		{
d6e8d8
			case 'display':
d6e8d8
				if (!empty($cache))
d6e8d8
				{
d6e8d8
					$cache->unload();
d6e8d8
				}
d6e8d8
				$this->sql_close();
d6e8d8
d6e8d8
				$mtime = explode(' ', microtime());
d6e8d8
				$totaltime = $mtime[0] + $mtime[1] - $starttime;
d6e8d8
d6e8d8
				echo '
d6e8d8
					<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
d6e8d8
					<head>
d6e8d8
						<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
d6e8d8
						<meta http-equiv="Content-Style-Type" content="text/css" />
d6e8d8
						<meta http-equiv="imagetoolbar" content="no" />
d6e8d8
						<title>SQL Report</title>
d6e8d8
						<link href="' . $phpbb_root_path . 'adm/style/admin.css" rel="stylesheet" type="text/css" media="screen" />
d6e8d8
					</head>
d6e8d8
					<body id="errorpage">
d6e8d8
					
d6e8d8
						
d6e8d8
							Return to previous page
d6e8d8
						
d6e8d8
						
d6e8d8
							
d6e8d8
							
d6e8d8
								
d6e8d8
								
d6e8d8
									

SQL Report

d6e8d8
									
d6e8d8
									

Page generated in ' . round($totaltime, 4) . " seconds with {$this->num_queries['normal']} queries" . (($this->num_queries['cached']) ? " + {$this->num_queries['cached']} " . (($this->num_queries['cached'] == 1) ? 'query' : 'queries') . ' returning data from cache' : '') . '

d6e8d8
d6e8d8
									

Time spent on ' . $this->sql_layer . ' queries: ' . round($this->sql_time, 5) . 's | Time spent on PHP: ' . round($totaltime - $this->sql_time, 5) . 's

d6e8d8
d6e8d8
									

d6e8d8
									' . $this->sql_report . '
d6e8d8
								
d6e8d8
								
d6e8d8
							
d6e8d8
							
d6e8d8
						
d6e8d8
						
d6e8d8
							Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
d6e8d8
						
d6e8d8
					
d6e8d8
					</body>
d6e8d8
					</html>';
d6e8d8
d6e8d8
				exit_handler();
d6e8d8
d6e8d8
			break;
d6e8d8
d6e8d8
			case 'stop':
d6e8d8
				$endtime = explode(' ', microtime());
d6e8d8
				$endtime = $endtime[0] + $endtime[1];
d6e8d8
d6e8d8
				$this->sql_report .= '
d6e8d8
d6e8d8
					
d6e8d8
					
d6e8d8
					
d6e8d8
						Query #' . $this->num_queries['total'] . '
d6e8d8
					
d6e8d8
					
d6e8d8
					
d6e8d8
					
d6e8d8
						<textarea style="font-family:\'Courier New\',monospace;width:99%" rows="5" cols="10">' . preg_replace('/\t(AND|OR)(\W)/', "\$1\$2", htmlspecialchars(preg_replace('/[\s]*[\n\r\t]+[\n\r\s\t]*/', "\n", $query))) . '</textarea>
d6e8d8
					
d6e8d8
					
d6e8d8
					
d6e8d8
d6e8d8
					' . $this->html_hold . '
d6e8d8
d6e8d8
					

d6e8d8
				';
d6e8d8
d6e8d8
				if ($this->query_result)
d6e8d8
				{
d6e8d8
					if (preg_match('/^(UPDATE|DELETE|REPLACE)/', $query))
d6e8d8
					{
d6e8d8
						$this->sql_report .= 'Affected rows: ' . $this->sql_affectedrows($this->query_result) . ' | ';
d6e8d8
					}
d6e8d8
					$this->sql_report .= 'Before: ' . sprintf('%.5f', $this->curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed: ' . sprintf('%.5f', $endtime - $this->curtime) . 's';
d6e8d8
				}
d6e8d8
				else
d6e8d8
				{
d6e8d8
					$error = $this->sql_error();
d6e8d8
					$this->sql_report .= 'FAILED - ' . $this->sql_layer . ' Error ' . $error['code'] . ': ' . htmlspecialchars($error['message']);
d6e8d8
				}
d6e8d8
d6e8d8
				$this->sql_report .= '



';
d6e8d8
d6e8d8
				$this->sql_time += $endtime - $this->curtime;
d6e8d8
			break;
d6e8d8
d6e8d8
			case 'start':
d6e8d8
				$this->query_hold = $query;
d6e8d8
				$this->html_hold = '';
d6e8d8
d6e8d8
				$this->_sql_report($mode, $query);
d6e8d8
d6e8d8
				$this->curtime = explode(' ', microtime());
d6e8d8
				$this->curtime = $this->curtime[0] + $this->curtime[1];
d6e8d8
d6e8d8
			break;
d6e8d8
d6e8d8
			case 'add_select_row':
d6e8d8
d6e8d8
				$html_table = func_get_arg(2);
d6e8d8
				$row = func_get_arg(3);
d6e8d8
d6e8d8
				if (!$html_table && sizeof($row))
d6e8d8
				{
d6e8d8
					$html_table = true;
d6e8d8
					$this->html_hold .= '';
d6e8d8
d6e8d8
					foreach (array_keys($row) as $val)
d6e8d8
					{
d6e8d8
						$this->html_hold .= '' . (($val) ? ucwords(str_replace('_', ' ', $val)) : ' ') . '';
d6e8d8
					}
d6e8d8
					$this->html_hold .= '';
d6e8d8
				}
d6e8d8
				$this->html_hold .= '';
d6e8d8
d6e8d8
				$class = 'row1';
d6e8d8
				foreach (array_values($row) as $val)
d6e8d8
				{
d6e8d8
					$class = ($class == 'row1') ? 'row2' : 'row1';
d6e8d8
					$this->html_hold .= '' . (($val) ? $val : ' ') . '';
d6e8d8
				}
d6e8d8
				$this->html_hold .= '';
d6e8d8
d6e8d8
				return $html_table;
d6e8d8
d6e8d8
			break;
d6e8d8
d6e8d8
			case 'fromcache':
d6e8d8
d6e8d8
				$this->_sql_report($mode, $query);
d6e8d8
d6e8d8
			break;
d6e8d8
d6e8d8
			case 'record_fromcache':
d6e8d8
d6e8d8
				$endtime = func_get_arg(2);
d6e8d8
				$splittime = func_get_arg(3);
d6e8d8
d6e8d8
				$time_cache = $endtime - $this->curtime;
d6e8d8
				$time_db = $splittime - $endtime;
d6e8d8
				$color = ($time_db > $time_cache) ? 'green' : 'red';
d6e8d8
d6e8d8
				$this->sql_report .= '';
Query results obtained from the cache
d6e8d8
				$this->sql_report .= '<textarea style="font-family:\'Courier New\',monospace;width:99%" rows="5" cols="10">' . preg_replace('/\t(AND|OR)(\W)/', "\$1\$2", htmlspecialchars(preg_replace('/[\s]*[\n\r\t]+[\n\r\s\t]*/', "\n", $query))) . '</textarea>';
d6e8d8
				$this->sql_report .= '

';

d6e8d8
				$this->sql_report .= 'Before: ' . sprintf('%.5f', $this->curtime - $starttime) . 's | After: ' . sprintf('%.5f', $endtime - $starttime) . 's | Elapsed [cache]: ' . sprintf('%.5f', ($time_cache)) . 's | Elapsed [db]: ' . sprintf('%.5f', $time_db) . 's



';
d6e8d8
d6e8d8
				// Pad the start time to not interfere with page timing
d6e8d8
				$starttime += $time_db;
d6e8d8
d6e8d8
			break;
d6e8d8
d6e8d8
			default:
d6e8d8
d6e8d8
				$this->_sql_report($mode, $query);
d6e8d8
d6e8d8
			break;
d6e8d8
		}
d6e8d8
d6e8d8
		return true;
d6e8d8
	}
d6e8d8
}
d6e8d8
d6e8d8
/**
d6e8d8
* This variable holds the class name to use later
d6e8d8
*/
d6e8d8
$sql_db = (!empty($dbms)) ? 'dbal_' . basename($dbms) : 'dbal';
d6e8d8
d6e8d8
?>