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