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-09-13 10:29:56 +00:00
* $Revision : 1.52 $
* $Date : 2009 - 09 - 13 10 : 29 : 56 $
2009-09-10 19:13:39 +00:00
* $Author : secretr $
2006-12-02 04:36:16 +00:00
*/
2008-12-13 22:35:13 +00:00
if ( defined ( 'MYSQL_LIGHT' ))
{
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 );
}
2009-09-01 20:09:36 +00:00
elseif ( defined ( 'E107_INSTALL' )) //TODO Remove the need for this if possible
2009-08-31 02:00:51 +00:00
{
define ( 'E107_DEBUG_LEVEL' , 0 );
define ( 'e_QUERY' , '' );
require_once ( " e107_config.php " );
define ( 'MPREFIX' , $mySQLprefix );
$sql = new db ;
2009-09-10 19:13:39 +00:00
$sql -> db_Connect ( $mySQLserver , $mySQLuser , $mySQLpassword , $mySQLdefaultdb );
2009-08-31 02:00:51 +00:00
}
2008-12-13 22:35:13 +00:00
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
/**
2009-09-10 12:06:39 +00:00
* MySQL Abstraction class
*
* @ package e107
* @ category e107_handlers
2009-09-13 10:29:56 +00:00
* @ version $Revision : 1.52 $
2009-09-10 19:13:39 +00:00
* @ author $Author : secretr $
2009-09-10 12:06:39 +00:00
*
*/
class e_db_mysql {
2006-12-02 04:36:16 +00:00
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 ;
2009-05-24 15:34:37 +00:00
var $mySQLerror ; // Error reporting mode - TRUE shows messages
var $mySQLlastErrNum ; // Number of last error
var $mySQLlastErrText ; // Text of last error (empty string if no error)
2006-12-02 04:36:16 +00:00
var $mySQLcurTable ;
var $mySQLlanguage ;
var $mySQLinfo ;
var $tabset ;
2009-09-05 18:58:56 +00:00
private $mySQLtableList = array (); // list of all Db tables.
var $mySQLtableListLanguage = array (); // Db table list for the currently selected language
2008-11-27 20:13:58 +00:00
2009-07-17 14:20:26 +00:00
/**
2009-09-10 19:13:39 +00:00
* MySQL Charset
*
* @ var string
*/
2009-09-10 12:06:39 +00:00
public $mySQLcharset ;
2009-07-17 14:20:26 +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
/**
2009-09-10 12:06:39 +00:00
* Constructor - gets language options from the cookie or session
2006-12-02 04:36:16 +00:00
* @ access public
*/
2009-09-10 12:06:39 +00:00
public function __construct ()
2008-05-14 20:20:32 +00:00
{
2009-08-31 02:00:51 +00:00
2009-09-13 10:29:56 +00:00
global $pref , $db_defaultPrefix ;
e107 :: getSingleton ( 'e107_traffic' ) -> BumpWho ( 'Create db object' , 1 );
2009-08-31 02:00:51 +00:00
2009-05-24 15:34:37 +00:00
$this -> mySQLPrefix = MPREFIX ; // Set the default prefix - may be overridden
$langid = 'e107language_' . $pref [ 'cookie_name' ];
if ( $pref [ 'user_tracking' ] == 'session' )
{
if ( ! isset ( $_SESSION [ $langid ])) { return ; }
$this -> mySQLlanguage = $_SESSION [ $langid ];
}
else
{
if ( ! isset ( $_COOKIE [ $langid ])) { return ; }
$this -> mySQLlanguage = $_COOKIE [ $langid ];
}
2006-12-02 04:36:16 +00:00
}
/**
2009-07-17 14:20:26 +00:00
* 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 e107_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 >
*
* @ 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
* @ param string $newLink force a new link connection if TRUE . Default FALSE
* @ param string $mySQLPrefix Tables prefix . Default to $mySQLPrefix from e107_config . php
* @ return null | string error code
*/
2009-09-10 12:06:39 +00:00
public function db_Connect ( $mySQLserver , $mySQLuser , $mySQLpassword , $mySQLdefaultdb , $newLink = FALSE , $mySQLPrefix = MPREFIX )
2007-06-26 21:34:34 +00:00
{
2009-09-13 10:29:56 +00:00
global $db_ConnectionID , $db_defaultPrefix ;
e107 :: getSingleton ( 'e107_traffic' ) -> BumpWho ( 'db Connect' , 1 );
2009-07-17 14:20:26 +00:00
$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 )
2008-05-14 20:20:32 +00:00
{
2009-07-17 14:20:26 +00:00
// No persistent link parameter permitted
if ( ! $this -> mySQLaccess = @ mysql_pconnect ( $this -> mySQLserver , $this -> mySQLuser , $this -> mySQLpassword ))
{
return 'e1' ;
}
2008-05-14 20:20:32 +00:00
}
2009-07-17 14:20:26 +00:00
else
2008-05-14 20:20:32 +00:00
{
2009-07-17 14:20:26 +00:00
if ( ! $this -> mySQLaccess = @ mysql_connect ( $this -> mySQLserver , $this -> mySQLuser , $this -> mySQLpassword , $newLink ))
{
return 'e1' ;
}
2008-11-27 20:13:58 +00:00
}
2006-12-02 04:36:16 +00:00
2009-07-17 14:20:26 +00:00
// Set utf8 connection?
//@TODO: simplify when yet undiscovered side-effects will be fixed
$this -> db_Set_Charset ();
2008-05-14 20:20:32 +00:00
2009-07-17 14:20:26 +00:00
if ( ! @ mysql_select_db ( $this -> mySQLdefaultdb , $this -> mySQLaccess ))
{
return 'e2' ;
}
2006-12-02 04:36:16 +00:00
2009-07-17 14:20:26 +00:00
$this -> dbError ( 'dbConnect/SelectDB' );
// Save the connection resource
if ( $db_ConnectionID == NULL )
$db_ConnectionID = $this -> mySQLaccess ;
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
{
2009-05-24 15:34:37 +00:00
if ( E107_DEBUG_LEVEL > 0 )
{
global $db_debug ;
$db_debug -> Mark_Time ( $sMarker );
}
2006-12-02 04:36:16 +00:00
}
2009-05-24 15:34:37 +00:00
2006-12-02 04:36:16 +00:00
/**
* @ return void
* @ desc Enter description here ...
* @ access private
*/
2009-05-24 15:34:37 +00:00
function db_Show_Performance ()
{
2006-12-02 04:36:16 +00:00
return $db_debug -> Show_Performance ();
}
2009-05-24 15:34:37 +00:00
2006-12-02 04:36:16 +00:00
/**
* @ return void
* @ desc add query to dblog table
* @ access private
*/
2009-05-24 15:34:37 +00:00
function db_Write_log ( $log_type = '' , $log_remark = '' , $log_query = '' )
{
2006-12-02 04:36:16 +00:00
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
}
2009-05-24 15:34:37 +00:00
2006-12-02 04:36:16 +00:00
/**
* @ desc Enter description here ...
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
2009-09-10 12:06:39 +00:00
*
* @ param unknown $query
* @ param unknown $rli
* @ return unknown
2006-12-02 04:36:16 +00:00
*/
2009-09-10 15:24:57 +00:00
public function db_Query ( $query , $rli = NULL , $qry_from = '' , $debug = FALSE , $log_type = '' , $log_remark = '' )
2009-05-24 15:34:37 +00:00
{
2009-09-13 10:29:56 +00:00
global $db_time , $db_mySQLQueryCount , $queryinfo ;
2006-12-02 04:36:16 +00:00
$db_mySQLQueryCount ++ ;
2009-05-24 15:34:37 +00:00
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 " ;
}
2009-05-24 15:34:37 +00:00
if ( $log_type != '' )
{
2006-12-02 04:36:16 +00:00
$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 ();
2009-09-13 10:29:56 +00:00
e107 :: getSingleton ( 'e107_traffic' ) -> Bump ( 'db_Query' , $b , $e );
$mytime = e107 :: getSingleton ( 'e107_traffic' ) -> TimeDelta ( $b , $e );
2006-12-02 04:36:16 +00:00
$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
2009-05-24 15:34:37 +00:00
// 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 );
$this -> total_results = $rc [ 'FOUND_ROWS()' ];
2008-05-17 17:18:36 +00:00
}
2009-05-24 15:34:37 +00:00
if ( E107_DEBUG_LEVEL )
{
2006-12-02 04:36:16 +00:00
global $db_debug ;
$aTrace = debug_backtrace ();
$pTable = $this -> mySQLcurTable ;
if ( ! strlen ( $pTable )) {
$pTable = '(complex query)' ;
} else {
$this -> mySQLcurTable = '' ; // clear before next query
}
2009-05-24 15:34:37 +00:00
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 );
2009-05-24 15:34:37 +00:00
}
else
{
2006-12-02 04:36:16 +00:00
echo " what happened to db_debug??!!<br /> " ;
}
}
return $sQryRes ;
}
2009-05-24 15:34:37 +00:00
2006-12-02 04:36:16 +00:00
/**
2009-09-10 12:06:39 +00:00
* Perform a mysql_query () using the arguments suplied by calling db :: db_Query () < br />
2006-12-02 04:36:16 +00:00
* < 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 >
*
2009-09-10 12:06:39 +00:00
* @ return integer Number of rows or false on error
2006-12-02 04:36:16 +00:00
*/
2009-09-10 12:06:39 +00:00
public function db_Select ( $table , $fields = '*' , $arg = '' , $mode = 'default' , $debug = FALSE , $log_type = '' , $log_remark = '' )
2008-05-14 20:20:32 +00:00
{
2009-05-24 15:34:37 +00:00
global $db_mySQLQueryCount ;
2008-05-14 20:20:32 +00:00
2006-12-02 04:36:16 +00:00
$table = $this -> db_IsLang ( $table );
$this -> mySQLcurTable = $table ;
if ( $arg != '' && $mode == 'default' )
{
2009-05-24 15:34:37 +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 ();
2009-05-24 15:34:37 +00:00
}
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 ;
}
2009-05-24 15:34:37 +00:00
}
elseif ( $arg != '' && $mode != 'default' )
{
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 ();
2009-05-24 15:34:37 +00:00
}
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 ;
}
2009-05-24 15:34:37 +00:00
}
else
{
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 ();
2009-05-24 15:34:37 +00:00
}
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 ;
}
}
}
2009-05-24 15:34:37 +00:00
2006-12-02 04:36:16 +00:00
/**
* @ return int Last insert ID or false on error
* @ param string $table
2009-08-29 18:07:42 +00:00
* @ param string / array $arg
2006-12-02 04:36:16 +00:00
* @ 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-08-29 18:07:42 +00:00
if ( isset ( $arg [ '_REPLACE' ]))
{
$REPLACE = TRUE ;
unset ( $arg [ '_REPLACE' ]);
}
else
{
$REPLACE = FALSE ;
}
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 );
}
2009-05-24 15:34:37 +00:00
if ( ! isset ( $arg [ 'data' ])) { return false ; }
2009-01-09 16:22:08 +00:00
2009-08-29 18:07:42 +00:00
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 );
2009-08-29 18:07:42 +00:00
if ( $REPLACE === FALSE )
{
$query = " INSERT INTO ` " . $this -> mySQLPrefix . " { $table } ` ( { $keyList } ) VALUES ( { $valList } ) " ;
}
else
{
$query = " REPLACE 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-05-24 15:34:37 +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 );
2009-05-24 15:34:37 +00:00
$this -> dbError ( 'db_Insert' );
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
{
2009-05-24 15:34:37 +00:00
$this -> dbError ( " db_Insert ( { $query } ) " );
2006-12-02 04:36:16 +00:00
return FALSE ;
}
}
2009-08-29 18:07:42 +00:00
/**
* @ return int Last insert ID or false on error
* @ param string $table
* @ param array $arg
* @ param string $debug
* @ desc Insert / REplace a row into the table < br />
* < br />
* Example :< br />
* < code > $sql -> db_Replace ( " links " , $array ); </ code >
*
* @ access public
*/
function db_Replace ( $table , $arg , $debug = FALSE , $log_type = '' , $log_remark = '' )
{
$arg [ '_REPLACE' ] = TRUE ;
2009-08-31 02:00:51 +00:00
return $this -> db_Insert ( $table , $arg , $debug , $log_type , $log_remark );
2009-08-29 18:07:42 +00:00
}
2009-05-24 15:34:37 +00:00
2006-12-02 04:36:16 +00:00
/**
* @ 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-05-24 15:34:37 +00:00
$this -> mySQLaccess = $db_ConnectionID ;
2006-12-30 03:07:50 +00:00
}
2009-09-10 19:13:39 +00:00
if ( is_array ( $arg )) // Remove the need for a separate db_UpdateArray() function.
{
$new_data = '' ;
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 ; }
2009-01-09 16:22:08 +00:00
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
2009-09-06 20:04:04 +00:00
$query = 'UPDATE ' . $this -> mySQLPrefix . $table . ' SET ' . $arg ;
if ( $result = $this -> mySQLresult = $this -> db_Query ( $query , NULL , 'db_Update' , $debug , $log_type , $log_remark ))
2008-11-26 23:34:35 +00:00
{
2006-12-30 01:11:49 +00:00
$result = mysql_affected_rows ( $this -> mySQLaccess );
2009-05-24 15:34:37 +00:00
$this -> dbError ( 'db_Update' );
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
{
2009-05-24 15:34:37 +00:00
$this -> dbError ( " db_Update ( { $query } ) " );
2006-12-02 04:36:16 +00:00
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' :
2009-09-10 19:13:39 +00:00
case 'integer' :
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 ;
2009-09-10 19:13:39 +00:00
case 'str' :
2008-11-29 01:24:27 +00:00
case 'string' :
return " ' { $fieldValue } ' " ;
2008-11-27 20:13:58 +00:00
break ;
2009-09-10 19:13:39 +00:00
case 'null' :
return ( $fieldValue ? " ' { $fieldValue } ' " : 'NULL' );
break ;
2008-11-27 20:13:58 +00:00
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 .
2009-05-24 15:34:37 +00:00
Deprecated routine - use db_Update () with array parameters
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-05-24 15:34:37 +00:00
function db_Fetch ( $type = MYSQL_ASSOC )
{
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 );
2009-09-13 10:29:56 +00:00
e107 :: getSingleton ( 'e107_traffic' ) -> Bump ( 'db_Fetch' , $b );
2009-05-24 15:34:37 +00:00
if ( $row )
{
2006-12-02 04:36:16 +00:00
$this -> dbError ( 'db_Fetch' );
return $row ;
2009-05-24 15:34:37 +00:00
}
else
{
2006-12-02 04:36:16 +00:00
$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
*/
2009-05-24 15:34:37 +00:00
function db_Count ( $table , $fields = '(*)' , $arg = '' , $debug = FALSE , $log_type = '' , $log_remark = '' )
{
2006-12-02 04:36:16 +00:00
$table = $this -> db_IsLang ( $table );
2009-05-24 15:34:37 +00:00
if ( $fields == 'generic' )
{
2006-12-02 04:36:16 +00:00
$query = $table ;
2009-05-24 15:34:37 +00:00
if ( $this -> mySQLresult = $this -> db_Query ( $query , NULL , 'db_Count' , $debug , $log_type , $log_remark ))
{
2006-12-02 04:36:16 +00:00
$rows = $this -> mySQLrows = @ mysql_fetch_array ( $this -> mySQLresult );
2009-05-24 15:34:37 +00:00
$this -> dbError ( 'db_Count' );
2006-12-02 04:36:16 +00:00
return $rows [ 'COUNT(*)' ];
2009-05-24 15:34:37 +00:00
}
else
{
$this -> dbError ( " db_Count ( { $query } ) " );
2006-12-02 04:36:16 +00:00
return FALSE ;
}
}
$this -> mySQLcurTable = $table ;
2008-05-14 20:20:32 +00:00
$query = 'SELECT COUNT' . $fields . ' FROM ' . $this -> mySQLPrefix . $table . ' ' . $arg ;
2009-05-24 15:34:37 +00:00
if ( $this -> mySQLresult = $this -> db_Query ( $query , NULL , 'db_Count' , $debug , $log_type , $log_remark ))
{
2006-12-02 04:36:16 +00:00
$rows = $this -> mySQLrows = @ mysql_fetch_array ( $this -> mySQLresult );
2009-05-24 15:34:37 +00:00
$this -> dbError ( 'db_Count' );
2006-12-02 04:36:16 +00:00
return $rows [ 0 ];
2009-05-24 15:34:37 +00:00
}
else
{
$this -> dbError ( " db_Count( { $query } ) " );
2006-12-02 04:36:16 +00:00
return FALSE ;
}
}
2009-05-24 15:34:37 +00:00
2006-12-02 04:36:16 +00:00
/**
* @ 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
*/
2009-05-24 15:34:37 +00:00
function db_Close ()
{
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
}
2009-09-13 10:29:56 +00:00
e107 :: getSingleton ( 'e107_traffic' ) -> 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' );
}
2009-05-24 15:34:37 +00:00
2006-12-02 04:36:16 +00:00
/**
* @ 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
*/
2009-05-24 15:34:37 +00:00
function db_Delete ( $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 ;
$this -> mySQLaccess = $db_ConnectionID ;
2006-12-30 03:07:50 +00:00
}
2009-05-24 15:34:37 +00:00
if ( ! $arg )
{
if ( $result = $this -> mySQLresult = $this -> db_Query ( 'DELETE FROM ' . $this -> mySQLPrefix . $table , NULL , 'db_Delete' , $debug , $log_type , $log_remark ))
{
$this -> dbError ( 'db_Delete' );
2006-12-02 04:36:16 +00:00
return $result ;
2009-05-24 15:34:37 +00:00
}
else
{
$this -> dbError ( " db_Delete( { $arg } ) " );
2006-12-02 04:36:16 +00:00
return FALSE ;
}
2009-05-24 15:34:37 +00:00
}
else
{
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 );
2009-05-24 15:34:37 +00:00
$this -> dbError ( 'db_Delete' );
2006-12-02 04:36:16 +00:00
return $tmp ;
2009-05-24 15:34:37 +00:00
}
else
{
2006-12-02 04:36:16 +00:00
$this -> dbError ( 'db_Delete (' . $arg . ')' );
return FALSE ;
}
}
}
2009-05-24 15:34:37 +00:00
2006-12-02 04:36:16 +00:00
/**
* @ return unknown
* @ desc Enter description here ...
* @ access private
*/
2009-05-24 15:34:37 +00:00
function db_Rows ()
{
2006-12-02 04:36:16 +00:00
$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 void
* @ param unknown $mode
* @ desc Enter description here ...
* @ access private
*/
2009-05-24 15:34:37 +00:00
function db_SetErrorReporting ( $mode )
{
2006-12-02 04:36:16 +00:00
$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
$this -> tabset = FALSE ;
2009-09-05 23:02:23 +00:00
$query .= " " ; // temp fix for failing regex below, when there is no space after the table name;
2007-06-15 08:04:07 +00:00
if ( strpos ( $query , '`#' ) !== FALSE )
2009-09-05 23:02:23 +00:00
{
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 )
2009-05-24 15:34:37 +00:00
{
2006-12-02 04:36:16 +00:00
$query = preg_replace_callback ( " / \ s#([ \ w]*?) \ W/ " , array ( $this , 'ml_check' ), $query );
2009-05-24 15:34:37 +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
2009-05-24 15:34:37 +00:00
$this -> dbError ( 'db_Select_gen' );
return TRUE ;
2007-04-22 09:07:27 +00:00
}
2007-06-26 21:34:34 +00:00
elseif ( $this -> mySQLresult === FALSE )
2007-04-22 09:07:27 +00:00
{ // Failed query
2009-05-24 15:34:37 +00:00
$this -> dbError ( 'db_Select_gen(' . $query . ')' );
return FALSE ;
2007-04-22 09:07:27 +00:00
}
else
{ // Successful query which does return a row count - get the count and return it
2009-05-24 15:34:37 +00:00
$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 )
2009-09-05 23:02:23 +00:00
{
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
*/
2009-05-24 15:34:37 +00:00
function db_Fieldname ( $offset )
{
2006-12-02 04:36:16 +00:00
$result = @ mysql_field_name ( $this -> mySQLresult , $offset );
return $result ;
}
2009-05-24 15:34:37 +00:00
2006-12-02 04:36:16 +00:00
/**
* @ return unknown
* @ desc Enter description here ...
* @ access private
*/
2009-05-24 15:34:37 +00:00
function db_Field_info ()
{
2006-12-02 04:36:16 +00:00
$result = @ mysql_fetch_field ( $this -> mySQLresult );
return $result ;
}
2009-05-24 15:34:37 +00:00
2006-12-02 04:36:16 +00:00
/**
* @ return unknown
* @ desc Enter description here ...
* @ access private
*/
2009-05-24 15:34:37 +00:00
function db_Num_fields ()
{
2006-12-02 04:36:16 +00:00
$result = @ mysql_num_fields ( $this -> mySQLresult );
return $result ;
}
/**
2009-09-05 18:58:56 +00:00
* Check for the existence of a matching language table when multi - language tables are active .
* @ param string $table Name of table , without the prefix .
2006-12-02 04:36:16 +00:00
* @ access private
2009-09-05 18:58:56 +00:00
* @ return name of the language table ( eg . lan_french_news )
2006-12-02 04:36:16 +00:00
*/
2009-05-24 15:34:37 +00:00
function db_IsLang ( $table , $multiple = FALSE )
{
2009-09-05 18:58:56 +00:00
global $pref ;
2009-05-24 15:34:37 +00:00
if (( ! $this -> mySQLlanguage || ! $pref [ 'multilanguage' ]) && $multiple == FALSE )
{
2006-12-02 04:36:16 +00:00
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
}
2009-09-05 18:58:56 +00:00
if ( $multiple == FALSE )
{
$mltable = " lan_ " . strtolower ( $this -> mySQLlanguage . '_' . $table );
return ( $this -> db_Table_exists ( $table , $this -> mySQLlanguage )) ? $mltable : $table ;
2006-12-02 04:36:16 +00:00
}
2009-09-05 18:58:56 +00:00
else // return an array of all matching language tables. eg [french]->e107_lan_news
{
2009-05-24 15:34:37 +00:00
if ( ! is_array ( $table ))
{
2006-12-02 04:36:16 +00:00
$table = array ( $table );
}
2009-09-05 18:58:56 +00:00
foreach ( $this -> mySQLtablelist as $tab )
2009-05-24 15:34:37 +00:00
{
if ( stristr ( $tab , $this -> mySQLPrefix . " lan_ " ) !== FALSE )
{
2008-05-14 20:20:32 +00:00
$tmp = explode ( " _ " , str_replace ( $this -> mySQLPrefix . " lan_ " , " " , $tab ));
2006-12-02 04:36:16 +00:00
$lng = $tmp [ 0 ];
2009-05-24 15:34:37 +00:00
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
}
}
}
}
2009-09-05 18:58:56 +00:00
2006-12-02 04:36:16 +00:00
return ( $lanlist ) ? $lanlist : FALSE ;
}
// -------------------------
2009-09-05 18:58:56 +00:00
2006-12-02 04:36:16 +00:00
}
/**
* @ return array
* @ param string fields to retrieve
* @ desc returns fields as structured array
* @ access public
*/
2009-05-24 15:34:37 +00:00
function db_getList ( $fields = 'ALL' , $amount = FALSE , $maximum = FALSE , $ordermode = FALSE )
{
2006-12-02 04:36:16 +00:00
$list = array ();
$counter = 1 ;
2009-05-24 15:34:37 +00:00
while ( $row = $this -> db_Fetch ())
{
foreach ( $row as $key => $value )
{
if ( is_string ( $key ))
{
if ( strtoupper ( $fields ) == 'ALL' || in_array ( $key , $fields ))
{
2006-12-02 04:36:16 +00:00
if ( ! $ordermode )
{
$list [ $counter ][ $key ] = $value ;
}
else
{
$list [ $row [ $ordermode ]][ $key ] = $value ;
}
}
}
}
2009-05-24 15:34:37 +00:00
if ( $amount && $amount == $counter || ( $maximum && $counter > $maximum ))
{
2006-12-02 04:36:16 +00:00
break ;
}
$counter ++ ;
}
return $list ;
}
/**
* @ return integer
* @ desc returns total number of queries made so far
* @ access public
*/
2009-05-24 15:34:37 +00:00
function db_QueryCount ()
{
2006-12-02 04:36:16 +00:00
global $db_mySQLQueryCount ;
return $db_mySQLQueryCount ;
}
/*
Multi - language Query Function .
*/
2009-05-24 15:34:37 +00:00
function db_Query_all ( $query , $debug = " " )
{
2006-12-02 04:36:16 +00:00
$error = " " ;
2008-05-14 20:20:32 +00:00
$query = str_replace ( " # " , $this -> mySQLPrefix , $query );
2006-12-02 04:36:16 +00:00
2009-05-24 15:34:37 +00:00
if ( ! $this -> db_Query ( $query ))
{ // run query on the default language first.
2006-12-02 04:36:16 +00:00
$error .= $query . " failed " ;
}
$tmp = explode ( " " , $query );
2009-05-24 15:34:37 +00:00
foreach ( $tmp as $val )
{
if ( strpos ( $val , $this -> mySQLPrefix ) !== FALSE )
{
2008-05-14 20:20:32 +00:00
$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.
2009-05-24 15:34:37 +00:00
if ( $tablist = $this -> db_IsLang ( $table , TRUE ))
{
foreach ( $tablist as $key => $tab )
{
2006-12-02 04:36:16 +00:00
$querylan = $query ;
2009-05-24 15:34:37 +00:00
foreach ( $search as $find )
{
2006-12-02 04:36:16 +00:00
$lang = $key ;
$replace = ( $tab [ $find ] != " " ) ? $tab [ $find ] : $find ;
$querylan = str_replace ( $find , $replace , $querylan );
}
2009-05-24 15:34:37 +00:00
if ( ! $this -> db_Query ( $querylan ))
{ // run query on other language tables.
2006-12-02 04:36:16 +00:00
$error .= $querylan . " failed for language " ;
}
if ( $debug ){ echo " <br />** lang= " . $querylan ; }
}
}
return ( $error ) ? FALSE : TRUE ;
}
2009-05-24 15:34:37 +00:00
2006-12-02 04:36:16 +00:00
// 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 )
{
2009-05-24 15:34:37 +00:00
if ( ! $this -> mySQLdefaultdb )
{
global $mySQLdefaultdb ;
$this -> mySQLdefaultdb = $mySQLdefaultdb ;
}
$convert = array ( " PRIMARY " => " PRI " , " INDEX " => " MUL " , " UNIQUE " => " UNI " );
2009-09-03 23:39:32 +00:00
$key = ( isset ( $convert [ $key ])) ? $convert [ $key ] : " OFF " ;
2006-12-02 04:36:16 +00:00
2009-05-24 15:34:37 +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
{
2009-05-24 15:34:37 +00:00
$c = 0 ;
while ( $row = mysql_fetch_assoc ( $result ))
2007-09-22 21:46:23 +00:00
{
2009-05-24 15:34:37 +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 ++ ;
2007-09-22 21:46:23 +00:00
}
2006-12-02 04:36:16 +00:00
}
return FALSE ;
}
2009-05-24 15:34:37 +00:00
2006-12-02 04:36:16 +00:00
/**
* 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
2009-05-24 15:34:37 +00:00
2009-09-05 12:48:28 +00:00
/**
2007-03-04 15:00:19 +00:00
* Verify whether a table exists , without causing an error
*
2009-09-05 12:48:28 +00:00
* @ param string $table Table name without the prefix
2009-09-05 18:58:56 +00:00
* @ param string $lanMode [ optional ] When set to TRUE , searches for multilanguage tables
* @ return boolean TRUE if exists
2007-03-04 15:00:19 +00:00
*
2009-09-05 12:48:28 +00:00
* NOTES : Slower ( 28 ms ) than " SELECT 1 FROM " ( 4 - 5 ms ), but doesn ' t produce MySQL errors .
* Multiple checks on a single page will only use 1 query . ie . faster on multiple calls .
2007-03-04 15:00:19 +00:00
*/
2009-09-05 18:58:56 +00:00
public function db_Table_exists ( $table , $language = '' )
2009-05-24 15:34:37 +00:00
{
2009-09-05 18:58:56 +00:00
global $pref ;
$table = strtolower ( $table ); // precaution for multilanguage
if ( $language && ( $language != $pref [ 'sitelanguage' ]))
2009-09-05 12:48:28 +00:00
{
2009-09-05 18:58:56 +00:00
if ( ! isset ( $this -> mySQLtableListLanguage [ $language ]))
2009-09-05 12:48:28 +00:00
{
2009-09-05 18:58:56 +00:00
$this -> mySQLtableListLanguage = $this -> db_mySQLtableList ( $language );
}
return in_array ( 'lan_' . strtolower ( $language ) . " _ " . $table , $this -> mySQLtableListLanguage [ $language ]);
}
else
{
if ( ! $this -> mySQLtableList )
{
$this -> mySQLtableList = $this -> db_mySQLtableList ();
}
return in_array ( $table , $this -> mySQLtableList );
}
}
/**
* Populate $this -> mySQLtableList ;
* @ return array
*/
private function db_mySQLtableList ( $language = '' )
{
if ( $language )
{
if ( ! isset ( $this -> mySQLtableListLanguage [ $language ]))
{
$table = array ();
if ( $res = $this -> db_Query ( " SHOW TABLES LIKE ' " . $this -> mySQLPrefix . " lan_ " . strtolower ( $language ) . " %' " ))
{
while ( $rows = $this -> db_Fetch ( MYSQL_NUM ))
{
$table [] = str_replace ( $this -> mySQLPrefix , " " , $rows [ 0 ]);
}
}
$ret = array ( $language => $table );
return $ret ;
}
else
{
return $this -> mySQLtableListLanguage [ $language ];
}
}
if ( ! $this -> mySQLtableList )
{
$table = array ();
if ( $res = $this -> db_Query ( " SHOW TABLES LIKE ' " . $this -> mySQLPrefix . " %' " ))
{
while ( $rows = $this -> db_Fetch ( MYSQL_NUM ))
{
$table [] = str_replace ( $this -> mySQLPrefix , " " , $rows [ 0 ]);
}
}
return $table ;
}
else
{
return $this -> mySQLtableList ;
}
}
public function db_ResetTableList ()
{
$this -> mySQLtableList = array ();
$this -> mySQLtableListLanguage = array ();
}
/**
* Return a filtered list of DB tables .
* @ param object $mode [ optional ] all | lan | nolan
* @ return array
*/
public function db_TableList ( $mode = 'all' )
{
if ( ! $this -> mySQLtableList )
{
$this -> mySQLtableList = $this -> db_mySQLtableList ();
}
if ( $mode == 'all' )
{
return $this -> mySQLtableList ;
2009-09-05 12:48:28 +00:00
}
2009-09-05 18:58:56 +00:00
if ( $mode == 'lan' || $mode == 'nolan' )
2009-09-05 12:48:28 +00:00
{
2009-09-05 18:58:56 +00:00
$nolan = array ();
$lan = array ();
foreach ( $this -> mySQLtableList as $tab )
{
if ( substr ( $tab , 0 , 4 ) != 'lan_' )
{
$nolan [] = $tab ;
}
else
{
$lan [] = $tab ;
}
}
return ( $mode == 'lan' ) ? $lan : $nolan ;
}
}
function db_CopyTable ( $oldtable , $newtable , $drop = FALSE , $data = FALSE )
{
$old = $this -> mySQLPrefix . strtolower ( $oldtable );
$new = $this -> mySQLPrefix . strtolower ( $newtable );
if ( $drop )
{
$this -> db_Select_gen ( " DROP TABLE IF EXISTS { $new } " );
}
//Get $old table structure
$this -> db_Select_gen ( 'SET SQL_QUOTE_SHOW_CREATE = 1' );
$qry = " SHOW CREATE TABLE { $old } " ;
if ( $this -> db_Select_gen ( $qry ))
{
$row = $this -> db_Fetch ( MYSQL_NUM );
$qry = $row [ 1 ];
// $qry = str_replace($old, $new, $qry);
$qry = preg_replace ( " #CREATE \ sTABLE \ s` { 0,1} " . $old . " ` { 0,1} \ s# " , " CREATE TABLE ` { $new } ` " , $qry , 1 ); // More selective search
2009-09-05 12:48:28 +00:00
}
else
{
2009-09-05 18:58:56 +00:00
return FALSE ;
}
if ( ! $this -> db_Table_exists ( $newtable ))
{
$result = $this -> db_Query ( $qry );
}
if ( $data ) //We need to copy the data too
{
$qry = " INSERT INTO { $new } SELECT * FROM { $old } " ;
$result = $this -> db_Select_gen ( $qry );
}
return $result ;
2007-03-04 15:00:19 +00:00
}
2009-09-05 18:58:56 +00:00
2009-05-24 15:34:37 +00:00
/**
* @ return text string relating to error ( empty string if no error )
* @ param unknown $from
* @ desc Calling method from within this class
* @ access private
*/
function dbError ( $from )
{
$this -> mySQLlastErrNum = mysql_errno ();
$this -> mySQLlastErrText = '' ;
if ( $this -> mySQLlastErrNum == 0 )
{
return '' ;
}
$this -> mySQLlastErrText = mysql_error (); // Get the error text.
if ( $this -> mySQLerror == TRUE )
{
message_handler ( 'ADMIN_MESSAGE' , '<b>mySQL Error!</b> Function: ' . $from . '. [' . $this -> mySQLlastErrNum . ' - ' . $this -> mySQLlastErrText . ']' , __LINE__ , __FILE__ );
}
return $this -> mySQLlastErrText ;
}
// Return error number for last operation
function getLastErrorNumber ()
{
return $this -> mySQLlastErrNum ; // Number of last error
}
// Return error text for last operation
function getLastErrorText ()
{
return $this -> mySQLlastErrText ; // Text of last error (empty string if no error)
}
2009-07-17 14:20:26 +00:00
/**
* Check if MySQL version is utf8 compatible and may be used as it accordingly to the user choice
*
* @ TODO Simplify when the conversion script will be available
*
* @ access public
* @ param string MySQL charset may be forced in special occasion .
* UTF - 8 encoding and decoding is left to the progammer
* @ param bool TRUE enter debug mode . default FALSE
* @ return string hardcoded error message
*/
function db_Set_Charset ( $charset = '' , $debug = FALSE )
{
// Get the default user choice
global $mySQLcharset ;
if ( varset ( $mySQLcharset ) != 'utf8' )
{
// Only utf8 is accepted
$mySQLcharset = '' ;
}
$charset = ( $charset ? $charset : $mySQLcharset );
$message = (( ! $charset && $debug ) ? 'Empty charset!' : '' );
if ( $charset )
{
if ( ! $debug )
{
@ mysql_query ( " SET NAMES ` $charset ` " );
}
else
{
// Check if MySQL version is utf8 compatible
preg_match ( '/^(.*?)($|-)/' , mysql_get_server_info (), $mysql_version );
if ( version_compare ( $mysql_version [ 1 ], '4.1.2' , '<' ))
{
// reset utf8
//@TODO reset globally? $mySQLcharset = '';
$charset = '' ;
$message = 'MySQL version is not utf8 compatible!' ;
}
else
{
// Use db_Query() debug handler
$this -> db_Query ( " SET NAMES ` $charset ` " , NULL , '' , $debug );
}
}
}
// Save mySQLcharset for further uses within this connection
$this -> mySQLcharset = $charset ;
return $message ;
}
2006-12-02 04:36:16 +00:00
}
2009-09-10 12:06:39 +00:00
/**
* BC
*/
class db extends e_db_mysql
{
}
2007-04-12 08:10:20 +00:00
?>