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