You are here

function autoban_get_hostnames in Automatic IP ban (Autoban) 7

Get hostnames for ban from watchdog table by autoban rules.

Parameters

object $options: Parameters query.

array $header: (optional) Hostnames table header.

Return value

array An array of hostnames.

2 calls to autoban_get_hostnames()
autoban_ban in ./autoban.module
Ban IP's for rules.
autoban_test in ./autoban.admin.inc
Menu callback. Test autoban rule page.

File

./autoban.module, line 473
Main file for autoban module.

Code

function autoban_get_hostnames($options, $header = array()) {
  $use_wildcards = variable_get('autoban_use_wildcards', FALSE);
  $query = db_select('watchdog', 'log')
    ->fields('log', array(
    'hostname',
  ))
    ->groupBy('log.hostname');
  if (!empty($options->type)) {
    $query
      ->condition('log.type', $options->type);
  }
  if (!empty($options->message)) {
    $regexp_mode = variable_get('autoban_query_mode', 0) == 1;
    $message_items = explode(AUTOBAN_MESSAGE_SEPARATOR, $options->message);
    if (count($message_items) > 1) {
      $or = db_or();
      foreach ($message_items as $message_item) {
        if ($regexp_mode) {
          $or
            ->where("log.message REGEXP(:message)", array(
            ':message' => trim($message_item),
          ));
        }
        else {
          $db_like = db_like(trim($message_item));
          $db_like = str_replace("\\%", "%", $db_like);
          if (!$use_wildcards) {
            $db_like = '%' . $db_like . '%';
          }
          $or
            ->condition('log.message', $db_like, 'LIKE');
        }
      }
      $query
        ->condition($or);
    }
    else {
      if ($regexp_mode) {
        $query
          ->where("log.message REGEXP(:message)", array(
          ':message' => $options->message,
        ));
      }
      else {
        $db_like = db_like(trim($options->message));
        $db_like = str_replace("\\%", "%", $db_like);
        if (!$use_wildcards) {
          $db_like = '%' . $db_like . '%';
        }
        $query
          ->condition('log.message', $db_like, 'LIKE');
      }
    }
  }
  if (!empty($options->referer)) {
    $referer_items = explode(AUTOBAN_MESSAGE_SEPARATOR, $options->referer);
    if (count($referer_items) > 1) {
      $or = db_or();
      foreach ($referer_items as $referer_item) {
        $db_like = db_like(trim($referer_item));
        $db_like = str_replace("\\%", "%", $db_like);
        if (!$use_wildcards) {
          $db_like = '%' . $db_like . '%';
        }
        $or
          ->condition('log.referer', $db_like, 'LIKE');
      }
      $query
        ->condition($or);
    }
    else {
      $db_like = db_like(trim($options->referer));
      $db_like = str_replace("\\%", "%", $db_like);
      if (!$use_wildcards) {
        $db_like = '%' . $db_like . '%';
      }
      $query
        ->condition('log.referer', $db_like, 'LIKE');
    }
  }
  if ($options->user_type > 0) {
    if ($options->user_type == 1) {

      // Anonymous user.
      $query
        ->condition('log.uid', 0);
    }
    else {

      // Authenticated user.
      $query
        ->condition('log.uid', 0, '>');
    }
  }
  $query
    ->addExpression('COUNT(*)', 'hcount');
  $query
    ->havingCondition('hcount', $options->threshold, '>=');
  if (count($header)) {
    $query
      ->extend('TableSort')
      ->orderByHeader($header);
  }
  return $query
    ->execute()
    ->fetchAll();
}