BxDolDb.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import('BxDolParams');
  10. class BxDolDb extends BxDol implements iBxDolSingleton {
  11. var $error_checking = true;
  12. var $error_message;
  13. var $host, $port, $socket, $dbname, $user, $password, $link;
  14. var $current_res, $current_arr_type;
  15. var $oParams = null;
  16. var $oDbCacheObject = null;
  17. /**
  18. * set database parameters and connect to it
  19. */
  20. protected function BxDolDb() {
  21. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  22. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  23. parent::BxDol();
  24. $this->host = BX_DATABASE_HOST;
  25. $this->port = BX_DATABASE_PORT;
  26. $this->socket = BX_DATABASE_SOCK;
  27. $this->dbname = BX_DATABASE_NAME;
  28. $this->user = BX_DATABASE_USER;
  29. $this->password = BX_DATABASE_PASS;
  30. $this->current_arr_type = MYSQL_ASSOC;
  31. // connect to db automatically
  32. if (empty($GLOBALS['bx_db_link'])) {
  33. $this->connect();
  34. $GLOBALS['gl_db_cache'] = array();
  35. } else {
  36. $this->link = $GLOBALS['bx_db_link'];
  37. }
  38. $this->oParams = BxDolParams::getInstance($this);
  39. }
  40. /**
  41. * Prevent cloning the instance
  42. */
  43. public function __clone() {
  44. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  45. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  46. }
  47. /**
  48. * Get singleton instance of the class
  49. */
  50. public static function getInstance() {
  51. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  52. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolDb();
  53. return $GLOBALS['bxDolClasses'][__CLASS__];
  54. }
  55. /**
  56. * connect to database with appointed parameters
  57. */
  58. function connect()
  59. {
  60. $full_host = $this->host;
  61. $full_host .= $this->port ? ':'.$this->port : '';
  62. $full_host .= $this->socket ? ':'.$this->socket : '';
  63. $this->link = @mysql_pconnect($full_host, $this->user, $this->password);
  64. if (!$this->link)
  65. $this->error('Database connect failed', true);
  66. if (!$this->select_db())
  67. $this->error('Database select failed', true);
  68. mysql_query("SET NAMES 'utf8'", $this->link);
  69. mysql_query("SET sql_mode = ''", $this->link);
  70. $GLOBALS['bx_db_link'] = $this->link;
  71. }
  72. function select_db()
  73. {
  74. return @mysql_select_db($this->dbname, $this->link) or $this->error('Cannot complete query (select_db)');
  75. }
  76. /**
  77. * close mysql connection
  78. */
  79. function close()
  80. {
  81. mysql_close($this->link);
  82. }
  83. /**
  84. * execute sql query and return one row result
  85. */
  86. function getRow($query, $arr_type = MYSQL_ASSOC)
  87. {
  88. if(!$query)
  89. return array();
  90. if($arr_type != MYSQL_ASSOC && $arr_type != MYSQL_NUM && $arr_type != MYSQL_BOTH)
  91. $arr_type = MYSQL_ASSOC;
  92. $res = $this->res ($query);
  93. $arr_res = array();
  94. if($res && mysql_num_rows($res))
  95. {
  96. $arr_res = mysql_fetch_array($res, $arr_type);
  97. mysql_free_result($res);
  98. }
  99. return $arr_res;
  100. }
  101. /**
  102. * execute sql query and return a column as result
  103. */
  104. function getColumn($sQuery) {
  105. if(!$sQuery)
  106. return array();
  107. $rResult = $this->res($sQuery);
  108. $aResult = array();
  109. if($rResult) {
  110. while($aRow = mysql_fetch_array($rResult, MYSQL_NUM))
  111. $aResult[] = $aRow[0];
  112. mysql_free_result($rResult);
  113. }
  114. return $aResult;
  115. }
  116. /**
  117. * execute sql query and return one value result
  118. */
  119. function getOne($query, $index = 0)
  120. {
  121. if(!$query)
  122. return false;
  123. $res = $this->res ($query);
  124. $arr_res = array();
  125. if($res && mysql_num_rows($res))
  126. $arr_res = mysql_fetch_array($res);
  127. if(count($arr_res))
  128. return $arr_res[$index];
  129. else
  130. return false;
  131. }
  132. /**
  133. * execute sql query and return the first row of result
  134. * and keep $array type and poiter to all data
  135. */
  136. function getFirstRow($query, $arr_type = MYSQL_ASSOC)
  137. {
  138. if(!$query)
  139. return array();
  140. if($arr_type != MYSQL_ASSOC && $arr_type != MYSQL_NUM)
  141. $this->current_arr_type = MYSQL_ASSOC;
  142. else
  143. $this->current_arr_type = $arr_type;
  144. $this->current_res = $this->res ($query);
  145. $arr_res = array();
  146. if($this->current_res && mysql_num_rows($this->current_res))
  147. $arr_res = mysql_fetch_array($this->current_res, $this->current_arr_type);
  148. return $arr_res;
  149. }
  150. /**
  151. * return next row of pointed last getFirstRow calling data
  152. */
  153. function getNextRow()
  154. {
  155. $arr_res = mysql_fetch_array($this->current_res, $this->current_arr_type);
  156. if($arr_res)
  157. return $arr_res;
  158. else
  159. {
  160. mysql_free_result($this->current_res);
  161. $this->current_arr_type = MYSQL_ASSOC;
  162. return array();
  163. }
  164. }
  165. /**
  166. * return number of affected rows in current mysql result
  167. */
  168. function getNumRows($res = false)
  169. {
  170. if ($res)
  171. return (int)@mysql_num_rows($res);
  172. elseif (!$this->current_res)
  173. return (int)@mysql_num_rows($this->current_res);
  174. else
  175. return 0;
  176. }
  177. /**
  178. * execute any query return number of rows affected/false
  179. */
  180. function getAffectedRows()
  181. {
  182. return mysql_affected_rows($this->link);
  183. }
  184. /**
  185. * execute any query return number of rows affected/false
  186. */
  187. function query($query)
  188. {
  189. $res = $this->res($query);
  190. if ($res)
  191. return mysql_affected_rows($this->link);
  192. return false;
  193. }
  194. /**
  195. * execute any query
  196. */
  197. function res($query, $error_checking = true)
  198. {
  199. if(!$query)
  200. return false;
  201. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginQuery($query);
  202. $res = @mysql_query($query, $this->link);
  203. if (false === $res)
  204. $this->error_message = @mysql_error($this->link); // we need to remeber last error message since mysql_ping will reset it on the next line !
  205. else
  206. $this->error_message = '';
  207. if (false === $res && !@mysql_ping($this->link)) { // if mysql connection is lost - reconnect and try again
  208. @mysql_close($this->link);
  209. $this->connect();
  210. $res = mysql_query($query, $this->link);
  211. }
  212. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endQuery($res);
  213. if (!$res)
  214. $this->error('Database query error', false, $query);
  215. return $res;
  216. }
  217. /**
  218. * execute sql query and return table of records as result
  219. */
  220. function getAll($query, $arr_type = MYSQL_ASSOC)
  221. {
  222. if(!$query)
  223. return array();
  224. if($arr_type != MYSQL_ASSOC && $arr_type != MYSQL_NUM && $arr_type != MYSQL_BOTH)
  225. $arr_type = MYSQL_ASSOC;
  226. $res = $this->res ($query);
  227. $arr_res = array();
  228. if($res)
  229. {
  230. while($row = mysql_fetch_array($res, $arr_type))
  231. $arr_res[] = $row;
  232. mysql_free_result($res);
  233. }
  234. return $arr_res;
  235. }
  236. /**
  237. * execute sql query and return table of records as result
  238. */
  239. function fillArray($res, $arr_type = MYSQL_ASSOC)
  240. {
  241. if(!$res)
  242. return array();
  243. if($arr_type != MYSQL_ASSOC && $arr_type != MYSQL_NUM && $arr_type != MYSQL_BOTH)
  244. $arr_type = MYSQL_ASSOC;
  245. $arr_res = array();
  246. while($row = mysql_fetch_array($res, $arr_type))
  247. $arr_res[] = $row;
  248. mysql_free_result($res);
  249. return $arr_res;
  250. }
  251. /**
  252. * execute sql query and return table of records as result
  253. */
  254. function getAllWithKey($query, $sFieldKey)
  255. {
  256. if(!$query)
  257. return array();
  258. $res = $this->res ($query);
  259. $arr_res = array();
  260. if($res)
  261. {
  262. while($row = mysql_fetch_array($res, MYSQL_ASSOC))
  263. {
  264. $arr_res[$row[$sFieldKey]] = $row;
  265. }
  266. mysql_free_result($res);
  267. }
  268. return $arr_res;
  269. }
  270. /**
  271. * execute sql query and return table of records as result
  272. */
  273. function getPairs($query, $sFieldKey, $sFieldValue, $arr_type = MYSQL_ASSOC)
  274. {
  275. if(!$query)
  276. return array();
  277. $res = $this->res ($query);
  278. $arr_res = array();
  279. if($res)
  280. {
  281. while($row = mysql_fetch_array($res, MYSQL_ASSOC))
  282. {
  283. $arr_res[$row[$sFieldKey]] = $row[$sFieldValue];
  284. }
  285. mysql_free_result($res);
  286. }
  287. return $arr_res;
  288. }
  289. function lastId()
  290. {
  291. return mysql_insert_id($this->link);
  292. }
  293. function getErrorMessage () {
  294. $s = mysql_error($this->link);
  295. if ($s)
  296. return $s;
  297. else
  298. return $this->error_message;
  299. }
  300. function error($text, $isForceErrorChecking = false, $sSqlQuery = '')
  301. {
  302. if ($this->error_checking || $isForceErrorChecking)
  303. $this->genMySQLErr ($text, $sSqlQuery);
  304. else
  305. $this->log($text.': ' . $this->getErrorMessage());
  306. }
  307. function isParam($sName, $bCache = true) {
  308. return $this->oParams->exists($sName, $bCache);
  309. }
  310. function addParam($sName, $sValue, $iKateg, $sDesc, $sType) {
  311. return $this->oParams->add($sName, $sValue, $iKateg, $sDesc, $sType);
  312. }
  313. function getParam($sName, $bCache = true) {
  314. return $this->oParams->get($sName, $bCache);
  315. }
  316. function setParam($sName, $sValue ) {
  317. $this->oParams->set($sName, $sValue);
  318. return true;
  319. }
  320. function listTables() {
  321. return mysql_list_tables($GLOBALS['db']['db'], $this->link);
  322. //return mysql_list_tables($GLOBALS['db']['db'], $this->link) or $this->error('Database get encoding error');
  323. }
  324. function getFields($sTable) {
  325. $rFields = mysql_list_fields($this->dbname, $sTable, $this->link);
  326. $iFields = mysql_num_fields($rFields);
  327. $aResult = array('original' => array(), 'uppercase' => array());
  328. for($i = 0; $i < $iFields; $i++) {
  329. $sName = mysql_field_name($rFields, $i);
  330. $aResult['original'][] = $sName;
  331. $aResult['uppercase'][] = strtoupper($sName);
  332. }
  333. return $aResult;
  334. }
  335. function isFieldExists($sTable, $sFieldName) {
  336. $aFields = $this->getFields($sTable);
  337. return in_array(strtoupper($sFieldName), $aFields['uppercase']);
  338. }
  339. function getEncoding() {
  340. return mysql_client_encoding($this->link) or $this->error('Database get encoding error');
  341. }
  342. function genMySQLErr( $out, $query ='' ) {
  343. $sParamsOutput = false;
  344. $sFoundError = '';
  345. $aBackTrace = debug_backtrace();
  346. unset( $aBackTrace[0] );
  347. if( $query )
  348. {
  349. //try help to find error
  350. $aFoundError = array();
  351. foreach( $aBackTrace as $aCall )
  352. {
  353. // truncating global settings since it repeated many times and output it separately
  354. if (isset($aCall['object']) && property_exists($aCall['object'], 'oParams') && property_exists($aCall['object']->oParams, '_aParams')) {
  355. if (false === $sParamsOutput)
  356. $sParamsOutput = var_export($aCall['object']->oParams->_aParams, true);
  357. $aCall['object']->oParams->_aParams = '[truncated]';
  358. }
  359. if (isset($aCall['args']) && is_array($aCall['args']))
  360. {
  361. foreach( $aCall['args'] as $argNum => $argVal )
  362. {
  363. if( is_string($argVal) and strcmp( $argVal, $query ) == 0 )
  364. {
  365. $aFoundError['file'] = isset($aCall['file']) ? $aCall['file'] : (isset($aCall['class']) ? 'class: ' . $aCall['class'] : 'undefined');
  366. $aFoundError['line'] = isset($aCall['line']) ? $aCall['line'] : 'undefined';
  367. $aFoundError['function'] = $aCall['function'];
  368. $aFoundError['arg'] = $argNum;
  369. }
  370. }
  371. }
  372. }
  373. if( $aFoundError )
  374. {
  375. $sFoundError = <<<EOJ
  376. Found error in the file '<b>{$aFoundError['file']}</b>' at line <b>{$aFoundError['line']}</b>.<br />
  377. Called '<b>{$aFoundError['function']}</b>' function with erroneous argument #<b>{$aFoundError['arg']}</b>.<br /><br />
  378. EOJ;
  379. }
  380. }
  381. bx_import('BxDolConfig');
  382. if (BxDolConfig::getInstance()->get('db', 'visual_processing')) {
  383. ?>
  384. <div style="border:2px solid red;padding:4px;width:600px;margin:0px auto;">
  385. <div style="text-align:center;background-color:red;color:white;font-weight:bold;">Error</div>
  386. <div style="text-align:center;"><?=$out?></div>
  387. <?
  388. if(BxDolConfig::getInstance()->get('db', 'debug_mode')) {
  389. if( strlen( $query ) )
  390. echo "<div><b>Query:</b><br />{$query}</div>";
  391. if ($this->link)
  392. echo '<div><b>Mysql error:</b><br />' . $this->getErrorMessage() . '</div>';
  393. echo '<div style="overflow:scroll;height:300px;border:1px solid gray;">';
  394. echo $sFoundError;
  395. echo "<b>Debug backtrace:</b><br />";
  396. $sBackTrace = print_r($aBackTrace, true);
  397. $sBackTrace = str_replace('[password] => ' . BX_DATABASE_PASS, '[password] => *****', $sBackTrace);
  398. $sBackTrace = str_replace('[user] => ' . BX_DATABASE_USER, '[user] => *****', $sBackTrace);
  399. echo '<pre>' . $sBackTrace . '</pre>';
  400. if ($sParamsOutput) {
  401. echo '<hr />';
  402. echo "<b>Settings:</b><br />";
  403. echo '<pre>' . htmlspecialchars_adv($sParamsOutput) . '</pre>';
  404. }
  405. echo "<b>Called script:</b> " . $_SERVER['PHP_SELF'] . "<br />";
  406. echo "<b>Request parameters:</b><br />";
  407. echoDbg( $_REQUEST );
  408. echo '</div>';
  409. }
  410. ?>
  411. </div>
  412. <?
  413. }
  414. else
  415. echo $out;
  416. if(BxDolConfig::getInstance()->get('db', 'error_remort_by_email')) {
  417. $sSiteTitle = getParam('site_title');
  418. $sMailBody = "Database error in " . $sSiteTitle . "<br /><br /> \n";
  419. if( strlen( $query ) )
  420. $sMailBody .= "Query: <pre>" . htmlspecialchars_adv($query) . "</pre> ";
  421. if ($this->link)
  422. $sMailBody .= "Mysql error: " . $this->getErrorMessage() . "<br /><br /> ";
  423. $sMailBody .= $sFoundError. '<br /> ';
  424. $sBackTrace = print_r($aBackTrace, true);
  425. $sBackTrace = str_replace('[password] => ' . BX_DATABASE_PASS, '[password] => *****', $sBackTrace);
  426. $sBackTrace = str_replace('[user] => ' . BX_DATABASE_USER, '[user] => *****', $sBackTrace);
  427. $sMailBody .= "Debug backtrace:\n <pre>" . htmlspecialchars_adv($sBackTrace) . "</pre> ";
  428. if ($sParamsOutput)
  429. $sMailBody .= "<hr />Settings:\n <pre>" . htmlspecialchars_adv($sParamsOutput) . "</pre> ";
  430. $sMailBody .= "<hr />Called script: " . $_SERVER['PHP_SELF'] . "<br /> ";
  431. $sMailBody .= "<hr />Request parameters: <pre>" . print_r( $_REQUEST, true ) . " </pre>";
  432. $sMailBody .= "--\nAuto-report system\n";
  433. sendMail(getParam('site_email_bug_report'), "Database error in " . $sSiteTitle, $sMailBody, 0, array(), BX_EMAIL_SYSTEM, 'html', true);
  434. }
  435. exit;
  436. }
  437. function setErrorChecking ($b) {
  438. $this->error_checking = $b;
  439. }
  440. function getDbCacheObject () {
  441. if ($this->oDbCacheObject != null) {
  442. return $this->oDbCacheObject;
  443. } else {
  444. $sEngine = getParam('sys_db_cache_engine');
  445. $this->oDbCacheObject = bx_instance ('BxDolCache'.$sEngine);
  446. if (!$this->oDbCacheObject->isAvailable())
  447. $this->oDbCacheObject = bx_instance ('BxDolCacheFile');
  448. return $this->oDbCacheObject;
  449. }
  450. }
  451. function genDbCacheKey ($sName) {
  452. return 'db_' . $sName . '_' . md5(BX_DOL_VERSION . BX_DOL_BUILD . BX_DOL_URL_ROOT) . '.php';
  453. }
  454. function fromCache ($sName, $sFunc) {
  455. $aArgs = func_get_args();
  456. array_shift ($aArgs); // shift $sName
  457. array_shift ($aArgs); // shift $sFunc
  458. if (!getParam('sys_db_cache_enable'))
  459. return call_user_func_array (array ($this, $sFunc), $aArgs); // pass other function parameters as database function parameters
  460. $oCache = $this->getDbCacheObject ();
  461. $sKey = $this->genDbCacheKey($sName);
  462. $mixedRet = $oCache->getData($sKey);
  463. if ($mixedRet !== null) {
  464. return $mixedRet;
  465. } else {
  466. $mixedRet = call_user_func_array (array ($this, $sFunc), $aArgs); // pass other function parameters as database function parameters
  467. $oCache->setData($sKey, $mixedRet);
  468. }
  469. return $mixedRet;
  470. }
  471. function cleanCache ($sName) {
  472. if (!getParam('sys_db_cache_enable'))
  473. return true;
  474. $oCache = $this->getDbCacheObject ();
  475. $sKey = $this->genDbCacheKey($sName);
  476. return $oCache->delData($sKey);
  477. }
  478. function & fromMemory ($sName, $sFunc) {
  479. if (array_key_exists($sName, $GLOBALS['gl_db_cache'])) {
  480. return $GLOBALS['gl_db_cache'][$sName];
  481. } else {
  482. $aArgs = func_get_args();
  483. array_shift ($aArgs); // shift $sName
  484. array_shift ($aArgs); // shift $sFunc
  485. $GLOBALS['gl_db_cache'][$sName] = call_user_func_array (array ($this, $sFunc), $aArgs); // pass other function parameters as database function parameters
  486. return $GLOBALS['gl_db_cache'][$sName];
  487. }
  488. }
  489. function cleanMemory ($sName) {
  490. if (isset($GLOBALS['gl_db_cache'][$sName])) {
  491. unset($GLOBALS['gl_db_cache'][$sName]);
  492. return true;
  493. }
  494. return false;
  495. }
  496. /**
  497. * It escapes string to pass to mysql query.
  498. * Try to use "prepare" function always (@see BxDolDb::prepare), use "escape" only if "prepare" function is not possible at all.
  499. * Also consider using "implode_escape" function (@see BxDolDb::implode_escape).
  500. *
  501. * @param string $s string to escape
  502. * @return escaped string whcich is ready to pass to SQL query.
  503. */
  504. function escape ($s) {
  505. return mysql_real_escape_string($s);
  506. }
  507. /**
  508. * This function is usefull when you need to form array of parameters to pass to IN(...) SQL construction.
  509. * Example:
  510. * @code
  511. * $a = array(2, 4.5, 'apple', 'car');
  512. * $s = "SELECT * FROM `t` WHERE `a` IN (" . $oDb->implode_escape($a) . ")";
  513. * echo $s; // outputs: SELECT * FROM `t` WHERE `a` IN (2, 4.5, 'apple', 'car')
  514. * @endcode
  515. *
  516. * @param $mixed array or parameters or just one paramter
  517. * @return string which is ready to pass to IN(...) SQL construction
  518. */
  519. function implode_escape ($mixed) {
  520. if (is_array($mixed)) {
  521. $s = '';
  522. foreach ($mixed as $v)
  523. $s .= is_numeric($v) ? $v : "'" . mysql_real_escape_string($v) . "',";
  524. if ($s)
  525. return substr($s, 0, -1);
  526. else
  527. return 'NULL';
  528. }
  529. return is_numeric($mixed) ? $mixed : ($mixed ? "'" . mysql_real_escape_string($mixed) . "'" : 'NULL');
  530. }
  531. /**
  532. * @deprecated
  533. */
  534. function unescape ($mixed) {
  535. if (is_array($mixed)) {
  536. foreach ($mixed as $k => $v)
  537. $mixed[$k] = $this->getOne("SELECT '$v'");
  538. return $mixed;
  539. } else {
  540. return $this->getOne("SELECT '$mixed'");
  541. }
  542. }
  543. /**
  544. * Prepare SQL query before execution if some arguments are need to be passed to it.
  545. * All parameters marked with question (?) symbol in SQL query are replaced with parameters passed after SQL query parameter.
  546. * Parameters are properly excaped and surrounded by qutes if needed.
  547. * Example:
  548. * @code
  549. * $sSql = $oDb->prepare("SELECT `a`, `b` from `t` WHERE `c` = ? and `d` = ?", 12, 'aa');
  550. * echo $sSql;// outputs: SELECT `a`, `b` from `t` WHERE `c` = 12 and `d` = 'aa'
  551. * $a = $oDb->getAll($sSql);
  552. * @endcode
  553. *
  554. * @param string $sQuery SQL query, parameters for replacing are marked with ? symbol
  555. * @param mixed $mixed any number if parameters to replace, number of parameters whould match number of ? symbols in SQL query
  556. * @return string with SQL query ready for execution
  557. */
  558. function prepare ($sQuery) {
  559. $aArgs = func_get_args();
  560. $sQuery = array_shift($aArgs);
  561. $iPos = 0;
  562. foreach ($aArgs as $mixedArg) {
  563. if (is_null($mixedArg))
  564. $s = 'NULL';
  565. elseif (is_numeric($mixedArg))
  566. $s = $mixedArg;
  567. else
  568. $s = "'" . mysql_real_escape_string($mixedArg) . "'";
  569. $i = bx_mb_strpos($sQuery, '?', $iPos);
  570. $sQuery = bx_mb_substr_replace($sQuery, $s, $i, 1);
  571. $iPos = $i + get_mb_len($s);
  572. }
  573. return $sQuery;
  574. }
  575. function log ($s) {
  576. return file_put_contents(BX_DIRECTORY_PATH_ROOT . 'tmp/db.err.log', date('Y-m-d H:i:s') . "\t" . $s . "\n", FILE_APPEND);
  577. }
  578. }
  579. /** @} */