| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115 |
- <?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 $_aDbCacheData;
- protected static $_aParams;
- protected static $_sParamsCacheName = 'sys_options';
- protected static $_sParamsCacheNameMixed = 'sys_options_mixed';
- 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 $_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;
- $this->_iPdoFetchType = PDO::FETCH_ASSOC;
- $this->_iPdoErrorMode = BX_DB_MODE_EXCEPTION;
- $this->_bErrorChecking = true;
- $this->_aError = array();
- $this->_sStorageEngine = 'MYISAM';
- $this->_sCharset = 'utf8';
- 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'];
- }
- @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($aDbConf = false, &$sError = null)
- {
- 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;
- }
- /**
- * connect to database with appointed parameters
- */
- public function connect()
- {
- if(self::$_rLink)
- return;
- try {
- $sDsn = "mysql:host=" . $this->_sHost . ";";
- $sDsn .= $this->_sPort ? "port=" . $this->_sPort . ";" : "";
- $sDsn .= $this->_sSocket ? "unix_socket=" . $this->_sSocket . ";" : "";
- $sDsn .= "dbname=" . $this->_sDbname . ";charset=" . $this->_sCharset;
- self::$_rLink = new PDO($sDsn, $this->_sUser, $this->_sPassword, array(
- PDO::ATTR_ERRMODE => $this->_iPdoErrorMode,
- PDO::ATTR_DEFAULT_FETCH_MODE => $this->_iPdoFetchType,
- PDO::ATTR_PERSISTENT => $this->_bPdoPersistent
- ));
- $this->pdoExec("SET NAMES 'utf8'");
- $this->pdoExec("SET sql_mode = ''");
- $this->pdoExec("SET storage_engine=" . $this->_sStorageEngine);
- self::$_aDbCacheData = array();
- }
- 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))
- 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]);
- return;
- }
- /**
- * 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)
- 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 = 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);
- }
- /**
- * execute sql query and return table of records as result
- */
- public function getAllWithKey($oStatement, $sFieldKey, $aBindings = array())
- {
- $aResult = array();
- if(!$oStatement)
- return $aResult;
- else if(!($oStatement instanceof PDOStatement) && is_string($oStatement))
- $oStatement = $this->prepare($oStatement);
- $aRow = $this->getFirstRow($oStatement, $aBindings, PDO::FETCH_ASSOC);
- while(!empty($aRow)) {
- $aResult[$aRow[$sFieldKey]] = $aRow;
- $aRow = $this->getNextRow(PDO::FETCH_ASSOC);
- }
- return $aResult;
- }
- /**
- * execute sql query and return table of records as result
- */
- public function getPairs($oStatement, $sFieldKey, $sFieldValue, $aBindings = array())
- {
- $aResult = array();
- if(!$oStatement)
- return $aResult;
- else if(!($oStatement instanceof PDOStatement) && is_string($oStatement))
- $oStatement = $this->prepare($oStatement);
- $aRow = $this->getFirstRow($oStatement, $aBindings, PDO::FETCH_ASSOC);
- while(!empty($aRow)) {
- $aResult[$aRow[$sFieldKey]] = $aRow[$sFieldValue];
- $aRow = $this->getNextRow(PDO::FETCH_ASSOC);
- }
- 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);
- $bResult = $this->executeStatement($oStatement, $aBindings, $bVerbose);
- //if mysql connection is lost - reconnect and try again
- if(!$bResult && !$this->ping()) {
- $this->disconnect();
- $this->connect();
- $bResult = $this->executeStatement($oStatement, $aBindings, $bVerbose);
- }
- if(isset($GLOBALS['bx_profiler']))
- $GLOBALS['bx_profiler']->endQuery($bResult);
- //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 list of tables in database
- */
- public function listTables()
- {
- $oStatement = $this->pdoQuery("SHOW TABLES FROM " . BX_DATABASE_NAME);
- 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 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) {
- $sOutput = '<div style="border:2px solid red;padding:4px;width:600px;margin:0px auto;">';
- $sOutput .= '<div style="text-align:center;background-color:red;color:white;font-weight:bold;">Error</div>';
- $sOutput .= '<div style="text-align:center;">' . $sErrorType . '</div>';
- if(defined('BX_DB_FULL_DEBUG_MODE') && BX_DB_FULL_DEBUG_MODE)
- $sOutput .= $this->errorOutput($aError);
- $sOutput .= '</div>';
- }
- 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);
- }
- protected function isParamInCache($sKey)
- {
- return is_array(self::$_aParams) && isset(self::$_aParams[$sKey]);
- }
- protected function cacheParams($bForceCacheInvalidate = false)
- {
- if ($bForceCacheInvalidate)
- $this->cacheParamsClear();
- self::$_aParams = $this->fromCache(self::$_sParamsCacheName, 'getPairs', "SELECT `name`, `value` FROM `sys_options`", "name", "value");
- $aMixed = $this->fromCache(self::$_sParamsCacheNameMixed, 'getPairs', "SELECT `tmo`.`option` AS `option`, `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'", "option", "value");
- if(!empty($aMixed))
- self::$_aParams = array_merge(self::$_aParams, $aMixed);
- if (empty(self::$_aParams)) {
- self::$_aParams = array ();
- return false;
- }
- return true;
- }
- public function cacheParamsClear()
- {
- return $this->cleanCache(self::$_sParamsCacheName);
- }
- 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 && $this->isParamInCache($sKey)) {
- return self::$_aParams[$sKey];
- } 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);
- $mixedValue = $this->getOne($sQuery);
- if($mixedValue !== false)
- return $mixedValue;
- $sQuery = $this->prepare("SELECT `value` FROM `sys_options` WHERE `name` = ? LIMIT 1", $sKey);
- return $this->getOne($sQuery);
- }
- }
- 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
- $bResult &= $this->cacheParams(true);
- return $bResult;
- }
- 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';
- }
- 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))
- 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;
- }
- /**
- * 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;
- }
- /**
- * 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;
- $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))
- $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 '
- * @return part of SQL query string
- */
- public function arrayToSQL($a, $sDiv = ',')
- {
- $s = '';
- foreach($a as $k => $v)
- $s .= "`{$k}` = " . $this->escape($v) . $sDiv;
- return trim($s, $sDiv);
- }
- 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)
- {
- $bResult = false;
- 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;
- }
- 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 = array();
- 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 />' . $aError['query'] . '</p>';
- if(!empty($aError['message']))
- $sOutput .= '<p><b>Mysql error:</b><br />' . $aError['message'] . '</p>';
- 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>';
- if(!empty($aError['trace'])) {
- $sBackTrace = print_r($aError['trace'], true);
- if (defined ('BX_DATABASE_USER'))
- $sBackTrace = str_replace('[_sUser:protected] => ' . BX_DATABASE_USER, '[_sUser:protected] => *****', $sBackTrace);
- if (defined ('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)) {
- $sSettings = var_export(self::$_aParams, true);
- $sOutput .= '<div><b>Settings:</b></div><div style="overflow:scroll;height:300px;border:1px solid gray;"><pre>' . htmlspecialchars_adv($sSettings) . '</pre></div>';
- }
- $sOutput .= '<p><b>Called script:</b><br />' . $_SERVER['PHP_SELF'] . '</p>';
- if(!empty($_REQUEST)) {
- $sRequest = var_export($_REQUEST, true);
- $sOutput .= '<p><b>Request parameters:</b><br /><pre>' . htmlspecialchars_adv($sRequest) . '</pre></p>';
- }
- return $sOutput;
- }
- }
- /**
- * Create the very first instance and initiate connetion to database.
- */
- BxDolDb::getInstance();
- function getParam($sParamName, $bUseCache = true)
- {
- return BxDolDb::getInstance()->getParam($sParamName, $bUseCache);
- }
- function setParam($sParamName, $sParamVal)
- {
- return BxDolDb::getInstance()->setParam($sParamName, $sParamVal);
- }
- /** @} */
|