public function AdvbanIpManager::isBanned in Advanced ban 8
Returns if this IP address is banned.
Parameters
string $ip: The IP address to check.
array $options: Options array.
Return value
bool|array TRUE if the IP address is banned, FALSE otherwise or result info array.
Overrides AdvbanIpManagerInterface::isBanned
File
- src/
AdvbanIpManager.php, line 68
Class
- AdvbanIpManager
- Ban IP manager.
Namespace
Drupal\advbanCode
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;
}
}