class AdvbanIpManager in Advanced ban 8
Ban IP manager.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\advban\AdvbanIpManager implements AdvbanIpManagerInterface
Expanded class hierarchy of AdvbanIpManager
1 file declares its use of AdvbanIpManager
- AdvbanSearchForm.php in src/
Form/ AdvbanSearchForm.php
1 string reference to 'AdvbanIpManager'
1 service uses AdvbanIpManager
File
- src/
AdvbanIpManager.php, line 16
Namespace
Drupal\advbanView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AdvbanIpManager:: |
protected | property | The configuration factory service. | |
AdvbanIpManager:: |
protected | property | The database connection used to check the IP against. | |
AdvbanIpManager:: |
protected | property | The date formatter service. | |
AdvbanIpManager:: |
protected | property | The time service. | |
AdvbanIpManager:: |
public | function |
Bans an IP address. Overrides AdvbanIpManagerInterface:: |
|
AdvbanIpManager:: |
public | function |
Create formatted ban text. Overrides AdvbanIpManagerInterface:: |
|
AdvbanIpManager:: |
public | function |
Get default expiry duration index. Overrides AdvbanIpManagerInterface:: |
|
AdvbanIpManager:: |
public | function |
Get expiry durations list or item. Overrides AdvbanIpManagerInterface:: |
|
AdvbanIpManager:: |
public | function |
Finds all banned IP addresses. Overrides AdvbanIpManagerInterface:: |
|
AdvbanIpManager:: |
public | function |
Finds a banned IP address by its ID. Overrides AdvbanIpManagerInterface:: |
|
AdvbanIpManager:: |
public | function |
Format of the IP record (individual or range). Overrides AdvbanIpManagerInterface:: |
|
AdvbanIpManager:: |
public | function |
Returns if this IP address is banned. Overrides AdvbanIpManagerInterface:: |
|
AdvbanIpManager:: |
public | function |
Unbans an IP address. Overrides AdvbanIpManagerInterface:: |
|
AdvbanIpManager:: |
public | function |
Unbans all IP addresses. Overrides AdvbanIpManagerInterface:: |
|
AdvbanIpManager:: |
public | function |
Unblock expired banned IP. Overrides AdvbanIpManagerInterface:: |
|
AdvbanIpManager:: |
public | function | Construct the AdvbanIpManager. | |
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity manager. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
40 |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity manager service. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
ControllerBase:: |
protected | function | Returns the state storage service. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |