]> git.mxchange.org Git - friendica.git/blob - include/dba.php
Merge branch 'master' of git://github.com/friendika/friendika
[friendica.git] / include / dba.php
1 <?php
2
3 /**
4  *
5  * MySQL database class
6  *
7  * For debugging, insert 'dbg(1);' anywhere in the program flow.
8  * dbg(0); will turn it off. Logging is performed at LOGGER_DATA level.
9  * When logging, all binary info is converted to text and html entities are escaped so that 
10  * the debugging stream is safe to view within both terminals and web pages.
11  *
12  */
13  
14 if(! class_exists('dba')) { 
15 class dba {
16
17         private $debug = 0;
18         private $db;
19
20         function __construct($server,$user,$pass,$db,$install = false) {
21                 $this->db = @new mysqli($server,$user,$pass,$db);
22                 if((mysqli_connect_errno()) && (! $install))
23                         system_unavailable();    
24         }
25
26         public function getdb() {
27                 return $this->db;
28         }
29
30         public function q($sql) {
31                 
32                 if(! $this->db )
33                         return false;
34                 
35                 $result = @$this->db->query($sql);
36
37                 if($this->debug) {
38
39                         $mesg = '';
40
41                         if($this->db->errno)
42                                 logger('dba: ' . $this->db->error);
43
44                         if($result === false)
45                                 $mesg = 'false';
46                         elseif($result === true)
47                                 $mesg = 'true';
48                         else
49                                 $mesg = $result->num_rows.' results' . EOL;
50         
51                         $str =  'SQL = ' . printable($sql) . EOL . 'SQL returned ' . $mesg . EOL;
52
53                         logger('dba: ' . $str );
54                 }
55                 else {
56
57                         /*
58                          * If dbfail.out exists, we will write any failed calls directly to it,
59                          * regardless of any logging that may or may nor be in effect.
60                          * These usually indicate SQL syntax errors that need to be resolved.
61                          */
62
63                         if($result === false) {
64                                 logger('dba: ' . printable($sql) . ' returned false.');
65                                 if(file_exists('dbfail.out'))
66                                         file_put_contents('dbfail.out', printable($sql) . ' returned false' . "\n", FILE_APPEND);
67                         }
68                 }
69
70                 if(($result === true) || ($result === false))
71                         return $result;
72
73                 $r = array();
74                 if($result->num_rows) {
75                         while($x = $result->fetch_array(MYSQL_ASSOC))
76                                 $r[] = $x;
77                         $result->free_result();
78                 }
79     
80                 if($this->debug)
81                         logger('dba: ' . printable(print_r($r, true)), LOGGER_DATA);
82                 return($r);
83         }
84
85         public function dbg($dbg) {
86                 $this->debug = $dbg;
87         }
88
89         public function escape($str) {
90                 return @$this->db->real_escape_string($str);
91         }
92
93         function __destruct() {
94                 @$this->db->close();
95         }
96 }}
97
98 if(! function_exists('printable')) {
99 function printable($s) {
100         $s = preg_replace("~([\x01-\x08\x0E-\x0F\x10-\x1F\x7F-\xFF])~",".", $s);
101         $s = str_replace("\x00",'.',$s);
102         if(x($_SERVER,'SERVER_NAME'))
103                 $s = escape_tags($s);
104         return $s;
105 }}
106
107 // Procedural functions
108 if(! function_exists('dbg')) { 
109 function dbg($state) {
110         global $db;
111         $db->dbg($state);
112 }}
113
114 if(! function_exists('dbesc')) { 
115 function dbesc($str) {
116         global $db;
117         return($db->escape($str));
118 }}
119
120
121 // Function: q($sql,$args);
122 // Description: execute SQL query with printf style args.
123 // Example: $r = q("SELECT * FROM `%s` WHERE `uid` = %d",
124 //                   'user', 1);
125
126 if(! function_exists('q')) { 
127 function q($sql) {
128
129         global $db;
130         $args = func_get_args();
131         unset($args[0]);
132         $ret = $db->q(vsprintf($sql,$args));
133         return $ret;
134 }}
135
136 // raw db query, no arguments
137
138 if(! function_exists('dbq')) { 
139 function dbq($sql) {
140
141         global $db;
142         $ret = $db->q($sql);
143         return $ret;
144 }}
145
146
147 // Caller is responsible for ensuring that any integer arguments to 
148 // dbesc_array are actually integers and not malformed strings containing
149 // SQL injection vectors. All integer array elements should be specifically 
150 // cast to int to avoid trouble. 
151
152
153 if(! function_exists('dbesc_array_cb')) {
154 function dbesc_array_cb(&$item, $key) {
155         if(is_string($item))
156                 $item = dbesc($item);
157 }}
158
159
160 if(! function_exists('dbesc_array')) {
161 function dbesc_array(&$arr) {
162         if(is_array($arr) && count($arr)) {
163                 array_walk($arr,'dbesc_array_cb');
164         }
165 }}