PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/php/Sources/ManageErrors.php

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