]> git.mxchange.org Git - friendica.git/blob - include/dba.php
make 'PHP "register_argc_argv"' easier to translate, may require fix for po2php
[friendica.git] / include / dba.php
1 <?php
2
3 require_once('include/datetime.php');
4
5 /**
6  *
7  * MySQL database class
8  *
9  * For debugging, insert 'dbg(1);' anywhere in the program flow.
10  * dbg(0); will turn it off. Logging is performed at LOGGER_DATA level.
11  * When logging, all binary info is converted to text and html entities are escaped so that 
12  * the debugging stream is safe to view within both terminals and web pages.
13  *
14  */
15  
16 if(! class_exists('dba')) { 
17 class dba {
18
19         private $debug = 0;
20         private $db;
21         public  $mysqli = true;
22         public  $connected = false;
23         public  $error = false;
24
25         function __construct($server,$user,$pass,$db,$install = false) {
26
27                 $server = trim($server);
28                 $user = trim($user);
29                 $pass = trim($pass);
30                 $db = trim($db);
31
32                 if (!(strlen($server) && strlen($user))){
33                         $this->connected = false;
34                         $this->db = null;
35                         return;                 
36                 }
37                 
38                 if($install) {
39                         if(strlen($server) && ($server !== 'localhost') && ($server !== '127.0.0.1')) {
40                                 if(! dns_get_record($server, DNS_A + DNS_CNAME + DNS_PTR)) {
41                                         $this->error = sprintf( t('Cannot locate DNS info for database server \'%s\''), $server);
42                                         $this->connected = false;
43                                         $this->db = null;
44                                         return;
45                                 }
46                         }
47                 }
48
49                 if(class_exists('mysqli')) {
50                         $this->db = @new mysqli($server,$user,$pass,$db);
51                         if(! mysqli_connect_errno()) {
52                                 $this->connected = true;
53                         }
54                 }
55                 else {
56                         $this->mysqli = false;
57                         $this->db = mysql_connect($server,$user,$pass);
58                         if($this->db && mysql_select_db($db,$this->db)) {
59                                 $this->connected = true;
60                         }
61                 }
62                 if(! $this->connected) {
63                         $this->db = null;
64                         if(! $install)
65                                 system_unavailable();
66                 }
67         }
68
69         public function getdb() {
70                 return $this->db;
71         }
72
73         public function q($sql) {
74                 
75                 if((! $this->db) || (! $this->connected))
76                         return false;
77                 
78                 if($this->mysqli)
79                         $result = @$this->db->query($sql);
80                 else
81                         $result = @mysql_query($sql,$this->db);
82
83                 if($this->debug) {
84
85                         $mesg = '';
86
87                         if($this->mysqli) {
88                                 if($this->db->errno)
89                                         logger('dba: ' . $this->db->error);
90                         }
91                         elseif(mysql_errno($this->db))
92                                 logger('dba: ' . mysql_error($this->db));
93
94                         if($result === false)
95                                 $mesg = 'false';
96                         elseif($result === true)
97                                 $mesg = 'true';
98                         else {
99                                 if($this->mysqli)
100                                         $mesg = $result->num_rows . ' results' . EOL;
101                         else
102                                         $mesg = mysql_num_rows($result) . ' results' . EOL;
103                         }
104     
105                         $str =  'SQL = ' . printable($sql) . EOL . 'SQL returned ' . $mesg . EOL;
106
107                         logger('dba: ' . $str );
108                 }
109
110                 /**
111                  * If dbfail.out exists, we will write any failed calls directly to it,
112                  * regardless of any logging that may or may nor be in effect.
113                  * These usually indicate SQL syntax errors that need to be resolved.
114                  */
115
116                 if($result === false) {
117                         logger('dba: ' . printable($sql) . ' returned false.');
118                         if(file_exists('dbfail.out'))
119                                 file_put_contents('dbfail.out', datetime_convert() . "\n" . printable($sql) . ' returned false' . "\n", FILE_APPEND);
120                 }
121
122                 if(($result === true) || ($result === false))
123                         return $result;
124
125                 $r = array();
126                 if($this->mysqli) {
127                         if($result->num_rows) {
128                                 while($x = $result->fetch_array(MYSQLI_ASSOC))
129                                         $r[] = $x;
130                                 $result->free_result();
131                         }
132                 }
133                 else {
134                         if(mysql_num_rows($result)) {
135                                 while($x = mysql_fetch_array($result, MYSQL_ASSOC))
136                                         $r[] = $x;
137                                 mysql_free_result($result);
138                         }
139                 }
140
141     
142                 if($this->debug)
143                         logger('dba: ' . printable(print_r($r, true)));
144                 return($r);
145         }
146
147         public function dbg($dbg) {
148                 $this->debug = $dbg;
149         }
150
151         public function escape($str) {
152                 if($this->db && $this->connected) {
153                         if($this->mysqli)
154                                 return @$this->db->real_escape_string($str);
155                         else
156                                 return @mysql_real_escape_string($str,$this->db);
157                 }
158         }
159
160         function __destruct() {
161                 if ($this->db) 
162                         if($this->mysqli)
163                                 $this->db->close();
164                         else
165                                 mysql_close($this->db);
166         }
167 }}
168
169 if(! function_exists('printable')) {
170 function printable($s) {
171         $s = preg_replace("~([\x01-\x08\x0E-\x0F\x10-\x1F\x7F-\xFF])~",".", $s);
172         $s = str_replace("\x00",'.',$s);
173         if(x($_SERVER,'SERVER_NAME'))
174                 $s = escape_tags($s);
175         return $s;
176 }}
177
178 // Procedural functions
179 if(! function_exists('dbg')) { 
180 function dbg($state) {
181         global $db;
182         if($db)
183         $db->dbg($state);
184 }}
185
186 if(! function_exists('dbesc')) { 
187 function dbesc($str) {
188         global $db;
189         if($db && $db->connected)
190                 return($db->escape($str));
191         else
192                 return(str_replace("'","\\'",$str));
193 }}
194
195
196
197 // Function: q($sql,$args);
198 // Description: execute SQL query with printf style args.
199 // Example: $r = q("SELECT * FROM `%s` WHERE `uid` = %d",
200 //                   'user', 1);
201
202 if(! function_exists('q')) { 
203 function q($sql) {
204
205         global $db;
206         $args = func_get_args();
207         unset($args[0]);
208
209         if($db && $db->connected) {
210                 $stmt = vsprintf($sql,$args);
211                 if($stmt === false)
212                         logger('dba: vsprintf error: ' . print_r(debug_backtrace(),true));
213                 return $db->q($stmt);
214         }
215
216         /**
217          *
218          * This will happen occasionally trying to store the 
219          * session data after abnormal program termination 
220          *
221          */
222         logger('dba: no database: ' . print_r($args,true));
223         return false; 
224
225 }}
226
227 /**
228  *
229  * Raw db query, no arguments
230  *
231  */
232
233 if(! function_exists('dbq')) { 
234 function dbq($sql) {
235
236         global $db;
237         if($db && $db->connected)
238                 $ret = $db->q($sql);
239         else
240                 $ret = false;
241         return $ret;
242 }}
243
244
245 // Caller is responsible for ensuring that any integer arguments to 
246 // dbesc_array are actually integers and not malformed strings containing
247 // SQL injection vectors. All integer array elements should be specifically 
248 // cast to int to avoid trouble. 
249
250
251 if(! function_exists('dbesc_array_cb')) {
252 function dbesc_array_cb(&$item, $key) {
253         if(is_string($item))
254                 $item = dbesc($item);
255 }}
256
257
258 if(! function_exists('dbesc_array')) {
259 function dbesc_array(&$arr) {
260         if(is_array($arr) && count($arr)) {
261                 array_walk($arr,'dbesc_array_cb');
262         }
263 }}              
264
265