]> git.mxchange.org Git - friendica.git/blob - include/dba.php
fixed star/unstar
[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         public  $error = false;
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 (!(strlen($server) && strlen($user))){
31                         $this->connected = false;
32                         $this->db = null;
33                         return;                 
34                 }
35                 
36                 if($install) {
37                         if(strlen($server) && ($server !== 'localhost') && ($server !== '127.0.0.1')) {
38                                 if(! dns_get_record($server, DNS_A + DNS_CNAME + DNS_PTR)) {
39                                         $this->error = sprintf( t('Cannot locate DNS info for database server \'%s\''), $server);
40                                         $this->connected = false;
41                                         $this->db = null;
42                                         return;
43                                 }
44                         }
45                 }
46
47                 if(class_exists('mysqli')) {
48                         $this->db = @new mysqli($server,$user,$pass,$db);
49                         if(! mysqli_connect_errno()) {
50                                 $this->connected = true;
51                         }
52                 }
53                 else {
54                         $this->mysqli = false;
55                         $this->db = mysql_connect($server,$user,$pass);
56                         if($this->db && mysql_select_db($db,$this->db)) {
57                                 $this->connected = true;
58                         }
59                 }
60                 if(! $this->connected) {
61                         $this->db = null;
62                         if(! $install)
63                                 system_unavailable();
64                 }
65         }
66
67         public function getdb() {
68                 return $this->db;
69         }
70
71         public function q($sql) {
72                 
73                 if((! $this->db) || (! $this->connected))
74                         return false;
75                 
76                 if($this->mysqli)
77                         $result = @$this->db->query($sql);
78                 else
79                         $result = @mysql_query($sql,$this->db);
80
81                 if($this->debug) {
82
83                         $mesg = '';
84
85                         if($this->mysqli) {
86                                 if($this->db->errno)
87                                         logger('dba: ' . $this->db->error);
88                         }
89                         elseif(mysql_errno($this->db))
90                                 logger('dba: ' . mysql_error($this->db));
91
92                         if($result === false)
93                                 $mesg = 'false';
94                         elseif($result === true)
95                                 $mesg = 'true';
96                         else {
97                                 if($this->mysqli)
98                                         $mesg = $result->num_rows . ' results' . EOL;
99                         else
100                                         $mesg = mysql_num_rows($result) . ' results' . EOL;
101                         }
102     
103                         $str =  'SQL = ' . printable($sql) . EOL . 'SQL returned ' . $mesg . EOL;
104
105                         logger('dba: ' . $str );
106                 }
107                 else {
108
109                         /**
110                          * If dbfail.out exists, we will write any failed calls directly to it,
111                          * regardless of any logging that may or may nor be in effect.
112                          * These usually indicate SQL syntax errors that need to be resolved.
113                          */
114
115                         if($result === false) {
116                                 logger('dba: ' . printable($sql) . ' returned false.');
117                                 if(file_exists('dbfail.out'))
118                                         file_put_contents('dbfail.out', printable($sql) . ' returned false' . "\n", FILE_APPEND);
119                         }
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)), LOGGER_DATA);
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                 $ret = $db->q(vsprintf($sql,$args));
211                 return $ret;
212         }
213
214         /**
215          *
216          * This will happen occasionally trying to store the 
217          * session data after abnormal program termination 
218          *
219          */
220         logger('dba: no database: ' . print_r($args,true));
221         return false; 
222
223 }}
224
225 /**
226  *
227  * Raw db query, no arguments
228  *
229  */
230
231 if(! function_exists('dbq')) { 
232 function dbq($sql) {
233
234         global $db;
235         if($db && $db->connected)
236                 $ret = $db->q($sql);
237         else
238                 $ret = false;
239         return $ret;
240 }}
241
242
243 // Caller is responsible for ensuring that any integer arguments to 
244 // dbesc_array are actually integers and not malformed strings containing
245 // SQL injection vectors. All integer array elements should be specifically 
246 // cast to int to avoid trouble. 
247
248
249 if(! function_exists('dbesc_array_cb')) {
250 function dbesc_array_cb(&$item, $key) {
251         if(is_string($item))
252                 $item = dbesc($item);
253 }}
254
255
256 if(! function_exists('dbesc_array')) {
257 function dbesc_array(&$arr) {
258         if(is_array($arr) && count($arr)) {
259                 array_walk($arr,'dbesc_array_cb');
260         }
261 }}              
262
263