You are here

filelog_ui.pages.inc in File Log 6.2

Log viewer page callbacks for the filelog_ui module.

File

filelog_ui.pages.inc
View source
<?php

/**
 * @file
 * Log viewer page callbacks for the filelog_ui module.
 */

/**
 * Menu callback; displays a listing of log messages.
 */
function filelog_ui_overview() {
  $output = drupal_get_form('filelog_ui_filter_form');
  $filter = $_SESSION['filelog_ui_overview_filter'];
  $filter = is_array($filter) ? $filter : array();
  $filter = is_array($filter['filter']) ? $filter['filter'] : array();
  $rows = array();
  $icons = array(
    WATCHDOG_DEBUG => '',
    WATCHDOG_INFO => '',
    WATCHDOG_NOTICE => '',
    WATCHDOG_WARNING => theme('image', 'misc/watchdog-warning.png', t('warning'), t('warning')),
    WATCHDOG_ERROR => theme('image', 'misc/watchdog-error.png', t('error'), t('error')),
    WATCHDOG_CRITICAL => theme('image', 'misc/watchdog-error.png', t('critical'), t('critical')),
    WATCHDOG_ALERT => theme('image', 'misc/watchdog-error.png', t('alert'), t('alert')),
    WATCHDOG_EMERG => theme('image', 'misc/watchdog-error.png', t('emergency'), t('emergency')),
  );
  $classes = array(
    WATCHDOG_DEBUG => 'filelog-ui-debug',
    WATCHDOG_INFO => 'filelog-ui-info',
    WATCHDOG_NOTICE => 'filelog-ui-notice',
    WATCHDOG_WARNING => 'filelog-ui-warning',
    WATCHDOG_ERROR => 'filelog-ui-error',
    WATCHDOG_CRITICAL => 'filelog-ui-critical',
    WATCHDOG_ALERT => 'filelog-ui-alert',
    WATCHDOG_EMERG => 'filelog-ui-emerg',
  );
  $header = array(
    ' ',
    array(
      'data' => t('Type'),
      'field' => 'f.type',
    ),
    array(
      'data' => t('Date'),
      'field' => 'f.timestamp',
      'sort' => 'desc',
    ),
    t('Message'),
    array(
      'data' => t('User'),
      'field' => 'u.name',
    ),
    array(
      'data' => t('Operations'),
    ),
  );
  $sql = "SELECT f.wid, f.uid, f.severity, f.type, f.timestamp, f.message, f.variables, f.link, u.name FROM {filelog} f INNER JOIN {users} u ON f.uid = u.uid";
  $tablesort = tablesort_sql($header);
  if (!empty($filter['where'])) {
    $result = pager_query($sql . " WHERE " . $filter['where'] . $tablesort, 50, 0, NULL, $filter['args']);
  }
  else {
    $result = pager_query($sql . $tablesort, 50);
  }
  while ($flog = db_fetch_object($result)) {
    $rows[] = array(
      'data' => array(
        // Cells
        $icons[$flog->severity],
        t($flog->type),
        format_date($flog->timestamp, 'small'),
        l(truncate_utf8(_filelog_ui_format_message($flog), 56, TRUE, TRUE), 'admin/reports/filelog-entry/' . $flog->wid, array(
          'html' => TRUE,
        )),
        theme('username', $flog),
        $flog->link,
      ),
      // Attributes for tr
      'class' => "filelog-ui-" . preg_replace('/[^a-z]/i', '-', $flog->type) . ' ' . $classes[$flog->severity],
    );
  }
  if (!$rows) {
    $rows[] = array(
      array(
        'data' => t('No log messages available.'),
        'colspan' => 6,
      ),
    );
  }
  $output .= theme('table', $header, $rows, array(
    'id' => 'admin-filelog-ui',
  ));
  $output .= theme('pager', NULL, 50, 0);
  return $output;
}

/**
 * Menu callback; generic function to display a page of the most frequent
 * filelog entries of a specified type.
 */
function filelog_ui_top($type) {
  $header = array(
    array(
      'data' => t('Count'),
      'field' => 'count',
      'sort' => 'desc',
    ),
    array(
      'data' => t('Message'),
      'field' => 'message',
    ),
  );
  $result = pager_query("SELECT COUNT(wid) AS count, message, variables FROM {filelog} WHERE type = '%s' GROUP BY message, variables " . tablesort_sql($header), 30, 0, "SELECT COUNT(DISTINCT(message)) FROM {watchdog} WHERE type = '%s'", $type);
  $rows = array();
  while ($flog = db_fetch_object($result)) {
    $rows[] = array(
      $flog->count,
      truncate_utf8(_filelog_ui_format_message($flog), 56, TRUE, TRUE),
    );
  }
  if (empty($rows)) {
    $rows[] = array(
      array(
        'data' => t('No log messages available.'),
        'colspan' => 2,
      ),
    );
  }
  $output = theme('table', $header, $rows);
  $output .= theme('pager', NULL, 30, 0);
  return $output;
}

/**
 * Menu callback; displays details about a log message.
 */
function filelog_ui_entry($id) {
  $severity = watchdog_severity_levels();
  $output = '';
  $result = db_query("SELECT w.*, u.name, u.uid FROM {filelog} w INNER JOIN {users} u ON w.uid = u.uid WHERE w.wid = '%s'", $id);
  if ($flog = db_fetch_object($result)) {
    $rows = array(
      array(
        array(
          'data' => t('Type'),
          'header' => TRUE,
        ),
        t($flog->type),
      ),
      array(
        array(
          'data' => t('Date'),
          'header' => TRUE,
        ),
        format_date($flog->timestamp, 'large'),
      ),
      array(
        array(
          'data' => t('User'),
          'header' => TRUE,
        ),
        theme('username', $flog),
      ),
      array(
        array(
          'data' => t('Location'),
          'header' => TRUE,
        ),
        l($flog->location, $flog->location),
      ),
      array(
        array(
          'data' => t('Referrer'),
          'header' => TRUE,
        ),
        l($flog->referer, $flog->referer),
      ),
      array(
        array(
          'data' => t('Message'),
          'header' => TRUE,
        ),
        _filelog_ui_format_message($flog),
      ),
      array(
        array(
          'data' => t('Severity'),
          'header' => TRUE,
        ),
        $severity[$flog->severity],
      ),
      array(
        array(
          'data' => t('Hostname'),
          'header' => TRUE,
        ),
        check_plain($flog->hostname),
      ),
      array(
        array(
          'data' => t('Operations'),
          'header' => TRUE,
        ),
        $flog->link,
      ),
    );
    $attributes = array(
      'class' => 'filelog-ui-event',
    );
    $output = theme('table', array(), $rows, $attributes);
  }
  return $output;
}

/**
 * Return form for filelog_ui administration filters.
 *
 * @ingroup forms
 * @see filelog_ui_filter_form_submit()
 */
function filelog_ui_filter_form() {
  $session =& $_SESSION['filelog_ui_overview_filter'];
  $session = is_array($session) ? $session : array();
  $types = array();
  foreach (_filelog_ui_get_message_types() as $type) {
    $types[$type] = $type;
  }
  $form['filters'] = array(
    '#type' => 'fieldset',
    '#title' => t('Filter log messages'),
    '#theme' => 'filelog_ui_filters',
    '#collapsible' => TRUE,
    '#collapsed' => !empty($session),
  );
  $form['filters']['status']['type'] = array(
    '#title' => t('Type'),
    '#type' => 'select',
    '#multiple' => TRUE,
    '#size' => 8,
    '#options' => $types,
    '#attributes' => array(
      'class' => 'filelog-ui-multiple',
    ),
  );
  if (!empty($session['type'])) {
    $form['filters']['status']['type']['#default_value'] = $session['type'];
  }
  $form['filters']['status']['severity'] = array(
    '#title' => t('Severity'),
    '#type' => 'select',
    '#multiple' => TRUE,
    '#size' => 8,
    '#options' => watchdog_severity_levels(),
    '#attributes' => array(
      'class' => 'filelog-ui-multiple',
    ),
  );
  if (!empty($session['severity'])) {
    $form['filters']['status']['severity']['#default_value'] = $session['severity'];
  }
  $form['filters']['status']['date_from'] = array(
    '#title' => t('Date from'),
    '#type' => 'date',
    '#default_value' => empty($session['date_from']) ? _filelog_ui_time_to_date(time() - 86400) : $session['date_from'],
    '#prefix' => '<div class="container-dates">',
  );
  $form['filters']['status']['date_to'] = array(
    '#title' => t('Date to'),
    '#type' => 'date',
    '#suffix' => '</div>',
  );
  if (!empty($session['date_to'])) {
    $form['filters']['status']['date_to']['#default_value'] = $session['date_to'];
  }
  $form['filters']['status']['hostname'] = array(
    '#title' => t('Hostname (IP address)'),
    '#type' => 'textfield',
    '#size' => 25,
    '#field_prefix' => t('Starts with'),
    '#prefix' => '<div class="container-texts">',
  );
  if (!empty($session['hostname'])) {
    $form['filters']['status']['hostname']['#default_value'] = $session['hostname'];
  }
  $form['filters']['status']['referer'] = array(
    '#title' => t('Referer'),
    '#type' => 'textfield',
    '#size' => 25,
    '#field_prefix' => t('Starts with'),
    '#suffix' => '</div>',
  );
  if (!empty($session['referer'])) {
    $form['filters']['status']['referer']['#default_value'] = $session['referer'];
  }
  $form['filters']['buttons']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Filter'),
  );
  if (!empty($session)) {
    $form['filters']['buttons']['reset'] = array(
      '#type' => 'submit',
      '#value' => t('Reset'),
    );
  }
  return $form;
}

/**
 * Process result from filelog_ui administration filter form.
 */
function filelog_ui_filter_form_submit($form, &$form_state) {
  $op = $form_state['values']['op'];
  switch ($op) {
    case t('Filter'):
      $s =& $_SESSION['filelog_ui_overview_filter'];
      $s = array();
      $v =& $form_state['values'];
      $filters = array(
        'type',
        'severity',
        'date_from',
        'date_to',
        'hostname',
        'referer',
      );
      foreach ($filters as $name) {
        if (isset($v[$name])) {
          $s[$name] = $v[$name];
        }
      }

      // Build query
      $where = $args = array();
      foreach ($s as $key => $filter) {
        switch ($key) {
          case 'date_from':
            $where[] = 'f.timestamp > %d';
            $args[] = _filelog_ui_date_to_time($filter);
            break;
          case 'date_to':
            $where[] = 'f.timestamp < %d';
            $args[] = _filelog_ui_date_to_time($filter) + 86400;
            break;
          case 'type':
          case 'severity':
            $filter_where = array();
            foreach ($filter as $value) {
              $filter_where[] = 'f.' . $key . " = '%s'";
              $args[] = $value;
            }
            if (!empty($filter_where)) {
              $where[] = '(' . implode(' OR ', $filter_where) . ')';
            }
            break;
          default:
            if (!empty($filter)) {
              $where[] = 'f.' . $key . " LIKE '%s%%'";
              $args[] = $filter;
            }
            break;
        }
      }
      $where = !empty($where) ? implode(' AND ', $where) : '';
      $s['filter'] = array(
        'where' => $where,
        'args' => $args,
      );
      break;
    case t('Reset'):
      $_SESSION['filelog_ui_overview_filter'] = array();
      break;
  }
}

/**
 * Theme filolog_ui administration filter selector.
 *
 * @ingroup themeable
 */
function theme_filelog_ui_filters($form) {
  $output = '';
  foreach (element_children($form['status']) as $key) {
    $output .= drupal_render($form['status'][$key]);
  }
  $output .= '<div id="filelog-ui-admin-buttons">' . drupal_render($form['buttons']) . '</div>';
  return $output;
}

/**
 * Formats a log message for display.
 *
 * @param $flog
 *   An object with at least the message and variables properties
 */
function _filelog_ui_format_message($flog) {
  if ($flog->variables) {
    return t($flog->message, unserialize($flog->variables));
  }
  else {
    return t($flog->message);
  }
}
function _filelog_ui_get_message_types($refresh = FALSE) {
  static $types = array();
  if (empty($types) || $refresh) {
    if ($cache = cache_get('filelog_type_registry', 'cache')) {
      $types = $cache->data;
    }
    else {
      $result = db_query('SELECT DISTINCT(type) FROM {filelog} ORDER BY type');
      while ($object = db_fetch_object($result)) {
        $types[] = $object->type;
      }
      cache_set('filelog_type_registry', $types, 'cache');
    }
  }
  return $types;
}
function _filelog_ui_time_to_date($time) {
  return array(
    'day' => format_date($time, 'custom', 'j'),
    'month' => format_date($time, 'custom', 'n'),
    'year' => format_date($time, 'custom', 'Y'),
  );
}
function _filelog_ui_date_to_time($date) {
  return mktime(0, 0, 0, $date['month'], $date['day'], $date['year']);
}

Functions

Namesort descending Description
filelog_ui_entry Menu callback; displays details about a log message.
filelog_ui_filter_form Return form for filelog_ui administration filters.
filelog_ui_filter_form_submit Process result from filelog_ui administration filter form.
filelog_ui_overview Menu callback; displays a listing of log messages.
filelog_ui_top Menu callback; generic function to display a page of the most frequent filelog entries of a specified type.
theme_filelog_ui_filters Theme filolog_ui administration filter selector.
_filelog_ui_date_to_time
_filelog_ui_format_message Formats a log message for display.
_filelog_ui_get_message_types
_filelog_ui_time_to_date