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