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