author | Dan |
Sat, 24 Nov 2007 00:53:23 -0500 | |
changeset 286 | b2f985e4cef3 |
parent 276 | acfdccf7a2bf |
child 288 | 9a1a32bc2050 |
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 |
|
142
ca9118d9c0f2
Rebrand as 1.0.2 (Coblynau); internal links are now parsed by RenderMan::parse_internal_links()
Dan
parents:
128
diff
changeset
|
5 |
* Version 1.0.2 (Coblynau) |
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(); |
|
286
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
30 |
if ( !isset($debug[0]['file']) ) |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
31 |
return false; |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
32 |
$debug = $debug[0]['file'] . ', line ' . $debug[0]['line']; |
1 | 33 |
echo "<b>$errtype:</b> $errstr<br />Error source:<pre>$debug</pre>"; |
34 |
} |
|
35 |
||
36 |
class mysql { |
|
286
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
37 |
var $num_queries, $query_backtrace, $query_times, $query_sources, $latest_result, $latest_query, $_conn, $sql_stack_fields, $sql_stack_values, $debug; |
1 | 38 |
var $row = array(); |
39 |
var $rowset = array(); |
|
40 |
var $errhandler; |
|
41 |
||
42 |
function enable_errorhandler() |
|
43 |
{ |
|
286
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
44 |
// echo "DBAL: enabling error handler<br />"; |
1 | 45 |
if ( function_exists('debug_backtrace') ) |
46 |
{ |
|
47 |
$this->errhandler = set_error_handler('db_error_handler'); |
|
48 |
} |
|
49 |
} |
|
50 |
||
51 |
function disable_errorhandler() |
|
52 |
{ |
|
286
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
53 |
// echo "DBAL: disabling error handler<br />"; |
1 | 54 |
if ( $this->errhandler ) |
55 |
{ |
|
56 |
set_error_handler($this->errhandler); |
|
57 |
} |
|
58 |
else |
|
59 |
{ |
|
60 |
restore_error_handler(); |
|
61 |
} |
|
62 |
} |
|
63 |
||
286
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
64 |
function sql_backtrace() |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
65 |
{ |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
66 |
return implode("\n-------------------------------------------------------------------\n", $this->query_backtrace); |
1 | 67 |
} |
68 |
||
69 |
function ensure_connection() |
|
70 |
{ |
|
71 |
if(!$this->_conn) |
|
72 |
{ |
|
73 |
$this->connect(); |
|
74 |
} |
|
75 |
} |
|
76 |
||
77 |
function _die($t = '') { |
|
78 |
if(defined('ENANO_HEADERS_SENT')) { |
|
79 |
ob_clean(); |
|
80 |
} |
|
81 |
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
|
82 |
$bt = $this->latest_query; // $this->sql_backtrace(); |
1 | 83 |
$e = htmlspecialchars(mysql_error()); |
84 |
if($e=='') $e='<none>'; |
|
91 | 85 |
$t = ( !empty($t) ) ? $t : '<No error description provided>'; |
86 |
global $email; |
|
87 |
$email_info = ( defined('ENANO_CONFIG_FETCHED') && is_object($email) ) ? ', at <' . $email->jscode() . $email->encryptEmail(getConfig('contact_email')) . '>' : ''; |
|
88 |
$internal_text = '<h3>The site was unable to finish serving your request.</h3> |
|
89 |
<p>We apologize for the inconveience, but an error occurred in the Enano database layer. Please report the full text of this page to the administrator of this site' . $email_info . '.</p> |
|
90 |
<p>Description or location of error: '.$t.'<br /> |
|
91 |
Error returned by MySQL extension: ' . $e . '<br /> |
|
92 |
Most recent SQL query:</p> |
|
93 |
<pre>'.$bt.'</pre>'; |
|
94 |
if(defined('ENANO_CONFIG_FETCHED')) die_semicritical('Database error', $internal_text); |
|
95 |
else grinding_halt('Database error', $internal_text); |
|
1 | 96 |
exit; |
97 |
} |
|
98 |
||
99 |
function die_json() |
|
100 |
{ |
|
101 |
$e = addslashes(htmlspecialchars(mysql_error())); |
|
102 |
$q = addslashes($this->latest_query); |
|
103 |
$t = "{'mode':'error','error':'An error occurred during database query.\nQuery was:\n $q\n\nError returned by MySQL: $e'}"; |
|
104 |
die($t); |
|
105 |
} |
|
106 |
||
107 |
function get_error($t = '') { |
|
108 |
header('HTTP/1.1 500 Internal Server Error'); |
|
109 |
$bt = $this->sql_backtrace(); |
|
110 |
$e = htmlspecialchars(mysql_error()); |
|
111 |
if($e=='') $e='<none>'; |
|
91 | 112 |
global $email; |
113 |
$email_info = ( defined('ENANO_CONFIG_FETCHED') && is_object($email) ) ? ', at <' . $email->jscode() . $email->encryptEmail(getConfig('contact_email')) . '>' : ''; |
|
114 |
$internal_text = '<h3>The site was unable to finish serving your request.</h3> |
|
115 |
<p>We apologize for the inconveience, but an error occurred in the Enano database layer. Please report the full text of this page to the administrator of this site' . $email_info . '.</p> |
|
116 |
<p>Description or location of error: '.$t.'<br /> |
|
117 |
Error returned by MySQL extension: ' . $e . '<br /> |
|
118 |
Most recent SQL query:</p> |
|
119 |
<pre>'.$bt.'</pre>'; |
|
120 |
return $internal_text; |
|
1 | 121 |
} |
122 |
||
268
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
123 |
function connect() |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
124 |
{ |
1 | 125 |
$this->enable_errorhandler(); |
268
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
126 |
|
1 | 127 |
dc_here('dbal: trying to connect....'); |
268
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
128 |
|
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
129 |
if ( defined('IN_ENANO_INSTALL') ) |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
130 |
{ |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
131 |
@include(ENANO_ROOT.'/config.new.php'); |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
132 |
} |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
133 |
else |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
134 |
{ |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
135 |
@include(ENANO_ROOT.'/config.php'); |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
136 |
} |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
137 |
|
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
138 |
if ( isset($crypto_key) ) |
1 | 139 |
unset($crypto_key); // Get this sucker out of memory fast |
268
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
140 |
|
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
141 |
if ( !defined('ENANO_INSTALLED') && !defined('MIDGET_INSTALLED') && !defined('IN_ENANO_INSTALL') ) |
1 | 142 |
{ |
143 |
dc_here('dbal: oops, looks like Enano isn\'t set up. Constants ENANO_INSTALLED, MIDGET_INSTALLED, and IN_ENANO_INSTALL are all undefined.'); |
|
272
e0ec986c0af3
Searching sucks, and Enano's search algorithm was complete bullcrap. So I rewrote it. No, it does not use Google search technology. Like they have a patent for using the Arial font on search result pages anyway.
Dan
parents:
268
diff
changeset
|
144 |
// scriptPath isn't set yet - we need to autodetect it to avoid infinite redirects |
e0ec986c0af3
Searching sucks, and Enano's search algorithm was complete bullcrap. So I rewrote it. No, it does not use Google search technology. Like they have a patent for using the Arial font on search result pages anyway.
Dan
parents:
268
diff
changeset
|
145 |
if ( !defined('scriptPath') ) |
e0ec986c0af3
Searching sucks, and Enano's search algorithm was complete bullcrap. So I rewrote it. No, it does not use Google search technology. Like they have a patent for using the Arial font on search result pages anyway.
Dan
parents:
268
diff
changeset
|
146 |
{ |
276
acfdccf7a2bf
Re-sync Oxygen and Mint and Oxygen simple with Oxygen main; a couple improvements to the redirect-on-no-config code
Dan
parents:
272
diff
changeset
|
147 |
if ( isset($_SERVER['PATH_INFO']) && !preg_match('/index\.php$/', $_SERVER['PATH_INFO']) ) |
272
e0ec986c0af3
Searching sucks, and Enano's search algorithm was complete bullcrap. So I rewrote it. No, it does not use Google search technology. Like they have a patent for using the Arial font on search result pages anyway.
Dan
parents:
268
diff
changeset
|
148 |
{ |
e0ec986c0af3
Searching sucks, and Enano's search algorithm was complete bullcrap. So I rewrote it. No, it does not use Google search technology. Like they have a patent for using the Arial font on search result pages anyway.
Dan
parents:
268
diff
changeset
|
149 |
$_SERVER['REQUEST_URI'] = preg_replace(';' . preg_quote($_SERVER['PATH_INFO']) . '$;', '', $_SERVER['REQUEST_URI']); |
e0ec986c0af3
Searching sucks, and Enano's search algorithm was complete bullcrap. So I rewrote it. No, it does not use Google search technology. Like they have a patent for using the Arial font on search result pages anyway.
Dan
parents:
268
diff
changeset
|
150 |
} |
e0ec986c0af3
Searching sucks, and Enano's search algorithm was complete bullcrap. So I rewrote it. No, it does not use Google search technology. Like they have a patent for using the Arial font on search result pages anyway.
Dan
parents:
268
diff
changeset
|
151 |
$sp = dirname($_SERVER['REQUEST_URI']); |
e0ec986c0af3
Searching sucks, and Enano's search algorithm was complete bullcrap. So I rewrote it. No, it does not use Google search technology. Like they have a patent for using the Arial font on search result pages anyway.
Dan
parents:
268
diff
changeset
|
152 |
if($sp == '/' || $sp == '\\') $sp = ''; |
e0ec986c0af3
Searching sucks, and Enano's search algorithm was complete bullcrap. So I rewrote it. No, it does not use Google search technology. Like they have a patent for using the Arial font on search result pages anyway.
Dan
parents:
268
diff
changeset
|
153 |
define('scriptPath', $sp); |
e0ec986c0af3
Searching sucks, and Enano's search algorithm was complete bullcrap. So I rewrote it. No, it does not use Google search technology. Like they have a patent for using the Arial font on search result pages anyway.
Dan
parents:
268
diff
changeset
|
154 |
define('contentPath', "$sp/index.php?title="); |
e0ec986c0af3
Searching sucks, and Enano's search algorithm was complete bullcrap. So I rewrote it. No, it does not use Google search technology. Like they have a patent for using the Arial font on search result pages anyway.
Dan
parents:
268
diff
changeset
|
155 |
} |
e0ec986c0af3
Searching sucks, and Enano's search algorithm was complete bullcrap. So I rewrote it. No, it does not use Google search technology. Like they have a patent for using the Arial font on search result pages anyway.
Dan
parents:
268
diff
changeset
|
156 |
$loc = scriptPath . '/install.php'; |
e0ec986c0af3
Searching sucks, and Enano's search algorithm was complete bullcrap. So I rewrote it. No, it does not use Google search technology. Like they have a patent for using the Arial font on search result pages anyway.
Dan
parents:
268
diff
changeset
|
157 |
// header("Location: $loc"); |
e0ec986c0af3
Searching sucks, and Enano's search algorithm was complete bullcrap. So I rewrote it. No, it does not use Google search technology. Like they have a patent for using the Arial font on search result pages anyway.
Dan
parents:
268
diff
changeset
|
158 |
redirect($loc, 'Enano not installed', 'We can\'t seem to find an Enano installation (valid config file). You will be transferred to the installation wizard momentarily...', 3); |
1 | 159 |
exit; |
160 |
} |
|
161 |
$this->_conn = @mysql_connect($dbhost, $dbuser, $dbpasswd); |
|
162 |
unset($dbuser); |
|
163 |
unset($dbpasswd); // Security |
|
268
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
164 |
|
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
165 |
if ( !$this->_conn ) |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
166 |
{ |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
167 |
dc_here('dbal: uhoh!<br />'.mysql_error()); |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
168 |
grinding_halt('Enano is having a problem', '<p>Error: couldn\'t connect to MySQL.<br />'.mysql_error().'</p>'); |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
169 |
} |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
170 |
|
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
171 |
// Reset some variables |
286
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
172 |
$this->query_backtrace = array(); |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
173 |
$this->query_times = array(); |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
174 |
$this->query_sources = array(); |
1 | 175 |
$this->num_queries = 0; |
268
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
176 |
|
286
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
177 |
$this->debug = ( defined('ENANO_DEBUG') ); |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
178 |
|
1 | 179 |
dc_here('dbal: we\'re in, selecting database...'); |
250
acb9d021b860
Database name can now contain dashes (as per requested at http://forum.enanocms.org/viewtopic.php?f=5&t=14); corrected some installer behavior issues with connecting as root and setting up permissions resulting in logs not being flushed, configs not being inserted, and what have you.
Dan
parents:
142
diff
changeset
|
180 |
$q = $this->sql_query('USE `'.$dbname.'`;'); |
268
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
181 |
|
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
182 |
if ( !$q ) |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
183 |
$this->_die('The database could not be selected.'); |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
184 |
|
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
185 |
// We're in! |
1 | 186 |
dc_here('dbal: connected to MySQL'); |
268
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
187 |
|
1 | 188 |
$this->disable_errorhandler(); |
268
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
189 |
return true; |
1 | 190 |
} |
191 |
||
268
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
192 |
function sql_query($q) |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
193 |
{ |
1 | 194 |
$this->enable_errorhandler(); |
286
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
195 |
|
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
196 |
if ( $this->debug && function_exists('debug_backtrace') ) |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
197 |
{ |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
198 |
$backtrace = @debug_backtrace(); |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
199 |
if ( is_array($backtrace) ) |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
200 |
{ |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
201 |
$bt = $backtrace[0]; |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
202 |
if ( isset($backtrace[1]['class']) ) |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
203 |
{ |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
204 |
if ( $backtrace[1]['class'] == 'sessionManager' ) |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
205 |
{ |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
206 |
$bt = $backtrace[1]; |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
207 |
} |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
208 |
} |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
209 |
$this->query_sources[$q] = substr($bt['file'], strlen(ENANO_ROOT) + 1) . ', line ' . $bt['line']; |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
210 |
} |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
211 |
unset($backtrace); |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
212 |
} |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
213 |
|
1 | 214 |
$this->num_queries++; |
286
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
215 |
$this->query_backtrace[] = $q; |
1 | 216 |
$this->latest_query = $q; |
217 |
dc_here('dbal: making SQL query:<br /><tt>'.$q.'</tt>'); |
|
268
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
218 |
// First make sure we have a connection |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
219 |
if ( !$this->_conn ) |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
220 |
{ |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
221 |
$this->_die('A database connection has not yet been established.'); |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
222 |
} |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
223 |
// Does this query look malicious? |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
224 |
if ( !$this->check_query($q) ) |
1 | 225 |
{ |
226 |
$this->report_query($q); |
|
227 |
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>'); |
|
228 |
} |
|
268
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
229 |
|
286
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
230 |
$time_start = microtime_float(); |
1 | 231 |
$r = mysql_query($q, $this->_conn); |
286
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
232 |
$this->query_times[$q] = microtime_float() - $time_start; |
1 | 233 |
$this->latest_result = $r; |
234 |
$this->disable_errorhandler(); |
|
235 |
return $r; |
|
236 |
} |
|
237 |
||
268
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
238 |
function sql_unbuffered_query($q) |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
239 |
{ |
1 | 240 |
$this->enable_errorhandler(); |
286
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
241 |
|
1 | 242 |
$this->num_queries++; |
286
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
243 |
$this->query_backtrace[] = '(UNBUFFERED) ' . $q; |
1 | 244 |
$this->latest_query = $q; |
245 |
dc_here('dbal: making SQL query:<br /><tt>'.$q.'</tt>'); |
|
268
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
246 |
// First make sure we have a connection |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
247 |
if ( !$this->_conn ) |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
248 |
{ |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
249 |
$this->_die('A database connection has not yet been established.'); |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
250 |
} |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
251 |
// Does this query look malicious? |
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
252 |
if ( !$this->check_query($q) ) |
1 | 253 |
{ |
254 |
$this->report_query($q); |
|
255 |
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>'); |
|
256 |
} |
|
268
58477ab3937f
Hopefully managed to put enough hacks in there to make renaming the config file the last step, so if it fails, it can be done manually
Dan
parents:
256
diff
changeset
|
257 |
|
286
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
258 |
$time_start = microtime_float(); |
1 | 259 |
$r = mysql_unbuffered_query($q, $this->_conn); |
286
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
260 |
$this->query_times[$q] = microtime_float() - $time_start; |
1 | 261 |
$this->latest_result = $r; |
262 |
$this->disable_errorhandler(); |
|
263 |
return $r; |
|
264 |
} |
|
265 |
||
266 |
/** |
|
267 |
* Checks a SQL query for possible signs of injection attempts |
|
268 |
* @param string $q the query to check |
|
269 |
* @return bool true if query passed check, otherwise false |
|
270 |
*/ |
|
271 |
||
272 |
function check_query($q, $debug = false) |
|
273 |
{ |
|
274 |
if($debug) echo "\$db->check_query(): checking query: ".htmlspecialchars($q).'<br />'."\n"; |
|
275 |
$sz = strlen($q); |
|
276 |
$quotechar = false; |
|
277 |
$quotepos = 0; |
|
278 |
$prev_is_quote = false; |
|
279 |
$just_started = false; |
|
128
01955bf53f96
Improved ban control page and allowed multiple entries/IP ranges; changed some parameters on jBox; user level changes are logged now
Dan
parents:
91
diff
changeset
|
280 |
for ( $i = 0; $i < strlen($q); $i++, $c = substr($q, $i, 1) ) |
1 | 281 |
{ |
282 |
$next = substr($q, $i+1, 1); |
|
283 |
$next2 = substr($q, $i+2, 1); |
|
284 |
$prev = substr($q, $i-1, 1); |
|
285 |
$prev2 = substr($q, $i-2, 1); |
|
286 |
if(isset($c) && in_array($c, Array('"', "'", '`'))) |
|
287 |
{ |
|
288 |
if($quotechar) |
|
289 |
{ |
|
128
01955bf53f96
Improved ban control page and allowed multiple entries/IP ranges; changed some parameters on jBox; user level changes are logged now
Dan
parents:
91
diff
changeset
|
290 |
if ( |
01955bf53f96
Improved ban control page and allowed multiple entries/IP ranges; changed some parameters on jBox; user level changes are logged now
Dan
parents:
91
diff
changeset
|
291 |
( $quotechar == $c && $quotechar != $next && ( $quotechar != $prev || $just_started ) && $prev != '\\') || |
1 | 292 |
( $prev2 == '\\' && $prev == $quotechar && $quotechar == $c ) |
293 |
) |
|
294 |
{ |
|
295 |
$quotechar = false; |
|
296 |
if($debug) echo('$db->check_query(): just finishing a quote section, quoted string: '.htmlspecialchars(substr($q, $quotepos, $i - $quotepos + 1)) . '<br />'); |
|
297 |
$q = substr($q, 0, $quotepos) . 'SAFE_QUOTE' . substr($q, $i + 1, strlen($q)); |
|
298 |
if($debug) echo('$db->check_query(): Filtered query: '.$q.'<br />'); |
|
299 |
$i = $quotepos; |
|
300 |
} |
|
301 |
} |
|
302 |
else |
|
303 |
{ |
|
304 |
$quotechar = $c; |
|
305 |
$quotepos = $i; |
|
128
01955bf53f96
Improved ban control page and allowed multiple entries/IP ranges; changed some parameters on jBox; user level changes are logged now
Dan
parents:
91
diff
changeset
|
306 |
$just_started = true; |
1 | 307 |
} |
308 |
if($debug) echo '$db->check_query(): found quote char as pos: '.$i.'<br />'; |
|
309 |
continue; |
|
310 |
} |
|
128
01955bf53f96
Improved ban control page and allowed multiple entries/IP ranges; changed some parameters on jBox; user level changes are logged now
Dan
parents:
91
diff
changeset
|
311 |
$just_started = false; |
1 | 312 |
} |
313 |
if(substr(trim($q), strlen(trim($q))-1, 1) == ';') $q = substr(trim($q), 0, strlen(trim($q))-1); |
|
314 |
for($i=0;$i<strlen($q);$i++,$c=substr($q, $i, 1)) |
|
315 |
{ |
|
128
01955bf53f96
Improved ban control page and allowed multiple entries/IP ranges; changed some parameters on jBox; user level changes are logged now
Dan
parents:
91
diff
changeset
|
316 |
if ( |
01955bf53f96
Improved ban control page and allowed multiple entries/IP ranges; changed some parameters on jBox; user level changes are logged now
Dan
parents:
91
diff
changeset
|
317 |
( ( $c == ';' && $i != $sz-1 ) || $c . substr($q, $i+1, 1) == '--' ) |
01955bf53f96
Improved ban control page and allowed multiple entries/IP ranges; changed some parameters on jBox; user level changes are logged now
Dan
parents:
91
diff
changeset
|
318 |
|| ( in_array($c, Array('"', "'", '`')) ) |
01955bf53f96
Improved ban control page and allowed multiple entries/IP ranges; changed some parameters on jBox; user level changes are logged now
Dan
parents:
91
diff
changeset
|
319 |
) // Don't permit semicolons in mid-query, and never allow comments |
1 | 320 |
{ |
321 |
// Injection attempt! |
|
322 |
if($debug) |
|
323 |
{ |
|
324 |
$e = ''; |
|
325 |
for($j=$i-5;$j<$i+5;$j++) |
|
326 |
{ |
|
327 |
if($j == $i) $e .= '<span style="color: red; text-decoration: underline;">' . $c . '</span>'; |
|
328 |
else $e .= $c; |
|
329 |
} |
|
330 |
echo 'Injection attempt caught at pos: '.$i.'<br />'; |
|
331 |
} |
|
332 |
return false; |
|
333 |
} |
|
334 |
} |
|
128
01955bf53f96
Improved ban control page and allowed multiple entries/IP ranges; changed some parameters on jBox; user level changes are logged now
Dan
parents:
91
diff
changeset
|
335 |
if ( preg_match('/[\s]+(SAFE_QUOTE|[\S]+)=\\1($|[\s]+)/', $q, $match) ) |
01955bf53f96
Improved ban control page and allowed multiple entries/IP ranges; changed some parameters on jBox; user level changes are logged now
Dan
parents:
91
diff
changeset
|
336 |
{ |
01955bf53f96
Improved ban control page and allowed multiple entries/IP ranges; changed some parameters on jBox; user level changes are logged now
Dan
parents:
91
diff
changeset
|
337 |
if ( $debug ) echo 'Found always-true test in query, injection attempt caught, match:<br />' . '<pre>' . print_r($match, true) . '</pre>'; |
01955bf53f96
Improved ban control page and allowed multiple entries/IP ranges; changed some parameters on jBox; user level changes are logged now
Dan
parents:
91
diff
changeset
|
338 |
return false; |
01955bf53f96
Improved ban control page and allowed multiple entries/IP ranges; changed some parameters on jBox; user level changes are logged now
Dan
parents:
91
diff
changeset
|
339 |
} |
1 | 340 |
return true; |
341 |
} |
|
342 |
||
343 |
/** |
|
344 |
* Set the internal result pointer to X |
|
345 |
* @param int $pos The number of the row |
|
346 |
* @param resource $result The MySQL result resource - if not given, the latest cached query is assumed |
|
347 |
* @return true on success, false on failure |
|
348 |
*/ |
|
349 |
||
350 |
function sql_data_seek($pos, $result = false) |
|
351 |
{ |
|
352 |
$this->enable_errorhandler(); |
|
353 |
if(!$result) |
|
354 |
$result = $this->latest_result; |
|
355 |
if(!$result) |
|
356 |
{ |
|
357 |
$this->disable_errorhandler(); |
|
358 |
return false; |
|
359 |
} |
|
360 |
if(mysql_data_seek($result, $pos)) |
|
361 |
{ |
|
362 |
$this->disable_errorhandler(); |
|
363 |
return true; |
|
364 |
} |
|
365 |
else |
|
366 |
{ |
|
367 |
$this->disable_errorhandler(); |
|
368 |
return false; |
|
369 |
} |
|
370 |
} |
|
371 |
||
372 |
/** |
|
373 |
* Reports a bad query to the admin |
|
374 |
* @param string $query the naughty query |
|
375 |
* @access private |
|
376 |
*/ |
|
377 |
||
378 |
function report_query($query) |
|
379 |
{ |
|
380 |
global $session; |
|
381 |
if(is_object($session) && defined('ENANO_MAINSTREAM')) |
|
382 |
$username = $session->username; |
|
383 |
else |
|
384 |
$username = 'Unavailable'; |
|
385 |
$query = $this->escape($query); |
|
386 |
$q = $this->sql_query('INSERT INTO '.table_prefix.'logs(log_type, action, time_id, date_string, page_text, author, edit_summary) |
|
387 |
VALUES(\'security\', \'sql_inject\', '.time().', \'\', \''.$query.'\', \''.$username.'\', \''.$_SERVER['REMOTE_ADDR'].'\');'); |
|
388 |
} |
|
389 |
||
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
|
390 |
/** |
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
|
391 |
* 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
|
392 |
* @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
|
393 |
*/ |
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
|
394 |
|
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
|
395 |
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
|
396 |
{ |
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
|
397 |
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
|
398 |
} |
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
|
399 |
|
1 | 400 |
function fetchrow($r = false) { |
401 |
$this->enable_errorhandler(); |
|
402 |
if(!$this->_conn) return false; |
|
403 |
if(!$r) $r = $this->latest_result; |
|
404 |
if(!$r) $this->_die('$db->fetchrow(): an invalid MySQL resource was passed.'); |
|
405 |
$row = mysql_fetch_assoc($r); |
|
406 |
$this->disable_errorhandler(); |
|
407 |
return $row; |
|
408 |
} |
|
409 |
||
410 |
function fetchrow_num($r = false) { |
|
411 |
$this->enable_errorhandler(); |
|
412 |
if(!$r) $r = $this->latest_result; |
|
413 |
if(!$r) $this->_die('$db->fetchrow(): an invalid MySQL resource was passed.'); |
|
414 |
$row = mysql_fetch_row($r); |
|
415 |
$this->disable_errorhandler(); |
|
416 |
return $row; |
|
417 |
} |
|
418 |
||
419 |
function numrows($r = false) { |
|
420 |
$this->enable_errorhandler(); |
|
421 |
if(!$r) $r = $this->latest_result; |
|
422 |
if(!$r) $this->_die('$db->fetchrow(): an invalid MySQL resource was passed.'); |
|
423 |
$n = mysql_num_rows($r); |
|
424 |
$this->disable_errorhandler(); |
|
425 |
return $n; |
|
426 |
} |
|
427 |
||
428 |
function escape($str) |
|
429 |
{ |
|
430 |
$this->enable_errorhandler(); |
|
431 |
$str = mysql_real_escape_string($str); |
|
432 |
$this->disable_errorhandler(); |
|
433 |
return $str; |
|
434 |
} |
|
435 |
||
436 |
function free_result($result = false) |
|
437 |
{ |
|
438 |
$this->enable_errorhandler(); |
|
439 |
if(!$result) |
|
440 |
$result = $this->latest_result; |
|
441 |
if(!$result) |
|
442 |
{ |
|
443 |
$this->disable_errorhandler(); |
|
444 |
return null; |
|
445 |
} |
|
446 |
mysql_free_result($result); |
|
447 |
$this->disable_errorhandler(); |
|
448 |
return null; |
|
449 |
} |
|
450 |
||
451 |
function close() { |
|
452 |
dc_here('dbal: closing MySQL connection'); |
|
453 |
mysql_close($this->_conn); |
|
454 |
unset($this->_conn); |
|
455 |
} |
|
456 |
||
457 |
// phpBB DBAL compatibility |
|
458 |
function sql_fetchrow($r = false) |
|
459 |
{ |
|
460 |
return $this->fetchrow($r); |
|
461 |
} |
|
462 |
function sql_freeresult($r = false) |
|
463 |
{ |
|
464 |
if(!$this->_conn) return false; |
|
465 |
if(!$r) $r = $this->latest_result; |
|
466 |
if(!$r) $this->_die('$db->fetchrow(): an invalid MySQL resource was passed.'); |
|
467 |
mysql_free_result($r); |
|
468 |
} |
|
469 |
function sql_numrows($r = false) |
|
470 |
{ |
|
471 |
if(!$this->_conn) return false; |
|
472 |
if(!$r) $r = $this->latest_result; |
|
473 |
if(!$r) $this->_die('$db->fetchrow(): an invalid MySQL resource was passed.'); |
|
474 |
return mysql_num_rows($r); |
|
475 |
} |
|
476 |
function sql_affectedrows($r = false, $f, $n) |
|
477 |
{ |
|
478 |
if(!$this->_conn) return false; |
|
479 |
if(!$r) $r = $this->latest_result; |
|
480 |
if(!$r) $this->_die('$db->fetchrow(): an invalid MySQL resource was passed.'); |
|
481 |
return mysql_affected_rows(); |
|
482 |
} |
|
483 |
||
484 |
function sql_type_cast(&$value) |
|
485 |
{ |
|
486 |
if ( is_float($value) ) |
|
487 |
{ |
|
488 |
return doubleval($value); |
|
489 |
} |
|
490 |
if ( is_integer($value) || is_bool($value) ) |
|
491 |
{ |
|
492 |
return intval($value); |
|
493 |
} |
|
494 |
if ( is_string($value) || empty($value) ) |
|
495 |
{ |
|
496 |
return '\'' . $this->sql_escape_string($value) . '\''; |
|
497 |
} |
|
498 |
// uncastable var : let's do a basic protection on it to prevent sql injection attempt |
|
499 |
return '\'' . $this->sql_escape_string(htmlspecialchars($value)) . '\''; |
|
500 |
} |
|
501 |
||
502 |
function sql_statement(&$fields, $fields_inc='') |
|
503 |
{ |
|
504 |
// init result |
|
505 |
$this->sql_fields = $this->sql_values = $this->sql_update = ''; |
|
506 |
if ( empty($fields) && empty($fields_inc) ) |
|
507 |
{ |
|
508 |
return; |
|
509 |
} |
|
510 |
||
511 |
// process |
|
512 |
if ( !empty($fields) ) |
|
513 |
{ |
|
514 |
$first = true; |
|
515 |
foreach ( $fields as $field => $value ) |
|
516 |
{ |
|
517 |
// field must contain a field name |
|
518 |
if ( !empty($field) && is_string($field) ) |
|
519 |
{ |
|
520 |
$value = $this->sql_type_cast($value); |
|
521 |
$this->sql_fields .= ( $first ? '' : ', ' ) . $field; |
|
522 |
$this->sql_values .= ( $first ? '' : ', ' ) . $value; |
|
523 |
$this->sql_update .= ( $first ? '' : ', ' ) . $field . ' = ' . $value; |
|
524 |
$first = false; |
|
525 |
} |
|
526 |
} |
|
527 |
} |
|
528 |
if ( !empty($fields_inc) ) |
|
529 |
{ |
|
530 |
foreach ( $fields_inc as $field => $indent ) |
|
531 |
{ |
|
532 |
if ( $indent != 0 ) |
|
533 |
{ |
|
534 |
$this->sql_update .= (empty($this->sql_update) ? '' : ', ') . $field . ' = ' . $field . ($indent < 0 ? ' - ' : ' + ') . abs($indent); |
|
535 |
} |
|
536 |
} |
|
537 |
} |
|
538 |
} |
|
539 |
||
540 |
function sql_stack_reset($id='') |
|
541 |
{ |
|
542 |
if ( empty($id) ) |
|
543 |
{ |
|
544 |
$this->sql_stack_fields = array(); |
|
545 |
$this->sql_stack_values = array(); |
|
546 |
} |
|
547 |
else |
|
548 |
{ |
|
549 |
$this->sql_stack_fields[$id] = array(); |
|
550 |
$this->sql_stack_values[$id] = array(); |
|
551 |
} |
|
552 |
} |
|
553 |
||
554 |
function sql_stack_statement(&$fields, $id='') |
|
555 |
{ |
|
556 |
$this->sql_statement($fields); |
|
557 |
if ( empty($id) ) |
|
558 |
{ |
|
559 |
$this->sql_stack_fields = $this->sql_fields; |
|
560 |
$this->sql_stack_values[] = '(' . $this->sql_values . ')'; |
|
561 |
} |
|
562 |
else |
|
563 |
{ |
|
564 |
$this->sql_stack_fields[$id] = $this->sql_fields; |
|
565 |
$this->sql_stack_values[$id][] = '(' . $this->sql_values . ')'; |
|
566 |
} |
|
567 |
} |
|
568 |
||
569 |
function sql_stack_insert($table, $transaction=false, $line='', $file='', $break_on_error=true, $id='') |
|
570 |
{ |
|
571 |
if ( (empty($id) && empty($this->sql_stack_values)) || (!empty($id) && empty($this->sql_stack_values[$id])) ) |
|
572 |
{ |
|
573 |
return false; |
|
574 |
} |
|
575 |
switch( SQL_LAYER ) |
|
576 |
{ |
|
577 |
case 'mysql': |
|
578 |
case 'mysql4': |
|
579 |
if ( empty($id) ) |
|
580 |
{ |
|
581 |
$sql = 'INSERT INTO ' . $table . ' |
|
582 |
(' . $this->sql_stack_fields . ') VALUES ' . implode(",\n", $this->sql_stack_values); |
|
583 |
} |
|
584 |
else |
|
585 |
{ |
|
586 |
$sql = 'INSERT INTO ' . $table . ' |
|
587 |
(' . $this->sql_stack_fields[$id] . ') VALUES ' . implode(",\n", $this->sql_stack_values[$id]); |
|
588 |
} |
|
589 |
$this->sql_stack_reset($id); |
|
590 |
return $this->sql_query($sql, $transaction, $line, $file, $break_on_error); |
|
591 |
break; |
|
592 |
default: |
|
593 |
$count_sql_stack_values = empty($id) ? count($this->sql_stack_values) : count($this->sql_stack_values[$id]); |
|
594 |
$result = !empty($count_sql_stack_values); |
|
595 |
for ( $i = 0; $i < $count_sql_stack_values; $i++ ) |
|
596 |
{ |
|
597 |
if ( empty($id) ) |
|
598 |
{ |
|
599 |
$sql = 'INSERT INTO ' . $table . ' |
|
600 |
(' . $this->sql_stack_fields . ') VALUES ' . $this->sql_stack_values[$i]; |
|
601 |
} |
|
602 |
else |
|
603 |
{ |
|
604 |
$sql = 'INSERT INTO ' . $table . ' |
|
605 |
(' . $this->sql_stack_fields[$id] . ') VALUES ' . $this->sql_stack_values[$id][$i]; |
|
606 |
} |
|
607 |
$result &= $this->sql_query($sql, $transaction, $line, $file, $break_on_error); |
|
608 |
} |
|
609 |
$this->sql_stack_reset($id); |
|
610 |
return $result; |
|
611 |
break; |
|
612 |
} |
|
613 |
} |
|
614 |
||
615 |
function sql_subquery($field, $sql, $line='', $file='', $break_on_error=true, $type=TYPE_INT) |
|
616 |
{ |
|
617 |
// sub-queries doable |
|
618 |
$this->sql_get_version(); |
|
619 |
if ( !in_array(SQL_LAYER, array('mysql', 'mysql4')) || (($this->sql_version[0] + ($this->sql_version[1] / 100)) >= 4.01) ) |
|
620 |
{ |
|
621 |
return $sql; |
|
622 |
} |
|
623 |
||
624 |
// no sub-queries |
|
625 |
$ids = array(); |
|
626 |
$result = $this->sql_query(trim($sql), false, $line, $file, $break_on_error); |
|
627 |
while ( $row = $this->sql_fetchrow($result) ) |
|
628 |
{ |
|
629 |
$ids[] = $type == TYPE_INT ? intval($row[$field]) : '\'' . $this->sql_escape_string($row[$field]) . '\''; |
|
630 |
} |
|
631 |
$this->sql_freeresult($result); |
|
632 |
return empty($ids) ? 'NULL' : implode(', ', $ids); |
|
633 |
} |
|
634 |
||
635 |
function sql_col_id($expr, $alias) |
|
636 |
{ |
|
637 |
$this->sql_get_version(); |
|
638 |
return in_array(SQL_LAYER, array('mysql', 'mysql4')) && (($this->sql_version[0] + ($this->sql_version[1] / 100)) <= 4.01) ? $alias : $expr; |
|
639 |
} |
|
640 |
||
641 |
function sql_get_version() |
|
642 |
{ |
|
643 |
if ( empty($this->sql_version) ) |
|
644 |
{ |
|
645 |
$this->sql_version = array(0, 0, 0); |
|
646 |
switch ( SQL_LAYER ) |
|
647 |
{ |
|
648 |
case 'mysql': |
|
649 |
case 'mysql4': |
|
650 |
if ( function_exists('mysql_get_server_info') ) |
|
651 |
{ |
|
652 |
$lo_version = explode('-', mysql_get_server_info()); |
|
653 |
$this->sql_version = explode('.', $lo_version[0]); |
|
654 |
$this->sql_version = array(intval($this->sql_version[0]), intval($this->sql_version[1]), intval($this->sql_version[2]), $lo_version[1]); |
|
655 |
} |
|
656 |
break; |
|
657 |
||
658 |
case 'postgresql': |
|
659 |
case 'mssql': |
|
660 |
case 'mssql-odbc': |
|
661 |
default: |
|
662 |
break; |
|
663 |
} |
|
664 |
} |
|
665 |
return $this->sql_version; |
|
666 |
} |
|
667 |
||
668 |
function sql_error() |
|
669 |
{ |
|
670 |
if ( $this->_conn ) |
|
671 |
{ |
|
672 |
return mysql_error(); |
|
673 |
} |
|
674 |
else |
|
675 |
{ |
|
676 |
return array(); |
|
677 |
} |
|
678 |
} |
|
679 |
function sql_escape_string($t) |
|
680 |
{ |
|
681 |
return mysql_real_escape_string($t); |
|
682 |
} |
|
683 |
function sql_close() |
|
684 |
{ |
|
685 |
$this->close(); |
|
686 |
} |
|
687 |
function sql_fetchrowset($query_id = 0) |
|
688 |
{ |
|
689 |
if( !$query_id ) |
|
690 |
{ |
|
691 |
$query_id = $this->query_result; |
|
692 |
} |
|
693 |
||
694 |
if( $query_id ) |
|
695 |
{ |
|
696 |
unset($this->rowset[$query_id]); |
|
697 |
unset($this->row[$query_id]); |
|
698 |
||
699 |
while($this->rowset[$query_id] = mysql_fetch_array($query_id, MYSQL_ASSOC)) |
|
700 |
{ |
|
701 |
$result[] = $this->rowset[$query_id]; |
|
702 |
} |
|
703 |
||
704 |
return $result; |
|
705 |
} |
|
706 |
else |
|
707 |
{ |
|
708 |
return false; |
|
709 |
} |
|
710 |
} |
|
286
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
711 |
/** |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
712 |
* Generates and outputs a report of all the SQL queries made during execution. Should only be called after everything's over with. |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
713 |
*/ |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
714 |
|
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
715 |
function sql_report() |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
716 |
{ |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
717 |
global $db, $session, $paths, $template, $plugins; // Common objects |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
718 |
if ( !$session->get_permissions('mod_misc') ) |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
719 |
{ |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
720 |
die_friendly('Access denied', '<p>You are not authorized to generate a SQL backtrace.</p>'); |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
721 |
} |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
722 |
// Create copies of variables that may be changed after header is called |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
723 |
$backtrace = $this->query_backtrace; |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
724 |
$times = $this->query_times; |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
725 |
$template->header(); |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
726 |
echo '<h3>SQL query log and timetable</h3>'; |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
727 |
echo '<div class="tblholder"> |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
728 |
<table border="0" cellspacing="1" cellpadding="4">'; |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
729 |
$i = 0; |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
730 |
foreach ( $backtrace as $query ) |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
731 |
{ |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
732 |
$i++; |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
733 |
$unbuffered = false; |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
734 |
if ( substr($query, 0, 13) == '(UNBUFFERED) ' ) |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
735 |
{ |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
736 |
$query = substr($query, 13); |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
737 |
$unbuffered = true; |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
738 |
} |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
739 |
if ( $i == 1 ) |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
740 |
{ |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
741 |
echo '<tr> |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
742 |
<th colspan="2">SQL backtrace for a normal page load of ' . htmlspecialchars($paths->cpage['urlname']) . '</th> |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
743 |
</tr>'; |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
744 |
} |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
745 |
else |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
746 |
{ |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
747 |
echo '<tr> |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
748 |
<th class="subhead" colspan="2"> </th> |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
749 |
</tr>'; |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
750 |
} |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
751 |
echo '<tr> |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
752 |
<td class="row2">Query:</td> |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
753 |
<td class="row1"><pre>' . htmlspecialchars($query) . '</pre></td> |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
754 |
</tr> |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
755 |
<tr> |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
756 |
<td class="row2">Time:</td> |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
757 |
<td class="row1">' . number_format($this->query_times[$query], 6) . ' seconds</td> |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
758 |
</tr> |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
759 |
<tr> |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
760 |
<td class="row2">Unbuffered:</td> |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
761 |
<td class="row1">' . ( $unbuffered ? 'Yes' : 'No' ) . '</td> |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
762 |
</tr>'; |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
763 |
if ( isset($this->query_sources[$query]) ) |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
764 |
{ |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
765 |
echo '<tr> |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
766 |
<td class="row2">Called from:</td> |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
767 |
<td class="row1">' . $this->query_sources[$query] . '</td> |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
768 |
</tr>'; |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
769 |
} |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
770 |
} |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
771 |
echo ' </table> |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
772 |
</div>'; |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
773 |
$template->footer(); |
b2f985e4cef3
Fixed a number of issues with SQL query readability and some undefined index-ish errors; consequently the SQL report feature was added
Dan
parents:
276
diff
changeset
|
774 |
} |
1 | 775 |
} |
776 |
||
777 |
?> |