]> git.mxchange.org Git - friendica.git/blob - include/dba.php
07142dbadfd5cd84ac71d79acb6cdf0277bbc605
[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 q($sql) {
24                 global $debug_text;
25                 
26                 if(! $this->db )
27                         return false;
28                 
29                 $result = @$this->db->query($sql);
30
31                 if($this->debug) {
32
33                         $mesg = '';
34
35                         if($this->db->mysqli->errno)
36                                 $debug_text .=  $this->db->mysqli->error . EOL;
37
38                         if($result === false)
39                                 $mesg = 'false';
40                         elseif($result === true)
41                                 $mesg = 'true';
42                         else
43                                 $mesg = $result->num_rows.' results' . EOL;
44         
45                         $str =  'SQL = ' . $sql . EOL . 'SQL returned ' . $mesg . EOL;
46
47                         switch($this->debug) {
48                                 case 3:
49                                         echo $str;
50                                         break;
51                                 default:
52                                         $debug_text .= $str;
53                                         break;
54                         }
55                 }
56
57                 if(($result === true) || ($result === false))
58                         return $result;
59
60                 $r = array();
61                 if($result->num_rows) {
62                         while($x = $result->fetch_array(MYSQL_ASSOC))
63                                 $r[] = $x;
64                         $result->free_result();
65                 }
66     
67                 if($this->debug == 2)
68                         $debug_text .= printable(print_r($r, true). EOL);
69                 elseif($this->debug == 3)
70                         echo printable(print_r($r, true) . EOL) ;
71
72                 return($r);
73         }
74
75         public function dbg($dbg) {
76                 $this->debug = $dbg;
77         }
78
79         public function escape($str) {
80                 return @$this->db->real_escape_string($str);
81         }
82
83         function __destruct() {
84                 @$this->db->close();
85         }
86 }}
87
88 if(! function_exists('printable')) {
89 function printable($s) {
90         $s = preg_replace("~([\x01-\x08\x0E-\x0F\x10-\x1F\x7F-\xFF])~",".", $s);
91         $s = str_replace("\x00",'.',$s);
92         if(x($_SERVER,'SERVER_NAME'))
93                 $s = escape_tags($s);
94         return $s;
95 }}
96
97 // Procedural functions
98 if(! function_exists('dbg')) { 
99 function dbg($state) {
100         global $db;
101         $db->dbg($state);
102 }}
103
104 if(! function_exists('dbesc')) { 
105 function dbesc($str) {
106         global $db;
107         return($db->escape($str));
108 }}
109
110
111 // Function: q($sql,$args);
112 // Description: execute SQL query with printf style args.
113 // Example: $r = q("SELECT * FROM `%s` WHERE `uid` = %d",
114 //                   'user', 1);
115
116 if(! function_exists('q')) { 
117 function q($sql) {
118
119         global $db;
120         $args = func_get_args();
121         unset($args[0]);
122         $ret = $db->q(vsprintf($sql,$args));
123         return $ret;
124 }}
125
126
127 // Caller is responsible for ensuring that any integer arguments to 
128 // dbesc_array are actually integers and not malformed strings containing
129 // SQL injection vectors. All integer array elements should be specifically 
130 // cast to int to avoid trouble. 
131
132
133 if(! function_exists('dbesc_array_cb')) {
134 function dbesc_array_cb(&$item, $key) {
135         if(is_string($item))
136                 $item = dbesc($item);
137 }}
138
139
140 if(! function_exists('dbesc_array')) {
141 function dbesc_array(&$arr) {
142         if(is_array($arr) && count($arr)) {
143                 array_walk($arr,'dbesc_array_cb');
144         }
145 }}