2007-10-22 00:13:57 +00:00
|
|
|
<?php
|
2009-08-20 22:37:17 +00:00
|
|
|
/*
|
2007-10-22 00:13:57 +00:00
|
|
|
* Name: Image Hash Ban
|
2007-12-04 18:20:46 +00:00
|
|
|
* Author: ATravelingGeek <atg@atravelinggeek.com>
|
2007-10-22 00:13:57 +00:00
|
|
|
* Link: http://atravelinggeek.com/
|
|
|
|
* License: GPLv2
|
|
|
|
* Description: Ban images based on their hash
|
|
|
|
* Based on the ResolutionLimit and IPban extensions by Shish
|
2009-08-20 22:37:17 +00:00
|
|
|
* Version 0.1, October 21, 2007
|
2007-10-22 00:13:57 +00:00
|
|
|
*/
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2007-10-22 00:13:57 +00:00
|
|
|
// RemoveImageHashBanEvent {{{
|
|
|
|
class RemoveImageHashBanEvent extends Event {
|
|
|
|
var $hash;
|
|
|
|
|
|
|
|
public function RemoveImageHashBanEvent($hash) {
|
|
|
|
$this->hash = $hash;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
// AddImageHashBanEvent {{{
|
2009-01-04 19:18:37 +00:00
|
|
|
class AddImageHashBanEvent extends Event {
|
2007-10-22 00:13:57 +00:00
|
|
|
var $hash;
|
|
|
|
var $reason;
|
|
|
|
|
|
|
|
public function AddImageHashBanEvent($hash, $reason) {
|
|
|
|
$this->hash = $hash;
|
|
|
|
$this->reason = $reason;
|
|
|
|
}
|
|
|
|
}
|
2008-04-06 16:43:09 +00:00
|
|
|
// }}}
|
2012-02-08 12:07:01 +00:00
|
|
|
class ImageBan extends Extension {
|
2010-05-28 01:07:33 +00:00
|
|
|
public function onInitExt(InitExtEvent $event) {
|
|
|
|
global $config, $database;
|
|
|
|
if($config->get_int("ext_imageban_version") < 1) {
|
|
|
|
$database->create_table("image_bans", "
|
|
|
|
id SCORE_AIPK,
|
|
|
|
hash CHAR(32) NOT NULL,
|
2014-03-02 18:50:46 +00:00
|
|
|
date SCORE_DATETIME DEFAULT SCORE_NOW,
|
2010-05-28 01:07:33 +00:00
|
|
|
reason TEXT NOT NULL
|
|
|
|
");
|
|
|
|
$config->set_int("ext_imageban_version", 1);
|
2007-10-22 00:13:57 +00:00
|
|
|
}
|
2010-05-28 01:07:33 +00:00
|
|
|
}
|
2008-04-06 16:47:05 +00:00
|
|
|
|
2010-05-28 01:07:33 +00:00
|
|
|
public function onDataUpload(DataUploadEvent $event) {
|
|
|
|
global $database;
|
2011-01-01 16:28:04 +00:00
|
|
|
$row = $database->get_row("SELECT * FROM image_bans WHERE hash = :hash", array("hash"=>$event->hash));
|
2010-05-28 01:07:33 +00:00
|
|
|
if($row) {
|
2012-03-08 01:37:42 +00:00
|
|
|
log_info("image_hash_ban", "Attempted to upload a blocked image ({$event->hash} - {$row['reason']})");
|
2010-05-28 01:07:33 +00:00
|
|
|
throw new UploadException("Image ".html_escape($row["hash"])." has been banned, reason: ".format_text($row["reason"]));
|
2007-10-22 00:13:57 +00:00
|
|
|
}
|
2010-05-28 01:07:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function onPageRequest(PageRequestEvent $event) {
|
|
|
|
global $config, $database, $page, $user;
|
2008-04-06 16:47:05 +00:00
|
|
|
|
2010-05-28 01:07:33 +00:00
|
|
|
if($event->page_matches("image_hash_ban")) {
|
2012-03-30 19:54:33 +00:00
|
|
|
if($user->can("ban_image")) {
|
2012-10-15 18:03:59 +00:00
|
|
|
if($event->get_arg(0) == "add") {
|
|
|
|
$image = isset($_POST['image_id']) ? Image::by_id(int_escape($_POST['image_id'])) : null;
|
|
|
|
$hash = isset($_POST["hash"]) ? $_POST["hash"] : $image->hash;
|
|
|
|
$reason = isset($_POST['reason']) ? $_POST['reason'] : "DNP";
|
2012-06-10 16:57:57 +00:00
|
|
|
|
2012-10-15 18:03:59 +00:00
|
|
|
if($hash) {
|
|
|
|
send_event(new AddImageHashBanEvent($hash, $reason));
|
2012-06-10 16:57:57 +00:00
|
|
|
flash_message("Image ban added");
|
2012-10-15 18:03:59 +00:00
|
|
|
|
2008-07-27 15:55:45 +00:00
|
|
|
if($image) {
|
|
|
|
send_event(new ImageDeletionEvent($image));
|
2012-06-10 16:57:57 +00:00
|
|
|
flash_message("Image deleted");
|
2008-07-27 15:55:45 +00:00
|
|
|
}
|
2013-05-30 09:18:56 +00:00
|
|
|
|
|
|
|
$page->set_mode("redirect");
|
|
|
|
$page->set_redirect($_SERVER['HTTP_REFERER']);
|
2008-07-27 15:55:45 +00:00
|
|
|
}
|
2007-10-22 00:13:57 +00:00
|
|
|
}
|
|
|
|
else if($event->get_arg(0) == "remove") {
|
|
|
|
if(isset($_POST['hash'])) {
|
|
|
|
send_event(new RemoveImageHashBanEvent($_POST['hash']));
|
|
|
|
|
2012-06-10 16:57:57 +00:00
|
|
|
flash_message("Image ban removed");
|
2007-10-22 00:13:57 +00:00
|
|
|
$page->set_mode("redirect");
|
2013-05-30 09:18:56 +00:00
|
|
|
$page->set_redirect($_SERVER['HTTP_REFERER']);
|
2007-10-22 00:13:57 +00:00
|
|
|
}
|
|
|
|
}
|
2008-12-12 19:23:42 +00:00
|
|
|
else if($event->get_arg(0) == "list") {
|
2008-12-14 23:57:26 +00:00
|
|
|
$page_num = 0;
|
|
|
|
if($event->count_args() == 2) {
|
|
|
|
$page_num = int_escape($event->get_arg(1));
|
|
|
|
}
|
2010-01-05 17:31:25 +00:00
|
|
|
$page_size = 100;
|
2011-01-01 16:28:04 +00:00
|
|
|
$page_count = ceil($database->get_one("SELECT COUNT(id) FROM image_bans")/$page_size);
|
2010-01-05 17:31:25 +00:00
|
|
|
$this->theme->display_Image_hash_Bans($page, $page_num, $page_count, $this->get_image_hash_bans($page_num, $page_size));
|
2008-12-12 19:23:42 +00:00
|
|
|
}
|
2007-10-22 00:13:57 +00:00
|
|
|
}
|
|
|
|
}
|
2010-05-28 01:07:33 +00:00
|
|
|
}
|
2008-04-06 16:47:05 +00:00
|
|
|
|
2010-05-28 01:07:33 +00:00
|
|
|
public function onUserBlockBuilding(UserBlockBuildingEvent $event) {
|
|
|
|
global $user;
|
2012-03-30 19:54:33 +00:00
|
|
|
if($user->can("ban_image")) {
|
2010-05-28 01:07:33 +00:00
|
|
|
$event->add_link("Image Bans", make_link("image_hash_ban/list/1"));
|
2007-10-22 00:13:57 +00:00
|
|
|
}
|
2010-05-28 01:07:33 +00:00
|
|
|
}
|
2007-10-22 00:13:57 +00:00
|
|
|
|
2010-05-28 01:07:33 +00:00
|
|
|
public function onAddImageHashBan(AddImageHashBanEvent $event) {
|
2012-06-10 16:57:57 +00:00
|
|
|
global $database;
|
|
|
|
$database->Execute(
|
|
|
|
"INSERT INTO image_bans (hash, reason, date) VALUES (?, ?, now())",
|
|
|
|
array($event->hash, $event->reason));
|
|
|
|
log_info("image_hash_ban", "Banned hash {$event->hash} because '{$event->reason}'");
|
2010-05-28 01:07:33 +00:00
|
|
|
}
|
2008-04-06 16:47:05 +00:00
|
|
|
|
2010-05-28 01:07:33 +00:00
|
|
|
public function onRemoveImageHashBan(RemoveImageHashBanEvent $event) {
|
2012-06-10 16:57:57 +00:00
|
|
|
global $database;
|
|
|
|
$database->Execute("DELETE FROM image_bans WHERE hash = ?", array($event->hash));
|
2007-10-22 00:13:57 +00:00
|
|
|
}
|
2008-04-06 16:47:05 +00:00
|
|
|
|
2010-05-28 01:07:33 +00:00
|
|
|
public function onImageAdminBlockBuilding(ImageAdminBlockBuildingEvent $event) {
|
|
|
|
global $user;
|
2012-03-30 19:54:33 +00:00
|
|
|
if($user->can("ban_image")) {
|
2010-05-28 01:07:33 +00:00
|
|
|
$event->add_part($this->theme->get_buttons_html($event->image));
|
|
|
|
}
|
2007-10-22 00:13:57 +00:00
|
|
|
}
|
2008-04-06 16:47:05 +00:00
|
|
|
|
2007-10-22 00:13:57 +00:00
|
|
|
// DB funness
|
2008-04-06 16:47:05 +00:00
|
|
|
|
2010-01-05 17:31:25 +00:00
|
|
|
public function get_image_hash_bans($page, $size=100) {
|
2012-01-17 15:33:42 +00:00
|
|
|
global $database;
|
|
|
|
|
2007-10-22 00:13:57 +00:00
|
|
|
// FIXME: many
|
2008-12-14 23:57:26 +00:00
|
|
|
$size_i = int_escape($size);
|
2009-01-14 23:25:50 +00:00
|
|
|
$offset_i = int_escape($page-1)*$size_i;
|
2012-01-17 15:33:42 +00:00
|
|
|
$where = array("(1=1)");
|
|
|
|
$args = array();
|
|
|
|
if(!empty($_GET['hash'])) {
|
|
|
|
$where[] = 'hash = ?';
|
|
|
|
$args[] = $_GET['hash'];
|
|
|
|
}
|
|
|
|
if(!empty($_GET['reason'])) {
|
|
|
|
$where[] = 'reason SCORE_ILIKE ?';
|
|
|
|
$args[] = "%".$_GET['reason']."%";
|
|
|
|
}
|
|
|
|
$where = implode(" AND ", $where);
|
2012-06-23 23:57:55 +00:00
|
|
|
$bans = $database->get_all($database->scoreql_to_sql("
|
2012-01-17 15:33:42 +00:00
|
|
|
SELECT *
|
|
|
|
FROM image_bans
|
|
|
|
WHERE $where
|
|
|
|
ORDER BY id DESC
|
|
|
|
LIMIT $size_i
|
|
|
|
OFFSET $offset_i
|
|
|
|
"), $args);
|
2007-10-22 00:13:57 +00:00
|
|
|
if($bans) {return $bans;}
|
|
|
|
else {return array();}
|
|
|
|
}
|
|
|
|
|
2010-05-28 01:07:33 +00:00
|
|
|
// in before resolution limit plugin
|
|
|
|
public function get_priority() {return 30;}
|
2007-10-22 00:13:57 +00:00
|
|
|
}
|
2007-12-04 18:20:46 +00:00
|
|
|
?>
|