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