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