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