2007-04-12 08:10:20 +00:00
< ? php
2006-12-02 04:36:16 +00:00
/*
2008-12-10 16:59:19 +00:00
* e107 website system
*
* Copyright ( C ) 2001 - 2008 e107 Inc ( e107 . org )
* Released under the terms and conditions of the
* GNU General Public License ( http :// www . gnu . org / licenses / gpl . txt )
*
* mySQL Handler
*
* $Source : / cvs_backup / e107_0 . 8 / e107_handlers / mysql_class . php , v $
2009-01-18 17:05:08 +00:00
* $Revision : 1.37 $
* $Date : 2009 - 01 - 18 17 : 05 : 08 $
2008-12-13 22:35:13 +00:00
* $Author : mcfly_e107 $
2006-12-02 04:36:16 +00:00
*/
2008-12-13 22:35:13 +00:00
if ( defined ( 'MYSQL_LIGHT' ))
{
class dummyTraffic {
function Bump () { return ; }
function BumpWho () { return ; }
function TimeDelta () { return ; }
}
$eTraffic = new dummyTraffic ;
define ( 'E107_DEBUG_LEVEL' , 0 );
define ( 'e_QUERY' , '' );
$path = ( MYSQL_LIGHT !== true ? MYSQL_LIGHT : '' );
require_once ( $path . 'e107_config.php' );
define ( 'MPREFIX' , $mySQLprefix );
$sql = new db ;
$sql -> db_Connect ( $mySQLserver , $mySQLuser , $mySQLpassword , $mySQLdefaultdb );
}
else
{
if ( ! defined ( 'e107_INIT' )) { exit ; }
}
2006-12-02 04:36:16 +00:00
$db_time = 0.0 ; // Global total time spent in all db object queries
$db_mySQLQueryCount = 0 ; // Global total number of db object queries (all db's)
2008-05-14 20:20:32 +00:00
$db_ConnectionID = NULL ; // Stores ID for the first DB connection used - which should be the main E107 DB - then used as default
2007-06-26 21:34:34 +00:00
2006-12-02 04:36:16 +00:00
/**
* MySQL Abstraction class
*
* @ package e107
2009-01-18 17:05:08 +00:00
* @ version $Revision : 1.37 $
2008-12-13 22:35:13 +00:00
* @ author $Author : mcfly_e107 $
2006-12-02 04:36:16 +00:00
*/
class db {
var $mySQLserver ;
var $mySQLuser ;
var $mySQLpassword ;
var $mySQLdefaultdb ;
2008-05-14 20:20:32 +00:00
var $mySQLPrefix ;
2006-12-02 04:36:16 +00:00
var $mySQLaccess ;
var $mySQLresult ;
var $mySQLrows ;
var $mySQLerror ;
var $mySQLcurTable ;
var $mySQLlanguage ;
var $mySQLinfo ;
var $tabset ;
2008-11-27 20:13:58 +00:00
2008-05-14 20:20:32 +00:00
var $total_results ; // Total number of results
2006-12-02 04:36:16 +00:00
/**
* @ return db
* @ desc db constructor gets language options from the cookie or session
* @ access public
*/
2008-11-27 20:13:58 +00:00
function db ()
2008-05-14 20:20:32 +00:00
{
global $pref , $eTraffic , $db_defaultPrefix ;
$eTraffic -> BumpWho ( 'Create db object' , 1 );
$this -> mySQLPrefix = MPREFIX ; // Set the default prefix - may be overridden
$langid = 'e107language_' . $pref [ 'cookie_name' ];
2008-11-27 20:13:58 +00:00
if ( $pref [ 'user_tracking' ] == 'session' )
2008-05-14 20:20:32 +00:00
{
if ( ! isset ( $_SESSION [ $langid ])) { return ; }
$this -> mySQLlanguage = $_SESSION [ $langid ];
2008-11-27 20:13:58 +00:00
}
else
2008-05-14 20:20:32 +00:00
{
if ( ! isset ( $_COOKIE [ $langid ])) { return ; }
$this -> mySQLlanguage = $_COOKIE [ $langid ];
}
2006-12-02 04:36:16 +00:00
}
/**
* @ return null or string error code
* @ param string $mySQLserver IP Or hostname of the MySQL server
* @ param string $mySQLuser MySQL username
* @ param string $mySQLpassword MySQL Password
* @ param string $mySQLdefaultdb The database schema to connect to
* @ desc Connects to mySQL server and selects database - generally not required if your table is in the main DB .< br />
* < br />
* Example using e107 database with variables defined in config . php :< br />
* < code > $sql = new db ;
* $sql -> db_Connect ( $mySQLserver , $mySQLuser , $mySQLpassword , $mySQLdefaultdb ); </ code >
* < br />
* OR to connect an other database :< br />
* < code > $sql = new db ;
* $sql -> db_Connect ( 'url_server_database' , 'user_database' , 'password_database' , 'name_of_database' ); </ code >
*
* @ access public
*/
2008-11-27 20:13:58 +00:00
function db_Connect ( $mySQLserver , $mySQLuser , $mySQLpassword , $mySQLdefaultdb , $newLink = FALSE , $mySQLPrefix = MPREFIX )
2007-06-26 21:34:34 +00:00
{
2008-05-14 20:20:32 +00:00
global $eTraffic , $db_ConnectionID , $db_defaultPrefix ;
$eTraffic -> BumpWho ( 'db Connect' , 1 );
$this -> mySQLserver = $mySQLserver ;
$this -> mySQLuser = $mySQLuser ;
$this -> mySQLpassword = $mySQLpassword ;
$this -> mySQLdefaultdb = $mySQLdefaultdb ;
$this -> mySQLPrefix = $mySQLPrefix ;
$temp = $this -> mySQLerror ;
$this -> mySQLerror = FALSE ;
if ( defined ( " USE_PERSISTANT_DB " ) && USE_PERSISTANT_DB == true )
{
if ( ! $this -> mySQLaccess = @ mysql_pconnect ( $this -> mySQLserver , $this -> mySQLuser , $this -> mySQLpassword )) // No persistent link parameter permitted
{
return 'e1' ;
}
}
else
{
2008-11-27 20:13:58 +00:00
if ( ! $this -> mySQLaccess = @ mysql_connect ( $this -> mySQLserver , $this -> mySQLuser , $this -> mySQLpassword , $newLink ))
2008-05-14 20:20:32 +00:00
{
return 'e1' ;
2008-11-27 20:13:58 +00:00
}
2008-05-14 20:20:32 +00:00
}
2006-12-02 04:36:16 +00:00
2008-11-27 20:13:58 +00:00
if ( !@ mysql_select_db ( $this -> mySQLdefaultdb , $this -> mySQLaccess ))
2008-05-14 20:20:32 +00:00
{
return 'e2' ;
2008-11-27 20:13:58 +00:00
}
2008-05-14 20:20:32 +00:00
$this -> dbError ( 'dbConnect/SelectDB' );
2006-12-02 04:36:16 +00:00
2007-08-08 21:01:46 +00:00
if ( $db_ConnectionID == NULL ) $db_ConnectionID = $this -> mySQLaccess ; // Save the connection resource
2008-05-14 20:20:32 +00:00
return TRUE ;
2006-12-02 04:36:16 +00:00
}
2006-12-30 01:11:49 +00:00
2006-12-02 04:36:16 +00:00
/**
* @ return void
* @ param unknown $sMarker
* @ desc Enter description here ...
* @ access private
*/
2008-11-27 20:13:58 +00:00
function db_Mark_Time ( $sMarker )
2008-05-14 20:20:32 +00:00
{
2008-11-27 20:13:58 +00:00
if ( E107_DEBUG_LEVEL > 0 )
2008-05-14 20:20:32 +00:00
{
global $db_debug ;
$db_debug -> Mark_Time ( $sMarker );
}
2006-12-02 04:36:16 +00:00
}
/**
* @ return void
* @ desc Enter description here ...
* @ access private
*/
function db_Show_Performance () {
return $db_debug -> Show_Performance ();
}
/**
* @ return void
* @ desc add query to dblog table
* @ access private
*/
function db_Write_log ( $log_type = '' , $log_remark = '' , $log_query = '' ) {
global $tp , $e107 ;
2007-12-18 20:57:37 +00:00
list ( $time_usec , $time_sec ) = explode ( " " , microtime ());
2006-12-02 04:36:16 +00:00
$uid = ( USER ) ? USERID : '0' ;
2008-11-27 20:13:58 +00:00
$userstring = ( USER === true ? USERNAME : " LAN_ANONYMOUS " );
2006-12-02 04:36:16 +00:00
$ip = $e107 -> getip ();
$qry = $tp -> toDB ( $log_query );
2007-12-18 20:57:37 +00:00
$this -> db_Insert ( 'dblog' , " 0, { $time_sec } , { $time_usec } , ' { $log_type } ', 'DBDEBUG', { $uid } , ' { $userstring } ', ' { $ip } ', '', ' { $log_remark } ', ' { $qry } ' " );
2006-12-02 04:36:16 +00:00
}
/**
* @ return unknown
* @ param unknown $query
* @ param unknown $rli
* @ desc Enter description here ...
* @ access private
2007-06-26 21:34:34 +00:00
* This is the 'core' routine which handles much of the interface between other functions and the DB
2006-12-02 04:36:16 +00:00
*/
function db_Query ( $query , $rli = NULL , $qry_from = '' , $debug = FALSE , $log_type = '' , $log_remark = '' ) {
global $db_time , $db_mySQLQueryCount , $queryinfo , $eTraffic ;
$db_mySQLQueryCount ++ ;
if ( $debug == 'now' ) {
2008-11-29 18:47:39 +00:00
echo " ** $query <br /> \n " ;
2006-12-02 04:36:16 +00:00
}
if ( $debug !== FALSE || strstr ( e_QUERY , 'showsql' ))
{
$queryinfo [] = " <b> { $qry_from } </b>: $query " ;
}
if ( $log_type != '' ) {
$this -> db_Write_log ( $log_type , $log_remark , $query );
}
2006-12-30 03:07:50 +00:00
if ( ! $this -> mySQLaccess )
{
2007-06-26 21:34:34 +00:00
global $db_ConnectionID ;
$this -> mySQLaccess = $db_ConnectionID ;
2006-12-30 03:07:50 +00:00
}
2006-12-02 04:36:16 +00:00
$b = microtime ();
2006-12-30 01:11:49 +00:00
$sQryRes = is_null ( $rli ) ? @ mysql_query ( $query , $this -> mySQLaccess ) : @ mysql_query ( $query , $rli );
2006-12-02 04:36:16 +00:00
$e = microtime ();
$eTraffic -> Bump ( 'db_Query' , $b , $e );
$mytime = $eTraffic -> TimeDelta ( $b , $e );
$db_time += $mytime ;
$this -> mySQLresult = $sQryRes ;
2008-05-17 17:18:36 +00:00
if (( strpos ( $query , 'SQL_CALC_FOUND_ROWS' ) !== FALSE ) && ( strpos ( $query , 'SELECT' ) !== FALSE ))
{ // Need to get the total record count as well. Return code is a resource identifier
// Have to do this before any debug action, otherwise this bit gets messed up
$fr = mysql_query ( " SELECT FOUND_ROWS() " , $this -> mySQLaccess );
$rc = mysql_fetch_array ( $fr );
2008-11-27 20:13:58 +00:00
$this -> total_results = $rc [ 'FOUND_ROWS()' ];
2008-05-17 17:18:36 +00:00
}
2006-12-02 04:36:16 +00:00
if ( E107_DEBUG_LEVEL ) {
global $db_debug ;
$aTrace = debug_backtrace ();
$pTable = $this -> mySQLcurTable ;
if ( ! strlen ( $pTable )) {
$pTable = '(complex query)' ;
} else {
$this -> mySQLcurTable = '' ; // clear before next query
}
if ( is_object ( $db_debug )) {
2007-10-15 11:03:29 +00:00
$buglink = is_null ( $rli ) ? $this -> mySQLaccess : $rli ;
$nFields = $db_debug -> Mark_Query ( $query , $buglink , $sQryRes , $aTrace , $mytime , $pTable );
2006-12-02 04:36:16 +00:00
} else {
echo " what happened to db_debug??!!<br /> " ;
}
}
return $sQryRes ;
}
/**
* @ return int Number of rows or false on error
*
* @ param string $table Table name to select data from
* @ param string $fields Table fields to be retrieved , default * ( all in table )
* @ param string $arg Query arguments , default null
* @ param string $mode Argument has WHERE or not , default = default ( WHERE )
*
* @ param bool $debug Debug mode on or off
*
* @ desc Perform a mysql_query () using the arguments suplied by calling db :: db_Query () < br />
* < br />
* If you need more requests think to call the class .< br />
* < br />
* Example using a unique connection to database :< br />
* < code > $sql -> db_Select ( " comments " , " * " , " comment_item_id = ' $id ' AND comment_type = '1' ORDER BY comment_datestamp " ); </ code >< br />
* < br />
* OR as second connection :< br />
* < code > $sql2 = new db ;
* $sql2 -> db_Select ( " chatbox " , " * " , " ORDER BY cb_datestamp DESC LIMIT $from , " . $view , 'no_where' ); </ code >
*
* @ access public
*/
2008-11-27 20:13:58 +00:00
function db_Select ( $table , $fields = '*' , $arg = '' , $mode = 'default' , $debug = FALSE , $log_type = '' , $log_remark = '' )
2008-05-14 20:20:32 +00:00
{
global $db_mySQLQueryCount ;
2006-12-02 04:36:16 +00:00
$table = $this -> db_IsLang ( $table );
$this -> mySQLcurTable = $table ;
if ( $arg != '' && $mode == 'default' )
{
2008-05-14 20:20:32 +00:00
if ( $this -> mySQLresult = $this -> db_Query ( 'SELECT ' . $fields . ' FROM ' . $this -> mySQLPrefix . $table . ' WHERE ' . $arg , NULL , 'db_Select' , $debug , $log_type , $log_remark )) {
2006-12-02 04:36:16 +00:00
$this -> dbError ( 'dbQuery' );
return $this -> db_Rows ();
} else {
2008-05-14 20:20:32 +00:00
$this -> dbError ( " db_Select (SELECT $fields FROM " . $this -> mySQLPrefix . " { $table } WHERE { $arg } ) " );
2006-12-02 04:36:16 +00:00
return FALSE ;
}
} elseif ( $arg != '' && $mode != 'default' ) {
2008-05-14 20:20:32 +00:00
if ( $this -> mySQLresult = $this -> db_Query ( 'SELECT ' . $fields . ' FROM ' . $this -> mySQLPrefix . $table . ' ' . $arg , NULL , 'db_Select' , $debug , $log_type , $log_remark )) {
2006-12-02 04:36:16 +00:00
$this -> dbError ( 'dbQuery' );
return $this -> db_Rows ();
} else {
2008-05-14 20:20:32 +00:00
$this -> dbError ( " db_Select (SELECT { $fields } FROM " . $this -> mySQLPrefix . " { $table } { $arg } ) " );
2006-12-02 04:36:16 +00:00
return FALSE ;
}
} else {
2008-05-14 20:20:32 +00:00
if ( $this -> mySQLresult = $this -> db_Query ( 'SELECT ' . $fields . ' FROM ' . $this -> mySQLPrefix . $table , NULL , 'db_Select' , $debug , $log_type , $log_remark )) {
2006-12-02 04:36:16 +00:00
$this -> dbError ( 'dbQuery' );
return $this -> db_Rows ();
} else {
2008-05-14 20:20:32 +00:00
$this -> dbError ( " db_Select (SELECT { $fields } FROM " . $this -> mySQLPrefix . " { $table } ) " );
2006-12-02 04:36:16 +00:00
return FALSE ;
}
}
}
/**
* @ return int Last insert ID or false on error
* @ param string $table
* @ param string $arg
* @ param string $debug
* @ desc Insert a row into the table < br />
* < br />
* Example :< br />
* < code > $sql -> db_Insert ( " links " , " 0, 'News', 'news.php', '', '', 1, 0, 0, 0 " ); </ code >
*
* @ access public
*/
2009-01-09 16:22:08 +00:00
function db_Insert ( $table , $arg , $debug = FALSE , $log_type = '' , $log_remark = '' )
{
2006-12-02 04:36:16 +00:00
$table = $this -> db_IsLang ( $table );
$this -> mySQLcurTable = $table ;
if ( is_array ( $arg ))
{
2009-01-09 16:22:08 +00:00
if ( ! isset ( $arg [ '_FIELD_TYPES' ]) && ! isset ( $arg [ 'data' ]))
{
//Convert data if not using 'new' format
$_tmp = array ();
$_tmp [ 'data' ] = $arg ;
$arg = $_tmp ;
unset ( $_tmp );
}
if ( ! isset ( $arg [ 'data' ])) { return false ; }
2008-11-29 01:24:27 +00:00
$fieldTypes = $this -> _getTypes ( $arg );
2009-01-09 16:22:08 +00:00
$keyList = '`' . implode ( '`,`' , array_keys ( $arg [ 'data' ])) . '`' ;
2008-11-27 20:13:58 +00:00
$tmp = array ();
2009-01-09 16:22:08 +00:00
foreach ( $arg [ 'data' ] as $fk => $fv )
2008-11-27 20:13:58 +00:00
{
2008-11-29 01:24:27 +00:00
$tmp [] = $this -> _getFieldValue ( $fk , $fv , $fieldTypes );
2008-11-27 20:13:58 +00:00
}
$valList = implode ( ', ' , $tmp );
unset ( $tmp );
2008-05-14 20:20:32 +00:00
$query = " INSERT INTO ` " . $this -> mySQLPrefix . " { $table } ` ( { $keyList } ) VALUES ( { $valList } ) " ;
2006-12-02 04:36:16 +00:00
}
else
{
2008-05-14 20:20:32 +00:00
$query = 'INSERT INTO ' . $this -> mySQLPrefix . " { $table } VALUES ( { $arg } ) " ;
2006-12-02 04:36:16 +00:00
}
2006-12-30 03:07:50 +00:00
if ( ! $this -> mySQLaccess )
{
2007-06-26 21:34:34 +00:00
global $db_ConnectionID ;
2009-01-09 16:22:08 +00:00
$this -> mySQLaccess = $db_ConnectionID ;
2006-12-30 03:07:50 +00:00
}
2008-11-29 01:24:27 +00:00
if ( $result = $this -> mySQLresult = $this -> db_Query ( $query , NULL , 'db_Insert' , $debug , $log_type , $log_remark ))
{
2006-12-30 01:11:49 +00:00
$tmp = mysql_insert_id ( $this -> mySQLaccess );
2007-02-20 17:43:17 +00:00
return ( $tmp ) ? $tmp : TRUE ; // return true even if table doesn't have auto-increment.
2008-11-29 01:24:27 +00:00
}
else
{
2006-12-02 04:36:16 +00:00
$this -> dbError ( " db_Insert ( $query ) " );
return FALSE ;
}
}
/**
* @ return int number of affected rows , or false on error
* @ param string $table
* @ param string $arg
* @ param bool $debug
* @ desc Update fields in ONE table of the database corresponding to your $arg variable < br />
* < br />
* Think to call it if you need to do an update while retrieving data .< br />
* < br />
* Example using a unique connection to database :< br />
* < code > $sql -> db_Update ( " user " , " user_viewed=' $u_new ' WHERE user_id=' " . USERID . " ' " ); </ code >
* < br />
* OR as second connection < br />
* < code > $sql2 = new db ;
* $sql2 -> db_Update ( " user " , " user_viewed = ' $u_new ' WHERE user_id = ' " . USERID . " ' " ); </ code >< br />
*
* @ access public
*/
2009-01-09 16:22:08 +00:00
function db_Update ( $table , $arg , $debug = FALSE , $log_type = '' , $log_remark = '' )
{
2006-12-02 04:36:16 +00:00
$table = $this -> db_IsLang ( $table );
$this -> mySQLcurTable = $table ;
2006-12-30 03:07:50 +00:00
if ( ! $this -> mySQLaccess )
{
2007-06-26 21:34:34 +00:00
global $db_ConnectionID ;
2009-01-09 16:22:08 +00:00
$this -> mySQLaccess = $db_ConnectionID ;
2006-12-30 03:07:50 +00:00
}
2009-01-09 16:22:08 +00:00
if ( is_array ( $arg )) // Remove the need for a separate db_UpdateArray() function.
{
2008-11-27 20:13:58 +00:00
$new_data = '' ;
2009-01-09 16:22:08 +00:00
if ( ! isset ( $arg [ '_FIELD_TYPES' ]) && ! isset ( $arg [ 'data' ]))
{
//Convert data if not using 'new' format (is this needed?)
$_tmp = array ();
if ( isset ( $arg [ 'WHERE' ]))
{
$_tmp [ 'WHERE' ] = $arg [ 'WHERE' ];
unset ( $arg [ 'WHERE' ]);
}
$_tmp [ 'data' ] = $arg ;
$arg = $_tmp ;
unset ( $_tmp );
}
if ( ! isset ( $arg [ 'data' ])) { return false ; }
2008-11-29 01:24:27 +00:00
$fieldTypes = $this -> _getTypes ( $arg );
2009-01-09 16:22:08 +00:00
foreach ( $arg [ 'data' ] as $fn => $fv )
2008-08-14 22:34:06 +00:00
{
2008-11-27 20:13:58 +00:00
$new_data .= ( $new_data ? ', ' : '' );
2008-11-29 01:24:27 +00:00
$new_data .= " ` { $fn } `= " . $this -> _getFieldValue ( $fn , $fv , $fieldTypes );
2008-11-27 20:13:58 +00:00
}
2009-01-09 16:22:08 +00:00
$arg = $new_data . ( isset ( $arg [ 'WHERE' ]) ? ' WHERE ' . $arg [ 'WHERE' ] : '' );
2008-11-26 23:34:35 +00:00
}
2008-08-14 22:34:06 +00:00
2008-11-26 23:34:35 +00:00
if ( $result = $this -> mySQLresult = $this -> db_Query ( 'UPDATE ' . $this -> mySQLPrefix . $table . ' SET ' . $arg , NULL , 'db_Update' , $debug , $log_type , $log_remark ))
{
2006-12-30 01:11:49 +00:00
$result = mysql_affected_rows ( $this -> mySQLaccess );
2008-11-27 20:13:58 +00:00
if ( $result == - 1 ) { return false ; } // Error return from mysql_affected_rows
2006-12-02 04:36:16 +00:00
return $result ;
2008-11-26 23:34:35 +00:00
}
else
{
2006-12-02 04:36:16 +00:00
$this -> dbError ( " db_Update ( $query ) " );
return FALSE ;
}
}
2008-11-29 01:24:27 +00:00
function _getTypes ( & $arg )
{
if ( isset ( $arg [ '_FIELD_TYPES' ]))
{
if ( ! isset ( $arg [ '_FIELD_TYPES' ][ '_DEFAULT' ]))
{
$arg [ '_FIELD_TYPES' ][ '_DEFAULT' ] = 'todb' ;
}
$fieldTypes = $arg [ '_FIELD_TYPES' ];
unset ( $arg [ '_FIELD_TYPES' ]);
}
else
{
$fieldTypes = array ();
$fieldTypes [ '_DEFAULT' ] = 'string' ;
}
return $fieldTypes ;
}
2008-11-27 20:13:58 +00:00
/**
* @ return mixed
* @ param string | array $fieldValue
* @ desc Return new field value in proper format < br />
*
* @ access private
*/
2008-11-29 01:24:27 +00:00
function _getFieldValue ( $fieldKey , $fieldValue , & $fieldTypes )
2008-11-27 20:13:58 +00:00
{
2008-11-29 18:22:46 +00:00
if ( $fieldValue === '_NULL_' ) { return 'NULL' ;}
2008-11-29 01:24:27 +00:00
$type = ( isset ( $fieldTypes [ $fieldKey ]) ? $fieldTypes [ $fieldKey ] : $fieldTypes [ '_DEFAULT' ]);
2008-11-27 20:13:58 +00:00
2008-11-29 01:24:27 +00:00
switch ( $type )
2008-11-27 20:13:58 +00:00
{
case 'int' :
2008-11-29 01:24:27 +00:00
return ( int ) $fieldValue ;
2008-11-27 20:13:58 +00:00
break ;
case 'cmd' :
2008-11-29 01:24:27 +00:00
return $fieldValue ;
2008-11-27 20:13:58 +00:00
break ;
2008-11-29 01:24:27 +00:00
case 'string' :
return " ' { $fieldValue } ' " ;
2008-11-27 20:13:58 +00:00
break ;
2008-12-07 00:21:21 +00:00
case 'escape' :
return " ' " . mysql_real_escape_string ( $fieldValue ) . " ' " ;
break ;
2008-11-29 01:24:27 +00:00
case 'todb' :
2008-11-27 20:13:58 +00:00
default :
2008-11-29 18:09:51 +00:00
if ( $fieldValue == '' ) { return " '' " ; }
2008-11-29 01:24:27 +00:00
$e107 = e107 :: getInstance ();
return " ' " . $e107 -> tp -> toDB ( $fieldValue ) . " ' " ;
2008-11-27 20:13:58 +00:00
break ;
}
}
2007-12-26 13:21:34 +00:00
/* Similar to db_Update (), but splits the variables and the 'WHERE' clause .
$vars may be an array ( fieldname => newvalue ) of fields to be updated , or a simple list .
$arg is usually a 'WHERE' clause
2008-11-27 20:13:58 +00:00
The way the code is written at the moment , a call to db_Update () with just the first two parameters specified can be
2007-12-30 11:03:57 +00:00
converted simply by changing the function name - it will still work .
2007-12-26 13:21:34 +00:00
*/
2008-11-27 20:13:58 +00:00
function db_UpdateArray ( $table , $vars , $arg = '' , $debug = FALSE , $log_type = '' , $log_remark = '' )
2007-12-26 13:21:34 +00:00
{
$table = $this -> db_IsLang ( $table );
$this -> mySQLcurTable = $table ;
if ( ! $this -> mySQLaccess )
{
global $db_ConnectionID ;
$this -> mySQLaccess = $db_ConnectionID ;
}
$new_data = '' ;
if ( is_array ( $vars ))
{
$spacer = '' ;
foreach ( $vars as $fn => $fv )
{
$new_data .= $spacer . " ` { $fn } `=' { $fv } ' " ;
$spacer = ', ' ;
}
$vars = '' ;
}
2008-11-27 20:13:58 +00:00
if ( $result = $this -> mySQLresult = $this -> db_Query ( 'UPDATE ' . $this -> mySQLPrefix . $table . ' SET ' . $new_data . $vars . ' ' . $arg , NULL , 'db_UpdateArray' , $debug , $log_type , $log_remark ))
2007-12-26 13:21:34 +00:00
{
$result = mysql_affected_rows ( $this -> mySQLaccess );
if ( $result == - 1 ) return FALSE ; // Error return from mysql_affected_rows
return $result ;
2008-11-27 20:13:58 +00:00
}
else
2007-12-26 13:21:34 +00:00
{
$this -> dbError ( " db_Update ( $query ) " );
return FALSE ;
}
}
2006-12-02 04:36:16 +00:00
/**
* @ return array MySQL row
* @ param string $mode
* @ desc Fetch an array containing row data ( see PHP ' s mysql_fetch_array () docs ) < br />
* < br />
* Example :< br />
* < code > while ( $row = $sql -> db_Fetch ()){
* $text .= $row [ 'username' ];
* } </ code >
*
* @ access public
*/
2009-01-18 17:05:08 +00:00
function db_Fetch ( $type = MYSQL_ASSOC ) {
2006-12-02 04:36:16 +00:00
global $eTraffic ;
2007-10-15 11:03:29 +00:00
if ( ! ( is_int ( $type ))) {
2009-01-18 17:05:08 +00:00
$type = MYSQL_ASSOC ;
2007-10-15 11:03:29 +00:00
}
2006-12-02 04:36:16 +00:00
$b = microtime ();
2007-06-26 21:34:34 +00:00
$row = @ mysql_fetch_array ( $this -> mySQLresult , $type );
2006-12-02 04:36:16 +00:00
$eTraffic -> Bump ( 'db_Fetch' , $b );
if ( $row ) {
$this -> dbError ( 'db_Fetch' );
return $row ;
} else {
$this -> dbError ( 'db_Fetch' );
return FALSE ;
}
}
/**
* @ return int number of affected rows or false on error
* @ param string $table
* @ param string $fields
* @ param string $arg
* @ desc Count the number of rows in a select < br />
* < br />
* Example :< br />
* < code > $topics = $sql -> db_Count ( " forum_t " , " (*) " , " WHERE thread_forum_id=' " . $forum_id . " ' AND thread_parent='0' " ); </ code >
*
* @ access public
*/
function db_Count ( $table , $fields = '(*)' , $arg = '' , $debug = FALSE , $log_type = '' , $log_remark = '' ) {
$table = $this -> db_IsLang ( $table );
if ( $fields == 'generic' ) {
$query = $table ;
if ( $this -> mySQLresult = $this -> db_Query ( $query , NULL , 'db_Count' , $debug , $log_type , $log_remark )) {
$rows = $this -> mySQLrows = @ mysql_fetch_array ( $this -> mySQLresult );
return $rows [ 'COUNT(*)' ];
} else {
$this -> dbError ( " dbCount ( $query ) " );
return FALSE ;
}
}
$this -> mySQLcurTable = $table ;
2008-05-14 20:20:32 +00:00
$query = 'SELECT COUNT' . $fields . ' FROM ' . $this -> mySQLPrefix . $table . ' ' . $arg ;
2006-12-02 04:36:16 +00:00
if ( $this -> mySQLresult = $this -> db_Query ( $query , NULL , 'db_Count' , $debug , $log_type , $log_remark )) {
$rows = $this -> mySQLrows = @ mysql_fetch_array ( $this -> mySQLresult );
return $rows [ 0 ];
} else {
$this -> dbError ( " dbCount ( $query ) " );
return FALSE ;
}
}
/**
* @ return void
* @ desc Closes the mySQL server connection .< br />
* < br />
* Only required if you open a second connection .< br />
* Native e107 connection is closed in the footer . php file < br />
* < br />
* Example :< br />
* < code > $sql -> db_Close (); </ code >
*
* @ access public
*/
function db_Close () {
global $eTraffic ;
2006-12-30 03:07:50 +00:00
if ( ! $this -> mySQLaccess )
{
2007-06-26 21:34:34 +00:00
global $db_ConnectionID ;
$this -> mySQLaccess = $db_ConnectionID ;
2006-12-30 03:07:50 +00:00
}
2006-12-02 04:36:16 +00:00
$eTraffic -> BumpWho ( 'db Close' , 1 );
2007-10-15 11:03:29 +00:00
$this -> mySQLaccess = NULL ; // correct way to do it when using shared links.
2006-12-02 04:36:16 +00:00
$this -> dbError ( 'dbClose' );
}
/**
* @ return int number of affected rows , or false on error
* @ param string $table
* @ param string $arg
* @ desc Delete rows from a table < br />
* < br />
* Example :
* < code > $sql -> db_Delete ( " tmp " , " tmp_ip=' $ip ' " ); </ code >< br />
* < br />
* @ access public
*/
function db_Delete ( $table , $arg = '' , $debug = FALSE , $log_type = '' , $log_remark = '' ) {
$table = $this -> db_IsLang ( $table );
$this -> mySQLcurTable = $table ;
2006-12-30 03:07:50 +00:00
if ( ! $this -> mySQLaccess )
{
2007-06-26 21:34:34 +00:00
global $db_ConnectionID ;
$this -> mySQLaccess = $db_ConnectionID ;
2006-12-30 03:07:50 +00:00
}
2006-12-02 04:36:16 +00:00
if ( ! $arg ) {
2008-05-14 20:20:32 +00:00
if ( $result = $this -> mySQLresult = $this -> db_Query ( 'DELETE FROM ' . $this -> mySQLPrefix . $table , NULL , 'db_Delete' , $debug , $log_type , $log_remark )) {
2006-12-02 04:36:16 +00:00
return $result ;
} else {
$this -> dbError ( " db_Delete ( $arg ) " );
return FALSE ;
}
} else {
2008-05-14 20:20:32 +00:00
if ( $result = $this -> mySQLresult = $this -> db_Query ( 'DELETE FROM ' . $this -> mySQLPrefix . $table . ' WHERE ' . $arg , NULL , 'db_Delete' , $debug , $log_type , $log_remark )) {
2006-12-30 01:11:49 +00:00
$tmp = mysql_affected_rows ( $this -> mySQLaccess );
2006-12-02 04:36:16 +00:00
return $tmp ;
} else {
$this -> dbError ( 'db_Delete (' . $arg . ')' );
return FALSE ;
}
}
}
/**
* @ return unknown
* @ desc Enter description here ...
* @ access private
*/
function db_Rows () {
$rows = $this -> mySQLrows = @ mysql_num_rows ( $this -> mySQLresult );
$this -> dbError ( 'db_Rows' );
2007-06-08 20:56:53 +00:00
return $rows ;
2006-12-02 04:36:16 +00:00
}
/**
* @ return unknown
* @ param unknown $from
* @ desc Enter description here ...
* @ access private
*/
function dbError ( $from ) {
if ( $error_message = @ mysql_error ()) {
if ( $this -> mySQLerror == TRUE ) {
message_handler ( 'ADMIN_MESSAGE' , '<b>mySQL Error!</b> Function: ' . $from . '. [' .@ mysql_errno () . ' - ' . $error_message . ']' , __LINE__ , __FILE__ );
return $error_message ;
}
}
}
/**
* @ return void
* @ param unknown $mode
* @ desc Enter description here ...
* @ access private
*/
function db_SetErrorReporting ( $mode ) {
$this -> mySQLerror = $mode ;
}
/**
* @ return unknown
* @ param unknown $arg
* @ desc Enter description here ...
* @ access private
*/
2007-06-15 08:04:07 +00:00
function db_Select_gen ( $query , $debug = FALSE , $log_type = '' , $log_remark = '' )
{
2006-12-02 04:36:16 +00:00
/*
changes by jalist 19 / 01 / 05 :
added string replace on table prefix to tidy up long database queries
2008-05-14 20:20:32 +00:00
usage : instead of sending " SELECT * FROM " . $this -> mySQLPrefix . " table " , do " SELECT * FROM #table "
2007-04-18 20:57:59 +00:00
Returns result compatible with mysql_query - may be TRUE for some results , resource ID for others
2006-12-02 04:36:16 +00:00
*/
$this -> tabset = FALSE ;
2007-06-15 08:04:07 +00:00
if ( strpos ( $query , '`#' ) !== FALSE )
{
2007-06-26 21:34:34 +00:00
$query = preg_replace_callback ( " / \ s`#([ \ w]*?)` \ W/ " , array ( $this , 'ml_check' ), $query );
2007-06-15 08:04:07 +00:00
}
elseif ( strpos ( $query , '#' ) !== FALSE )
{
2006-12-02 04:36:16 +00:00
$query = preg_replace_callback ( " / \ s#([ \ w]*?) \ W/ " , array ( $this , 'ml_check' ), $query );
2007-06-26 21:34:34 +00:00
}
2007-06-15 08:04:07 +00:00
2007-06-26 21:34:34 +00:00
if (( $this -> mySQLresult = $this -> db_Query ( $query , NULL , 'db_Select_gen' , $debug , $log_type , $log_remark )) === TRUE )
2007-04-22 09:07:27 +00:00
{ // Successful query which doesn't return a row count
$this -> dbError ( 'db_Select_gen' );
return TRUE ;
}
2007-06-26 21:34:34 +00:00
elseif ( $this -> mySQLresult === FALSE )
2007-04-22 09:07:27 +00:00
{ // Failed query
$this -> dbError ( 'dbQuery (' . $query . ')' );
return FALSE ;
}
else
{ // Successful query which does return a row count - get the count and return it
$this -> dbError ( 'db_Select_gen' );
return $this -> db_Rows ();
2006-12-02 04:36:16 +00:00
}
}
2007-06-15 08:04:07 +00:00
function ml_check ( $matches )
{
2006-12-02 04:36:16 +00:00
$table = $this -> db_IsLang ( $matches [ 1 ]);
2007-06-15 08:04:07 +00:00
if ( $this -> tabset == false )
{
2006-12-02 04:36:16 +00:00
$this -> mySQLcurTable = $table ;
$this -> tabset = true ;
}
2008-05-14 20:20:32 +00:00
return ' `' . $this -> mySQLPrefix . $table . '`' . substr ( $matches [ 0 ], - 1 );
2006-12-02 04:36:16 +00:00
}
2007-06-15 08:04:07 +00:00
2006-12-02 04:36:16 +00:00
/**
* @ return unknown
* @ param unknown $offset
* @ desc Enter description here ...
* @ access private
*/
function db_Fieldname ( $offset ) {
$result = @ mysql_field_name ( $this -> mySQLresult , $offset );
return $result ;
}
/**
* @ return unknown
* @ desc Enter description here ...
* @ access private
*/
function db_Field_info () {
$result = @ mysql_fetch_field ( $this -> mySQLresult );
return $result ;
}
/**
* @ return unknown
* @ desc Enter description here ...
* @ access private
*/
function db_Num_fields () {
$result = @ mysql_num_fields ( $this -> mySQLresult );
return $result ;
}
/**
* @ return unknown
* @ param unknown $table
* @ desc Enter description here ...
* @ access private
*/
function db_IsLang ( $table , $multiple = FALSE ) {
global $pref , $mySQLtablelist ;
if (( ! $this -> mySQLlanguage || ! $pref [ 'multilanguage' ]) && $multiple == FALSE ) {
return $table ;
}
2006-12-30 03:07:50 +00:00
if ( ! $this -> mySQLaccess )
{
2007-06-26 21:34:34 +00:00
global $db_ConnectionID ;
$this -> mySQLaccess = $db_ConnectionID ;
2006-12-30 03:07:50 +00:00
}
2006-12-02 04:36:16 +00:00
if ( ! $mySQLtablelist ) {
2006-12-30 01:11:49 +00:00
$tablist = mysql_list_tables ( $this -> mySQLdefaultdb , $this -> mySQLaccess );
2006-12-02 04:36:16 +00:00
while ( list ( $temp ) = mysql_fetch_array ( $tablist )) {
$mySQLtablelist [] = $temp ;
}
}
$mltable = " lan_ " . strtolower ( $this -> mySQLlanguage . '_' . $table );
// ---- Find all multi-language tables.
if ( $multiple == TRUE ){ // return an array of all matching language tables. eg [french]->e107_lan_news
if ( ! is_array ( $table )){
$table = array ( $table );
}
foreach ( $mySQLtablelist as $tab ){
2008-05-14 20:20:32 +00:00
if ( stristr ( $tab , $this -> mySQLPrefix . " lan_ " ) !== FALSE ){
$tmp = explode ( " _ " , str_replace ( $this -> mySQLPrefix . " lan_ " , " " , $tab ));
2006-12-02 04:36:16 +00:00
$lng = $tmp [ 0 ];
foreach ( $table as $t ){
if ( eregi ( $t . " $ " , $tab )){
2008-05-14 20:20:32 +00:00
$lanlist [ $lng ][ $this -> mySQLPrefix . $t ] = $tab ;
2006-12-02 04:36:16 +00:00
}
}
}
}
return ( $lanlist ) ? $lanlist : FALSE ;
}
// -------------------------
2008-05-14 20:20:32 +00:00
if ( in_array ( $this -> mySQLPrefix . $mltable , $mySQLtablelist )) {
2006-12-02 04:36:16 +00:00
return $mltable ;
}
return $table ;
}
/**
* @ return array
* @ param string fields to retrieve
* @ desc returns fields as structured array
* @ access public
*/
function db_getList ( $fields = 'ALL' , $amount = FALSE , $maximum = FALSE , $ordermode = FALSE ) {
$list = array ();
$counter = 1 ;
while ( $row = $this -> db_Fetch ()) {
foreach ( $row as $key => $value ) {
if ( is_string ( $key )) {
if ( strtoupper ( $fields ) == 'ALL' || in_array ( $key , $fields )) {
if ( ! $ordermode )
{
$list [ $counter ][ $key ] = $value ;
}
else
{
$list [ $row [ $ordermode ]][ $key ] = $value ;
}
}
}
}
if ( $amount && $amount == $counter || ( $maximum && $counter > $maximum )) {
break ;
}
$counter ++ ;
}
return $list ;
}
/**
* @ return integer
* @ desc returns total number of queries made so far
* @ access public
*/
function db_QueryCount () {
global $db_mySQLQueryCount ;
return $db_mySQLQueryCount ;
}
/*
Multi - language Query Function .
*/
function db_Query_all ( $query , $debug = " " ){
$error = " " ;
2008-05-14 20:20:32 +00:00
$query = str_replace ( " # " , $this -> mySQLPrefix , $query );
2006-12-02 04:36:16 +00:00
if ( ! $this -> db_Query ( $query )){ // run query on the default language first.
$error .= $query . " failed " ;
}
$tmp = explode ( " " , $query );
foreach ( $tmp as $val ){
2008-05-14 20:20:32 +00:00
if ( strpos ( $val , $this -> mySQLPrefix ) !== FALSE ){
$table [] = str_replace ( $this -> mySQLPrefix , " " , $val );
2006-12-02 04:36:16 +00:00
$search [] = $val ;
}
}
// Loop thru relevant language tables and replace each tablename within the query.
if ( $tablist = $this -> db_IsLang ( $table , TRUE )){
foreach ( $tablist as $key => $tab ){
$querylan = $query ;
foreach ( $search as $find ){
$lang = $key ;
$replace = ( $tab [ $find ] != " " ) ? $tab [ $find ] : $find ;
$querylan = str_replace ( $find , $replace , $querylan );
}
if ( ! $this -> db_Query ( $querylan )){ // run query on other language tables.
$error .= $querylan . " failed for language " ;
}
if ( $debug ){ echo " <br />** lang= " . $querylan ; }
}
}
return ( $error ) ? FALSE : TRUE ;
}
// Determines if a plugin field (and key) exist. OR if fieldid is numeric - return the field name in that position.
2007-09-22 21:46:23 +00:00
// If $retinfo is true, returns complete array of field data; FALSE if not found
function db_Field ( $table , $fieldid = " " , $key = " " , $retinfo = FALSE )
{
if ( ! $this -> mySQLdefaultdb )
{
global $mySQLdefaultdb ;
$this -> mySQLdefaultdb = $mySQLdefaultdb ;
}
$convert = array ( " PRIMARY " => " PRI " , " INDEX " => " MUL " , " UNIQUE " => " UNI " );
$key = ( $convert [ $key ]) ? $convert [ $key ] : " OFF " ;
2006-12-02 04:36:16 +00:00
2007-09-22 21:46:23 +00:00
if ( ! $this -> mySQLaccess )
{
global $db_ConnectionID ;
$this -> mySQLaccess = $db_ConnectionID ;
}
2006-12-30 03:07:50 +00:00
2008-05-14 20:20:32 +00:00
$result = mysql_query ( " SHOW COLUMNS FROM " . $this -> mySQLPrefix . $table , $this -> mySQLaccess );
2008-11-27 20:13:58 +00:00
if ( mysql_num_rows ( $result ) > 0 )
2007-09-22 21:46:23 +00:00
{
$c = 0 ;
2008-11-27 20:13:58 +00:00
while ( $row = mysql_fetch_assoc ( $result ))
2007-09-22 21:46:23 +00:00
{
if ( is_numeric ( $fieldid ))
{
if ( $c == $fieldid )
{
if ( $retinfo ) return $row ;
return $row [ 'Field' ]; // field number matches.
}
}
else
{ // Check for match of key name - and allow that key might not be used
if (( $fieldid == $row [ 'Field' ]) && (( $key == " OFF " ) || ( $key == $row [ 'Key' ])))
{
if ( $retinfo ) return $row ;
return TRUE ;
}
}
$c ++ ;
}
2006-12-02 04:36:16 +00:00
}
return FALSE ;
}
/**
* A pointer to mysql_real_escape_string () - see http :// www . php . net / mysql_real_escape_string
*
* @ param string $data
* @ return string
*/
2007-06-26 21:34:34 +00:00
function escape ( $data , $strip = true )
{
if ( $strip )
{
2006-12-02 04:36:16 +00:00
$data = strip_if_magic ( $data );
}
2006-12-30 03:07:50 +00:00
if ( ! $this -> mySQLaccess )
{
2007-06-26 21:34:34 +00:00
global $db_ConnectionID ;
$this -> mySQLaccess = $db_ConnectionID ;
2006-12-30 03:07:50 +00:00
}
2006-12-30 01:11:49 +00:00
return mysql_real_escape_string ( $data , $this -> mySQLaccess );
2006-12-02 04:36:16 +00:00
}
2007-03-04 15:00:19 +00:00
/*
* Verify whether a table exists , without causing an error
*
* @ param string $table
* @ return string
*
* NOTES : the 'official' way to do this uses SHOW TABLE STATUS , but that is 20 x slower !
* LIMIT 0 is 3 x slower than LIMIT 1
*/
function db_Table_exists ( $table ){
2008-05-14 20:20:32 +00:00
$res = $this -> db_Query ( " SELECT 1 FROM " . $this -> mySQLPrefix . $table . " LIMIT 1 " ); // error if not there
2007-03-04 15:00:19 +00:00
if ( $res ) return TRUE ;
return FALSE ;
}
2006-12-02 04:36:16 +00:00
}
2007-04-12 08:10:20 +00:00
?>