]> git.mxchange.org Git - friendica.git/blob - include/dba.php
sorting out the last(?) quirks in group pages.
[friendica.git] / include / dba.php
1 <?php
2
3 // MySQL database class
4 //
5 // For debugging, insert 'dbg(x);' anywhere in the program flow.
6 // x = 1: display db success/failure following content
7 // x = 2: display full queries following content
8 // x = 3: display full queries using echo; which will mess up display
9 //        really bad but will return output in stubborn cases.
10  
11 if(! class_exists('dba')) { 
12 class dba {
13
14         private $debug = 0;
15         private $db;
16
17         function __construct($server,$user,$pass,$db,$install = false) {
18                 $this->db = @new mysqli($server,$user,$pass,$db);
19                 if((mysqli_connect_errno()) && (! install))
20                         system_unavailable();    
21         }
22
23         public function getdb() {
24                 return $this->db;
25         }
26
27         public function q($sql) {
28                 global $debug_text;
29                 
30                 if(! $this->db )
31                         return false;
32                 
33                 $result = @$this->db->query($sql);
34
35                 if($this->debug) {
36
37                         $mesg = '';
38
39                         if($this->db->mysqli->errno)
40                                 $debug_text .=  $this->db->mysqli->error . EOL;
41
42                         if($result === false)
43                                 $mesg = 'false';
44                         elseif($result === true)
45                                 $mesg = 'true';
46                         else
47                                 $mesg = $result->num_rows.' results' . EOL;
48         
49                         $str =  'SQL = ' . printable($sql) . EOL . 'SQL returned ' . $mesg . EOL;
50
51                         switch($this->debug) {
52                                 case 3:
53                                         echo $str;
54                                         break;
55                                 default:
56                                         $debug_text .= $str;
57                                         break;
58                         }
59                 }
60                 else {
61                         if(($result === false) && (file_exists('dbfail.out')))
62                                 file_put_contents('dbfail.out', printable($sql) . ' returned false' . "\n", FILE_APPEND);
63                 }
64
65                 if(($result === true) || ($result === false))
66                         return $result;
67
68                 $r = array();
69                 if($result->num_rows) {
70                         while($x = $result->fetch_array(MYSQL_ASSOC))
71                                 $r[] = $x;
72                         $result->free_result();
73                 }
74     
75                 if($this->debug == 2)
76                         $debug_text .= printable(print_r($r, true). EOL);
77                 elseif($this->debug == 3)
78                         echo printable(print_r($r, true) . EOL) ;
79
80                 return($r);
81         }
82
83         public function dbg($dbg) {
84                 $this->debug = $dbg;
85         }
86
87         public function escape($str) {
88                 return @$this->db->real_escape_string($str);
89         }
90
91         function __destruct() {
92                 @$this->db->close();
93         }
94 }}
95
96 if(! function_exists('printable')) {
97 function printable($s) {
98         $s = preg_replace("~([\x01-\x08\x0E-\x0F\x10-\x1F\x7F-\xFF])~",".", $s);
99         $s = str_replace("\x00",'.',$s);
100         if(x($_SERVER,'SERVER_NAME'))
101                 $s = escape_tags($s);
102         return $s;
103 }}
104
105 // Procedural functions
106 if(! function_exists('dbg')) { 
107 function dbg($state) {
108         global $db;
109         $db->dbg($state);
110 }}
111
112 if(! function_exists('dbesc')) { 
113 function dbesc($str) {
114         global $db;
115         return($db->escape($str));
116 }}
117
118
119 // Function: q($sql,$args);
120 // Description: execute SQL query with printf style args.
121 // Example: $r = q("SELECT * FROM `%s` WHERE `uid` = %d",
122 //                   'user', 1);
123
124 if(! function_exists('q')) { 
125 function q($sql) {
126
127         global $db;
128         $args = func_get_args();
129         unset($args[0]);
130         $ret = $db->q(vsprintf($sql,$args));
131         return $ret;
132 }}
133
134
135 // Caller is responsible for ensuring that any integer arguments to 
136 // dbesc_array are actually integers and not malformed strings containing
137 // SQL injection vectors. All integer array elements should be specifically 
138 // cast to int to avoid trouble. 
139
140
141 if(! function_exists('dbesc_array_cb')) {
142 function dbesc_array_cb(&$item, $key) {
143         if(is_string($item))
144                 $item = dbesc($item);
145 }}
146
147
148 if(! function_exists('dbesc_array')) {
149 function dbesc_array(&$arr) {
150         if(is_array($arr) && count($arr)) {
151                 array_walk($arr,'dbesc_array_cb');
152         }
153 }}