link) or $db->error($query);
}
//--------------------------------------------------
// Explain how the query is executed
$htmlExplain = '';
if (preg_match('/^\W*\(?\W*SELECT/i', $query)) {
$htmlExplain .= '
';
$tableHeadersPrinted = false;
$rst = @mysql_query('EXPLAIN ' . $query, $db->link);
if ($rst) {
while ($row = mysql_fetch_assoc($rst)) {
if ($tableHeadersPrinted == false) {
$tableHeadersPrinted = true;
$htmlExplain .= '
';
foreach ($row as $key => $value) {
$htmlExplain .= '
| ' . html($key) . ' | ';
}
$htmlExplain .= '
';
}
$htmlExplain .= '
';
foreach ($row as $key => $value) {
$htmlExplain .= '
| ' . ($key == 'type' ? '' : '') . ($value == '' ? ' ' : html($value)) . ($key == 'type' ? '' : '') . ' | ';
}
$htmlExplain .= '
';
}
}
$htmlExplain .= '
';
}
//--------------------------------------------------
// Get all the table references, and if any of them
// have a "deleted" column, make sure that it's
// being used
$htmlTest = '';
if (preg_match('/^(SELECT|UPDATE|DELETE)/i', ltrim($query))) {
$tables = array();
if (preg_match('/WHERE(.*)/ims', $query, $matches)) {
$whereClause = $matches[1];
$whereClause = preg_replace('/ORDER BY.*/ms', '', $whereClause);
$whereClause = preg_replace('/LIMIT\W+[0-9].*/ms', '', $whereClause);
} else {
$whereClause = '';
}
if (DB_T_PREFIX != '') {
preg_match_all('/\b(' . preg_quote(DB_T_PREFIX, '/') .'[a-z0-9_]+)( AS ([a-z0-9]+))?/', $query, $matches, PREG_SET_ORDER);
} else {
$matches = array();
preg_match_all('/(UPDATE|FROM)([^\(]*?)(WHERE|GROUP BY|HAVING|ORDER BY|LIMIT|$)/isD', $query, $fromMatches, PREG_SET_ORDER);
foreach ($fromMatches as $cMatch) {
foreach (preg_split('/(,|(NATURAL\s+)?(LEFT|RIGHT|INNER|CROSS)\s+(OUTER\s+)?JOIN)/', $cMatch[2]) as $cTable) {
if (preg_match('/([a-z0-9_]+)( AS ([a-z0-9]+))?/', $cTable, $cRefs)) {
$matches[] = $cRefs;
}
}
}
}
foreach ($matches as $cTable) {
$found = array();
foreach ($GLOBALS['debugRequiredFields'] as $requiredField) {
$rst = @mysql_query('SHOW COLUMNS FROM ' . $cTable[1] . ' LIKE "' . $requiredField . '"', $db->link);
if ($rst && $row = mysql_fetch_assoc($rst)) {
//--------------------------------------------------
// Found
$found[] = $requiredField;
//--------------------------------------------------
// Table name
$requiredClause = (isset($cTable[3]) ? '`' . $cTable[3] . '`.' : '') . '`' . $requiredField . '`';
//--------------------------------------------------
// Test
if (!preg_match('/' . str_replace('`', '`?', preg_quote($requiredClause, '/')) . ' (=|>|>=|<|<=|!=) /', $whereClause)) {
$GLOBALS['debugShowOutput'] = false;
exit('
Error
Missing reference to "' . html($requiredField) . '" column on the table "' . html($cTable[1]) . '".
' . html($query) . '
');
}
}
}
$tables[] = $cTable[1] . ': ' . (count($found) > 0 ? implode(', ', $found) : 'N/A');
}
if (count($tables) > 0) {
$htmlTest .= '
';
foreach ($tables as $cResult) {
$htmlTest .= '
- ' . preg_replace('/: (.*)/', ': $1', html($cResult)) . '
';
}
$htmlTest .= '
';
}
}
//--------------------------------------------------
// Time start
$timeStart = explode(' ', microtime());
$timeStart = ((float)$timeStart[0] + (float)$timeStart[1]);
//--------------------------------------------------
// Run query
$result = mysql_query($query, $db->link) or $db->error($query);
//--------------------------------------------------
// Time end
$timeEnd = explode(' ', microtime());
$timeEnd = ((float)$timeEnd[0] + (float)$timeEnd[1]);
$timeTotal = round(($timeEnd - $timeStart), 3);
$GLOBALS['debugQueryTime'] += $timeTotal;
//--------------------------------------------------
// Create debug output
$GLOBALS['htmlDebugOutput'] .= '
' . nl2br(preg_replace('/^[ \t]*(?! |\t|SELECT|UPDATE|DELETE|INSERT|FROM|LEFT|SET|WHERE|GROUP|ORDER|LIMIT)/m', ' \0', html($query))) . '
Time Elapsed: ' . html($timeTotal) . '
' . $htmlExplain . '
' . $htmlTest . '
';
//--------------------------------------------------
// Return the result
return $result;
}
//--------------------------------------------------
// Allow script to add note
function debugAddNote($note = NULL) {
//--------------------------------------------------
// Time position
$timeEnd = explode(' ', microtime());
$timeEnd = ((float)$timeEnd[0] + (float)$timeEnd[1]);
$timeTotal = round(($timeEnd - $GLOBALS['debugTimeStart']), 3);
//--------------------------------------------------
// Note
$GLOBALS['htmlDebugOutput'] .= '
' . ($note === NULL ? '' : '
' . nl2br(str_replace(' ', ' ', html($note))) . '
') . '
Time Elapsed: ' . html($timeTotal) . '
';
}
//--------------------------------------------------
// Add debug output
function debugShutdown($buffer) {
//--------------------------------------------------
// Suppression
if ($GLOBALS['debugShowOutput'] == false) {
return $buffer;
}
//--------------------------------------------------
// Time taken
$timeEnd = explode(' ', microtime());
$timeEnd = ((float)$timeEnd[0] + (float)$timeEnd[1]);
$timeTotal = round(($timeEnd - $GLOBALS['debugTimeStart']), 3);
$htmlOutput = '
Time Elapsed: ' . html($timeTotal) . '
Query time: ' . html($GLOBALS['debugQueryTime']) . '
';
//--------------------------------------------------
// Current debug output
$htmlOutput .= $GLOBALS['htmlDebugOutput'];
//--------------------------------------------------
// Wrapper
if ($htmlOutput != '') {
$htmlOutput = "\n\n\n\n\n" . '
' . "\n\n\n\n\n";
}
//--------------------------------------------------
// Add
$pos = strpos(strtolower($buffer), '