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