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