]> git.mxchange.org Git - friendica.git/blob - include/dba.php
db update script
[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 .= print_r($r, true). EOL;
69 //                      $debug_text .= quoted_printable_encode(print_r($r, true). EOL);
70                 elseif($this->debug == 3)
71                         echo print_r($r, true) . EOL ;
72 //                      echo quoted_printable_encode(print_r($r, true) . EOL) ;
73
74                 return($r);
75         }
76
77         public function dbg($dbg) {
78                 $this->debug = $dbg;
79         }
80
81         public function escape($str) {
82                 return @$this->db->real_escape_string($str);
83         }
84
85         function __destruct() {
86                 @$this->db->close();
87         }
88 }}
89
90 // Procedural functions
91 if(! function_exists('dbg')) { 
92 function dbg($state) {
93         global $db;
94         $db->dbg($state);
95 }}
96
97 if(! function_exists('dbesc')) { 
98 function dbesc($str) {
99         global $db;
100         return($db->escape($str));
101 }}
102
103
104 // Function: q($sql,$args);
105 // Description: execute SQL query with printf style args.
106 // Example: $r = q("SELECT * FROM `%s` WHERE `uid` = %d",
107 //                   'user', 1);
108
109 if(! function_exists('q')) { 
110 function q($sql) {
111
112         global $db;
113         $args = func_get_args();
114         unset($args[0]);
115         $ret = $db->q(vsprintf($sql,$args));
116         return $ret;
117 }}
118
119
120 // Caller is responsible for ensuring that any integer arguments to 
121 // dbesc_array are actually integers and not malformed strings containing
122 // SQL injection vectors. All integer array elements should be specifically 
123 // cast to int to avoid trouble. 
124
125
126 if(! function_exists('dbesc_array_cb')) {
127 function dbesc_array_cb(&$item, $key) {
128         if(is_string($item))
129                 $item = dbesc($item);
130 }}
131
132
133 if(! function_exists('dbesc_array')) {
134 function dbesc_array(&$a) {
135         if(is_array($a) && count($a)) {
136                 array_walk($a,'dbesc_array_cb');
137         }
138 }}