You are here

public function AdvbanIpManager::banIp in Advanced ban 8

Bans an IP address.

Parameters

string $ip: The IP address to ban.

string $ip_end: The end of the IP address to ban (optional).

int $expiry_date: Expiry date of IP ban (optional).

Overrides AdvbanIpManagerInterface::banIp

File

src/AdvbanIpManager.php, line 160

Class

AdvbanIpManager
Ban IP manager.

Namespace

Drupal\advban

Code

public function banIp($ip, $ip_end = '', $expiry_date = NULL) {
  if (!empty($ip_end)) {
    $ip = sprintf("%u", ip2long($ip));
    $ip_end = sprintf("%u", ip2long($ip_end));
    $fields = [
      'ip' => $ip,
      'ip_end' => $ip_end,
    ];
  }
  else {
    $fields = [
      'ip' => $ip,
    ];
  }

  // Set expiry date using defaut expiry durations.
  if (!$expiry_date) {
    $expiry_duration = $this->config
      ->get('advban.settings')
      ->get('default_expiry_duration');
    if ($expiry_duration && $expiry_duration != AdvbanHelper::ADVBAN_NEVER) {
      $expiry_date = strtotime($expiry_duration);
      if (!$expiry_date) {
        $this
          ->messenger()
          ->addMessage($this
          ->t('Wrong expiry date for duration %expiry_duration', [
          '%expiry_duration' => $expiry_duration,
        ]), 'error');
        return;
      }
    }
  }
  $fields['expiry_date'] = $expiry_date ?: 0;
  $this->connection
    ->merge('advban_ip')
    ->key([
    'ip' => $ip,
  ])
    ->fields($fields)
    ->execute();
}