|
1 <?php |
|
2 /*********************************************************************** |
|
3 |
|
4 Copyright (C) 2002-2008 PunBB.org |
|
5 |
|
6 This file is part of PunBB. |
|
7 |
|
8 PunBB is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published |
|
10 by the Free Software Foundation; either version 2 of the License, |
|
11 or (at your option) any later version. |
|
12 |
|
13 PunBB is distributed in the hope that it will be useful, but |
|
14 WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 GNU General Public License for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with this program; if not, write to the Free Software |
|
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
|
21 MA 02111-1307 USA |
|
22 |
|
23 ************************************************************************/ |
|
24 |
|
25 |
|
26 // Make sure we have built in support for MySQL |
|
27 if (!function_exists('mysqli_connect')) |
|
28 exit('This PHP environment doesn\'t have Improved MySQL (mysqli) support built in. Improved MySQL support is required if you want to use a MySQL 4.1 (or later) database to run this forum. Consult the PHP documentation for further assistance.'); |
|
29 |
|
30 |
|
31 class DBLayer |
|
32 { |
|
33 var $prefix; |
|
34 var $link_id; |
|
35 var $query_result; |
|
36 |
|
37 var $saved_queries = array(); |
|
38 var $num_queries = 0; |
|
39 |
|
40 |
|
41 function DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $foo) |
|
42 { |
|
43 $this->prefix = $db_prefix; |
|
44 |
|
45 // Was a custom port supplied with $db_host? |
|
46 if (strpos($db_host, ':') !== false) |
|
47 list($db_host, $db_port) = explode(':', $db_host); |
|
48 |
|
49 if (isset($db_port)) |
|
50 $this->link_id = @mysqli_connect($db_host, $db_username, $db_password, $db_name, $db_port); |
|
51 else |
|
52 $this->link_id = @mysqli_connect($db_host, $db_username, $db_password, $db_name); |
|
53 |
|
54 if (!$this->link_id) |
|
55 error('Unable to connect to MySQL and select database. MySQL reported: '.mysqli_connect_error(), __FILE__, __LINE__); |
|
56 |
|
57 // Setup the client-server character set (UTF-8) |
|
58 if (!defined('PUN_NO_SET_NAMES')) |
|
59 mysqli_query($this->link_id, 'SET NAMES \'utf8\'') or error(__FILE__, __LINE__); |
|
60 } |
|
61 |
|
62 |
|
63 function start_transaction() |
|
64 { |
|
65 return; |
|
66 } |
|
67 |
|
68 |
|
69 function end_transaction() |
|
70 { |
|
71 return; |
|
72 } |
|
73 |
|
74 |
|
75 function query($sql, $unbuffered = false) |
|
76 { |
|
77 if (strlen($sql) > 140000) |
|
78 exit('Insane query. Aborting.'); |
|
79 |
|
80 if (defined('PUN_SHOW_QUERIES')) |
|
81 $q_start = get_microtime(); |
|
82 |
|
83 $this->query_result = @mysqli_query($this->link_id, $sql); |
|
84 |
|
85 if ($this->query_result) |
|
86 { |
|
87 if (defined('PUN_SHOW_QUERIES')) |
|
88 $this->saved_queries[] = array($sql, sprintf('%.5f', get_microtime() - $q_start)); |
|
89 |
|
90 ++$this->num_queries; |
|
91 |
|
92 return $this->query_result; |
|
93 } |
|
94 else |
|
95 { |
|
96 if (defined('PUN_SHOW_QUERIES')) |
|
97 $this->saved_queries[] = array($sql, 0); |
|
98 |
|
99 return false; |
|
100 } |
|
101 } |
|
102 |
|
103 |
|
104 function query_build($query, $unbuffered = false) |
|
105 { |
|
106 $sql = ''; |
|
107 |
|
108 if (isset($query['SELECT'])) |
|
109 { |
|
110 $sql = 'SELECT '.$query['SELECT'].' FROM '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['FROM']; |
|
111 |
|
112 if (isset($query['JOINS'])) |
|
113 { |
|
114 foreach ($query['JOINS'] as $cur_join) |
|
115 $sql .= ' '.key($cur_join).' '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).current($cur_join).' ON '.$cur_join['ON']; |
|
116 } |
|
117 |
|
118 if (!empty($query['WHERE'])) |
|
119 $sql .= ' WHERE '.$query['WHERE']; |
|
120 if (!empty($query['GROUP BY'])) |
|
121 $sql .= ' GROUP BY '.$query['GROUP BY']; |
|
122 if (!empty($query['HAVING'])) |
|
123 $sql .= ' HAVING '.$query['HAVING']; |
|
124 if (!empty($query['ORDER BY'])) |
|
125 $sql .= ' ORDER BY '.$query['ORDER BY']; |
|
126 if (!empty($query['LIMIT'])) |
|
127 $sql .= ' LIMIT '.$query['LIMIT']; |
|
128 } |
|
129 else if (isset($query['INSERT'])) |
|
130 { |
|
131 $sql = 'INSERT INTO '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['INTO']; |
|
132 |
|
133 if (!empty($query['INSERT'])) |
|
134 $sql .= ' ('.$query['INSERT'].')'; |
|
135 |
|
136 $sql .= ' VALUES('.$query['VALUES'].')'; |
|
137 } |
|
138 else if (isset($query['UPDATE'])) |
|
139 { |
|
140 $query['UPDATE'] = (isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['UPDATE']; |
|
141 |
|
142 if (isset($query['PARAMS']['LOW_PRIORITY'])) |
|
143 $query['UPDATE'] = 'LOW_PRIORITY '.$query['UPDATE']; |
|
144 |
|
145 $sql = 'UPDATE '.$query['UPDATE'].' SET '.$query['SET']; |
|
146 |
|
147 if (!empty($query['WHERE'])) |
|
148 $sql .= ' WHERE '.$query['WHERE']; |
|
149 } |
|
150 else if (isset($query['DELETE'])) |
|
151 { |
|
152 $sql = 'DELETE FROM '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['DELETE']; |
|
153 |
|
154 if (!empty($query['WHERE'])) |
|
155 $sql .= ' WHERE '.$query['WHERE']; |
|
156 } |
|
157 else if (isset($query['REPLACE'])) |
|
158 { |
|
159 $sql = 'REPLACE INTO '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['INTO']; |
|
160 |
|
161 if (!empty($query['REPLACE'])) |
|
162 $sql .= ' ('.$query['REPLACE'].')'; |
|
163 |
|
164 $sql .= ' VALUES('.$query['VALUES'].')'; |
|
165 } |
|
166 |
|
167 return $this->query($sql, $unbuffered); |
|
168 } |
|
169 |
|
170 |
|
171 function result($query_id = 0, $row = 0) |
|
172 { |
|
173 if ($query_id) |
|
174 { |
|
175 if ($row) |
|
176 @mysqli_data_seek($query_id, $row); |
|
177 |
|
178 $cur_row = @mysqli_fetch_row($query_id); |
|
179 return $cur_row[0]; |
|
180 } |
|
181 else |
|
182 return false; |
|
183 } |
|
184 |
|
185 |
|
186 function fetch_assoc($query_id = 0) |
|
187 { |
|
188 return ($query_id) ? @mysqli_fetch_assoc($query_id) : false; |
|
189 } |
|
190 |
|
191 |
|
192 function fetch_row($query_id = 0) |
|
193 { |
|
194 return ($query_id) ? @mysqli_fetch_row($query_id) : false; |
|
195 } |
|
196 |
|
197 |
|
198 function num_rows($query_id = 0) |
|
199 { |
|
200 return ($query_id) ? @mysqli_num_rows($query_id) : false; |
|
201 } |
|
202 |
|
203 |
|
204 function affected_rows() |
|
205 { |
|
206 return ($this->link_id) ? @mysqli_affected_rows($this->link_id) : false; |
|
207 } |
|
208 |
|
209 |
|
210 function insert_id() |
|
211 { |
|
212 return ($this->link_id) ? @mysqli_insert_id($this->link_id) : false; |
|
213 } |
|
214 |
|
215 |
|
216 function get_num_queries() |
|
217 { |
|
218 return $this->num_queries; |
|
219 } |
|
220 |
|
221 |
|
222 function get_saved_queries() |
|
223 { |
|
224 return $this->saved_queries; |
|
225 } |
|
226 |
|
227 |
|
228 function free_result($query_id = false) |
|
229 { |
|
230 return ($query_id) ? @mysqli_free_result($query_id) : false; |
|
231 } |
|
232 |
|
233 |
|
234 function escape($str) |
|
235 { |
|
236 return is_array($str) ? '' : mysqli_real_escape_string($this->link_id, $str); |
|
237 } |
|
238 |
|
239 |
|
240 function error() |
|
241 { |
|
242 $result['error_sql'] = @current(@end($this->saved_queries)); |
|
243 $result['error_no'] = @mysqli_errno($this->link_id); |
|
244 $result['error_msg'] = @mysqli_error($this->link_id); |
|
245 |
|
246 return $result; |
|
247 } |
|
248 |
|
249 |
|
250 function close() |
|
251 { |
|
252 if ($this->link_id) |
|
253 { |
|
254 if ($this->query_result) |
|
255 @mysqli_free_result($this->query_result); |
|
256 |
|
257 return @mysqli_close($this->link_id); |
|
258 } |
|
259 else |
|
260 return false; |
|
261 } |
|
262 |
|
263 |
|
264 function table_exists($table_name) |
|
265 { |
|
266 $result = $this->query('SHOW TABLES LIKE \''.$this->escape($table_name).'\''); |
|
267 return $this->num_rows($result) > 0; |
|
268 } |
|
269 |
|
270 |
|
271 function field_exists($table_name, $field_name) |
|
272 { |
|
273 $result = $this->query('SHOW COLUMNS FROM '.$table_name.' LIKE \''.$this->escape($field_name).'\''); |
|
274 return $this->num_rows($result) > 0; |
|
275 } |
|
276 |
|
277 |
|
278 function index_exists($table_name, $index_name) |
|
279 { |
|
280 $exists = false; |
|
281 |
|
282 $result = $this->query('SHOW INDEX FROM '.$table_name); |
|
283 while ($cur_index = $this->fetch_assoc($result)) |
|
284 { |
|
285 if ($cur_index['Key_name'] == $index_name) |
|
286 { |
|
287 $exists = true; |
|
288 break; |
|
289 } |
|
290 } |
|
291 |
|
292 return $exists; |
|
293 } |
|
294 |
|
295 |
|
296 function add_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null) |
|
297 { |
|
298 if ($this->field_exists($table_name, $field_name)) |
|
299 return; |
|
300 |
|
301 if ($default_value !== null && !is_int($default_value) && !is_float($default_value)) |
|
302 $default_value = '\''.$this->escape($default_value).'\''; |
|
303 |
|
304 $this->query('ALTER TABLE '.$table_name.' ADD '.$field_name.' '.$field_type.($allow_null ? ' ' : ' NOT NULL').($default_value !== null ? ' DEFAULT '.$default_value : ' ').($after_field != null ? ' AFTER '.$after_field : '')) or error(__FILE__, __LINE__); |
|
305 } |
|
306 |
|
307 |
|
308 function drop_field($table_name, $field_name) |
|
309 { |
|
310 if (!$this->field_exists($table_name, $field_name)) |
|
311 return; |
|
312 |
|
313 $this->query('ALTER TABLE '.$table_name.' DROP '.$field_name) or error(__FILE__, __LINE__); |
|
314 } |
|
315 } |