| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505 |
- <?php defined('BX_DOL') or defined('BX_DOL_INSTALL') or die('hack attempt');
- /**
- * Copyright (c) UNA, Inc - https://una.io
- * MIT License - https://opensource.org/licenses/MIT
- *
- * @defgroup UnaCore UNA Core
- * @{
- */
- define('BX_DB_MODE_SILENT', PDO::ERRMODE_SILENT);
- define('BX_DB_MODE_EXCEPTION', PDO::ERRMODE_EXCEPTION);
- define('BX_DB_ERR_CONNECT_FAILD', 1);
- define('BX_DB_ERR_QUERY_ERROR', 2);
- define('BX_DB_ERR_ESCAPE', 3);
- define('BX_PDO_STATE_NOT_EXECUTED', NULL);
- define('BX_PDO_STATE_SUCCESS', '00000');
- class BxDolDb extends BxDolFactory implements iBxDolSingleton
- {
- protected static $_rLink;
- protected static $_iQueriesCounter = 0;
- protected static $_aDbCacheData = [];
- protected static $_aParams;
- protected static $_sParamsCacheName = 'sys_options';
- protected static $_sParamsCacheNameMixed = 'sys_options_mixed_';
- protected static $_sParamsCacheNameMix = 'sys_options_mix_';
- protected static $_sErrorKey = 'bx_db_error';
- protected static $_aErrors = array(
- BX_DB_ERR_CONNECT_FAILD => 'Database connect failed',
- BX_DB_ERR_QUERY_ERROR => 'Database query error',
- BX_DB_ERR_ESCAPE => 'Escape string error'
- );
- protected static $_bReadOnlyMode = true;
- protected static $_bMultuServersMode = false;
- protected $_bPdoPersistent;
- protected $_iPdoFetchType;
- protected $_iPdoErrorMode;
- protected $_bErrorChecking;
- protected $_aError;
- protected $_sHost, $_sPort, $_sSocket, $_sDbname, $_sUser, $_sPassword, $_sCharset, $_sStorageEngine;
- protected $_oStatement = null;
- protected $_oDbCacheObject = null;
- /**
- * set database parameters and connect to it
- */
- protected function __construct($aDbConf = false)
- {
- if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
- trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
- parent::__construct();
- $this->_bPdoPersistent = true;
- if (defined('BX_DATABASE_PERSISTENT'))
- $this->_bPdoPersistent = (bool)BX_DATABASE_PERSISTENT;
- $this->_iPdoFetchType = PDO::FETCH_ASSOC;
- $this->_iPdoErrorMode = BX_DB_MODE_EXCEPTION;
- $this->_bErrorChecking = true;
- $this->_aError = array();
- $this->_sStorageEngine = 'MYISAM';
- if (defined('BX_DATABASE_ENGINE'))
- $this->_sStorageEngine = BX_DATABASE_ENGINE;
- $this->_sCharset = 'utf8mb4';
- if($aDbConf === false) {
- $this->_sHost = BX_DATABASE_HOST;
- $this->_sPort = BX_DATABASE_PORT;
- $this->_sSocket = BX_DATABASE_SOCK;
- $this->_sDbname = BX_DATABASE_NAME;
- $this->_sUser = BX_DATABASE_USER;
- $this->_sPassword = BX_DATABASE_PASS;
- }
- else {
- $this->_sHost = $aDbConf['host'];
- $this->_sPort = $aDbConf['port'];
- $this->_sSocket = $aDbConf['sock'];
- $this->_sDbname = $aDbConf['name'];
- $this->_sUser = $aDbConf['user'];
- $this->_sPassword = $aDbConf['pwd'];
- if(isset($aDbConf['charset']))
- $this->_sCharset = $aDbConf['charset'];
- if(isset($aDbConf['error_checking']))
- $this->_bErrorChecking = $aDbConf['error_checking'];
- if(isset($aDbConf['storage_engine']))
- $this->_sStorageEngine = $aDbConf['storage_engine'];
- }
- @set_exception_handler(array($this, 'pdoExceptionHandler'));
- }
- /**
- * Prevent cloning the instance
- */
- public function __clone()
- {
- if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
- trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
- }
- /**
- * Get singleton instance of the class
- */
- public static function getInstance()
- {
- $null = null;
- return self::getInstanceWithConf(false, $null);
- }
-
- public static function getInstanceWithConf($aDbConf, &$sError)
- {
- if(!isset($GLOBALS['bxDolClasses'][__CLASS__])) {
- if($aDbConf === false && !defined('BX_DATABASE_HOST'))
- return null;
- $o = new BxDolDb($aDbConf);
- $sErrorMessage = $o->connect();
- if($sErrorMessage) {
- if($sError !== null)
- $sError = $sErrorMessage;
- return null;
- }
- $GLOBALS['bxDolClasses'][__CLASS__] = $o;
- }
- return $GLOBALS['bxDolClasses'][__CLASS__];
- }
- public static function getLink()
- {
- return self::$_rLink;
- }
- public static function getQueriesCounter()
- {
- return self::$_iQueriesCounter;
- }
- public function setReadOnlyMode($b)
- {
- if ($b == self::$_bReadOnlyMode)
- return;
- self::$_bReadOnlyMode = $b;
- if (self::$_bMultuServersMode) {
- $this->disconnect();
- $this->connect();
- }
- }
- protected function balancer()
- {
- self::$_bMultuServersMode = false;
- // regular mode - one DB server
- if (!is_array($this->_sHost))
- return array($this->_sHost, $this->_sPort, $this->_sSocket, $this->_sUser, $this->_sPassword, $this->_sDbname);
- $iServersNum = count($this->_sHost);
- if ($iServersNum > 1)
- self::$_bMultuServersMode = true;
- // in read/write mode always use first server, which should be always master (also check for regular mode but with array with 1 item)
- if (!self::$_bReadOnlyMode || 1 == $iServersNum)
- $i = 0;
- // in case of 2 servers - always use second server, in read only mode
- elseif (2 == $iServersNum)
- $i = 1;
- // when there are more that 2 servers, randomly select read only server
- else
- $i = rand(1, $iServersNum-1);
-
- return array($this->_sHost[$i], $this->_sPort[$i], $this->_sSocket[$i], $this->_sUser[$i], $this->_sPassword[$i], $this->_sDbname[$i]);
- }
- /**
- * connect to database with appointed parameters
- */
- public function connect()
- {
- if(self::$_rLink)
- return;
- list ($sHost, $sPort, $sSocket, $sUser, $sPassword, $sDBName) = $this->balancer();
- try {
- $sDsn = "mysql:host=" . $sHost . ";";
- $sDsn .= $sPort ? "port=" . $sPort . ";" : "";
- $sDsn .= $sSocket ? "unix_socket=" . $sSocket . ";" : "";
- $sDsn .= "dbname=" . $sDBName . ";charset=" . $this->_sCharset;
- self::$_rLink = new PDO($sDsn, $sUser, $sPassword, array(
- PDO::ATTR_ERRMODE => $this->_iPdoErrorMode,
- PDO::ATTR_DEFAULT_FETCH_MODE => $this->_iPdoFetchType,
- PDO::ATTR_PERSISTENT => $this->_bPdoPersistent
- ));
- $this->pdoExec("SET NAMES 'utf8mb4' COLLATE '" . (defined('BX_DATABASE_COLLATE') ? BX_DATABASE_COLLATE : 'utf8mb4_unicode_ci') . "'");
- $this->pdoExec("SET sql_mode = ''");
- if (0 == strcasecmp($this->_sStorageEngine, 'INNODB'))
- $this->pdoExec("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE");
- $sVer = $this->getVersion();
- $sStorageEngine = !$sVer || version_compare($sVer, '5.7.5', '>=') ? 'default_storage_engine' : 'storage_engine';
- $this->pdoExec("SET $sStorageEngine=" . $this->_sStorageEngine);
- $this->cleanMemoryAll();
- }
- catch (PDOException $oException) {
- $oException->errorInfo[self::$_sErrorKey] = array(
- 'code' => BX_DB_ERR_CONNECT_FAILD,
- 'message' => $oException->getMessage(),
- 'trace' => $oException->getTrace()
- );
- throw $oException;
- }
- }
- /**
- * close mysql connection
- */
- public function disconnect()
- {
- self::$_rLink = null;
- }
- /**
- * check mysql connection
- */
- public function ping()
- {
- try {
- $this->pdoQuery("SELECT 1");
- }
- catch (PDOException $e) {
- return false;
- }
- return true;
- }
- /**
- * Can be used to execute queries which shouldn't return data
- */
- public function pdoExec($sQuery)
- {
- return self::$_rLink->exec($sQuery);
- }
- /**
- * Executes query and returns PDOStatement object or false
- */
- public function pdoQuery($sQuery)
- {
- return self::$_rLink->query($sQuery);
- }
- /**
- * database query exception handler for exceptions appeared out of the try/catch block
- */
- public function pdoExceptionHandler($oException)
- {
- if(!($oException instanceof PDOException)) {
- throw $oException;
- return;
- }
- if(!isset($oException->errorInfo[self::$_sErrorKey]))
- $oException->errorInfo[self::$_sErrorKey] = array(
- 'code' => BX_DB_ERR_QUERY_ERROR,
- 'message' => !empty($oException->errorInfo[2]) ? $oException->errorInfo[2] : $oException->getMessage(),
- 'trace' => $oException->getTrace()
- );
- $this->error($oException->errorInfo[self::$_sErrorKey]);
- exit(255);
- }
- /**
- * get mysql option
- */
- function getOption($sName)
- {
- $oStatement = $this->pdoQuery("SELECT @@{$sName}");
- return $this->getOne($oStatement);
- }
- /**
- * execute sql query and return one value result
- */
- public function getOne($oStatement, $aBindings = array(), $iIndex = 0)
- {
- if(!$oStatement)
- return false;
- else if(!($oStatement instanceof PDOStatement) && is_string($oStatement))
- $oStatement = $this->prepare($oStatement);
- $aResult = array();
- if($this->res($oStatement, $aBindings))
- $aResult = $oStatement->fetch(PDO::FETCH_NUM);
- return is_array($aResult) && count($aResult) ? $aResult[$iIndex] : false;
- }
- /**
- * execute sql query and return one row result
- */
- function getRow($oStatement, $aBindings = array(), $iFetchType = PDO::FETCH_ASSOC)
- {
- $aResult = array();
- if(!$oStatement)
- return $aResult;
- else if(!($oStatement instanceof PDOStatement) && is_string($oStatement))
- $oStatement = $this->prepare($oStatement);
- if(!in_array($iFetchType, array(PDO::FETCH_NUM, PDO::FETCH_ASSOC, PDO::FETCH_BOTH)))
- $iFetchType = $this->_iPdoFetchType;
- if(!$this->res($oStatement, $aBindings))
- return array();
- $aResult = $oStatement->fetch($iFetchType);
- if($aResult === false)
- return array();
- return $aResult;
- }
- /**
- * execute sql query and return a column as result
- */
- function getColumn($oStatement, $aBindings = array(), $iFetchColumnNumber = 0)
- {
- $aResult = array();
- if(!$oStatement)
- return $aResult;
- else if(!($oStatement instanceof PDOStatement) && is_string($oStatement))
- $oStatement = $this->prepare($oStatement);
- if(!$this->res($oStatement, $aBindings))
- return array();
- $aResult = $oStatement->fetchAll(PDO::FETCH_COLUMN, $iFetchColumnNumber);
- if($aResult === false)
- return array();
- return $aResult;
- }
- /**
- * execute sql query and return the first row of result
- * and keep $array type and poiter to all data
- */
- public function getFirstRow($oStatement, $aBindings = array(), $iFetchType = PDO::FETCH_ASSOC)
- {
- if(!$oStatement)
- return array();
- else if(!($oStatement instanceof PDOStatement) && is_string($oStatement))
- $oStatement = $this->prepare($oStatement);
- if(!in_array($iFetchType, array(PDO::FETCH_NUM, PDO::FETCH_ASSOC, PDO::FETCH_BOTH)))
- $iFetchType = $this->_iPdoFetchType;
- if(!$this->res($oStatement, $aBindings))
- return array();
- $aResult = $oStatement->fetch($iFetchType);
- if($aResult === false) {
- $oStatement->closeCursor();
- return array();
- }
- $this->_oStatement = $oStatement;
- return $aResult;
- }
- /**
- * return next row of pointed last getFirstRow calling data
- */
- public function getNextRow($iFetchType = PDO::FETCH_ASSOC)
- {
- if(!$this->_oStatement)
- return array();
- if(!in_array($iFetchType, array(PDO::FETCH_NUM, PDO::FETCH_ASSOC, PDO::FETCH_BOTH)))
- $iFetchType = $this->_iPdoFetchType;
- $aResult = $this->_oStatement->fetch($iFetchType);
- if($aResult !== false)
- return $aResult;
- $this->_oStatement->closeCursor();
- $this->_oStatement = null;
- return array();
- }
- /**
- * execute sql query and return table of records as result
- */
- public function getAll($oStatement, $aBindings = array(), $iFetchType = PDO::FETCH_ASSOC)
- {
- $aResult = array();
- if(!$oStatement)
- return $aResult;
- else if(!($oStatement instanceof PDOStatement) && is_string($oStatement))
- $oStatement = $this->prepare($oStatement);
- if(!in_array($iFetchType, array(PDO::FETCH_NUM, PDO::FETCH_ASSOC, PDO::FETCH_BOTH)))
- $iFetchType = $this->_iPdoFetchType;
- if(!$this->res($oStatement, $aBindings))
- return array();
- $aResult = $oStatement->fetchAll($iFetchType);
- if($aResult === false)
- return array();
- return $aResult;
- }
- /**
- * Executes sql query and returns table of records as result.
- *
- * @deprecated use getAll instead.
- */
- public function fillArray($oStatement, $aBindings = array(), $iFetchType = PDO::FETCH_ASSOC)
- {
- return $this->getAll($oStatement, $aBindings, $iFetchType);
- }
- /**
- * execute sql query and return table of records as result
- */
- public function getAllWithKey($oStatement, $sFieldKey, $aBindings = array(), $iFetchType = PDO::FETCH_ASSOC)
- {
- $aResult = array();
- if(!$oStatement)
- return $aResult;
- else if(!($oStatement instanceof PDOStatement) && is_string($oStatement))
- $oStatement = $this->prepare($oStatement);
- $aRow = $this->getFirstRow($oStatement, $aBindings, $iFetchType);
- while(!empty($aRow)) {
- $aResult[$aRow[$sFieldKey]] = $aRow;
- $aRow = $this->getNextRow($iFetchType);
- }
- return $aResult;
- }
- /**
- * execute sql query and return table of records as result
- */
- public function getPairs($oStatement, $sFieldKey, $sFieldValue, $aBindings = [], $iFetchType = PDO::FETCH_ASSOC)
- {
- $aResult = [];
- if(!$oStatement)
- return $aResult;
- else if(!($oStatement instanceof PDOStatement) && is_string($oStatement))
- $oStatement = $this->prepare($oStatement);
- $aRow = $this->getFirstRow($oStatement, $aBindings, $iFetchType);
- while(!empty($aRow)) {
- $aResult[$aRow[$sFieldKey] ?? ''] = $aRow[$sFieldValue];
- $aRow = $this->getNextRow($iFetchType);
- }
- return $aResult;
- }
- /**
- * return number of affected rows in current mysql result
- *
- * NOTE: PDOStatement::rowCount works for SELECT queries in MySQL.
- * So, this method should be rewritten if the other DB engine will be used.
- */
- public function getNumRows($oStatement = null)
- {
- if($oStatement && ($oStatement instanceof PDOStatement))
- return $oStatement->rowCount();
- if($this->_oStatement && ($this->_oStatement instanceof PDOStatement))
- return $this->_oStatement->rowCount();
- return 0;
- }
- /**
- * returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement.
- */
- public function getAffectedRows($oStatement = null)
- {
- return $this->getNumRows($oStatement);
- }
- public function lastId()
- {
- return self::$_rLink->lastInsertId();
- }
- /**
- * execute any query return number of rows affected/false
- */
- public function query($oStatement, $aBindings = array(), $bVerbose = null)
- {
- if(!$oStatement)
- return false;
- else if(!($oStatement instanceof PDOStatement) && is_string($oStatement))
- $oStatement = $this->prepare($oStatement);
- if($this->res($oStatement, $aBindings, $bVerbose))
- return $oStatement->rowCount();
- return false;
- }
- /**
- * execute any query
- */
- public function res($oStatement, $aBindings = array(), $bVerbose = null)
- {
- if(!$oStatement || !($oStatement instanceof PDOStatement))
- return false;
- if($oStatement->errorCode() == BX_PDO_STATE_SUCCESS)
- return true;
- if(isset($GLOBALS['bx_profiler']))
- $GLOBALS['bx_profiler']->beginQuery($oStatement->queryString);
- for ($iAttempts = 3; $iAttempts > 0; --$iAttempts) {
- $oException = null;
- $iErrorCode = 0;
- $bResult = false;
- try {
- $bResult = $this->executeStatement($oStatement, $aBindings, $bVerbose, $iErrorCode);
- } catch (PDOException $oException) {
- $iErrorCode = $oException->errorInfo[1];
- }
- // if deadlock timeout - try again, make sure that deadlock timeout is short
- if (!$bResult && (1205 == $iErrorCode || 1213 == $iErrorCode)) {
- continue;
- }
- // if mysql connection is lost - reconnect and try again
- if(!$bResult && (2006 == $iErrorCode || !$this->ping())) {
- $this->disconnect();
- $this->connect();
- continue;
- }
- // in all other cases exit from the loop
- if ($oException)
- throw $oException;
- break;
- }
- if(isset($GLOBALS['bx_profiler']))
- $GLOBALS['bx_profiler']->endQuery($oStatement);
- ++self::$_iQueriesCounter;
- //is needed for SILENT mode
- if(!$bResult && !empty($this->_aError))
- $this->error($this->_aError);
- return $bResult;
- }
- /**
- * get mysql server info
- */
- public function getServerInfo()
- {
- return self::$_rLink->getAttribute(PDO::ATTR_SERVER_VERSION);
- }
- /**
- * get mysql version
- */
- public function getVersion()
- {
- $s = $this->getOne("SELECT VERSION()");
- return preg_match("/([0-9\.]+)/", $s, $m) ? $m[1] : false;
- }
-
- /**
- * get list of tables in database
- */
- public function listTables()
- {
- $sDBName = is_array($this->_sDbname) ? $this->_sDbname[0] : $this->_sDbname;
- $oStatement = $this->pdoQuery("SHOW TABLES FROM `" . $sDBName . "`");
- return $this->getColumn($oStatement);
- }
- public function getFields($sTable)
- {
- $oStatement = $this->pdoQuery("SHOW COLUMNS FROM `" . $sTable . "`");
- $aFields = $this->getAll($oStatement);
- $aResult = array('original' => array(), 'uppercase' => array());
- foreach($aFields as $aField) {
- $aResult['original'][] = $aField['Field'];
- $aResult['uppercase'][] = strtoupper($aField['Field']);
- }
- return $aResult;
- }
- public function fetchField($oStatement, $iField, $aBindings = array())
- {
- $aResult = array();
- if(!$oStatement)
- return $aResult;
- else if(!($oStatement instanceof PDOStatement) && is_string($oStatement)) {
- $oStatement = $this->prepare($oStatement);
- if(!$this->res($oStatement, $aBindings))
- return $aResult;
- }
- return $oStatement->getColumnMeta($iField);
- }
- public function isTableExists($sTable)
- {
- $aTableNames = $this->listTables();
- foreach($aTableNames as $iKey => $sTableName)
- $aTableNames[$iKey] = strtoupper($sTableName);
- return in_array(strtoupper($sTable), $aTableNames);
- }
- public function isFieldExists($sTable, $sFieldName)
- {
- $aFields = $this->getFields($sTable);
- return in_array(strtoupper($sFieldName), $aFields['uppercase']);
- }
- public function isIndexExists($sTable, $sIndexName)
- {
- $bIndex = false;
- $aIndexes = $this->getAll("SHOW INDEXES FROM `" . $sTable . "`");
- foreach($aIndexes as $aIndex)
- if($aIndex['Key_name'] == $sIndexName) {
- $bIndex = true;
- break;
- }
- return $bIndex;
- }
- public function error($aError)
- {
- $sErrorType = self::$_aErrors[$aError['code']];
- $bVerbose = isset($aError['verbose']) ? (bool)$aError['verbose'] : $this->_bErrorChecking;
- if(!$bVerbose) {
- $this->log($sErrorType . ': ' . $aError['message']);
- if (!defined('BX_DOL_INSTALL')) // this is needed to display error during installation
- return;
- }
- if((defined('BX_DB_FULL_VISUAL_PROCESSING') && BX_DB_FULL_VISUAL_PROCESSING) || defined('BX_DOL_INSTALL')) {
- $sOutput = '<div style="border:2px solid red;padding:4px;width:600px;margin:0px auto;">'."\n";
- $sOutput .= '<div style="text-align:center;background-color:red;color:white;font-weight:bold;">Error</div>'."\n";
- $sOutput .= '<div style="text-align:center;">' . $sErrorType . '</div>'."\n";
- if((defined('BX_DB_FULL_DEBUG_MODE') && BX_DB_FULL_DEBUG_MODE) || defined('BX_DOL_INSTALL'))
- $sOutput .= $this->errorOutput($aError);
- $sOutput .= '</div>';
- }
- if (self::$_rLink) { // connection errors aren't logged since bx_log required DB connection
- bx_log('sys_db', "$sErrorType\n" .
- (empty($aError['message']) ? '' : " Error: {$aError['message']}\n") .
- (empty($aError['query']) ? '' : " Query: {$aError['query']}\n") .
- (!function_exists('getLoggedId') || !getLoggedId() ? '' : " Account ID: " . getLoggedId() . "\n"),
- BX_LOG_ERR
- );
- }
- if(defined('BX_DB_DO_EMAIL_ERROR_REPORT') && BX_DB_DO_EMAIL_ERROR_REPORT) {
- $sSiteTitle = $this->getParam('site_title');
- $sMailBody = "Database error in " . $sSiteTitle . "<br /><br /> \n";
- $sMailBody .= $this->errorOutput($aError);
- $sMailBody .= "<hr />Auto-report system";
- sendMail($this->getParam('site_email'), "Database error in " . $sSiteTitle, $sMailBody, 0, array(), BX_EMAIL_SYSTEM, 'html', true);
- }
- bx_show_service_unavailable_error_and_exit($sOutput);
- }
- public function isParamInCache($sKey)
- {
- return is_array(self::$_aParams) && isset(self::$_aParams[$sKey]);
- }
- public function cacheParams($bForceCacheInvalidate = false, $bForceCacheInvalidateMixed = false)
- {
- if ($bForceCacheInvalidate)
- $this->cacheParamsClear();
- self::$_aParams = $this->getPairs("SELECT `name`, `value` FROM `sys_options`", "name", "value");
- list($sTmplCode, $sTmplName) = BxDolTemplate::retrieveCode();
- if(!empty($sTmplCode) && !empty($sTmplName)) {
- $iTmplMix = 0;
- if(is_array($sTmplCode))
- list($sTmplCode, $iTmplMix) = $sTmplCode;
- $bMixesDiabled = ($GLOBALS['glMixesDisabled'] ?? false);
- if(empty($iTmplMix) && !$bMixesDiabled)
- $iTmplMix = (int)$this->getParam($sTmplName . '_default_mix');
- if(!empty($iTmplMix) && !$bMixesDiabled) {
- $sCacheNameMixed = self::$_sParamsCacheNameMixed . $sTmplCode . '_' . $iTmplMix;
- if($bForceCacheInvalidateMixed)
- $this->cacheParamsClear($sCacheNameMixed);
- $aMixed = $this->fromCache($sCacheNameMixed, 'getPairs', "SELECT `option`, `value` FROM `sys_options_mixes2options` WHERE `mix_id`=:mix_id", "option", "value", array(
- 'mix_id' => $iTmplMix
- ));
- if(!empty($aMixed) && is_array($aMixed))
- self::$_aParams = array_merge(self::$_aParams, $aMixed);
- }
- }
- if (empty(self::$_aParams)) {
- self::$_aParams = array();
- return false;
- }
- return true;
- }
- public function cacheParamsClear($sCacheName = '')
- {
- if(empty($sCacheName)) {
- self::$_aParams = [];
- $sCacheName = self::$_sParamsCacheName;
- }
- return $this->cleanCache($sCacheName);
- }
- public function isParam($sKey, $bFromCache = true)
- {
- if ($bFromCache && $this->isParamInCache($sKey))
- return true;
- $sQuery = $this->prepare("SELECT `name` FROM `sys_options` WHERE `name` = ? LIMIT 1", $sKey);
- return $this->getOne($sQuery) == $sKey;
- }
- public function addParam($sName, $sValue, $iKateg, $sDesc, $sType)
- {
- $sQuery = $this->prepare("INSERT INTO `sys_options` SET `category_id` = ?, `name` = ?, `caption` = ?, `value` = ?, `type` = ?", $iKateg, $sName, $sDesc, $sValue, $sType);
- $this->query($sQuery);
- // renew params cache
- $this->cacheParams(true);
- }
- public function getParam($sKey, $bFromCache = true)
- {
- if (!$sKey)
- return false;
- if ($bFromCache) {
- return $this->isParamInCache($sKey) ? self::$_aParams[$sKey] : false;
- } else {
- $sQuery = $this->prepare("SELECT `tmo`.`value` AS `value` FROM `sys_options_mixes2options` AS `tmo` INNER JOIN `sys_options_mixes` AS `tm` ON `tmo`.`mix_id`=`tm`.`id` AND `tm`.`active`='1' WHERE `tmo`.`option`=? LIMIT 1", $sKey);
- $s = $this->getOne($sQuery);
- if($s === false) {
- $sQuery = $this->prepare("SELECT `value` FROM `sys_options` WHERE `name` = ? LIMIT 1", $sKey);
- $s = $this->getOne($sQuery);
- }
- self::$_aParams[$sKey] = $s; // cache it
- return $s;
- }
- }
- public function setParam($sKey, $mixedValue, $iMixId = 0)
- {
- if(empty($iMixId))
- $sQuery = $this->prepare("UPDATE `sys_options` SET `value` = ? WHERE `name` = ? LIMIT 1", $mixedValue, $sKey);
- else
- $sQuery = $this->prepare("REPLACE INTO `sys_options_mixes2options` SET `option` = ?, `mix_id` = ?, `value` = ?", $sKey, $iMixId, $mixedValue);
- $bResult = (int)$this->query($sQuery) > 0;
- // renew params cache
- self::$_aParams[$sKey] = $mixedValue;
- return $bResult;
- }
- public function getParamsMix($iId)
- {
- return $this->fromCache(self::$_sParamsCacheNameMix . $iId, 'getRow', 'SELECT * FROM `sys_options_mixes` WHERE `id`=:id', array(
- 'id' => $iId
- ));
- }
- public function getParamsMixActive($sType)
- {
- return $this->getRow("SELECT * FROM `sys_options_mixes` WHERE `type`=:type AND `active`='1'", array(
- 'type' => $sType
- ));
- }
- public function getParamsMixes($sType, $mixedPublished = false)
- {
- $aBindings = array('type' => $sType);
- $sQuery = "SELECT * FROM `sys_options_mixes` WHERE `type`=:type";
- if($mixedPublished !== false) {
- $aBindings['published'] = (int)$mixedPublished;
- $sQuery .= " AND `published`=:published";
- }
- return $this->getAll($sQuery, $aBindings);
- }
- public function setTimezone($sTimezone)
- {
- $oDate = new DateTime('now', new DateTimeZone($sTimezone));
- return $this->pdoExec('SET time_zone = "' . $oDate->format('P') . '"') !== false;
- }
- public function getEncoding()
- {
- $oStatement = $this->pdoQuery('SELECT @@character_set_database');
- if($oStatement !== false)
- return $this->getOne($oStatement);
- return false;
- }
- public function setErrorChecking ($b)
- {
- $this->_bErrorChecking = $b;
- }
- /**
- * Cache functions.
- */
- public function getDbCacheObject ()
- {
- if($this->_oDbCacheObject != null)
- return $this->_oDbCacheObject;
- $sEngine = $this->getParam('sys_db_cache_engine');
- $this->_oDbCacheObject = bx_instance('BxDolCache'.$sEngine);
- if(!$this->_oDbCacheObject->isAvailable())
- $this->_oDbCacheObject = bx_instance('BxDolCacheFile');
- return $this->_oDbCacheObject;
- }
- public function genDbCacheKey ($sName)
- {
- return 'db_' . $sName . '_' . bx_site_hash() . '.php';
- }
- /**
- * @param mixed $iTTL seconds, when an integer. A non-integer third argument is ignored:
- * existing callers pass a query string in that position. The file engine only
- * drops the entry when this read passes a TTL; other engines expire on write.
- */
- public function getCache ($sName, $sFunc, $iTTL = false)
- {
- if (!$this->getParam('sys_db_cache_enable'))
- return false;
- if (!is_int($iTTL) || $iTTL < 1)
- $iTTL = false;
- $oCache = $this->getDbCacheObject ();
- $sKey = $this->genDbCacheKey($sName);
- return $oCache->getData($sKey, $iTTL);
- }
- /**
- * @param mixed $iTTL seconds, when an integer greater than zero. Otherwise the engine default
- * (the file engine keeps the entry until it is deleted).
- */
- public function setCache ($sName, $mData, $iTTL = false)
- {
- if (!$this->getParam('sys_db_cache_enable'))
- return false;
- if (!is_int($iTTL) || $iTTL < 1)
- $iTTL = false;
- $oCache = $this->getDbCacheObject ();
- $sKey = $this->genDbCacheKey($sName);
- $oCache->setData($sKey, $mData, $iTTL);
- }
-
- public function fromCache ($sName, $sFunc)
- {
- $aArgs = func_get_args();
- array_shift ($aArgs); // shift $sName
- array_shift ($aArgs); // shift $sFunc
- if (!$this->getParam('sys_db_cache_enable'))
- return call_user_func_array (array ($this, $sFunc), $aArgs); // pass other function parameters as database function parameters
- $oCache = $this->getDbCacheObject ();
- $sKey = $this->genDbCacheKey($sName);
- $mixedRet = $oCache->getData($sKey);
- if ($mixedRet !== null) {
- return $mixedRet;
- } else {
- $mixedRet = call_user_func_array (array ($this, $sFunc), $aArgs); // pass other function parameters as database function parameters
- $oCache->setData($sKey, $mixedRet);
- }
- return $mixedRet;
- }
- public function cleanCache ($sName)
- {
- $oCache = $this->getDbCacheObject();
- $sKey = $this->genDbCacheKey($sName);
- return $oCache->delData($sKey);
- }
- public function &fromMemory ($sName, $sFunc)
- {
- if(array_key_exists($sName, self::$_aDbCacheData) && !defined('BX_DOL_INSTALL') && !defined('BX_DOL_CRON_EXECUTE'))
- return self::$_aDbCacheData[$sName];
- $aArgs = func_get_args();
- array_shift($aArgs); // shift $sName
- array_shift($aArgs); // shift $sFunc
- self::$_aDbCacheData[$sName] = call_user_func_array (array ($this, $sFunc), $aArgs); // pass other function parameters as database function parameters
- return self::$_aDbCacheData[$sName];
- }
- public function cleanMemory ($sName)
- {
- if(!isset(self::$_aDbCacheData[$sName]))
- return false;
- unset(self::$_aDbCacheData[$sName]);
- return true;
- }
- public function cleanMemoryAll ()
- {
- self::$_aDbCacheData = array();
- return true;
- }
- /**
- * It escapes string to pass to mysql query.
- * Try to use "prepare" function always (@see BxDolDb::prepare), use "escape" only if "prepare" function is not possible at all.
- * Also consider using "implode_escape" function (@see BxDolDb::implode_escape).
- *
- * @param string $s string to escape
- * @return escaped string whcich is ready to pass to SQL query.
- */
- public function escape($s)
- {
- try {
- $s = self::$_rLink->quote($s);
- }
- catch (PDOException $oException) {
- $oException->errorInfo[self::$_sErrorKey] = array(
- 'code' => BX_DB_ERR_ESCAPE,
- 'message' => $oException->getMessage(),
- 'trace' => $oException->getTrace()
- );
- throw $oException;
- }
- return $s;
- }
- /**
- * @param bool $bStrict one identifier of letters, digits and underscores. The loose check also allows a qualified name and most characters inside backticks.
- */
- public function isValidFieldName($s, $bStrict = false)
- {
- if ($bStrict)
- return is_string($s) && (bool)preg_match('/^[A-Za-z0-9_]+$/', $s);
- if (!is_string($s))
- return false;
- if (false !== strpos($s, '`.`')) {
- $a = explode('`.`', $s, 2);
- foreach ($a as $sField) {
- if (false === $this->isValidFieldName($sField, $bStrict))
- return false;
- }
- return true;
- }
- $inner = $s;
- // Disallow trailing space
- if ($inner === '' || preg_match('/\s$/u', $inner)) {
- return false;
- }
- // Disallow all-numeric content (even if quoted)
- if (preg_match('/^\d+$/u', $inner)) {
- return false;
- }
- $i = 0;
- $len = mb_strlen($inner, 'UTF-8');
- while ($i < $len) {
- $char = mb_substr($inner, $i, 1, 'UTF-8');
- // Allow escaped backtick (``)
- if ($char === '`') {
- if ($i + 1 >= $len || mb_substr($inner, $i + 1, 1, 'UTF-8') !== '`') {
- return false; // Single backtick is invalid
- }
- $i += 2;
- continue;
- }
- // Convert to UTF-8 bytes to check for supplementary chars
- $utf8 = mb_convert_encoding($char, 'UTF-8');
- $byteLen = strlen($utf8);
- // 1-byte to 3-byte UTF-8 sequences = BMP (valid)
- // 4-byte UTF-8 sequences = Supplementary chars (invalid)
- if ($byteLen === 4) {
- return false;
- }
- // Disallow ASCII NUL (0x00)
- if ($utf8 === "\x00") {
- return false;
- }
- $i++;
- }
- return true;
- /*
- // alternative quick checking without checking BMP range
- if (preg_match('/^[0-9]+$/', $s))
- return false;
- return preg_match('/^(?:(?!`)[\x01-\x7F\x{0080}-\x{FFFF}]|``)*[^[:space:]]$/u', $s);
- */
- }
- /**
- * This function is usefull when you need to form array of parameters to pass to IN(...) SQL construction.
- * Example:
- * @code
- * $a = array(2, 4.5, 'apple', 'car');
- * $s = "SELECT * FROM `t` WHERE `a` IN (" . $oDb->implode_escape($a) . ")";
- * echo $s; // outputs: SELECT * FROM `t` WHERE `a` IN (2, 4.5, 'apple', 'car')
- * @endcode
- *
- * @param $mixed array or parameters or just one paramter
- * @return string which is ready to pass to IN(...) SQL construction
- */
- public function implode_escape ($mixed)
- {
- if (is_array($mixed)) {
- $s = '';
- foreach ($mixed as $v)
- $s .= (is_numeric($v) ? $v : $this->escape($v)) . ',';
- if ($s)
- return substr($s, 0, -1);
- else
- return 'NULL';
- }
- return is_numeric($mixed) ? $mixed : ($mixed ? $this->escape($mixed) : 'NULL');
- }
- /**
- * @deprecated
- */
- public function unescape ($mixed)
- {
- if(is_array($mixed)) {
- foreach($mixed as $k => $v)
- $mixed[$k] = $this->getOne("SELECT '$v'");
- return $mixed;
- }
- else
- return $this->getOne("SELECT '$mixed'");
- }
- /**
- * Prepare SQL query before execution if some arguments are need to be passed to it.
- * All parameters marked with question (?) symbol in SQL query are replaced with parameters passed after SQL query parameter.
- * Parameters are properly excaped and surrounded by qutes if needed.
- * Example:
- * @code
- * $sSql = $oDb->prepare("SELECT `a`, `b` from `t` WHERE `c` = ? and `d` = ?", 12, 'aa');
- * echo $sSql;// outputs: SELECT `a`, `b` from `t` WHERE `c` = 12 and `d` = 'aa'
- * $a = $oDb->getAll($sSql);
- * @endcode
- *
- * @param string $sQuery SQL query, parameters for replacing are marked with ? symbol
- * @param mixed $mixed any number if parameters to replace, number of parameters whould match number of ? symbols in SQL query
- * @return PDOStatement object with SQL query ready for execution
- */
- public function prepare($sQuery)
- {
- if(!self::$_rLink)
- return false;
- if (self::$_bMultuServersMode && (0 != strncasecmp(ltrim($sQuery, " \t\n\r(\0\x0B"), 'SELECT', 6)))
- $this->setReadOnlyMode(false);
- $aArgs = func_get_args();
- $sQuery = array_shift($aArgs);
- $oStatement = self::$_rLink->prepare($sQuery);
- $iIndex = 1;
- foreach($aArgs as $mixedArg) {
- if(is_null($mixedArg))
- $iValueType = PDO::PARAM_NULL;
- else if(is_numeric($mixedArg) && is_int($mixedArg))
- $iValueType = PDO::PARAM_INT;
- else
- $iValueType = PDO::PARAM_STR;
- $oStatement->bindValue($iIndex++, $mixedArg, $iValueType);
- }
- return $oStatement;
- }
- /**
- * Prepare SQL query before execution if some arguments are need to be passed to it.
- * All parameters marked with question (?) symbol in SQL query are replaced with parameters passed after SQL query parameter.
- * Parameters are properly excaped and surrounded by qutes if needed.
- * Example:
- * @code
- * $sSql = $oDb->prepare("SELECT `a`, `b` from `t` WHERE `c` = ? and `d` = ?", 12, 'aa');
- * echo $sSql;// outputs: SELECT `a`, `b` from `t` WHERE `c` = 12 and `d` = 'aa'
- * $a = $oDb->getAll($sSql);
- * @endcode
- *
- * @param string $sQuery SQL query, parameters for replacing are marked with ? symbol
- * @param mixed $mixed any number if parameters to replace, number of parameters whould match number of ? symbols in SQL query
- * @return string with SQL query.
- */
- function prepareAsString($sQuery)
- {
- $aArgs = func_get_args();
- $sQuery = array_shift($aArgs);
- $iPos = 0;
- foreach ($aArgs as $mixedArg) {
- if (is_null($mixedArg))
- $s = 'NULL';
- elseif (is_numeric($mixedArg))
- $s = $mixedArg;
- else
- $s = $this->escape($mixedArg);
- $i = bx_mb_strpos($sQuery, '?', $iPos);
- $sQuery = bx_mb_substr_replace($sQuery, $s, $i, 1);
- $iPos = $i + get_mb_len($s);
- }
- return $sQuery;
- }
- /**
- * Convert array of key => values to SQL query.
- * Array keys are field names and array values are field values.
- * @param $a array
- * @param $sDiv fields separator, by default it is ',', another useful value is ' AND '
- * @param $sOperator operator for comparision: '=' or 'LIKE'
- * @param $bWildcardSpaceChars substiture any space and dash characters with single wildcard symbol
- * @return part of SQL query string
- */
- public function arrayToSQL($a, $sDiv = ',', $sOperator = '=', $bWildcardSpaceChars = false)
- {
- $aOperators = ['=', 'LIKE', 'like'];
- $sOperator = in_array($sOperator, $aOperators) ? $sOperator : '=';
- $s = '';
- foreach($a as $k => $v) {
- if (!$this->isValidFieldName($k))
- throw new Exception('Invalid field name in arrayToSQL method');
- if ($bWildcardSpaceChars)
- $v = preg_replace('/[\p{Zs}\p{Cc}\p{Pd}]/', '_', $v);
- $s .= "`{$k}` {$sOperator} " . (is_null($v) ? 'NULL' : $this->escape($v)) . $sDiv;
- }
- return trim($s, $sDiv);
- }
- function isValidOperator(string $operator): bool
- {
- static $operators = [
- // Comparison
- '=', '!=', '<>', '<', '>', '<=', '>=',
- '<=>',
-
- // Logical
- 'AND', 'OR', 'XOR', 'NOT',
-
- // Arithmetic
- '+', '-', '*', '/', '%',
- 'DIV', 'MOD',
-
- // Bitwise
- '&', '|', '^', '<<', '>>',
-
- // Other MySQL operators
- 'IS', 'IS NOT',
- 'LIKE', 'NOT LIKE',
- 'REGEXP', 'NOT REGEXP',
- 'IN', 'NOT IN',
- 'BETWEEN', 'NOT BETWEEN',
-
- // Custom functions/operators
- 'COUNT', 'LOCATE',
- 'EMPTY VALUE',
- 'NOT EMPTY VALUE',
- 'NOTHING',
- 'IN_SET',
- ];
-
- return in_array(strtoupper(trim($operator)), $operators, true);
- }
- protected function log($s)
- {
- if (defined('BX_DIRECTORY_PATH_LOGS')) {
- $sPath = BX_DIRECTORY_PATH_LOGS;
- }
- else {
- $sDirName = pathinfo(__FILE__, PATHINFO_DIRNAME);
- $sPath = $sDirName . '/../../logs/';
- }
- return file_put_contents($sPath . 'db.err.log', date('Y-m-d H:i:s') . "\t" . $s . "\n", FILE_APPEND);
- }
- public function executeSQL($sPath, $aReplace = array (), $isBreakOnError = true)
- {
- if(!file_exists($sPath) || !($rHandler = fopen($sPath, "r")))
- return array(array ('query' => "fopen($sPath, 'r')", 'error' => 'file not found or permission denied'));
- self::$_rLink->setAttribute(PDO::ATTR_ERRMODE, BX_DB_MODE_SILENT);
- $sQuery = "";
- $sDelimiter = ';';
- $aResult = array();
- while(!feof($rHandler)) {
- $sStr = trim(fgets($rHandler));
- if(empty($sStr) || $sStr[0] == "" || $sStr[0] == "#" || ($sStr[0] == "-" && $sStr[1] == "-"))
- continue;
- //--- Change delimiter ---//
- if(strpos($sStr, "DELIMITER //") !== false || strpos($sStr, "DELIMITER ;") !== false) {
- $sDelimiter = trim(str_replace('DELIMITER', '', $sStr));
- continue;
- }
- $sQuery .= $sStr;
- //--- Check for multiline query ---//
- if(substr($sStr, -strlen($sDelimiter)) != $sDelimiter)
- continue;
- //--- Execute query ---//
- if ($aReplace)
- $sQuery = str_replace($aReplace['from'], $aReplace['to'], $sQuery);
- if($sDelimiter != ';')
- $sQuery = str_replace($sDelimiter, "", $sQuery);
- if($this->query(trim($sQuery), array(), false) === false) {
- $aResult[] = array('query' => $sQuery, 'error' => $this->_aError['message']);
- if ($isBreakOnError)
- break;
- }
- $sQuery = "";
- }
- fclose($rHandler);
- self::$_rLink->setAttribute(PDO::ATTR_ERRMODE, $this->_iPdoErrorMode);
- return empty($aResult) ? true : $aResult;
- }
- protected function executeStatement($oStatement, $aBindings = array(), $bVerbose = null, &$iErrorCode = null)
- {
- $bResult = false;
- if (!isset($aBindings[0])) {
- foreach($aBindings as $sKey => $mixedValue) {
- if(is_null($mixedValue))
- $oStatement->bindValue(":{$sKey}", $mixedValue, PDO::PARAM_NULL);
- else if(is_numeric($mixedValue) && is_int($mixedValue))
- $oStatement->bindValue(":{$sKey}", $mixedValue, PDO::PARAM_INT);
- else
- $oStatement->bindValue(":{$sKey}", $mixedValue, PDO::PARAM_STR);
- }
- $aBindings = array();
- }
- switch (self::$_rLink->getAttribute(PDO::ATTR_ERRMODE)) {
- case PDO::ERRMODE_SILENT:
- $bResult = $this->executeStatementSilent($oStatement, $aBindings, $bVerbose);
- break;
- case PDO::ERRMODE_EXCEPTION:
- $bResult = $this->executeStatementException($oStatement, $aBindings, $bVerbose);
- break;
- }
- if (!$bResult && !is_null($iErrorCode)) {
- $aError = $oStatement->errorInfo();
- $iErrorCode = $aError[1];
- }
- return $bResult;
- }
- protected function executeStatementException($oStatement, $aBindings = array(), $bVerbose = null)
- {
- $bResult = false;
- try {
- $bResult = $oStatement->execute(!empty($aBindings) && is_array($aBindings) ? $aBindings : null);
- }
- catch (PDOException $oException) {
- $aError = $oStatement->errorInfo();
- $oException->errorInfo[self::$_sErrorKey] = array(
- 'code' => BX_DB_ERR_QUERY_ERROR,
- 'message' => !empty($aError[2]) ? $aError[2] : $oException->getMessage(),
- 'query' => $oStatement->queryString,
- 'trace' => $oException->getTrace(),
- 'verbose' => $bVerbose
- );
- throw $oException;
- }
- return $bResult;
- }
- protected function executeStatementSilent($oStatement, $aBindings = array(), $bVerbose = null)
- {
- $bResult = $oStatement->execute(!empty($aBindings) && is_array($aBindings) ? $aBindings : null);
- if($bResult)
- return true;
- $aError = $oStatement->errorInfo();
- $aTrace = debug_backtrace();
- unset($aTrace[0]);
- $this->_aError = array(
- 'code' => BX_DB_ERR_QUERY_ERROR,
- 'message' => !empty($aError[2]) ? $aError[2] : '',
- 'query' => $oStatement->queryString,
- 'trace' => $aTrace,
- 'verbose' => $bVerbose
- );
- return false;
- }
- protected function errorOutput($aError)
- {
- $aErrorLocation = [];
- if(!empty($aError['query']) && !empty($aError['trace']))
- foreach($aError['trace'] as $aCall )
- if(isset($aCall['args']) && is_array($aCall['args']))
- foreach($aCall['args'] as $argNum => $argVal)
- if((is_string($argVal) && strcmp($argVal, $aError['query']) == 0) || ($argVal instanceof PDOStatement && strcmp($argVal->queryString, $aError['query']) == 0)) {
- $aErrorLocation['file'] = isset($aCall['file']) ? $aCall['file'] : (isset($aCall['class']) ? 'class: ' . $aCall['class'] : 'undefined');
- $aErrorLocation['line'] = isset($aCall['line']) ? $aCall['line'] : 'undefined';
- $aErrorLocation['function'] = $aCall['function'];
- $aErrorLocation['arg'] = $argNum;
- }
- $sOutput = '';
- if(!empty($aError['query']))
- $sOutput .= '<p><b>Query:</b><br />' . bx_process_output($aError['query']) . '</p>'."\n";
- if(!empty($aError['message']))
- $sOutput .= '<p><b>Mysql error:</b><br />' . $aError['message'] . '</p>'."\n";
- if(!empty($aErrorLocation))
- $sOutput .= '<p><b>Location:</b><br />The error was found in <b>' . $aErrorLocation['function'] . '</b> function in the file <b>' . $aErrorLocation['file'] . '</b> at line <b>' . $aErrorLocation['line'] . '</b>.</p>'."\n";
- $sOutput .= '<p><b>collation_connection:</b><br />' . $this->getOne("SELECT @@collation_connection") . '</p>'."\n";
- if(!empty($aError['trace'])) {
- $sBackTrace = print_r($aError['trace'], true);
- if(defined ('BX_DATABASE_USER') && !is_array(BX_DATABASE_USER))
- $sBackTrace = str_replace('[_sUser:protected] => ' . BX_DATABASE_USER, '[_sUser:protected] => *****', $sBackTrace);
- if(defined ('BX_DATABASE_PASS') && !is_array(BX_DATABASE_PASS))
- $sBackTrace = str_replace('[_sPassword:protected] => ' . BX_DATABASE_PASS, '[_sPassword:protected] => *****', $sBackTrace);
- $sOutput .= '<div><b>Debug backtrace:</b></div><div style="overflow:scroll;height:300px;border:1px solid gray;"><pre>' . htmlspecialchars_adv($sBackTrace) . '</pre></div>';
- }
- if(!empty(self::$_aParams)) {
- $aParams = self::$_aParams;
- $aParamsSecret = $this->getColumn("SELECT `name` FROM `sys_options` WHERE `type`='secret'", "name");
- if($aParamsSecret && is_array($aParamsSecret))
- array_walk($aParams, function(&$sValue, $sKey) use ($aParamsSecret) {
- if(in_array($sKey, $aParamsSecret)) {
- $sValue = bx_gen_secret($sValue);
- } elseif (mb_strlen($sValue) > 100) {
- $sValue = mb_substr($sValue, 0, 100) . '...';
- }
- });
- $sOutput .= '<div><b>Settings:</b></div><div style="overflow:scroll;height:300px;border:1px solid gray;"><pre>' . htmlspecialchars_adv(var_export($aParams, true)) . '</pre></div>';
- }
- $sOutput .= '<p><b>Called script:</b><br />' . htmlspecialchars_adv($_SERVER['PHP_SELF']) . '</p>';
- if(!empty($_REQUEST))
- $sOutput .= '<p><b>Request parameters:</b><br /><pre>' . htmlspecialchars_adv(var_export($_REQUEST, true)) . '</pre></p>';
- return $sOutput;
- }
- }
- /**
- * Create the very first instance and initiate connetion to database.
- */
- BxDolDb::getInstance();
- function getParam($sParamName, $bUseCache = true)
- {
- if (bx_is_api() && $sParamName == 'permalinks_seo_links')
- return true;
-
- $oObj = BxDolDb::getInstance();
- if ($oObj === null)
- return false;
-
- return $oObj->getParam($sParamName, $bUseCache);
- }
- function setParam($sParamName, $sParamVal)
- {
- $oObj = BxDolDb::getInstance();
- if ($oObj === null)
- return false;
-
- return $oObj->setParam($sParamName, $sParamVal);
- }
- /** @} */
|