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