]> git.mxchange.org Git - friendica.git/blob - include/dba.php
48dcc12a985eb1f6c173422dbc96d270a871569f
[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         return(escape_tags(str_replace("\x00",'.',$s)));
92 }}
93
94 // Procedural functions
95 if(! function_exists('dbg')) { 
96 function dbg($state) {
97         global $db;
98         $db->dbg($state);
99 }}
100
101 if(! function_exists('dbesc')) { 
102 function dbesc($str) {
103         global $db;
104         return($db->escape($str));
105 }}
106
107
108 // Function: q($sql,$args);
109 // Description: execute SQL query with printf style args.
110 // Example: $r = q("SELECT * FROM `%s` WHERE `uid` = %d",
111 //                   'user', 1);
112
113 if(! function_exists('q')) { 
114 function q($sql) {
115
116         global $db;
117         $args = func_get_args();
118         unset($args[0]);
119         $ret = $db->q(vsprintf($sql,$args));
120         return $ret;
121 }}
122
123
124 // Caller is responsible for ensuring that any integer arguments to 
125 // dbesc_array are actually integers and not malformed strings containing
126 // SQL injection vectors. All integer array elements should be specifically 
127 // cast to int to avoid trouble. 
128
129
130 if(! function_exists('dbesc_array_cb')) {
131 function dbesc_array_cb(&$item, $key) {
132         if(is_string($item))
133                 $item = dbesc($item);
134 }}
135
136
137 if(! function_exists('dbesc_array')) {
138 function dbesc_array(&$arr) {
139         if(is_array($arr) && count($arr)) {
140                 array_walk($arr,'dbesc_array_cb');
141         }
142 }}