public function AutobanController::getBannedIp in Automatic IP ban (Autoban) 8
Retrieve IP addresses for autoban rule.
Parameters
string $rule: Autoban rule ID.
array $params: Params (type, message) for special query.
Return value
array IP addresses as query result.
File
- src/
Controller/ AutobanController.php, line 29
Class
- AutobanController
- Provides an Autoban functional.
Namespace
Drupal\autoban\ControllerCode
public function getBannedIp($rule, array $params = []) {
$query_mode = $this
->config('autoban.settings')
->get('autoban_query_mode');
$use_wildcards = $this
->config('autoban.settings')
->get('autoban_use_wildcards') ?: FALSE;
$regexp_query_mode = $query_mode == 'regexp';
$from_analyze = AutobanUtils::isFromAnalyze($rule) && !empty($params);
if ($from_analyze) {
$entity = NULL;
$message = Html::decodeEntities(trim($params['message']));
$type = trim($params['type']);
$threshold = 1;
$referer = NULL;
$user_type = AutobanUtils::AUTOBAN_USER_ANY;
}
else {
$entity = Autoban::load($rule);
$message = trim($entity->message);
$type = trim($entity->type);
$threshold = (int) $entity->threshold;
$referer = trim($entity->referer);
$user_type = (int) $entity->user_type;
}
$connection = Database::getConnection();
$query = $connection
->select('watchdog', 'log');
$query
->fields('log', [
'hostname',
]);
$group = $query
->orConditionGroup();
// Checking for multiple messages divided by separator.
$message_items = explode('|', $message);
if (count($message_items) > 1) {
foreach ($message_items as $message_item) {
if ($from_analyze) {
$group
->condition('log.message', trim($message_item))
->condition('log.variables', trim($message_item));
}
else {
if ($regexp_query_mode) {
$group
->condition('log.message', trim($message_item), 'REGEXP')
->condition('log.variables', trim($message_item), 'REGEXP');
}
else {
if (!$use_wildcards) {
$group
->condition('log.message', '%' . $query
->escapeLike(trim($message_item)) . '%', 'LIKE')
->condition('log.variables', '%' . $query
->escapeLike(trim($message_item)) . '%', 'LIKE');
}
else {
$group
->condition('log.message', trim($message_item), 'LIKE')
->condition('log.variables', trim($message_item), 'LIKE');
}
}
}
}
}
else {
if ($from_analyze) {
$group
->condition('log.message', $message)
->condition('log.variables', $message);
}
else {
if ($regexp_query_mode) {
$group
->condition('log.message', $message, 'REGEXP')
->condition('log.variables', $message, 'REGEXP');
}
else {
if (!$use_wildcards) {
$group
->condition('log.message', '%' . $query
->escapeLike($message) . '%', 'LIKE')
->condition('log.variables', '%' . $query
->escapeLike($message) . '%', 'LIKE');
}
else {
$group
->condition('log.message', $message, 'LIKE')
->condition('log.variables', $message, 'LIKE');
}
}
}
}
$query
->condition('log.type', $type)
->condition($group);
if (!empty($referer)) {
$query
->condition('log.referer', '%' . $query
->escapeLike($referer) . '%', 'LIKE');
}
if ($user_type > 0) {
switch ($user_type) {
case AutobanUtils::AUTOBAN_USER_ANONYMOUS:
// Anonymous.
$query
->condition('log.uid', 0);
break;
case AutobanUtils::AUTOBAN_USER_AUTHENTICATED:
// Authenticated.
$query
->condition('log.uid', 0, '>');
break;
}
}
$query
->groupBy('log.hostname');
$query
->addExpression('COUNT(log.hostname)', 'hcount');
$query
->having('COUNT(log.hostname) >= :cnt', [
':cnt' => $threshold,
]);
$result = $query
->execute()
->fetchAll();
return $result;
}