You are here

function autoban_ban in Automatic IP ban (Autoban) 7

Ban IP's for rules.

Parameters

object $rule: Autoban rule object.

array $context: The batch context array, passed by reference.

Return value

int Number of successful IP insertion for one rule.

2 calls to autoban_ban()
autoban_ban_all in ./autoban.module
Ban all IP's for rules.
autoban_ban_form_submit in ./autoban.admin.inc
Form submission for autoban_ban_form().
1 string reference to 'autoban_ban'
autoban_ban_all_form_submit in ./autoban.admin.inc
Form submission for autoban_ban_all_form().

File

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

Code

function autoban_ban($rule, &$context = NULL) {
  $count = 0;
  if (!empty($rule)) {

    // Retrieve hostnames(IP) for ban.
    $hostnames = autoban_get_hostnames($rule);
    if (!empty($hostnames)) {
      foreach ($hostnames as $hostname) {

        // Make IP string: single or range.
        $banned_ip = autoban_make_ip_style($hostname->hostname, $rule->ip_type);
        if (isset($rule->rid)) {
          $message_variables = array(
            '@rule_url' => url(AUTOBAN_BASE_URL . '/edit/' . $rule->rid),
            '@rule' => $rule->rid,
          );
          $message_rule = "by <a href='@rule_url'>rule @rule</a>";
        }
        else {
          $message_variables = array();
          $message_rule = '';
        }

        // Check IP.
        if (autoban_can_banned($banned_ip, $hostname->hostname)) {

          // Insert banned IP to IP banned tables.
          $success = autoban_insert_banned_table($banned_ip, $rule->ip_type);
          if ($success) {
            $message_variables['@ip'] = $banned_ip;
            watchdog('autoban', "Banned IP @ip {$message_rule}.", $message_variables);
            $count++;
          }
        }
      }
    }

    // Batch operations.
    if (!empty($context)) {
      $context['results'][] = t('Banned by rule @rid', array(
        '@rid' => $rule->rid,
      ));
      $context['message'] = t('Ban by rule @rid', array(
        '@rid' => $rule->rid,
      ));

      // Update number banned IP.
      if (isset($_SESSION['autoban_ban_all_count'])) {
        $_SESSION['autoban_ban_all_count'] += $count;
      }
    }
  }
  return $count;
}