Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

842 lines
20KB

  1. <?php
  2. require_once("dbm.php");
  3. require_once('include/datetime.php');
  4. /**
  5. * @class MySQL database class
  6. *
  7. * For debugging, insert 'dbg(1);' anywhere in the program flow.
  8. * dbg(0); will turn it off. Logging is performed at LOGGER_DATA level.
  9. * When logging, all binary info is converted to text and html entities are escaped so that
  10. * the debugging stream is safe to view within both terminals and web pages.
  11. *
  12. */
  13. class dba {
  14. private $debug = 0;
  15. //private $db;
  16. public $db;
  17. private $result;
  18. //private $driver;
  19. public $driver;
  20. public $connected = false;
  21. public $error = false;
  22. private $_server_info = '';
  23. private static $dbo;
  24. function __construct($server, $user, $pass, $db, $install = false) {
  25. $a = get_app();
  26. $stamp1 = microtime(true);
  27. $server = trim($server);
  28. $user = trim($user);
  29. $pass = trim($pass);
  30. $db = trim($db);
  31. if (!(strlen($server) && strlen($user))) {
  32. $this->connected = false;
  33. $this->db = null;
  34. return;
  35. }
  36. if ($install) {
  37. if (strlen($server) && ($server !== 'localhost') && ($server !== '127.0.0.1')) {
  38. if (! dns_get_record($server, DNS_A + DNS_CNAME + DNS_PTR)) {
  39. $this->error = sprintf(t('Cannot locate DNS info for database server \'%s\''), $server);
  40. $this->connected = false;
  41. $this->db = null;
  42. return;
  43. }
  44. }
  45. }
  46. if (class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
  47. $this->driver = 'pdo';
  48. $connect = "mysql:host=".$server.";dbname=".$db;
  49. if (isset($a->config["system"]["db_charset"])) {
  50. $connect .= ";charset=".$a->config["system"]["db_charset"];
  51. }
  52. $this->db = @new PDO($connect, $user, $pass);
  53. if (!$this->db->errorCode()) {
  54. $this->connected = true;
  55. }
  56. } elseif (class_exists('mysqli')) {
  57. $this->driver = 'mysqli';
  58. $this->db = @new mysqli($server,$user,$pass,$db);
  59. if (!mysqli_connect_errno()) {
  60. $this->connected = true;
  61. if (isset($a->config["system"]["db_charset"])) {
  62. $this->db->set_charset($a->config["system"]["db_charset"]);
  63. }
  64. }
  65. } elseif (function_exists('mysql_connect')) {
  66. $this->driver = 'mysql';
  67. $this->db = mysql_connect($server,$user,$pass);
  68. if ($this->db && mysql_select_db($db,$this->db)) {
  69. $this->connected = true;
  70. if (isset($a->config["system"]["db_charset"])) {
  71. mysql_set_charset($a->config["system"]["db_charset"], $this->db);
  72. }
  73. }
  74. } else {
  75. // No suitable SQL driver was found.
  76. if (!$install) {
  77. system_unavailable();
  78. }
  79. }
  80. if (!$this->connected) {
  81. $this->db = null;
  82. if (!$install) {
  83. system_unavailable();
  84. }
  85. }
  86. $a->save_timestamp($stamp1, "network");
  87. self::$dbo = $this;
  88. }
  89. /**
  90. * @brief Returns the MySQL server version string
  91. *
  92. * This function discriminate between the deprecated mysql API and the current
  93. * object-oriented mysqli API. Example of returned string: 5.5.46-0+deb8u1
  94. *
  95. * @return string
  96. */
  97. public function server_info() {
  98. if ($this->_server_info == '') {
  99. switch ($this->driver) {
  100. case 'pdo':
  101. $this->_server_info = $this->db->getAttribute(PDO::ATTR_SERVER_VERSION);
  102. break;
  103. case 'mysqli':
  104. $this->_server_info = $this->db->server_info;
  105. break;
  106. case 'mysql':
  107. $this->_server_info = mysql_get_server_info($this->db);
  108. break;
  109. }
  110. }
  111. return $this->_server_info;
  112. }
  113. /**
  114. * @brief Returns the selected database name
  115. *
  116. * @return string
  117. */
  118. public function database_name() {
  119. $r = $this->q("SELECT DATABASE() AS `db`");
  120. return $r[0]['db'];
  121. }
  122. /**
  123. * @brief Returns the number of rows
  124. *
  125. * @return integer
  126. */
  127. public function num_rows() {
  128. if (!$this->result) {
  129. return 0;
  130. }
  131. switch ($this->driver) {
  132. case 'pdo':
  133. $rows = $this->result->rowCount();
  134. break;
  135. case 'mysqli':
  136. $rows = $this->result->num_rows;
  137. break;
  138. case 'mysql':
  139. $rows = mysql_num_rows($this->result);
  140. break;
  141. }
  142. return $rows;
  143. }
  144. /**
  145. * @brief Analyze a database query and log this if some conditions are met.
  146. *
  147. * @param string $query The database query that will be analyzed
  148. */
  149. public function log_index($query) {
  150. $a = get_app();
  151. if ($a->config["system"]["db_log_index"] == "") {
  152. return;
  153. }
  154. // Don't explain an explain statement
  155. if (strtolower(substr($query, 0, 7)) == "explain") {
  156. return;
  157. }
  158. // Only do the explain on "select", "update" and "delete"
  159. if (!in_array(strtolower(substr($query, 0, 6)), array("select", "update", "delete"))) {
  160. return;
  161. }
  162. $r = $this->q("EXPLAIN ".$query);
  163. if (!dbm::is_result($r)) {
  164. return;
  165. }
  166. $watchlist = explode(',', $a->config["system"]["db_log_index_watch"]);
  167. $blacklist = explode(',', $a->config["system"]["db_log_index_blacklist"]);
  168. foreach ($r AS $row) {
  169. if ((intval($a->config["system"]["db_loglimit_index"]) > 0)) {
  170. $log = (in_array($row['key'], $watchlist) AND
  171. ($row['rows'] >= intval($a->config["system"]["db_loglimit_index"])));
  172. } else {
  173. $log = false;
  174. }
  175. if ((intval($a->config["system"]["db_loglimit_index_high"]) > 0) AND ($row['rows'] >= intval($a->config["system"]["db_loglimit_index_high"]))) {
  176. $log = true;
  177. }
  178. if (in_array($row['key'], $blacklist) OR ($row['key'] == "")) {
  179. $log = false;
  180. }
  181. if ($log) {
  182. $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
  183. @file_put_contents($a->config["system"]["db_log_index"], datetime_convert()."\t".
  184. $row['key']."\t".$row['rows']."\t".$row['Extra']."\t".
  185. basename($backtrace[1]["file"])."\t".
  186. $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
  187. substr($query, 0, 2000)."\n", FILE_APPEND);
  188. }
  189. }
  190. }
  191. public function q($sql, $onlyquery = false) {
  192. $a = get_app();
  193. if (!$this->db || !$this->connected) {
  194. return false;
  195. }
  196. $this->error = '';
  197. $connstr = ($this->connected() ? "Connected" : "Disonnected");
  198. $stamp1 = microtime(true);
  199. $orig_sql = $sql;
  200. if (x($a->config,'system') && x($a->config['system'], 'db_callstack')) {
  201. $sql = "/*".$a->callstack()." */ ".$sql;
  202. }
  203. $columns = 0;
  204. switch ($this->driver) {
  205. case 'pdo':
  206. $result = @$this->db->query($sql);
  207. // Is used to separate between queries that returning data - or not
  208. if (!is_bool($result)) {
  209. $columns = $result->columnCount();
  210. }
  211. break;
  212. case 'mysqli':
  213. $result = @$this->db->query($sql);
  214. break;
  215. case 'mysql':
  216. $result = @mysql_query($sql,$this->db);
  217. break;
  218. }
  219. $stamp2 = microtime(true);
  220. $duration = (float)($stamp2-$stamp1);
  221. $a->save_timestamp($stamp1, "database");
  222. if (strtolower(substr($orig_sql, 0, 6)) != "select") {
  223. $a->save_timestamp($stamp1, "database_write");
  224. }
  225. if (x($a->config,'system') && x($a->config['system'],'db_log')) {
  226. if (($duration > $a->config["system"]["db_loglimit"])) {
  227. $duration = round($duration, 3);
  228. $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
  229. @file_put_contents($a->config["system"]["db_log"], datetime_convert()."\t".$duration."\t".
  230. basename($backtrace[1]["file"])."\t".
  231. $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
  232. substr($sql, 0, 2000)."\n", FILE_APPEND);
  233. }
  234. }
  235. switch ($this->driver) {
  236. case 'pdo':
  237. $errorInfo = $this->db->errorInfo();
  238. if ($errorInfo) {
  239. $this->error = $errorInfo[2];
  240. $this->errorno = $errorInfo[1];
  241. }
  242. break;
  243. case 'mysqli':
  244. if ($this->db->errno) {
  245. $this->error = $this->db->error;
  246. $this->errorno = $this->db->errno;
  247. }
  248. break;
  249. case 'mysql':
  250. if (mysql_errno($this->db)) {
  251. $this->error = mysql_error($this->db);
  252. $this->errorno = mysql_errno($this->db);
  253. }
  254. break;
  255. }
  256. if (strlen($this->error)) {
  257. logger('DB Error ('.$connstr.') '.$this->errorno.': '.$this->error);
  258. }
  259. if ($this->debug) {
  260. $mesg = '';
  261. if ($result === false) {
  262. $mesg = 'false';
  263. } elseif ($result === true) {
  264. $mesg = 'true';
  265. } else {
  266. switch ($this->driver) {
  267. case 'pdo':
  268. $mesg = $result->rowCount().' results'.EOL;
  269. break;
  270. case 'mysqli':
  271. $mesg = $result->num_rows.' results'.EOL;
  272. break;
  273. case 'mysql':
  274. $mesg = mysql_num_rows($result).' results'.EOL;
  275. break;
  276. }
  277. }
  278. $str = 'SQL = ' . printable($sql) . EOL . 'SQL returned ' . $mesg
  279. . (($this->error) ? ' error: ' . $this->error : '')
  280. . EOL;
  281. logger('dba: ' . $str );
  282. }
  283. /**
  284. * If dbfail.out exists, we will write any failed calls directly to it,
  285. * regardless of any logging that may or may nor be in effect.
  286. * These usually indicate SQL syntax errors that need to be resolved.
  287. */
  288. if ($result === false) {
  289. logger('dba: ' . printable($sql) . ' returned false.' . "\n" . $this->error);
  290. if (file_exists('dbfail.out')) {
  291. file_put_contents('dbfail.out', datetime_convert() . "\n" . printable($sql) . ' returned false' . "\n" . $this->error . "\n", FILE_APPEND);
  292. }
  293. }
  294. if (is_bool($result)) {
  295. return $result;
  296. }
  297. if ($onlyquery) {
  298. $this->result = $result;
  299. return true;
  300. }
  301. $r = array();
  302. switch ($this->driver) {
  303. case 'pdo':
  304. while ($x = $result->fetch(PDO::FETCH_ASSOC)) {
  305. $r[] = $x;
  306. }
  307. $result->closeCursor();
  308. break;
  309. case 'mysqli':
  310. while ($x = $result->fetch_array(MYSQLI_ASSOC)) {
  311. $r[] = $x;
  312. }
  313. $result->free_result();
  314. break;
  315. case 'mysql':
  316. while ($x = mysql_fetch_array($result, MYSQL_ASSOC)) {
  317. $r[] = $x;
  318. }
  319. mysql_free_result($result);
  320. break;
  321. }
  322. // PDO doesn't return "true" on successful operations - like mysqli does
  323. // Emulate this behaviour by checking if the query returned data and had columns
  324. // This should be reliable enough
  325. if (($this->driver == 'pdo') AND (count($r) == 0) AND ($columns == 0)) {
  326. return true;
  327. }
  328. //$a->save_timestamp($stamp1, "database");
  329. if ($this->debug) {
  330. logger('dba: ' . printable(print_r($r, true)));
  331. }
  332. return($r);
  333. }
  334. public function qfetch() {
  335. $x = false;
  336. if ($this->result) {
  337. switch ($this->driver) {
  338. case 'pdo':
  339. $x = $this->result->fetch(PDO::FETCH_ASSOC);
  340. break;
  341. case 'mysqli':
  342. $x = $this->result->fetch_array(MYSQLI_ASSOC);
  343. break;
  344. case 'mysql':
  345. $x = mysql_fetch_array($this->result, MYSQL_ASSOC);
  346. break;
  347. }
  348. }
  349. return($x);
  350. }
  351. public function qclose() {
  352. if ($this->result) {
  353. switch ($this->driver) {
  354. case 'pdo':
  355. $this->result->closeCursor();
  356. break;
  357. case 'mysqli':
  358. $this->result->free_result();
  359. break;
  360. case 'mysql':
  361. mysql_free_result($this->result);
  362. break;
  363. }
  364. }
  365. }
  366. public function dbg($dbg) {
  367. $this->debug = $dbg;
  368. }
  369. public function escape($str) {
  370. if ($this->db && $this->connected) {
  371. switch ($this->driver) {
  372. case 'pdo':
  373. return substr(@$this->db->quote($str, PDO::PARAM_STR), 1, -1);
  374. case 'mysqli':
  375. return @$this->db->real_escape_string($str);
  376. case 'mysql':
  377. return @mysql_real_escape_string($str,$this->db);
  378. }
  379. }
  380. }
  381. function connected() {
  382. switch ($this->driver) {
  383. case 'pdo':
  384. // Not sure if this really is working like expected
  385. $connected = ($this->db->getAttribute(PDO::ATTR_CONNECTION_STATUS) != "");
  386. break;
  387. case 'mysqli':
  388. $connected = $this->db->ping();
  389. break;
  390. case 'mysql':
  391. $connected = mysql_ping($this->db);
  392. break;
  393. }
  394. return $connected;
  395. }
  396. function insert_id() {
  397. switch ($this->driver) {
  398. case 'pdo':
  399. $id = $this->db->lastInsertId();
  400. break;
  401. case 'mysqli':
  402. $id = $this->db->insert_id;
  403. break;
  404. case 'mysql':
  405. $id = mysql_insert_id($this->db);
  406. break;
  407. }
  408. return $id;
  409. }
  410. /**
  411. * @brief Replaces ANY_VALUE() function by MIN() function,
  412. * if the database server does not support ANY_VALUE().
  413. *
  414. * Considerations for Standard SQL, or MySQL with ONLY_FULL_GROUP_BY (default since 5.7.5).
  415. * ANY_VALUE() is available from MySQL 5.7.5 https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html
  416. * A standard fall-back is to use MIN().
  417. *
  418. * @param string $sql An SQL string without the values
  419. * @return string The input SQL string modified if necessary.
  420. */
  421. public function any_value_fallback($sql) {
  422. $server_info = $this->server_info();
  423. if (version_compare($server_info, '5.7.5', '<') ||
  424. (stripos($server_info, 'MariaDB') !== false)) {
  425. $sql = str_ireplace('ANY_VALUE(', 'MIN(', $sql);
  426. }
  427. return $sql;
  428. }
  429. function __destruct() {
  430. if ($this->db) {
  431. switch ($this->driver) {
  432. case 'pdo':
  433. $this->db = null;
  434. break;
  435. case 'mysqli':
  436. $this->db->close();
  437. break;
  438. case 'mysql':
  439. mysql_close($this->db);
  440. break;
  441. }
  442. }
  443. }
  444. /**
  445. * @brief Executes a prepared statement
  446. *
  447. * @param string $sql SQL statement
  448. * @return object statement object
  449. */
  450. static public function p($sql) {
  451. $a = get_app();
  452. $stamp1 = microtime(true);
  453. $args = func_get_args();
  454. unset($args[0]);
  455. if (!self::$dbo OR !self::$dbo->connected) {
  456. return false;
  457. }
  458. $sql = self::$dbo->any_value_fallback($sql);
  459. $orig_sql = $sql;
  460. if (x($a->config,'system') && x($a->config['system'], 'db_callstack')) {
  461. $sql = "/*".$a->callstack()." */ ".$sql;
  462. }
  463. switch (self::$dbo->driver) {
  464. case 'pdo':
  465. $stmt = self::$dbo->db->prepare($sql);
  466. foreach ($args AS $param => $value) {
  467. $stmt->bindParam($param, $args[$param]);
  468. }
  469. $success = $stmt->execute();
  470. if ($success) {
  471. $retval = $stmt;
  472. } else {
  473. $retval = false;
  474. }
  475. $errorInfo = self::$dbo->db->errorInfo();
  476. if ($errorInfo) {
  477. self::$dbo->error = $errorInfo[2];
  478. self::$dbo->errorno = $errorInfo[1];
  479. }
  480. break;
  481. case 'mysqli':
  482. $stmt = self::$dbo->db->stmt_init();
  483. if (!$stmt->prepare($sql)) {
  484. self::$dbo->error = self::$dbo->db->error;
  485. self::$dbo->errorno = self::$dbo->db->errno;
  486. $retval = false;
  487. break;
  488. }
  489. $params = '';
  490. $values = array();
  491. foreach ($args AS $param => $value) {
  492. if (is_int($args[$param])) {
  493. $params .= 'i';
  494. } elseif (is_float($args[$param])) {
  495. $params .= 'd';
  496. } elseif (is_string($args[$param])) {
  497. $params .= 's';
  498. } else {
  499. $params .= 'b';
  500. }
  501. $values[] = &$args[$param];
  502. }
  503. array_unshift($values, $params);
  504. call_user_func_array(array($stmt, 'bind_param'), $values);
  505. if (!$stmt->execute()) {
  506. self::$dbo->error = self::$dbo->db->error;
  507. self::$dbo->errorno = self::$dbo->db->errno;
  508. $retval = false;
  509. } elseif (method_exists($stmt, 'get_result')) {
  510. // Is mysqlnd installed?
  511. $retval = $stmt->get_result();
  512. } else {
  513. $retval = $stmt;
  514. }
  515. break;
  516. case 'mysql':
  517. // For the old "mysql" functions we cannot use prepared statements
  518. foreach ($args AS $param => $value) {
  519. if (is_int($args[$param]) OR is_float($args[$param])) {
  520. $replace = intval($args[$param]);
  521. } else {
  522. $replace = "'".dbesc($args[$param])."'";
  523. }
  524. $pos = strpos($sql, '?');
  525. if ($pos !== false) {
  526. $sql = substr_replace($sql, $replace, $pos, 1);
  527. }
  528. }
  529. $retval = mysql_query($sql, self::$dbo->db);
  530. if (mysql_errno(self::$dbo->db)) {
  531. self::$dbo->error = mysql_error(self::$dbo->db);
  532. self::$dbo->errorno = mysql_errno(self::$dbo->db);
  533. }
  534. break;
  535. }
  536. $stamp2 = microtime(true);
  537. $duration = (float)($stamp2 - $stamp1);
  538. $a->save_timestamp($stamp1, 'database');
  539. if (strtolower(substr($orig_sql, 0, 6)) != "select") {
  540. $a->save_timestamp($stamp1, "database_write");
  541. }
  542. return $retval;
  543. }
  544. /**
  545. * @brief Executes a prepared statement
  546. *
  547. * @param string $sql SQL statement
  548. * @return boolean Was the query successfull?
  549. */
  550. static public function e($sql) {
  551. $args = func_get_args();
  552. $stmt = self::p();
  553. self::close($stmt);
  554. }
  555. /**
  556. * @brief Fetch a single row
  557. *
  558. * @param object $stmt statement object
  559. * @return array current row
  560. */
  561. static public function fetch($stmt) {
  562. switch (self::$dbo->driver) {
  563. case 'pdo':
  564. return $stmt->fetch(PDO::FETCH_ASSOC);
  565. case 'mysqli':
  566. // When mysqlnd is installed, we can use a shortcut
  567. if (method_exists($stmt, 'fetch_array')) {
  568. return $stmt->fetch_array(MYSQLI_ASSOC);
  569. }
  570. // This code works, but is slow
  571. // Bind the result to a result array
  572. $cols = array();
  573. $cols_num = array();
  574. for ($x = 0; $x < $stmt->field_count; $x++) {
  575. $cols[] = &$cols_num[$x];
  576. }
  577. call_user_func_array(array($stmt, 'bind_result'), $cols);
  578. $success = $stmt->fetch();
  579. if (!$success) {
  580. return false;
  581. }
  582. // The slow part:
  583. // We need to get the field names for the array keys
  584. // It seems that there is no better way to do this.
  585. $result = $stmt->result_metadata();
  586. $columns = array();
  587. foreach ($cols_num AS $col) {
  588. $field = $result->fetch_field();
  589. $columns[$field->name] = $col;
  590. }
  591. return $columns;
  592. case 'mysql':
  593. return mysql_fetch_array(self::$dbo->result, MYSQL_ASSOC);
  594. }
  595. }
  596. /**
  597. * @brief Closes the current statement
  598. *
  599. * @param object $stmt statement object
  600. * @return boolean was the close successfull?
  601. */
  602. static public function close($stmt) {
  603. switch (self::$dbo->driver) {
  604. case 'pdo':
  605. return $stmt->closeCursor();
  606. case 'mysqli':
  607. return $stmt->close();
  608. case 'mysql':
  609. return mysql_free_result($stmt);
  610. }
  611. }
  612. }
  613. function printable($s) {
  614. $s = preg_replace("~([\x01-\x08\x0E-\x0F\x10-\x1F\x7F-\xFF])~",".", $s);
  615. $s = str_replace("\x00",'.',$s);
  616. if (x($_SERVER,'SERVER_NAME')) {
  617. $s = escape_tags($s);
  618. }
  619. return $s;
  620. }
  621. // Procedural functions
  622. function dbg($state) {
  623. global $db;
  624. if ($db) {
  625. $db->dbg($state);
  626. }
  627. }
  628. function dbesc($str) {
  629. global $db;
  630. if ($db && $db->connected) {
  631. return($db->escape($str));
  632. } else {
  633. return(str_replace("'","\\'",$str));
  634. }
  635. }
  636. // Function: q($sql,$args);
  637. // Description: execute SQL query with printf style args.
  638. // Example: $r = q("SELECT * FROM `%s` WHERE `uid` = %d",
  639. // 'user', 1);
  640. function q($sql) {
  641. global $db;
  642. if ($db && $db->connected) {
  643. $sql = $db->any_value_fallback($sql);
  644. $stmt = @vsprintf($sql,$args); // Disabled warnings
  645. //logger("dba: q: $stmt", LOGGER_ALL);
  646. if ($stmt === false)
  647. logger('dba: vsprintf error: ' . print_r(debug_backtrace(),true), LOGGER_DEBUG);
  648. $db->log_index($stmt);
  649. return $db->q($stmt);
  650. }
  651. /**
  652. *
  653. * This will happen occasionally trying to store the
  654. * session data after abnormal program termination
  655. *
  656. */
  657. logger('dba: no database: ' . print_r($args,true));
  658. return false;
  659. }
  660. /**
  661. * @brief Performs a query with "dirty reads"
  662. *
  663. * By doing dirty reads (reading uncommitted data) no locks are performed
  664. * This function can be used to fetch data that doesn't need to be reliable.
  665. *
  666. * @param $args Query parameters (1 to N parameters of different types)
  667. * @return array Query array
  668. */
  669. function qu($sql) {
  670. global $db;
  671. $args = func_get_args();
  672. unset($args[0]);
  673. if ($db && $db->connected) {
  674. $sql = $db->any_value_fallback($sql);
  675. $stmt = @vsprintf($sql,$args); // Disabled warnings
  676. if ($stmt === false)
  677. logger('dba: vsprintf error: ' . print_r(debug_backtrace(),true), LOGGER_DEBUG);
  678. $db->log_index($stmt);
  679. $db->q("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;");
  680. $retval = $db->q($stmt);
  681. $db->q("SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;");
  682. return $retval;
  683. }
  684. /**
  685. *
  686. * This will happen occasionally trying to store the
  687. * session data after abnormal program termination
  688. *
  689. */
  690. logger('dba: no database: ' . print_r($args,true));
  691. return false;
  692. }
  693. /**
  694. *
  695. * Raw db query, no arguments
  696. *
  697. */
  698. function dbq($sql) {
  699. global $db;
  700. if ($db && $db->connected) {
  701. $ret = $db->q($sql);
  702. } else {
  703. $ret = false;
  704. }
  705. return $ret;
  706. }
  707. // Caller is responsible for ensuring that any integer arguments to
  708. // dbesc_array are actually integers and not malformed strings containing
  709. // SQL injection vectors. All integer array elements should be specifically
  710. // cast to int to avoid trouble.
  711. function dbesc_array_cb(&$item, $key) {
  712. if (is_string($item))
  713. $item = dbesc($item);
  714. }
  715. function dbesc_array(&$arr) {
  716. if (is_array($arr) && count($arr)) {
  717. array_walk($arr,'dbesc_array_cb');
  718. }
  719. }
  720. function dba_timer() {
  721. return microtime(true);
  722. }