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