You are here

function autoban_analyze in Automatic IP ban (Autoban) 7

Menu callback. Analize watchdog.

1 string reference to 'autoban_analyze'
autoban_menu in ./autoban.module
Implements hook_menu().

File

./autoban.admin.inc, line 600
Configuration for autoban module.

Code

function autoban_analyze() {

  // Default value.
  $threshold = 5;
  if (!empty($_SESSION['autoban']['analyze']['threshold'])) {
    $threshold = intval($_SESSION['autoban']['analyze']['threshold']);
  }
  $header = array(
    '#',
    array(
      'data' => t('Count'),
      'field' => 'cnt',
      'sort' => 'desc',
    ),
    array(
      'data' => t('Type'),
      'field' => 'type',
    ),
    array(
      'data' => t('Message'),
      'field' => 'message',
    ),
    array(
      'data' => t('Referrer'),
      'field' => 'referer',
    ),
    t('Actions'),
  );
  $query = db_select('watchdog', 'log')
    ->fields('log', array(
    'type',
    'message',
    'referer',
  ));
  $query
    ->addExpression('COUNT(*)', 'cnt');
  $query
    ->groupBy('log.message');
  $query
    ->condition('log.type', autoban_get_log_whitelist(), 'NOT IN');
  $query
    ->havingCondition('cnt', $threshold, '>=');
  $query
    ->extend('TableSort')
    ->orderByHeader($header);
  $variant2 = FALSE;
  try {
    $log = $query
      ->execute()
      ->fetchAll();
  } catch (Exception $e) {
    $query = db_select('watchdog', 'log')
      ->fields('log', array(
      'type',
      'message',
      'referer',
    ));
    $query
      ->addExpression('COUNT(*)', 'cnt');
    $query
      ->groupBy('log.message');
    $query
      ->condition('log.type', autoban_get_log_whitelist(), 'NOT IN');
    $query
      ->extend('TableSort')
      ->orderByHeader($header);
    $log = $query
      ->execute()
      ->fetchAll();
    $variant2 = TRUE;
  }
  $rows = array();
  if (count($log)) {
    $ind = 0;
    foreach ($log as $item) {
      $options = array();
      $options['query'] = array(
        'type' => $item->type,
        'message' => filter_xss($item->message),
        drupal_get_destination(),
      );
      $newitem = array(
        ++$ind,
        l($item->cnt, AUTOBAN_BASE_URL . '/test/direct', $options),
        $item->type,
        filter_xss($item->message),
        _autoban_short_referer($item->referer),
      );
      $actions = array();
      $actions[] = l(t('Add rule'), AUTOBAN_BASE_URL . '/add', $options);
      $actions[] = l(t('Test'), AUTOBAN_BASE_URL . '/test/direct', $options);
      $newitem['action'] = implode(' ', $actions);
      $rows[] = $newitem;
    }
  }
  $build = array();
  if (!$variant2) {
    $params = t("Parameters: Threshold>=%threshold.", array(
      '%threshold' => $threshold,
    ));
    $build['parameters'] = array(
      '#type' => 'markup',
      '#markup' => '<h2>' . $params . '</h2>',
    );
    $build['autoban_ban_all_form'] = drupal_get_form('autoban_analyze_form', $threshold);
  }
  $build['autoban_table'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => t('Table has no rows!'),
  );
  return $build;
}