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