PageRenderTime 37ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/forum/Sources/ManageErrors.php

https://github.com/leftnode/nooges.com
PHP | 381 lines | 249 code | 48 blank | 84 comment | 41 complexity | 73faa1f85a37533deacf3bd9382dce64 MD5 | raw file
  1. <?php
  2. /**********************************************************************************
  3. * ManageErrors.php *
  4. ***********************************************************************************
  5. * SMF: Simple Machines Forum *
  6. * Open-Source Project Inspired by Zef Hemel (zef@zefhemel.com) *
  7. * =============================================================================== *
  8. * Software Version: SMF 2.0 RC2 *
  9. * Software by: Simple Machines (http://www.simplemachines.org) *
  10. * Copyright 2006-2009 by: Simple Machines LLC (http://www.simplemachines.org) *
  11. * 2001-2006 by: Lewis Media (http://www.lewismedia.com) *
  12. * Support, News, Updates at: http://www.simplemachines.org *
  13. ***********************************************************************************
  14. * This program is free software; you may redistribute it and/or modify it under *
  15. * the terms of the provided license as published by Simple Machines LLC. *
  16. * *
  17. * This program is distributed in the hope that it is and will be useful, but *
  18. * WITHOUT ANY WARRANTIES; without even any implied warranty of MERCHANTABILITY *
  19. * or FITNESS FOR A PARTICULAR PURPOSE. *
  20. * *
  21. * See the "license.txt" file for details of the Simple Machines license. *
  22. * The latest version can always be found at http://www.simplemachines.org. *
  23. **********************************************************************************/
  24. if (!defined('SMF'))
  25. die('Hacking attempt...');
  26. /* Show a list of all errors that were logged on the forum.
  27. void ViewErrorLog()
  28. - sets all the context up to show the error log for maintenance.
  29. - uses the Errors template and error_log sub template.
  30. - requires the maintain_forum permission.
  31. - uses the 'view_errors' administration area.
  32. - accessed from ?action=admin;area=logs;sa=errorlog.
  33. void deleteErrors()
  34. - deletes all or some of the errors in the error log.
  35. - applies any necessary filters to deletion.
  36. - should only be called by ViewErrorLog().
  37. - attempts to TRUNCATE the table to reset the auto_increment.
  38. - redirects back to the error log when done.
  39. void ViewFile()
  40. - will do php highlighting on the file specified in $_REQUEST['file']
  41. - file must be readable
  42. - full file path must be base64 encoded
  43. - user must have admin_forum permission
  44. - the line number number is specified by $_REQUEST['line']
  45. - Will try to get the 20 lines before and after the specified line
  46. */
  47. // View the forum's error log.
  48. function ViewErrorLog()
  49. {
  50. global $scripturl, $txt, $context, $modSettings, $user_profile, $filter, $boarddir, $sourcedir, $themedir, $smcFunc;
  51. // Viewing contents of a file?
  52. if (isset($_GET['file']))
  53. return ViewFile();
  54. // Check for the administrative permission to do this.
  55. isAllowedTo('admin_forum');
  56. // Templates, etc...
  57. loadLanguage('ManageMaintenance');
  58. loadTemplate('Errors');
  59. // You can filter by any of the following columns:
  60. $filters = array(
  61. 'id_member' => $txt['username'],
  62. 'ip' => $txt['ip_address'],
  63. 'session' => $txt['session'],
  64. 'url' => $txt['error_url'],
  65. 'message' => $txt['error_message'],
  66. 'error_type' => $txt['error_type'],
  67. 'file' => $txt['file'],
  68. 'line' => $txt['line'],
  69. );
  70. // Set up the filtering...
  71. if (isset($_GET['value'], $_GET['filter']) && isset($filters[$_GET['filter']]))
  72. $filter = array(
  73. 'variable' => $_GET['filter'],
  74. 'value' => array(
  75. 'sql' => in_array($_GET['filter'], array('message', 'url', 'file')) ? base64_decode(strtr($_GET['value'], array(' ' => '+'))) : $smcFunc['db_escape_wildcard_string']($_GET['value']),
  76. ),
  77. 'href' => ';filter=' . $_GET['filter'] . ';value=' . $_GET['value'],
  78. 'entity' => $filters[$_GET['filter']]
  79. );
  80. // Deleting, are we?
  81. if (isset($_POST['delall']) || isset($_POST['delete']))
  82. deleteErrors();
  83. // Just how many errors are there?
  84. $result = $smcFunc['db_query']('', '
  85. SELECT COUNT(*)
  86. FROM {db_prefix}log_errors' . (isset($filter) ? '
  87. WHERE ' . $filter['variable'] . ' LIKE {string:filter}' : ''),
  88. array(
  89. 'filter' => isset($filter) ? $filter['value']['sql'] : '',
  90. )
  91. );
  92. list ($num_errors) = $smcFunc['db_fetch_row']($result);
  93. $smcFunc['db_free_result']($result);
  94. // If this filter is empty...
  95. if ($num_errors == 0 && isset($filter))
  96. redirectexit('action=admin;area=logs;sa=errorlog' . (isset($_REQUEST['desc']) ? ';desc' : ''));
  97. // Clean up start.
  98. if (!isset($_GET['start']) || $_GET['start'] < 0)
  99. $_GET['start'] = 0;
  100. // Do we want to reverse error listing?
  101. $context['sort_direction'] = isset($_REQUEST['desc']) ? 'down' : 'up';
  102. // Set the page listing up.
  103. $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['defaultMaxMessages']);
  104. $context['start'] = $_GET['start'];
  105. // Find and sort out the errors.
  106. $request = $smcFunc['db_query']('', '
  107. SELECT id_error, id_member, ip, url, log_time, message, session, error_type, file, line
  108. FROM {db_prefix}log_errors' . (isset($filter) ? '
  109. WHERE ' . $filter['variable'] . ' LIKE {string:filter}' : '') . '
  110. ORDER BY id_error ' . ($context['sort_direction'] == 'down' ? 'DESC' : '') . '
  111. LIMIT ' . $_GET['start'] . ', ' . $modSettings['defaultMaxMessages'],
  112. array(
  113. 'filter' => isset($filter) ? $filter['value']['sql'] : '',
  114. )
  115. );
  116. $context['errors'] = array();
  117. $members = array();
  118. while ($row = $smcFunc['db_fetch_assoc']($request))
  119. {
  120. $search_message = preg_replace('~&lt;span class=&quot;remove&quot;&gt;(.+?)&lt;/span&gt;~', '%', $smcFunc['db_escape_wildcard_string']($row['message']));
  121. if ($search_message == $filter['value']['sql'])
  122. $search_message = $smcFunc['db_escape_wildcard_string']($row['message']);
  123. $show_message = strtr(strtr(preg_replace('~&lt;span class=&quot;remove&quot;&gt;(.+?)&lt;/span&gt;~', '$1', $row['message']), array("\r" => '', '<br />' => "\n", '<' => '&lt;', '>' => '&gt;', '"' => '&quot;')), array("\n" => '<br />'));
  124. $context['errors'][$row['id_error']] = array(
  125. 'member' => array(
  126. 'id' => $row['id_member'],
  127. 'ip' => $row['ip'],
  128. 'session' => $row['session']
  129. ),
  130. 'time' => timeformat($row['log_time']),
  131. 'timestamp' => $row['log_time'],
  132. 'url' => array(
  133. 'html' => htmlspecialchars((substr($row['url'], 0, 1) == '?' ? $scripturl : '') . $row['url']),
  134. 'href' => base64_encode($smcFunc['db_escape_wildcard_string']($row['url']))
  135. ),
  136. 'message' => array(
  137. 'html' => $show_message,
  138. 'href' => base64_encode($search_message)
  139. ),
  140. 'id' => $row['id_error'],
  141. 'error_type' => array(
  142. 'type' => $row['error_type'],
  143. 'name' => isset($txt['errortype_'.$row['error_type']]) ? $txt['errortype_'.$row['error_type']] : $row['error_type'],
  144. ),
  145. 'file' => array(),
  146. );
  147. if (!empty($row['file']) && !empty($row['line']))
  148. {
  149. // Eval'd files rarely point to the right location and cause havoc for linking, so don't link them.
  150. $linkfile = strpos($row['file'], 'eval') === false || strpos($row['file'], '?') === false; // De Morgan's Law. Want this true unless both are present.
  151. $context['errors'][$row['id_error']]['file'] = array(
  152. 'file' => $row['file'],
  153. 'line' => $row['line'],
  154. 'href' => $scripturl . '?action=admin;area=logs;sa=errorlog;file=' . base64_encode($row['file']) . ';line=' . $row['line'],
  155. '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, 400, false);">' . $row['file'] . '</a>' : $row['file'],
  156. 'search' => base64_encode($row['file']),
  157. );
  158. }
  159. // Make a list of members to load later.
  160. $members[$row['id_member']] = $row['id_member'];
  161. }
  162. $smcFunc['db_free_result']($request);
  163. // Load the member data.
  164. if (!empty($members))
  165. {
  166. // Get some additional member info...
  167. $request = $smcFunc['db_query']('', '
  168. SELECT id_member, member_name, real_name
  169. FROM {db_prefix}members
  170. WHERE id_member IN ({array_int:member_list})
  171. LIMIT ' . count($members),
  172. array(
  173. 'member_list' => $members,
  174. )
  175. );
  176. while ($row = $smcFunc['db_fetch_assoc']($request))
  177. $members[$row['id_member']] = $row;
  178. $smcFunc['db_free_result']($request);
  179. // This is a guest...
  180. $members[0] = array(
  181. 'id_member' => 0,
  182. 'member_name' => '',
  183. 'real_name' => $txt['guest_title']
  184. );
  185. // Go through each error and tack the data on.
  186. foreach ($context['errors'] as $id => $dummy)
  187. {
  188. $memID = $context['errors'][$id]['member']['id'];
  189. $context['errors'][$id]['member']['username'] = $members[$memID]['member_name'];
  190. $context['errors'][$id]['member']['name'] = $members[$memID]['real_name'];
  191. $context['errors'][$id]['member']['href'] = empty($memID) ? '' : $scripturl . '?action=profile;u=' . $memID;
  192. $context['errors'][$id]['member']['link'] = empty($memID) ? $txt['guest_title'] : '<a href="' . $scripturl . '?action=profile;u=' . $memID . '">' . $context['errors'][$id]['member']['name'] . '</a>';
  193. }
  194. }
  195. // Filtering anything?
  196. if (isset($filter))
  197. {
  198. $context['filter'] = &$filter;
  199. // Set the filtering context.
  200. if ($filter['variable'] == 'id_member')
  201. {
  202. $id = $filter['value']['sql'];
  203. loadMemberData($id, false, 'minimal');
  204. $context['filter']['value']['html'] = '<a href="' . $scripturl . '?action=profile;u=' . $id . '">' . $user_profile[$id]['real_name'] . '</a>';
  205. }
  206. elseif ($filter['variable'] == 'url')
  207. $context['filter']['value']['html'] = '\'' . strtr(htmlspecialchars((substr($filter['value']['sql'], 0, 1) == '?' ? $scripturl : '') . $filter['value']['sql']), array('\_' => '_')) . '\'';
  208. elseif ($filter['variable'] == 'message')
  209. {
  210. $context['filter']['value']['html'] = '\'' . strtr(htmlspecialchars($filter['value']['sql']), array("\n" => '<br />', '&lt;br /&gt;' => '<br />', "\t" => '&nbsp;&nbsp;&nbsp;', '\_' => '_', '\\%' => '%', '\\\\' => '\\')) . '\'';
  211. $context['filter']['value']['html'] = preg_replace('~&amp;lt;span class=&amp;quot;remove&amp;quot;&amp;gt;(.+?)&amp;lt;/span&amp;gt;~', '$1', $context['filter']['value']['html']);
  212. }
  213. elseif ($filter['variable'] == 'error_type')
  214. {
  215. $context['filter']['value']['html'] = '\'' . strtr(htmlspecialchars($filter['value']['sql']), array("\n" => '<br />', '&lt;br /&gt;' => '<br />', "\t" => '&nbsp;&nbsp;&nbsp;', '\_' => '_', '\\%' => '%', '\\\\' => '\\')) . '\'';
  216. }
  217. else
  218. $context['filter']['value']['html'] = &$filter['value']['sql'];
  219. }
  220. $context['error_types'] = array();
  221. $context['error_types']['all'] = array(
  222. 'label' => $txt['errortype_all'],
  223. 'description' => isset($txt['errortype_all_desc']) ? $txt['errortype_all_desc'] : '',
  224. 'url' => $scripturl . '?action=admin;area=logs;sa=errorlog' . ($context['sort_direction'] == 'down' ? ';desc' : ''),
  225. 'is_selected' => empty($filter),
  226. );
  227. $sum = 0;
  228. // What type of errors do we have and how many do we have?
  229. $request = $smcFunc['db_query']('', '
  230. SELECT error_type, COUNT(*) AS num_errors
  231. FROM {db_prefix}log_errors
  232. GROUP BY error_type
  233. ORDER BY error_type = {string:critical_type} DESC, error_type ASC',
  234. array(
  235. 'critical_type' => 'critical',
  236. )
  237. );
  238. while ($row = $smcFunc['db_fetch_assoc']($request))
  239. {
  240. // Total errors so far?
  241. $sum += $row['num_errors'];
  242. $context['error_types'][$sum] = array(
  243. 'label' => (isset($txt['errortype_' . $row['error_type']]) ? $txt['errortype_' . $row['error_type']] : $row['error_type']) . ' (' . $row['num_errors'] . ')',
  244. 'description' => isset($txt['errortype_' . $row['error_type'] . '_desc']) ? $txt['errortype_' . $row['error_type'] . '_desc'] : '',
  245. 'url' => $scripturl . '?action=admin;area=logs;sa=errorlog' . ($context['sort_direction'] == 'down' ? ';desc' : '') . ';filter=error_type;value='. $row['error_type'],
  246. 'is_selected' => isset($filter) && $filter['value']['sql'] == $smcFunc['db_escape_wildcard_string']($row['error_type']),
  247. );
  248. }
  249. $smcFunc['db_free_result']($request);
  250. // Update the all errors tab with the total number of errors
  251. $context['error_types']['all']['label'] .= ' (' . $sum . ')';
  252. // Finally, work out what is the last tab!
  253. if (isset($context['error_types'][$sum]))
  254. $context['error_types'][$sum]['is_last'] = true;
  255. else
  256. $context['error_types']['all']['is_last'] = true;
  257. // And this is pretty basic ;).
  258. $context['page_title'] = $txt['errlog'];
  259. $context['has_filter'] = isset($filter);
  260. $context['sub_template'] = 'error_log';
  261. }
  262. // Delete errors from the database.
  263. function deleteErrors()
  264. {
  265. global $filter, $smcFunc;
  266. // Make sure the session exists and is correct; otherwise, might be a hacker.
  267. checkSession();
  268. // Delete all or just some?
  269. if (isset($_POST['delall']) && !isset($filter))
  270. $smcFunc['db_query']('truncate_table', '
  271. TRUNCATE {db_prefix}log_errors',
  272. array(
  273. )
  274. );
  275. // Deleting all with a filter?
  276. elseif (isset($_POST['delall']) && isset($filter))
  277. $smcFunc['db_query']('', '
  278. DELETE FROM {db_prefix}log_errors
  279. WHERE ' . $filter['variable'] . ' LIKE {string:filter}',
  280. array(
  281. 'filter' => $filter['value']['sql'],
  282. )
  283. );
  284. // Just specific errors?
  285. elseif (!empty($_POST['delete']))
  286. {
  287. $smcFunc['db_query']('', '
  288. DELETE FROM {db_prefix}log_errors
  289. WHERE id_error IN ({array_int:error_list})',
  290. array(
  291. 'error_list' => array_unique($_POST['delete']),
  292. )
  293. );
  294. // Go back to where we were.
  295. redirectexit('action=admin;area=logs;sa=errorlog' . (isset($_REQUEST['desc']) ? ';desc' : '') . ';start=' . $_GET['start'] . (isset($filter) ? ';filter=' . $_GET['filter'] . ';value=' . $_GET['value'] : ''));
  296. }
  297. // Back to the error log!
  298. redirectexit('action=admin;area=logs;sa=errorlog' . (isset($_REQUEST['desc']) ? ';desc' : ''));
  299. }
  300. function ViewFile()
  301. {
  302. global $context, $txt;
  303. // Check for the administrative permission to do this.
  304. isAllowedTo('admin_forum');
  305. // decode the file and get the line
  306. $file = base64_decode($_REQUEST['file']);
  307. $line = (int)$_REQUEST['line'];
  308. // Make sure the file we are looking for is one they are allowed to look at
  309. if (!is_readable($file))
  310. fatal_lang_error('error_bad_file', true, array($file));
  311. // get the min and max lines
  312. $min = $line - 20 <= 0 ? 1 : $line - 20;
  313. $max = $line + 21; // One additional line to make everything work out correctly
  314. if ($max <= 0 || $min >= $max)
  315. fatal_lang_error('error_bad_line');
  316. $file_data = explode('<br />', highlight_php_code(htmlspecialchars(implode('', file($file)))));
  317. // We don't want to slice off too many so lets make sure we stop at the last one
  318. $max = min($max, max(array_keys($file_data)));
  319. $file_data = array_slice($file_data, $min-1, $max - $min);
  320. $context['file_data'] = array(
  321. 'contents' => $file_data,
  322. 'min' => $min,
  323. 'target' => $line,
  324. 'file' => strtr($file, array('"' => '\\"')),
  325. );
  326. loadTemplate('Errors');
  327. $context['template_layers'] = array();
  328. $context['sub_template'] = 'show_file';
  329. }
  330. ?>