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