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
*
2020-01-19 13:33:52 +01:00
* Copyright ( C ) 2008 - 2020 e107 Inc ( e107 . org )
2008-12-10 16:59:19 +00:00
* Released under the terms and conditions of the
* GNU General Public License ( http :// www . gnu . org / licenses / gpl . txt )
*
2013-04-15 12:44:56 +02:00
* MySQL Handler
2008-12-10 16:59:19 +00:00
*
2006-12-02 04:36:16 +00:00
*/
2009-12-25 10:25:12 +00:00
/**
* MySQL Abstraction class
*
* @ package e107
* @ subpackage e107_handlers
* @ todo separate cache for db type tables
2010-10-28 13:31:54 +00:00
*
* WARNING !!! System config should be DIRECTLY called inside db handler like this :
2011-05-01 16:35:57 +00:00
* e107 :: getConfig ( 'core' , false );
* FALSE ( don ' t load ) is critical important - if missed , expect dead loop ( preference handler is calling db handler as well
2010-10-28 13:31:54 +00:00
* when data is initally loaded )
* Always use $this -> getConfig () method to avoid issues pointed above
2009-12-25 10:25:12 +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-12-02 16:51:04 +00:00
elseif ( defined ( 'E107_INSTALL' ))
2009-08-31 02:00:51 +00:00
{
define ( 'E107_DEBUG_LEVEL' , 0 );
2009-12-02 16:51:04 +00:00
require ( 'e107_config.php' );
$sql_info = compact ( 'mySQLserver' , 'mySQLuser' , 'mySQLpassword' , 'mySQLdefaultdb' , 'mySQLprefix' );
e107 :: getInstance () -> initInstallSql ( $sql_info );
2009-08-31 02:00:51 +00:00
$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
2016-02-14 12:15:55 -08:00
2019-12-23 16:37:48 +01:00
class e_db_mysql implements e_db
2009-12-25 10:25:12 +00:00
{
2009-12-10 22:46:46 +00:00
// TODO switch to protected vars where needed
2016-02-12 19:28:35 -08:00
public $mySQLserver ;
2016-07-07 15:09:38 -07:00
public $mySQLuser ;
2016-02-12 19:28:35 -08:00
protected $mySQLpassword ;
protected $mySQLdefaultdb ;
2016-07-07 15:09:38 -07:00
protected $mySQLport = 3306 ;
2016-02-12 19:28:35 -08:00
public $mySQLPrefix ;
2019-01-26 10:28:08 -08:00
2020-01-19 13:33:52 +01:00
/** @var mysqli */
2016-02-12 19:28:35 -08:00
protected $mySQLaccess ;
public $mySQLresult ;
public $mySQLrows ;
public $mySQLerror = '' ; // Error reporting mode - TRUE shows messages
protected $mySQLlastErrNum = 0 ; // Number of last error - now protected, use getLastErrorNumber()
protected $mySQLlastErrText = '' ; // Text of last error - now protected, use getLastErrorText()
protected $mySQLlastQuery = '' ;
public $mySQLcurTable ;
2016-07-07 15:09:38 -07:00
public $mySQLlanguage ;
2016-02-12 19:28:35 -08:00
public $mySQLinfo ;
public $tabset ;
public $mySQLtableList = array (); // list of all Db tables.
public $mySQLtableListLanguage = array (); // Db table list for the currently selected language
public $mySQLtablelist = array ();
2008-11-27 20:13:58 +00:00
2009-12-25 10:25:12 +00:00
protected $dbFieldDefs = array (); // Local cache - Field type definitions for _FIELD_DEFS and _NOTNULL arrays
2016-02-12 19:28:35 -08:00
public $mySQLcharset ;
public $mySqlServerInfo = '?' ; // Server info - needed for various things
2009-07-17 14:20:26 +00:00
2016-02-12 19:28:35 -08:00
public $total_results = false ; // Total number of results
2006-12-02 04:36:16 +00:00
2019-02-10 16:19:25 -08:00
/** @var e107_db_debug */
2019-02-11 15:40:10 -08:00
private $dbg ;
2019-02-10 16:19:25 -08:00
2019-02-11 15:40:10 -08:00
private $debugMode = false ;
2016-07-07 15:09:38 -07:00
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-12-02 16:51:04 +00:00
e107 :: getSingleton ( 'e107_traffic' ) -> BumpWho ( 'Create db object' , 1 );
2009-05-24 15:34:37 +00:00
$this -> mySQLPrefix = MPREFIX ; // Set the default prefix - may be overridden
2009-12-02 16:51:04 +00:00
2016-07-07 15:09:38 -07:00
if ( $port = e107 :: getMySQLConfig ( 'port' ))
{
$this -> mySQLport = intval ( $port );
}
2011-05-01 16:35:57 +00:00
/* $langid = ( isset ( $pref [ 'cookie_name' ])) ? 'e107language_' . $pref [ 'cookie_name' ] : 'e107language_temp' ;
2009-09-17 00:13:40 +00:00
if ( isset ( $pref [ 'user_tracking' ]) && ( $pref [ 'user_tracking' ] == 'session' ))
2009-05-24 15:34:37 +00:00
{
if ( ! isset ( $_SESSION [ $langid ])) { return ; }
$this -> mySQLlanguage = $_SESSION [ $langid ];
}
else
{
if ( ! isset ( $_COOKIE [ $langid ])) { return ; }
$this -> mySQLlanguage = $_COOKIE [ $langid ];
2011-05-01 16:35:57 +00:00
} */
// Detect is already done in language handler, use it if not too early
if ( defined ( 'e_LANGUAGE' )) $this -> mySQLlanguage = e107 :: getLanguage () -> e_language ;
2019-02-10 16:19:25 -08:00
if ( E107_DEBUG_LEVEL > 0 )
{
2019-02-11 15:40:10 -08:00
$this -> debugMode = true ;
2019-02-10 16:19:25 -08:00
}
2019-02-11 15:40:10 -08:00
$this -> dbg = e107 :: getDebug ();
2021-09-04 15:06:19 +02:00
/**
* Revert PHP 8.1 mysqli default error mode
* @ link https :// github . com / php / php - src / blob / 4025 cf2875f895e9f7193cebb1c8efa4290d052e / UPGRADING #L101-L105
*/
mysqli_report ( MYSQLI_REPORT_OFF );
2006-12-02 04:36:16 +00:00
}
2016-02-11 20:57:30 -08:00
function getPDO ()
{
2020-01-19 12:52:22 +01:00
return false ;
2016-02-11 20:57:30 -08:00
}
2015-02-15 16:07:27 -08:00
2019-02-11 15:40:10 -08:00
function debugMode ( $bool )
{
$this -> debugMode = ( bool ) $bool ;
}
2015-02-15 16:07:27 -08:00
2016-02-11 20:57:30 -08:00
function getMode ()
{
$this -> gen ( 'SELECT @@sql_mode' );
$row = $this -> fetch ();
return $row [ '@@sql_mode' ];
}
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
2016-02-11 20:57:30 -08:00
$this -> mySQLserver = $mySQLserver ;
$this -> mySQLuser = $mySQLuser ;
$this -> mySQLpassword = $mySQLpassword ;
$this -> mySQLdefaultdb = $mySQLdefaultdb ;
$this -> mySQLPrefix = $mySQLPrefix ;
$this -> mySQLerror = false ;
2016-09-19 13:38:59 -07:00
2020-01-19 13:33:52 +01:00
if ( ! $this -> mySQLaccess = @ mysqli_connect ( $this -> mySQLserver , $this -> mySQLuser , $this -> mySQLpassword , $newLink ))
2008-05-14 20:20:32 +00:00
{
2020-01-19 13:33:52 +01:00
$this -> mySQLlastErrText = mysqli_connect_error ();
2020-01-19 12:57:39 +01:00
return 'e1' ;
2008-11-27 20:13:58 +00:00
}
2006-12-02 04:36:16 +00:00
2020-01-19 13:33:52 +01:00
$this -> mySqlServerInfo = mysqli_get_server_info ( $this -> mySQLaccess ); // We always need this for db_Set_Charset() - so make generally available
2010-01-07 21:05:24 +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 ();
2017-01-11 13:30:57 -08:00
$this -> setSQLMode ();
2013-07-14 09:41:30 -07:00
if ( ! $this -> database ( $this -> mySQLdefaultdb ))
2009-07-17 14:20:26 +00:00
{
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
2016-02-11 20:57:30 -08:00
if ( $db_ConnectionID == null )
{
2009-07-17 14:20:26 +00:00
$db_ConnectionID = $this -> mySQLaccess ;
2016-02-11 20:57:30 -08:00
}
return true ;
2006-12-02 04:36:16 +00:00
}
2013-07-14 09:41:30 -07:00
/**
* Connect ONLY - used in v2 . x
* @ 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
2020-01-19 12:52:22 +01:00
* @ return boolean true on success , false on error .
2013-07-14 09:41:30 -07:00
*/
public function connect ( $mySQLserver , $mySQLuser , $mySQLpassword , $newLink = false )
{
global $db_ConnectionID , $db_defaultPrefix ;
2020-01-19 12:52:22 +01:00
2013-07-14 09:41:30 -07:00
e107 :: getSingleton ( 'e107_traffic' ) -> BumpWho ( 'db Connect' , 1 );
$this -> mySQLserver = $mySQLserver ;
$this -> mySQLuser = $mySQLuser ;
$this -> mySQLpassword = $mySQLpassword ;
$this -> mySQLerror = false ;
2016-07-09 10:55:27 -07:00
2017-04-28 11:07:37 -07:00
if ( strpos ( $mySQLserver , ':' ) !== false && substr_count ( $mySQLserver , ':' ) === 1 )
2016-07-09 10:55:27 -07:00
{
list ( $this -> mySQLserver , $this -> mySQLport ) = explode ( ':' , $mySQLserver , 2 );
}
2017-02-06 11:18:36 -08:00
2020-01-19 13:33:52 +01:00
if ( ! $this -> mySQLaccess = @ mysqli_connect ( $this -> mySQLserver , $this -> mySQLuser , $this -> mySQLpassword , $newLink ))
2017-02-06 11:18:36 -08:00
{
2020-01-19 13:33:52 +01:00
$this -> mySQLlastErrText = mysqli_connect_error ();
2020-01-19 12:52:22 +01:00
return false ;
2017-02-06 11:18:36 -08:00
}
2020-01-19 13:33:52 +01:00
$this -> mySqlServerInfo = mysqli_get_server_info ( $this -> mySQLaccess );
2013-07-14 09:41:30 -07:00
$this -> db_Set_Charset ();
2017-01-11 13:30:57 -08:00
$this -> setSQLMode ();
2017-03-31 10:51:21 -07:00
2020-01-19 12:52:22 +01:00
$db_ConnectionID = $this -> mySQLaccess ;
2013-07-14 09:41:30 -07:00
return true ;
}
2016-02-11 20:57:30 -08:00
/**
* Get Server Info
* @ return mixed
*/
public function getServerInfo ()
{
2019-02-11 15:40:10 -08:00
$this -> provide_mySQLaccess ();
2016-02-11 20:57:30 -08:00
return $this -> mySqlServerInfo ;
}
2013-07-14 09:41:30 -07:00
/**
2020-01-19 12:52:22 +01:00
* Select the database to use .
2017-03-31 10:51:21 -07:00
* @ param string $database name
* @ param string $table prefix . eg . e107_
* @ param boolean $multiple set to maintain connection to a secondary database .
2020-01-19 12:52:22 +01:00
* @ return boolean true when database selection was successful otherwise false .
2013-07-14 09:41:30 -07:00
*/
2017-03-31 10:51:21 -07:00
public function database ( $database , $prefix = MPREFIX , $multiple = false )
2013-07-14 09:41:30 -07:00
{
$this -> mySQLdefaultdb = $database ;
$this -> mySQLPrefix = $prefix ;
2017-03-31 10:51:21 -07:00
if ( $multiple === true )
{
$this -> mySQLPrefix = " ` " . $database . " `. " . $prefix ;
return true ;
}
2020-01-19 13:33:52 +01:00
if ( !@ mysqli_select_db ( $this -> mySQLaccess , $database ))
2013-07-14 09:41:30 -07:00
{
return false ;
}
2020-01-19 12:52:22 +01:00
return true ;
2013-07-14 09:41:30 -07:00
}
2010-10-28 13:31:54 +00:00
/**
* Get system config
* @ return e_core_pref
*/
public function getConfig ()
{
return e107 :: getConfig ( 'core' , false );
}
2006-12-30 01:11:49 +00:00
2006-12-02 04:36:16 +00:00
/**
* @ return void
2016-05-01 09:30:33 -07:00
* @ param string $sMarker
2020-05-02 15:35:30 -07:00
* @ deprecated Use e107 :: getDebug () -> logTime ();
2006-12-02 04:36:16 +00:00
* @ 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
{
2020-12-21 10:00:28 -08:00
trigger_error ( '<b>' . __METHOD__ . ' is deprecated.</b> Use e107::getDebug()->logTime() instead.' , E_USER_DEPRECATED ); // NO LAN
2019-02-11 15:40:10 -08:00
if ( $this -> debugMode !== true )
2009-05-24 15:34:37 +00:00
{
2019-02-10 16:19:25 -08:00
return null ;
2009-05-24 15:34:37 +00:00
}
2019-02-10 16:19:25 -08:00
$this -> dbg -> 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
/**
2016-02-11 20:57:30 -08:00
* @ deprecated
2006-12-02 04:36:16 +00:00
* @ return void
* @ desc Enter description here ...
* @ access private
*/
2009-12-02 16:51:04 +00:00
function db_Show_Performance ()
2009-05-24 15:34:37 +00:00
{
2016-02-11 20:57:30 -08:00
// e107::getDebug()-Show_P
// return $db_debug->Show_Performance();
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 add query to dblog table
* @ access private
*/
2009-12-02 16:51:04 +00:00
function db_Write_log ( $log_type = '' , $log_remark = '' , $log_query = '' )
2009-05-24 15:34:37 +00:00
{
2019-02-09 12:47:54 -08:00
$tp = e107 :: getParser ();
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 " );
2012-01-02 22:06:22 +00:00
$ip = e107 :: getIPHandler () -> getIP ( FALSE );
2006-12-02 04:36:16 +00:00
$qry = $tp -> toDB ( $log_query );
2015-02-14 23:34:15 -08:00
$this -> 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
/**
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-12-02 16:51:04 +00:00
*
2009-12-01 20:05:54 +00:00
* If a SELECT query includes SQL_CALC_FOUND_ROWS , the value of FOUND_ROWS () is retrieved and stored in $this -> total_results
2016-03-20 18:31:11 -07:00
* @ param string | array $query
2020-01-19 13:33:52 +01:00
* @ param mysqli $rli Your own mysqli connection instead of the one in this object
* @ return boolean | mysqli_result - as mysqli_query () function .
2009-12-01 20:05:54 +00:00
* FALSE indicates an error
* For SELECT , SHOW , DESCRIBE , EXPLAIN and others returning a result set , returns a resource
* TRUE indicates success in other cases
2006-12-02 04:36:16 +00:00
*/
2009-12-02 16:51:04 +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 ++ ;
2010-02-19 09:27:43 +00:00
2009-12-27 10:52:22 +00:00
$this -> mySQLlastQuery = $query ;
2006-12-02 04:36:16 +00:00
2009-12-02 16:51:04 +00:00
if ( $debug == 'now' )
2009-05-24 15:34:37 +00:00
{
2019-12-23 17:08:01 +01:00
$this -> dbg -> log ( $query );
2006-12-02 04:36:16 +00:00
}
2020-12-20 11:50:10 -08:00
if ( $debug !== FALSE || strpos ( $_SERVER [ 'QUERY_STRING' ], 'showsql' ) !== false )
2006-12-02 04:36:16 +00:00
{
2017-11-30 12:40:06 -08:00
$debugQry = is_array ( $query ) ? print_a ( $query , true ) : $query ;
$queryinfo [] = " <b> { $qry_from } </b>: " . $debugQry ;
2006-12-02 04:36:16 +00:00
}
2009-12-02 16:51:04 +00:00
if ( $log_type != '' )
2009-05-24 15:34:37 +00:00
{
2006-12-02 04:36:16 +00:00
$this -> db_Write_log ( $log_type , $log_remark , $query );
}
2018-07-20 05:24:55 -05:00
$this -> provide_mySQLaccess ();
2006-12-30 03:07:50 +00:00
2006-12-02 04:36:16 +00:00
$b = microtime ();
2016-08-12 16:02:55 -07:00
2020-01-19 13:33:52 +01:00
$sQryRes = is_null ( $rli ) ? @ mysqli_query ( $this -> mySQLaccess , $query ) : @ mysqli_query ( $rli , $query );
$this -> mySQLlastErrNum = mysqli_errno ( $this -> mySQLaccess );
$this -> mySQLlastErrText = mysqli_error ( $this -> mySQLaccess );
2016-02-11 20:57:30 -08:00
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
2015-07-25 18:14:44 -07:00
2016-03-29 14:55:38 -07:00
if ( ! E107_DEBUG_LEVEL )
{
$this -> total_results = false ;
}
2015-07-25 18:14:44 -07:00
// 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
2016-03-29 14:55:38 -07:00
if ( ! is_array ( $query ) && ( strpos ( $query , 'EXPLAIN' ) !== 0 ) && ( strpos ( $query , 'SQL_CALC_FOUND_ROWS' ) !== false ) && ( strpos ( $query , 'SELECT' ) !== false ))
2015-07-25 18:14:44 -07:00
{
2016-03-29 14:55:38 -07:00
2020-01-19 13:33:52 +01:00
$fr = mysqli_query ( $this -> mySQLaccess , 'SELECT FOUND_ROWS()' );
$rc = mysqli_fetch_array ( $fr );
2020-01-19 12:52:22 +01:00
$this -> total_results = ( int ) $rc [ 'FOUND_ROWS()' ];
2015-07-25 18:14:44 -07:00
2008-05-17 17:18:36 +00:00
}
2019-02-11 15:40:10 -08:00
if ( $this -> debugMode === true )
2009-05-24 15:34:37 +00:00
{
2019-01-26 10:28:08 -08:00
/** @var $db_debug e107_db_debug */
2006-12-02 04:36:16 +00:00
global $db_debug ;
$aTrace = debug_backtrace ();
2021-09-04 15:06:19 +02:00
$pTable = ( string ) $this -> mySQLcurTable ;
2006-12-02 04:36:16 +00:00
if ( ! strlen ( $pTable )) {
$pTable = '(complex query)' ;
} else {
$this -> mySQLcurTable = '' ; // clear before next query
}
2009-12-02 16:51:04 +00:00
if ( is_object ( $db_debug ))
2009-05-24 15:34:37 +00:00
{
2007-10-15 11:03:29 +00:00
$buglink = is_null ( $rli ) ? $this -> mySQLaccess : $rli ;
2020-01-19 12:52:22 +01:00
$db_debug -> Mark_Query ( $query , $buglink , $sQryRes , $aTrace , $mytime , $pTable );
2006-12-02 04:36:16 +00:00
}
}
2016-03-20 18:31:11 -07:00
2006-12-02 04:36:16 +00:00
return $sQryRes ;
}
2012-11-22 12:14:57 +02:00
/**
* Query and fetch at once
2015-02-15 02:37:36 -08:00
*
2012-11-22 12:14:57 +02:00
* Examples :
* < code >
* < ? php
2015-02-15 02:37:36 -08:00
*
2012-11-22 12:14:57 +02:00
* // Get single value, $multi and indexField are ignored
* $string = e107 :: getDb () -> retrieve ( 'user' , 'user_email' , 'user_id=1' );
2015-02-15 02:37:36 -08:00
*
2012-11-22 12:14:57 +02:00
* // Get single row set, $multi and indexField are ignored
* $array = e107 :: getDb () -> retrieve ( 'user' , 'user_email, user_name' , 'user_id=1' );
2015-02-15 02:37:36 -08:00
*
2012-12-08 21:09:58 +02:00
* // Fetch all, don't append WHERE to the query, index by user_id, noWhere auto detected (string starts with upper case ORDER)
* $array = e107 :: getDb () -> retrieve ( 'user' , 'user_id, user_email, user_name' , 'ORDER BY user_email LIMIT 0,20' , true , 'user_id' );
2015-02-15 02:37:36 -08:00
*
2012-11-22 12:14:57 +02:00
* // Same as above but retrieve() is only used to fetch, not useable for single return value
* if ( e107 :: getDb () -> select ( 'user' , 'user_id, user_email, user_name' , 'ORDER BY user_email LIMIT 0,20' , true ))
* {
2015-02-15 02:37:36 -08:00
* $array = e107 :: getDb () -> retrieve ( null , null , null , true , 'user_id' );
2012-11-22 12:14:57 +02:00
* }
2015-02-15 02:37:36 -08:00
*
2020-03-13 15:06:26 -07:00
* // Using whole query example, in this case default mode is 'one'
2015-02-15 02:37:36 -08:00
* $array = e107 :: getDb () -> retrieve ( ' SELECT
* p .* , u . user_email , u . user_name FROM `#user` AS u
* LEFT JOIN `#myplug_table` AS p ON p . myplug_table = u . user_id
* ORDER BY u . user_email LIMIT 0 , 20 '
2012-12-08 21:09:58 +02:00
* );
2015-02-15 02:37:36 -08:00
*
2012-12-08 21:09:58 +02:00
* // Using whole query example, multi mode - $fields argument mapped to $multi
* $array = e107 :: getDb () -> retrieve ( 'SELECT u.user_email, u.user_name FROM `#user` AS U ORDER BY user_email LIMIT 0,20' , true );
2015-02-15 02:37:36 -08:00
*
2012-12-08 21:09:58 +02:00
* // Using whole query example, multi mode with index field
* $array = e107 :: getDb () -> retrieve ( 'SELECT u.user_email, u.user_name FROM `#user` AS U ORDER BY user_email LIMIT 0,20' , null , null , true , 'user_id' );
2012-11-22 12:14:57 +02:00
* </ code >
2015-02-15 02:37:36 -08:00
*
2012-11-22 12:14:57 +02:00
* @ param string $table if empty , enter fetch only mode
2012-12-08 21:09:58 +02:00
* @ param string $fields comma separated list of fields or * or single field name ( get one ); if $fields is of type boolean and $where is not found , $fields overrides $multi
2012-11-22 12:14:57 +02:00
* @ param string $where WHERE / ORDER / LIMIT etc clause , empty to disable
* @ param boolean $multi if true , fetch all ( multi mode )
* @ param string $indexField field name to be used for indexing when in multi mode
2012-12-08 21:09:58 +02:00
* @ param boolean $debug
2018-08-26 10:23:55 -07:00
* @ return string | array
2012-11-22 12:14:57 +02:00
*/
2012-12-08 21:09:58 +02:00
public function retrieve ( $table , $fields = null , $where = null , $multi = false , $indexField = null , $debug = false )
2012-11-22 12:14:57 +02:00
{
// fetch mode
if ( empty ( $table ))
{
$ret = array ();
if ( ! $multi ) return $this -> fetch ();
2020-01-19 12:52:22 +01:00
2012-11-22 12:14:57 +02:00
while ( $row = $this -> fetch ())
{
if ( null !== $indexField ) $ret [ $row [ $indexField ]] = $row ;
else $ret [] = $row ;
}
return $ret ;
}
2020-01-19 12:52:22 +01:00
2012-11-22 12:14:57 +02:00
// detect mode
$mode = 'one' ;
2012-12-08 21:09:58 +02:00
if ( $table && ! $where && is_bool ( $fields ))
{
// table is the query, fields used for multi
if ( $fields ) $mode = 'multi' ;
else $mode = 'single' ;
$fields = null ;
}
elseif ( $fields && '*' !== $fields && strpos ( $fields , ',' ) === false && $where )
2012-11-22 12:14:57 +02:00
{
$mode = 'single' ;
}
2012-12-08 21:09:58 +02:00
if ( $multi )
2012-11-22 12:14:57 +02:00
{
$mode = 'multi' ;
}
2020-01-19 12:52:22 +01:00
2012-12-08 21:09:58 +02:00
// detect query type
$select = true ;
$noWhere = false ;
if ( ! $fields && ! $where )
{
// gen()
$select = false ;
2020-03-13 15:06:26 -07:00
if ( $mode == 'one' && ! preg_match ( '/[,*]+[\s\S]*FROM/im' , $table )) // if a comma or astericks is found before "FROM" then leave it in 'one' row mode.
{
$mode = 'single' ;
}
2012-12-08 21:09:58 +02:00
}
// auto detect noWhere - if where string starts with upper case LATIN word
elseif ( ! $where || preg_match ( '/^[A-Z]+\S.*$/' , trim ( $where )))
{
// FIXME - move auto detect to select()?
$noWhere = true ;
}
2020-01-19 12:52:22 +01:00
2012-11-22 12:14:57 +02:00
// execute & fetch
2020-01-19 12:52:22 +01:00
switch ( $mode )
2012-11-22 12:14:57 +02:00
{
case 'single' :
2012-12-08 21:09:58 +02:00
if ( $select && ! $this -> select ( $table , $fields , $where , $noWhere , $debug ))
{
return null ;
}
2012-12-15 04:42:13 -08:00
elseif ( ! $select && ! $this -> gen ( $table , $debug ))
2012-11-22 12:14:57 +02:00
{
return null ;
}
2016-02-16 11:10:30 -08:00
$rows = $this -> fetch ();
return array_shift ( $rows );
2012-11-22 12:14:57 +02:00
break ;
2020-01-19 12:52:22 +01:00
2012-11-22 12:14:57 +02:00
case 'one' :
2012-12-08 21:09:58 +02:00
if ( $select && ! $this -> select ( $table , $fields , $where , $noWhere , $debug ))
{
return array ();
}
2012-12-15 04:42:13 -08:00
elseif ( ! $select && ! $this -> gen ( $table , $debug ))
2012-11-22 12:14:57 +02:00
{
return array ();
}
return $this -> fetch ();
break ;
2020-01-19 12:52:22 +01:00
2012-11-22 12:14:57 +02:00
case 'multi' :
2012-12-08 21:09:58 +02:00
if ( $select && ! $this -> select ( $table , $fields , $where , $noWhere , $debug ))
{
return array ();
}
2012-12-15 04:42:13 -08:00
elseif ( ! $select && ! $this -> gen ( $table , $debug ))
2012-11-27 19:57:34 -08:00
{
2012-11-22 12:14:57 +02:00
return array ();
}
$ret = array ();
while ( $row = $this -> fetch ())
{
if ( null !== $indexField ) $ret [ $row [ $indexField ]] = $row ;
else $ret [] = $row ;
}
return $ret ;
break ;
2020-01-19 12:52:22 +01:00
2012-11-22 12:14:57 +02:00
}
}
2009-05-24 15:34:37 +00:00
2006-12-02 04:36:16 +00:00
/**
2020-01-19 13:33:52 +01:00
* Perform a mysqli_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 />
2012-11-22 12:14:57 +02:00
* < code > e107 :: getDb () -> select ( " comments " , " * " , " comment_item_id = ' $id ' AND comment_type = '1' ORDER BY comment_datestamp " ); </ code >< br />
2006-12-02 04:36:16 +00:00
* < br />
* OR as second connection :< br />
2012-11-22 12:14:57 +02:00
* < code >
* e107 :: getDb ( 'sql2' ) -> select ( " chatbox " , " * " , " ORDER BY cb_datestamp DESC LIMIT $from , " . $view , true ); </ code >
2006-12-02 04:36:16 +00:00
*
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
*/
2012-11-22 12:14:57 +02:00
public function select ( $table , $fields = '*' , $arg = '' , $noWhere = false , $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
2020-12-14 16:21:48 -08:00
$table = $this -> hasLanguage ( $table );
2009-10-22 13:00:37 +00:00
2006-12-02 04:36:16 +00:00
$this -> mySQLcurTable = $table ;
2020-01-19 12:52:22 +01:00
2013-03-28 00:42:02 -07:00
if ( $arg != '' && ( $noWhere === false || $noWhere === 'default' )) // 'default' for BC.
2006-12-02 04:36:16 +00:00
{
2009-12-02 16:51:04 +00:00
if ( $this -> mySQLresult = $this -> db_Query ( 'SELECT ' . $fields . ' FROM ' . $this -> mySQLPrefix . $table . ' WHERE ' . $arg , NULL , 'db_Select' , $debug , $log_type , $log_remark ))
2009-05-24 15:34:37 +00:00
{
2006-12-02 04:36:16 +00:00
$this -> dbError ( 'dbQuery' );
2014-11-23 20:01:32 -08:00
return $this -> rowCount ();
2009-12-02 16:51:04 +00:00
}
else
2009-05-24 15:34:37 +00:00
{
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-12-02 16:51:04 +00:00
}
2013-03-28 00:42:02 -07:00
elseif ( $arg != '' && ( $noWhere !== false ) && ( $noWhere !== 'default' )) // 'default' for BC.
2009-05-24 15:34:37 +00:00
{
2009-12-02 16:51:04 +00:00
if ( $this -> mySQLresult = $this -> db_Query ( 'SELECT ' . $fields . ' FROM ' . $this -> mySQLPrefix . $table . ' ' . $arg , NULL , 'db_Select' , $debug , $log_type , $log_remark ))
2009-05-24 15:34:37 +00:00
{
2006-12-02 04:36:16 +00:00
$this -> dbError ( 'dbQuery' );
2014-11-23 20:01:32 -08:00
return $this -> rowCount ();
2009-12-02 16:51:04 +00:00
}
else
2009-05-24 15:34:37 +00:00
{
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-12-02 16:51:04 +00:00
}
else
2009-05-24 15:34:37 +00:00
{
2009-12-02 16:51:04 +00:00
if ( $this -> mySQLresult = $this -> db_Query ( 'SELECT ' . $fields . ' FROM ' . $this -> mySQLPrefix . $table , NULL , 'db_Select' , $debug , $log_type , $log_remark ))
2009-05-24 15:34:37 +00:00
{
2006-12-02 04:36:16 +00:00
$this -> dbError ( 'dbQuery' );
2014-11-23 20:01:32 -08:00
return $this -> rowCount ();
2009-12-02 16:51:04 +00:00
}
else
2009-05-24 15:34:37 +00:00
{
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 ;
}
}
}
2012-11-22 12:14:57 +02:00
/**
* select () alias
2020-01-19 12:52:22 +01:00
*
2012-11-22 12:14:57 +02:00
* @ deprecated
*/
public function db_Select ( $table , $fields = '*' , $arg = '' , $mode = 'default' , $debug = FALSE , $log_type = '' , $log_remark = '' )
{
2020-12-21 10:00:28 -08:00
trigger_error ( '<b>' . __METHOD__ . ' is deprecated.</b> Use e107::getDb()->select() instead.' , E_USER_DEPRECATED ); // NO LAN
2012-11-22 12:14:57 +02:00
return $this -> select ( $table , $fields , $arg , $mode !== 'default' , $debug , $log_type , $log_remark );
}
2009-05-24 15:34:37 +00:00
2006-12-02 04:36:16 +00:00
/**
2019-01-26 10:28:08 -08:00
* @ param string $tableName - Name of table to access , without any language or general DB prefix
* @ param $arg
* @ param bool $debug
* @ param string $log_type
* @ param string $log_remark
* @ return int Last insert ID or false on error . When using '_DUPLICATE_KEY_UPDATE' return ID , true on update , 0 on no change and false on error .
* @ desc Insert a row into the table < br />
* < br />
* Example :< br />
* < code > e107 :: getDb () -> insert ( " links " , " 0, 'News', 'news.php', '', '', 1, 0, 0, 0 " ); </ code >
*
* @ access public
*/
2012-11-22 12:14:57 +02:00
function insert ( $tableName , $arg , $debug = FALSE , $log_type = '' , $log_remark = '' )
2009-01-09 16:22:08 +00:00
{
2020-12-14 16:21:48 -08:00
$table = $this -> hasLanguage ( $tableName );
2006-12-02 04:36:16 +00:00
$this -> mySQLcurTable = $table ;
2010-02-19 09:27:43 +00:00
$REPLACE = false ; // kill any PHP notices
2016-08-07 13:21:22 -07:00
$DUPEKEY_UPDATE = false ;
2018-05-09 11:13:05 -07:00
$IGNORE = '' ;
2016-08-07 13:21:22 -07:00
2006-12-02 04:36:16 +00:00
if ( is_array ( $arg ))
{
2009-12-02 16:51:04 +00:00
if ( isset ( $arg [ 'WHERE' ])) // use same array for update and insert.
2009-11-20 05:01:51 +00:00
{
unset ( $arg [ 'WHERE' ]);
}
2016-08-07 13:21:22 -07:00
2009-08-29 18:07:42 +00:00
if ( isset ( $arg [ '_REPLACE' ]))
{
$REPLACE = TRUE ;
2009-12-02 16:51:04 +00:00
unset ( $arg [ '_REPLACE' ]);
2009-08-29 18:07:42 +00:00
}
2009-12-02 16:51:04 +00:00
2016-08-02 14:57:29 -07:00
if ( isset ( $arg [ '_DUPLICATE_KEY_UPDATE' ]))
{
$DUPEKEY_UPDATE = true ;
unset ( $arg [ '_DUPLICATE_KEY_UPDATE' ]);
2018-05-09 11:13:05 -07:00
}
2016-08-07 13:21:22 -07:00
2018-05-09 11:13:05 -07:00
if ( isset ( $arg [ '_IGNORE' ]))
{
$IGNORE = ' IGNORE' ;
unset ( $arg [ '_IGNORE' ]);
2016-08-02 14:57:29 -07:00
}
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 );
}
2016-08-07 13:21:22 -07:00
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
2009-12-25 10:25:12 +00:00
// See if we need to auto-add field types array
2020-01-17 15:54:56 +01:00
if ( ! isset ( $arg [ '_FIELD_TYPES' ]))
2009-12-25 10:25:12 +00:00
{
2020-01-17 17:24:06 +01:00
$fieldDefs = $this -> getFieldDefs ( $tableName );
if ( is_array ( $fieldDefs )) $arg = array_merge ( $arg , $fieldDefs );
2009-12-25 10:25:12 +00:00
}
2016-08-07 14:17:27 -07:00
$argUpdate = $arg ; // used when DUPLICATE_KEY_UPDATE is active;
2009-12-25 10:25:12 +00:00
2016-03-20 18:31:11 -07:00
2009-10-29 21:28:09 +00:00
// Handle 'NOT NULL' fields without a default value
if ( isset ( $arg [ '_NOTNULL' ]))
{
foreach ( $arg [ '_NOTNULL' ] as $f => $v )
{
if ( ! isset ( $arg [ 'data' ][ $f ]))
{
$arg [ 'data' ][ $f ] = $v ;
}
}
}
2016-03-20 18:31:11 -07: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 ();
2016-03-20 18:31:11 -07:00
2009-01-09 16:22:08 +00:00
foreach ( $arg [ 'data' ] as $fk => $fv )
2008-11-27 20:13:58 +00:00
{
2020-01-19 12:52:22 +01:00
$tmp [] = $this -> _getFieldValue ( $fk , $fv , $fieldTypes );
2008-11-27 20:13:58 +00:00
}
2016-03-20 18:31:11 -07:00
2008-11-27 20:13:58 +00:00
$valList = implode ( ', ' , $tmp );
2016-03-20 18:31:11 -07:00
2008-11-27 20:13:58 +00:00
unset ( $tmp );
2009-12-02 16:51:04 +00:00
2016-08-02 14:57:29 -07:00
2016-03-20 18:31:11 -07:00
if ( $REPLACE === false )
2009-08-29 18:07:42 +00:00
{
2019-01-05 14:37:12 -08:00
$query = " INSERT " . $IGNORE . " INTO " . $this -> mySQLPrefix . " { $table } ( { $keyList } ) VALUES ( { $valList } ) " ;
2016-04-11 15:19:05 -07:00
2016-08-02 14:57:29 -07:00
if ( $DUPEKEY_UPDATE === true )
{
$query .= " ON DUPLICATE KEY UPDATE " ;
2016-08-07 14:17:27 -07:00
$query .= $this -> _prepareUpdateArg ( $tableName , $argUpdate );
2016-08-02 14:57:29 -07:00
}
2009-08-29 18:07:42 +00:00
}
else
{
2019-01-05 14:37:12 -08:00
$query = " REPLACE INTO " . $this -> mySQLPrefix . " { $table } ( { $keyList } ) VALUES ( { $valList } ) " ;
2009-08-29 18:07:42 +00:00
}
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
}
2018-07-20 05:24:55 -05:00
$this -> provide_mySQLaccess ();
2010-02-19 09:27:43 +00:00
2010-02-08 09:12:07 +00:00
$this -> mySQLresult = $this -> db_Query ( $query , NULL , 'db_Insert' , $debug , $log_type , $log_remark );
2016-03-20 18:31:11 -07:00
2016-08-07 13:21:22 -07:00
if ( $DUPEKEY_UPDATE === true )
{
$result = false ; // ie. there was an error.
2020-01-19 13:33:52 +01:00
$this -> mySQLresult = mysqli_affected_rows ( $this -> mySQLaccess );
2016-08-17 15:23:33 -07:00
if ( $this -> mySQLresult === 1 ) // insert.
2016-08-07 13:21:22 -07:00
{
$result = $this -> lastInsertId ();
}
2016-08-17 15:23:33 -07:00
elseif ( $this -> mySQLresult === 2 || $this -> mySQLresult === true ) // updated
2016-08-07 13:21:22 -07:00
{
$result = true ;
2019-02-25 18:51:49 -08:00
// reset auto-increment to prevent gaps.
$this -> db_Query ( " ALTER TABLE " . $this -> mySQLPrefix . $table . " AUTO_INCREMENT=1 " , NULL , 'db_Insert' , $debug , $log_type , $log_remark );
2016-08-07 13:21:22 -07:00
}
elseif ( $this -> mySQLresult === 0 ) // updated (no change)
{
$result = 0 ;
}
$this -> dbError ( 'db_Insert' );
return $result ;
}
2010-02-08 09:12:07 +00:00
if ( $this -> mySQLresult )
2008-11-29 01:24:27 +00:00
{
2010-02-08 09:12:07 +00:00
if ( true === $REPLACE )
{
2020-01-19 13:33:52 +01:00
$tmp = mysqli_affected_rows ( $this -> mySQLaccess );
2010-02-08 09:12:07 +00:00
$this -> dbError ( 'db_Replace' );
// $tmp == -1 (error), $tmp == 0 (not modified), $tmp == 1 (added), greater (replaced)
2020-01-19 13:33:52 +01:00
if ( $tmp == - 1 ) { return false ; } // mysqli_affected_rows error
2010-02-08 09:12:07 +00:00
return $tmp ;
}
2016-03-20 18:31:11 -07:00
$tmp = $this -> lastInsertId ();
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
{
2016-05-29 11:00:37 -07:00
// $this->dbError("db_Insert ({$query})");
2006-12-02 04:36:16 +00:00
return FALSE ;
}
}
2020-01-19 12:52:22 +01:00
2014-11-23 20:01:32 -08:00
public function lastInsertId ()
{
2020-01-19 13:33:52 +01:00
$tmp = mysqli_insert_id ( $this -> mySQLaccess );
2014-11-23 20:01:32 -08:00
return ( $tmp ) ? $tmp : true ; // return true even if table doesn't have auto-increment.
}
2016-03-18 08:33:06 -07:00
/**
2016-03-29 14:55:38 -07:00
* Return the total number of results on the last query regardless of the LIMIT value when SELECT SQL_CALC_FOUND_ROWS is used .
2016-03-18 08:33:06 -07:00
* @ return bool
*/
2016-03-29 14:55:38 -07:00
public function foundRows ()
2016-03-18 08:33:06 -07:00
{
return $this -> total_results ;
}
2020-01-19 12:52:22 +01:00
/**
2020-01-19 13:33:52 +01:00
* @ param mysqli_result $result
2020-01-19 12:52:22 +01:00
* @ return false | int
*/
2014-11-23 20:01:32 -08:00
public function rowCount ( $result = null )
{
2020-01-19 13:33:52 +01:00
if ( ! ( $result instanceof mysqli_result ))
2016-02-12 19:28:35 -08:00
{
2020-01-19 12:52:22 +01:00
$result = $this -> mySQLresult ;
2020-01-19 12:30:23 +01:00
}
2020-01-19 13:33:52 +01:00
if ( $result instanceof mysqli_result )
2020-01-19 12:30:23 +01:00
{
2020-01-19 13:33:52 +01:00
$this -> mySQLrows = mysqli_num_rows ( $result );
2020-01-19 12:30:23 +01:00
}
2014-11-23 20:01:32 -08:00
$this -> dbError ( 'db_Rows' );
2020-01-19 12:30:23 +01:00
return $this -> mySQLrows ;
2014-11-23 20:01:32 -08:00
}
2020-02-22 10:03:41 -08:00
/**
* hasLanguage () alias
2020-12-14 16:21:48 -08:00
* @ deprecated Use hasLanguage ( $table , $multiple )
2020-02-22 10:03:41 -08:00
*/
function db_IsLang ( $table , $multiple = false )
{
return $this -> hasLanguage ( $table , $multiple );
}
2012-11-22 12:14:57 +02:00
/**
* insert () alias
* @ deprecated
*/
function db_Insert ( $tableName , $arg , $debug = FALSE , $log_type = '' , $log_remark = '' )
{
return $this -> insert ( $tableName , $arg , $debug , $log_type , $log_remark );
}
2006-12-02 04:36:16 +00:00
2009-08-29 18:07:42 +00:00
/**
2019-01-26 10:28:08 -08:00
* @ param string $table
* @ param array $arg
* @ param bool $debug
* @ param string $log_type
* @ param string $log_remark
* @ return int Last insert ID or false on error
* @ desc Insert / REplace a row into the table < br />
* < br />
* Example :< br />
* < code > e107 :: getDb () -> replace ( " links " , $array ); </ code >
*
* @ access public
*/
2012-11-22 12:14:57 +02:00
function replace ( $table , $arg , $debug = FALSE , $log_type = '' , $log_remark = '' )
2009-08-29 18:07:42 +00:00
{
$arg [ '_REPLACE' ] = TRUE ;
2013-04-16 15:59:17 -07:00
return $this -> insert ( $table , $arg , $debug , $log_type , $log_remark );
2009-08-29 18:07:42 +00:00
}
2020-01-19 12:52:22 +01:00
2012-11-22 12:14:57 +02:00
/**
* replace () alias
* @ deprecated
*/
function db_Replace ( $table , $arg , $debug = FALSE , $log_type = '' , $log_remark = '' )
{
return $this -> replace ( $table , $arg , $debug , $log_type , $log_remark );
}
2009-08-29 18:07:42 +00:00
2009-05-24 15:34:37 +00:00
2016-08-02 14:57:29 -07:00
private function _prepareUpdateArg ( $tableName , $arg )
2009-01-09 16:22:08 +00:00
{
2016-08-02 14:57:29 -07:00
if ( is_array ( $arg )) // Remove the need for a separate db_UpdateArray() function.
2009-09-10 19:13:39 +00:00
{
if ( ! isset ( $arg [ '_FIELD_TYPES' ]) && ! isset ( $arg [ 'data' ]))
{
2009-12-25 10:25:12 +00:00
//Convert data if not using 'new' format
2009-09-10 19:13:39 +00:00
$_tmp = array ();
if ( isset ( $arg [ 'WHERE' ]))
{
$_tmp [ 'WHERE' ] = $arg [ 'WHERE' ];
unset ( $arg [ 'WHERE' ]);
}
$_tmp [ 'data' ] = $arg ;
$arg = $_tmp ;
unset ( $_tmp );
}
2016-08-02 14:57:29 -07:00
2009-09-10 19:13:39 +00:00
if ( ! isset ( $arg [ 'data' ])) { return false ; }
2009-01-09 16:22:08 +00:00
2009-12-25 10:25:12 +00:00
// See if we need to auto-add field types array
2020-01-17 15:54:56 +01:00
if ( ! isset ( $arg [ '_FIELD_TYPES' ]))
2009-12-25 10:25:12 +00:00
{
2020-01-17 17:24:06 +01:00
$fieldDefs = $this -> getFieldDefs ( $tableName );
if ( is_array ( $fieldDefs )) $arg = array_merge ( $arg , $fieldDefs );
2009-12-25 10:25:12 +00:00
}
2008-11-29 01:24:27 +00:00
$fieldTypes = $this -> _getTypes ( $arg );
2013-03-31 00:06:21 -07:00
$new_data = '' ;
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 ? ', ' : '' );
2016-03-20 18:31:11 -07:00
2020-01-19 12:52:22 +01:00
$new_data .= " ` { $fn } `= " . $this -> _getFieldValue ( $fn , $fv , $fieldTypes );
2008-11-27 20:13:58 +00:00
}
2016-03-20 18:31:11 -07:00
2009-01-09 16:22:08 +00:00
$arg = $new_data . ( isset ( $arg [ 'WHERE' ]) ? ' WHERE ' . $arg [ 'WHERE' ] : '' );
2016-03-20 18:31:11 -07:00
2008-11-26 23:34:35 +00:00
}
2008-08-14 22:34:06 +00:00
2016-08-02 14:57:29 -07:00
return $arg ;
}
/**
* @ return int number of affected rows , or false on error
* @ param string $tableName - Name of table to access , without any language or general DB prefix
* @ param array | string $arg ( array preferred )
* @ 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 > e107 :: getDb () -> update ( " user " , " user_viewed=' $u_new ' WHERE user_id=' " . USERID . " ' " ); </ code >
* < br />
* OR as second connection < br />
* < code >
* e107 :: getDb ( 'sql2' ) -> update ( " user " , " user_viewed = ' $u_new ' WHERE user_id = ' " . USERID . " ' " ); </ code >< br />
*
* @ access public
*/
function update ( $tableName , $arg , $debug = FALSE , $log_type = '' , $log_remark = '' )
{
2020-12-14 16:21:48 -08:00
$table = $this -> hasLanguage ( $tableName );
2016-08-02 14:57:29 -07:00
$this -> mySQLcurTable = $table ;
2018-07-20 05:24:55 -05:00
$this -> provide_mySQLaccess ();
2016-08-02 14:57:29 -07:00
$arg = $this -> _prepareUpdateArg ( $tableName , $arg );
2009-09-06 20:04:04 +00:00
$query = 'UPDATE ' . $this -> mySQLPrefix . $table . ' SET ' . $arg ;
2016-03-20 18:31:11 -07:00
2016-08-02 14:57:29 -07:00
$result = $this -> mySQLresult = $this -> db_Query ( $query , NULL , 'db_Update' , $debug , $log_type , $log_remark );
2016-03-20 18:31:11 -07:00
2016-08-02 14:57:29 -07:00
if ( $result !== false )
2008-11-26 23:34:35 +00:00
{
2020-01-19 13:33:52 +01:00
$result = mysqli_affected_rows ( $this -> mySQLaccess );
2016-08-02 14:57:29 -07:00
2009-05-24 15:34:37 +00:00
$this -> dbError ( 'db_Update' );
2020-01-19 13:33:52 +01:00
if ( $result === - 1 ) { return false ; } // Error return from mysqli_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 ;
}
}
2012-11-22 12:14:57 +02:00
/**
* update () alias
* @ deprecated
*/
function db_Update ( $tableName , $arg , $debug = FALSE , $log_type = '' , $log_remark = '' )
{
return $this -> update ( $tableName , $arg , $debug , $log_type , $log_remark );
}
2008-11-29 01:24:27 +00:00
function _getTypes ( & $arg )
{
if ( isset ( $arg [ '_FIELD_TYPES' ]))
{
if ( ! isset ( $arg [ '_FIELD_TYPES' ][ '_DEFAULT' ]))
{
2013-03-31 05:55:08 -07:00
$arg [ '_FIELD_TYPES' ][ '_DEFAULT' ] = 'string' ;
2008-11-29 01:24:27 +00:00
}
$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' :
2009-10-21 09:12:12 +00:00
return ( int ) $fieldValue ;
2012-12-13 16:13:41 +02:00
break ;
2008-11-27 20:13:58 +00:00
case 'cmd' :
2008-11-29 01:24:27 +00:00
return $fieldValue ;
2012-12-13 16:13:41 +02:00
break ;
2020-01-19 12:52:22 +01:00
2012-12-13 16:13:41 +02:00
case 'safestr' :
return " ' { $fieldValue } ' " ;
break ;
2008-11-27 20:13:58 +00:00
2009-09-10 19:13:39 +00:00
case 'str' :
2008-11-29 01:24:27 +00:00
case 'string' :
2012-12-13 15:47:48 +02:00
//return "'{$fieldValue}'";
return " ' " . $this -> escape ( $fieldValue , false ) . " ' " ;
2012-12-13 16:13:41 +02:00
break ;
2009-12-02 16:51:04 +00:00
case 'float' :
2011-01-04 12:10:47 +00:00
// fix - convert localized float numbers
2018-05-01 23:29:50 +02:00
// $larr = localeconv();
// $search = array($larr['decimal_point'], $larr['mon_decimal_point'], $larr['thousands_sep'], $larr['mon_thousands_sep'], $larr['currency_symbol'], $larr['int_curr_symbol']);
// $replace = array('.', '.', '', '', '', '');
2011-01-04 12:10:47 +00:00
2018-05-01 23:29:50 +02:00
// return str_replace($search, $replace, floatval($fieldValue));
2018-05-04 19:27:00 +02:00
return e107 :: getParser () -> toNumber ( $fieldValue );
2009-10-21 09:12:12 +00:00
break ;
2009-12-02 16:51:04 +00:00
2009-09-10 19:13:39 +00:00
case 'null' :
2012-12-13 15:47:48 +02:00
//return ($fieldValue && $fieldValue !== 'NULL' ? "'{$fieldValue}'" : 'NULL');
return ( $fieldValue && $fieldValue !== 'NULL' ? " ' " . $this -> escape ( $fieldValue , false ) . " ' " : 'NULL' );
2009-09-10 19:13:39 +00:00
break ;
2008-11-27 20:13:58 +00:00
2012-02-07 16:37:44 +00:00
case 'array' :
if ( is_array ( $fieldValue ))
{
2020-12-22 14:48:28 -08:00
return " ' " . e107 :: serialize ( $fieldValue , true ) . " ' " ;
2012-02-07 16:37:44 +00:00
}
return " ' " . ( string ) $fieldValue . " ' " ;
break ;
2008-12-07 00:21:21 +00:00
2013-04-16 15:59:17 -07:00
case 'todb' : // using as default causes serious BC issues.
2008-11-29 18:09:51 +00:00
if ( $fieldValue == '' ) { return " '' " ; }
2011-06-22 09:01:02 +00:00
return " ' " . e107 :: getParser () -> toDB ( $fieldValue ) . " ' " ;
2012-12-13 16:13:41 +02:00
break ;
2020-01-19 12:52:22 +01:00
2013-04-16 15:59:17 -07:00
case 'escape' :
default :
return " ' " . $this -> escape ( $fieldValue , false ) . " ' " ;
break ;
2008-11-27 20:13:58 +00:00
}
}
2013-03-31 00:06:21 -07:00
/**
* @ DEPRECATED
Similar to db_Update (), but splits the variables and the 'WHERE' clause .
2007-12-26 13:21:34 +00:00
$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
{
2020-12-14 16:21:48 -08:00
$table = $this -> hasLanguage ( $table );
2007-12-26 13:21:34 +00:00
$this -> mySQLcurTable = $table ;
2018-07-20 05:24:55 -05:00
$this -> provide_mySQLaccess ();
2007-12-26 13:21:34 +00:00
$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
{
2020-01-19 13:33:52 +01:00
$result = mysqli_affected_rows ( $this -> mySQLaccess );
if ( $result == - 1 ) return FALSE ; // Error return from mysqli_affected_rows
2007-12-26 13:21:34 +00:00
return $result ;
2008-11-27 20:13:58 +00:00
}
else
2007-12-26 13:21:34 +00:00
{
2018-09-06 11:20:43 -07:00
$query = 'UPDATE ' . $this -> mySQLPrefix . $table . ' SET ' . $new_data . $vars . ' ' . $arg ;
$this -> dbError ( " Error in deprecated db_UpdateArray method query:( $query ) " );
2007-12-26 13:21:34 +00:00
return FALSE ;
}
}
2013-04-16 15:59:17 -07:00
/**
* Truncate a table
* @ param string $table - table name without e107 prefix
*/
function truncate ( $table = null )
{
2019-01-05 14:37:12 -08:00
if ( $table == null ){ return null ; }
return $this -> gen ( " TRUNCATE TABLE " . $this -> mySQLPrefix . $table );
2013-04-16 15:59:17 -07:00
}
2006-12-02 04:36:16 +00:00
/**
2016-02-14 12:15:55 -08:00
* @ param string $type assoc | num | both
2019-01-05 14:37:12 -08:00
* @ return array | bool MySQL row
2020-01-19 13:33:52 +01:00
* @ desc Fetch an array containing row data ( see PHP ' s mysqli_fetch_array () docs ) < br />
2016-02-11 20:57:30 -08:00
* @ example
2006-12-02 04:36:16 +00:00
* Example :< br />
2012-11-22 12:14:57 +02:00
* < code > while ( $row = $sql -> fetch ()){
2006-12-02 04:36:16 +00:00
* $text .= $row [ 'username' ];
* } </ code >
*
* @ access public
*/
2017-01-06 19:30:34 -08:00
function fetch ( $type = null )
2009-05-24 15:34:37 +00:00
{
2016-02-14 12:15:55 -08:00
if ( defined ( 'MYSQL_ASSOC' ))
2013-04-24 17:44:07 -07:00
{
2016-02-14 12:15:55 -08:00
switch ( $type )
2013-04-24 17:44:07 -07:00
{
2016-02-14 12:15:55 -08:00
case 'both' :
2016-02-15 00:56:08 -08:00
case 3 : // MYSQL_BOTH:
2020-01-19 12:52:22 +01:00
$type = MYSQL_BOTH ; // 3
2016-02-14 12:15:55 -08:00
break ;
case 'num' :
2016-02-15 00:56:08 -08:00
case 2 ; // MYSQL_NUM: // 2
2020-01-19 12:52:22 +01:00
$type = MYSQL_NUM ;
2016-02-14 12:15:55 -08:00
break ;
2017-01-06 19:30:34 -08:00
default :
2016-02-14 12:15:55 -08:00
case 'assoc' :
2016-02-15 00:56:08 -08:00
case 1 ; //: // 1
2020-01-19 12:52:22 +01:00
$type = MYSQL_ASSOC ;
2016-02-14 12:15:55 -08:00
break ;
}
}
2016-02-14 12:33:35 -08:00
2006-12-02 04:36:16 +00:00
$b = microtime ();
2019-01-26 10:28:08 -08:00
2009-09-17 14:25:11 +00:00
if ( $this -> mySQLresult )
{
2020-01-19 13:33:52 +01:00
$row = @ mysqli_fetch_array ( $this -> mySQLresult , $type );
2009-09-17 14:25:11 +00:00
e107 :: getSingleton ( 'e107_traffic' ) -> Bump ( 'db_Fetch' , $b );
2009-12-02 16:51:04 +00:00
if ( $row )
2009-10-03 14:54:46 +00:00
{
$this -> dbError ( 'db_Fetch' );
return $row ; // Success - return data
2009-12-02 16:51:04 +00:00
}
2009-09-17 14:25:11 +00:00
}
2009-10-03 14:54:46 +00:00
$this -> dbError ( 'db_Fetch' );
return FALSE ; // Failure
2006-12-02 04:36:16 +00:00
}
2020-01-19 12:52:22 +01:00
2012-11-22 12:14:57 +02:00
/**
* fetch () alias
* @ deprecated
*/
2016-02-14 12:15:55 -08:00
function db_Fetch ( $type = null )
2012-11-22 12:14:57 +02:00
{
return $this -> fetch ( $type );
}
2006-12-02 04:36:16 +00:00
/**
2019-01-26 10:28:08 -08:00
* @ param string $table
* @ param string $fields
* @ param string $arg
* @ param bool $debug
* @ param string $log_type
* @ param string $log_remark
* @ return int number of affected rows or false on error
* @ desc Count the number of rows in a select < br />
* < br />
* Example :< br />
* < code > $topics = e107 :: getDb () -> count ( " forum_thread " , " (*) " , " thread_forum_id=' " . $forum_id . " ' AND thread_parent='0' " ); </ code >
*
* @ access public
*/
2012-11-22 12:14:57 +02:00
function count ( $table , $fields = '(*)' , $arg = '' , $debug = FALSE , $log_type = '' , $log_remark = '' )
2009-05-24 15:34:37 +00:00
{
2020-12-14 16:21:48 -08:00
$table = $this -> hasLanguage ( $table );
2006-12-02 04:36:16 +00:00
2009-12-02 16:51:04 +00:00
if ( $fields == 'generic' )
2009-05-24 15:34:37 +00:00
{
2006-12-02 04:36:16 +00:00
$query = $table ;
2009-12-02 16:51:04 +00:00
if ( $this -> mySQLresult = $this -> db_Query ( $query , NULL , 'db_Count' , $debug , $log_type , $log_remark ))
2009-05-24 15:34:37 +00:00
{
2020-01-19 13:33:52 +01:00
$rows = $this -> mySQLrows = @ mysqli_fetch_array ( $this -> mySQLresult );
2009-05-24 15:34:37 +00:00
$this -> dbError ( 'db_Count' );
2019-01-05 14:37:12 -08:00
return ( int ) $rows [ 'COUNT(*)' ];
2009-12-02 16:51:04 +00:00
}
else
2009-05-24 15:34:37 +00:00
{
$this -> dbError ( " db_Count ( { $query } ) " );
2006-12-02 04:36:16 +00:00
return FALSE ;
}
}
$this -> mySQLcurTable = $table ;
2009-12-02 16:51:04 +00:00
// normalize query arguments - only COUNT expected 'WHERE', not anymore
2009-11-26 17:14:07 +00:00
if ( $arg && stripos ( trim ( $arg ), 'WHERE' ) !== 0 )
{
$arg = 'WHERE ' . $arg ;
}
2008-05-14 20:20:32 +00:00
$query = 'SELECT COUNT' . $fields . ' FROM ' . $this -> mySQLPrefix . $table . ' ' . $arg ;
2009-12-02 16:51:04 +00:00
if ( $this -> mySQLresult = $this -> db_Query ( $query , NULL , 'db_Count' , $debug , $log_type , $log_remark ))
2009-05-24 15:34:37 +00:00
{
2020-01-19 13:33:52 +01:00
$rows = $this -> mySQLrows = @ mysqli_fetch_array ( $this -> mySQLresult );
2009-05-24 15:34:37 +00:00
$this -> dbError ( 'db_Count' );
2019-01-05 14:37:12 -08:00
return ( int ) $rows [ 0 ];
2009-12-02 16:51:04 +00:00
}
else
2009-05-24 15:34:37 +00:00
{
$this -> dbError ( " db_Count( { $query } ) " );
2006-12-02 04:36:16 +00:00
return FALSE ;
}
}
2015-06-17 20:17:00 -07:00
/**
* @ deprecated use count ()
*/
2012-11-22 12:14:57 +02:00
function db_Count ( $table , $fields = '(*)' , $arg = '' , $debug = FALSE , $log_type = '' , $log_remark = '' )
{
return $this -> count ( $table , $fields , $arg , $debug , $log_type , $log_remark );
}
2006-12-02 04:36:16 +00:00
2009-05-24 15:34:37 +00:00
2006-12-02 04:36:16 +00:00
/**
2015-03-07 16:50:57 -08:00
* @ 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
* @ return void
*/
function close ()
2009-05-24 15:34:37 +00:00
{
2018-07-20 05:24:55 -05:00
$this -> provide_mySQLaccess ();
2009-09-13 10:29:56 +00:00
e107 :: getSingleton ( 'e107_traffic' ) -> BumpWho ( 'db Close' , 1 );
2020-01-19 14:33:11 +01:00
@ mysqli_close ( $this -> mySQLaccess );
2006-12-02 04:36:16 +00:00
}
2009-05-24 15:34:37 +00:00
2015-03-07 16:50:57 -08:00
/**
* BC Alias of close ()
*/
function db_Close ()
{
$this -> close ();
}
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 :
2012-11-22 12:14:57 +02:00
* < code > $sql -> delete ( " tmp " , " tmp_ip=' $ip ' " ); </ code >< br />
2006-12-02 04:36:16 +00:00
* < br />
* @ access public
*/
2012-11-22 12:14:57 +02:00
function delete ( $table , $arg = '' , $debug = FALSE , $log_type = '' , $log_remark = '' )
2009-05-24 15:34:37 +00:00
{
2020-12-14 16:21:48 -08:00
$table = $this -> hasLanguage ( $table );
2006-12-02 04:36:16 +00:00
$this -> mySQLcurTable = $table ;
2006-12-30 03:07:50 +00:00
2018-07-20 05:24:55 -05:00
$this -> provide_mySQLaccess ();
2006-12-30 03:07:50 +00:00
2009-12-02 16:51:04 +00:00
if ( ! $arg )
2009-05-24 15:34:37 +00:00
{
2009-12-02 16:51:04 +00:00
if ( $result = $this -> mySQLresult = $this -> db_Query ( 'DELETE FROM ' . $this -> mySQLPrefix . $table , NULL , 'db_Delete' , $debug , $log_type , $log_remark ))
2009-05-24 15:34:37 +00:00
{
2019-02-03 19:35:45 +01:00
// return the number of records deleted instead of an object
2020-01-19 13:33:52 +01:00
$this -> mySQLrows = mysqli_affected_rows ( $this -> mySQLaccess );
2009-05-24 15:34:37 +00:00
$this -> dbError ( 'db_Delete' );
2020-01-19 12:30:23 +01:00
return $this -> mySQLrows ;
2009-12-02 16:51:04 +00:00
}
else
2009-05-24 15:34:37 +00:00
{
$this -> dbError ( " db_Delete( { $arg } ) " );
2006-12-02 04:36:16 +00:00
return FALSE ;
}
2009-12-02 16:51:04 +00:00
}
else
2009-05-24 15:34:37 +00:00
{
2009-12-02 16:51:04 +00:00
if ( $result = $this -> mySQLresult = $this -> db_Query ( 'DELETE FROM ' . $this -> mySQLPrefix . $table . ' WHERE ' . $arg , NULL , 'db_Delete' , $debug , $log_type , $log_remark ))
2009-05-24 15:34:37 +00:00
{
2020-01-19 13:33:52 +01:00
$this -> mySQLrows = mysqli_affected_rows ( $this -> mySQLaccess );
2009-05-24 15:34:37 +00:00
$this -> dbError ( 'db_Delete' );
2020-01-19 12:30:23 +01:00
return $this -> mySQLrows ;
2009-12-02 16:51:04 +00:00
}
else
2009-05-24 15:34:37 +00:00
{
2006-12-02 04:36:16 +00:00
$this -> dbError ( 'db_Delete (' . $arg . ')' );
return FALSE ;
}
}
}
2015-02-14 23:34:15 -08:00
/**
* @ deprecated use $sql -> delete ();
*/
2012-11-22 12:14:57 +02:00
function db_Delete ( $table , $arg = '' , $debug = FALSE , $log_type = '' , $log_remark = '' )
{
return $this -> delete ( $table , $arg , $debug , $log_type , $log_remark );
}
2006-12-02 04:36:16 +00:00
2009-05-24 15:34:37 +00:00
2006-12-02 04:36:16 +00:00
/**
2014-11-23 20:01:32 -08:00
* @ deprecated
2006-12-02 04:36:16 +00:00
* @ desc Enter description here ...
* @ access private
*/
2009-12-02 16:51:04 +00:00
function db_Rows ()
2009-05-24 15:34:37 +00:00
{
2014-11-23 20:01:32 -08:00
return $this -> rowCount ();
2020-01-19 12:52:22 +01:00
2006-12-02 04:36:16 +00:00
}
/**
* @ return void
2016-06-02 08:38:39 -07:00
* @ param bool $mode
2006-12-02 04:36:16 +00:00
* @ desc Enter description here ...
* @ access private
*/
2019-02-09 13:07:34 -08:00
public function db_SetErrorReporting ( $mode )
2009-05-24 15:34:37 +00:00
{
2006-12-02 04:36:16 +00:00
$this -> mySQLerror = $mode ;
}
/**
2009-12-01 20:05:54 +00:00
* Function to handle any MySQL query
* @ param string $query - the MySQL query string , where '#' represents the database prefix in front of table names .
2010-01-05 22:00:41 +00:00
* Strongly recommended to enclose all table names in backticks , to minimise the possibility of erroneous substitutions - its
* likely that this will become mandatory at some point
2009-12-01 20:05:54 +00:00
* @ return boolean | integer
* Returns FALSE if there is an error in the query
* Returns TRUE if the query is successful , and it does not return a row count
* Returns the number of rows added / updated / deleted for DELETE , INSERT , REPLACE , or UPDATE
2006-12-02 04:36:16 +00:00
*/
2012-11-22 12:14:57 +02:00
public function gen ( $query , $debug = FALSE , $log_type = '' , $log_remark = '' )
2007-06-15 08:04:07 +00:00
{
2009-12-27 10:52:22 +00:00
global $db_mySQLQueryCount ;
2010-02-19 09:27:43 +00:00
2006-12-02 04:36:16 +00:00
$this -> tabset = FALSE ;
2010-02-19 09:27:43 +00:00
2009-09-05 23:02:23 +00:00
$query .= " " ; // temp fix for failing regex below, when there is no space after the table name;
2009-12-02 16:51:04 +00:00
2007-06-15 08:04:07 +00:00
if ( strpos ( $query , '`#' ) !== FALSE )
2009-12-02 16:51:04 +00:00
{
2011-05-01 16:35:57 +00:00
//$query = str_replace('`#','`'.$this->mySQLPrefix,$query); // This simple substitution should be OK when backticks used
// SecretR - reverted back - breaks multi-language
$query = preg_replace_callback ( " / \ s`#([ \ w]*?)` \ W/ " , array ( $this , 'ml_check' ), $query );
2007-06-15 08:04:07 +00:00
}
elseif ( strpos ( $query , '#' ) !== FALSE )
2010-01-05 22:00:41 +00:00
{ // Deprecated scenario - caused problems when '#' appeared in data - hence use of backticks
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
}
2009-12-02 16:51:04 +00:00
2010-02-19 09:27:43 +00:00
//$query = str_replace("#",$this->mySQLPrefix,$query); //FIXME - quick fix for those that slip-thru - but destroys
2010-01-05 22:00:41 +00:00
// the point of requiring backticks round table names - wrecks ', for example
2010-01-10 13:48:42 +00:00
if (( $this -> mySQLresult = $this -> db_Query ( $query , NULL , 'db_Select_gen' , $debug , $log_type , $log_remark )) === 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
}
2010-01-10 13:48:42 +00:00
elseif ( $this -> mySQLresult === TRUE )
2009-12-01 20:05:54 +00:00
{ // Successful query which may return a row count (because it operated on a number of rows without returning a result set)
if ( preg_match ( '#^(DELETE|INSERT|REPLACE|UPDATE)#' , $query , $matches ))
2020-01-19 13:33:52 +01:00
{ // Need to check mysqli_affected_rows() - to return number of rows actually updated
$tmp = mysqli_affected_rows ( $this -> mySQLaccess );
2009-12-01 20:05:54 +00:00
$this -> dbError ( 'db_Select_gen' );
return $tmp ;
}
$this -> dbError ( 'db_Select_gen' ); // No row count here
return TRUE ;
}
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' );
2014-11-23 20:01:32 -08:00
return $this -> rowCount ();
2006-12-02 04:36:16 +00:00
}
}
2012-11-22 12:14:57 +02:00
/**
* gen () alias
* @ deprecated
*/
public function db_Select_gen ( $query , $debug = FALSE , $log_type = '' , $log_remark = '' )
{
return $this -> gen ( $query , $debug , $log_type , $log_remark );
}
2007-06-15 08:04:07 +00:00
function ml_check ( $matches )
2009-12-02 16:51:04 +00:00
{
2020-12-14 16:21:48 -08:00
$table = $this -> hasLanguage ( $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 ;
}
2017-03-31 10:51:21 -07:00
return " " . $this -> mySQLPrefix . $table . substr ( $matches [ 0 ], - 1 );
2006-12-02 04:36:16 +00:00
}
/**
2009-12-02 16:51:04 +00:00
* Check for the existence of a matching language table when multi - language tables are active .
2017-09-18 12:20:39 -07:00
* @ param string | array $table Name of table , without the prefix . or an array of table names .
2006-12-02 04:36:16 +00:00
* @ access private
2015-07-06 14:04:40 -07:00
* @ return mixed the name of the language table ( eg . lan_french_news ) or an array of all matching language tables . ( with mprefix )
2006-12-02 04:36:16 +00:00
*/
2020-02-22 10:03:41 -08:00
function hasLanguage ( $table , $multiple = false )
2009-05-24 15:34:37 +00:00
{
2009-12-02 16:51:04 +00:00
//When running a multi-language site with english included. English must be the main site language.
2010-10-28 13:31:54 +00:00
// WARNING!!! FALSE is critical important - if missed, expect dead loop (prefs are calling db handler as well when loading)
// Temporary solution, better one is needed
2011-05-01 16:35:57 +00:00
$core_pref = $this -> getConfig ();
2010-10-28 10:43:35 +00:00
//if ((!$this->mySQLlanguage || !$pref['multilanguage'] || $this->mySQLlanguage=='English') && $multiple==FALSE)
if (( ! $this -> mySQLlanguage || ! $core_pref -> get ( 'multilanguage' ) || ! $core_pref -> get ( 'sitelanguage' ) /*|| $this->mySQLlanguage==$core_pref->get('sitelanguage')*/ ) && $multiple == FALSE )
2009-05-24 15:34:37 +00:00
{
2006-12-02 04:36:16 +00:00
return $table ;
}
2018-07-20 05:24:55 -05:00
$this -> provide_mySQLaccess ();
2009-12-02 16:51:04 +00:00
2009-09-05 18:58:56 +00:00
if ( $multiple == FALSE )
2009-12-02 16:51:04 +00:00
{
2009-09-05 18:58:56 +00:00
$mltable = " lan_ " . strtolower ( $this -> mySQLlanguage . '_' . $table );
2015-07-07 13:02:34 -07:00
return ( $this -> isTable ( $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-12-02 16:51:04 +00:00
{
2009-05-24 15:34:37 +00:00
if ( ! is_array ( $table ))
{
2006-12-02 04:36:16 +00:00
$table = array ( $table );
}
2015-07-06 14:04:40 -07:00
if ( ! $this -> mySQLtableList )
{
$this -> mySQLtableList = $this -> db_mySQLtableList ();
}
$lanlist = array ();
foreach ( $this -> mySQLtableList as $tab )
2009-05-24 15:34:37 +00:00
{
2015-07-06 14:04:40 -07:00
2020-12-20 11:50:10 -08:00
if ( strpos ( $tab , " lan_ " ) === 0 )
2009-05-24 15:34:37 +00:00
{
2015-07-06 14:04:40 -07:00
list ( $tmp , $lng , $tableName ) = explode ( " _ " , $tab , 3 );
2009-05-24 15:34:37 +00:00
foreach ( $table as $t )
{
2015-07-06 14:04:40 -07:00
if ( $tableName == $t )
2009-05-24 15:34:37 +00:00
{
2015-07-06 14:04:40 -07:00
$lanlist [ $lng ][ $this -> mySQLPrefix . $t ] = $this -> mySQLPrefix . $tab ; // prefix needed.
2006-12-02 04:36:16 +00:00
}
2015-07-06 14:04:40 -07:00
2006-12-02 04:36:16 +00:00
}
}
}
2009-12-02 16:51:04 +00:00
2015-07-06 14:04:40 -07:00
if ( empty ( $lanlist ))
{
return false ;
}
else
{
return $lanlist ;
}
2006-12-02 04:36:16 +00:00
}
// -------------------------
2009-09-05 18:58:56 +00:00
2006-12-02 04:36:16 +00:00
}
2013-03-16 03:57:28 -07:00
2020-01-19 12:52:22 +01:00
/**
* Deprecated alias of the rows () function below .
2013-03-16 03:57:28 -07:00
*/
function db_getList ( $fields = 'ALL' , $amount = FALSE , $maximum = FALSE , $ordermode = FALSE )
{
return $this -> rows ( $fields , $amount , $maximum , $ordermode );
}
2020-01-19 12:52:22 +01:00
2006-12-02 04:36:16 +00:00
/**
* @ return array
* @ param string fields to retrieve
* @ desc returns fields as structured array
* @ access public
2019-02-11 15:40:10 -08:00
* @ return array rows of the database as an array .
2006-12-02 04:36:16 +00:00
*/
2013-03-16 03:57:28 -07:00
function rows ( $fields = 'ALL' , $amount = FALSE , $maximum = FALSE , $ordermode = FALSE )
2009-05-24 15:34:37 +00:00
{
2006-12-02 04:36:16 +00:00
$list = array ();
$counter = 1 ;
2013-03-16 03:57:28 -07:00
while ( $row = $this -> fetch ())
2009-05-24 15:34:37 +00:00
{
2009-12-02 16:51:04 +00:00
foreach ( $row as $key => $value )
2009-05-24 15:34:37 +00:00
{
2009-12-02 16:51:04 +00:00
if ( is_string ( $key ))
2009-05-24 15:34:37 +00:00
{
2009-12-02 16:51:04 +00:00
if ( strtoupper ( $fields ) == 'ALL' || in_array ( $key , $fields ))
2009-05-24 15:34:37 +00:00
{
2006-12-02 04:36:16 +00:00
if ( ! $ordermode )
{
$list [ $counter ][ $key ] = $value ;
}
else
{
$list [ $row [ $ordermode ]][ $key ] = $value ;
}
}
}
}
2009-12-02 16:51:04 +00:00
if ( $amount && $amount == $counter || ( $maximum && $counter > $maximum ))
2009-05-24 15:34:37 +00:00
{
2006-12-02 04:36:16 +00:00
break ;
}
$counter ++ ;
}
return $list ;
}
2015-04-15 12:27:31 -07:00
/**
* Return the maximum value for a given table / field
* @ param $table ( without the prefix )
* @ param $field
* @ param string $where ( optional )
* @ return bool | resource
*/
public function max ( $table , $field , $where = '' )
{
$qry = " SELECT MAX( " . $field . " ) FROM ` " . $this -> mySQLPrefix . $table . " ` " ;
if ( ! empty ( $where ))
{
$qry .= " WHERE " . $where ;
}
return $this -> retrieve ( $qry );
}
2017-04-19 16:35:10 -07:00
/**
2017-04-19 17:15:32 -07:00
* Return a sorted list of parent / child tree with an optional where clause .
2017-04-19 16:35:10 -07:00
* @ param string $table Name of table ( without the prefix )
* @ param string $parent Name of the parent field
* @ param string $pid Name of the primary id
* @ param string $where ( Optional ) where condition .
* @ param string $order Name of the order field .
* @ todo Add extra params to each procedure so we only need 2 of them site - wide .
* @ return boolean | integer with the addition of _treesort and _depth fields in the results .
*/
2017-04-19 17:15:32 -07:00
public function selectTree ( $table , $parent , $pid , $order , $where = null )
2017-04-19 16:35:10 -07:00
{
if ( empty ( $table ) || empty ( $parent ) || empty ( $pid ))
{
$this -> mySQLlastErrText = " missing variables in sql->categories() " ;
return false ;
}
$sql = " DROP FUNCTION IF EXISTS `getDepth` ; " ;
$this -> gen ( $sql );
$sql = "
CREATE FUNCTION `getDepth` ( project_id INT ) RETURNS int
BEGIN
DECLARE depth INT ;
SET depth = 1 ;
WHILE project_id > 0 DO
SELECT IFNULL ( " . $parent . " , - 1 )
INTO project_id
FROM ( SELECT " . $parent . " FROM `#".$table."` WHERE " . $pid . " = project_id ) AS t ;
IF project_id > 0 THEN
SET depth = depth + 1 ;
END IF ;
END WHILE ;
RETURN depth ;
END
;
" ;
$this -> gen ( $sql );
$sql = " DROP FUNCTION IF EXISTS `getTreeSort`; " ;
$this -> gen ( $sql );
$sql = "
CREATE FUNCTION getTreeSort ( incid INT )
RETURNS CHAR ( 255 )
BEGIN
SET @ parentstr = CONVERT ( incid , CHAR );
SET @ parent = - 1 ;
label1 : WHILE @ parent != 0 DO
SET @ parent = ( SELECT " . $parent . " FROM `#".$table."` WHERE " . $pid . " = incid );
SET @ order = ( SELECT " . $order . " FROM `#".$table."` WHERE " . $pid . " = incid );
SET @ parentstr = CONCAT ( if ( @ parent = 0 , '' , @ parent ), LPAD ( @ order , 4 , 0 ), @ parentstr );
SET incid = @ parent ;
END WHILE label1 ;
RETURN @ parentstr ;
END
;
" ;
$this -> gen ( $sql );
$qry = " SELECT SQL_CALC_FOUND_ROWS *, getTreeSort( " . $pid . " ) as _treesort, getDepth( " . $pid . " ) as _depth FROM `# " . $table . " ` " ;
if ( $where !== null )
{
$qry .= " WHERE " . $where ;
}
2017-04-19 17:15:32 -07:00
$qry .= " ORDER BY _treesort " ;
2017-04-19 16:35:10 -07:00
return $this -> gen ( $qry );
}
2015-04-15 12:27:31 -07:00
2006-12-02 04:36:16 +00:00
/**
* @ return integer
* @ desc returns total number of queries made so far
* @ access public
*/
2009-12-02 16:51:04 +00:00
function db_QueryCount ()
2009-05-24 15:34:37 +00:00
{
2006-12-02 04:36:16 +00:00
global $db_mySQLQueryCount ;
return $db_mySQLQueryCount ;
}
2015-07-06 14:04:40 -07:00
/**
* Multi - language Query Function . Run a query on the same table across all languages .
* @ param $query
* @ param bool $debug
* @ return bool
*/
function db_Query_all ( $query , $debug = false )
2009-05-24 15:34:37 +00:00
{
2006-12-02 04:36:16 +00:00
$error = " " ;
2015-07-06 14:04:40 -07: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 " ;
}
2015-07-06 14:04:40 -07:00
$table = array ();
$search = array ();
$tmp = explode ( " " , $query ); // split the query
2009-05-24 15:34:37 +00:00
foreach ( $tmp as $val )
{
2015-07-06 14:04:40 -07:00
if ( strpos ( $val , $this -> mySQLPrefix ) !== false ) // search for table names references using the mprefix
2009-05-24 15:34:37 +00:00
{
2015-07-06 14:04:40 -07:00
$table [] = str_replace ( array ( $this -> mySQLPrefix , " ` " ), " " , $val );
$search [] = str_replace ( " ` " , " " , $val );
2006-12-02 04:36:16 +00:00
}
}
2015-07-06 14:04:40 -07:00
if ( empty ( $table ) || empty ( $search ))
{
return false ;
}
2006-12-02 04:36:16 +00:00
// Loop thru relevant language tables and replace each tablename within the query.
2015-07-06 14:04:40 -07:00
2020-12-14 16:21:48 -08:00
if ( $tablist = $this -> hasLanguage ( $table , true ))
2009-05-24 15:34:37 +00:00
{
foreach ( $tablist as $key => $tab )
{
2006-12-02 04:36:16 +00:00
$querylan = $query ;
2015-07-06 14:04:40 -07:00
2009-05-24 15:34:37 +00:00
foreach ( $search as $find )
{
2006-12-02 04:36:16 +00:00
$replace = ( $tab [ $find ] != " " ) ? $tab [ $find ] : $find ;
$querylan = str_replace ( $find , $replace , $querylan );
}
2015-07-06 14:04:40 -07: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 " ;
}
2015-07-06 14:04:40 -07:00
2006-12-02 04:36:16 +00:00
if ( $debug ){ echo " <br />** lang= " . $querylan ; }
}
}
2015-07-06 14:04:40 -07:00
return ( $error ) ? false : true ;
2006-12-02 04:36:16 +00:00
}
2009-05-24 15:34:37 +00:00
2009-12-15 22:34:04 +00:00
/**
* Return a list of the field names in a table .
*
* @ param string $table - table name ( no prefix )
* @ param string $prefix - table prefix to apply . If empty , MPREFIX is used .
* @ param boolean $retinfo = FALSE - just returns array of field names . TRUE - returns all field info
* @ return array | boolean - FALSE on error , field list array on success
*/
function db_FieldList ( $table , $prefix = '' , $retinfo = FALSE )
{
if ( ! $this -> mySQLdefaultdb )
{
global $mySQLdefaultdb ;
$this -> mySQLdefaultdb = $mySQLdefaultdb ;
}
2018-07-20 05:24:55 -05:00
$this -> provide_mySQLaccess ();
2009-12-15 22:34:04 +00:00
if ( $prefix == '' ) $prefix = $this -> mySQLPrefix ;
2019-02-13 15:54:04 -08:00
if ( false === $this -> gen ( 'SHOW COLUMNS FROM ' . $prefix . $table ))
2009-12-15 22:34:04 +00:00
{
2019-02-13 15:54:04 -08:00
return false ; // Error return
2009-12-15 22:34:04 +00:00
}
2020-01-19 12:52:22 +01:00
2009-12-15 22:34:04 +00:00
$ret = array ();
2020-01-19 12:52:22 +01:00
2014-11-23 20:01:32 -08:00
if ( $this -> rowCount () > 0 )
2009-12-15 22:34:04 +00:00
{
2019-02-13 15:54:04 -08:00
while ( $row = $this -> fetch ())
2009-12-15 22:34:04 +00:00
{
2010-02-19 09:27:43 +00:00
if ( $retinfo )
2009-12-15 22:34:04 +00:00
{
$ret [ $row [ 'Field' ]] = $row [ 'Field' ];
}
else
{
2010-02-19 09:27:43 +00:00
$ret [] = $row [ 'Field' ];
2009-12-15 22:34:04 +00:00
}
}
}
2019-02-13 15:54:04 -08:00
2009-12-15 22:34:04 +00:00
return $ret ;
}
2013-03-09 14:53:01 -08:00
function db_Field ( $table , $fieldid = " " , $key = " " , $retinfo = FALSE )
{
2020-01-19 12:52:22 +01:00
return $this -> field ( $table , $fieldid , $key , $retinfo );
2013-03-09 14:53:01 -08:00
}
2013-04-24 17:44:07 -07:00
function columnCount ()
{
2020-01-19 13:33:52 +01:00
return mysqli_num_fields ( $this -> mySQLresult );
2013-04-24 17:44:07 -07:00
}
2009-12-15 22:34:04 +00:00
/**
* Determines if a plugin field ( and key ) exist . OR if fieldid is numeric - return the field name in that position .
*
* @ param string $table - table name ( no prefix )
* @ param string $fieldid - Numeric offset or field / key name
* @ param string $key - PRIMARY | INDEX | UNIQUE - type of key when searching for key name
2016-08-12 16:02:55 -07:00
* @ param boolean $retinfo = FALSE - just returns true | false . TRUE - returns all field info
2009-12-15 22:34:04 +00:00
* @ return array | boolean - FALSE on error , field information on success
*/
2013-03-09 14:53:01 -08:00
function field ( $table , $fieldid = " " , $key = " " , $retinfo = FALSE )
2007-09-22 21:46:23 +00:00
{
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
2018-07-20 05:24:55 -05:00
$this -> provide_mySQLaccess ();
2006-12-30 03:07:50 +00:00
2017-02-06 11:18:36 -08:00
$result = $this -> gen ( " SHOW COLUMNS FROM " . $this -> mySQLPrefix . $table );
2014-11-23 20:01:32 -08:00
if ( $result && ( $this -> rowCount () > 0 ))
2007-09-22 21:46:23 +00:00
{
2009-05-24 15:34:37 +00:00
$c = 0 ;
2014-11-23 20:01:32 -08:00
while ( $row = $this -> fetch ())
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 ;
2016-08-12 16:02:55 -07:00
return true ;
2009-05-24 15:34:37 +00:00
}
}
$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
2018-06-08 22:59:24 +02:00
/**
* Determines if a table index ( key ) exist .
*
* @ param string $table - table name ( no prefix )
* @ param string $keyname - Name of the key to
* @ param array $fields - OPTIONAL list of fieldnames , the index ( key ) must contain
* @ param boolean $retinfo = FALSE - just returns true | false . TRUE - returns all key info
* @ return array | boolean - FALSE on error , key information on success
*/
function index ( $table , $keyname , $fields = null , $retinfo = FALSE )
{
if ( ! $this -> mySQLdefaultdb )
{
global $mySQLdefaultdb ;
$this -> mySQLdefaultdb = $mySQLdefaultdb ;
}
2018-07-20 05:24:55 -05:00
$this -> provide_mySQLaccess ();
2018-06-08 22:59:24 +02:00
if ( ! empty ( $fields ) && ! is_array ( $fields ))
{
$fields = explode ( ',' , str_replace ( ' ' , '' , $fields ));
}
elseif ( empty ( $fields ))
{
$fields = array ();
}
$check_field = count ( $fields ) > 0 ;
$info = array ();
$result = $this -> gen ( " SHOW INDEX FROM " . $this -> mySQLPrefix . $table );
if ( $result && ( $this -> rowCount () > 0 ))
{
$c = 0 ;
while ( $row = $this -> fetch ())
{
// Check for match of key name - and allow that key might not be used
if ( $keyname == $row [ 'Key_name' ])
{
// a key can contain severeal fields which are returned as 1 row per field
if ( ! $check_field )
{ // Check only for keyname
$info [] = $row ;
}
elseif ( $check_field && in_array ( $row [ 'Column_name' ], $fields ))
{ // Check also for fieldnames
$info [] = $row ;
}
$c ++ ;
}
}
if ( count ( $info ) > 0 )
{
// Kex does not consist of all keys
if ( $check_field && $c != count ( $fields )) return false ;
// Return full information
if ( $retinfo ) return $info ;
// Return only if index was found
return true ;
}
}
return FALSE ;
}
2006-12-02 04:36:16 +00:00
/**
2020-01-19 13:33:52 +01:00
* A pointer to mysqli_real_escape_string () - see https :// www . php . net / manual / en / mysqli . real - escape - string . php
2006-12-02 04:36:16 +00:00
*
* @ param string $data
* @ return string
*/
2007-06-26 21:34:34 +00:00
function escape ( $data , $strip = true )
{
2020-12-22 14:48:28 -08:00
/* if ( $strip )
2007-06-26 21:34:34 +00:00
{
2006-12-02 04:36:16 +00:00
$data = strip_if_magic ( $data );
2020-12-22 14:48:28 -08:00
} */
2006-12-30 03:07:50 +00:00
2018-07-20 05:24:55 -05:00
$this -> provide_mySQLaccess ();
2006-12-30 03:07:50 +00:00
2020-01-19 13:33:52 +01:00
return mysqli_real_escape_string ( $this -> mySQLaccess , $data );
2006-12-02 04:36:16 +00:00
}
2007-03-04 15:00:19 +00:00
2009-05-24 15:34:37 +00:00
2015-07-07 13:02:34 -07:00
/**
* Legacy Alias of isTable ();
* @ deprecated
* @ param $table
* @ param string $language
* @ return bool
*/
public function db_Table_exists ( $table , $language = '' )
{
return $this -> isTable ( $table , $language );
}
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
2015-07-07 13:02:34 -07:00
* @ param string $language ( optional ) leave blank to search for a regular table , or language - name to search for a language table .
* @ example $sql -> isTable ( 'news' , 'Spanish' );
2009-09-05 18:58:56 +00:00
* @ return boolean TRUE if exists
2007-03-04 15:00:19 +00:00
*
2009-12-02 16:51:04 +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
*/
2015-07-07 13:02:34 -07:00
public function isTable ( $table , $language = '' )
2009-05-24 15:34:37 +00:00
{
2015-07-07 13:02:34 -07:00
// global $pref;
2009-09-05 18:58:56 +00:00
$table = strtolower ( $table ); // precaution for multilanguage
2015-07-07 13:02:34 -07:00
if ( ! empty ( $language )) //ie. is it a language table?
2009-09-05 12:48:28 +00:00
{
2015-07-07 13:02:34 -07:00
$sitelanguage = $this -> getConfig () -> get ( 'sitelanguage' );
if ( $language == $sitelanguage )
{
return false ;
}
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 );
2009-12-02 16:51:04 +00:00
}
2015-07-07 13:02:34 -07:00
2009-12-02 16:51:04 +00:00
return in_array ( 'lan_' . strtolower ( $language ) . " _ " . $table , $this -> mySQLtableListLanguage [ $language ]);
2009-09-05 18:58:56 +00:00
}
2015-07-07 13:02:34 -07:00
else // regular search
2009-09-05 18:58:56 +00:00
{
if ( ! $this -> mySQLtableList )
{
2009-12-02 16:51:04 +00:00
$this -> mySQLtableList = $this -> db_mySQLtableList ();
}
2015-07-07 13:02:34 -07:00
2009-09-05 18:58:56 +00:00
return in_array ( $table , $this -> mySQLtableList );
}
2009-12-02 16:51:04 +00:00
2009-09-05 18:58:56 +00:00
}
2015-08-24 17:39:28 -07:00
/**
* Check if a database table is empty or not .
* @ param $table
* @ return bool
*/
2019-02-11 15:40:10 -08:00
function isEmpty ( $table = null )
2015-08-24 17:39:28 -07:00
{
if ( empty ( $table ))
{
return false ;
}
2019-01-05 14:37:12 -08:00
$result = $this -> gen ( " SELECT NULL FROM " . $this -> mySQLPrefix . $table . " LIMIT 1 " );
2015-08-24 17:39:28 -07:00
if ( $result === 0 )
{
return true ;
}
return false ;
}
2015-07-07 13:02:34 -07:00
2009-09-05 18:58:56 +00:00
/**
2011-05-01 16:35:57 +00:00
* Populate mySQLtableList and mySQLtableListLanguage
* TODO - better runtime cache - use e107 :: getRegistry () && e107 :: setRegistry ()
2009-09-05 18:58:56 +00:00
* @ return array
*/
private function db_mySQLtableList ( $language = '' )
{
2019-02-11 15:40:10 -08:00
2019-05-28 10:49:32 -07:00
$database = ! empty ( $this -> mySQLdefaultdb ) ? " FROM ` " . $this -> mySQLdefaultdb . " ` " : " " ;
2019-02-11 15:40:10 -08:00
$prefix = $this -> mySQLPrefix ;
if ( strpos ( $prefix , " . " ) !== false ) // eg. `my_database`.$prefix
{
$tmp = explode ( " . " , $prefix );
$prefix = $tmp [ 1 ];
}
2009-09-05 18:58:56 +00:00
if ( $language )
{
if ( ! isset ( $this -> mySQLtableListLanguage [ $language ]))
{
$table = array ();
2019-02-11 15:40:10 -08:00
if ( $res = $this -> db_Query ( " SHOW TABLES " . $database . " LIKE ' " . $prefix . " lan_ " . strtolower ( $language ) . " %' " ))
2009-12-02 16:51:04 +00:00
{
2016-02-14 12:15:55 -08:00
while ( $rows = $this -> fetch ( 'num' ))
2009-09-05 18:58:56 +00:00
{
2019-02-11 15:40:10 -08:00
$table [] = str_replace ( $prefix , " " , $rows [ 0 ]);
2009-09-05 18:58:56 +00:00
}
}
2021-01-24 17:00:02 -08:00
return array ( $language => $table );
2009-09-05 18:58:56 +00:00
}
else
{
2009-12-02 16:51:04 +00:00
return $this -> mySQLtableListLanguage [ $language ];
}
}
2009-09-05 18:58:56 +00:00
if ( ! $this -> mySQLtableList )
{
$table = array ();
2015-11-26 12:48:16 -08:00
2019-02-11 15:40:10 -08:00
if ( $res = $this -> db_Query ( " SHOW TABLES " . $database . " LIKE ' " . $prefix . " %' " ))
2009-12-02 16:51:04 +00:00
{
2019-02-11 15:40:10 -08:00
$length = strlen ( $prefix );
2016-02-14 12:15:55 -08:00
while ( $rows = $this -> fetch ( 'num' ))
2009-09-05 18:58:56 +00:00
{
2015-11-26 12:48:16 -08:00
$table [] = substr ( $rows [ 0 ], $length );
2009-09-05 18:58:56 +00:00
}
}
2009-12-02 16:51:04 +00:00
return $table ;
2009-09-05 18:58:56 +00:00
}
else
{
return $this -> mySQLtableList ;
}
}
2009-12-02 16:51:04 +00:00
2009-09-05 18:58:56 +00:00
public function db_ResetTableList ()
{
$this -> mySQLtableList = array ();
$this -> mySQLtableListLanguage = array ();
}
2019-12-23 17:12:47 +01:00
/**
* @ inheritDoc
*/
public function resetTableList ()
{
2020-12-17 13:13:29 -08:00
$this -> db_ResetTableList ();
2019-12-23 17:12:47 +01:00
}
2009-09-05 18:58:56 +00:00
/**
2015-07-06 14:04:40 -07:00
* Legacy Alias of tables
2020-12-14 16:21:48 -08:00
* @ deprecated Use $sql -> tables ( $mode ) instead .
2015-07-06 14:04:40 -07:00
* @ param string $mode
2009-12-02 16:51:04 +00:00
* @ return array
2009-09-05 18:58:56 +00:00
*/
public function db_TableList ( $mode = 'all' )
2015-07-06 14:04:40 -07:00
{
return $this -> tables ( $mode );
}
/**
* Return a filtered list of DB tables .
* @ param object $mode [ optional ] all | lan | nolan | nologs
* @ return array
*/
public function tables ( $mode = 'all' )
2009-09-05 18:58:56 +00:00
{
2009-12-02 16:51:04 +00:00
2009-09-05 18:58:56 +00:00
if ( ! $this -> mySQLtableList )
{
$this -> mySQLtableList = $this -> db_mySQLtableList ();
}
2020-01-19 12:52:22 +01:00
2014-01-25 15:56:20 -08:00
if ( $mode == 'nologs' )
{
$ret = array ();
foreach ( $this -> mySQLtableList as $table )
{
if ( substr ( $table , - 4 ) != '_log' && $table != 'download_requests' )
{
2020-01-19 12:52:22 +01:00
$ret [] = $table ;
}
2014-01-25 15:56:20 -08:00
}
2020-01-19 12:52:22 +01:00
2014-01-25 15:56:20 -08:00
return $ret ;
}
2020-01-19 12:52:22 +01:00
2009-12-02 16:51:04 +00:00
2009-09-05 18:58:56 +00:00
if ( $mode == 'all' )
{
return $this -> mySQLtableList ;
2009-09-05 12:48:28 +00:00
}
2009-12-02 16:51:04 +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 ();
2009-12-02 16:51:04 +00:00
2009-09-05 18:58:56 +00:00
foreach ( $this -> mySQLtableList as $tab )
{
2020-12-20 11:50:10 -08:00
if ( strpos ( $tab , 'lan_' ) === 0 )
2009-09-05 18:58:56 +00:00
{
2020-12-20 11:50:10 -08:00
$lan [] = $tab ;
2009-09-05 18:58:56 +00:00
}
else
{
2020-12-20 11:50:10 -08:00
$nolan [] = $tab ;
2009-12-02 16:51:04 +00:00
}
2009-09-05 18:58:56 +00:00
}
2009-12-02 16:51:04 +00:00
2009-09-05 18:58:56 +00:00
return ( $mode == 'lan' ) ? $lan : $nolan ;
}
}
2015-07-06 14:04:40 -07:00
2011-07-25 02:41:49 +00:00
/**
2020-01-19 12:52:22 +01:00
* Duplicate a Table Row in a table .
2011-07-25 02:41:49 +00:00
*/
function db_CopyRow ( $table , $fields = '*' , $args = '' )
{
if ( ! $table || ! $args )
{
2012-02-07 16:37:44 +00:00
return false ;
2011-07-25 02:41:49 +00:00
}
2020-01-19 12:52:22 +01:00
2019-02-11 15:40:10 -08:00
if ( $fields === '*' )
2011-07-25 02:41:49 +00:00
{
2020-01-19 12:52:22 +01:00
$fields = $this -> db_FieldList ( $table );
2011-07-25 02:41:49 +00:00
unset ( $fields [ 0 ]); // Remove primary_id.
$fieldList = implode ( " , " , $fields );
}
else
{
$fieldList = $fields ;
}
2020-01-19 12:52:22 +01:00
2019-02-11 15:40:10 -08:00
if ( empty ( $fields ))
{
$this -> mysqlLastErrText = " copyRow \$ fields list was empty " ;
return false ;
}
2019-01-05 14:37:12 -08:00
$id = $this -> gen ( " INSERT INTO " . $this -> mySQLPrefix . $table . " ( " . $fieldList . " ) SELECT " . $fieldList . " FROM " . $this -> mySQLPrefix . $table . " WHERE " . $args );
2015-03-01 12:43:02 -08:00
$lastInsertId = $this -> lastInsertId ();
return ( $id && $lastInsertId ) ? $lastInsertId : false ;
2014-11-23 20:01:32 -08:00
2011-07-25 02:41:49 +00:00
}
2020-01-19 12:52:22 +01:00
2009-12-02 16:51:04 +00:00
2009-09-05 18:58:56 +00:00
function db_CopyTable ( $oldtable , $newtable , $drop = FALSE , $data = FALSE )
{
$old = $this -> mySQLPrefix . strtolower ( $oldtable );
$new = $this -> mySQLPrefix . strtolower ( $newtable );
2009-12-02 16:51:04 +00:00
2009-09-05 18:58:56 +00:00
if ( $drop )
{
2014-11-23 20:01:32 -08:00
$this -> gen ( " DROP TABLE IF EXISTS { $new } " );
2009-09-05 18:58:56 +00:00
}
2009-12-02 16:51:04 +00:00
2009-09-05 18:58:56 +00:00
//Get $old table structure
2014-11-23 20:01:32 -08:00
$this -> gen ( 'SET SQL_QUOTE_SHOW_CREATE = 1' );
2009-12-02 16:51:04 +00:00
2009-09-05 18:58:56 +00:00
$qry = " SHOW CREATE TABLE { $old } " ;
2014-11-23 20:01:32 -08:00
if ( $this -> gen ( $qry ))
2009-09-05 18:58:56 +00:00
{
2016-02-14 12:15:55 -08:00
$row = $this -> fetch ( 'num' );
2009-09-05 18:58:56 +00:00
$qry = $row [ 1 ];
// $qry = str_replace($old, $new, $qry);
2020-12-18 13:07:45 -08:00
$qry = preg_replace ( " #CREATE \ sTABLE \ s`? " . $old . " `? \ s# " , " CREATE TABLE { $new } " , $qry , 1 ); // More selective search
2009-09-05 12:48:28 +00:00
}
else
{
2009-12-02 16:51:04 +00:00
return FALSE ;
2009-09-05 18:58:56 +00:00
}
2009-12-02 16:51:04 +00:00
2015-07-07 13:02:34 -07:00
if ( ! $this -> isTable ( $newtable ))
2009-09-05 18:58:56 +00:00
{
$result = $this -> db_Query ( $qry );
}
2009-12-02 16:51:04 +00:00
2009-09-05 18:58:56 +00:00
if ( $data ) //We need to copy the data too
{
$qry = " INSERT INTO { $new } SELECT * FROM { $old } " ;
2014-11-23 20:01:32 -08:00
$result = $this -> gen ( $qry );
2009-09-05 18:58:56 +00:00
}
return $result ;
2007-03-04 15:00:19 +00:00
}
2019-12-23 16:37:48 +01:00
/**
* @ inheritDoc
*/
public function copyTable ( $oldtable , $newtable , $drop = false , $data = false ) {
return $this -> db_CopyTable ( $oldtable , $newtable , $drop , $data );
}
2007-03-04 15:00:19 +00:00
2009-09-05 18:58:56 +00:00
2013-03-04 14:17:07 -08:00
/**
2020-01-19 12:52:22 +01:00
* Dump MySQL Table ( s ) to a file in the Backup folder .
2013-03-04 14:17:07 -08:00
* @ param $table string - name without the prefix or '*' for all
2020-01-19 12:52:22 +01:00
* @ param $file string - optional file name . or leave blank to generate .
* @ param $options - additional preferences .
2019-01-28 17:53:45 -08:00
* @ return string | bool backup file path .
2013-03-04 14:17:07 -08:00
*/
function backup ( $table = '*' , $file = '' , $options = null )
{
2020-01-19 12:52:22 +01:00
$this -> mysqlLastErrText = " PDO is required to use the mysql backup() method " ;
return false ;
2013-03-04 14:17:07 -08:00
}
2009-05-24 15:34:37 +00:00
/**
2019-02-11 15:40:10 -08:00
* @ return string relating to error ( empty string if no error )
2016-03-20 18:31:11 -07:00
* @ param string $from
2009-05-24 15:34:37 +00:00
* @ desc Calling method from within this class
* @ access private
*/
2009-12-02 16:51:04 +00:00
function dbError ( $from )
2009-05-24 15:34:37 +00:00
{
2020-01-19 13:33:52 +01:00
$this -> mySQLlastErrNum = mysqli_errno ( $this -> mySQLaccess );
2009-05-24 15:34:37 +00:00
$this -> mySQLlastErrText = '' ;
if ( $this -> mySQLlastErrNum == 0 )
{
return '' ;
}
2020-01-19 13:33:52 +01:00
$this -> mySQLlastErrText = mysqli_error ( $this -> mySQLaccess ); // Get the error text.
2009-12-02 16:51:04 +00:00
if ( $this -> mySQLerror == TRUE )
2009-05-24 15:34:37 +00:00
{
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)
}
2011-05-01 16:35:57 +00:00
2010-04-28 15:44:46 +00:00
function resetLastError ()
{
$this -> mySQLlastErrNum = 0 ;
$this -> mySQLlastErrText = '' ;
}
2010-02-19 09:27:43 +00:00
2009-12-27 10:52:22 +00:00
function getLastQuery ()
{
return $this -> mySQLlastQuery ;
}
2009-07-17 14:20:26 +00:00
2017-01-11 13:30:57 -08:00
private function setSQLMode ()
{
$this -> db_Query ( " SET SESSION sql_mode='NO_ENGINE_SUBSTITUTION'; " );
}
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
2009-12-25 10:25:12 +00:00
* @ param string MySQL charset may be forced in special circumstances
2009-07-17 14:20:26 +00:00
* 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 ;
2013-04-24 17:44:07 -07:00
if ( isset ( $mySQLcharset ) && $mySQLcharset != 'utf8' )
2009-07-17 14:20:26 +00:00
{
// Only utf8 is accepted
$mySQLcharset = '' ;
}
$charset = ( $charset ? $charset : $mySQLcharset );
$message = (( ! $charset && $debug ) ? 'Empty charset!' : '' );
if ( $charset )
{
if ( ! $debug )
{
2020-01-19 13:33:52 +01:00
@ mysqli_query ( $this -> mySQLaccess , " SET NAMES ` $charset ` " );
2009-07-17 14:20:26 +00:00
}
else
{
// Check if MySQL version is utf8 compatible
2010-01-07 21:05:24 +00:00
preg_match ( '/^(.*?)($|-)/' , $this -> mySqlServerInfo , $mysql_version );
2009-07-17 14:20:26 +00:00
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 ;
}
2009-12-25 10:25:12 +00:00
/**
* Get the _FIELD_DEFS and _NOTNULL definitions for a table
2010-01-05 22:00:41 +00:00
*< code >
2009-12-25 10:25:12 +00:00
* The information is sought in a specific order :
* a ) In our internal cache
2010-03-03 17:28:26 +00:00
* b ) in the directory e_CACHE_DBDIR - file name $tableName . php
2009-12-25 10:25:12 +00:00
* c ) An override file for a core or plugin - related table . If found , the information is copied to the cache directory
* For core overrides , e_ADMIN . 'core_sql/db_field_defs.php' is searched
* For plugins , $pref [ 'e_sql_list' ] is used as a search list - any file 'db_field_defs.php' in the plugin directory is earched
* d ) The table structure is read from the DB , and a definition created :
* AUTOINCREMENT fields - ignored ( or integer )
* integer type fields - 'int' processing
* character / string type fields - todb processing
* fields which are 'NOT NULL' but have no default are added to the '_NOTNULL' list
2010-01-05 22:00:41 +00:00
*</ code >
2009-12-25 10:25:12 +00:00
* @ param string $tableName - table name , without any prefixes ( language or general )
* @ return boolean | array - FALSE if not found / not to be used . Array of field names and processing types and null overrides if found
*/
public function getFieldDefs ( $tableName )
{
if ( ! isset ( $this -> dbFieldDefs [ $tableName ]))
{
2010-03-03 17:28:26 +00:00
if ( is_readable ( e_CACHE_DB . $tableName . '.php' ))
2009-12-25 10:25:12 +00:00
{
2021-09-04 15:06:19 +02:00
$temp = file_get_contents ( e_CACHE_DB . $tableName . '.php' );
2009-12-25 10:25:12 +00:00
if ( $temp !== FALSE )
{
2015-02-14 23:34:15 -08:00
$typeDefs = e107 :: unserialize ( $temp );
2009-12-25 10:25:12 +00:00
unset ( $temp );
$this -> dbFieldDefs [ $tableName ] = $typeDefs ;
}
}
else
{ // Need to try and find a table definition
2013-01-23 13:38:21 -08:00
$searchArray = array ( e_CORE . 'sql/db_field_defs.php' );
2010-10-28 13:31:54 +00:00
// e107::getPref() shouldn't be used inside db handler! See db_IsLang() comments
$sqlFiles = ( array ) $this -> getConfig () -> get ( 'e_sql_list' , array ()); // kill any PHP notices
2009-12-25 10:25:12 +00:00
foreach ( $sqlFiles as $p => $f )
{
$searchArray [] = e_PLUGIN . $p . '/db_field_defs.php' ;
}
unset ( $sqlFiles );
$found = FALSE ;
foreach ( $searchArray as $defFile )
{
//echo "Check: {$defFile}, {$tableName}<br />";
if ( $this -> loadTableDef ( $defFile , $tableName ))
{
$found = TRUE ;
break ;
}
}
if ( ! $found )
{ // Need to read table structure from DB and create the file
$this -> makeTableDef ( $tableName );
}
}
}
return $this -> dbFieldDefs [ $tableName ];
}
2010-02-19 09:27:43 +00:00
2016-02-11 20:57:30 -08:00
/**
2009-12-25 10:25:12 +00:00
* Search the specified file for a field type definition of the specified table .
2010-03-03 17:28:26 +00:00
* If found , generate and save a cache file in the e_CACHE_DB directory ,
2009-12-25 10:25:12 +00:00
* Always also update $this -> dbFieldDefs [ $tableName ] - FALSE if not found , data if found
* @ param string $defFile - file name , including path
* @ param string $tableName - name of table sought
* @ return boolean TRUE on success , FALSE on not found ( some errors intentionally ignored )
*/
protected function loadTableDef ( $defFile , $tableName )
{
2017-04-19 09:49:15 -07:00
$result = false ;
2011-12-27 12:00:55 +00:00
if ( is_readable ( $defFile ))
{
// Read the file using the array handler routines
// File structure is a nested array - first level is table name, second level is either FALSE (for do nothing) or array(_FIELD_DEFS => array(), _NOTNULL => array())
$temp = file_get_contents ( $defFile );
// Strip any comments (only /*...*/ supported)
$temp = preg_replace ( " # \ / \ *.*? \ * \ /#mis " , '' , $temp );
//echo "Check: {$defFile}, {$tableName}<br />";
2017-04-19 09:49:15 -07:00
if ( $temp !== false )
2009-12-25 10:25:12 +00:00
{
2017-04-19 09:49:15 -07:00
// $array = e107::getArrayStorage();
$typeDefs = e107 :: unserialize ( $temp );
2011-12-27 12:00:55 +00:00
unset ( $temp );
if ( isset ( $typeDefs [ $tableName ]))
{
$this -> dbFieldDefs [ $tableName ] = $typeDefs [ $tableName ];
2017-04-19 09:49:15 -07:00
$fileData = e107 :: serialize ( $typeDefs [ $tableName ], false );
if ( false === file_put_contents ( e_CACHE_DB . $tableName . '.php' , $fileData ))
2011-12-27 12:00:55 +00:00
{ // Could do something with error - but mustn't return FALSE - would trigger auto-generated structure
2020-12-18 19:55:12 -08:00
$result = false ;
2011-12-27 12:00:55 +00:00
}
2017-04-19 09:49:15 -07:00
$result = true ;
2009-12-25 10:25:12 +00:00
}
}
}
2010-02-19 09:27:43 +00:00
2009-12-25 10:25:12 +00:00
if ( ! $result )
{
2017-04-19 09:49:15 -07:00
$this -> dbFieldDefs [ $tableName ] = false ;
2009-12-25 10:25:12 +00:00
}
return $result ;
}
/**
* Creates a field type definition from the structure of the table in the DB
2010-03-03 17:28:26 +00:00
* Generate and save a cache file in the e_CACHE_DB directory ,
2009-12-25 10:25:12 +00:00
* Also update $this -> dbFieldDefs [ $tableName ] - FALSE if error , data if found
* @ param string $tableName - name of table sought
* @ return boolean TRUE on success , FALSE on not found ( some errors intentionally ignored )
*/
protected function makeTableDef ( $tableName )
{
require_once ( e_HANDLER . 'db_table_admin_class.php' );
$dbAdm = new db_table_admin ();
$baseStruct = $dbAdm -> get_current_table ( $tableName );
2020-01-18 18:52:20 +01:00
$baseStruct = isset ( $baseStruct [ 0 ][ 2 ]) ? $baseStruct [ 0 ][ 2 ] : null ;
$fieldDefs = $dbAdm -> parse_field_defs ( $baseStruct ); // Required definitions
2020-01-17 17:24:06 +01:00
if ( ! $fieldDefs ) return false ;
2017-04-19 09:49:15 -07:00
2020-11-29 14:05:14 -08:00
$outDefs = $dbAdm -> make_field_types ( $fieldDefs );
2017-04-19 09:49:15 -07:00
2009-12-25 10:25:12 +00:00
$this -> dbFieldDefs [ $tableName ] = $outDefs ;
2017-04-19 09:49:15 -07:00
$toSave = e107 :: serialize ( $outDefs , false ); // 2nd parameter to TRUE if needs to be written to DB
2010-03-03 17:28:26 +00:00
if ( FALSE === file_put_contents ( e_CACHE_DB . $tableName . '.php' , $toSave ))
2009-12-25 10:25:12 +00:00
{ // Could do something with error - but mustn't return FALSE - would trigger auto-generated structure
2012-12-07 03:38:21 -08:00
$mes = e107 :: getMessage ();
2020-01-19 12:52:22 +01:00
$mes -> addDebug ( " Error writing file: " . e_CACHE_DB . $tableName . '.php' ); //Fix for during v1.x -> 2.x upgrade.
2012-12-07 03:38:21 -08:00
// echo "Error writing file: ".e_CACHE_DB.$tableName.'.php'.'<br />';
2009-12-25 10:25:12 +00:00
}
2016-04-13 16:43:19 -07:00
2009-12-25 10:25:12 +00:00
}
2018-07-20 05:24:55 -05:00
/**
* In case e_db_mysql :: $mySQLaccess is not set , set it .
*
* Uses the global variable $db_ConnectionID if available .
*
* When the global variable has been unset like in https :// github . com / e107inc / e107 - test / issues / 6 ,
* use the " mySQLaccess " from the default e_db_mysql instance singleton .
*/
private function provide_mySQLaccess ()
{
if ( ! $this -> mySQLaccess ) {
global $db_ConnectionID ;
$this -> mySQLaccess = $db_ConnectionID ;
}
if ( ! $this -> mySQLaccess ) {
$this -> mySQLaccess = e107 :: getDb () -> get_mySQLaccess ();
}
}
/**
* @ deprecated 2.1 . 9 Used only to provide $mySQLaccess to other instances of e_db_mysql scattered around
2020-01-19 12:52:22 +01:00
* @ return resource
2018-07-20 05:24:55 -05:00
*/
public function get_mySQLaccess ()
{
return $this -> mySQLaccess ;
}
2019-12-23 16:37:48 +01:00
/**
* @ inheritDoc
*/
public function setLanguage ( $lang )
{
$this -> mySQLlanguage = $lang ;
}
/**
* @ inheritDoc
*/
public function getLanguage ()
{
return $this -> mySQLlanguage ;
}
/**
* @ inheritDoc
*/
public function dropTable ( $table )
{
$name = $this -> mySQLPrefix . strtolower ( $table );
return $this -> gen ( " DROP TABLE IF EXISTS " . $name );
}
2006-12-02 04:36:16 +00:00
}
2009-09-10 12:06:39 +00:00
/**
2018-07-20 05:24:55 -05:00
* Backwards compatibility
2019-06-05 21:59:03 -07:00
*/
2019-06-06 07:06:55 -07:00
if ( ! class_exists ( 'db' ))
2009-09-10 12:06:39 +00:00
{
2019-06-06 07:06:55 -07:00
class db extends e_db_mysql
{
2009-12-02 16:51:04 +00:00
2019-06-06 07:06:55 -07:00
}
2019-06-05 21:59:03 -07:00
}