/**
* @version $Id: database.mysqli.php 973 2005-11-11 02:18:08Z eddieajau $
* @package Joomla
* @subpackage Database
* @copyright Copyright (C) 2005 Open Source Matters. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/
// no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );
/**
* Database connector class
* @subpackage Database
* @package Joomla
*/
class database {
/** @var string Internal variable to hold the query sql */
var $_sql = '';
/** @var int Internal variable to hold the database error number */
var $_errorNum = 0;
/** @var string Internal variable to hold the database error message */
var $_errorMsg = '';
/** @var string Internal variable to hold the prefix used on all database tables */
var $_table_prefix = '';
/** @var Internal variable to hold the connector resource */
var $_resource = '';
/** @var Internal variable to hold the last query cursor */
var $_cursor = null;
/** @var boolean Debug option */
var $_debug = 0;
/** @var int The limit for the query */
var $_limit = 0;
/** @var int The for offset for the limit */
var $_offset = 0;
/** @var int A counter for the number of queries performed by the object instance */
var $_ticker = 0;
/** @var array A log of queries */
var $_log = null;
/** @var string The null/zero date string */
var $_nullDate = '0000-00-00 00:00:00';
/** @var string Quote for named objects */
var $_nameQuote = '`';
/**
* Database object constructor
* @param string Database host
* @param string Database user name
* @param string Database user password
* @param string Database name
* @param string Common prefix for all tables
* @param boolean If true and there is an error, go offline
*/
function database( $host='localhost', $user, $pass, $db='', $table_prefix='', $goOffline=true ) {
// perform a number of fatality checks, then die gracefully
if (!function_exists( 'mysqli_connect' )) {
$mosSystemError = 1;
if ($goOffline) {
$basePath = dirname( __FILE__ );
include $basePath . '/../configuration.php';
include $basePath . '/../offline.php';
exit();
}
}
if (!($this->_resource = @mysqli_connect( $host, $user, $pass ))) {
$mosSystemError = 2;
if ($goOffline) {
$basePath = dirname( __FILE__ );
include $basePath . '/../configuration.php';
include $basePath . '/../offline.php';
exit();
}
}
if ($db != '' && !mysqli_select_db($this->_resource, $db)) {
$mosSystemError = 3;
if ($goOffline) {
$basePath = dirname( __FILE__ );
include $basePath . '/../configuration.php';
include $basePath . '/../offline.php';
exit();
}
}
$this->_table_prefix = $table_prefix;
$this->_ticker = 0;
$this->_log = array();
$this->setSQLMode();
}
/**
* @param int
*/
function debug( $level ) {
$this->_debug = intval( $level );
}
/**
* @return int The error number for the most recent query
*/
function getErrorNum() {
return $this->_errorNum;
}
/**
* @return string The error message for the most recent query
*/
function getErrorMsg() {
return str_replace( array( "\n", "'" ), array( '\n', "\'" ), $this->_errorMsg );
}
/**
* Get a database escaped string
*
* @param string The string to be escaped
* @param boolean Optional parameter to provide extra escaping
* @return string
* @access public
* @abstract
*/
function getEscaped( $text, $extra = false ) {
$string = mysqli_real_escape_string( $this->_resource, $text );
if ($extra) {
$string = addcslashes( $string, '%_' );
}
return $string;
}
/**
* Get a quoted database escaped string
*
* @param string A string
* @param boolean Default true to escape string, false to leave the string unchanged
* @return string
* @access public
*/
function Quote( $text, $escaped = true )
{
return '\''.($escaped ? $this->getEscaped( $text ) : $text).'\'';
}
/**
* Quote an identifier name (field, table, etc)
* @param string The name
* @return string The quoted name
*/
function NameQuote( $s ) {
$q = $this->_nameQuote;
if (strlen( $q ) == 1) {
return $q . $s . $q;
} else {
return $q{0} . $s . $q{1};
}
}
/**
* Quote based on field type
* @param mixed The value of the field
* @param string The field type
* @return string The correct field format
* @private
*/
function _quoteField( $value, $type ) {
switch ($type) {
case 'text':
case 'mediumtext':
case 'varchar':
$result = $this->Quote( $value );
break;
case 'date':
case 'datetime':
if (empty( $value )) {
$value = $this->_nullDate;
}
$result = $this->Quote( $value );
break;
case 'float':
case 'double':
$result = (double) $value;
break;
case 'int':
case 'tinyint':
case 'tinyint unsigned':
case 'int unsigned':
case 'unsigned':
default:
$result = (int) $value;
break;
}
return $result;
}
/**
* @return string The database prefix
*/
function getPrefix() {
return $this->_table_prefix;
}
/**
* @return string Quoted null/zero date string
*/
function getNullDate() {
return $this->_nullDate;
}
/**
* Sets the SQL query string for later execution.
*
* This function replaces a string identifier $prefix with the
* string held is the _table_prefix class variable.
*
* @param string The SQL query
* @param string The offset to start selection
* @param string The number of results to return
* @param string The common table prefix
*/
function setQuery( $sql, $offset = 0, $limit = 0, $prefix='#__' ) {
$this->_sql = $this->replacePrefix( $sql, $prefix );
$this->_limit = intval( $limit );
$this->_offset = intval( $offset );
}
/**
* This function replaces a string identifier $prefix with the
* string held is the _table_prefix class variable.
*
* @param string The SQL query
* @param string The common table prefix
* @author thede, David McKinnis
*/
function replacePrefix( $sql, $prefix='#__' ) {
$sql = trim( $sql );
$escaped = false;
$quoteChar = '';
$n = strlen( $sql );
$startPos = 0;
$literal = '';
while ($startPos < $n) {
$ip = strpos($sql, $prefix, $startPos);
if ($ip === false) {
break;
}
$j = strpos( $sql, "'", $startPos );
$k = strpos( $sql, '"', $startPos );
if (($k !== FALSE) && (($k < $j) || ($j === FALSE))) {
$quoteChar = '"';
$j = $k;
} else {
$quoteChar = "'";
}
if ($j === false) {
$j = $n;
}
$literal .= str_replace( $prefix, $this->_table_prefix, substr( $sql, $startPos, $j - $startPos ) );
$startPos = $j;
$j = $startPos + 1;
if ($j >= $n) {
break;
}
// quote comes first, find end of quote
while (TRUE) {
$k = strpos( $sql, $quoteChar, $j );
$escaped = false;
if ($k === false) {
break;
}
$l = $k - 1;
while ($l >= 0 && $sql{$l} == '\\') {
$l--;
$escaped = !$escaped;
}
if ($escaped) {
$j = $k+1;
continue;
}
break;
}
if ($k === FALSE) {
// error in the query - no end quote; ignore it
break;
}
$literal .= substr( $sql, $startPos, $k - $startPos + 1 );
$startPos = $k+1;
}
if ($startPos < $n) {
$literal .=