]> git.mxchange.org Git - friendica.git/blob - include/dba.php
73246d4908530e8c3366a7529776ab8e370497bd
[friendica.git] / include / dba.php
1 <?php
2 use \Friendica\Core\System;
3
4 require_once("dbm.php");
5 require_once('include/datetime.php');
6
7 /**
8  * @class MySQL database class
9  *
10  * This class is for the low level database stuff that does driver specific things.
11  */
12
13 class dba {
14
15         private $debug = 0;
16         private $db;
17         private $result;
18         private $driver;
19         public  $connected = false;
20         public  $error = false;
21         public  $errorno = 0;
22         public  $affected_rows = 0;
23         private $_server_info = '';
24         private static $in_transaction = false;
25         private static $dbo;
26         private static $relation = array();
27
28         function __construct($serveraddr, $user, $pass, $db, $install = false) {
29                 $a = get_app();
30
31                 $stamp1 = microtime(true);
32
33                 $serveraddr = trim($serveraddr);
34
35                 $serverdata = explode(':', $serveraddr);
36                 $server = $serverdata[0];
37
38                 if (count($serverdata) > 1) {
39                         $port = trim($serverdata[1]);
40                 }
41
42                 $server = trim($server);
43                 $user = trim($user);
44                 $pass = trim($pass);
45                 $db = trim($db);
46
47                 if (!(strlen($server) && strlen($user))) {
48                         $this->connected = false;
49                         $this->db = null;
50                         return;
51                 }
52
53                 if ($install) {
54                         if (strlen($server) && ($server !== 'localhost') && ($server !== '127.0.0.1')) {
55                                 if (! dns_get_record($server, DNS_A + DNS_CNAME + DNS_PTR)) {
56                                         $this->error = sprintf(t('Cannot locate DNS info for database server \'%s\''), $server);
57                                         $this->connected = false;
58                                         $this->db = null;
59                                         return;
60                                 }
61                         }
62                 }
63
64                 if (class_exists('\PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
65                         $this->driver = 'pdo';
66                         $connect = "mysql:host=".$server.";dbname=".$db;
67
68                         if (isset($port)) {
69                                 $connect .= ";port=".$port;
70                         }
71
72                         if (isset($a->config["system"]["db_charset"])) {
73                                 $connect .= ";charset=".$a->config["system"]["db_charset"];
74                         }
75                         try {
76                                 $this->db = @new PDO($connect, $user, $pass);
77                                 $this->connected = true;
78                         } catch (PDOException $e) {
79                                 $this->connected = false;
80                         }
81                 }
82
83                 if (!$this->connected && class_exists('mysqli')) {
84                         $this->driver = 'mysqli';
85                         $this->db = @new mysqli($server, $user, $pass, $db, $port);
86                         if (!mysqli_connect_errno()) {
87                                 $this->connected = true;
88
89                                 if (isset($a->config["system"]["db_charset"])) {
90                                         $this->db->set_charset($a->config["system"]["db_charset"]);
91                                 }
92                         }
93                 }
94
95                 if (!$this->connected && function_exists('mysql_connect')) {
96                         $this->driver = 'mysql';
97                         $this->db = mysql_connect($serveraddr, $user, $pass);
98                         if ($this->db && mysql_select_db($db, $this->db)) {
99                                 $this->connected = true;
100
101                                 if (isset($a->config["system"]["db_charset"])) {
102                                         mysql_set_charset($a->config["system"]["db_charset"], $this->db);
103                                 }
104                         }
105                 }
106
107                 // No suitable SQL driver was found.
108                 if (!$this->connected) {
109                         $this->db = null;
110                         if (!$install) {
111                                 system_unavailable();
112                         }
113                 }
114                 $a->save_timestamp($stamp1, "network");
115
116                 self::$dbo = $this;
117         }
118
119         /**
120          * @brief Checks if the database object is initialized
121          *
122          * This is a possible bugfix for something that doesn't occur for me.
123          * There seems to be situations, where the object isn't initialized.
124          */
125         private static function initialize() {
126                 if (!is_object(self::$dbo)) {
127                         global $db;
128                         self::$dbo = $db;
129                         if (!is_object(self::$dbo)) {
130                                 die('Database is uninitialized!');
131                         }
132                 }
133         }
134
135         /**
136          * @brief Returns the MySQL server version string
137          *
138          * This function discriminate between the deprecated mysql API and the current
139          * object-oriented mysqli API. Example of returned string: 5.5.46-0+deb8u1
140          *
141          * @return string
142          */
143         public function server_info() {
144                 if ($this->_server_info == '') {
145                         switch ($this->driver) {
146                                 case 'pdo':
147                                         $this->_server_info = $this->db->getAttribute(PDO::ATTR_SERVER_VERSION);
148                                         break;
149                                 case 'mysqli':
150                                         $this->_server_info = $this->db->server_info;
151                                         break;
152                                 case 'mysql':
153                                         $this->_server_info = mysql_get_server_info($this->db);
154                                         break;
155                         }
156                 }
157                 return $this->_server_info;
158         }
159
160         /**
161          * @brief Returns the selected database name
162          *
163          * @return string
164          */
165         public function database_name() {
166                 $r = $this->q("SELECT DATABASE() AS `db`");
167
168                 return $r[0]['db'];
169         }
170
171         /**
172          * @brief Analyze a database query and log this if some conditions are met.
173          *
174          * @param string $query The database query that will be analyzed
175          */
176         public function log_index($query) {
177                 $a = get_app();
178
179                 if (empty($a->config["system"]["db_log_index"])) {
180                         return;
181                 }
182
183                 // Don't explain an explain statement
184                 if (strtolower(substr($query, 0, 7)) == "explain") {
185                         return;
186                 }
187
188                 // Only do the explain on "select", "update" and "delete"
189                 if (!in_array(strtolower(substr($query, 0, 6)), array("select", "update", "delete"))) {
190                         return;
191                 }
192
193                 $r = $this->q("EXPLAIN ".$query);
194                 if (!dbm::is_result($r)) {
195                         return;
196                 }
197
198                 $watchlist = explode(',', $a->config["system"]["db_log_index_watch"]);
199                 $blacklist = explode(',', $a->config["system"]["db_log_index_blacklist"]);
200
201                 foreach ($r AS $row) {
202                         if ((intval($a->config["system"]["db_loglimit_index"]) > 0)) {
203                                 $log = (in_array($row['key'], $watchlist) &&
204                                         ($row['rows'] >= intval($a->config["system"]["db_loglimit_index"])));
205                         } else {
206                                 $log = false;
207                         }
208
209                         if ((intval($a->config["system"]["db_loglimit_index_high"]) > 0) && ($row['rows'] >= intval($a->config["system"]["db_loglimit_index_high"]))) {
210                                 $log = true;
211                         }
212
213                         if (in_array($row['key'], $blacklist) || ($row['key'] == "")) {
214                                 $log = false;
215                         }
216
217                         if ($log) {
218                                 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
219                                 @file_put_contents($a->config["system"]["db_log_index"], datetime_convert()."\t".
220                                                 $row['key']."\t".$row['rows']."\t".$row['Extra']."\t".
221                                                 basename($backtrace[1]["file"])."\t".
222                                                 $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
223                                                 substr($query, 0, 2000)."\n", FILE_APPEND);
224                         }
225                 }
226         }
227
228         /**
229          * @brief execute SQL query - deprecated
230          *
231          * Please use the dba:: functions instead:
232          * dba::select, dba::exists, dba::insert
233          * dba::delete, dba::update, dba::p, dba::e
234          *
235          * @param string $sql SQL query
236          * @return array Query array
237          */
238         public function q($sql) {
239                 $ret = self::p($sql);
240
241                 if (is_bool($ret)) {
242                         return $ret;
243                 }
244
245                 $columns = self::columnCount($ret);
246
247                 $data = self::inArray($ret);
248
249                 if ((count($data) == 0) && ($columns == 0)) {
250                         return true;
251                 }
252
253                 return $data;
254         }
255
256         public function escape($str) {
257                 if ($this->db && $this->connected) {
258                         switch ($this->driver) {
259                                 case 'pdo':
260                                         return substr(@$this->db->quote($str, PDO::PARAM_STR), 1, -1);
261                                 case 'mysqli':
262                                         return @$this->db->real_escape_string($str);
263                                 case 'mysql':
264                                         return @mysql_real_escape_string($str,$this->db);
265                         }
266                 }
267         }
268
269         function connected() {
270                 switch ($this->driver) {
271                         case 'pdo':
272                                 // Not sure if this really is working like expected
273                                 $connected = ($this->db->getAttribute(PDO::ATTR_CONNECTION_STATUS) != "");
274                                 break;
275                         case 'mysqli':
276                                 $connected = $this->db->ping();
277                                 break;
278                         case 'mysql':
279                                 $connected = mysql_ping($this->db);
280                                 break;
281                 }
282                 return $connected;
283         }
284
285         function __destruct() {
286                 if ($this->db) {
287                         switch ($this->driver) {
288                                 case 'pdo':
289                                         $this->db = null;
290                                         break;
291                                 case 'mysqli':
292                                         $this->db->close();
293                                         break;
294                                 case 'mysql':
295                                         mysql_close($this->db);
296                                         break;
297                         }
298                 }
299         }
300
301         /**
302          * @brief Replaces ANY_VALUE() function by MIN() function,
303          *  if the database server does not support ANY_VALUE().
304          *
305          * Considerations for Standard SQL, or MySQL with ONLY_FULL_GROUP_BY (default since 5.7.5).
306          * ANY_VALUE() is available from MySQL 5.7.5 https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html
307          * A standard fall-back is to use MIN().
308          *
309          * @param string $sql An SQL string without the values
310          * @return string The input SQL string modified if necessary.
311          */
312         public function any_value_fallback($sql) {
313                 $server_info = $this->server_info();
314                 if (version_compare($server_info, '5.7.5', '<') ||
315                         (stripos($server_info, 'MariaDB') !== false)) {
316                         $sql = str_ireplace('ANY_VALUE(', 'MIN(', $sql);
317                 }
318                 return $sql;
319         }
320
321         /**
322          * @brief beautifies the query - useful for "SHOW PROCESSLIST"
323          *
324          * This is safe when we bind the parameters later.
325          * The parameter values aren't part of the SQL.
326          *
327          * @param string $sql An SQL string without the values
328          * @return string The input SQL string modified if necessary.
329          */
330         public function clean_query($sql) {
331                 $search = array("\t", "\n", "\r", "  ");
332                 $replace = array(' ', ' ', ' ', ' ');
333                 do {
334                         $oldsql = $sql;
335                         $sql = str_replace($search, $replace, $sql);
336                 } while ($oldsql != $sql);
337
338                 return $sql;
339         }
340
341
342         /**
343          * @brief Replaces the ? placeholders with the parameters in the $args array
344          *
345          * @param string $sql SQL query
346          * @param array $args The parameters that are to replace the ? placeholders
347          * @return string The replaced SQL query
348          */
349         private static function replace_parameters($sql, $args) {
350                 $offset = 0;
351                 foreach ($args AS $param => $value) {
352                         if (is_int($args[$param]) || is_float($args[$param])) {
353                                 $replace = intval($args[$param]);
354                         } else {
355                                 $replace = "'".self::$dbo->escape($args[$param])."'";
356                         }
357
358                         $pos = strpos($sql, '?', $offset);
359                         if ($pos !== false) {
360                                 $sql = substr_replace($sql, $replace, $pos, 1);
361                         }
362                         $offset = $pos + strlen($replace);
363                 }
364                 return $sql;
365         }
366
367         /**
368          * @brief Convert parameter array to an universal form
369          * @param array $args Parameter array
370          * @return array universalized parameter array
371          */
372         private static function getParam($args) {
373                 unset($args[0]);
374
375                 // When the second function parameter is an array then use this as the parameter array
376                 if ((count($args) > 0) && (is_array($args[1]))) {
377                         return $args[1];
378                 } else {
379                         return $args;
380                 }
381         }
382
383         /**
384          * @brief Executes a prepared statement that returns data
385          * @usage Example: $r = p("SELECT * FROM `item` WHERE `guid` = ?", $guid);
386          *
387          * Please only use it with complicated queries.
388          * For all regular queries please use dba::select or dba::exists
389          *
390          * @param string $sql SQL statement
391          * @return object statement object
392          */
393         public static function p($sql) {
394                 self::initialize();
395
396                 $a = get_app();
397
398                 $stamp1 = microtime(true);
399
400                 $params = self::getParam(func_get_args());
401
402                 // Renumber the array keys to be sure that they fit
403                 $i = 0;
404                 $args = array();
405                 foreach ($params AS $param) {
406                         // Avoid problems with some MySQL servers and boolean values. See issue #3645
407                         if (is_bool($param)) {
408                                 $param = (int)$param;
409                         }
410                         $args[++$i] = $param;
411                 }
412
413                 if (!self::$dbo || !self::$dbo->connected) {
414                         return false;
415                 }
416
417                 if ((substr_count($sql, '?') != count($args)) && (count($args) > 0)) {
418                         // Question: Should we continue or stop the query here?
419                         logger('Parameter mismatch. Query "'.$sql.'" - Parameters '.print_r($args, true), LOGGER_DEBUG);
420                 }
421
422                 $sql = self::$dbo->clean_query($sql);
423                 $sql = self::$dbo->any_value_fallback($sql);
424
425                 $orig_sql = $sql;
426
427                 if (x($a->config,'system') && x($a->config['system'], 'db_callstack')) {
428                         $sql = "/*".System::callstack()." */ ".$sql;
429                 }
430
431                 self::$dbo->error = '';
432                 self::$dbo->errorno = 0;
433                 self::$dbo->affected_rows = 0;
434
435                 // We have to make some things different if this function is called from "e"
436                 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
437
438                 if (isset($trace[1])) {
439                         $called_from = $trace[1];
440                 } else {
441                         // We use just something that is defined to avoid warnings
442                         $called_from = $trace[0];
443                 }
444                 // We are having an own error logging in the function "e"
445                 $called_from_e = ($called_from['function'] == 'e');
446
447                 switch (self::$dbo->driver) {
448                         case 'pdo':
449                                 // If there are no arguments we use "query"
450                                 if (count($args) == 0) {
451                                         if (!$retval = self::$dbo->db->query($sql)) {
452                                                 $errorInfo = self::$dbo->db->errorInfo();
453                                                 self::$dbo->error = $errorInfo[2];
454                                                 self::$dbo->errorno = $errorInfo[1];
455                                                 $retval = false;
456                                                 break;
457                                         }
458                                         self::$dbo->affected_rows = $retval->rowCount();
459                                         break;
460                                 }
461
462                                 if (!$stmt = self::$dbo->db->prepare($sql)) {
463                                         $errorInfo = self::$dbo->db->errorInfo();
464                                         self::$dbo->error = $errorInfo[2];
465                                         self::$dbo->errorno = $errorInfo[1];
466                                         $retval = false;
467                                         break;
468                                 }
469
470                                 foreach ($args AS $param => $value) {
471                                         $stmt->bindParam($param, $args[$param]);
472                                 }
473
474                                 if (!$stmt->execute()) {
475                                         $errorInfo = $stmt->errorInfo();
476                                         self::$dbo->error = $errorInfo[2];
477                                         self::$dbo->errorno = $errorInfo[1];
478                                         $retval = false;
479                                 } else {
480                                         $retval = $stmt;
481                                         self::$dbo->affected_rows = $retval->rowCount();
482                                 }
483                                 break;
484                         case 'mysqli':
485                                 // There are SQL statements that cannot be executed with a prepared statement
486                                 $parts = explode(' ', $orig_sql);
487                                 $command = strtolower($parts[0]);
488                                 $can_be_prepared = in_array($command, array('select', 'update', 'insert', 'delete'));
489
490                                 // The fallback routine is called as well when there are no arguments
491                                 if (!$can_be_prepared || (count($args) == 0)) {
492                                         $retval = self::$dbo->db->query(self::replace_parameters($sql, $args));
493                                         if (self::$dbo->db->errno) {
494                                                 self::$dbo->error = self::$dbo->db->error;
495                                                 self::$dbo->errorno = self::$dbo->db->errno;
496                                                 $retval = false;
497                                         } else {
498                                                 if (isset($retval->num_rows)) {
499                                                         self::$dbo->affected_rows = $retval->num_rows;
500                                                 } else {
501                                                         self::$dbo->affected_rows = self::$dbo->db->affected_rows;
502                                                 }
503                                         }
504                                         break;
505                                 }
506
507                                 $stmt = self::$dbo->db->stmt_init();
508
509                                 if (!$stmt->prepare($sql)) {
510                                         self::$dbo->error = $stmt->error;
511                                         self::$dbo->errorno = $stmt->errno;
512                                         $retval = false;
513                                         break;
514                                 }
515
516                                 $params = '';
517                                 $values = array();
518                                 foreach ($args AS $param => $value) {
519                                         if (is_int($args[$param])) {
520                                                 $params .= 'i';
521                                         } elseif (is_float($args[$param])) {
522                                                 $params .= 'd';
523                                         } elseif (is_string($args[$param])) {
524                                                 $params .= 's';
525                                         } else {
526                                                 $params .= 'b';
527                                         }
528                                         $values[] = &$args[$param];
529                                 }
530
531                                 if (count($values) > 0) {
532                                         array_unshift($values, $params);
533                                         call_user_func_array(array($stmt, 'bind_param'), $values);
534                                 }
535
536                                 if (!$stmt->execute()) {
537                                         self::$dbo->error = self::$dbo->db->error;
538                                         self::$dbo->errorno = self::$dbo->db->errno;
539                                         $retval = false;
540                                 } else {
541                                         $stmt->store_result();
542                                         $retval = $stmt;
543                                         self::$dbo->affected_rows = $retval->affected_rows;
544                                 }
545                                 break;
546                         case 'mysql':
547                                 // For the old "mysql" functions we cannot use prepared statements
548                                 $retval = mysql_query(self::replace_parameters($sql, $args), self::$dbo->db);
549                                 if (mysql_errno(self::$dbo->db)) {
550                                         self::$dbo->error = mysql_error(self::$dbo->db);
551                                         self::$dbo->errorno = mysql_errno(self::$dbo->db);
552                                 } else {
553                                         self::$dbo->affected_rows = mysql_affected_rows($retval);
554
555                                         // Due to missing mysql_* support this here wasn't tested at all
556                                         // See here: http://php.net/manual/en/function.mysql-num-rows.php
557                                         if (self::$dbo->affected_rows <= 0) {
558                                                 self::$dbo->affected_rows = mysql_num_rows($retval);
559                                         }
560                                 }
561                                 break;
562                 }
563
564                 // We are having an own error logging in the function "e"
565                 if ((self::$dbo->errorno != 0) && !$called_from_e) {
566                         // We have to preserve the error code, somewhere in the logging it get lost
567                         $error = self::$dbo->error;
568                         $errorno = self::$dbo->errorno;
569
570                         logger('DB Error '.self::$dbo->errorno.': '.self::$dbo->error."\n".
571                                 System::callstack(8)."\n".self::replace_parameters($sql, $params));
572
573                         self::$dbo->error = $error;
574                         self::$dbo->errorno = $errorno;
575                 }
576
577                 $a->save_timestamp($stamp1, 'database');
578
579                 if (x($a->config,'system') && x($a->config['system'], 'db_log')) {
580
581                         $stamp2 = microtime(true);
582                         $duration = (float)($stamp2 - $stamp1);
583
584                         if (($duration > $a->config["system"]["db_loglimit"])) {
585                                 $duration = round($duration, 3);
586                                 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
587
588                                 @file_put_contents($a->config["system"]["db_log"], datetime_convert()."\t".$duration."\t".
589                                                 basename($backtrace[1]["file"])."\t".
590                                                 $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
591                                                 substr(self::replace_parameters($sql, $args), 0, 2000)."\n", FILE_APPEND);
592                         }
593                 }
594                 return $retval;
595         }
596
597         /**
598          * @brief Executes a prepared statement like UPDATE or INSERT that doesn't return data
599          *
600          * Please use dba::delete, dba::insert, dba::update, ... instead
601          *
602          * @param string $sql SQL statement
603          * @return boolean Was the query successfull? False is returned only if an error occurred
604          */
605         public static function e($sql) {
606                 self::initialize();
607
608                 $a = get_app();
609
610                 $stamp = microtime(true);
611
612                 $params = self::getParam(func_get_args());
613
614                 // In a case of a deadlock we are repeating the query 20 times
615                 $timeout = 20;
616
617                 do {
618                         $stmt = self::p($sql, $params);
619
620                         if (is_bool($stmt)) {
621                                 $retval = $stmt;
622                         } elseif (is_object($stmt)) {
623                                 $retval = true;
624                         } else {
625                                 $retval = false;
626                         }
627
628                         self::close($stmt);
629
630                 } while ((self::$dbo->errorno == 1213) && (--$timeout > 0));
631
632                 if (self::$dbo->errorno != 0) {
633                         // We have to preserve the error code, somewhere in the logging it get lost
634                         $error = self::$dbo->error;
635                         $errorno = self::$dbo->errorno;
636
637                         logger('DB Error '.self::$dbo->errorno.': '.self::$dbo->error."\n".
638                                 System::callstack(8)."\n".self::replace_parameters($sql, $params));
639
640                         self::$dbo->error = $error;
641                         self::$dbo->errorno = $errorno;
642                 }
643
644                 $a->save_timestamp($stamp, "database_write");
645
646                 return $retval;
647         }
648
649         /**
650          * @brief Check if data exists
651          *
652          * @param string $table Table name
653          * @param array $condition array of fields for condition
654          *
655          * @return boolean Are there rows for that condition?
656          */
657         public static function exists($table, $condition) {
658                 self::initialize();
659
660                 if (empty($table)) {
661                         return false;
662                 }
663
664                 $fields = array();
665
666                 $array_element = each($condition);
667                 $array_key = $array_element['key'];
668                 if (!is_int($array_key)) {
669                         $fields = array($array_key);
670                 }
671
672                 $stmt = self::select($table, $fields, $condition, array('limit' => 1, 'only_query' => true));
673
674                 if (is_bool($stmt)) {
675                         $retval = $stmt;
676                 } else {
677                         $retval = (self::num_rows($stmt) > 0);
678                 }
679
680                 self::close($stmt);
681
682                 return $retval;
683         }
684
685         /**
686          * @brief Fetches the first row
687          *
688          * Please use dba::select or dba::exists whenever this is possible.
689          *
690          * @param string $sql SQL statement
691          * @return array first row of query
692          */
693         public static function fetch_first($sql) {
694                 self::initialize();
695
696                 $params = self::getParam(func_get_args());
697
698                 $stmt = self::p($sql, $params);
699
700                 if (is_bool($stmt)) {
701                         $retval = $stmt;
702                 } else {
703                         $retval = self::fetch($stmt);
704                 }
705
706                 self::close($stmt);
707
708                 return $retval;
709         }
710
711         /**
712          * @brief Returns the number of affected rows of the last statement
713          *
714          * @return int Number of rows
715          */
716         public static function affected_rows() {
717                 self::initialize();
718
719                 return self::$dbo->affected_rows;
720         }
721
722         /**
723          * @brief Returns the number of columns of a statement
724          *
725          * @param object Statement object
726          * @return int Number of columns
727          */
728         public static function columnCount($stmt) {
729                 self::initialize();
730
731                 if (!is_object($stmt)) {
732                         return 0;
733                 }
734                 switch (self::$dbo->driver) {
735                         case 'pdo':
736                                 return $stmt->columnCount();
737                         case 'mysqli':
738                                 return $stmt->field_count;
739                         case 'mysql':
740                                 return mysql_affected_rows($stmt);
741                 }
742                 return 0;
743         }
744         /**
745          * @brief Returns the number of rows of a statement
746          *
747          * @param object Statement object
748          * @return int Number of rows
749          */
750         public static function num_rows($stmt) {
751                 self::initialize();
752
753                 if (!is_object($stmt)) {
754                         return 0;
755                 }
756                 switch (self::$dbo->driver) {
757                         case 'pdo':
758                                 return $stmt->rowCount();
759                         case 'mysqli':
760                                 return $stmt->num_rows;
761                         case 'mysql':
762                                 return mysql_num_rows($stmt);
763                 }
764                 return 0;
765         }
766
767         /**
768          * @brief Fetch a single row
769          *
770          * @param object $stmt statement object
771          * @return array current row
772          */
773         public static function fetch($stmt) {
774                 self::initialize();
775
776                 if (!is_object($stmt)) {
777                         return false;
778                 }
779
780                 switch (self::$dbo->driver) {
781                         case 'pdo':
782                                 return $stmt->fetch(PDO::FETCH_ASSOC);
783                         case 'mysqli':
784                                 if (get_class($stmt) == 'mysqli_result') {
785                                         return $stmt->fetch_assoc();
786                                 }
787
788                                 // This code works, but is slow
789
790                                 // Bind the result to a result array
791                                 $cols = array();
792
793                                 $cols_num = array();
794                                 for ($x = 0; $x < $stmt->field_count; $x++) {
795                                         $cols[] = &$cols_num[$x];
796                                 }
797
798                                 call_user_func_array(array($stmt, 'bind_result'), $cols);
799
800                                 if (!$stmt->fetch()) {
801                                         return false;
802                                 }
803
804                                 // The slow part:
805                                 // We need to get the field names for the array keys
806                                 // It seems that there is no better way to do this.
807                                 $result = $stmt->result_metadata();
808                                 $fields = $result->fetch_fields();
809
810                                 $columns = array();
811                                 foreach ($cols_num AS $param => $col) {
812                                         $columns[$fields[$param]->name] = $col;
813                                 }
814                                 return $columns;
815                         case 'mysql':
816                                 return mysql_fetch_array(self::$dbo->result, MYSQL_ASSOC);
817                 }
818         }
819
820         /**
821          * @brief Insert a row into a table
822          *
823          * @param string $table Table name
824          * @param array $param parameter array
825          * @param bool $on_duplicate_update Do an update on a duplicate entry
826          *
827          * @return boolean was the insert successfull?
828          */
829         public static function insert($table, $param, $on_duplicate_update = false) {
830                 self::initialize();
831
832                 $sql = "INSERT INTO `".self::$dbo->escape($table)."` (`".implode("`, `", array_keys($param))."`) VALUES (".
833                         substr(str_repeat("?, ", count($param)), 0, -2).")";
834
835                 if ($on_duplicate_update) {
836                         $sql .= " ON DUPLICATE KEY UPDATE `".implode("` = ?, `", array_keys($param))."` = ?";
837
838                         $values = array_values($param);
839                         $param = array_merge_recursive($values, $values);
840                 }
841
842                 return self::e($sql, $param);
843         }
844
845         /**
846          * @brief Fetch the id of the last insert command
847          *
848          * @return integer Last inserted id
849          */
850         public static function lastInsertId() {
851                 self::initialize();
852
853                 switch (self::$dbo->driver) {
854                         case 'pdo':
855                                 $id = self::$dbo->db->lastInsertId();
856                                 break;
857                         case 'mysqli':
858                                 $id = self::$dbo->db->insert_id;
859                                 break;
860                         case 'mysql':
861                                 $id = mysql_insert_id(self::$dbo);
862                                 break;
863                 }
864                 return $id;
865         }
866
867         /**
868          * @brief Locks a table for exclusive write access
869          *
870          * This function can be extended in the future to accept a table array as well.
871          *
872          * @param string $table Table name
873          *
874          * @return boolean was the lock successful?
875          */
876         public static function lock($table) {
877                 self::initialize();
878
879                 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
880                 self::e("SET autocommit=0");
881                 $success = self::e("LOCK TABLES `".self::$dbo->escape($table)."` WRITE");
882                 if (!$success) {
883                         self::e("SET autocommit=1");
884                 } else {
885                         self::$in_transaction = true;
886                 }
887                 return $success;
888         }
889
890         /**
891          * @brief Unlocks all locked tables
892          *
893          * @return boolean was the unlock successful?
894          */
895         public static function unlock() {
896                 self::initialize();
897
898                 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
899                 self::e("COMMIT");
900                 $success = self::e("UNLOCK TABLES");
901                 self::e("SET autocommit=1");
902                 self::$in_transaction = false;
903                 return $success;
904         }
905
906         /**
907          * @brief Starts a transaction
908          *
909          * @return boolean Was the command executed successfully?
910          */
911         public static function transaction() {
912                 self::initialize();
913
914                 if (!self::e('COMMIT')) {
915                         return false;
916                 }
917                 if (!self::e('START TRANSACTION')) {
918                         return false;
919                 }
920                 self::$in_transaction = true;
921                 return true;
922         }
923
924         /**
925          * @brief Does a commit
926          *
927          * @return boolean Was the command executed successfully?
928          */
929         public static function commit() {
930                 self::initialize();
931
932                 if (!self::e('COMMIT')) {
933                         return false;
934                 }
935                 self::$in_transaction = false;
936                 return true;
937         }
938
939         /**
940          * @brief Does a rollback
941          *
942          * @return boolean Was the command executed successfully?
943          */
944         public static function rollback() {
945                 self::initialize();
946
947                 if (!self::e('ROLLBACK')) {
948                         return false;
949                 }
950                 self::$in_transaction = false;
951                 return true;
952         }
953
954         /**
955          * @brief Build the array with the table relations
956          *
957          * The array is build from the database definitions in dbstructure.php
958          *
959          * This process must only be started once, since the value is cached.
960          */
961         private static function build_relation_data() {
962                 $definition = db_definition();
963
964                 foreach ($definition AS $table => $structure) {
965                         foreach ($structure['fields'] AS $field => $field_struct) {
966                                 if (isset($field_struct['relation'])) {
967                                         foreach ($field_struct['relation'] AS $rel_table => $rel_field) {
968                                                 self::$relation[$rel_table][$rel_field][$table][] = $field;
969                                         }
970                                 }
971                         }
972                 }
973         }
974
975         /**
976          * @brief Delete a row from a table
977          *
978          * @param string $table Table name
979          * @param array $param parameter array
980          * @param boolean $in_process Internal use: Only do a commit after the last delete
981          * @param array $callstack Internal use: prevent endless loops
982          *
983          * @return boolean|array was the delete successfull? When $in_process is set: deletion data
984          */
985         public static function delete($table, $param, $in_process = false, &$callstack = array()) {
986                 self::initialize();
987
988                 $commands = array();
989
990                 // Create a key for the loop prevention
991                 $key = $table.':'.implode(':', array_keys($param)).':'.implode(':', $param);
992
993                 // We quit when this key already exists in the callstack.
994                 if (isset($callstack[$key])) {
995                         return $commands;
996                 }
997
998                 $callstack[$key] = true;
999
1000                 $table = self::$dbo->escape($table);
1001
1002                 $commands[$key] = array('table' => $table, 'param' => $param);
1003
1004                 // To speed up the whole process we cache the table relations
1005                 if (count(self::$relation) == 0) {
1006                         self::build_relation_data();
1007                 }
1008
1009                 // Is there a relation entry for the table?
1010                 if (isset(self::$relation[$table])) {
1011                         // We only allow a simple "one field" relation.
1012                         $field = array_keys(self::$relation[$table])[0];
1013                         $rel_def = array_values(self::$relation[$table])[0];
1014
1015                         // Create a key for preventing double queries
1016                         $qkey = $field.'-'.$table.':'.implode(':', array_keys($param)).':'.implode(':', $param);
1017
1018                         // When the search field is the relation field, we don't need to fetch the rows
1019                         // This is useful when the leading record is already deleted in the frontend but the rest is done in the backend
1020                         if ((count($param) == 1) && ($field == array_keys($param)[0])) {
1021                                 foreach ($rel_def AS $rel_table => $rel_fields) {
1022                                         foreach ($rel_fields AS $rel_field) {
1023                                                 $retval = self::delete($rel_table, array($rel_field => array_values($param)[0]), true, $callstack);
1024                                                 $commands = array_merge($commands, $retval);
1025                                         }
1026                                 }
1027                         // We quit when this key already exists in the callstack.
1028                         } elseif (!isset($callstack[$qkey])) {
1029
1030                                 $callstack[$qkey] = true;
1031
1032                                 // Fetch all rows that are to be deleted
1033                                 $data = self::select($table, array($field), $param);
1034
1035                                 while ($row = self::fetch($data)) {
1036                                         // Now we accumulate the delete commands
1037                                         $retval = self::delete($table, array($field => $row[$field]), true, $callstack);
1038                                         $commands = array_merge($commands, $retval);
1039                                 }
1040
1041                                 self::close($data);
1042
1043                                 // Since we had split the delete command we don't need the original command anymore
1044                                 unset($commands[$key]);
1045                         }
1046                 }
1047
1048                 if (!$in_process) {
1049                         // Now we finalize the process
1050                         $do_transaction = !self::$in_transaction;
1051
1052                         if ($do_transaction) {
1053                                 self::transaction();
1054                         }
1055
1056                         $compacted = array();
1057                         $counter = array();
1058
1059                         foreach ($commands AS $command) {
1060                                 $condition = $command['param'];
1061                                 $array_element = each($condition);
1062                                 $array_key = $array_element['key'];
1063                                 if (is_int($array_key)) {
1064                                         $condition_string = " WHERE ".array_shift($condition);
1065                                 } else {
1066                                         $condition_string = " WHERE `".implode("` = ? AND `", array_keys($condition))."` = ?";
1067                                 }
1068
1069                                 if ((count($command['param']) > 1) || is_int($array_key)) {
1070                                         $sql = "DELETE FROM `".$command['table']."`".$condition_string;
1071                                         logger(self::replace_parameters($sql, $condition), LOGGER_DATA);
1072
1073                                         if (!self::e($sql, $condition)) {
1074                                                 if ($do_transaction) {
1075                                                         self::rollback();
1076                                                 }
1077                                                 return false;
1078                                         }
1079                                 } else {
1080                                         $key_table = $command['table'];
1081                                         $key_param = array_keys($command['param'])[0];
1082                                         $value = array_values($command['param'])[0];
1083
1084                                         // Split the SQL queries in chunks of 100 values
1085                                         // We do the $i stuff here to make the code better readable
1086                                         $i = $counter[$key_table][$key_param];
1087                                         if (count($compacted[$key_table][$key_param][$i]) > 100) {
1088                                                 ++$i;
1089                                         }
1090
1091                                         $compacted[$key_table][$key_param][$i][$value] = $value;
1092                                         $counter[$key_table][$key_param] = $i;
1093                                 }
1094                         }
1095                         foreach ($compacted AS $table => $values) {
1096                                 foreach ($values AS $field => $field_value_list) {
1097                                         foreach ($field_value_list AS $field_values) {
1098                                                 $sql = "DELETE FROM `".$table."` WHERE `".$field."` IN (".
1099                                                         substr(str_repeat("?, ", count($field_values)), 0, -2).");";
1100
1101                                                 logger(self::replace_parameters($sql, $field_values), LOGGER_DATA);
1102
1103                                                 if (!self::e($sql, $field_values)) {
1104                                                         if ($do_transaction) {
1105                                                                 self::rollback();
1106                                                         }
1107                                                         return false;
1108                                                 }
1109                                         }
1110                                 }
1111                         }
1112                         if ($do_transaction) {
1113                                 self::commit();
1114                         }
1115                         return true;
1116                 }
1117
1118                 return $commands;
1119         }
1120
1121         /**
1122          * @brief Updates rows
1123          *
1124          * Updates rows in the database. When $old_fields is set to an array,
1125          * the system will only do an update if the fields in that array changed.
1126          *
1127          * Attention:
1128          * Only the values in $old_fields are compared.
1129          * This is an intentional behaviour.
1130          *
1131          * Example:
1132          * We include the timestamp field in $fields but not in $old_fields.
1133          * Then the row will only get the new timestamp when the other fields had changed.
1134          *
1135          * When $old_fields is set to a boolean value the system will do this compare itself.
1136          * When $old_fields is set to "true" the system will do an insert if the row doesn't exists.
1137          *
1138          * Attention:
1139          * Only set $old_fields to a boolean value when you are sure that you will update a single row.
1140          * When you set $old_fields to "true" then $fields must contain all relevant fields!
1141          *
1142          * @param string $table Table name
1143          * @param array $fields contains the fields that are updated
1144          * @param array $condition condition array with the key values
1145          * @param array|boolean $old_fields array with the old field values that are about to be replaced (true = update on duplicate)
1146          *
1147          * @return boolean was the update successfull?
1148          */
1149         public static function update($table, $fields, $condition, $old_fields = array()) {
1150                 self::initialize();
1151
1152                 $table = self::$dbo->escape($table);
1153
1154                 if (count($condition) > 0) {
1155                         $array_element = each($condition);
1156                         $array_key = $array_element['key'];
1157                         if (is_int($array_key)) {
1158                                 $condition_string = " WHERE ".array_shift($condition);
1159                         } else {
1160                                 $condition_string = " WHERE `".implode("` = ? AND `", array_keys($condition))."` = ?";
1161                         }
1162                 } else {
1163                         $condition_string = "";
1164                 }
1165
1166                 if (is_bool($old_fields)) {
1167                         $do_insert = $old_fields;
1168
1169                         $old_fields = self::select($table, array(), $condition, array('limit' => 1));
1170
1171                         if (is_bool($old_fields)) {
1172                                 if ($do_insert) {
1173                                         $values = array_merge($condition, $fields);
1174                                         return self::insert($table, $values, $do_insert);
1175                                 }
1176                                 $old_fields = array();
1177                         }
1178                 }
1179
1180                 $do_update = (count($old_fields) == 0);
1181
1182                 foreach ($old_fields AS $fieldname => $content) {
1183                         if (isset($fields[$fieldname])) {
1184                                 if ($fields[$fieldname] == $content) {
1185                                         unset($fields[$fieldname]);
1186                                 } else {
1187                                         $do_update = true;
1188                                 }
1189                         }
1190                 }
1191
1192                 if (!$do_update || (count($fields) == 0)) {
1193                         return true;
1194                 }
1195
1196                 $sql = "UPDATE `".$table."` SET `".
1197                         implode("` = ?, `", array_keys($fields))."` = ?".$condition_string;
1198
1199                 $params1 = array_values($fields);
1200                 $params2 = array_values($condition);
1201                 $params = array_merge_recursive($params1, $params2);
1202
1203                 return self::e($sql, $params);
1204         }
1205
1206         /**
1207          * @brief Select rows from a table
1208          *
1209          * @param string $table Table name
1210          * @param array $fields array of selected fields
1211          * @param array $condition array of fields for condition
1212          * @param array $params array of several parameters
1213          *
1214          * @return boolean|object If "limit" is equal "1" only a single row is returned, else a query object is returned
1215          *
1216          * Example:
1217          * $table = "item";
1218          * $fields = array("id", "uri", "uid", "network");
1219          *
1220          * $condition = array("uid" => 1, "network" => 'dspr');
1221          * or:
1222          * $condition = array("`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr');
1223          *
1224          * $params = array("order" => array("id", "received" => true), "limit" => 1);
1225          *
1226          * $data = dba::select($table, $fields, $condition, $params);
1227          */
1228         public static function select($table, $fields = array(), $condition = array(), $params = array()) {
1229                 self::initialize();
1230
1231                 if ($table == '') {
1232                         return false;
1233                 }
1234
1235                 if (count($fields) > 0) {
1236                         $select_fields = "`".implode("`, `", array_values($fields))."`";
1237                 } else {
1238                         $select_fields = "*";
1239                 }
1240
1241                 if (count($condition) > 0) {
1242                         $array_element = each($condition);
1243                         $array_key = $array_element['key'];
1244                         if (is_int($array_key)) {
1245                                 $condition_string = " WHERE ".array_shift($condition);
1246                         } else {
1247                                 $condition_string = " WHERE `".implode("` = ? AND `", array_keys($condition))."` = ?";
1248                         }
1249                 } else {
1250                         $condition_string = "";
1251                 }
1252
1253                 $param_string = '';
1254                 $single_row = false;
1255
1256                 if (isset($params['order'])) {
1257                         $param_string .= " ORDER BY ";
1258                         foreach ($params['order'] AS $fields => $order) {
1259                                 if (!is_int($fields)) {
1260                                         $param_string .= "`".$fields."` ".($order ? "DESC" : "ASC").", ";
1261                                 } else {
1262                                         $param_string .= "`".$order."`, ";
1263                                 }
1264                         }
1265                         $param_string = substr($param_string, 0, -2);
1266                 }
1267
1268                 if (isset($params['limit']) && is_int($params['limit'])) {
1269                         $param_string .= " LIMIT ".$params['limit'];
1270                         $single_row = ($params['limit'] == 1);
1271                 }
1272
1273                 if (isset($params['only_query']) && $params['only_query']) {
1274                         $single_row = !$params['only_query'];
1275                 }
1276
1277                 $sql = "SELECT ".$select_fields." FROM `".$table."`".$condition_string.$param_string;
1278
1279                 $result = self::p($sql, $condition);
1280
1281                 if (is_bool($result) || !$single_row) {
1282                         return $result;
1283                 } else {
1284                         $row = self::fetch($result);
1285                         self::close($result);
1286                         return $row;
1287                 }
1288         }
1289
1290
1291         /**
1292          * @brief Fills an array with data from a query
1293          *
1294          * @param object $stmt statement object
1295          * @return array Data array
1296          */
1297         public static function inArray($stmt, $do_close = true) {
1298                 self::initialize();
1299
1300                 if (is_bool($stmt)) {
1301                         return $stmt;
1302                 }
1303
1304                 $data = array();
1305                 while ($row = self::fetch($stmt)) {
1306                         $data[] = $row;
1307                 }
1308                 if ($do_close) {
1309                         self::close($stmt);
1310                 }
1311                 return $data;
1312         }
1313
1314         /**
1315          * @brief Returns the error number of the last query
1316          *
1317          * @return string Error number (0 if no error)
1318          */
1319         public static function errorNo() {
1320                 self::initialize();
1321
1322                 return self::$dbo->errorno;
1323         }
1324
1325         /**
1326          * @brief Returns the error message of the last query
1327          *
1328          * @return string Error message ('' if no error)
1329          */
1330         public static function errorMessage() {
1331                 self::initialize();
1332
1333                 return self::$dbo->error;
1334         }
1335
1336         /**
1337          * @brief Closes the current statement
1338          *
1339          * @param object $stmt statement object
1340          * @return boolean was the close successfull?
1341          */
1342         public static function close($stmt) {
1343                 self::initialize();
1344
1345                 if (!is_object($stmt)) {
1346                         return false;
1347                 }
1348
1349                 switch (self::$dbo->driver) {
1350                         case 'pdo':
1351                                 return $stmt->closeCursor();
1352                         case 'mysqli':
1353                                 return $stmt->free_result();
1354                                 return $stmt->close();
1355                         case 'mysql':
1356                                 return mysql_free_result($stmt);
1357                 }
1358         }
1359 }
1360
1361 function dbesc($str) {
1362         global $db;
1363
1364         if ($db && $db->connected) {
1365                 return($db->escape($str));
1366         } else {
1367                 return(str_replace("'","\\'",$str));
1368         }
1369 }
1370
1371 /**
1372  * @brief execute SQL query with printf style args - deprecated
1373  *
1374  * Please use the dba:: functions instead:
1375  * dba::select, dba::exists, dba::insert
1376  * dba::delete, dba::update, dba::p, dba::e
1377  *
1378  * @param $args Query parameters (1 to N parameters of different types)
1379  * @return array Query array
1380  */
1381 function q($sql) {
1382         global $db;
1383
1384         $args = func_get_args();
1385         unset($args[0]);
1386
1387         if (!$db || !$db->connected) {
1388                 return false;
1389         }
1390
1391         $sql = $db->clean_query($sql);
1392         $sql = $db->any_value_fallback($sql);
1393
1394         $stmt = @vsprintf($sql, $args);
1395
1396         $ret = dba::p($stmt);
1397
1398         if (is_bool($ret)) {
1399                 return $ret;
1400         }
1401
1402         $columns = dba::columnCount($ret);
1403
1404         $data = dba::inArray($ret);
1405
1406         if ((count($data) == 0) && ($columns == 0)) {
1407                 return true;
1408         }
1409
1410         return $data;
1411 }
1412
1413 function dba_timer() {
1414         return microtime(true);
1415 }