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