2007-04-16 11:58:25 +00:00
|
|
|
<?php
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class RemoveIPBanEvent extends Event
|
|
|
|
{
|
|
|
|
public $id;
|
|
|
|
|
|
|
|
public function __construct(int $id)
|
|
|
|
{
|
|
|
|
$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;
|
|
|
|
public $reason;
|
|
|
|
public $end;
|
|
|
|
|
|
|
|
public function __construct(string $ip, string $reason, string $end)
|
|
|
|
{
|
|
|
|
$this->ip = trim($ip);
|
|
|
|
$this->reason = trim($reason);
|
|
|
|
$this->end = trim($end);
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class IPBan extends Extension
|
|
|
|
{
|
|
|
|
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'
|
|
|
|
);
|
|
|
|
$this->check_ip_ban();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onPageRequest(PageRequestEvent $event)
|
|
|
|
{
|
|
|
|
if ($event->page_matches("ip_ban")) {
|
|
|
|
global $page, $user;
|
2019-07-09 14:10:21 +00:00
|
|
|
if ($user->can(Permissions::BAN_IP)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($event->get_arg(0) == "add" && $user->check_auth_token()) {
|
|
|
|
if (isset($_POST['ip']) && isset($_POST['reason']) && isset($_POST['end'])) {
|
|
|
|
if (empty($_POST['end'])) {
|
|
|
|
$end = null;
|
|
|
|
} else {
|
|
|
|
$end = $_POST['end'];
|
|
|
|
}
|
|
|
|
send_event(new AddIPBanEvent($_POST['ip'], $_POST['reason'], $end));
|
|
|
|
|
|
|
|
flash_message("Ban for {$_POST['ip']} added");
|
2019-06-19 01:58:28 +00:00
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
2019-05-28 16:59:38 +00:00
|
|
|
$page->set_redirect(make_link("ip_ban/list"));
|
|
|
|
}
|
|
|
|
} elseif ($event->get_arg(0) == "remove" && $user->check_auth_token()) {
|
|
|
|
if (isset($_POST['id'])) {
|
|
|
|
send_event(new RemoveIPBanEvent($_POST['id']));
|
|
|
|
|
|
|
|
flash_message("Ban removed");
|
2019-06-19 01:58:28 +00:00
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
2019-05-28 16:59:38 +00:00
|
|
|
$page->set_redirect(make_link("ip_ban/list"));
|
|
|
|
}
|
|
|
|
} elseif ($event->get_arg(0) == "list") {
|
|
|
|
$bans = (isset($_GET["all"])) ? $this->get_bans() : $this->get_active_bans();
|
|
|
|
$this->theme->display_bans($page, $bans);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->theme->display_permission_denied();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onSetupBuilding(SetupBuildingEvent $event)
|
|
|
|
{
|
|
|
|
$sb = new SetupBlock("IP Ban");
|
|
|
|
$sb->add_longtext_option("ipban_message", 'Message to show to banned users:<br>(with $IP, $DATE, $ADMIN, $REASON, and $CONTACT)');
|
|
|
|
$event->panel->add_block($sb);
|
|
|
|
}
|
|
|
|
|
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)) {
|
|
|
|
$event->add_nav_link("ip_bans", new Link('ip_ban/list'), "IP Bans", NavLink::is_active(["ip_ban"]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-05-28 16:59:38 +00:00
|
|
|
$event->add_link("IP Bans", make_link("ip_ban/list"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onAddIPBan(AddIPBanEvent $event)
|
|
|
|
{
|
2019-10-02 09:49:32 +00:00
|
|
|
global $cache, $user, $database;
|
2019-11-03 23:17:09 +00:00
|
|
|
$sql = "INSERT INTO bans (ip, reason, expires, banner_id) VALUES (:ip, :reason, :expires, :admin_id)";
|
|
|
|
$database->Execute($sql, ["ip"=>$event->ip, "reason"=>$event->reason, "expires"=>$event->end, "admin_id"=>$user->id]);
|
2019-10-02 09:49:32 +00:00
|
|
|
$cache->delete("ip_bans_sorted");
|
2019-05-28 16:59:38 +00:00
|
|
|
log_info("ipban", "Banned {$event->ip} because '{$event->reason}' until {$event->end}");
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
$database->Execute("DELETE FROM bans WHERE id = :id", ["id"=>$event->id]);
|
2019-10-02 09:49:32 +00:00
|
|
|
$cache->delete("ip_bans_sorted");
|
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
|
|
|
{
|
|
|
|
global $database;
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
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) {
|
2019-05-28 16:59:38 +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) {
|
2019-05-28 16:59:38 +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) {
|
|
|
|
$database->execute("ALTER TABLE bans ADD COLUMN expire NULL TIMESTAMP DEFAULT NULL");
|
|
|
|
$database->execute("UPDATE bans SET expire = dateadd(s, end_timestamp, '1970-01-01 00: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
|
|
|
}
|
2019-11-03 16:22:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
private function check_ip_ban()
|
|
|
|
{
|
|
|
|
$remote = $_SERVER['REMOTE_ADDR'];
|
|
|
|
$bans = $this->get_active_bans_sorted();
|
|
|
|
|
|
|
|
// bans[0] = IPs
|
|
|
|
if (isset($bans[0][$remote])) {
|
|
|
|
$this->block($remote); // never returns
|
|
|
|
}
|
|
|
|
|
|
|
|
// bans[1] = CIDR nets
|
|
|
|
foreach ($bans[1] as $ip => $true) {
|
|
|
|
if (ip_in_range($remote, $ip)) {
|
|
|
|
$this->block($remote); // never returns
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function block(string $remote)
|
|
|
|
{
|
|
|
|
global $config, $database;
|
|
|
|
|
2019-06-20 15:42:32 +00:00
|
|
|
$prefix = ($database->get_driver_name() == DatabaseDriver::SQLITE ? "bans." : "");
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
$bans = $this->get_active_bans();
|
|
|
|
|
|
|
|
foreach ($bans as $row) {
|
|
|
|
$ip = $row[$prefix."ip"];
|
|
|
|
if (
|
|
|
|
(strstr($ip, '/') && ip_in_range($remote, $ip)) ||
|
|
|
|
($ip == $remote)
|
|
|
|
) {
|
|
|
|
$reason = $row[$prefix.'reason'];
|
|
|
|
$admin = User::by_id($row[$prefix.'banner_id']);
|
2019-11-03 23:17:09 +00:00
|
|
|
$date = $row['expire'];
|
2019-05-28 16:59:38 +00:00
|
|
|
$msg = $config->get_string("ipban_message");
|
|
|
|
$msg = str_replace('$IP', $ip, $msg);
|
|
|
|
$msg = str_replace('$DATE', $date, $msg);
|
|
|
|
$msg = str_replace('$ADMIN', $admin->name, $msg);
|
|
|
|
$msg = str_replace('$REASON', $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);
|
|
|
|
}
|
|
|
|
header("HTTP/1.0 403 Forbidden");
|
|
|
|
print "$msg";
|
|
|
|
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log_error("ipban", "block($remote) called but no bans matched");
|
|
|
|
exit;
|
|
|
|
}
|
2019-11-03 16:22:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
private function get_bans()
|
|
|
|
{
|
|
|
|
global $database;
|
|
|
|
$bans = $database->get_all("
|
2008-07-17 08:13:52 +00:00
|
|
|
SELECT bans.*, users.name as banner_name
|
|
|
|
FROM bans
|
|
|
|
JOIN users ON banner_id = users.id
|
2019-11-03 23:17:09 +00:00
|
|
|
ORDER BY added, expires, bans.id
|
2009-01-17 04:40:51 +00:00
|
|
|
");
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($bans) {
|
|
|
|
return $bans;
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function get_active_bans()
|
|
|
|
{
|
|
|
|
global $database;
|
|
|
|
|
|
|
|
$bans = $database->get_all("
|
2010-02-02 11:32:05 +00:00
|
|
|
SELECT bans.*, users.name as banner_name
|
|
|
|
FROM bans
|
|
|
|
JOIN users ON banner_id = users.id
|
2019-11-03 23:17:09 +00:00
|
|
|
WHERE (expires > CURRENT_TIMESTAMP) OR (expires IS NULL)
|
|
|
|
ORDER BY expires, bans.id
|
|
|
|
");
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
if ($bans) {
|
|
|
|
return $bans;
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns [ips, nets]
|
|
|
|
private function get_active_bans_sorted()
|
|
|
|
{
|
2019-10-02 09:49:32 +00:00
|
|
|
global $cache;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2019-10-02 09:49:32 +00:00
|
|
|
$cached = $cache->get("ip_bans_sorted");
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($cached) {
|
|
|
|
return $cached;
|
|
|
|
}
|
|
|
|
|
|
|
|
$bans = $this->get_active_bans();
|
|
|
|
$ips = []; # "0.0.0.0" => false);
|
|
|
|
$nets = []; # "0.0.0.0/32" => false);
|
|
|
|
foreach ($bans as $row) {
|
|
|
|
if (strstr($row['ip'], '/')) {
|
|
|
|
$nets[$row['ip']] = true;
|
|
|
|
} else {
|
|
|
|
$ips[$row['ip']] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$sorted = [$ips, $nets];
|
2019-10-02 09:49:32 +00:00
|
|
|
$cache->set("ip_bans_sorted", $sorted, 600);
|
2019-05-28 16:59:38 +00:00
|
|
|
return $sorted;
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|