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