You are here

function autoban_test in Automatic IP ban (Autoban) 7

Menu callback. Test autoban rule page.

Parameters

int|string $rid: Autoban rule id.

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

File

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

Code

function autoban_test($rid) {
  $backlink = isset($_GET['destination']) ? $_GET['destination'] : AUTOBAN_BASE_URL;
  $build = array();
  $build['backlink'] = array(
    '#type' => 'markup',
    '#markup' => l(t('Back'), $backlink),
  );
  if ($rid == 'direct') {

    // Test for direct type and message.
    $params = drupal_get_query_parameters();
    if (!isset($params['type']) || !isset($params['message'])) {
      drupal_set_message(t('Wrong parameters'), 'error');
      return $build;
    }

    // Create rule object manually.
    $rule = new stdClass();
    $rule->type = filter_xss($params['type']);
    $rule->message = filter_xss($params['message']);
    $rule->threshold = 1;
    $rule->user_type = 0;
    $rule->ip_type = 0;
    $rule->referer = '';
  }
  else {
    $rule = autoban_get_rules($rid);
  }
  if (!empty($rule)) {
    $params = t("Parameters: Type='%type' Message pattern '%message' Threshold>=%threshold User type=%user_type IP type=%ip_type Referer like %referer.", array(
      '%type' => filter_xss($rule->type),
      '%message' => filter_xss($rule->message),
      '%threshold' => $rule->threshold,
      '%user_type' => _autoban_get_user_type_name($rule->user_type),
      '%ip_type' => _autoban_get_ip_type_name($rule->ip_type),
      '%referer' => filter_xss($rule->referer),
    ));
    $build['parameters'] = array(
      '#type' => 'markup',
      '#markup' => '<h2>' . $params . '</h2>',
    );
    $rows = array();
    $header = array(
      '#',
      array(
        'data' => t('IP'),
        'field' => 'hostname',
      ),
      array(
        'data' => t('Count'),
        'field' => 'hcount',
      ),
      t('IP for ban'),
      t('Hostname'),
      t('Location'),
      t('Status'),
      t('Actions'),
    );

    // Hostnames(IP) list from watchdog table using the rule.
    $hostnames = autoban_get_hostnames($rule, $header);
    if (!empty($hostnames)) {
      $large_list = count($hostnames) > variable_get('autoban_large_list', 150);
      $ipstack_hostname = variable_get('autoban_gethostbyaddr_function', 'gethostbyaddr') == 'ipstack';
      $ind = 0;
      unset($_GET['destination']);
      $options = array(
        'query' => drupal_get_destination(),
      );
      foreach ($hostnames as $hostname) {
        $banned_ip = autoban_make_ip_style($hostname->hostname, $rule->ip_type);
        if ($ipstack_hostname) {
          $ipstack_info = autoban_get_ip_location($hostname->hostname, TRUE);
          $ip_location = $ipstack_info['location'];
          $real_hostname = $ipstack_info['hostname'];
        }
        else {
          $ip_location = autoban_get_ip_location($hostname->hostname);
          $real_hostname = autoban_gethostbyaddr($hostname->hostname);
        }
        $ip_status = '';
        if (_autoban_is_own_ip($banned_ip)) {
          $ip_status = t('Own IP address');
        }
        else {
          if (autoban_whitelist_ip($banned_ip)) {
            $ip_status = t('In whitelist');
          }
        }
        if (!$large_list) {

          // For large lists, this operation can significantly reduce performance.
          $is_banned = autoban_is_banned($hostname->hostname, $rule->ip_type);
          if ($is_banned) {
            $ip_status = t('Banned');
          }
        }
        else {
          $ip_status = t('Undefined');
        }
        $srows = array();
        $srows[] = l(t(_autoban_get_ip_type_name(AUTOBAN_SINGLE_IP) . ' ban'), AUTOBAN_BASE_URL . "/ban/{$hostname->hostname}/" . AUTOBAN_SINGLE_IP, $options);
        if (_autoban_ip_ranges_enable()) {
          $srows[] = l(t(_autoban_get_ip_type_name(AUTOBAN_RANGE_IP) . ' ban'), AUTOBAN_BASE_URL . "/ban/{$hostname->hostname}/" . AUTOBAN_RANGE_IP, $options);
        }
        $rows[] = array(
          ++$ind,
          $hostname->hostname,
          $hostname->hcount,
          $banned_ip,
          $real_hostname ? $real_hostname : '',
          $ip_location,
          $ip_status,
          implode(' ', $srows),
        );
      }
      $build['autoban_ban_form'] = drupal_get_form('autoban_ban_form', $rule);
    }
    $build['autoban_table'] = array(
      '#theme' => 'table',
      '#header' => $header,
      '#rows' => $rows,
      '#empty' => t('Table has no rows!'),
    );
  }
  else {
    drupal_set_message(t('Empty rules list for id=@id.', array(
      '@id' => $rid,
    )), 'error');
  }
  return $build;
}