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