6
|
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('mysql_connect'))
|
|
28 |
exit('This PHP environment doesn\'t have MySQL support built in. MySQL support is required if you want to use a MySQL 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()
|
|
42 |
{
|
|
43 |
global $db;
|
|
44 |
$this->prefix = table_prefix . 'pun_';
|
|
45 |
$this->link_id =& $db->_conn;
|
|
46 |
|
|
47 |
// Setup the client-server character set (UTF-8)
|
|
48 |
if (!defined('PUN_NO_SET_NAMES'))
|
|
49 |
mysql_query('SET NAMES \'utf8\'', $this->link_id) or error(__FILE__, __LINE__);
|
|
50 |
}
|
|
51 |
|
|
52 |
|
|
53 |
function start_transaction()
|
|
54 |
{
|
|
55 |
return;
|
|
56 |
}
|
|
57 |
|
|
58 |
|
|
59 |
function end_transaction()
|
|
60 |
{
|
|
61 |
return;
|
|
62 |
}
|
|
63 |
|
|
64 |
|
|
65 |
function query($sql, $unbuffered = false)
|
|
66 |
{
|
|
67 |
if (strlen($sql) > 140000)
|
|
68 |
exit('Insane query. Aborting.');
|
|
69 |
|
|
70 |
if (defined('PUN_SHOW_QUERIES'))
|
|
71 |
$q_start = get_microtime();
|
|
72 |
|
|
73 |
if ($unbuffered)
|
|
74 |
$this->query_result = @mysql_unbuffered_query($sql, $this->link_id);
|
|
75 |
else
|
|
76 |
$this->query_result = @mysql_query($sql, $this->link_id);
|
|
77 |
|
|
78 |
if ($this->query_result)
|
|
79 |
{
|
|
80 |
if (defined('PUN_SHOW_QUERIES'))
|
|
81 |
$this->saved_queries[] = array($sql, sprintf('%.5f', get_microtime() - $q_start));
|
|
82 |
|
|
83 |
++$this->num_queries;
|
|
84 |
|
|
85 |
return $this->query_result;
|
|
86 |
}
|
|
87 |
else
|
|
88 |
{
|
|
89 |
if (defined('PUN_SHOW_QUERIES'))
|
|
90 |
$this->saved_queries[] = array($sql, 0);
|
|
91 |
|
|
92 |
return false;
|
|
93 |
}
|
|
94 |
}
|
|
95 |
|
|
96 |
|
|
97 |
function query_build($query, $unbuffered = false)
|
|
98 |
{
|
|
99 |
$sql = '';
|
|
100 |
|
|
101 |
if (isset($query['SELECT']))
|
|
102 |
{
|
|
103 |
$sql = 'SELECT '.$query['SELECT'].' FROM '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['FROM'];
|
|
104 |
|
|
105 |
if (isset($query['JOINS']))
|
|
106 |
{
|
|
107 |
foreach ($query['JOINS'] as $cur_join)
|
|
108 |
$sql .= ' '.key($cur_join).' '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).current($cur_join).' ON '.$cur_join['ON'];
|
|
109 |
}
|
|
110 |
|
|
111 |
if (!empty($query['WHERE']))
|
|
112 |
$sql .= ' WHERE '.$query['WHERE'];
|
|
113 |
if (!empty($query['GROUP BY']))
|
|
114 |
$sql .= ' GROUP BY '.$query['GROUP BY'];
|
|
115 |
if (!empty($query['HAVING']))
|
|
116 |
$sql .= ' HAVING '.$query['HAVING'];
|
|
117 |
if (!empty($query['ORDER BY']))
|
|
118 |
$sql .= ' ORDER BY '.$query['ORDER BY'];
|
|
119 |
if (!empty($query['LIMIT']))
|
|
120 |
$sql .= ' LIMIT '.$query['LIMIT'];
|
|
121 |
}
|
|
122 |
else if (isset($query['INSERT']))
|
|
123 |
{
|
|
124 |
$sql = 'INSERT INTO '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['INTO'];
|
|
125 |
|
|
126 |
if (!empty($query['INSERT']))
|
|
127 |
$sql .= ' ('.$query['INSERT'].')';
|
|
128 |
|
|
129 |
$sql .= ' VALUES('.$query['VALUES'].')';
|
|
130 |
}
|
|
131 |
else if (isset($query['UPDATE']))
|
|
132 |
{
|
|
133 |
$query['UPDATE'] = (isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['UPDATE'];
|
|
134 |
|
|
135 |
if (isset($query['PARAMS']['LOW_PRIORITY']))
|
|
136 |
$query['UPDATE'] = 'LOW_PRIORITY '.$query['UPDATE'];
|
|
137 |
|
|
138 |
$sql = 'UPDATE '.$query['UPDATE'].' SET '.$query['SET'];
|
|
139 |
|
|
140 |
if (!empty($query['WHERE']))
|
|
141 |
$sql .= ' WHERE '.$query['WHERE'];
|
|
142 |
}
|
|
143 |
else if (isset($query['DELETE']))
|
|
144 |
{
|
|
145 |
$sql = 'DELETE FROM '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['DELETE'];
|
|
146 |
|
|
147 |
if (!empty($query['WHERE']))
|
|
148 |
$sql .= ' WHERE '.$query['WHERE'];
|
|
149 |
}
|
|
150 |
else if (isset($query['REPLACE']))
|
|
151 |
{
|
|
152 |
$sql = 'REPLACE INTO '.(isset($query['PARAMS']['NO_PREFIX']) ? '' : $this->prefix).$query['INTO'];
|
|
153 |
|
|
154 |
if (!empty($query['REPLACE']))
|
|
155 |
$sql .= ' ('.$query['REPLACE'].')';
|
|
156 |
|
|
157 |
$sql .= ' VALUES('.$query['VALUES'].')';
|
|
158 |
}
|
|
159 |
|
|
160 |
return $this->query($sql, $unbuffered);
|
|
161 |
}
|
|
162 |
|
|
163 |
function result($query_id = 0, $row = 0)
|
|
164 |
{
|
|
165 |
return ($query_id) ? @mysql_result($query_id, $row) : false;
|
|
166 |
}
|
|
167 |
|
|
168 |
|
|
169 |
function fetch_assoc($query_id = 0)
|
|
170 |
{
|
|
171 |
return ($query_id) ? @mysql_fetch_assoc($query_id) : false;
|
|
172 |
}
|
|
173 |
|
|
174 |
|
|
175 |
function fetch_row($query_id = 0)
|
|
176 |
{
|
|
177 |
return ($query_id) ? @mysql_fetch_row($query_id) : false;
|
|
178 |
}
|
|
179 |
|
|
180 |
|
|
181 |
function num_rows($query_id = 0)
|
|
182 |
{
|
|
183 |
return ($query_id) ? @mysql_num_rows($query_id) : false;
|
|
184 |
}
|
|
185 |
|
|
186 |
|
|
187 |
function affected_rows()
|
|
188 |
{
|
|
189 |
return ($this->link_id) ? @mysql_affected_rows($this->link_id) : false;
|
|
190 |
}
|
|
191 |
|
|
192 |
|
|
193 |
function insert_id()
|
|
194 |
{
|
|
195 |
return ($this->link_id) ? @mysql_insert_id($this->link_id) : false;
|
|
196 |
}
|
|
197 |
|
|
198 |
|
|
199 |
function get_num_queries()
|
|
200 |
{
|
|
201 |
return $this->num_queries;
|
|
202 |
}
|
|
203 |
|
|
204 |
|
|
205 |
function get_saved_queries()
|
|
206 |
{
|
|
207 |
return $this->saved_queries;
|
|
208 |
}
|
|
209 |
|
|
210 |
|
|
211 |
function free_result($query_id = false)
|
|
212 |
{
|
|
213 |
return ($query_id) ? @mysql_free_result($query_id) : false;
|
|
214 |
}
|
|
215 |
|
|
216 |
|
|
217 |
function escape($str)
|
|
218 |
{
|
|
219 |
if (is_array($str))
|
|
220 |
return '';
|
|
221 |
else if (function_exists('mysql_real_escape_string'))
|
|
222 |
return mysql_real_escape_string($str, $this->link_id);
|
|
223 |
else
|
|
224 |
return mysql_escape_string($str);
|
|
225 |
}
|
|
226 |
|
|
227 |
|
|
228 |
function error()
|
|
229 |
{
|
|
230 |
$result['error_sql'] = @current(@end($this->saved_queries));
|
|
231 |
$result['error_no'] = @mysql_errno($this->link_id);
|
|
232 |
$result['error_msg'] = @mysql_error($this->link_id);
|
|
233 |
|
|
234 |
return $result;
|
|
235 |
}
|
|
236 |
|
|
237 |
|
|
238 |
function close()
|
|
239 |
{
|
|
240 |
return true;
|
|
241 |
}
|
|
242 |
|
|
243 |
|
|
244 |
function table_exists($table_name)
|
|
245 |
{
|
|
246 |
$result = $this->query('SHOW TABLES LIKE \''.$this->escape($table_name).'\'');
|
|
247 |
return $this->num_rows($result) > 0;
|
|
248 |
}
|
|
249 |
|
|
250 |
|
|
251 |
function field_exists($table_name, $field_name)
|
|
252 |
{
|
|
253 |
$result = $this->query('SHOW COLUMNS FROM '.$table_name.' LIKE \''.$this->escape($field_name).'\'');
|
|
254 |
return $this->num_rows($result) > 0;
|
|
255 |
}
|
|
256 |
|
|
257 |
|
|
258 |
function index_exists($table_name, $index_name)
|
|
259 |
{
|
|
260 |
$exists = false;
|
|
261 |
|
|
262 |
$result = $this->query('SHOW INDEX FROM '.$table_name);
|
|
263 |
while ($cur_index = $this->fetch_assoc($result))
|
|
264 |
{
|
|
265 |
if ($cur_index['Key_name'] == $index_name)
|
|
266 |
{
|
|
267 |
$exists = true;
|
|
268 |
break;
|
|
269 |
}
|
|
270 |
}
|
|
271 |
|
|
272 |
return $exists;
|
|
273 |
}
|
|
274 |
|
|
275 |
|
|
276 |
function add_field($table_name, $field_name, $field_type, $allow_null, $default_value = null, $after_field = null)
|
|
277 |
{
|
|
278 |
if ($this->field_exists($table_name, $field_name))
|
|
279 |
return;
|
|
280 |
|
|
281 |
if ($default_value !== null && !is_int($default_value) && !is_float($default_value))
|
|
282 |
$default_value = '\''.$this->escape($default_value).'\'';
|
|
283 |
|
|
284 |
$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__);
|
|
285 |
}
|
|
286 |
|
|
287 |
|
|
288 |
function drop_field($table_name, $field_name)
|
|
289 |
{
|
|
290 |
if (!$this->field_exists($table_name, $field_name))
|
|
291 |
return;
|
|
292 |
|
|
293 |
$this->query('ALTER TABLE '.$table_name.' DROP '.$field_name) or error(__FILE__, __LINE__);
|
|
294 |
}
|
|
295 |
}
|