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