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