author | Dan |
Thu, 09 Aug 2007 12:26:16 -0400 | |
changeset 85 | 7c68a18a27be |
parent 73 | 0a74676a2f2f |
child 91 | 8079b0288e8e |
permissions | -rw-r--r-- |
1 | 1 |
<?php |
2 |
||
3 |
/* |
|
4 |
* Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between |
|
73
0a74676a2f2f
Made the move to Loch Ness, and got some basic page grouping functionality working. TODO: fix some UI issues in Javascript ACL editor and change non-JS ACL editor to work with page groups too
Dan
parents:
21
diff
changeset
|
5 |
* Version 1.0.1 (Loch Ness) |
1 | 6 |
* Copyright (C) 2006-2007 Dan Fuhry |
7 |
* |
|
8 |
* This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License |
|
9 |
* as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
|
12 |
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. |
|
13 |
*/ |
|
14 |
||
15 |
function db_error_handler($errno, $errstr, $errfile = false, $errline = false, $errcontext = Array() ) |
|
16 |
{ |
|
17 |
if ( !defined('ENANO_DEBUG') ) |
|
18 |
return; |
|
19 |
$e = error_reporting(0); |
|
20 |
error_reporting($e); |
|
21 |
if ( $e < $errno ) |
|
22 |
return; |
|
23 |
$errtype = 'Notice'; |
|
24 |
switch ( $errno ) |
|
25 |
{ |
|
26 |
case E_ERROR: case E_USER_ERROR: case E_CORE_ERROR: case E_COMPILE_ERROR: $errtype = 'Error'; break; |
|
27 |
case E_WARNING: case E_USER_WARNING: case E_CORE_WARNING: case E_COMPILE_WARNING: $errtype = 'Warning'; break; |
|
28 |
} |
|
29 |
$debug = debug_backtrace(); |
|
30 |
$debug = $debug[2]['file'] . ', line ' . $debug[2]['line']; |
|
31 |
echo "<b>$errtype:</b> $errstr<br />Error source:<pre>$debug</pre>"; |
|
32 |
} |
|
33 |
||
34 |
class mysql { |
|
35 |
var $num_queries, $query_backtrace, $latest_result, $latest_query, $_conn, $sql_stack_fields, $sql_stack_values; |
|
36 |
var $row = array(); |
|
37 |
var $rowset = array(); |
|
38 |
var $errhandler; |
|
39 |
||
40 |
function enable_errorhandler() |
|
41 |
{ |
|
42 |
if ( function_exists('debug_backtrace') ) |
|
43 |
{ |
|
44 |
$this->errhandler = set_error_handler('db_error_handler'); |
|
45 |
} |
|
46 |
} |
|
47 |
||
48 |
function disable_errorhandler() |
|
49 |
{ |
|
50 |
if ( $this->errhandler ) |
|
51 |
{ |
|
52 |
set_error_handler($this->errhandler); |
|
53 |
} |
|
54 |
else |
|
55 |
{ |
|
56 |
restore_error_handler(); |
|
57 |
} |
|
58 |
} |
|
59 |
||
60 |
function sql_backtrace() { |
|
61 |
$qb = explode("\n", $this->query_backtrace); |
|
62 |
$bt = ''; |
|
63 |
//for($i=sizeof($qb)-1;$i>=0;$i--) { |
|
64 |
for($i=0;$i<sizeof($qb);$i++) { |
|
65 |
$bt .= $qb[$i]."\n"; |
|
66 |
} |
|
67 |
return $bt; |
|
68 |
} |
|
69 |
||
70 |
function ensure_connection() |
|
71 |
{ |
|
72 |
if(!$this->_conn) |
|
73 |
{ |
|
74 |
$this->connect(); |
|
75 |
} |
|
76 |
} |
|
77 |
||
78 |
function _die($t = '') { |
|
79 |
if(defined('ENANO_HEADERS_SENT')) { |
|
80 |
ob_clean(); |
|
81 |
} |
|
82 |
header('HTTP/1.1 500 Internal Server Error'); |
|
15
ad5986a53197
Fixed complicated SQL injection vulnerability in URL handler, updated license info for Tigra Tree Menu, and killed one XSS vulnerability
Dan
parents:
1
diff
changeset
|
83 |
$bt = $this->latest_query; // $this->sql_backtrace(); |
1 | 84 |
$e = htmlspecialchars(mysql_error()); |
85 |
if($e=='') $e='<none>'; |
|
86 |
if(defined('ENANO_CONFIG_FETCHED')) die_semicritical('Database error', '<h3>An error occurred during a database query.</h3><p>'.$t.'<br />Error returned by MySQL: '.$e.'<br />SQL Backtrace:</p><pre>'.$bt.'</pre>'); |
|
87 |
else grinding_halt('Database error', '<h3>An error occurred during a database query.</h3><p>'.$t.'<br />Error returned by MySQL: '.$e.'<br />SQL Backtrace:</p><pre>'.$bt.'</pre>'); |
|
88 |
exit; |
|
89 |
} |
|
90 |
||
91 |
function die_json() |
|
92 |
{ |
|
93 |
$e = addslashes(htmlspecialchars(mysql_error())); |
|
94 |
$q = addslashes($this->latest_query); |
|
95 |
$t = "{'mode':'error','error':'An error occurred during database query.\nQuery was:\n $q\n\nError returned by MySQL: $e'}"; |
|
96 |
die($t); |
|
97 |
} |
|
98 |
||
99 |
function get_error($t = '') { |
|
100 |
header('HTTP/1.1 500 Internal Server Error'); |
|
101 |
$bt = $this->sql_backtrace(); |
|
102 |
$e = htmlspecialchars(mysql_error()); |
|
103 |
if($e=='') $e='<none>'; |
|
104 |
$text = '<h3>An error occurred during a database query.</h3><p>'.$t.'<br />Error returned by MySQL: '.$e.'<br />SQL Backtrace:</p><pre>'.$bt.'</pre>'; |
|
105 |
return $text; |
|
106 |
} |
|
107 |
||
108 |
function connect() { |
|
109 |
$this->enable_errorhandler(); |
|
110 |
dc_here('dbal: trying to connect....'); |
|
111 |
@include(ENANO_ROOT.'/config.php'); |
|
112 |
if(isset($crypto_key)) |
|
113 |
unset($crypto_key); // Get this sucker out of memory fast |
|
114 |
if(!defined('ENANO_INSTALLED') && !defined('MIDGET_INSTALLED') && !defined('IN_ENANO_INSTALL') ) |
|
115 |
{ |
|
116 |
dc_here('dbal: oops, looks like Enano isn\'t set up. Constants ENANO_INSTALLED, MIDGET_INSTALLED, and IN_ENANO_INSTALL are all undefined.'); |
|
117 |
header('Location: install.php'); |
|
118 |
exit; |
|
119 |
} |
|
120 |
$this->_conn = @mysql_connect($dbhost, $dbuser, $dbpasswd); |
|
121 |
unset($dbuser); |
|
122 |
unset($dbpasswd); // Security |
|
123 |
if(!$this->_conn) { dc_here('dbal: uhoh!<br />'.mysql_error()); grinding_halt('Enano is having a problem', '<p>Error: couldn\'t connect to MySQL.<br />'.mysql_error().'</p>'); } |
|
124 |
$this->query_backtrace = ''; |
|
125 |
$this->num_queries = 0; |
|
126 |
dc_here('dbal: we\'re in, selecting database...'); |
|
127 |
$q = $this->sql_query('USE '.$dbname.';'); |
|
128 |
if(!$q) $this->_die('The database could not be selected.'); |
|
129 |
dc_here('dbal: connected to MySQL'); |
|
130 |
$this->disable_errorhandler(); |
|
131 |
} |
|
132 |
||
133 |
function sql_query($q) { |
|
134 |
$this->enable_errorhandler(); |
|
135 |
$this->num_queries++; |
|
136 |
$this->query_backtrace .= $q."\n"; |
|
137 |
$this->latest_query = $q; |
|
138 |
dc_here('dbal: making SQL query:<br /><tt>'.$q.'</tt>'); |
|
139 |
if(!$this->_conn) $this->_die('A database connection has not yet been established.'); |
|
140 |
if(!$this->check_query($q)) |
|
141 |
{ |
|
142 |
$this->report_query($q); |
|
143 |
grinding_halt('SQL Injection attempt', '<p>Enano has caught and prevented an SQL injection attempt. Your IP address has been recorded and the administrator has been notified.</p><p>Query was:</p><pre>'.htmlspecialchars($q).'</pre>'); |
|
144 |
} |
|
145 |
$r = mysql_query($q, $this->_conn); |
|
146 |
$this->latest_result = $r; |
|
147 |
$this->disable_errorhandler(); |
|
148 |
return $r; |
|
149 |
} |
|
150 |
||
151 |
function sql_unbuffered_query($q) { |
|
152 |
$this->enable_errorhandler(); |
|
153 |
$this->num_queries++; |
|
154 |
$this->query_backtrace .= '(UNBUFFERED) ' . $q."\n"; |
|
155 |
$this->latest_query = $q; |
|
156 |
dc_here('dbal: making SQL query:<br /><tt>'.$q.'</tt>'); |
|
157 |
if(!$this->_conn) $this->_die('A database connection has not yet been established.'); |
|
158 |
if(!$this->check_query($q)) |
|
159 |
{ |
|
160 |
$this->report_query($q); |
|
161 |
grinding_halt('SQL Injection attempt', '<p>Enano has caught and prevented an SQL injection attempt. Your IP address has been recorded and the administrator has been notified.</p><p>Query was:</p><pre>'.htmlspecialchars($q).'</pre>'); |
|
162 |
} |
|
163 |
$r = mysql_unbuffered_query($q, $this->_conn); |
|
164 |
$this->latest_result = $r; |
|
165 |
$this->disable_errorhandler(); |
|
166 |
return $r; |
|
167 |
} |
|
168 |
||
169 |
/** |
|
170 |
* Checks a SQL query for possible signs of injection attempts |
|
171 |
* @param string $q the query to check |
|
172 |
* @return bool true if query passed check, otherwise false |
|
173 |
*/ |
|
174 |
||
175 |
function check_query($q, $debug = false) |
|
176 |
{ |
|
177 |
if($debug) echo "\$db->check_query(): checking query: ".htmlspecialchars($q).'<br />'."\n"; |
|
178 |
$sz = strlen($q); |
|
179 |
$quotechar = false; |
|
180 |
$quotepos = 0; |
|
181 |
$prev_is_quote = false; |
|
182 |
$just_started = false; |
|
183 |
for($i=0;$i<strlen($q);$i++,$c=substr($q, $i, 1)) |
|
184 |
{ |
|
185 |
$next = substr($q, $i+1, 1); |
|
186 |
$next2 = substr($q, $i+2, 1); |
|
187 |
$prev = substr($q, $i-1, 1); |
|
188 |
$prev2 = substr($q, $i-2, 1); |
|
189 |
if(isset($c) && in_array($c, Array('"', "'", '`'))) |
|
190 |
{ |
|
191 |
if($quotechar) |
|
192 |
{ |
|
193 |
if( |
|
194 |
( $quotechar == $c && $quotechar != $next && ( $quotechar != $prev || $just_entered ) && $prev != '\\') || |
|
195 |
( $prev2 == '\\' && $prev == $quotechar && $quotechar == $c ) |
|
196 |
) |
|
197 |
{ |
|
198 |
$quotechar = false; |
|
199 |
if($debug) echo('$db->check_query(): just finishing a quote section, quoted string: '.htmlspecialchars(substr($q, $quotepos, $i - $quotepos + 1)) . '<br />'); |
|
200 |
$q = substr($q, 0, $quotepos) . 'SAFE_QUOTE' . substr($q, $i + 1, strlen($q)); |
|
201 |
if($debug) echo('$db->check_query(): Filtered query: '.$q.'<br />'); |
|
202 |
$i = $quotepos; |
|
203 |
} |
|
204 |
} |
|
205 |
else |
|
206 |
{ |
|
207 |
$quotechar = $c; |
|
208 |
$quotepos = $i; |
|
209 |
$just_entered = true; |
|
210 |
} |
|
211 |
if($debug) echo '$db->check_query(): found quote char as pos: '.$i.'<br />'; |
|
212 |
continue; |
|
213 |
} |
|
214 |
$just_entered = false; |
|
215 |
} |
|
216 |
if(substr(trim($q), strlen(trim($q))-1, 1) == ';') $q = substr(trim($q), 0, strlen(trim($q))-1); |
|
217 |
for($i=0;$i<strlen($q);$i++,$c=substr($q, $i, 1)) |
|
218 |
{ |
|
219 |
if( ( $c == ';' && $i != $sz-1 ) || $c . substr($q, $i+1, 1) == '--') // Don't permit semicolons in mid-query, and never allow comments |
|
220 |
{ |
|
221 |
// Injection attempt! |
|
222 |
if($debug) |
|
223 |
{ |
|
224 |
$e = ''; |
|
225 |
for($j=$i-5;$j<$i+5;$j++) |
|
226 |
{ |
|
227 |
if($j == $i) $e .= '<span style="color: red; text-decoration: underline;">' . $c . '</span>'; |
|
228 |
else $e .= $c; |
|
229 |
} |
|
230 |
echo 'Injection attempt caught at pos: '.$i.'<br />'; |
|
231 |
} |
|
232 |
return false; |
|
233 |
} |
|
234 |
} |
|
235 |
return true; |
|
236 |
} |
|
237 |
||
238 |
/** |
|
239 |
* Set the internal result pointer to X |
|
240 |
* @param int $pos The number of the row |
|
241 |
* @param resource $result The MySQL result resource - if not given, the latest cached query is assumed |
|
242 |
* @return true on success, false on failure |
|
243 |
*/ |
|
244 |
||
245 |
function sql_data_seek($pos, $result = false) |
|
246 |
{ |
|
247 |
$this->enable_errorhandler(); |
|
248 |
if(!$result) |
|
249 |
$result = $this->latest_result; |
|
250 |
if(!$result) |
|
251 |
{ |
|
252 |
$this->disable_errorhandler(); |
|
253 |
return false; |
|
254 |
} |
|
255 |
if(mysql_data_seek($result, $pos)) |
|
256 |
{ |
|
257 |
$this->disable_errorhandler(); |
|
258 |
return true; |
|
259 |
} |
|
260 |
else |
|
261 |
{ |
|
262 |
$this->disable_errorhandler(); |
|
263 |
return false; |
|
264 |
} |
|
265 |
} |
|
266 |
||
267 |
/** |
|
268 |
* Reports a bad query to the admin |
|
269 |
* @param string $query the naughty query |
|
270 |
* @access private |
|
271 |
*/ |
|
272 |
||
273 |
function report_query($query) |
|
274 |
{ |
|
275 |
global $session; |
|
276 |
if(is_object($session) && defined('ENANO_MAINSTREAM')) |
|
277 |
$username = $session->username; |
|
278 |
else |
|
279 |
$username = 'Unavailable'; |
|
280 |
$query = $this->escape($query); |
|
281 |
$q = $this->sql_query('INSERT INTO '.table_prefix.'logs(log_type, action, time_id, date_string, page_text, author, edit_summary) |
|
282 |
VALUES(\'security\', \'sql_inject\', '.time().', \'\', \''.$query.'\', \''.$username.'\', \''.$_SERVER['REMOTE_ADDR'].'\');'); |
|
283 |
} |
|
284 |
||
73
0a74676a2f2f
Made the move to Loch Ness, and got some basic page grouping functionality working. TODO: fix some UI issues in Javascript ACL editor and change non-JS ACL editor to work with page groups too
Dan
parents:
21
diff
changeset
|
285 |
/** |
0a74676a2f2f
Made the move to Loch Ness, and got some basic page grouping functionality working. TODO: fix some UI issues in Javascript ACL editor and change non-JS ACL editor to work with page groups too
Dan
parents:
21
diff
changeset
|
286 |
* Returns the ID of the row last inserted. |
0a74676a2f2f
Made the move to Loch Ness, and got some basic page grouping functionality working. TODO: fix some UI issues in Javascript ACL editor and change non-JS ACL editor to work with page groups too
Dan
parents:
21
diff
changeset
|
287 |
* @return int |
0a74676a2f2f
Made the move to Loch Ness, and got some basic page grouping functionality working. TODO: fix some UI issues in Javascript ACL editor and change non-JS ACL editor to work with page groups too
Dan
parents:
21
diff
changeset
|
288 |
*/ |
0a74676a2f2f
Made the move to Loch Ness, and got some basic page grouping functionality working. TODO: fix some UI issues in Javascript ACL editor and change non-JS ACL editor to work with page groups too
Dan
parents:
21
diff
changeset
|
289 |
|
0a74676a2f2f
Made the move to Loch Ness, and got some basic page grouping functionality working. TODO: fix some UI issues in Javascript ACL editor and change non-JS ACL editor to work with page groups too
Dan
parents:
21
diff
changeset
|
290 |
function insert_id() |
0a74676a2f2f
Made the move to Loch Ness, and got some basic page grouping functionality working. TODO: fix some UI issues in Javascript ACL editor and change non-JS ACL editor to work with page groups too
Dan
parents:
21
diff
changeset
|
291 |
{ |
0a74676a2f2f
Made the move to Loch Ness, and got some basic page grouping functionality working. TODO: fix some UI issues in Javascript ACL editor and change non-JS ACL editor to work with page groups too
Dan
parents:
21
diff
changeset
|
292 |
return @mysql_insert_id(); |
0a74676a2f2f
Made the move to Loch Ness, and got some basic page grouping functionality working. TODO: fix some UI issues in Javascript ACL editor and change non-JS ACL editor to work with page groups too
Dan
parents:
21
diff
changeset
|
293 |
} |
0a74676a2f2f
Made the move to Loch Ness, and got some basic page grouping functionality working. TODO: fix some UI issues in Javascript ACL editor and change non-JS ACL editor to work with page groups too
Dan
parents:
21
diff
changeset
|
294 |
|
1 | 295 |
function fetchrow($r = false) { |
296 |
$this->enable_errorhandler(); |
|
297 |
if(!$this->_conn) return false; |
|
298 |
if(!$r) $r = $this->latest_result; |
|
299 |
if(!$r) $this->_die('$db->fetchrow(): an invalid MySQL resource was passed.'); |
|
300 |
$row = mysql_fetch_assoc($r); |
|
301 |
$this->disable_errorhandler(); |
|
302 |
return $row; |
|
303 |
} |
|
304 |
||
305 |
function fetchrow_num($r = false) { |
|
306 |
$this->enable_errorhandler(); |
|
307 |
if(!$r) $r = $this->latest_result; |
|
308 |
if(!$r) $this->_die('$db->fetchrow(): an invalid MySQL resource was passed.'); |
|
309 |
$row = mysql_fetch_row($r); |
|
310 |
$this->disable_errorhandler(); |
|
311 |
return $row; |
|
312 |
} |
|
313 |
||
314 |
function numrows($r = false) { |
|
315 |
$this->enable_errorhandler(); |
|
316 |
if(!$r) $r = $this->latest_result; |
|
317 |
if(!$r) $this->_die('$db->fetchrow(): an invalid MySQL resource was passed.'); |
|
318 |
$n = mysql_num_rows($r); |
|
319 |
$this->disable_errorhandler(); |
|
320 |
return $n; |
|
321 |
} |
|
322 |
||
323 |
function escape($str) |
|
324 |
{ |
|
325 |
$this->enable_errorhandler(); |
|
326 |
$str = mysql_real_escape_string($str); |
|
327 |
$this->disable_errorhandler(); |
|
328 |
return $str; |
|
329 |
} |
|
330 |
||
331 |
function free_result($result = false) |
|
332 |
{ |
|
333 |
$this->enable_errorhandler(); |
|
334 |
if(!$result) |
|
335 |
$result = $this->latest_result; |
|
336 |
if(!$result) |
|
337 |
{ |
|
338 |
$this->disable_errorhandler(); |
|
339 |
return null; |
|
340 |
} |
|
341 |
mysql_free_result($result); |
|
342 |
$this->disable_errorhandler(); |
|
343 |
return null; |
|
344 |
} |
|
345 |
||
346 |
function close() { |
|
347 |
dc_here('dbal: closing MySQL connection'); |
|
348 |
mysql_close($this->_conn); |
|
349 |
unset($this->_conn); |
|
350 |
} |
|
351 |
||
352 |
// phpBB DBAL compatibility |
|
353 |
function sql_fetchrow($r = false) |
|
354 |
{ |
|
355 |
return $this->fetchrow($r); |
|
356 |
} |
|
357 |
function sql_freeresult($r = false) |
|
358 |
{ |
|
359 |
if(!$this->_conn) return false; |
|
360 |
if(!$r) $r = $this->latest_result; |
|
361 |
if(!$r) $this->_die('$db->fetchrow(): an invalid MySQL resource was passed.'); |
|
362 |
mysql_free_result($r); |
|
363 |
} |
|
364 |
function sql_numrows($r = false) |
|
365 |
{ |
|
366 |
if(!$this->_conn) return false; |
|
367 |
if(!$r) $r = $this->latest_result; |
|
368 |
if(!$r) $this->_die('$db->fetchrow(): an invalid MySQL resource was passed.'); |
|
369 |
return mysql_num_rows($r); |
|
370 |
} |
|
371 |
function sql_affectedrows($r = false, $f, $n) |
|
372 |
{ |
|
373 |
if(!$this->_conn) return false; |
|
374 |
if(!$r) $r = $this->latest_result; |
|
375 |
if(!$r) $this->_die('$db->fetchrow(): an invalid MySQL resource was passed.'); |
|
376 |
return mysql_affected_rows(); |
|
377 |
} |
|
378 |
||
379 |
function sql_type_cast(&$value) |
|
380 |
{ |
|
381 |
if ( is_float($value) ) |
|
382 |
{ |
|
383 |
return doubleval($value); |
|
384 |
} |
|
385 |
if ( is_integer($value) || is_bool($value) ) |
|
386 |
{ |
|
387 |
return intval($value); |
|
388 |
} |
|
389 |
if ( is_string($value) || empty($value) ) |
|
390 |
{ |
|
391 |
return '\'' . $this->sql_escape_string($value) . '\''; |
|
392 |
} |
|
393 |
// uncastable var : let's do a basic protection on it to prevent sql injection attempt |
|
394 |
return '\'' . $this->sql_escape_string(htmlspecialchars($value)) . '\''; |
|
395 |
} |
|
396 |
||
397 |
function sql_statement(&$fields, $fields_inc='') |
|
398 |
{ |
|
399 |
// init result |
|
400 |
$this->sql_fields = $this->sql_values = $this->sql_update = ''; |
|
401 |
if ( empty($fields) && empty($fields_inc) ) |
|
402 |
{ |
|
403 |
return; |
|
404 |
} |
|
405 |
||
406 |
// process |
|
407 |
if ( !empty($fields) ) |
|
408 |
{ |
|
409 |
$first = true; |
|
410 |
foreach ( $fields as $field => $value ) |
|
411 |
{ |
|
412 |
// field must contain a field name |
|
413 |
if ( !empty($field) && is_string($field) ) |
|
414 |
{ |
|
415 |
$value = $this->sql_type_cast($value); |
|
416 |
$this->sql_fields .= ( $first ? '' : ', ' ) . $field; |
|
417 |
$this->sql_values .= ( $first ? '' : ', ' ) . $value; |
|
418 |
$this->sql_update .= ( $first ? '' : ', ' ) . $field . ' = ' . $value; |
|
419 |
$first = false; |
|
420 |
} |
|
421 |
} |
|
422 |
} |
|
423 |
if ( !empty($fields_inc) ) |
|
424 |
{ |
|
425 |
foreach ( $fields_inc as $field => $indent ) |
|
426 |
{ |
|
427 |
if ( $indent != 0 ) |
|
428 |
{ |
|
429 |
$this->sql_update .= (empty($this->sql_update) ? '' : ', ') . $field . ' = ' . $field . ($indent < 0 ? ' - ' : ' + ') . abs($indent); |
|
430 |
} |
|
431 |
} |
|
432 |
} |
|
433 |
} |
|
434 |
||
435 |
function sql_stack_reset($id='') |
|
436 |
{ |
|
437 |
if ( empty($id) ) |
|
438 |
{ |
|
439 |
$this->sql_stack_fields = array(); |
|
440 |
$this->sql_stack_values = array(); |
|
441 |
} |
|
442 |
else |
|
443 |
{ |
|
444 |
$this->sql_stack_fields[$id] = array(); |
|
445 |
$this->sql_stack_values[$id] = array(); |
|
446 |
} |
|
447 |
} |
|
448 |
||
449 |
function sql_stack_statement(&$fields, $id='') |
|
450 |
{ |
|
451 |
$this->sql_statement($fields); |
|
452 |
if ( empty($id) ) |
|
453 |
{ |
|
454 |
$this->sql_stack_fields = $this->sql_fields; |
|
455 |
$this->sql_stack_values[] = '(' . $this->sql_values . ')'; |
|
456 |
} |
|
457 |
else |
|
458 |
{ |
|
459 |
$this->sql_stack_fields[$id] = $this->sql_fields; |
|
460 |
$this->sql_stack_values[$id][] = '(' . $this->sql_values . ')'; |
|
461 |
} |
|
462 |
} |
|
463 |
||
464 |
function sql_stack_insert($table, $transaction=false, $line='', $file='', $break_on_error=true, $id='') |
|
465 |
{ |
|
466 |
if ( (empty($id) && empty($this->sql_stack_values)) || (!empty($id) && empty($this->sql_stack_values[$id])) ) |
|
467 |
{ |
|
468 |
return false; |
|
469 |
} |
|
470 |
switch( SQL_LAYER ) |
|
471 |
{ |
|
472 |
case 'mysql': |
|
473 |
case 'mysql4': |
|
474 |
if ( empty($id) ) |
|
475 |
{ |
|
476 |
$sql = 'INSERT INTO ' . $table . ' |
|
477 |
(' . $this->sql_stack_fields . ') VALUES ' . implode(",\n", $this->sql_stack_values); |
|
478 |
} |
|
479 |
else |
|
480 |
{ |
|
481 |
$sql = 'INSERT INTO ' . $table . ' |
|
482 |
(' . $this->sql_stack_fields[$id] . ') VALUES ' . implode(",\n", $this->sql_stack_values[$id]); |
|
483 |
} |
|
484 |
$this->sql_stack_reset($id); |
|
485 |
return $this->sql_query($sql, $transaction, $line, $file, $break_on_error); |
|
486 |
break; |
|
487 |
default: |
|
488 |
$count_sql_stack_values = empty($id) ? count($this->sql_stack_values) : count($this->sql_stack_values[$id]); |
|
489 |
$result = !empty($count_sql_stack_values); |
|
490 |
for ( $i = 0; $i < $count_sql_stack_values; $i++ ) |
|
491 |
{ |
|
492 |
if ( empty($id) ) |
|
493 |
{ |
|
494 |
$sql = 'INSERT INTO ' . $table . ' |
|
495 |
(' . $this->sql_stack_fields . ') VALUES ' . $this->sql_stack_values[$i]; |
|
496 |
} |
|
497 |
else |
|
498 |
{ |
|
499 |
$sql = 'INSERT INTO ' . $table . ' |
|
500 |
(' . $this->sql_stack_fields[$id] . ') VALUES ' . $this->sql_stack_values[$id][$i]; |
|
501 |
} |
|
502 |
$result &= $this->sql_query($sql, $transaction, $line, $file, $break_on_error); |
|
503 |
} |
|
504 |
$this->sql_stack_reset($id); |
|
505 |
return $result; |
|
506 |
break; |
|
507 |
} |
|
508 |
} |
|
509 |
||
510 |
function sql_subquery($field, $sql, $line='', $file='', $break_on_error=true, $type=TYPE_INT) |
|
511 |
{ |
|
512 |
// sub-queries doable |
|
513 |
$this->sql_get_version(); |
|
514 |
if ( !in_array(SQL_LAYER, array('mysql', 'mysql4')) || (($this->sql_version[0] + ($this->sql_version[1] / 100)) >= 4.01) ) |
|
515 |
{ |
|
516 |
return $sql; |
|
517 |
} |
|
518 |
||
519 |
// no sub-queries |
|
520 |
$ids = array(); |
|
521 |
$result = $this->sql_query(trim($sql), false, $line, $file, $break_on_error); |
|
522 |
while ( $row = $this->sql_fetchrow($result) ) |
|
523 |
{ |
|
524 |
$ids[] = $type == TYPE_INT ? intval($row[$field]) : '\'' . $this->sql_escape_string($row[$field]) . '\''; |
|
525 |
} |
|
526 |
$this->sql_freeresult($result); |
|
527 |
return empty($ids) ? 'NULL' : implode(', ', $ids); |
|
528 |
} |
|
529 |
||
530 |
function sql_col_id($expr, $alias) |
|
531 |
{ |
|
532 |
$this->sql_get_version(); |
|
533 |
return in_array(SQL_LAYER, array('mysql', 'mysql4')) && (($this->sql_version[0] + ($this->sql_version[1] / 100)) <= 4.01) ? $alias : $expr; |
|
534 |
} |
|
535 |
||
536 |
function sql_get_version() |
|
537 |
{ |
|
538 |
if ( empty($this->sql_version) ) |
|
539 |
{ |
|
540 |
$this->sql_version = array(0, 0, 0); |
|
541 |
switch ( SQL_LAYER ) |
|
542 |
{ |
|
543 |
case 'mysql': |
|
544 |
case 'mysql4': |
|
545 |
if ( function_exists('mysql_get_server_info') ) |
|
546 |
{ |
|
547 |
$lo_version = explode('-', mysql_get_server_info()); |
|
548 |
$this->sql_version = explode('.', $lo_version[0]); |
|
549 |
$this->sql_version = array(intval($this->sql_version[0]), intval($this->sql_version[1]), intval($this->sql_version[2]), $lo_version[1]); |
|
550 |
} |
|
551 |
break; |
|
552 |
||
553 |
case 'postgresql': |
|
554 |
case 'mssql': |
|
555 |
case 'mssql-odbc': |
|
556 |
default: |
|
557 |
break; |
|
558 |
} |
|
559 |
} |
|
560 |
return $this->sql_version; |
|
561 |
} |
|
562 |
||
563 |
function sql_error() |
|
564 |
{ |
|
565 |
if ( $this->_conn ) |
|
566 |
{ |
|
567 |
return mysql_error(); |
|
568 |
} |
|
569 |
else |
|
570 |
{ |
|
571 |
return array(); |
|
572 |
} |
|
573 |
} |
|
574 |
function sql_escape_string($t) |
|
575 |
{ |
|
576 |
return mysql_real_escape_string($t); |
|
577 |
} |
|
578 |
function sql_close() |
|
579 |
{ |
|
580 |
$this->close(); |
|
581 |
} |
|
582 |
function sql_fetchrowset($query_id = 0) |
|
583 |
{ |
|
584 |
if( !$query_id ) |
|
585 |
{ |
|
586 |
$query_id = $this->query_result; |
|
587 |
} |
|
588 |
||
589 |
if( $query_id ) |
|
590 |
{ |
|
591 |
unset($this->rowset[$query_id]); |
|
592 |
unset($this->row[$query_id]); |
|
593 |
||
594 |
while($this->rowset[$query_id] = mysql_fetch_array($query_id, MYSQL_ASSOC)) |
|
595 |
{ |
|
596 |
$result[] = $this->rowset[$query_id]; |
|
597 |
} |
|
598 |
||
599 |
return $result; |
|
600 |
} |
|
601 |
else |
|
602 |
{ |
|
603 |
return false; |
|
604 |
} |
|
605 |
} |
|
606 |
} |
|
607 |
||
608 |
?> |