dblog.admin.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <?php
  2. /**
  3. * @file
  4. * Administrative page callbacks for the Database Logging module.
  5. */
  6. /**
  7. * Page callback: Displays a listing of database log messages.
  8. *
  9. * Messages are truncated at 56 chars. Full-length messages can be viewed on the
  10. * message details page.
  11. *
  12. * @see dblog_clear_log_form()
  13. * @see dblog_event()
  14. * @see dblog_filter_form()
  15. * @see dblog_menu()
  16. *
  17. * @ingroup logging_severity_levels
  18. */
  19. function dblog_overview() {
  20. $filter = dblog_build_filter_query();
  21. $rows = array();
  22. $classes = array(
  23. WATCHDOG_DEBUG => 'dblog-debug',
  24. WATCHDOG_INFO => 'dblog-info',
  25. WATCHDOG_NOTICE => 'dblog-notice',
  26. WATCHDOG_WARNING => 'dblog-warning',
  27. WATCHDOG_ERROR => 'dblog-error',
  28. WATCHDOG_CRITICAL => 'dblog-critical',
  29. WATCHDOG_ALERT => 'dblog-alert',
  30. WATCHDOG_EMERGENCY => 'dblog-emerg',
  31. );
  32. $build['dblog_filter_form'] = drupal_get_form('dblog_filter_form');
  33. $build['dblog_clear_log_form'] = drupal_get_form('dblog_clear_log_form');
  34. $header = array(
  35. '', // Icon column.
  36. array('data' => t('Type'), 'field' => 'w.type'),
  37. array('data' => t('Date'), 'field' => 'w.wid', 'sort' => 'desc'),
  38. t('Message'),
  39. array('data' => t('User'), 'field' => 'u.name'),
  40. array('data' => t('Operations')),
  41. );
  42. $query = db_select('watchdog', 'w')->extend('PagerDefault')->extend('TableSort');
  43. $query->leftJoin('users', 'u', 'w.uid = u.uid');
  44. $query
  45. ->fields('w', array('wid', 'uid', 'severity', 'type', 'timestamp', 'message', 'variables', 'link'))
  46. ->addField('u', 'name');
  47. if (!empty($filter['where'])) {
  48. $query->where($filter['where'], $filter['args']);
  49. }
  50. $result = $query
  51. ->limit(50)
  52. ->orderByHeader($header)
  53. ->execute();
  54. foreach ($result as $dblog) {
  55. $rows[] = array('data' =>
  56. array(
  57. // Cells
  58. array('class' => 'icon'),
  59. t($dblog->type),
  60. format_date($dblog->timestamp, 'short'),
  61. theme('dblog_message', array('event' => $dblog, 'link' => TRUE)),
  62. theme('username', array('account' => $dblog)),
  63. filter_xss($dblog->link),
  64. ),
  65. // Attributes for tr
  66. 'class' => array(drupal_html_class('dblog-' . $dblog->type), $classes[$dblog->severity]),
  67. );
  68. }
  69. $build['dblog_table'] = array(
  70. '#theme' => 'table',
  71. '#header' => $header,
  72. '#rows' => $rows,
  73. '#attributes' => array('id' => 'admin-dblog'),
  74. '#empty' => t('No log messages available.'),
  75. );
  76. $build['dblog_pager'] = array('#theme' => 'pager');
  77. return $build;
  78. }
  79. /**
  80. * Page callback: Shows the most frequent log messages of a given event type.
  81. *
  82. * Messages are not truncated on this page because events detailed herein do not
  83. * have links to a detailed view.
  84. *
  85. * @param string $type
  86. * Type of database log events to display (e.g., 'search').
  87. *
  88. * @return array
  89. * A build array in the format expected by drupal_render().
  90. *
  91. * @see dblog_menu()
  92. */
  93. function dblog_top($type) {
  94. $header = array(
  95. array('data' => t('Count'), 'field' => 'count', 'sort' => 'desc'),
  96. array('data' => t('Message'), 'field' => 'message')
  97. );
  98. $count_query = db_select('watchdog');
  99. $count_query->addExpression('COUNT(DISTINCT(message))');
  100. $count_query->condition('type', $type);
  101. $query = db_select('watchdog', 'w')->extend('PagerDefault')->extend('TableSort');
  102. $query->addExpression('COUNT(wid)', 'count');
  103. $query = $query
  104. ->fields('w', array('message', 'variables'))
  105. ->condition('w.type', $type)
  106. ->groupBy('message')
  107. ->groupBy('variables')
  108. ->limit(30)
  109. ->orderByHeader($header);
  110. $query->setCountQuery($count_query);
  111. $result = $query->execute();
  112. $rows = array();
  113. foreach ($result as $dblog) {
  114. $rows[] = array($dblog->count, theme('dblog_message', array('event' => $dblog)));
  115. }
  116. $build['dblog_top_table'] = array(
  117. '#theme' => 'table',
  118. '#header' => $header,
  119. '#rows' => $rows,
  120. '#empty' => t('No log messages available.'),
  121. );
  122. $build['dblog_top_pager'] = array('#theme' => 'pager');
  123. return $build;
  124. }
  125. /**
  126. * Page callback: Displays details about a specific database log message.
  127. *
  128. * @param int $id
  129. * Unique ID of the database log message.
  130. *
  131. * @return array|string
  132. * If the ID is located in the Database Logging table, a build array in the
  133. * format expected by drupal_render(); otherwise, an empty string.
  134. *
  135. * @see dblog_menu()
  136. */
  137. function dblog_event($id) {
  138. $severity = watchdog_severity_levels();
  139. $result = db_query('SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid WHERE w.wid = :id', array(':id' => $id))->fetchObject();
  140. if ($dblog = $result) {
  141. $rows = array(
  142. array(
  143. array('data' => t('Type'), 'header' => TRUE),
  144. t($dblog->type),
  145. ),
  146. array(
  147. array('data' => t('Date'), 'header' => TRUE),
  148. format_date($dblog->timestamp, 'long'),
  149. ),
  150. array(
  151. array('data' => t('User'), 'header' => TRUE),
  152. theme('username', array('account' => $dblog)),
  153. ),
  154. array(
  155. array('data' => t('Location'), 'header' => TRUE),
  156. l($dblog->location, $dblog->location),
  157. ),
  158. array(
  159. array('data' => t('Referrer'), 'header' => TRUE),
  160. l($dblog->referer, $dblog->referer),
  161. ),
  162. array(
  163. array('data' => t('Message'), 'header' => TRUE),
  164. theme('dblog_message', array('event' => $dblog)),
  165. ),
  166. array(
  167. array('data' => t('Severity'), 'header' => TRUE),
  168. $severity[$dblog->severity],
  169. ),
  170. array(
  171. array('data' => t('Hostname'), 'header' => TRUE),
  172. check_plain($dblog->hostname),
  173. ),
  174. array(
  175. array('data' => t('Operations'), 'header' => TRUE),
  176. $dblog->link,
  177. ),
  178. );
  179. $build['dblog_table'] = array(
  180. '#theme' => 'table',
  181. '#rows' => $rows,
  182. '#attributes' => array('class' => array('dblog-event')),
  183. );
  184. return $build;
  185. }
  186. else {
  187. return '';
  188. }
  189. }
  190. /**
  191. * Builds a query for database log administration filters based on session.
  192. *
  193. * @return array
  194. * An associative array with keys 'where' and 'args'.
  195. */
  196. function dblog_build_filter_query() {
  197. if (empty($_SESSION['dblog_overview_filter'])) {
  198. return;
  199. }
  200. $filters = dblog_filters();
  201. // Build query
  202. $where = $args = array();
  203. foreach ($_SESSION['dblog_overview_filter'] as $key => $filter) {
  204. $filter_where = array();
  205. foreach ($filter as $value) {
  206. $filter_where[] = $filters[$key]['where'];
  207. $args[] = $value;
  208. }
  209. if (!empty($filter_where)) {
  210. $where[] = '(' . implode(' OR ', $filter_where) . ')';
  211. }
  212. }
  213. $where = !empty($where) ? implode(' AND ', $where) : '';
  214. return array(
  215. 'where' => $where,
  216. 'args' => $args,
  217. );
  218. }
  219. /**
  220. * Creates a list of database log administration filters that can be applied.
  221. *
  222. * @return array
  223. * Associative array of filters. The top-level keys are used as the form
  224. * element names for the filters, and the values are arrays with the following
  225. * elements:
  226. * - title: Title of the filter.
  227. * - where: The filter condition.
  228. * - options: Array of options for the select list for the filter.
  229. */
  230. function dblog_filters() {
  231. $filters = array();
  232. foreach (_dblog_get_message_types() as $type) {
  233. $types[$type] = t($type);
  234. }
  235. if (!empty($types)) {
  236. $filters['type'] = array(
  237. 'title' => t('Type'),
  238. 'where' => "w.type = ?",
  239. 'options' => $types,
  240. );
  241. }
  242. $filters['severity'] = array(
  243. 'title' => t('Severity'),
  244. 'where' => 'w.severity = ?',
  245. 'options' => watchdog_severity_levels(),
  246. );
  247. return $filters;
  248. }
  249. /**
  250. * Returns HTML for a log message.
  251. *
  252. * @param array $variables
  253. * An associative array containing:
  254. * - event: An object with at least the message and variables properties.
  255. * - link: (optional) Format message as link, event->wid is required.
  256. *
  257. * @ingroup themeable
  258. */
  259. function theme_dblog_message($variables) {
  260. $output = '';
  261. $event = $variables['event'];
  262. // Check for required properties.
  263. if (isset($event->message) && isset($event->variables)) {
  264. // Messages without variables or user specified text.
  265. if ($event->variables === 'N;') {
  266. $output = $event->message;
  267. }
  268. // Message to translate with injected variables.
  269. else {
  270. $output = t($event->message, unserialize($event->variables));
  271. }
  272. // If the output is expected to be a link, strip all the tags and
  273. // special characters by using filter_xss() without any allowed tags.
  274. // If not, use filter_xss_admin() to allow some tags.
  275. if ($variables['link'] && isset($event->wid)) {
  276. // Truncate message to 56 chars after stripping all the tags.
  277. $output = truncate_utf8(filter_xss($output, array()), 56, TRUE, TRUE);
  278. $output = l($output, 'admin/reports/event/' . $event->wid, array('html' => TRUE));
  279. }
  280. else {
  281. // Prevent XSS in log detail pages.
  282. $output = filter_xss_admin($output);
  283. }
  284. }
  285. return $output;
  286. }
  287. /**
  288. * Form constructor for the database logging filter form.
  289. *
  290. * @see dblog_filter_form_validate()
  291. * @see dblog_filter_form_submit()
  292. * @see dblog_overview()
  293. *
  294. * @ingroup forms
  295. */
  296. function dblog_filter_form($form) {
  297. $filters = dblog_filters();
  298. $form['filters'] = array(
  299. '#type' => 'fieldset',
  300. '#title' => t('Filter log messages'),
  301. '#collapsible' => TRUE,
  302. '#collapsed' => empty($_SESSION['dblog_overview_filter']),
  303. );
  304. foreach ($filters as $key => $filter) {
  305. $form['filters']['status'][$key] = array(
  306. '#title' => $filter['title'],
  307. '#type' => 'select',
  308. '#multiple' => TRUE,
  309. '#size' => 8,
  310. '#options' => $filter['options'],
  311. );
  312. if (!empty($_SESSION['dblog_overview_filter'][$key])) {
  313. $form['filters']['status'][$key]['#default_value'] = $_SESSION['dblog_overview_filter'][$key];
  314. }
  315. }
  316. $form['filters']['actions'] = array(
  317. '#type' => 'actions',
  318. '#attributes' => array('class' => array('container-inline')),
  319. );
  320. $form['filters']['actions']['submit'] = array(
  321. '#type' => 'submit',
  322. '#value' => t('Filter'),
  323. );
  324. if (!empty($_SESSION['dblog_overview_filter'])) {
  325. $form['filters']['actions']['reset'] = array(
  326. '#type' => 'submit',
  327. '#value' => t('Reset')
  328. );
  329. }
  330. return $form;
  331. }
  332. /**
  333. * Form validation handler for dblog_filter_form().
  334. *
  335. * @see dblog_filter_form_submit()
  336. */
  337. function dblog_filter_form_validate($form, &$form_state) {
  338. if ($form_state['values']['op'] == t('Filter') && empty($form_state['values']['type']) && empty($form_state['values']['severity'])) {
  339. form_set_error('type', t('You must select something to filter by.'));
  340. }
  341. }
  342. /**
  343. * Form submission handler for dblog_filter_form().
  344. *
  345. * @see dblog_filter_form_validate()
  346. */
  347. function dblog_filter_form_submit($form, &$form_state) {
  348. $op = $form_state['values']['op'];
  349. $filters = dblog_filters();
  350. switch ($op) {
  351. case t('Filter'):
  352. foreach ($filters as $name => $filter) {
  353. if (isset($form_state['values'][$name])) {
  354. $_SESSION['dblog_overview_filter'][$name] = $form_state['values'][$name];
  355. }
  356. }
  357. break;
  358. case t('Reset'):
  359. $_SESSION['dblog_overview_filter'] = array();
  360. break;
  361. }
  362. return 'admin/reports/dblog';
  363. }
  364. /**
  365. * Form constructor for the form that clears out the log.
  366. *
  367. * @see dblog_clear_log_submit()
  368. * @ingroup forms
  369. */
  370. function dblog_clear_log_form($form) {
  371. $form['dblog_clear'] = array(
  372. '#type' => 'fieldset',
  373. '#title' => t('Clear log messages'),
  374. '#description' => t('This will permanently remove the log messages from the database.'),
  375. '#collapsible' => TRUE,
  376. '#collapsed' => TRUE,
  377. );
  378. $form['dblog_clear']['clear'] = array(
  379. '#type' => 'submit',
  380. '#value' => t('Clear log messages'),
  381. '#submit' => array('dblog_clear_log_submit'),
  382. );
  383. return $form;
  384. }
  385. /**
  386. * Form submission handler for dblog_clear_log_form().
  387. */
  388. function dblog_clear_log_submit() {
  389. $_SESSION['dblog_overview_filter'] = array();
  390. db_delete('watchdog')->execute();
  391. drupal_set_message(t('Database log cleared.'));
  392. }