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