This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/image_hash_ban/main.php

175 lines
5.8 KiB
PHP
Raw Normal View History

2020-01-26 13:19:35 +00:00
<?php declare(strict_types=1);
2009-01-04 19:18:37 +00:00
2019-12-26 00:36:32 +00:00
use MicroCRUD\ActionColumn;
2019-11-29 01:52:24 +00:00
use MicroCRUD\StringColumn;
use MicroCRUD\DateColumn;
use MicroCRUD\TextColumn;
use MicroCRUD\Table;
class HashBanTable extends Table
{
2019-11-29 02:06:22 +00:00
public function __construct(\FFSPHP\PDO $db)
2019-11-29 01:52:24 +00:00
{
2019-11-29 02:06:22 +00:00
parent::__construct($db);
2019-11-29 02:04:14 +00:00
$this->table = "image_bans";
$this->base_query = "SELECT * FROM image_bans";
2019-12-15 15:31:44 +00:00
$this->primary_key = "hash";
2019-11-29 02:04:14 +00:00
$this->size = 100;
2019-12-15 15:31:44 +00:00
$this->limit = 1000000;
2019-12-26 00:36:32 +00:00
$this->set_columns([
2019-11-29 01:52:24 +00:00
new StringColumn("hash", "Hash"),
new TextColumn("reason", "Reason"),
new DateColumn("date", "Date"),
2020-01-01 10:42:38 +00:00
new ActionColumn("hash"),
2019-12-26 00:36:32 +00:00
]);
2019-11-29 01:52:24 +00:00
$this->order_by = ["date DESC", "id"];
$this->create_url = make_link("image_hash_ban/add");
$this->delete_url = make_link("image_hash_ban/remove");
$this->table_attrs = ["class" => "zebra"];
}
}
class RemoveImageHashBanEvent extends Event
{
public $hash;
public function __construct(string $hash)
{
2020-01-26 13:19:35 +00:00
parent::__construct();
$this->hash = $hash;
}
}
2019-11-03 16:22:59 +00:00
class AddImageHashBanEvent extends Event
{
public $hash;
public $reason;
public function __construct(string $hash, string $reason)
{
2020-01-26 13:19:35 +00:00
parent::__construct();
$this->hash = $hash;
$this->reason = $reason;
}
}
2019-11-03 16:22:59 +00:00
class ImageBan extends Extension
{
2020-01-26 13:19:35 +00:00
/** @var ImageBanTheme */
protected $theme;
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event)
{
2020-01-26 13:19:35 +00:00
global $database;
2019-11-03 19:49:52 +00:00
if ($this->get_version("ext_imageban_version") < 1) {
$database->create_table("image_bans", "
id SCORE_AIPK,
hash CHAR(32) NOT NULL,
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
reason TEXT NOT NULL
");
2019-11-03 19:49:52 +00:00
$this->set_version("ext_imageban_version", 1);
}
}
public function onDataUpload(DataUploadEvent $event)
{
global $database;
$row = $database->get_row("SELECT * FROM image_bans WHERE hash = :hash", ["hash"=>$event->hash]);
if ($row) {
log_info("image_hash_ban", "Attempted to upload a blocked image ({$event->hash} - {$row['reason']})");
2020-10-26 15:16:21 +00:00
throw new UploadException("Post ".html_escape($row["hash"])." has been banned, reason: ".format_text($row["reason"]));
}
}
public function onPageRequest(PageRequestEvent $event)
{
global $database, $page, $user;
if ($event->page_matches("image_hash_ban")) {
2019-07-09 14:10:21 +00:00
if ($user->can(Permissions::BAN_IMAGE)) {
if ($event->get_arg(0) == "add") {
2019-11-29 01:52:24 +00:00
$user->ensure_authed();
$input = validate_input(["c_hash"=>"optional,string", "c_reason"=>"string", "c_image_id"=>"optional,int"]);
$image = isset($input['c_image_id']) ? Image::by_id($input['c_image_id']) : null;
$hash = isset($input["c_hash"]) ? $input["c_hash"] : $image->hash;
$reason = isset($input['c_reason']) ? $input['c_reason'] : "DNP";
if ($hash) {
send_event(new AddImageHashBanEvent($hash, $reason));
2020-10-26 15:16:21 +00:00
$page->flash("Post ban added");
if ($image) {
send_event(new ImageDeletionEvent($image));
2020-10-26 15:16:21 +00:00
$page->flash("Post deleted");
}
2019-06-19 01:58:28 +00:00
$page->set_mode(PageMode::REDIRECT);
2020-03-27 19:41:34 +00:00
$page->set_redirect(referer_or(make_link()));
}
} elseif ($event->get_arg(0) == "remove") {
2019-11-29 01:52:24 +00:00
$user->ensure_authed();
2019-11-29 02:06:22 +00:00
$input = validate_input(["d_hash"=>"string"]);
2019-11-29 02:21:00 +00:00
send_event(new RemoveImageHashBanEvent($input['d_hash']));
2020-10-26 15:16:21 +00:00
$page->flash("Post ban removed");
2019-11-29 01:52:24 +00:00
$page->set_mode(PageMode::REDIRECT);
2020-03-27 19:41:34 +00:00
$page->set_redirect(referer_or(make_link()));
} elseif ($event->get_arg(0) == "list") {
2019-11-29 01:52:24 +00:00
$t = new HashBanTable($database->raw_db());
$t->token = $user->get_auth_token();
$t->inputs = $_GET;
$this->theme->display_bans($page, $t->table($t->query()), $t->paginator());
}
}
}
}
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event)
{
global $user;
2019-09-29 13:30:55 +00:00
if ($event->parent==="system") {
if ($user->can(Permissions::BAN_IMAGE)) {
2020-10-26 15:16:21 +00:00
$event->add_nav_link("image_bans", new Link('image_hash_ban/list/1'), "Post Bans", NavLink::is_active(["image_hash_ban"]));
}
}
}
public function onUserBlockBuilding(UserBlockBuildingEvent $event)
{
global $user;
2019-07-09 14:10:21 +00:00
if ($user->can(Permissions::BAN_IMAGE)) {
2020-10-26 15:16:21 +00:00
$event->add_link("Post Bans", make_link("image_hash_ban/list/1"));
}
}
public function onAddImageHashBan(AddImageHashBanEvent $event)
{
global $database;
2019-11-29 01:52:24 +00:00
$database->execute(
2019-11-27 11:22:46 +00:00
"INSERT INTO image_bans (hash, reason, date) VALUES (:hash, :reason, now())",
["hash"=>$event->hash, "reason"=>$event->reason]
);
log_info("image_hash_ban", "Banned hash {$event->hash} because '{$event->reason}'");
}
public function onRemoveImageHashBan(RemoveImageHashBanEvent $event)
{
global $database;
2019-11-29 01:52:24 +00:00
$database->execute("DELETE FROM image_bans WHERE hash = :hash", ["hash"=>$event->hash]);
}
public function onImageAdminBlockBuilding(ImageAdminBlockBuildingEvent $event)
{
global $user;
2019-07-09 14:10:21 +00:00
if ($user->can(Permissions::BAN_IMAGE)) {
$event->add_part($this->theme->get_buttons_html($event->image));
}
}
// in before resolution limit plugin
public function get_priority(): int
{
return 30;
}
}