| 1 |
<?php
|
| 2 |
// $Id: dblog.admin.inc,v 1.30 2009/09/18 00:12:46 webchick Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Administrative page callbacks for the dblog module.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Menu callback; displays a listing of log messages.
|
| 11 |
*/
|
| 12 |
function dblog_overview() {
|
| 13 |
$filter = dblog_build_filter_query();
|
| 14 |
$rows = array();
|
| 15 |
$icons = array(
|
| 16 |
WATCHDOG_DEBUG => '',
|
| 17 |
WATCHDOG_INFO => '',
|
| 18 |
WATCHDOG_NOTICE => '',
|
| 19 |
WATCHDOG_WARNING => theme('image', array('path' => 'misc/watchdog-warning.png', 'alt' => t('warning'), 'title' => t('warning'))),
|
| 20 |
WATCHDOG_ERROR => theme('image', array('path' => 'misc/watchdog-error.png', 'alt' => t('error'), 'title' => t('error'))),
|
| 21 |
WATCHDOG_CRITICAL => theme('image', array('path' => 'misc/watchdog-error.png', 'alt' => t('critical'), 'title' => t('critical'))),
|
| 22 |
WATCHDOG_ALERT => theme('image', array('path' => 'misc/watchdog-error.png', 'alt' => t('alert'), 'title' => t('alert'))),
|
| 23 |
WATCHDOG_EMERG => theme('image', array('path' => 'misc/watchdog-error.png', 'alt' => t('emergency'), 'title' => t('emergency'))),
|
| 24 |
);
|
| 25 |
$classes = array(
|
| 26 |
WATCHDOG_DEBUG => 'dblog-debug',
|
| 27 |
WATCHDOG_INFO => 'dblog-info',
|
| 28 |
WATCHDOG_NOTICE => 'dblog-notice',
|
| 29 |
WATCHDOG_WARNING => 'dblog-warning',
|
| 30 |
WATCHDOG_ERROR => 'dblog-error',
|
| 31 |
WATCHDOG_CRITICAL => 'dblog-critical',
|
| 32 |
WATCHDOG_ALERT => 'dblog-alert',
|
| 33 |
WATCHDOG_EMERG => 'dblog-emerg',
|
| 34 |
);
|
| 35 |
|
| 36 |
$build['dblog_filter_form'] = drupal_get_form('dblog_filter_form');
|
| 37 |
$build['dblog_clear_log_form'] = drupal_get_form('dblog_clear_log_form');
|
| 38 |
|
| 39 |
$header = array(
|
| 40 |
'', // Icon column.
|
| 41 |
array('data' => t('Type'), 'field' => 'w.type'),
|
| 42 |
array('data' => t('Date'), 'field' => 'w.wid', 'sort' => 'desc'),
|
| 43 |
t('Message'),
|
| 44 |
array('data' => t('User'), 'field' => 'u.name'),
|
| 45 |
array('data' => t('Operations')),
|
| 46 |
);
|
| 47 |
|
| 48 |
$query = db_select('watchdog', 'w')->extend('PagerDefault')->extend('TableSort');
|
| 49 |
$query->join('users', 'u', 'w.uid = u.uid');
|
| 50 |
$query
|
| 51 |
->fields('w', array('wid', 'uid', 'severity', 'type', 'timestamp', 'message', 'variables', 'link'))
|
| 52 |
->addField('u', 'name');
|
| 53 |
if (!empty($filter['where'])) {
|
| 54 |
$query->where($filter['where'], $filter['args']);
|
| 55 |
}
|
| 56 |
$result = $query
|
| 57 |
->limit(50)
|
| 58 |
->orderByHeader($header)
|
| 59 |
->execute();
|
| 60 |
|
| 61 |
foreach ($result as $dblog) {
|
| 62 |
$rows[] = array('data' =>
|
| 63 |
array(
|
| 64 |
// Cells
|
| 65 |
$icons[$dblog->severity],
|
| 66 |
t($dblog->type),
|
| 67 |
format_date($dblog->timestamp, 'short'),
|
| 68 |
l(truncate_utf8(_dblog_format_message($dblog), 56, TRUE, TRUE), 'admin/reports/event/' . $dblog->wid, array('html' => TRUE)),
|
| 69 |
theme('username', array('account' => $dblog)),
|
| 70 |
$dblog->link,
|
| 71 |
),
|
| 72 |
// Attributes for tr
|
| 73 |
'class' => array('dblog-' . preg_replace('/[^a-z]/i', '-', $dblog->type), $classes[$dblog->severity]),
|
| 74 |
);
|
| 75 |
}
|
| 76 |
|
| 77 |
if (!$rows) {
|
| 78 |
$rows[] = array(array('data' => t('No log messages available.'), 'colspan' => 6));
|
| 79 |
}
|
| 80 |
|
| 81 |
$build['dblog_table'] = array(
|
| 82 |
'#theme' => 'table',
|
| 83 |
'#header' => $header,
|
| 84 |
'#rows' => $rows,
|
| 85 |
'#attributes' => array('id' => 'admin-dblog'),
|
| 86 |
);
|
| 87 |
$build['dblog_pager'] = array('#theme' => 'pager');
|
| 88 |
|
| 89 |
return $build;
|
| 90 |
}
|
| 91 |
|
| 92 |
/**
|
| 93 |
* Menu callback; generic function to display a page of the most frequent
|
| 94 |
* dblog events of a specified type.
|
| 95 |
*/
|
| 96 |
function dblog_top($type) {
|
| 97 |
|
| 98 |
$header = array(
|
| 99 |
array('data' => t('Count'), 'field' => 'count', 'sort' => 'desc'),
|
| 100 |
array('data' => t('Message'), 'field' => 'message')
|
| 101 |
);
|
| 102 |
$count_query = db_select('watchdog');
|
| 103 |
$count_query->addExpression('COUNT(DISTINCT(message))');
|
| 104 |
$count_query->condition('type', $type);
|
| 105 |
|
| 106 |
$query = db_select('watchdog', 'w')->extend('PagerDefault')->extend('TableSort');
|
| 107 |
$query->addExpression('COUNT(wid)', 'count');
|
| 108 |
$query = $query
|
| 109 |
->fields('w', array('message', 'variables'))
|
| 110 |
->condition('w.type', $type)
|
| 111 |
->groupBy('message')
|
| 112 |
->groupBy('variables')
|
| 113 |
->limit(30)
|
| 114 |
->orderByHeader($header);
|
| 115 |
$query->setCountQuery($count_query);
|
| 116 |
$result = $query->execute();
|
| 117 |
|
| 118 |
$rows = array();
|
| 119 |
foreach ($result as $dblog) {
|
| 120 |
$rows[] = array($dblog->count, truncate_utf8(_dblog_format_message($dblog), 56, TRUE, TRUE));
|
| 121 |
}
|
| 122 |
|
| 123 |
if (empty($rows)) {
|
| 124 |
$rows[] = array(array('data' => t('No log messages available.'), 'colspan' => 2));
|
| 125 |
}
|
| 126 |
|
| 127 |
$build['dblog_top_table'] = array(
|
| 128 |
'#theme' => 'table',
|
| 129 |
'#header' => $header,
|
| 130 |
'#rows' => $rows,
|
| 131 |
);
|
| 132 |
$build['dblog_top_pager'] = array('#theme' => 'pager');
|
| 133 |
|
| 134 |
return $build;
|
| 135 |
}
|
| 136 |
|
| 137 |
/**
|
| 138 |
* Menu callback; displays details about a log message.
|
| 139 |
*/
|
| 140 |
function dblog_event($id) {
|
| 141 |
$severity = watchdog_severity_levels();
|
| 142 |
$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();
|
| 143 |
if ($dblog = $result) {
|
| 144 |
$rows = array(
|
| 145 |
array(
|
| 146 |
array('data' => t('Type'), 'header' => TRUE),
|
| 147 |
t($dblog->type),
|
| 148 |
),
|
| 149 |
array(
|
| 150 |
array('data' => t('Date'), 'header' => TRUE),
|
| 151 |
format_date($dblog->timestamp, 'long'),
|
| 152 |
),
|
| 153 |
array(
|
| 154 |
array('data' => t('User'), 'header' => TRUE),
|
| 155 |
theme('username', array('account' => $dblog)),
|
| 156 |
),
|
| 157 |
array(
|
| 158 |
array('data' => t('Location'), 'header' => TRUE),
|
| 159 |
l($dblog->location, $dblog->location),
|
| 160 |
),
|
| 161 |
array(
|
| 162 |
array('data' => t('Referrer'), 'header' => TRUE),
|
| 163 |
l($dblog->referer, $dblog->referer),
|
| 164 |
),
|
| 165 |
array(
|
| 166 |
array('data' => t('Message'), 'header' => TRUE),
|
| 167 |
_dblog_format_message($dblog),
|
| 168 |
),
|
| 169 |
array(
|
| 170 |
array('data' => t('Severity'), 'header' => TRUE),
|
| 171 |
$severity[$dblog->severity],
|
| 172 |
),
|
| 173 |
array(
|
| 174 |
array('data' => t('Hostname'), 'header' => TRUE),
|
| 175 |
check_plain($dblog->hostname),
|
| 176 |
),
|
| 177 |
array(
|
| 178 |
array('data' => t('Operations'), 'header' => TRUE),
|
| 179 |
$dblog->link,
|
| 180 |
),
|
| 181 |
);
|
| 182 |
$build['dblog_table'] = array(
|
| 183 |
'#theme' => 'table',
|
| 184 |
'#rows' => $rows,
|
| 185 |
'#attributes' => array('class' => array('dblog-event')),
|
| 186 |
);
|
| 187 |
return $build;
|
| 188 |
}
|
| 189 |
else {
|
| 190 |
return '';
|
| 191 |
}
|
| 192 |
}
|
| 193 |
|
| 194 |
/**
|
| 195 |
* Build query for dblog administration filters based on session.
|
| 196 |
*/
|
| 197 |
function dblog_build_filter_query() {
|
| 198 |
if (empty($_SESSION['dblog_overview_filter'])) {
|
| 199 |
return;
|
| 200 |
}
|
| 201 |
|
| 202 |
$filters = dblog_filters();
|
| 203 |
|
| 204 |
// Build query
|
| 205 |
$where = $args = array();
|
| 206 |
foreach ($_SESSION['dblog_overview_filter'] as $key => $filter) {
|
| 207 |
$filter_where = array();
|
| 208 |
foreach ($filter as $value) {
|
| 209 |
$filter_where[] = $filters[$key]['where'];
|
| 210 |
$args[] = $value;
|
| 211 |
}
|
| 212 |
if (!empty($filter_where)) {
|
| 213 |
$where[] = '(' . implode(' OR ', $filter_where) . ')';
|
| 214 |
}
|
| 215 |
}
|
| 216 |
$where = !empty($where) ? implode(' AND ', $where) : '';
|
| 217 |
|
| 218 |
return array(
|
| 219 |
'where' => $where,
|
| 220 |
'args' => $args,
|
| 221 |
);
|
| 222 |
}
|
| 223 |
|
| 224 |
|
| 225 |
/**
|
| 226 |
* List dblog administration filters that can be applied.
|
| 227 |
*/
|
| 228 |
function dblog_filters() {
|
| 229 |
$filters = array();
|
| 230 |
|
| 231 |
foreach (_dblog_get_message_types() as $type) {
|
| 232 |
$types[$type] = $type;
|
| 233 |
}
|
| 234 |
|
| 235 |
if (!empty($types)) {
|
| 236 |
$filters['type'] = array(
|
| 237 |
'title' => t('Type'),
|
| 238 |
'where' => "w.type = ?",
|
| 239 |
'options' => $types,
|
| 240 |
);
|
| 241 |
}
|
| 242 |
|
| 243 |
$filters['severity'] = array(
|
| 244 |
'title' => t('Severity'),
|
| 245 |
'where' => 'w.severity = ?',
|
| 246 |
'options' => watchdog_severity_levels(),
|
| 247 |
);
|
| 248 |
|
| 249 |
return $filters;
|
| 250 |
}
|
| 251 |
|
| 252 |
/**
|
| 253 |
* Formats a log message for display.
|
| 254 |
*
|
| 255 |
* @param $dblog
|
| 256 |
* An object with at least the message and variables properties
|
| 257 |
*/
|
| 258 |
function _dblog_format_message($dblog) {
|
| 259 |
// Legacy messages and user specified text
|
| 260 |
if ($dblog->variables === 'N;') {
|
| 261 |
return $dblog->message;
|
| 262 |
}
|
| 263 |
// Message to translate with injected variables
|
| 264 |
else {
|
| 265 |
return t($dblog->message, unserialize($dblog->variables));
|
| 266 |
}
|
| 267 |
}
|
| 268 |
|
| 269 |
|
| 270 |
/**
|
| 271 |
* Return form for dblog administration filters.
|
| 272 |
*
|
| 273 |
* @ingroup forms
|
| 274 |
* @see dblog_filter_form_submit()
|
| 275 |
* @see dblog_filter_form_validate()
|
| 276 |
*/
|
| 277 |
function dblog_filter_form($form) {
|
| 278 |
$filters = dblog_filters();
|
| 279 |
|
| 280 |
$form['filters'] = array(
|
| 281 |
'#type' => 'fieldset',
|
| 282 |
'#title' => t('Filter log messages'),
|
| 283 |
'#theme' => 'dblog_filters',
|
| 284 |
'#collapsible' => TRUE,
|
| 285 |
'#collapsed' => empty($session),
|
| 286 |
);
|
| 287 |
foreach ($filters as $key => $filter) {
|
| 288 |
$form['filters']['status'][$key] = array(
|
| 289 |
'#title' => $filter['title'],
|
| 290 |
'#type' => 'select',
|
| 291 |
'#multiple' => TRUE,
|
| 292 |
'#size' => 8,
|
| 293 |
'#options' => $filter['options'],
|
| 294 |
);
|
| 295 |
if (!empty($_SESSION['dblog_overview_filter'][$key])) {
|
| 296 |
$form['filters']['status'][$key]['#default_value'] = $_SESSION['dblog_overview_filter'][$key];
|
| 297 |
}
|
| 298 |
}
|
| 299 |
|
| 300 |
$form['filters']['buttons']['submit'] = array(
|
| 301 |
'#type' => 'submit',
|
| 302 |
'#value' => t('Filter'),
|
| 303 |
);
|
| 304 |
if (!empty($_SESSION['dblog_overview_filter'])) {
|
| 305 |
$form['filters']['buttons']['reset'] = array(
|
| 306 |
'#type' => 'submit',
|
| 307 |
'#value' => t('Reset')
|
| 308 |
);
|
| 309 |
}
|
| 310 |
|
| 311 |
return $form;
|
| 312 |
}
|
| 313 |
|
| 314 |
/**
|
| 315 |
* Validate result from dblog administration filter form.
|
| 316 |
*/
|
| 317 |
function dblog_filter_form_validate($form, &$form_state) {
|
| 318 |
if ($form_state['values']['op'] == t('Filter') && empty($form_state['values']['type']) && empty($form_state['values']['severity'])) {
|
| 319 |
form_set_error('type', t('You must select something to filter by.'));
|
| 320 |
}
|
| 321 |
}
|
| 322 |
|
| 323 |
/**
|
| 324 |
* Process result from dblog administration filter form.
|
| 325 |
*/
|
| 326 |
function dblog_filter_form_submit($form, &$form_state) {
|
| 327 |
$op = $form_state['values']['op'];
|
| 328 |
$filters = dblog_filters();
|
| 329 |
switch ($op) {
|
| 330 |
case t('Filter'):
|
| 331 |
foreach ($filters as $name => $filter) {
|
| 332 |
if (isset($form_state['values'][$name])) {
|
| 333 |
$_SESSION['dblog_overview_filter'][$name] = $form_state['values'][$name];
|
| 334 |
}
|
| 335 |
}
|
| 336 |
break;
|
| 337 |
case t('Reset'):
|
| 338 |
$_SESSION['dblog_overview_filter'] = array();
|
| 339 |
break;
|
| 340 |
}
|
| 341 |
return 'admin/reports/dblog';
|
| 342 |
}
|
| 343 |
|
| 344 |
/**
|
| 345 |
* Return form for dblog clear button.
|
| 346 |
*
|
| 347 |
* @ingroup forms
|
| 348 |
* @see dblog_clear_log_submit()
|
| 349 |
*/
|
| 350 |
function dblog_clear_log_form($form) {
|
| 351 |
$form['dblog_clear'] = array(
|
| 352 |
'#type' => 'fieldset',
|
| 353 |
'#title' => t('Clear log messages'),
|
| 354 |
'#description' => t('This will permanently remove the log messages from the database.'),
|
| 355 |
'#collapsible' => TRUE,
|
| 356 |
'#collapsed' => TRUE,
|
| 357 |
);
|
| 358 |
$form['dblog_clear']['clear'] = array(
|
| 359 |
'#type' => 'submit',
|
| 360 |
'#value' => t('Clear log messages'),
|
| 361 |
'#submit' => array('dblog_clear_log_submit'),
|
| 362 |
);
|
| 363 |
|
| 364 |
return $form;
|
| 365 |
}
|
| 366 |
|
| 367 |
/**
|
| 368 |
* Submit callback: clear database with log messages.
|
| 369 |
*/
|
| 370 |
function dblog_clear_log_submit() {
|
| 371 |
db_delete('watchdog')->execute();
|
| 372 |
drupal_set_message(t('Database log cleared.'));
|
| 373 |
}
|