-
Notifications
You must be signed in to change notification settings - Fork 58
/
AdminDebug.php
248 lines (212 loc) · 6.86 KB
/
AdminDebug.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
/**
* @name ElkArte Forum
* @copyright ElkArte Forum contributors
* @license BSD http://opensource.org/licenses/BSD-3-Clause
*
* This software is a derived product, based on:
*
* Simple Machines Forum (SMF)
* copyright: 2011 Simple Machines (http://www.simplemachines.org)
* license: BSD, See included LICENSE.TXT for terms and conditions.
*
* @version 1.0 Alpha
*
* Functions concerned with viewing queries, and is used for debugging.
*
*/
if (!defined('ELK'))
die('No access...');
class AdminDebug_Controller extends Action_Controller
{
/**
* Main dispatcher.
*
* @see Action_Controller::action_index()
*/
public function action_index()
{
// what to do first... viewquery! What, it'll work or it won't.
// $this->action_viewquery();
}
/**
* Show the database queries for debugging
* What this does:
* - Toggles the session variable 'view_queries'.
* - Views a list of queries and analyzes them.
* - Requires the admin_forum permission.
* - Is accessed via ?action=viewquery.
* - Strings in this function have not been internationalized.
*/
public function action_viewquery()
{
global $scripturl, $settings, $context, $db_connection, $txt, $db_show_debug;
// We should have debug mode enabled, as well as something to display!
if (!isset($db_show_debug) || $db_show_debug !== true || !isset($_SESSION['debug']))
fatal_lang_error('no_access', false);
// Don't allow except for administrators.
isAllowedTo('admin_forum');
// If we're just hiding/showing, do it now.
if (isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'hide')
{
$_SESSION['view_queries'] = $_SESSION['view_queries'] == 1 ? 0 : 1;
if (strpos($_SESSION['old_url'], 'action=viewquery') !== false)
redirectexit();
else
redirectexit($_SESSION['old_url']);
}
call_integration_hook('integrate_egg_nog');
$query_id = isset($_REQUEST['qq']) ? (int) $_REQUEST['qq'] - 1 : -1;
echo '<!DOCTYPE html>
<html', $context['right_to_left'] ? 'dir="rtl"' : '', '>
<head>
<title>', $context['forum_name_html_safe'], '</title>
<link rel="stylesheet" type="text/css" href="', $settings['theme_url'], '/css/index', $context['theme_variant'], '.css?alp21" />
<style type="text/css">
body {
margin: 1ex;
}
body, td, th, .normaltext {
font-size: x-small;
}
.smalltext {
font-size: xx-small;
}
</style>
</head>
<body id="help_popup">
<div class="tborder windowbg description">';
// db work...
$db = database();
foreach ($_SESSION['debug'] as $q => $query_data)
{
// Fix the indentation....
$query_data['q'] = ltrim(str_replace("\r", '', $query_data['q']), "\n");
$query = explode("\n", $query_data['q']);
$min_indent = 0;
foreach ($query as $line)
{
preg_match('/^(\t*)/', $line, $temp);
if (strlen($temp[0]) < $min_indent || $min_indent == 0)
$min_indent = strlen($temp[0]);
}
foreach ($query as $l => $dummy)
$query[$l] = substr($dummy, $min_indent);
$query_data['q'] = implode("\n", $query);
// Make the filenames look a bit better.
if (isset($query_data['f']))
$query_data['f'] = preg_replace('~^' . preg_quote(BOARDDIR, '~') . '~', '...', $query_data['f']);
$is_select_query = substr(trim($query_data['q']), 0, 6) == 'SELECT';
if ($is_select_query)
$select = $query_data['q'];
elseif (preg_match('~^INSERT(?: IGNORE)? INTO \w+(?:\s+\([^)]+\))?\s+(SELECT .+)$~s', trim($query_data['q']), $matches) != 0)
{
$is_select_query = true;
$select = $matches[1];
}
elseif (preg_match('~^CREATE TEMPORARY TABLE .+?(SELECT .+)$~s', trim($query_data['q']), $matches) != 0)
{
$is_select_query = true;
$select = $matches[1];
}
// Temporary tables created in earlier queries are not explainable.
if ($is_select_query)
{
foreach (array('log_topics_unread', 'topics_posted_in', 'tmp_log_search_topics', 'tmp_log_search_messages') as $tmp)
if (strpos($select, $tmp) !== false)
{
$is_select_query = false;
break;
}
}
echo '
<div id="qq', $q, '" style="margin-bottom: 2ex;">
<a', $is_select_query ? ' href="' . $scripturl . '?action=viewquery;qq=' . ($q + 1) . '#qq' . $q . '"' : '', ' style="font-weight: bold; text-decoration: none;">
', nl2br(str_replace("\t", ' ', htmlspecialchars($query_data['q']))), '
</a><br />';
if (!empty($query_data['f']) && !empty($query_data['l']))
echo sprintf($txt['debug_query_in_line'], $query_data['f'], $query_data['l']);
if (isset($query_data['s'], $query_data['t']) && isset($txt['debug_query_which_took_at']))
echo sprintf($txt['debug_query_which_took_at'], round($query_data['t'], 8), round($query_data['s'], 8));
else
echo sprintf($txt['debug_query_which_took'], round($query_data['t'], 8));
echo '
</div>';
// Explain the query.
if ($query_id == $q && $is_select_query)
{
$result = $db->query('', '
EXPLAIN ' . $select,
array(
)
);
if ($result === false)
{
echo '
<table border="1" cellpadding="4" cellspacing="0" style="empty-cells: show; font-family: serif; margin-bottom: 2ex;">
<tr><td>', $db->last_error($db_connection), '</td></tr>
</table>';
continue;
}
echo '
<table border="1" rules="all" cellpadding="4" cellspacing="0" style="empty-cells: show; font-family: serif; margin-bottom: 2ex;">';
$row = $db->fetch_assoc($result);
echo '
<tr>
<th>' . implode('</th>
<th>', array_keys($row)) . '</th>
</tr>';
$db->data_seek($result, 0);
while ($row = $db->fetch_assoc($result))
{
echo '
<tr>
<td>' . implode('</td>
<td>', $row) . '</td>
</tr>';
}
$db->free_result($result);
echo '
</table>';
}
}
echo '
</div>
</body>
</html>';
obExit(false);
}
/**
* Get admin information from the database.
* Accessed by ?action=viewadminfile.
*/
public function action_viewadminfile()
{
global $context, $modSettings;
require_once(SUBSDIR . '/AdminDebug.subs.php');
// Don't allow non-administrators.
isAllowedTo('admin_forum');
setMemoryLimit('32M');
if (empty($_REQUEST['filename']) || !is_string($_REQUEST['filename']))
fatal_lang_error('no_access', false);
$file = adminInfoFile($_REQUEST['filename']);
// @todo Temp
// Figure out if sesc is still being used.
if (strpos($file['file_data'], ';sesc=') !== false)
$file['file_data'] = '
if (!(\'smfForum_sessionvar\' in window))
window.smfForum_sessionvar = \'sesc\';
' . strtr($file['file_data'], array(';sesc=' => ';\' + window.smfForum_sessionvar + \'='));
Template_Layers::getInstance()->removeAll();
// Lets make sure we aren't going to output anything nasty.
@ob_end_clean();
if (!empty($modSettings['enableCompressedOutput']))
@ob_start('ob_gzhandler');
else
@ob_start();
// Make sure they know what type of file we are.
header('Content-Type: ' . $file['filetype']);
echo $file['file_data'];
obExit(false);
}
}