2007-04-16 11:58:25 +00:00
|
|
|
<?php
|
2009-08-20 22:37:17 +00:00
|
|
|
/*
|
2008-04-11 06:12:07 +00:00
|
|
|
* Name: IP Ban
|
|
|
|
* Author: Shish <webmaster@shishnet.org>
|
|
|
|
* License: GPLv2
|
|
|
|
* Description: Ban IP addresses
|
2009-01-16 08:18:41 +00:00
|
|
|
* Documentation:
|
|
|
|
* <b>Adding a Ban</b>
|
|
|
|
* <br>IP: Can be a single IP (eg. 123.234.210.21), or a CIDR block (eg. 152.23.43.0/24)
|
|
|
|
* <br>Reason: Any text, for the admin to remember why the ban was put in place
|
|
|
|
* <br>Until: Either a date in YYYY-MM-DD format, or an offset like "3 days"
|
2008-04-11 06:12:07 +00:00
|
|
|
*/
|
2007-04-16 11:58:25 +00:00
|
|
|
|
|
|
|
// RemoveIPBanEvent {{{
|
|
|
|
class RemoveIPBanEvent extends Event {
|
2007-10-21 23:31:23 +00:00
|
|
|
var $id;
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2007-10-21 23:31:23 +00:00
|
|
|
public function RemoveIPBanEvent($id) {
|
|
|
|
$this->id = $id;
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
// AddIPBanEvent {{{
|
2009-01-04 19:18:37 +00:00
|
|
|
class AddIPBanEvent extends Event {
|
2007-04-16 11:58:25 +00:00
|
|
|
var $ip;
|
|
|
|
var $reason;
|
2007-05-28 23:22:37 +00:00
|
|
|
var $end;
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2007-05-28 23:22:37 +00:00
|
|
|
public function AddIPBanEvent($ip, $reason, $end) {
|
2007-04-16 11:58:25 +00:00
|
|
|
$this->ip = $ip;
|
|
|
|
$this->reason = $reason;
|
2007-05-28 23:22:37 +00:00
|
|
|
$this->end = $end;
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
|
2008-08-23 12:08:19 +00:00
|
|
|
class IPBan implements Extension {
|
2007-06-30 01:19:11 +00:00
|
|
|
var $theme;
|
2007-04-16 11:58:25 +00:00
|
|
|
// event handler {{{
|
2008-08-23 12:08:19 +00:00
|
|
|
public function receive_event(Event $event) {
|
2009-05-11 14:04:33 +00:00
|
|
|
global $config, $database, $page, $user;
|
2008-09-06 16:59:02 +00:00
|
|
|
if(is_null($this->theme)) $this->theme = get_theme_object($this);
|
2007-06-30 01:19:11 +00:00
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if($event instanceof InitExtEvent) {
|
2008-07-25 11:30:47 +00:00
|
|
|
if($config->get_int("ext_ipban_version") < 5) {
|
2007-04-16 11:58:25 +00:00
|
|
|
$this->install();
|
|
|
|
}
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2007-08-08 05:16:16 +00:00
|
|
|
$this->check_ip_ban();
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
2008-09-06 17:48:03 +00:00
|
|
|
if(($event instanceof PageRequestEvent) && $event->page_matches("ip_ban")) {
|
2007-04-16 11:58:25 +00:00
|
|
|
if($user->is_admin()) {
|
|
|
|
if($event->get_arg(0) == "add") {
|
2007-05-28 23:22:37 +00:00
|
|
|
if(isset($_POST['ip']) && isset($_POST['reason']) && isset($_POST['end'])) {
|
2007-07-24 13:25:11 +00:00
|
|
|
if(empty($_POST['end'])) $end = null;
|
|
|
|
else $end = $_POST['end'];
|
|
|
|
send_event(new AddIPBanEvent($_POST['ip'], $_POST['reason'], $end));
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2009-05-11 14:04:33 +00:00
|
|
|
$page->set_mode("redirect");
|
|
|
|
$page->set_redirect(make_link("ip_ban/list"));
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if($event->get_arg(0) == "remove") {
|
2007-10-21 23:31:23 +00:00
|
|
|
if(isset($_POST['id'])) {
|
|
|
|
send_event(new RemoveIPBanEvent($_POST['id']));
|
2009-05-11 14:04:33 +00:00
|
|
|
$page->set_mode("redirect");
|
|
|
|
$page->set_redirect(make_link("ip_ban/list"));
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
2008-04-11 05:23:11 +00:00
|
|
|
else if($event->get_arg(0) == "list") {
|
2009-01-17 04:40:51 +00:00
|
|
|
$bans = (isset($_GET["all"])) ? $this->get_bans() : $this->get_active_bans();
|
2009-05-11 14:04:33 +00:00
|
|
|
$this->theme->display_bans($page, $bans);
|
2008-04-11 05:23:11 +00:00
|
|
|
}
|
|
|
|
}
|
2008-10-18 06:05:26 +00:00
|
|
|
else {
|
2009-05-11 14:04:33 +00:00
|
|
|
$this->theme->display_permission_denied($page);
|
2008-10-18 06:05:26 +00:00
|
|
|
}
|
2008-04-11 05:23:11 +00:00
|
|
|
}
|
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if($event instanceof UserBlockBuildingEvent) {
|
2009-05-11 14:04:33 +00:00
|
|
|
if($user->is_admin()) {
|
2008-04-11 05:23:11 +00:00
|
|
|
$event->add_link("IP Bans", make_link("ip_ban/list"));
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if($event instanceof AddIPBanEvent) {
|
2007-10-21 23:31:23 +00:00
|
|
|
$this->add_ip_ban($event->ip, $event->reason, $event->end, $user);
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if($event instanceof RemoveIPBanEvent) {
|
2008-08-12 01:36:39 +00:00
|
|
|
$database->Execute("DELETE FROM bans WHERE id = ?", array($event->id));
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
// installer {{{
|
|
|
|
protected function install() {
|
|
|
|
global $database;
|
|
|
|
global $config;
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2008-05-18 02:17:16 +00:00
|
|
|
// shortcut to latest
|
|
|
|
if($config->get_int("ext_ipban_version") < 1) {
|
2009-01-22 12:05:55 +00:00
|
|
|
$database->create_table("bans", "
|
|
|
|
id SCORE_AIPK,
|
|
|
|
banner_id INTEGER NOT NULL,
|
|
|
|
ip SCORE_INET NOT NULL,
|
|
|
|
end_timestamp INTEGER,
|
|
|
|
reason TEXT NOT NULL,
|
|
|
|
INDEX (end_timestamp)
|
2008-05-18 02:17:16 +00:00
|
|
|
");
|
2008-09-07 04:52:40 +00:00
|
|
|
$config->set_int("ext_ipban_version", 6);
|
2008-05-18 02:17:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ===
|
|
|
|
|
|
|
|
if($config->get_int("ext_ipban_version") < 1) {
|
|
|
|
$database->Execute("CREATE TABLE bans (
|
|
|
|
id int(11) NOT NULL auto_increment,
|
|
|
|
ip char(15) default NULL,
|
|
|
|
date datetime default NULL,
|
|
|
|
end datetime default NULL,
|
|
|
|
reason varchar(255) default NULL,
|
|
|
|
PRIMARY KEY (id)
|
|
|
|
)");
|
|
|
|
$config->set_int("ext_ipban_version", 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($config->get_int("ext_ipban_version") == 1) {
|
|
|
|
$database->execute("ALTER TABLE bans ADD COLUMN banner_id INTEGER NOT NULL AFTER id");
|
|
|
|
$config->set_int("ext_ipban_version", 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($config->get_int("ext_ipban_version") == 2) {
|
|
|
|
$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)");
|
|
|
|
$config->set_int("ext_ipban_version", 3);
|
2007-10-21 23:31:23 +00:00
|
|
|
}
|
2008-05-18 03:32:28 +00:00
|
|
|
|
|
|
|
if($config->get_int("ext_ipban_version") == 3) {
|
|
|
|
$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)");
|
|
|
|
$config->set_int("ext_ipban_version", 4);
|
|
|
|
}
|
2008-07-25 11:30:47 +00:00
|
|
|
|
|
|
|
if($config->get_int("ext_ipban_version") == 4) {
|
|
|
|
$database->execute("ALTER TABLE bans CHANGE end end_timestamp INTEGER");
|
|
|
|
$config->set_int("ext_ipban_version", 5);
|
|
|
|
}
|
2008-09-07 04:52:40 +00:00
|
|
|
|
|
|
|
if($config->get_int("ext_ipban_version") == 5) {
|
|
|
|
$database->execute("ALTER TABLE bans CHANGE ip ip VARCHAR(15)");
|
|
|
|
$config->set_int("ext_ipban_version", 6);
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
// deal with banned person {{{
|
|
|
|
private function check_ip_ban() {
|
2008-03-31 02:49:40 +00:00
|
|
|
global $config;
|
|
|
|
global $database;
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2009-07-17 01:43:57 +00:00
|
|
|
$prefix = ($database->engine->name == "sqlite" ? "bans." : "");
|
|
|
|
|
2008-04-01 10:57:18 +00:00
|
|
|
$remote = $_SERVER['REMOTE_ADDR'];
|
2008-03-31 02:49:40 +00:00
|
|
|
$bans = $this->get_active_bans();
|
|
|
|
foreach($bans as $row) {
|
2009-07-17 01:43:57 +00:00
|
|
|
$ip = $row[$prefix."ip"];
|
2008-04-01 10:57:18 +00:00
|
|
|
if(
|
2009-07-17 01:43:57 +00:00
|
|
|
(strstr($ip, '/') && ip_in_range($remote, $ip)) ||
|
|
|
|
($ip == $remote)
|
2008-04-01 10:57:18 +00:00
|
|
|
) {
|
2009-07-17 01:43:57 +00:00
|
|
|
$reason = $row[$prefix.'reason'];
|
|
|
|
$admin = User::by_id($row[$prefix.'banner_id']);
|
|
|
|
$date = date("Y-m-d", $row[$prefix.'end_timestamp']);
|
|
|
|
print "IP <b>$ip</b> has been banned until <b>$date</b> by <b>{$admin->name}</b> because of <b>$reason</b>";
|
2008-03-31 02:49:40 +00:00
|
|
|
|
|
|
|
$contact_link = $config->get_string("contact_link");
|
|
|
|
if(!empty($contact_link)) {
|
|
|
|
print "<p><a href='$contact_link'>Contact The Admin</a>";
|
|
|
|
}
|
|
|
|
exit;
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
// database {{{
|
2007-10-21 23:31:23 +00:00
|
|
|
private function get_bans() {
|
2007-04-16 11:58:25 +00:00
|
|
|
global $database;
|
2008-07-17 08:13:52 +00:00
|
|
|
$bans = $database->get_all("
|
|
|
|
SELECT bans.*, users.name as banner_name
|
|
|
|
FROM bans
|
|
|
|
JOIN users ON banner_id = users.id
|
2009-07-17 01:43:57 +00:00
|
|
|
ORDER BY end_timestamp, bans.id
|
2009-01-17 04:40:51 +00:00
|
|
|
");
|
2007-04-16 11:58:25 +00:00
|
|
|
if($bans) {return $bans;}
|
|
|
|
else {return array();}
|
|
|
|
}
|
|
|
|
|
2008-03-31 02:49:40 +00:00
|
|
|
private function get_active_bans() {
|
|
|
|
global $database;
|
2009-01-19 18:53:09 +00:00
|
|
|
|
2010-02-02 11:20:13 +00:00
|
|
|
$cached = $database->cache->get("ip_bans");
|
2009-01-19 18:53:09 +00:00
|
|
|
if($cached) return $cached;
|
|
|
|
|
2008-08-12 01:36:39 +00:00
|
|
|
$bans = $database->get_all("
|
2010-02-02 11:20:13 +00:00
|
|
|
SELECT * FROM bans
|
|
|
|
WHERE (end_timestamp > ?) OR (end_timestamp IS NULL)
|
|
|
|
", array(time()));
|
2009-01-19 18:53:09 +00:00
|
|
|
|
2010-02-02 11:20:13 +00:00
|
|
|
$database->cache->set("ip_bans", $bans, 600);
|
2009-01-19 18:53:09 +00:00
|
|
|
|
2008-03-31 02:49:40 +00:00
|
|
|
if($bans) {return $bans;}
|
|
|
|
else {return array();}
|
|
|
|
}
|
|
|
|
|
2007-10-21 23:31:23 +00:00
|
|
|
private function add_ip_ban($ip, $reason, $end, $user) {
|
2007-04-16 11:58:25 +00:00
|
|
|
global $database;
|
2008-07-25 11:30:47 +00:00
|
|
|
$sql = "INSERT INTO bans (ip, reason, end_timestamp, banner_id) VALUES (?, ?, ?, ?)";
|
2008-06-16 11:28:10 +00:00
|
|
|
$database->Execute($sql, array($ip, $reason, strtotime($end), $user->id));
|
2009-01-20 10:47:20 +00:00
|
|
|
$database->cache->delete("bans");
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
}
|
|
|
|
add_event_listener(new IPBan(), 10);
|
|
|
|
?>
|