BxSEDb.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup SocialEngineMigration SocialEngine Migration
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * BxSEDb Class duplicate for connecting with Social Engine database
  13. * It allows to create two connections for different databases
  14. */
  15. class BxSEDb {
  16. static $_rLink;
  17. static $_aDbCacheData;
  18. static $_sErrorKey = 'bx_db_error';
  19. static $_aErrors = array(
  20. BX_DB_ERR_CONNECT_FAILD => 'Database connect failed',
  21. BX_DB_ERR_QUERY_ERROR => 'Database query error',
  22. BX_DB_ERR_ESCAPE => 'Escape string error'
  23. );
  24. var $_bPdoPersistent;
  25. var $_iPdoFetchType;
  26. var $_iPdoErrorMode;
  27. var $_bErrorChecking;
  28. var $_aError;
  29. var $_sHost, $_sPort, $_sSocket, $_sDbname, $_sUser, $_sPassword, $_sCharset, $_sStorageEngine;
  30. var $_oStatement = null;
  31. var $_oDbCacheObject = null;
  32. public function __construct($aDbConf = false)
  33. {
  34. $this->_iPdoFetchType = PDO::FETCH_ASSOC;
  35. $this->_iPdoErrorMode = BX_DB_MODE_EXCEPTION;
  36. $this->_bErrorChecking = true;
  37. $this->_aError = array();
  38. $this->_sStorageEngine = 'MYISAM';
  39. $this->_sHost = $aDbConf['host'];
  40. $this->_sPort = $aDbConf['port'];
  41. $this->_sSocket = $aDbConf['sock'];
  42. $this->_sDbname = $aDbConf['name'];
  43. $this->_sUser = $aDbConf['user'];
  44. $this->_sPassword = $aDbConf['pwd'];
  45. if(isset($aDbConf['charset']))
  46. $this->_sCharset = $aDbConf['charset'];
  47. @set_exception_handler(array($this, 'pdoExceptionHandler'));
  48. }
  49. public function connect()
  50. {
  51. if(self::$_rLink)
  52. return true;
  53. try {
  54. $sDsn = "mysql:host=" . $this->_sHost . ";";
  55. $sDsn .= $this->_sPort ? "port=" . $this->_sPort . ";" : "";
  56. $sDsn .= $this->_sSocket ? "unix_socket=" . $this->_sSocket . ";" : "";
  57. $sDsn .= "dbname=" . $this->_sDbname . ";charset=" . $this->_sCharset;
  58. self::$_rLink = new PDO($sDsn, $this->_sUser, $this->_sPassword, array(
  59. PDO::ATTR_ERRMODE => $this->_iPdoErrorMode,
  60. PDO::ATTR_DEFAULT_FETCH_MODE => $this->_iPdoFetchType,
  61. PDO::ATTR_PERSISTENT => $this->_bPdoPersistent
  62. ));
  63. $this->pdoExec("SET NAMES 'utf8'");
  64. $this->pdoExec("SET sql_mode = ''");
  65. $this->pdoExec("SET storage_engine=" . $this->_sStorageEngine);
  66. self::$_aDbCacheData = array();
  67. }
  68. catch (PDOException $oException) {
  69. $oException->errorInfo[self::$_sErrorKey] = array(
  70. 'code' => BX_DB_ERR_CONNECT_FAILD,
  71. 'message' => $oException->getMessage(),
  72. 'trace' => $oException->getTrace()
  73. );
  74. throw $oException;
  75. }
  76. return true;
  77. }
  78. public function res($oStatement, $aBindings = array(), $bVerbose = null)
  79. {
  80. if(!$oStatement || !($oStatement instanceof PDOStatement))
  81. return false;
  82. if($oStatement->errorCode() == BX_PDO_STATE_SUCCESS)
  83. return true;
  84. $bResult = $this->executeStatement($oStatement, $aBindings, $bVerbose);
  85. //if mysql connection is lost - reconnect and try again
  86. if(!$bResult && !$this->ping()) {
  87. $this->disconnect();
  88. $this->connect();
  89. $bResult = $this->executeStatement($oStatement, $aBindings, $bVerbose);
  90. }
  91. if(isset($GLOBALS['bx_profiler']))
  92. $GLOBALS['bx_profiler']->endQuery($bResult);
  93. //is needed for SILENT mode
  94. if(!$bResult && !empty($this->_aError))
  95. $this->error($this->_aError);
  96. return $bResult;
  97. }
  98. public function ping()
  99. {
  100. try {
  101. $this->pdoQuery("SELECT 1");
  102. }
  103. catch (PDOException $e) {
  104. return false;
  105. }
  106. return true;
  107. }
  108. public function pdoQuery($sQuery)
  109. {
  110. return self::$_rLink->query($sQuery);
  111. }
  112. public function pdoExec($sQuery)
  113. {
  114. return self::$_rLink->exec($sQuery);
  115. }
  116. public function getOne($oStatement, $aBindings = array(), $iIndex = 0)
  117. {
  118. if(!$oStatement)
  119. return false;
  120. else if(!($oStatement instanceof PDOStatement) && is_string($oStatement))
  121. $oStatement = $this->prepare($oStatement);
  122. $aResult = array();
  123. if($this->res($oStatement, $aBindings))
  124. $aResult = $oStatement->fetch(PDO::FETCH_NUM);
  125. return is_array($aResult) && count($aResult) ? $aResult[$iIndex] : false;
  126. }
  127. public function prepare($sQuery)
  128. {
  129. if(!self::$_rLink)
  130. return false;
  131. $aArgs = func_get_args();
  132. $sQuery = array_shift($aArgs);
  133. $oStatement = self::$_rLink->prepare($sQuery);
  134. $iIndex = 1;
  135. foreach($aArgs as $mixedArg) {
  136. if(is_null($mixedArg))
  137. $iValueType = PDO::PARAM_NULL;
  138. else if(is_numeric($mixedArg))
  139. $iValueType = PDO::PARAM_INT;
  140. else
  141. $iValueType = PDO::PARAM_STR;
  142. $oStatement->bindValue($iIndex++, $mixedArg, $iValueType);
  143. }
  144. return $oStatement;
  145. }
  146. protected function executeStatement($oStatement, $aBindings = array(), $bVerbose = null)
  147. {
  148. $bResult = false;
  149. switch (self::$_rLink->getAttribute(PDO::ATTR_ERRMODE)) {
  150. case PDO::ERRMODE_SILENT:
  151. $bResult = $this->executeStatementSilent($oStatement, $aBindings, $bVerbose);
  152. break;
  153. case PDO::ERRMODE_EXCEPTION:
  154. $bResult = $this->executeStatementException($oStatement, $aBindings, $bVerbose);
  155. break;
  156. }
  157. return $bResult;
  158. }
  159. protected function executeStatementException($oStatement, $aBindings = array(), $bVerbose = null)
  160. {
  161. $bResult = false;
  162. try {
  163. $bResult = $oStatement->execute(!empty($aBindings) && is_array($aBindings) ? $aBindings : null);
  164. }
  165. catch (PDOException $oException) {
  166. $aError = $oStatement->errorInfo();
  167. $oException->errorInfo[self::$_sErrorKey] = array(
  168. 'code' => BX_DB_ERR_QUERY_ERROR,
  169. 'message' => !empty($aError[2]) ? $aError[2] : $oException->getMessage(),
  170. 'query' => $oStatement->queryString,
  171. 'trace' => $oException->getTrace(),
  172. 'verbose' => $bVerbose
  173. );
  174. throw $oException;
  175. }
  176. return $bResult;
  177. }
  178. protected function executeStatementSilent($oStatement, $aBindings = array(), $bVerbose = null)
  179. {
  180. $bResult = $oStatement->execute(!empty($aBindings) && is_array($aBindings) ? $aBindings : null);
  181. if($bResult)
  182. return true;
  183. $aError = $oStatement->errorInfo();
  184. $aTrace = debug_backtrace();
  185. unset($aTrace[0]);
  186. $this->_aError = array(
  187. 'code' => BX_DB_ERR_QUERY_ERROR,
  188. 'message' => !empty($aError[2]) ? $aError[2] : '',
  189. 'query' => $oStatement->queryString,
  190. 'trace' => $aTrace,
  191. 'verbose' => $bVerbose
  192. );
  193. return false;
  194. }
  195. function getRow($oStatement, $aBindings = array(), $iFetchType = PDO::FETCH_ASSOC)
  196. {
  197. $aResult = array();
  198. if(!$oStatement)
  199. return $aResult;
  200. else if(!($oStatement instanceof PDOStatement) && is_string($oStatement))
  201. $oStatement = $this->prepare($oStatement);
  202. if(!in_array($iFetchType, array(PDO::FETCH_NUM, PDO::FETCH_ASSOC, PDO::FETCH_BOTH)))
  203. $iFetchType = $this->_iPdoFetchType;
  204. if(!$this->res($oStatement, $aBindings))
  205. return array();
  206. $aResult = $oStatement->fetch($iFetchType);
  207. if($aResult === false)
  208. return array();
  209. return $aResult;
  210. }
  211. public function query($oStatement, $aBindings = array(), $bVerbose = null)
  212. {
  213. if(!$oStatement)
  214. return false;
  215. else if(!($oStatement instanceof PDOStatement) && is_string($oStatement))
  216. $oStatement = $this->prepare($oStatement);
  217. if($this->res($oStatement, $aBindings, $bVerbose))
  218. return $oStatement->rowCount();
  219. return false;
  220. }
  221. public function getAll($oStatement, $aBindings = array(), $iFetchType = PDO::FETCH_ASSOC)
  222. {
  223. $aResult = array();
  224. if(!$oStatement)
  225. return $aResult;
  226. else if(!($oStatement instanceof PDOStatement) && is_string($oStatement))
  227. $oStatement = $this->prepare($oStatement);
  228. if(!in_array($iFetchType, array(PDO::FETCH_NUM, PDO::FETCH_ASSOC, PDO::FETCH_BOTH)))
  229. $iFetchType = $this->_iPdoFetchType;
  230. if(!$this->res($oStatement, $aBindings))
  231. return array();
  232. $aResult = $oStatement->fetchAll($iFetchType);
  233. if($aResult === false)
  234. return array();
  235. return $aResult;
  236. }
  237. public function getPairs($oStatement, $sFieldKey, $sFieldValue, $aBindings = array())
  238. {
  239. $aResult = array();
  240. if(!$oStatement)
  241. return $aResult;
  242. else if(!($oStatement instanceof PDOStatement) && is_string($oStatement))
  243. $oStatement = $this->prepare($oStatement);
  244. $aRow = $this->getFirstRow($oStatement, $aBindings, PDO::FETCH_ASSOC);
  245. while(!empty($aRow)) {
  246. $aResult[$aRow[$sFieldKey]] = $aRow[$sFieldValue];
  247. $aRow = $this->getNextRow(PDO::FETCH_ASSOC);
  248. }
  249. return $aResult;
  250. }
  251. public function getFirstRow($oStatement, $aBindings = array(), $iFetchType = PDO::FETCH_ASSOC)
  252. {
  253. if(!$oStatement)
  254. return array();
  255. else if(!($oStatement instanceof PDOStatement) && is_string($oStatement))
  256. $oStatement = $this->prepare($oStatement);
  257. if(!in_array($iFetchType, array(PDO::FETCH_NUM, PDO::FETCH_ASSOC, PDO::FETCH_BOTH)))
  258. $iFetchType = $this->_iPdoFetchType;
  259. if(!$this->res($oStatement, $aBindings))
  260. return array();
  261. $aResult = $oStatement->fetch($iFetchType);
  262. if($aResult === false)
  263. return array();
  264. $this->_oStatement = $oStatement;
  265. return $aResult;
  266. }
  267. public function getNextRow($iFetchType = PDO::FETCH_ASSOC)
  268. {
  269. if(!$this->_oStatement)
  270. return array();
  271. if(!in_array($iFetchType, array(PDO::FETCH_NUM, PDO::FETCH_ASSOC, PDO::FETCH_BOTH)))
  272. $iFetchType = $this->_iPdoFetchType;
  273. $aResult = $this->_oStatement->fetch($iFetchType);
  274. if($aResult !== false)
  275. return $aResult;
  276. $this->_oStatement = null;
  277. return array();
  278. }
  279. public function isTableExists($sTable){
  280. $sQuery = $this -> prepare("SHOW TABLES LIKE ?", $sTable);
  281. $aResult = $this -> getRow($sQuery);
  282. return !empty($aResult);
  283. }
  284. public function getSEParam($sTable, $sName){
  285. if (!$this -> isTableExists($sTable)) return '';
  286. $sQuery = $this -> prepare("SELECT `value` FROM `{$sTable}` WHERE `name` = ?", $sName);
  287. return $this -> getOne($sQuery);
  288. }
  289. }
  290. /** @} */