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