2020-01-26 13:19:35 +00:00
|
|
|
<?php declare(strict_types=1);
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2019-12-26 00:36:32 +00:00
|
|
|
use MicroCRUD\ActionColumn;
|
2019-11-27 21:06:14 +00:00
|
|
|
use MicroCRUD\InetColumn;
|
2019-11-27 12:13:04 +00:00
|
|
|
use MicroCRUD\StringColumn;
|
|
|
|
use MicroCRUD\DateColumn;
|
|
|
|
use MicroCRUD\TextColumn;
|
|
|
|
use MicroCRUD\EnumColumn;
|
|
|
|
use MicroCRUD\Table;
|
2019-11-24 15:59:14 +00:00
|
|
|
|
2019-11-27 12:13:04 +00:00
|
|
|
class IPBanTable extends Table
|
|
|
|
{
|
2019-11-29 02:06:22 +00:00
|
|
|
public function __construct(\FFSPHP\PDO $db)
|
2019-11-27 12:13:04 +00:00
|
|
|
{
|
2019-11-29 02:06:22 +00:00
|
|
|
parent::__construct($db);
|
2019-11-24 15:59:14 +00:00
|
|
|
$this->table = "bans";
|
|
|
|
$this->base_query = "
|
2019-11-27 21:06:14 +00:00
|
|
|
SELECT * FROM (
|
|
|
|
SELECT bans.*, users.name AS banner
|
|
|
|
FROM bans JOIN users ON banner_id=users.id
|
|
|
|
) AS tbl1
|
2019-11-24 15:59:14 +00:00
|
|
|
";
|
2019-11-28 21:32:18 +00:00
|
|
|
$this->size = 100;
|
|
|
|
$this->limit = 1000000;
|
2019-12-26 00:36:32 +00:00
|
|
|
$this->set_columns([
|
2019-11-27 21:06:14 +00:00
|
|
|
new InetColumn("ip", "IP"),
|
2019-11-28 21:32:18 +00:00
|
|
|
new EnumColumn("mode", "Mode", [
|
|
|
|
"Block"=>"block",
|
|
|
|
"Firewall"=>"firewall",
|
|
|
|
"Ghost"=>"ghost",
|
|
|
|
"Anon Ghost"=>"anon-ghost"
|
|
|
|
]),
|
2019-11-24 15:59:14 +00:00
|
|
|
new TextColumn("reason", "Reason"),
|
|
|
|
new StringColumn("banner", "Banner"),
|
|
|
|
new DateColumn("added", "Added"),
|
|
|
|
new DateColumn("expires", "Expires"),
|
2019-12-26 00:36:32 +00:00
|
|
|
new ActionColumn("id"),
|
|
|
|
]);
|
2019-11-24 15:59:14 +00:00
|
|
|
$this->order_by = ["expires", "id"];
|
|
|
|
$this->flags = [
|
|
|
|
"all" => ["((expires > CURRENT_TIMESTAMP) OR (expires IS NULL))", null],
|
|
|
|
];
|
2019-11-27 21:06:14 +00:00
|
|
|
$this->create_url = make_link("ip_ban/create");
|
|
|
|
$this->delete_url = make_link("ip_ban/delete");
|
2019-11-28 21:32:18 +00:00
|
|
|
$this->table_attrs = ["class" => "zebra"];
|
2019-11-24 15:59:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class RemoveIPBanEvent extends Event
|
|
|
|
{
|
|
|
|
public $id;
|
|
|
|
|
|
|
|
public function __construct(int $id)
|
|
|
|
{
|
2020-01-26 13:19:35 +00:00
|
|
|
parent::__construct();
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->id = $id;
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2019-11-03 16:22:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class AddIPBanEvent extends Event
|
|
|
|
{
|
|
|
|
public $ip;
|
2019-11-28 14:57:56 +00:00
|
|
|
public $mode;
|
2019-05-28 16:59:38 +00:00
|
|
|
public $reason;
|
2019-11-05 00:16:26 +00:00
|
|
|
public $expires;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2019-11-28 14:57:56 +00:00
|
|
|
public function __construct(string $ip, string $mode, string $reason, ?string $expires)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-01-26 13:19:35 +00:00
|
|
|
parent::__construct();
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->ip = trim($ip);
|
2019-11-28 14:57:56 +00:00
|
|
|
$this->mode = $mode;
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->reason = trim($reason);
|
2019-11-05 00:16:26 +00:00
|
|
|
$this->expires = $expires;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class IPBan extends Extension
|
|
|
|
{
|
2020-01-26 13:19:35 +00:00
|
|
|
/** @var IPBanTheme */
|
|
|
|
protected $theme;
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function get_priority(): int
|
|
|
|
{
|
|
|
|
return 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onInitExt(InitExtEvent $event)
|
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
$config->set_default_string(
|
|
|
|
"ipban_message",
|
|
|
|
'<p>IP <b>$IP</b> has been banned until <b>$DATE</b> by <b>$ADMIN</b> because of <b>$REASON</b>
|
2019-02-22 21:24:53 +00:00
|
|
|
<p>If you couldn\'t possibly be guilty of what you\'re banned for, the person we banned probably had a dynamic IP address and so do you.
|
|
|
|
<p>See <a href="http://whatismyipaddress.com/dynamic-static">http://whatismyipaddress.com/dynamic-static</a> for more information.
|
2019-05-28 16:59:38 +00:00
|
|
|
<p>$CONTACT'
|
|
|
|
);
|
2019-11-28 18:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function onUserLogin(UserLoginEvent $event)
|
|
|
|
{
|
2020-01-26 13:19:35 +00:00
|
|
|
global $cache, $config, $database, $page, $_shm_user_classes;
|
2019-11-28 18:01:21 +00:00
|
|
|
|
2019-11-28 21:32:18 +00:00
|
|
|
// Get lists of banned IPs and banned networks
|
2019-11-28 18:01:21 +00:00
|
|
|
$ips = $cache->get("ip_bans");
|
|
|
|
$networks = $cache->get("network_bans");
|
|
|
|
if ($ips === false || $networks === false) {
|
2019-11-28 21:32:18 +00:00
|
|
|
$rows = $database->get_pairs("
|
2019-11-28 18:01:21 +00:00
|
|
|
SELECT ip, id
|
|
|
|
FROM bans
|
|
|
|
WHERE ((expires > CURRENT_TIMESTAMP) OR (expires IS NULL))
|
|
|
|
");
|
|
|
|
|
2019-11-28 21:32:18 +00:00
|
|
|
$ips = []; # "0.0.0.0" => 123;
|
|
|
|
$networks = []; # "0.0.0.0/32" => 456;
|
|
|
|
foreach ($rows as $ip => $id) {
|
2020-10-25 19:31:58 +00:00
|
|
|
if (str_contains($ip, '/')) {
|
2019-11-28 21:32:18 +00:00
|
|
|
$networks[$ip] = $id;
|
|
|
|
} else {
|
|
|
|
$ips[$ip] = $id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 23:40:14 +00:00
|
|
|
$cache->set("ip_bans", $ips, 60);
|
|
|
|
$cache->set("network_bans", $networks, 60);
|
2019-11-28 18:01:21 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 21:32:18 +00:00
|
|
|
// Check if our current IP is in either of the ban lists
|
2020-03-21 22:47:34 +00:00
|
|
|
$active_ban_id = (
|
2020-03-22 14:01:27 +00:00
|
|
|
$this->find_active_ban($ips, $_SERVER['REMOTE_ADDR'], $networks) ??
|
2020-03-21 22:47:34 +00:00
|
|
|
$this->find_active_ban($ips, @$_SERVER['HTTP_X_FORWARDED_FOR'], $networks)
|
|
|
|
);
|
2019-11-28 21:32:18 +00:00
|
|
|
|
|
|
|
// If an active ban is found, act on it
|
|
|
|
if (!is_null($active_ban_id)) {
|
|
|
|
$row = $database->get_row("SELECT * FROM bans WHERE id=:id", ["id"=>$active_ban_id]);
|
2019-11-28 23:40:14 +00:00
|
|
|
if (empty($row)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-28 21:32:18 +00:00
|
|
|
|
2019-11-28 23:40:14 +00:00
|
|
|
$msg = $config->get_string("ipban_message_{$row['mode']}") ?? $config->get_string("ipban_message");
|
2019-11-28 21:32:18 +00:00
|
|
|
$msg = str_replace('$IP', $row["ip"], $msg);
|
2019-11-28 23:40:14 +00:00
|
|
|
$msg = str_replace('$DATE', $row['expires'] ?? 'the end of time', $msg);
|
2019-11-28 21:32:18 +00:00
|
|
|
$msg = str_replace('$ADMIN', User::by_id($row['banner_id'])->name, $msg);
|
|
|
|
$msg = str_replace('$REASON', $row['reason'], $msg);
|
|
|
|
$contact_link = contact_link();
|
|
|
|
if (!empty($contact_link)) {
|
|
|
|
$msg = str_replace('$CONTACT', "<a href='$contact_link'>Contact the staff (be sure to include this message)</a>", $msg);
|
|
|
|
} else {
|
|
|
|
$msg = str_replace('$CONTACT', "", $msg);
|
|
|
|
}
|
2019-11-28 23:40:14 +00:00
|
|
|
$msg .= "<!-- $active_ban_id / {$row["mode"]} -->";
|
2019-11-28 21:32:18 +00:00
|
|
|
|
|
|
|
if ($row["mode"] == "ghost") {
|
|
|
|
$b = new Block(null, $msg, "main", 0);
|
|
|
|
$b->is_content = false;
|
|
|
|
$page->add_block($b);
|
2019-11-28 21:46:34 +00:00
|
|
|
$page->add_cookie("nocache", "Ghost Banned", time()+60*60*2, "/");
|
2019-11-28 21:32:18 +00:00
|
|
|
$event->user->class = $_shm_user_classes["ghost"];
|
|
|
|
} elseif ($row["mode"] == "anon-ghost") {
|
|
|
|
if ($event->user->is_anonymous()) {
|
|
|
|
$b = new Block(null, $msg, "main", 0);
|
|
|
|
$b->is_content = false;
|
|
|
|
$page->add_block($b);
|
2019-11-28 21:46:34 +00:00
|
|
|
$page->add_cookie("nocache", "Ghost Banned", time()+60*60*2, "/");
|
2019-11-28 21:32:18 +00:00
|
|
|
$event->user->class = $_shm_user_classes["ghost"];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
header("HTTP/1.0 403 Forbidden");
|
|
|
|
print "$msg";
|
|
|
|
exit;
|
|
|
|
}
|
2019-11-28 18:01:21 +00:00
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function onPageRequest(PageRequestEvent $event)
|
|
|
|
{
|
|
|
|
if ($event->page_matches("ip_ban")) {
|
2019-11-28 21:32:18 +00:00
|
|
|
global $database, $page, $user;
|
2019-07-09 14:10:21 +00:00
|
|
|
if ($user->can(Permissions::BAN_IP)) {
|
2019-11-27 21:06:14 +00:00
|
|
|
if ($event->get_arg(0) == "create") {
|
2019-11-28 21:32:18 +00:00
|
|
|
$user->ensure_authed();
|
|
|
|
$input = validate_input(["c_ip"=>"string", "c_mode"=>"string", "c_reason"=>"string", "c_expires"=>"optional,date"]);
|
|
|
|
send_event(new AddIPBanEvent($input['c_ip'], $input['c_mode'], $input['c_reason'], $input['c_expires']));
|
2019-12-15 19:47:18 +00:00
|
|
|
$page->flash("Ban for {$input['c_ip']} added");
|
2019-11-28 21:32:18 +00:00
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
|
|
|
$page->set_redirect(make_link("ip_ban/list"));
|
2019-11-27 21:06:14 +00:00
|
|
|
} elseif ($event->get_arg(0) == "delete") {
|
2019-11-28 21:32:18 +00:00
|
|
|
$user->ensure_authed();
|
|
|
|
$input = validate_input(["d_id"=>"int"]);
|
|
|
|
send_event(new RemoveIPBanEvent($input['d_id']));
|
2019-12-15 19:47:18 +00:00
|
|
|
$page->flash("Ban removed");
|
2019-11-28 21:32:18 +00:00
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
|
|
|
$page->set_redirect(make_link("ip_ban/list"));
|
2019-05-28 16:59:38 +00:00
|
|
|
} elseif ($event->get_arg(0) == "list") {
|
2019-11-28 21:32:18 +00:00
|
|
|
$_GET['c_banner'] = $user->name;
|
|
|
|
$_GET['c_added'] = date('Y-m-d');
|
2019-11-28 15:27:36 +00:00
|
|
|
$t = new IPBanTable($database->raw_db());
|
|
|
|
$t->token = $user->get_auth_token();
|
|
|
|
$t->inputs = $_GET;
|
2019-11-29 01:52:24 +00:00
|
|
|
$this->theme->display_bans($page, $t->table($t->query()), $t->paginator());
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->theme->display_permission_denied();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onSetupBuilding(SetupBuildingEvent $event)
|
|
|
|
{
|
2019-11-28 23:40:14 +00:00
|
|
|
global $config;
|
|
|
|
|
2020-10-26 15:13:28 +00:00
|
|
|
$sb = $event->panel->create_new_block("IP Ban");
|
2019-05-28 16:59:38 +00:00
|
|
|
$sb->add_longtext_option("ipban_message", 'Message to show to banned users:<br>(with $IP, $DATE, $ADMIN, $REASON, and $CONTACT)');
|
2019-11-28 23:40:14 +00:00
|
|
|
if ($config->get_string("ipban_message_ghost")) {
|
|
|
|
$sb->add_longtext_option("ipban_message_ghost", 'Message to show to ghost users:');
|
|
|
|
}
|
|
|
|
if ($config->get_string("ipban_message_anon-ghost")) {
|
|
|
|
$sb->add_longtext_option("ipban_message_anon-ghost", 'Message to show to ghost anons:');
|
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 19:54:48 +00:00
|
|
|
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event)
|
|
|
|
{
|
|
|
|
global $user;
|
2019-09-29 13:30:55 +00:00
|
|
|
if ($event->parent==="system") {
|
2019-08-02 19:54:48 +00:00
|
|
|
if ($user->can(Permissions::BAN_IP)) {
|
2019-11-24 13:24:42 +00:00
|
|
|
$event->add_nav_link("ip_bans", new Link('ip_ban/list'), "IP Bans", NavLink::is_active(["ip_ban"]));
|
2019-08-02 19:54:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function onUserBlockBuilding(UserBlockBuildingEvent $event)
|
|
|
|
{
|
|
|
|
global $user;
|
2019-07-09 14:10:21 +00:00
|
|
|
if ($user->can(Permissions::BAN_IP)) {
|
2019-11-24 13:24:42 +00:00
|
|
|
$event->add_link("IP Bans", make_link("ip_ban/list"));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onAddIPBan(AddIPBanEvent $event)
|
|
|
|
{
|
2019-10-02 09:49:32 +00:00
|
|
|
global $cache, $user, $database;
|
2019-11-28 14:57:56 +00:00
|
|
|
$sql = "INSERT INTO bans (ip, mode, reason, expires, banner_id) VALUES (:ip, :mode, :reason, :expires, :admin_id)";
|
2020-01-28 21:19:59 +00:00
|
|
|
$database->execute($sql, ["ip"=>$event->ip, "mode"=>$event->mode, "reason"=>$event->reason, "expires"=>$event->expires, "admin_id"=>$user->id]);
|
2019-11-28 23:40:14 +00:00
|
|
|
$cache->delete("ip_bans");
|
|
|
|
$cache->delete("network_bans");
|
2019-12-15 16:21:48 +00:00
|
|
|
log_info("ipban", "Banned ({$event->mode}) {$event->ip} because '{$event->reason}' until {$event->expires}");
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function onRemoveIPBan(RemoveIPBanEvent $event)
|
|
|
|
{
|
2019-10-02 09:49:32 +00:00
|
|
|
global $cache, $database;
|
2019-05-28 16:59:38 +00:00
|
|
|
$ban = $database->get_row("SELECT * FROM bans WHERE id = :id", ["id"=>$event->id]);
|
|
|
|
if ($ban) {
|
2020-10-25 21:34:52 +00:00
|
|
|
$database->execute("DELETE FROM bans WHERE id = :id", ["id"=>$event->id]);
|
2019-11-28 23:40:14 +00:00
|
|
|
$cache->delete("ip_bans");
|
|
|
|
$cache->delete("network_bans");
|
2019-05-28 16:59:38 +00:00
|
|
|
log_info("ipban", "Removed {$ban['ip']}'s ban");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-03 17:19:37 +00:00
|
|
|
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-01-26 13:19:35 +00:00
|
|
|
global $database;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
// shortcut to latest
|
2019-11-03 19:43:39 +00:00
|
|
|
if ($this->get_version("ext_ipban_version") < 1) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$database->create_table("bans", "
|
2009-01-22 12:05:55 +00:00
|
|
|
id SCORE_AIPK,
|
|
|
|
banner_id INTEGER NOT NULL,
|
2020-01-28 21:19:59 +00:00
|
|
|
mode VARCHAR(16) NOT NULL DEFAULT 'block',
|
2009-01-22 12:05:55 +00:00
|
|
|
ip SCORE_INET NOT NULL,
|
|
|
|
reason TEXT NOT NULL,
|
2019-11-03 18:28:05 +00:00
|
|
|
added TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
2019-11-03 23:17:09 +00:00
|
|
|
expires TIMESTAMP NULL DEFAULT NULL,
|
2015-08-08 16:21:37 +00:00
|
|
|
FOREIGN KEY (banner_id) REFERENCES users(id) ON DELETE CASCADE,
|
2008-05-18 02:17:16 +00:00
|
|
|
");
|
2019-11-03 23:17:09 +00:00
|
|
|
$database->execute("CREATE INDEX bans__expires ON bans(expires)");
|
|
|
|
$this->set_version("ext_ipban_version", 10);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2008-05-18 02:17:16 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// ===
|
2008-05-18 02:17:16 +00:00
|
|
|
|
2019-11-03 19:43:39 +00:00
|
|
|
if ($this->get_version("ext_ipban_version") < 1) {
|
2020-10-25 21:34:52 +00:00
|
|
|
$database->execute("CREATE TABLE bans (
|
2008-05-18 02:17:16 +00:00
|
|
|
id int(11) NOT NULL auto_increment,
|
|
|
|
ip char(15) default NULL,
|
2019-11-03 18:28:05 +00:00
|
|
|
date TIMESTAMP default NULL,
|
|
|
|
end TIMESTAMP default NULL,
|
2008-05-18 02:17:16 +00:00
|
|
|
reason varchar(255) default NULL,
|
|
|
|
PRIMARY KEY (id)
|
|
|
|
)");
|
2019-11-03 19:43:39 +00:00
|
|
|
$this->set_version("ext_ipban_version", 1);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 19:43:39 +00:00
|
|
|
if ($this->get_version("ext_ipban_version") == 1) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$database->execute("ALTER TABLE bans ADD COLUMN banner_id INTEGER NOT NULL AFTER id");
|
2019-11-03 19:43:39 +00:00
|
|
|
$this->set_version("ext_ipban_version", 2);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 19:43:39 +00:00
|
|
|
if ($this->get_version("ext_ipban_version") == 2) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$database->execute("ALTER TABLE bans DROP COLUMN date");
|
|
|
|
$database->execute("ALTER TABLE bans CHANGE ip ip CHAR(20) NOT NULL");
|
|
|
|
$database->execute("ALTER TABLE bans CHANGE reason reason TEXT NOT NULL");
|
|
|
|
$database->execute("CREATE INDEX bans__end ON bans(end)");
|
2019-11-03 19:43:39 +00:00
|
|
|
$this->set_version("ext_ipban_version", 3);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 19:43:39 +00:00
|
|
|
if ($this->get_version("ext_ipban_version") == 3) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$database->execute("ALTER TABLE bans CHANGE end old_end DATE NOT NULL");
|
|
|
|
$database->execute("ALTER TABLE bans ADD COLUMN end INTEGER");
|
|
|
|
$database->execute("UPDATE bans SET end = UNIX_TIMESTAMP(old_end)");
|
|
|
|
$database->execute("ALTER TABLE bans DROP COLUMN old_end");
|
|
|
|
$database->execute("CREATE INDEX bans__end ON bans(end)");
|
2019-11-03 19:43:39 +00:00
|
|
|
$this->set_version("ext_ipban_version", 4);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 19:43:39 +00:00
|
|
|
if ($this->get_version("ext_ipban_version") == 4) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$database->execute("ALTER TABLE bans CHANGE end end_timestamp INTEGER");
|
2019-11-03 19:43:39 +00:00
|
|
|
$this->set_version("ext_ipban_version", 5);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 19:43:39 +00:00
|
|
|
if ($this->get_version("ext_ipban_version") == 5) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$database->execute("ALTER TABLE bans CHANGE ip ip VARCHAR(15)");
|
2019-11-03 19:43:39 +00:00
|
|
|
$this->set_version("ext_ipban_version", 6);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 19:43:39 +00:00
|
|
|
if ($this->get_version("ext_ipban_version") == 6) {
|
2020-10-25 21:34:52 +00:00
|
|
|
$database->execute("ALTER TABLE bans ADD FOREIGN KEY (banner_id) REFERENCES users(id) ON DELETE CASCADE");
|
2019-11-03 19:43:39 +00:00
|
|
|
$this->set_version("ext_ipban_version", 7);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 19:43:39 +00:00
|
|
|
if ($this->get_version("ext_ipban_version") == 7) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$database->execute($database->scoreql_to_sql("ALTER TABLE bans CHANGE ip ip SCORE_INET"));
|
2019-11-03 23:17:09 +00:00
|
|
|
$database->execute("ALTER TABLE bans ADD COLUMN added TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP");
|
2019-11-03 19:43:39 +00:00
|
|
|
$this->set_version("ext_ipban_version", 8);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-11-03 17:53:52 +00:00
|
|
|
|
2019-11-03 19:43:39 +00:00
|
|
|
if ($this->get_version("ext_ipban_version") == 8) {
|
2019-11-03 23:17:09 +00:00
|
|
|
$database->execute("ALTER TABLE bans ADD COLUMN mode VARCHAR(16) NOT NULL DEFAULT 'block'");
|
2019-11-03 19:43:39 +00:00
|
|
|
$this->set_version("ext_ipban_version", 9);
|
2019-11-03 17:53:52 +00:00
|
|
|
}
|
2019-11-03 23:17:09 +00:00
|
|
|
|
|
|
|
if ($this->get_version("ext_ipban_version") == 9) {
|
2019-11-03 23:29:29 +00:00
|
|
|
$database->execute("ALTER TABLE bans ADD COLUMN expires TIMESTAMP DEFAULT NULL");
|
|
|
|
$database->execute("UPDATE bans SET expires = to_date('1970/01/01', 'YYYY/MM/DD') + (end_timestamp * interval '1 seconds')");
|
2019-11-03 23:17:09 +00:00
|
|
|
$database->execute("ALTER TABLE bans DROP COLUMN end_timestamp");
|
|
|
|
$database->execute("CREATE INDEX bans__expires ON bans(expires)");
|
|
|
|
$this->set_version("ext_ipban_version", 10);
|
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2020-03-21 22:47:34 +00:00
|
|
|
|
|
|
|
public function find_active_ban($ips, $remote, $networks)
|
|
|
|
{
|
2020-03-23 00:06:43 +00:00
|
|
|
if (!$remote) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-03-21 22:47:34 +00:00
|
|
|
$active_ban_id = null;
|
|
|
|
if (isset($ips[$remote])) {
|
|
|
|
$active_ban_id = $ips[$remote];
|
|
|
|
} else {
|
|
|
|
foreach ($networks as $range => $ban_id) {
|
|
|
|
if (ip_in_range($remote, $range)) {
|
|
|
|
$active_ban_id = $ban_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $active_ban_id;
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|