You are here

class AdvbanIpManager in Advanced ban 8

Ban IP manager.

Hierarchy

Expanded class hierarchy of AdvbanIpManager

1 file declares its use of AdvbanIpManager
AdvbanSearchForm.php in src/Form/AdvbanSearchForm.php
1 string reference to 'AdvbanIpManager'
advban.services.yml in ./advban.services.yml
advban.services.yml
1 service uses AdvbanIpManager
advban.ip_manager in ./advban.services.yml
Drupal\advban\AdvbanIpManager

File

src/AdvbanIpManager.php, line 16

Namespace

Drupal\advban
View source
class AdvbanIpManager extends ControllerBase implements AdvbanIpManagerInterface {

  /**
   * The database connection used to check the IP against.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * The configuration factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactory
   */
  protected $config;

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatter
   */
  protected $dateFormatter;

  /**
   * The time service.
   *
   * @var \Drupal\Component\Datetime\Time
   */
  protected $time;

  /**
   * Construct the AdvbanIpManager.
   *
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection which will be used to check the IP against.
   * @param \Drupal\Core\Config\ConfigFactory $config
   *   The configuration factory service.
   * @param \Drupal\Core\Datetime\DateFormatter $dateFormatter
   *   The date formatter service.
   * @param \Drupal\Component\Datetime\Time $time
   *   The time service.
   */
  public function __construct(Connection $connection, ConfigFactory $config, DateFormatter $dateFormatter, Time $time) {
    $this->connection = $connection;
    $this->config = $config;
    $this->dateFormatter = $dateFormatter;
    $this->time = $time;
  }

  /**
   * {@inheritdoc}
   */
  public function isBanned($ip, array $options = []) {

    // Merge in defaults.
    $options += [
      'expiry_check' => TRUE,
      'info_output' => FALSE,
      'no_limit' => FALSE,
    ];

    // Collect result info.
    if ($options['info_output']) {
      $result_info = [
        'iid' => '',
        'expiry_date' => '',
      ];
    }
    if (!$options['expiry_check']) {
      $ban_info = $this->connection
        ->query("SELECT iid FROM {advban_ip} WHERE ip = :ip", [
        ':ip' => $ip,
      ])
        ->fetchField();
      $is_banned = (bool) $ban_info;
      if ($is_banned && $options['info_output']) {
        $result_info['iid'] = $ban_info;
      }
    }
    else {
      $ban_info = $this->connection
        ->query("SELECT iid, expiry_date FROM {advban_ip} WHERE ip = :ip", [
        ':ip' => $ip,
      ])
        ->fetchAll();
      $is_banned = count($ban_info) && (empty($ban_info[0]->expiry_date) || $ban_info[0]->expiry_date > $this->time
        ->getRequestTime());
      if ($is_banned && $options['info_output']) {
        $result_info['iid'] = $ban_info[0]->iid;
        $result_info['expiry_date'] = $ban_info[0]->expiry_date;
      }
    }
    if (!$is_banned) {

      // Check for a range ban.
      $ip_long = ip2long($ip);
      if ($ip_long) {
        $limit = $options['no_limit'] ? NULL : 1;
        if (!$options['expiry_check']) {
          if (!$limit) {
            $ban_info = $this->connection
              ->query("SELECT iid FROM {advban_ip} WHERE ip_end <> '' AND ip <= {$ip_long} AND ip_end >= {$ip_long}")
              ->fetchAll();
            $is_banned = count($ban_info);
            if ($is_banned && $options['info_output']) {
              $result_info['iid'] = [];
              foreach ($ban_info as $item) {
                $result_info['iid'][] = $item->iid;
              }
            }
          }
          else {
            $ban_info = $this->connection
              ->queryRange("SELECT iid FROM {advban_ip} WHERE ip_end <> '' AND ip <= {$ip_long} AND ip_end >= {$ip_long}", 0, $limit)
              ->fetchField();
            $is_banned = (bool) $ban_info;
            if ($is_banned && $options['info_output']) {
              $result_info['iid'] = $ban_info;
            }
          }
        }
        else {
          if ($limit) {
            $query = $this->connection
              ->queryRange("SELECT iid, expiry_date FROM {advban_ip} WHERE ip_end <> '' AND ip <= {$ip_long} AND ip_end >= {$ip_long}", 0, $limit);
          }
          else {
            $query = $this->connection
              ->query("SELECT iid, expiry_date FROM {advban_ip} WHERE ip_end <> '' AND ip <= {$ip_long} AND ip_end >= {$ip_long}");
          }
          $ban_info = $query
            ->fetchAll();
          $is_banned = count($ban_info) && (empty($ban_info[0]->expiry_date) || $ban_info[0]->expiry_date > $this->time
            ->getRequestTime());
          if ($is_banned && $options['info_output']) {
            $result_info['iid'] = [];
            foreach ($ban_info as $item) {
              $result_info['iid'][] = $item->iid;
            }
            $result_info['expiry_date'] = $ban_info[0]->expiry_date;
          }
        }
      }
    }
    if ($options['info_output']) {
      $result_info['is_banned'] = $is_banned;
      return $result_info;
    }
    else {
      return $is_banned;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function findAll() {
    return $this->connection
      ->query('SELECT * FROM {advban_ip}');
  }

  /**
   * {@inheritdoc}
   */
  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();
  }

  /**
   * {@inheritdoc}
   */
  public function unbanIp($ip, $ip_end = '') {
    $query = $this->connection
      ->delete('advban_ip');
    if (!empty($ip_end)) {
      $query
        ->condition('ip', $ip);
      $query
        ->condition('ip_end', $ip_end);
    }
    else {
      $query
        ->condition('ip', $ip);
    }
    $query
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function unbanIpAll(array $params = []) {
    $query = $this->connection
      ->delete('advban_ip');
    if (!empty($params)) {

      // Range parameters.
      if (!empty($params['range']) && $params['range'] != 'all') {
        switch ($params['range']) {
          case 'simple':
            $query
              ->condition('ip_end', '');
            break;
          case 'range':
            $query
              ->condition('ip_end', '', '<>');
            break;
        }
      }

      // Expire parameters.
      if (!empty($params['expire']) && $params['expire'] != 'all') {
        switch ($params['expire']) {
          case 'expired':
            $query
              ->condition('expiry_date', 0, '<>');
            break;
          case 'not_expired':
            $query
              ->condition('expiry_date', 0);
            break;
        }
      }
    }
    return $query
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function findById($ban_id) {
    return $this->connection
      ->query("SELECT * FROM {advban_ip} WHERE iid = :iid", [
      ':iid' => $ban_id,
    ])
      ->fetchAll();
  }

  /**
   * {@inheritdoc}
   */
  public function formatIp($ip, $ip_end = '') {
    if (!empty($ip_end)) {
      if (is_numeric($ip)) {
        $ip = long2ip($ip);
      }
      if (is_numeric($ip_end)) {
        $ip_end = long2ip($ip_end);
      }
      $format_text = $this->config
        ->get('advban.settings')
        ->get('range_ip_format') ?: '@ip_start ... @ip_end';
      $text = new FormattableMarkup($format_text, [
        '@ip_start' => $ip,
        '@ip_end' => $ip_end,
      ]);
      return $text;
    }
    else {
      return $ip;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function expiryDurations($index = NULL) {
    $expiry_durations = $this->config
      ->get('advban.settings')
      ->get('expiry_durations');
    if (empty($expiry_durations)) {
      $expiry_durations = "+1 hour\n+1 day\n+1 week\n+1 month\n+1 year";
      $this->config
        ->getEditable('advban.settings')
        ->set('expiry_durations', $expiry_durations)
        ->save();
    }
    $list = explode("\n", $expiry_durations);
    return $index != NULL ? $list[$index] : $list;
  }

  /**
   * {@inheritdoc}
   */
  public function expiryDurationIndex(array $expiry_durations, $default_expiry_duration) {
    if (!$default_expiry_duration || $default_expiry_duration == AdvbanHelper::ADVBAN_NEVER) {
      $expiry_durations_index = AdvbanHelper::ADVBAN_NEVER;
    }
    else {
      $expiry_durations_index = array_search($default_expiry_duration, $expiry_durations);
      if ($expiry_durations_index === FALSE) {
        $expiry_durations_index = AdvbanHelper::ADVBAN_NEVER;
      }
    }
    return $expiry_durations_index;
  }

  /**
   * {@inheritdoc}
   */
  public function unblockExpiredIp() {
    $query = $this->connection
      ->delete('advban_ip');
    $query
      ->condition('expiry_date', 0, '>');
    $query
      ->condition('expiry_date', strtotime('now'), '<');
    return $query
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function banText(array $variables) {
    $ban_text = $this->config
      ->get('advban.settings')
      ->get('advban_ban_text') ?: '@ip has been banned';
    $ban_text_params = [
      '@ip' => $variables['ip'],
    ];
    $expiry_date = $variables['expiry_date'];
    if (!empty($expiry_date)) {
      $ban_text = $this->config
        ->get('advban.settings')
        ->get('advban_ban_expire_text') ?: '@ip has been banned up to @expiry_date';
      $ban_text_params['@expiry_date'] = $this->dateFormatter
        ->format($expiry_date);
    }
    return new FormattableMarkup($ban_text, $ban_text_params);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AdvbanIpManager::$config protected property The configuration factory service.
AdvbanIpManager::$connection protected property The database connection used to check the IP against.
AdvbanIpManager::$dateFormatter protected property The date formatter service.
AdvbanIpManager::$time protected property The time service.
AdvbanIpManager::banIp public function Bans an IP address. Overrides AdvbanIpManagerInterface::banIp
AdvbanIpManager::banText public function Create formatted ban text. Overrides AdvbanIpManagerInterface::banText
AdvbanIpManager::expiryDurationIndex public function Get default expiry duration index. Overrides AdvbanIpManagerInterface::expiryDurationIndex
AdvbanIpManager::expiryDurations public function Get expiry durations list or item. Overrides AdvbanIpManagerInterface::expiryDurations
AdvbanIpManager::findAll public function Finds all banned IP addresses. Overrides AdvbanIpManagerInterface::findAll
AdvbanIpManager::findById public function Finds a banned IP address by its ID. Overrides AdvbanIpManagerInterface::findById
AdvbanIpManager::formatIp public function Format of the IP record (individual or range). Overrides AdvbanIpManagerInterface::formatIp
AdvbanIpManager::isBanned public function Returns if this IP address is banned. Overrides AdvbanIpManagerInterface::isBanned
AdvbanIpManager::unbanIp public function Unbans an IP address. Overrides AdvbanIpManagerInterface::unbanIp
AdvbanIpManager::unbanIpAll public function Unbans all IP addresses. Overrides AdvbanIpManagerInterface::unbanIpAll
AdvbanIpManager::unblockExpiredIp public function Unblock expired banned IP. Overrides AdvbanIpManagerInterface::unblockExpiredIp
AdvbanIpManager::__construct public function Construct the AdvbanIpManager.
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 40
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.