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