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