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