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