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