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