1 | <?php |
||
2 | |||
3 | /** |
||
4 | * The main purpose of this file is to show a list of all errors that were |
||
5 | * logged on the forum, and allow filtering and deleting them. |
||
6 | * |
||
7 | * Simple Machines Forum (SMF) |
||
8 | * |
||
9 | * @package SMF |
||
10 | * @author Simple Machines http://www.simplemachines.org |
||
11 | * @copyright 2019 Simple Machines and individual contributors |
||
12 | * @license http://www.simplemachines.org/about/smf/license.php BSD |
||
13 | * |
||
14 | * @version 2.1 RC2 |
||
15 | */ |
||
16 | |||
17 | if (!defined('SMF')) |
||
18 | die('No direct access...'); |
||
19 | |||
20 | /** |
||
21 | * View the forum's error log. |
||
22 | * This function sets all the context up to show the error log for maintenance. |
||
23 | * It requires the maintain_forum permission. |
||
24 | * It is accessed from ?action=admin;area=logs;sa=errorlog. |
||
25 | * |
||
26 | * @uses the Errors template and error_log sub template. |
||
27 | */ |
||
28 | function ViewErrorLog() |
||
29 | { |
||
30 | global $scripturl, $txt, $context, $modSettings, $user_profile, $filter, $smcFunc; |
||
31 | |||
32 | // Viewing contents of a file? |
||
33 | if (isset($_GET['file'])) |
||
34 | return ViewFile(); |
||
35 | |||
36 | // Viewing contents of a backtrace? |
||
37 | if (isset($_GET['backtrace'])) |
||
38 | return ViewBacktrace(); |
||
39 | |||
40 | // Check for the administrative permission to do this. |
||
41 | isAllowedTo('admin_forum'); |
||
42 | |||
43 | // Templates, etc... |
||
44 | loadLanguage('ManageMaintenance'); |
||
45 | loadTemplate('Errors'); |
||
46 | |||
47 | // You can filter by any of the following columns: |
||
48 | $filters = array( |
||
49 | 'id_member' => array( |
||
50 | 'txt' => $txt['username'], |
||
51 | 'operator' => '=', |
||
52 | 'datatype' => 'int', |
||
53 | ), |
||
54 | 'ip' => array( |
||
55 | 'txt' => $txt['ip_address'], |
||
56 | 'operator' => '=', |
||
57 | 'datatype' => 'inet', |
||
58 | ), |
||
59 | 'session' => array( |
||
60 | 'txt' => $txt['session'], |
||
61 | 'operator' => 'LIKE', |
||
62 | 'datatype' => 'string', |
||
63 | ), |
||
64 | 'url' => array( |
||
65 | 'txt' => $txt['error_url'], |
||
66 | 'operator' => 'LIKE', |
||
67 | 'datatype' => 'string', |
||
68 | ), |
||
69 | 'message' => array( |
||
70 | 'txt' => $txt['error_message'], |
||
71 | 'operator' => 'LIKE', |
||
72 | 'datatype' => 'string', |
||
73 | ), |
||
74 | 'error_type' => array( |
||
75 | 'txt' => $txt['error_type'], |
||
76 | 'operator' => 'LIKE', |
||
77 | 'datatype' => 'string', |
||
78 | ), |
||
79 | 'file' => array( |
||
80 | 'txt' => $txt['file'], |
||
81 | 'operator' => 'LIKE', |
||
82 | 'datatype' => 'string', |
||
83 | ), |
||
84 | 'line' => array( |
||
85 | 'txt' => $txt['line'], |
||
86 | 'operator' => '=', |
||
87 | 'datatype' => 'int', |
||
88 | ), |
||
89 | ); |
||
90 | |||
91 | // Set up the filtering... |
||
92 | if (isset($_GET['value'], $_GET['filter']) && isset($filters[$_GET['filter']])) |
||
93 | $filter = array( |
||
94 | 'variable' => $_GET['filter'], |
||
95 | 'value' => array( |
||
96 | 'sql' => in_array($_GET['filter'], array('message', 'url', 'file')) ? base64_decode(strtr($_GET['value'], array(' ' => '+'))) : $smcFunc['db_escape_wildcard_string']($_GET['value']), |
||
97 | ), |
||
98 | 'href' => ';filter=' . $_GET['filter'] . ';value=' . $_GET['value'], |
||
99 | 'entity' => $filters[$_GET['filter']]['txt'] |
||
100 | ); |
||
101 | |||
102 | // Deleting, are we? |
||
103 | if (isset($_POST['delall']) || isset($_POST['delete'])) |
||
104 | deleteErrors(); |
||
105 | |||
106 | // Just how many errors are there? |
||
107 | $result = $smcFunc['db_query']('', ' |
||
108 | SELECT COUNT(*) |
||
109 | FROM {db_prefix}log_errors' . (isset($filter) ? ' |
||
110 | WHERE ' . $filter['variable'] . ' ' . $filters[$_GET['filter']]['operator'] . ' {' . $filters[$_GET['filter']]['datatype'] . ':filter}' : ''), |
||
111 | array( |
||
112 | 'filter' => isset($filter) ? $filter['value']['sql'] : '', |
||
113 | ) |
||
114 | ); |
||
115 | list ($num_errors) = $smcFunc['db_fetch_row']($result); |
||
116 | $smcFunc['db_free_result']($result); |
||
117 | |||
118 | // If this filter is empty... |
||
119 | if ($num_errors == 0 && isset($filter)) |
||
120 | redirectexit('action=admin;area=logs;sa=errorlog' . (isset($_REQUEST['desc']) ? ';desc' : '')); |
||
121 | |||
122 | // Clean up start. |
||
123 | if (!isset($_GET['start']) || $_GET['start'] < 0) |
||
124 | $_GET['start'] = 0; |
||
125 | |||
126 | // Do we want to reverse error listing? |
||
127 | $context['sort_direction'] = isset($_REQUEST['desc']) ? 'down' : 'up'; |
||
128 | |||
129 | // Set the page listing up. |
||
130 | $context['page_index'] = constructPageIndex($scripturl . '?action=admin;area=logs;sa=errorlog' . ($context['sort_direction'] == 'down' ? ';desc' : '') . (isset($filter) ? $filter['href'] : ''), $_GET['start'], $num_errors, $modSettings['defaultMaxListItems']); |
||
131 | $context['start'] = $_GET['start']; |
||
132 | |||
133 | // Update the error count |
||
134 | if (!isset($filter)) |
||
135 | $context['num_errors'] = $num_errors; |
||
136 | else |
||
137 | { |
||
138 | // We want all errors, not just the number of filtered messages... |
||
139 | $query = $smcFunc['db_query']('', ' |
||
140 | SELECT COUNT(id_error) |
||
141 | FROM {db_prefix}log_errors', |
||
142 | array() |
||
143 | ); |
||
144 | |||
145 | list($context['num_errors']) = $smcFunc['db_fetch_row']($query); |
||
146 | $smcFunc['db_free_result']($query); |
||
147 | } |
||
148 | |||
149 | // Find and sort out the errors. |
||
150 | $request = $smcFunc['db_query']('', ' |
||
151 | SELECT id_error, id_member, ip, url, log_time, message, session, error_type, file, line |
||
152 | FROM {db_prefix}log_errors' . (isset($filter) ? ' |
||
153 | WHERE ' . $filter['variable'] . ' ' . $filters[$_GET['filter']]['operator'] . ' {' . $filters[$_GET['filter']]['datatype'] . ':filter}' : '') . ' |
||
154 | ORDER BY id_error ' . ($context['sort_direction'] == 'down' ? 'DESC' : '') . ' |
||
155 | LIMIT {int:start}, {int:max}', |
||
156 | array( |
||
157 | 'filter' => isset($filter) ? $filter['value']['sql'] : '', |
||
158 | 'start' => $_GET['start'], |
||
159 | 'max' => $modSettings['defaultMaxListItems'], |
||
160 | ) |
||
161 | ); |
||
162 | $context['errors'] = array(); |
||
163 | $members = array(); |
||
164 | |||
165 | for ($i = 0; $row = $smcFunc['db_fetch_assoc']($request); $i++) |
||
166 | { |
||
167 | $search_message = preg_replace('~<span class="remove">(.+?)</span>~', '%', $smcFunc['db_escape_wildcard_string']($row['message'])); |
||
168 | if ($search_message == $filter['value']['sql']) |
||
169 | $search_message = $smcFunc['db_escape_wildcard_string']($row['message']); |
||
170 | $show_message = strtr(strtr(preg_replace('~<span class="remove">(.+?)</span>~', '$1', $row['message']), array("\r" => '', '<br>' => "\n", '<' => '<', '>' => '>', '"' => '"')), array("\n" => '<br>')); |
||
171 | |||
172 | $context['errors'][$row['id_error']] = array( |
||
173 | 'member' => array( |
||
174 | 'id' => $row['id_member'], |
||
175 | 'ip' => inet_dtop($row['ip']), |
||
176 | 'session' => $row['session'] |
||
177 | ), |
||
178 | 'time' => timeformat($row['log_time']), |
||
179 | 'timestamp' => $row['log_time'], |
||
180 | 'url' => array( |
||
181 | 'html' => $smcFunc['htmlspecialchars'](strpos($row['url'], 'cron.php') === false ? (substr($row['url'], 0, 1) == '?' ? $scripturl : '') . $row['url'] : $row['url']), |
||
182 | 'href' => base64_encode($smcFunc['db_escape_wildcard_string']($row['url'])) |
||
183 | ), |
||
184 | 'message' => array( |
||
185 | 'html' => $show_message, |
||
186 | 'href' => base64_encode($search_message) |
||
187 | ), |
||
188 | 'id' => $row['id_error'], |
||
189 | 'error_type' => array( |
||
190 | 'type' => $row['error_type'], |
||
191 | 'name' => isset($txt['errortype_' . $row['error_type']]) ? $txt['errortype_' . $row['error_type']] : $row['error_type'], |
||
192 | ), |
||
193 | 'file' => array(), |
||
194 | ); |
||
195 | if (!empty($row['file']) && !empty($row['line'])) |
||
196 | { |
||
197 | // Eval'd files rarely point to the right location and cause havoc for linking, so don't link them. |
||
198 | $linkfile = strpos($row['file'], 'eval') === false || strpos($row['file'], '?') === false; // De Morgan's Law. Want this true unless both are present. |
||
199 | |||
200 | $context['errors'][$row['id_error']]['file'] = array( |
||
201 | 'file' => $row['file'], |
||
202 | 'line' => $row['line'], |
||
203 | 'href' => $scripturl . '?action=admin;area=logs;sa=errorlog;file=' . base64_encode($row['file']) . ';line=' . $row['line'], |
||
204 | 'link' => $linkfile ? '<a href="' . $scripturl . '?action=admin;area=logs;sa=errorlog;file=' . base64_encode($row['file']) . ';line=' . $row['line'] . '" onclick="return reqWin(this.href, 600, 480, false);">' . $row['file'] . '</a>' : $row['file'], |
||
205 | 'search' => base64_encode($row['file']), |
||
206 | ); |
||
207 | } |
||
208 | |||
209 | // Make a list of members to load later. |
||
210 | $members[$row['id_member']] = $row['id_member']; |
||
211 | } |
||
212 | $smcFunc['db_free_result']($request); |
||
213 | |||
214 | // Load the member data. |
||
215 | if (!empty($members)) |
||
216 | { |
||
217 | // Get some additional member info... |
||
218 | $request = $smcFunc['db_query']('', ' |
||
219 | SELECT id_member, member_name, real_name |
||
220 | FROM {db_prefix}members |
||
221 | WHERE id_member IN ({array_int:member_list}) |
||
222 | LIMIT {int:members}', |
||
223 | array( |
||
224 | 'member_list' => $members, |
||
225 | 'members' => count($members), |
||
226 | ) |
||
227 | ); |
||
228 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
229 | $members[$row['id_member']] = $row; |
||
230 | $smcFunc['db_free_result']($request); |
||
231 | |||
232 | // This is a guest... |
||
233 | $members[0] = array( |
||
234 | 'id_member' => 0, |
||
235 | 'member_name' => '', |
||
236 | 'real_name' => $txt['guest_title'] |
||
237 | ); |
||
238 | |||
239 | // Go through each error and tack the data on. |
||
240 | foreach ($context['errors'] as $id => $dummy) |
||
241 | { |
||
242 | $memID = $context['errors'][$id]['member']['id']; |
||
243 | $context['errors'][$id]['member']['username'] = $members[$memID]['member_name']; |
||
244 | $context['errors'][$id]['member']['name'] = $members[$memID]['real_name']; |
||
245 | $context['errors'][$id]['member']['href'] = empty($memID) ? '' : $scripturl . '?action=profile;u=' . $memID; |
||
246 | $context['errors'][$id]['member']['link'] = empty($memID) ? $txt['guest_title'] : '<a href="' . $scripturl . '?action=profile;u=' . $memID . '">' . $context['errors'][$id]['member']['name'] . '</a>'; |
||
247 | } |
||
248 | } |
||
249 | |||
250 | // Filtering anything? |
||
251 | if (isset($filter)) |
||
252 | { |
||
253 | $context['filter'] = &$filter; |
||
254 | |||
255 | // Set the filtering context. |
||
256 | if ($filter['variable'] == 'id_member') |
||
257 | { |
||
258 | $id = $filter['value']['sql']; |
||
259 | loadMemberData($id, false, 'minimal'); |
||
260 | $context['filter']['value']['html'] = '<a href="' . $scripturl . '?action=profile;u=' . $id . '">' . $user_profile[$id]['real_name'] . '</a>'; |
||
261 | } |
||
262 | elseif ($filter['variable'] == 'url') |
||
263 | $context['filter']['value']['html'] = '\'' . strtr($smcFunc['htmlspecialchars']((substr($filter['value']['sql'], 0, 1) == '?' ? $scripturl : '') . $filter['value']['sql']), array('\_' => '_')) . '\''; |
||
264 | elseif ($filter['variable'] == 'message') |
||
265 | { |
||
266 | $context['filter']['value']['html'] = '\'' . strtr($smcFunc['htmlspecialchars']($filter['value']['sql']), array("\n" => '<br>', '<br />' => '<br>', "\t" => ' ', '\_' => '_', '\\%' => '%', '\\\\' => '\\')) . '\''; |
||
267 | $context['filter']['value']['html'] = preg_replace('~&lt;span class=&quot;remove&quot;&gt;(.+?)&lt;/span&gt;~', '$1', $context['filter']['value']['html']); |
||
268 | } |
||
269 | elseif ($filter['variable'] == 'error_type') |
||
270 | { |
||
271 | $context['filter']['value']['html'] = '\'' . strtr($smcFunc['htmlspecialchars']($filter['value']['sql']), array("\n" => '<br>', '<br />' => '<br>', "\t" => ' ', '\_' => '_', '\\%' => '%', '\\\\' => '\\')) . '\''; |
||
272 | } |
||
273 | else |
||
274 | $context['filter']['value']['html'] = &$filter['value']['sql']; |
||
275 | } |
||
276 | |||
277 | $context['error_types'] = array(); |
||
278 | |||
279 | $context['error_types']['all'] = array( |
||
280 | 'label' => $txt['errortype_all'], |
||
281 | 'description' => isset($txt['errortype_all_desc']) ? $txt['errortype_all_desc'] : '', |
||
282 | 'url' => $scripturl . '?action=admin;area=logs;sa=errorlog' . ($context['sort_direction'] == 'down' ? ';desc' : ''), |
||
283 | 'is_selected' => empty($filter), |
||
284 | ); |
||
285 | |||
286 | $sum = 0; |
||
287 | // What type of errors do we have and how many do we have? |
||
288 | $request = $smcFunc['db_query']('', ' |
||
289 | SELECT error_type, COUNT(*) AS num_errors |
||
290 | FROM {db_prefix}log_errors |
||
291 | GROUP BY error_type |
||
292 | ORDER BY error_type = {string:critical_type} DESC, error_type ASC', |
||
293 | array( |
||
294 | 'critical_type' => 'critical', |
||
295 | ) |
||
296 | ); |
||
297 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
298 | { |
||
299 | // Total errors so far? |
||
300 | $sum += $row['num_errors']; |
||
301 | |||
302 | $context['error_types'][$sum] = array( |
||
303 | 'label' => (isset($txt['errortype_' . $row['error_type']]) ? $txt['errortype_' . $row['error_type']] : $row['error_type']) . ' (' . $row['num_errors'] . ')', |
||
304 | 'description' => isset($txt['errortype_' . $row['error_type'] . '_desc']) ? $txt['errortype_' . $row['error_type'] . '_desc'] : '', |
||
305 | 'url' => $scripturl . '?action=admin;area=logs;sa=errorlog' . ($context['sort_direction'] == 'down' ? ';desc' : '') . ';filter=error_type;value=' . $row['error_type'], |
||
306 | 'is_selected' => isset($filter) && $filter['value']['sql'] == $smcFunc['db_escape_wildcard_string']($row['error_type']), |
||
307 | ); |
||
308 | } |
||
309 | $smcFunc['db_free_result']($request); |
||
310 | |||
311 | // Update the all errors tab with the total number of errors |
||
312 | $context['error_types']['all']['label'] .= ' (' . $sum . ')'; |
||
313 | |||
314 | // Finally, work out what is the last tab! |
||
315 | if (isset($context['error_types'][$sum])) |
||
316 | $context['error_types'][$sum]['is_last'] = true; |
||
317 | else |
||
318 | $context['error_types']['all']['is_last'] = true; |
||
319 | |||
320 | // And this is pretty basic ;). |
||
321 | $context['page_title'] = $txt['errorlog']; |
||
322 | $context['has_filter'] = isset($filter); |
||
323 | $context['sub_template'] = 'error_log'; |
||
324 | |||
325 | createToken('admin-el'); |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Delete all or some of the errors in the error log. |
||
330 | * It applies any necessary filters to deletion. |
||
331 | * This should only be called by ViewErrorLog(). |
||
332 | * It attempts to TRUNCATE the table to reset the auto_increment. |
||
333 | * Redirects back to the error log when done. |
||
334 | */ |
||
335 | function deleteErrors() |
||
336 | { |
||
337 | global $filter, $smcFunc; |
||
338 | |||
339 | // Make sure the session exists and is correct; otherwise, might be a hacker. |
||
340 | checkSession(); |
||
341 | validateToken('admin-el'); |
||
342 | |||
343 | // Delete all or just some? |
||
344 | if (isset($_POST['delall']) && !isset($filter)) |
||
345 | $smcFunc['db_query']('truncate_table', ' |
||
346 | TRUNCATE {db_prefix}log_errors', |
||
347 | array( |
||
348 | ) |
||
349 | ); |
||
350 | // Deleting all with a filter? |
||
351 | elseif (isset($_POST['delall']) && isset($filter)) |
||
352 | $smcFunc['db_query']('', ' |
||
353 | DELETE FROM {db_prefix}log_errors |
||
354 | WHERE ' . $filter['variable'] . ' LIKE {string:filter}', |
||
355 | array( |
||
356 | 'filter' => $filter['value']['sql'], |
||
357 | ) |
||
358 | ); |
||
359 | // Just specific errors? |
||
360 | elseif (!empty($_POST['delete'])) |
||
361 | { |
||
362 | $smcFunc['db_query']('', ' |
||
363 | DELETE FROM {db_prefix}log_errors |
||
364 | WHERE id_error IN ({array_int:error_list})', |
||
365 | array( |
||
366 | 'error_list' => array_unique($_POST['delete']), |
||
367 | ) |
||
368 | ); |
||
369 | |||
370 | // Go back to where we were. |
||
371 | redirectexit('action=admin;area=logs;sa=errorlog' . (isset($_REQUEST['desc']) ? ';desc' : '') . ';start=' . $_GET['start'] . (isset($filter) ? ';filter=' . $_GET['filter'] . ';value=' . $_GET['value'] : '')); |
||
372 | } |
||
373 | |||
374 | // Back to the error log! |
||
375 | redirectexit('action=admin;area=logs;sa=errorlog' . (isset($_REQUEST['desc']) ? ';desc' : '')); |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * View a file specified in $_REQUEST['file'], with php highlighting on it |
||
380 | * Preconditions: |
||
381 | * - file must be readable, |
||
382 | * - full file path must be base64 encoded, |
||
383 | * - user must have admin_forum permission. |
||
384 | * The line number number is specified by $_REQUEST['line']... |
||
385 | * The function will try to get the 20 lines before and after the specified line. |
||
386 | */ |
||
387 | function ViewFile() |
||
388 | { |
||
389 | global $context, $boarddir, $sourcedir, $cachedir, $smcFunc; |
||
390 | |||
391 | // Check for the administrative permission to do this. |
||
392 | isAllowedTo('admin_forum'); |
||
393 | |||
394 | // Decode the file and get the line |
||
395 | $file = realpath(base64_decode($_REQUEST['file'])); |
||
396 | $real_board = realpath($boarddir); |
||
397 | $real_source = realpath($sourcedir); |
||
398 | $real_cache = realpath($cachedir); |
||
399 | $basename = strtolower(basename($file)); |
||
400 | $ext = strrchr($basename, '.'); |
||
401 | $line = isset($_REQUEST['line']) ? (int) $_REQUEST['line'] : 0; |
||
402 | |||
403 | // Make sure the file we are looking for is one they are allowed to look at |
||
404 | if ($ext != '.php' || (strpos($file, $real_board) === false && strpos($file, $real_source) === false) || ($basename == 'settings.php' || $basename == 'settings_bak.php') || strpos($file, $real_cache) !== false || !is_readable($file)) |
||
405 | fatal_lang_error('error_bad_file', true, array($smcFunc['htmlspecialchars']($file))); |
||
406 | |||
407 | // get the min and max lines |
||
408 | $min = $line - 20 <= 0 ? 1 : $line - 20; |
||
409 | $max = $line + 21; // One additional line to make everything work out correctly |
||
410 | |||
411 | if ($max <= 0 || $min >= $max) |
||
412 | fatal_lang_error('error_bad_line'); |
||
413 | |||
414 | $file_data = explode('<br />', highlight_php_code($smcFunc['htmlspecialchars'](implode('', file($file))))); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
415 | |||
416 | // We don't want to slice off too many so lets make sure we stop at the last one |
||
417 | $max = min($max, max(array_keys($file_data))); |
||
418 | |||
419 | $file_data = array_slice($file_data, $min - 1, $max - $min); |
||
420 | |||
421 | $context['file_data'] = array( |
||
422 | 'contents' => $file_data, |
||
423 | 'min' => $min, |
||
424 | 'target' => $line, |
||
425 | 'file' => strtr($file, array('"' => '\\"')), |
||
426 | ); |
||
427 | |||
428 | loadTemplate('Errors'); |
||
429 | $context['template_layers'] = array(); |
||
430 | $context['sub_template'] = 'show_file'; |
||
431 | |||
432 | } |
||
433 | |||
434 | /** |
||
435 | * View a backtrace specified in $_REQUEST['backtrace'], with php highlighting on it |
||
436 | * Preconditions: |
||
437 | * - user must have admin_forum permission. |
||
438 | */ |
||
439 | function ViewBacktrace() |
||
440 | { |
||
441 | global $context, $smcFunc, $scripturl; |
||
442 | |||
443 | // Check for the administrative permission to do this. |
||
444 | isAllowedTo('admin_forum'); |
||
445 | |||
446 | $id_error = (int) $_REQUEST['backtrace']; |
||
447 | $request = $smcFunc['db_query']('', ' |
||
448 | SELECT backtrace, error_type, message, file, line, url |
||
449 | FROM {db_prefix}log_errors |
||
450 | WHERE id_error = {int:id_error}', |
||
451 | array( |
||
452 | 'id_error' => $id_error, |
||
453 | ) |
||
454 | ); |
||
455 | |||
456 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
457 | { |
||
458 | $context['error_info'] = $row; |
||
459 | $context['error_info']['url'] = $scripturl . $row['url']; |
||
460 | $context['error_info']['backtrace'] = $smcFunc['json_decode']($row['backtrace']); |
||
461 | } |
||
462 | $smcFunc['db_free_result']($request); |
||
463 | |||
464 | loadCSSFile('admin.css', array(), 'smf_admin'); |
||
465 | loadTemplate('Errors'); |
||
466 | loadLanguage('ManageMaintenance'); |
||
467 | $context['template_layers'] = array(); |
||
468 | $context['sub_template'] = 'show_backtrace'; |
||
469 | |||
470 | } |
||
471 | |||
472 | ?> |