]> git.mxchange.org Git - friendica.git/blob - include/dba.php
Replace direct accesses to App->config by Config::get/set calls
[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         private static $db_charset = '';
33
34         public static function connect($serveraddr, $user, $pass, $db, $charset = null)
35         {
36                 if (!is_null(self::$db) && self::connected()) {
37                         return true;
38                 }
39
40                 // We are storing these values for being able to perform a reconnect
41                 self::$db_serveraddr = $serveraddr;
42                 self::$db_user = $user;
43                 self::$db_pass = $pass;
44                 self::$db_name = $db;
45                 self::$db_charset = $charset;
46
47                 $serveraddr = trim($serveraddr);
48
49                 $serverdata = explode(':', $serveraddr);
50                 $server = $serverdata[0];
51
52                 if (count($serverdata) > 1) {
53                         $port = trim($serverdata[1]);
54                 }
55
56                 $server = trim($server);
57                 $user = trim($user);
58                 $pass = trim($pass);
59                 $db = trim($db);
60                 $charset = trim($charset);
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 ($charset) {
75                                 $connect .= ";charset=".$charset;
76                         }
77
78                         try {
79                                 self::$db = @new PDO($connect, $user, $pass);
80                                 self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
81                                 self::$connected = true;
82                         } catch (PDOException $e) {
83                         }
84                 }
85
86                 if (!self::$connected && class_exists('mysqli')) {
87                         self::$driver = 'mysqli';
88                         self::$db = @new mysqli($server, $user, $pass, $db, $port);
89                         if (!mysqli_connect_errno()) {
90                                 self::$connected = true;
91
92                                 if ($charset) {
93                                         self::$db->set_charset($charset);
94                                 }
95                         }
96                 }
97
98                 // No suitable SQL driver was found.
99                 if (!self::$connected) {
100                         self::$driver = null;
101                         self::$db = null;
102                 }
103
104                 return self::$connected;
105         }
106
107         /**
108          * Disconnects the current database connection
109          */
110         public static function disconnect()
111         {
112                 if (is_null(self::$db)) {
113                         return;
114                 }
115
116                 switch (self::$driver) {
117                         case 'pdo':
118                                 self::$db = null;
119                                 break;
120                         case 'mysqli':
121                                 self::$db->close();
122                                 self::$db = null;
123                                 break;
124                 }
125         }
126
127         /**
128          * Perform a reconnect of an existing database connection
129          */
130         public static function reconnect() {
131                 self::disconnect();
132
133                 $ret = self::connect(self::$db_serveraddr, self::$db_user, self::$db_pass, self::$db_name, self::$db_charset);
134                 return $ret;
135         }
136
137         /**
138          * Return the database object.
139          * @return PDO|mysqli
140          */
141         public static function get_db()
142         {
143                 return self::$db;
144         }
145
146         /**
147          * @brief Returns the MySQL server version string
148          *
149          * This function discriminate between the deprecated mysql API and the current
150          * object-oriented mysqli API. Example of returned string: 5.5.46-0+deb8u1
151          *
152          * @return string
153          */
154         public static function server_info() {
155                 if (self::$_server_info == '') {
156                         switch (self::$driver) {
157                                 case 'pdo':
158                                         self::$_server_info = self::$db->getAttribute(PDO::ATTR_SERVER_VERSION);
159                                         break;
160                                 case 'mysqli':
161                                         self::$_server_info = self::$db->server_info;
162                                         break;
163                         }
164                 }
165                 return self::$_server_info;
166         }
167
168         /**
169          * @brief Returns the selected database name
170          *
171          * @return string
172          */
173         public static function database_name() {
174                 $ret = self::p("SELECT DATABASE() AS `db`");
175                 $data = self::inArray($ret);
176                 return $data[0]['db'];
177         }
178
179         /**
180          * @brief Analyze a database query and log this if some conditions are met.
181          *
182          * @param string $query The database query that will be analyzed
183          */
184         private static function logIndex($query) {
185                 $a = get_app();
186
187                 if (empty(Config::get('system', 'db_log_index'))) {
188                         return;
189                 }
190
191                 // Don't explain an explain statement
192                 if (strtolower(substr($query, 0, 7)) == "explain") {
193                         return;
194                 }
195
196                 // Only do the explain on "select", "update" and "delete"
197                 if (!in_array(strtolower(substr($query, 0, 6)), ["select", "update", "delete"])) {
198                         return;
199                 }
200
201                 $r = self::p("EXPLAIN ".$query);
202                 if (!DBM::is_result($r)) {
203                         return;
204                 }
205
206                 $watchlist = explode(',', Config::get('system', 'db_log_index_watch'));
207                 $blacklist = explode(',', Config::get('system', 'db_log_index_blacklist'));
208
209                 while ($row = dba::fetch($r)) {
210                         if ((intval(Config::get('system', 'db_loglimit_index')) > 0)) {
211                                 $log = (in_array($row['key'], $watchlist) &&
212                                         ($row['rows'] >= intval(Config::get('system', 'db_loglimit_index'))));
213                         } else {
214                                 $log = false;
215                         }
216
217                         if ((intval(Config::get('system', 'db_loglimit_index_high')) > 0) && ($row['rows'] >= intval(Config::get('system', 'db_loglimit_index_high')))) {
218                                 $log = true;
219                         }
220
221                         if (in_array($row['key'], $blacklist) || ($row['key'] == "")) {
222                                 $log = false;
223                         }
224
225                         if ($log) {
226                                 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
227                                 @file_put_contents(Config::get('system', 'db_log_index'), DateTimeFormat::utcNow()."\t".
228                                                 $row['key']."\t".$row['rows']."\t".$row['Extra']."\t".
229                                                 basename($backtrace[1]["file"])."\t".
230                                                 $backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
231                                                 substr($query, 0, 2000)."\n", FILE_APPEND);
232                         }
233                 }
234         }
235
236         public static function escape($str) {
237                 switch (self::$driver) {
238                         case 'pdo':
239                                 return substr(@self::$db->quote($str, PDO::PARAM_STR), 1, -1);
240                         case 'mysqli':
241                                 return @self::$db->real_escape_string($str);
242                 }
243         }
244
245         public static function connected() {
246                 $connected = false;
247
248                 switch (self::$driver) {
249                         case 'pdo':
250                                 $r = dba::p("SELECT 1");
251                                 if (DBM::is_result($r)) {
252                                         $row = dba::inArray($r);
253                                         $connected = ($row[0]['1'] == '1');
254                                 }
255                                 break;
256                         case 'mysqli':
257                                 $connected = self::$db->ping();
258                                 break;
259                 }
260                 return $connected;
261         }
262
263         /**
264          * @brief Replaces ANY_VALUE() function by MIN() function,
265          *  if the database server does not support ANY_VALUE().
266          *
267          * Considerations for Standard SQL, or MySQL with ONLY_FULL_GROUP_BY (default since 5.7.5).
268          * ANY_VALUE() is available from MySQL 5.7.5 https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html
269          * A standard fall-back is to use MIN().
270          *
271          * @param string $sql An SQL string without the values
272          * @return string The input SQL string modified if necessary.
273          */
274         public static function any_value_fallback($sql) {
275                 $server_info = self::server_info();
276                 if (version_compare($server_info, '5.7.5', '<') ||
277                         (stripos($server_info, 'MariaDB') !== false)) {
278                         $sql = str_ireplace('ANY_VALUE(', 'MIN(', $sql);
279                 }
280                 return $sql;
281         }
282
283         /**
284          * @brief beautifies the query - useful for "SHOW PROCESSLIST"
285          *
286          * This is safe when we bind the parameters later.
287          * The parameter values aren't part of the SQL.
288          *
289          * @param string $sql An SQL string without the values
290          * @return string The input SQL string modified if necessary.
291          */
292         public static function clean_query($sql) {
293                 $search = ["\t", "\n", "\r", "  "];
294                 $replace = [' ', ' ', ' ', ' '];
295                 do {
296                         $oldsql = $sql;
297                         $sql = str_replace($search, $replace, $sql);
298                 } while ($oldsql != $sql);
299
300                 return $sql;
301         }
302
303
304         /**
305          * @brief Replaces the ? placeholders with the parameters in the $args array
306          *
307          * @param string $sql SQL query
308          * @param array $args The parameters that are to replace the ? placeholders
309          * @return string The replaced SQL query
310          */
311         private static function replaceParameters($sql, $args) {
312                 $offset = 0;
313                 foreach ($args AS $param => $value) {
314                         if (is_int($args[$param]) || is_float($args[$param])) {
315                                 $replace = intval($args[$param]);
316                         } else {
317                                 $replace = "'".self::escape($args[$param])."'";
318                         }
319
320                         $pos = strpos($sql, '?', $offset);
321                         if ($pos !== false) {
322                                 $sql = substr_replace($sql, $replace, $pos, 1);
323                         }
324                         $offset = $pos + strlen($replace);
325                 }
326                 return $sql;
327         }
328
329         /**
330          * @brief Convert parameter array to an universal form
331          * @param array $args Parameter array
332          * @return array universalized parameter array
333          */
334         private static function getParam($args) {
335                 unset($args[0]);
336
337                 // When the second function parameter is an array then use this as the parameter array
338                 if ((count($args) > 0) && (is_array($args[1]))) {
339                         return $args[1];
340                 } else {
341                         return $args;
342                 }
343         }
344
345         /**
346          * @brief Executes a prepared statement that returns data
347          * @usage Example: $r = p("SELECT * FROM `item` WHERE `guid` = ?", $guid);
348          *
349          * Please only use it with complicated queries.
350          * For all regular queries please use dba::select or dba::exists
351          *
352          * @param string $sql SQL statement
353          * @return bool|object statement object or result object
354          */
355         public static function p($sql) {
356                 $a = get_app();
357
358                 $stamp1 = microtime(true);
359
360                 $params = self::getParam(func_get_args());
361
362                 // Renumber the array keys to be sure that they fit
363                 $i = 0;
364                 $args = [];
365                 foreach ($params AS $param) {
366                         // Avoid problems with some MySQL servers and boolean values. See issue #3645
367                         if (is_bool($param)) {
368                                 $param = (int)$param;
369                         }
370                         $args[++$i] = $param;
371                 }
372
373                 if (!self::$connected) {
374                         return false;
375                 }
376
377                 if ((substr_count($sql, '?') != count($args)) && (count($args) > 0)) {
378                         // Question: Should we continue or stop the query here?
379                         logger('Parameter mismatch. Query "'.$sql.'" - Parameters '.print_r($args, true), LOGGER_DEBUG);
380                 }
381
382                 $sql = self::clean_query($sql);
383                 $sql = self::any_value_fallback($sql);
384
385                 $orig_sql = $sql;
386
387                 if (Config::get('system', 'db_callstack')) {
388                         $sql = "/*".System::callstack()." */ ".$sql;
389                 }
390
391                 self::$error = '';
392                 self::$errorno = 0;
393                 self::$affected_rows = 0;
394
395                 // We have to make some things different if this function is called from "e"
396                 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
397
398                 if (isset($trace[1])) {
399                         $called_from = $trace[1];
400                 } else {
401                         // We use just something that is defined to avoid warnings
402                         $called_from = $trace[0];
403                 }
404                 // We are having an own error logging in the function "e"
405                 $called_from_e = ($called_from['function'] == 'e');
406
407                 switch (self::$driver) {
408                         case 'pdo':
409                                 // If there are no arguments we use "query"
410                                 if (count($args) == 0) {
411                                         if (!$retval = self::$db->query($sql)) {
412                                                 $errorInfo = self::$db->errorInfo();
413                                                 self::$error = $errorInfo[2];
414                                                 self::$errorno = $errorInfo[1];
415                                                 $retval = false;
416                                                 break;
417                                         }
418                                         self::$affected_rows = $retval->rowCount();
419                                         break;
420                                 }
421
422                                 if (!$stmt = self::$db->prepare($sql)) {
423                                         $errorInfo = self::$db->errorInfo();
424                                         self::$error = $errorInfo[2];
425                                         self::$errorno = $errorInfo[1];
426                                         $retval = false;
427                                         break;
428                                 }
429
430                                 foreach ($args AS $param => $value) {
431                                         if (is_int($args[$param])) {
432                                                 $data_type = PDO::PARAM_INT;
433                                         } else {
434                                                 $data_type = PDO::PARAM_STR;
435                                         }
436                                         $stmt->bindParam($param, $args[$param], $data_type);
437                                 }
438
439                                 if (!$stmt->execute()) {
440                                         $errorInfo = $stmt->errorInfo();
441                                         self::$error = $errorInfo[2];
442                                         self::$errorno = $errorInfo[1];
443                                         $retval = false;
444                                 } else {
445                                         $retval = $stmt;
446                                         self::$affected_rows = $retval->rowCount();
447                                 }
448                                 break;
449                         case 'mysqli':
450                                 // There are SQL statements that cannot be executed with a prepared statement
451                                 $parts = explode(' ', $orig_sql);
452                                 $command = strtolower($parts[0]);
453                                 $can_be_prepared = in_array($command, ['select', 'update', 'insert', 'delete']);
454
455                                 // The fallback routine is called as well when there are no arguments
456                                 if (!$can_be_prepared || (count($args) == 0)) {
457                                         $retval = self::$db->query(self::replaceParameters($sql, $args));
458                                         if (self::$db->errno) {
459                                                 self::$error = self::$db->error;
460                                                 self::$errorno = self::$db->errno;
461                                                 $retval = false;
462                                         } else {
463                                                 if (isset($retval->num_rows)) {
464                                                         self::$affected_rows = $retval->num_rows;
465                                                 } else {
466                                                         self::$affected_rows = self::$db->affected_rows;
467                                                 }
468                                         }
469                                         break;
470                                 }
471
472                                 $stmt = self::$db->stmt_init();
473
474                                 if (!$stmt->prepare($sql)) {
475                                         self::$error = $stmt->error;
476                                         self::$errorno = $stmt->errno;
477                                         $retval = false;
478                                         break;
479                                 }
480
481                                 $param_types = '';
482                                 $values = [];
483                                 foreach ($args AS $param => $value) {
484                                         if (is_int($args[$param])) {
485                                                 $param_types .= 'i';
486                                         } elseif (is_float($args[$param])) {
487                                                 $param_types .= 'd';
488                                         } elseif (is_string($args[$param])) {
489                                                 $param_types .= 's';
490                                         } else {
491                                                 $param_types .= 'b';
492                                         }
493                                         $values[] = &$args[$param];
494                                 }
495
496                                 if (count($values) > 0) {
497                                         array_unshift($values, $param_types);
498                                         call_user_func_array([$stmt, 'bind_param'], $values);
499                                 }
500
501                                 if (!$stmt->execute()) {
502                                         self::$error = self::$db->error;
503                                         self::$errorno = self::$db->errno;
504                                         $retval = false;
505                                 } else {
506                                         $stmt->store_result();
507                                         $retval = $stmt;
508                                         self::$affected_rows = $retval->affected_rows;
509                                 }
510                                 break;
511                 }
512
513                 // We are having an own error logging in the function "e"
514                 if ((self::$errorno != 0) && !$called_from_e) {
515                         // We have to preserve the error code, somewhere in the logging it get lost
516                         $error = self::$error;
517                         $errorno = self::$errorno;
518
519                         logger('DB Error '.self::$errorno.': '.self::$error."\n".
520                                 System::callstack(8)."\n".self::replaceParameters($sql, $args));
521
522                         // On a lost connection we try to reconnect - but only once.
523                         if ($errorno == 2006) {
524                                 if (self::$in_retrial || !self::reconnect()) {
525                                         // It doesn't make sense to continue when the database connection was lost
526                                         if (self::$in_retrial) {
527                                                 logger('Giving up retrial because of database error '.$errorno.': '.$error);
528                                         } else {
529                                                 logger("Couldn't reconnect after database error ".$errorno.': '.$error);
530                                         }
531                                         exit(1);
532                                 } else {
533                                         // We try it again
534                                         logger('Reconnected after database error '.$errorno.': '.$error);
535                                         self::$in_retrial = true;
536                                         $ret = self::p($sql, $args);
537                                         self::$in_retrial = false;
538                                         return $ret;
539                                 }
540                         }
541
542                         self::$error = $error;
543                         self::$errorno = $errorno;
544                 }
545
546                 $a->save_timestamp($stamp1, 'database');
547
548                 if (Config::get('system', 'db_log')) {
549                         $stamp2 = microtime(true);
550                         $duration = (float)($stamp2 - $stamp1);
551
552                         if (($duration > Config::get('system', 'db_loglimit'))) {
553                                 $duration = round($duration, 3);
554                                 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
555
556                                 @file_put_contents(Config::get('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                 if (self::$driver == 'pdo') {
844                         self::e("SET autocommit=0");
845                         self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
846                 } else {
847                         self::$db->autocommit(false);
848                 }
849
850                 $success = self::e("LOCK TABLES `".self::escape($table)."` WRITE");
851
852                 if (self::$driver == 'pdo') {
853                         self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
854                 }
855
856                 if (!$success) {
857                         if (self::$driver == 'pdo') {
858                                 self::e("SET autocommit=1");
859                         } else {
860                                 self::$db->autocommit(true);
861                         }
862                 } else {
863                         self::$in_transaction = true;
864                 }
865                 return $success;
866         }
867
868         /**
869          * @brief Unlocks all locked tables
870          *
871          * @return boolean was the unlock successful?
872          */
873         public static function unlock() {
874                 // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
875                 self::performCommit();
876
877                 if (self::$driver == 'pdo') {
878                         self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
879                 }
880
881                 $success = self::e("UNLOCK TABLES");
882
883                 if (self::$driver == 'pdo') {
884                         self::$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
885                         self::e("SET autocommit=1");
886                 } else {
887                         self::$db->autocommit(true);
888                 }
889
890                 self::$in_transaction = false;
891                 return $success;
892         }
893
894         /**
895          * @brief Starts a transaction
896          *
897          * @return boolean Was the command executed successfully?
898          */
899         public static function transaction() {
900                 if (!self::performCommit()) {
901                         return false;
902                 }
903
904                 switch (self::$driver) {
905                         case 'pdo':
906                                 if (self::$db->inTransaction()) {
907                                         break;
908                                 }
909                                 if (!self::$db->beginTransaction()) {
910                                         return false;
911                                 }
912                                 break;
913                         case 'mysqli':
914                                 if (!self::$db->begin_transaction()) {
915                                         return false;
916                                 }
917                                 break;
918                 }
919
920                 self::$in_transaction = true;
921                 return true;
922         }
923
924         private static function performCommit()
925         {
926                 switch (self::$driver) {
927                         case 'pdo':
928                                 if (!self::$db->inTransaction()) {
929                                         return true;
930                                 }
931                                 return self::$db->commit();
932                         case 'mysqli':
933                                 return self::$db->commit();
934                 }
935                 return true;
936         }
937
938         /**
939          * @brief Does a commit
940          *
941          * @return boolean Was the command executed successfully?
942          */
943         public static function commit() {
944                 if (!self::performCommit()) {
945                         return false;
946                 }
947                 self::$in_transaction = false;
948                 return true;
949         }
950
951         /**
952          * @brief Does a rollback
953          *
954          * @return boolean Was the command executed successfully?
955          */
956         public static function rollback() {
957                 $ret = false;
958
959                 switch (self::$driver) {
960                         case 'pdo':
961                                 if (!self::$db->inTransaction()) {
962                                         $ret = true;
963                                         break;
964                                 }
965                                 $ret = self::$db->rollBack();
966                                 break;
967                         case 'mysqli':
968                                 $ret = self::$db->rollback();
969                                 break;
970                 }
971                 self::$in_transaction = false;
972                 return $ret;
973         }
974
975         /**
976          * @brief Build the array with the table relations
977          *
978          * The array is build from the database definitions in DBStructure.php
979          *
980          * This process must only be started once, since the value is cached.
981          */
982         private static function buildRelationData() {
983                 $definition = DBStructure::definition();
984
985                 foreach ($definition AS $table => $structure) {
986                         foreach ($structure['fields'] AS $field => $field_struct) {
987                                 if (isset($field_struct['relation'])) {
988                                         foreach ($field_struct['relation'] AS $rel_table => $rel_field) {
989                                                 self::$relation[$rel_table][$rel_field][$table][] = $field;
990                                         }
991                                 }
992                         }
993                 }
994         }
995
996         /**
997          * @brief Delete a row from a table
998          *
999          * @param string  $table       Table name
1000          * @param array   $conditions  Field condition(s)
1001          * @param array   $options
1002          *                - cascade: If true we delete records in other tables that depend on the one we're deleting through
1003          *                           relations (default: true)
1004          * @param boolean $in_process  Internal use: Only do a commit after the last delete
1005          * @param array   $callstack   Internal use: prevent endless loops
1006          *
1007          * @return boolean|array was the delete successful? When $in_process is set: deletion data
1008          */
1009         public static function delete($table, array $conditions, array $options = [], $in_process = false, array &$callstack = [])
1010         {
1011                 if (empty($table) || empty($conditions)) {
1012                         logger('Table and conditions have to be set');
1013                         return false;
1014                 }
1015
1016                 $commands = [];
1017
1018                 // Create a key for the loop prevention
1019                 $key = $table . ':' . json_encode($conditions);
1020
1021                 // We quit when this key already exists in the callstack.
1022                 if (isset($callstack[$key])) {
1023                         return $commands;
1024                 }
1025
1026                 $callstack[$key] = true;
1027
1028                 $table = self::escape($table);
1029
1030                 $commands[$key] = ['table' => $table, 'conditions' => $conditions];
1031
1032                 $cascade = defaults($options, 'cascade', true);
1033
1034                 // To speed up the whole process we cache the table relations
1035                 if ($cascade && count(self::$relation) == 0) {
1036                         self::buildRelationData();
1037                 }
1038
1039                 // Is there a relation entry for the table?
1040                 if ($cascade && isset(self::$relation[$table])) {
1041                         // We only allow a simple "one field" relation.
1042                         $field = array_keys(self::$relation[$table])[0];
1043                         $rel_def = array_values(self::$relation[$table])[0];
1044
1045                         // Create a key for preventing double queries
1046                         $qkey = $field . '-' . $table . ':' . json_encode($conditions);
1047
1048                         // When the search field is the relation field, we don't need to fetch the rows
1049                         // This is useful when the leading record is already deleted in the frontend but the rest is done in the backend
1050                         if ((count($conditions) == 1) && ($field == array_keys($conditions)[0])) {
1051                                 foreach ($rel_def AS $rel_table => $rel_fields) {
1052                                         foreach ($rel_fields AS $rel_field) {
1053                                                 $retval = self::delete($rel_table, [$rel_field => array_values($conditions)[0]], $options, true, $callstack);
1054                                                 $commands = array_merge($commands, $retval);
1055                                         }
1056                                 }
1057                                 // We quit when this key already exists in the callstack.
1058                         } elseif (!isset($callstack[$qkey])) {
1059
1060                                 $callstack[$qkey] = true;
1061
1062                                 // Fetch all rows that are to be deleted
1063                                 $data = self::select($table, [$field], $conditions);
1064
1065                                 while ($row = self::fetch($data)) {
1066                                         // Now we accumulate the delete commands
1067                                         $retval = self::delete($table, [$field => $row[$field]], $options, true, $callstack);
1068                                         $commands = array_merge($commands, $retval);
1069                                 }
1070
1071                                 self::close($data);
1072
1073                                 // Since we had split the delete command we don't need the original command anymore
1074                                 unset($commands[$key]);
1075                         }
1076                 }
1077
1078                 if (!$in_process) {
1079                         // Now we finalize the process
1080                         $do_transaction = !self::$in_transaction;
1081
1082                         if ($do_transaction) {
1083                                 self::transaction();
1084                         }
1085
1086                         $compacted = [];
1087                         $counter = [];
1088
1089                         foreach ($commands AS $command) {
1090                                 $conditions = $command['conditions'];
1091                                 reset($conditions);
1092                                 $first_key = key($conditions);
1093
1094                                 $condition_string = self::buildCondition($conditions);
1095
1096                                 if ((count($command['conditions']) > 1) || is_int($first_key)) {
1097                                         $sql = "DELETE FROM `" . $command['table'] . "`" . $condition_string;
1098                                         logger(self::replaceParameters($sql, $conditions), LOGGER_DATA);
1099
1100                                         if (!self::e($sql, $conditions)) {
1101                                                 if ($do_transaction) {
1102                                                         self::rollback();
1103                                                 }
1104                                                 return false;
1105                                         }
1106                                 } else {
1107                                         $key_table = $command['table'];
1108                                         $key_condition = array_keys($command['conditions'])[0];
1109                                         $value = array_values($command['conditions'])[0];
1110
1111                                         // Split the SQL queries in chunks of 100 values
1112                                         // We do the $i stuff here to make the code better readable
1113                                         $i = isset($counter[$key_table][$key_condition]) ? $counter[$key_table][$key_condition] : 0;
1114                                         if (isset($compacted[$key_table][$key_condition][$i]) && count($compacted[$key_table][$key_condition][$i]) > 100) {
1115                                                 ++$i;
1116                                         }
1117
1118                                         $compacted[$key_table][$key_condition][$i][$value] = $value;
1119                                         $counter[$key_table][$key_condition] = $i;
1120                                 }
1121                         }
1122                         foreach ($compacted AS $table => $values) {
1123                                 foreach ($values AS $field => $field_value_list) {
1124                                         foreach ($field_value_list AS $field_values) {
1125                                                 $sql = "DELETE FROM `" . $table . "` WHERE `" . $field . "` IN (" .
1126                                                         substr(str_repeat("?, ", count($field_values)), 0, -2) . ");";
1127
1128                                                 logger(self::replaceParameters($sql, $field_values), LOGGER_DATA);
1129
1130                                                 if (!self::e($sql, $field_values)) {
1131                                                         if ($do_transaction) {
1132                                                                 self::rollback();
1133                                                         }
1134                                                         return false;
1135                                                 }
1136                                         }
1137                                 }
1138                         }
1139                         if ($do_transaction) {
1140                                 self::commit();
1141                         }
1142                         return true;
1143                 }
1144
1145                 return $commands;
1146         }
1147
1148         /**
1149          * @brief Updates rows
1150          *
1151          * Updates rows in the database. When $old_fields is set to an array,
1152          * the system will only do an update if the fields in that array changed.
1153          *
1154          * Attention:
1155          * Only the values in $old_fields are compared.
1156          * This is an intentional behaviour.
1157          *
1158          * Example:
1159          * We include the timestamp field in $fields but not in $old_fields.
1160          * Then the row will only get the new timestamp when the other fields had changed.
1161          *
1162          * When $old_fields is set to a boolean value the system will do this compare itself.
1163          * When $old_fields is set to "true" the system will do an insert if the row doesn't exists.
1164          *
1165          * Attention:
1166          * Only set $old_fields to a boolean value when you are sure that you will update a single row.
1167          * When you set $old_fields to "true" then $fields must contain all relevant fields!
1168          *
1169          * @param string $table Table name
1170          * @param array $fields contains the fields that are updated
1171          * @param array $condition condition array with the key values
1172          * @param array|boolean $old_fields array with the old field values that are about to be replaced (true = update on duplicate)
1173          *
1174          * @return boolean was the update successfull?
1175          */
1176         public static function update($table, $fields, $condition, $old_fields = []) {
1177
1178                 if (empty($table) || empty($fields) || empty($condition)) {
1179                         logger('Table, fields and condition have to be set');
1180                         return false;
1181                 }
1182
1183                 $table = self::escape($table);
1184
1185                 $condition_string = self::buildCondition($condition);
1186
1187                 if (is_bool($old_fields)) {
1188                         $do_insert = $old_fields;
1189
1190                         $old_fields = self::selectFirst($table, [], $condition);
1191
1192                         if (is_bool($old_fields)) {
1193                                 if ($do_insert) {
1194                                         $values = array_merge($condition, $fields);
1195                                         return self::insert($table, $values, $do_insert);
1196                                 }
1197                                 $old_fields = [];
1198                         }
1199                 }
1200
1201                 $do_update = (count($old_fields) == 0);
1202
1203                 foreach ($old_fields AS $fieldname => $content) {
1204                         if (isset($fields[$fieldname])) {
1205                                 if ($fields[$fieldname] == $content) {
1206                                         unset($fields[$fieldname]);
1207                                 } else {
1208                                         $do_update = true;
1209                                 }
1210                         }
1211                 }
1212
1213                 if (!$do_update || (count($fields) == 0)) {
1214                         return true;
1215                 }
1216
1217                 $sql = "UPDATE `".$table."` SET `".
1218                         implode("` = ?, `", array_keys($fields))."` = ?".$condition_string;
1219
1220                 $params1 = array_values($fields);
1221                 $params2 = array_values($condition);
1222                 $params = array_merge_recursive($params1, $params2);
1223
1224                 return self::e($sql, $params);
1225         }
1226
1227         /**
1228          * Retrieve a single record from a table and returns it in an associative array
1229          *
1230          * @brief Retrieve a single record from a table
1231          * @param string $table
1232          * @param array  $fields
1233          * @param array  $condition
1234          * @param array  $params
1235          * @return bool|array
1236          * @see dba::select
1237          */
1238         public static function selectFirst($table, array $fields = [], array $condition = [], $params = [])
1239         {
1240                 $params['limit'] = 1;
1241                 $result = self::select($table, $fields, $condition, $params);
1242
1243                 if (is_bool($result)) {
1244                         return $result;
1245                 } else {
1246                         $row = self::fetch($result);
1247                         self::close($result);
1248                         return $row;
1249                 }
1250         }
1251
1252         /**
1253          * @brief Select rows from a table
1254          *
1255          * @param string $table     Table name
1256          * @param array  $fields    Array of selected fields, empty for all
1257          * @param array  $condition Array of fields for condition
1258          * @param array  $params    Array of several parameters
1259          *
1260          * @return boolean|object
1261          *
1262          * Example:
1263          * $table = "item";
1264          * $fields = array("id", "uri", "uid", "network");
1265          *
1266          * $condition = array("uid" => 1, "network" => 'dspr');
1267          * or:
1268          * $condition = array("`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr');
1269          *
1270          * $params = array("order" => array("id", "received" => true), "limit" => 10);
1271          *
1272          * $data = dba::select($table, $fields, $condition, $params);
1273          */
1274         public static function select($table, array $fields = [], array $condition = [], array $params = [])
1275         {
1276                 if ($table == '') {
1277                         return false;
1278                 }
1279
1280                 $table = self::escape($table);
1281
1282                 if (count($fields) > 0) {
1283                         $select_fields = "`" . implode("`, `", array_values($fields)) . "`";
1284                 } else {
1285                         $select_fields = "*";
1286                 }
1287
1288                 $condition_string = self::buildCondition($condition);
1289
1290                 $param_string = self::buildParameter($params);
1291
1292                 $sql = "SELECT " . $select_fields . " FROM `" . $table . "`" . $condition_string . $param_string;
1293
1294                 $result = self::p($sql, $condition);
1295
1296                 return $result;
1297         }
1298
1299         /**
1300          * @brief Counts the rows from a table satisfying the provided condition
1301          *
1302          * @param string $table Table name
1303          * @param array $condition array of fields for condition
1304          *
1305          * @return int
1306          *
1307          * Example:
1308          * $table = "item";
1309          *
1310          * $condition = ["uid" => 1, "network" => 'dspr'];
1311          * or:
1312          * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
1313          *
1314          * $count = dba::count($table, $condition);
1315          */
1316         public static function count($table, array $condition = [])
1317         {
1318                 if ($table == '') {
1319                         return false;
1320                 }
1321
1322                 $condition_string = self::buildCondition($condition);
1323
1324                 $sql = "SELECT COUNT(*) AS `count` FROM `".$table."`".$condition_string;
1325
1326                 $row = self::fetch_first($sql, $condition);
1327
1328                 return $row['count'];
1329         }
1330
1331         /**
1332          * @brief Returns the SQL condition string built from the provided condition array
1333          *
1334          * This function operates with two modes.
1335          * - Supplied with a filed/value associative array, it builds simple strict
1336          *   equality conditions linked by AND.
1337          * - Supplied with a flat list, the first element is the condition string and
1338          *   the following arguments are the values to be interpolated
1339          *
1340          * $condition = ["uid" => 1, "network" => 'dspr'];
1341          * or:
1342          * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr'];
1343          *
1344          * In either case, the provided array is left with the parameters only
1345          *
1346          * @param array $condition
1347          * @return string
1348          */
1349         public static function buildCondition(array &$condition = [])
1350         {
1351                 $condition_string = '';
1352                 if (count($condition) > 0) {
1353                         reset($condition);
1354                         $first_key = key($condition);
1355                         if (is_int($first_key)) {
1356                                 $condition_string = " WHERE (" . array_shift($condition) . ")";
1357                         } else {
1358                                 $new_values = [];
1359                                 $condition_string = "";
1360                                 foreach ($condition as $field => $value) {
1361                                         if ($condition_string != "") {
1362                                                 $condition_string .= " AND ";
1363                                         }
1364                                         if (is_array($value)) {
1365                                                 /* Workaround for MySQL Bug #64791.
1366                                                  * Never mix data types inside any IN() condition.
1367                                                  * In case of mixed types, cast all as string.
1368                                                  * Logic needs to be consistent with dba::p() data types.
1369                                                  */
1370                                                 $is_int = false;
1371                                                 $is_alpha = false;
1372                                                 foreach ($value as $single_value) {
1373                                                         if (is_int($single_value)) {
1374                                                                 $is_int = true;
1375                                                         } else {
1376                                                                 $is_alpha = true;
1377                                                         }
1378                                                 }
1379
1380                                                 if ($is_int && $is_alpha) {
1381                                                         foreach ($value as &$ref) {
1382                                                                 if (is_int($ref)) {
1383                                                                         $ref = (string)$ref;
1384                                                                 }
1385                                                         }
1386                                                         unset($ref); //Prevent accidental re-use.
1387                                                 }
1388
1389                                                 $new_values = array_merge($new_values, array_values($value));
1390                                                 $placeholders = substr(str_repeat("?, ", count($value)), 0, -2);
1391                                                 $condition_string .= "`" . $field . "` IN (" . $placeholders . ")";
1392                                         } else {
1393                                                 $new_values[$field] = $value;
1394                                                 $condition_string .= "`" . $field . "` = ?";
1395                                         }
1396                                 }
1397                                 $condition_string = " WHERE (" . $condition_string . ")";
1398                                 $condition = $new_values;
1399                         }
1400                 }
1401
1402                 return $condition_string;
1403         }
1404
1405         /**
1406          * @brief Returns the SQL parameter string built from the provided parameter array
1407          *
1408          * @param array $params
1409          * @return string
1410          */
1411         public static function buildParameter(array $params = [])
1412         {
1413                 $order_string = '';
1414                 if (isset($params['order'])) {
1415                         $order_string = " ORDER BY ";
1416                         foreach ($params['order'] AS $fields => $order) {
1417                                 if (!is_int($fields)) {
1418                                         $order_string .= "`" . $fields . "` " . ($order ? "DESC" : "ASC") . ", ";
1419                                 } else {
1420                                         $order_string .= "`" . $order . "`, ";
1421                                 }
1422                         }
1423                         $order_string = substr($order_string, 0, -2);
1424                 }
1425
1426                 $limit_string = '';
1427                 if (isset($params['limit']) && is_int($params['limit'])) {
1428                         $limit_string = " LIMIT " . $params['limit'];
1429                 }
1430
1431                 if (isset($params['limit']) && is_array($params['limit'])) {
1432                         $limit_string = " LIMIT " . intval($params['limit'][0]) . ", " . intval($params['limit'][1]);
1433                 }
1434
1435                 return $order_string.$limit_string;
1436         }
1437
1438         /**
1439          * @brief Fills an array with data from a query
1440          *
1441          * @param object $stmt statement object
1442          * @return array Data array
1443          */
1444         public static function inArray($stmt, $do_close = true) {
1445                 if (is_bool($stmt)) {
1446                         return $stmt;
1447                 }
1448
1449                 $data = [];
1450                 while ($row = self::fetch($stmt)) {
1451                         $data[] = $row;
1452                 }
1453                 if ($do_close) {
1454                         self::close($stmt);
1455                 }
1456                 return $data;
1457         }
1458
1459         /**
1460          * @brief Returns the error number of the last query
1461          *
1462          * @return string Error number (0 if no error)
1463          */
1464         public static function errorNo() {
1465                 return self::$errorno;
1466         }
1467
1468         /**
1469          * @brief Returns the error message of the last query
1470          *
1471          * @return string Error message ('' if no error)
1472          */
1473         public static function errorMessage() {
1474                 return self::$error;
1475         }
1476
1477         /**
1478          * @brief Closes the current statement
1479          *
1480          * @param object $stmt statement object
1481          * @return boolean was the close successful?
1482          */
1483         public static function close($stmt) {
1484                 $a = get_app();
1485
1486                 $stamp1 = microtime(true);
1487
1488                 if (!is_object($stmt)) {
1489                         return false;
1490                 }
1491
1492                 switch (self::$driver) {
1493                         case 'pdo':
1494                                 $ret = $stmt->closeCursor();
1495                                 break;
1496                         case 'mysqli':
1497                                 // MySQLi offers both a mysqli_stmt and a mysqli_result class.
1498                                 // We should be careful not to assume the object type of $stmt
1499                                 // because dba::p() has been able to return both types.
1500                                 if ($stmt instanceof mysqli_stmt) {
1501                                         $stmt->free_result();
1502                                         $ret = $stmt->close();
1503                                 } elseif ($stmt instanceof mysqli_result) {
1504                                         $stmt->free();
1505                                         $ret = true;
1506                                 } else {
1507                                         $ret = false;
1508                                 }
1509                                 break;
1510                 }
1511
1512                 $a->save_timestamp($stamp1, 'database');
1513
1514                 return $ret;
1515         }
1516 }
1517
1518 function dbesc($str) {
1519         if (dba::$connected) {
1520                 return(dba::escape($str));
1521         } else {
1522                 return(str_replace("'","\\'",$str));
1523         }
1524 }
1525
1526 /**
1527  * @brief execute SQL query with printf style args - deprecated
1528  *
1529  * Please use the dba:: functions instead:
1530  * dba::select, dba::exists, dba::insert
1531  * dba::delete, dba::update, dba::p, dba::e
1532  *
1533  * @param $args Query parameters (1 to N parameters of different types)
1534  * @return array|bool Query array
1535  */
1536 function q($sql) {
1537         $args = func_get_args();
1538         unset($args[0]);
1539
1540         if (!dba::$connected) {
1541                 return false;
1542         }
1543
1544         $sql = dba::clean_query($sql);
1545         $sql = dba::any_value_fallback($sql);
1546
1547         $stmt = @vsprintf($sql, $args);
1548
1549         $ret = dba::p($stmt);
1550
1551         if (is_bool($ret)) {
1552                 return $ret;
1553         }
1554
1555         $columns = dba::columnCount($ret);
1556
1557         $data = dba::inArray($ret);
1558
1559         if ((count($data) == 0) && ($columns == 0)) {
1560                 return true;
1561         }
1562
1563         return $data;
1564 }
1565
1566 function dba_timer() {
1567         return microtime(true);
1568 }