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