You are here

public function AutobanController::banIp in Automatic IP ban (Autoban) 8

Ban address.

Parameters

string $ip: IP address.

array $banManagerData: Ban manager data.

bool $debug: Show debug message.

Return value

bool IP banned status.

2 calls to AutobanController::banIp()
AutobanController::banIpAction in src/Controller/AutobanController.php
Direct ban controller.
AutobanController::banIpList in src/Controller/AutobanController.php
Ban addresses.

File

src/Controller/AutobanController.php, line 264

Class

AutobanController
Provides an Autoban functional.

Namespace

Drupal\autoban\Controller

Code

public function banIp($ip, array $banManagerData, $debug = FALSE) {
  if (empty($banManagerData)) {
    if ($debug) {
      $this
        ->messenger()
        ->addMessage($this
        ->t('Empty banManagerData.'), 'warning');
    }
    return FALSE;
  }
  $banManager = $banManagerData['ban_manager'];
  if (!$this
    ->canIpBan($ip)) {
    if ($debug) {
      $this
        ->messenger()
        ->addMessage($this
        ->t('Cannot ban this IP.'), 'warning');
    }
    return FALSE;
  }
  if ($banManager
    ->isBanned($ip)) {
    if ($debug) {
      $this
        ->messenger()
        ->addMessage($this
        ->t('This IP already banned.'), 'warning');
    }
    return FALSE;
  }
  $banType = $banManagerData['ban_type'];
  switch ($banType) {
    case 'single':
      $banManager
        ->banIp($ip);
      break;
    case 'range':
      $ip_range = $this
        ->createIpRange($ip);
      if (empty($ip_range)) {

        // If cannot create IP range banned single IP.
        $banManager
          ->banIp($ip);
      }
      else {
        $banManager
          ->banIp($ip_range['ip_start'], $ip_range['ip_end']);
      }
      break;
  }
  return TRUE;
}