2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2023-01-10 22:44:09 +00:00
|
|
|
namespace Shimmie2;
|
|
|
|
|
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;
|
|
|
|
|
2024-02-10 18:35:55 +00:00
|
|
|
use function MicroHTML\{INPUT,emptyHTML};
|
|
|
|
|
2019-11-29 01:52:24 +00:00
|
|
|
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");
|
2024-01-08 21:03:04 +00:00
|
|
|
$this->table_attrs = ["class" => "zebra form"];
|
2019-11-29 01:52:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class RemoveImageHashBanEvent extends Event
|
|
|
|
{
|
2021-03-14 23:43:50 +00:00
|
|
|
public string $hash;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
public function __construct(string $hash)
|
|
|
|
{
|
2020-01-26 13:19:35 +00:00
|
|
|
parent::__construct();
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->hash = $hash;
|
|
|
|
}
|
2007-10-22 00:13:57 +00:00
|
|
|
}
|
2019-11-03 16:22:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class AddImageHashBanEvent extends Event
|
|
|
|
{
|
2021-03-14 23:43:50 +00:00
|
|
|
public string $hash;
|
|
|
|
public string $reason;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
public function __construct(string $hash, string $reason)
|
|
|
|
{
|
2020-01-26 13:19:35 +00:00
|
|
|
parent::__construct();
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->hash = $hash;
|
|
|
|
$this->reason = $reason;
|
|
|
|
}
|
2007-10-22 00:13:57 +00:00
|
|
|
}
|
2019-11-03 16:22:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class ImageBan extends Extension
|
|
|
|
{
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
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) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$database->create_table("image_bans", "
|
2010-05-28 01:07:33 +00:00
|
|
|
id SCORE_AIPK,
|
|
|
|
hash CHAR(32) NOT NULL,
|
2019-11-03 18:28:05 +00:00
|
|
|
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
2010-05-28 01:07:33 +00:00
|
|
|
reason TEXT NOT NULL
|
|
|
|
");
|
2019-11-03 19:49:52 +00:00
|
|
|
$this->set_version("ext_imageban_version", 1);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onDataUpload(DataUploadEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $database;
|
2023-11-11 21:49:12 +00:00
|
|
|
$row = $database->get_row("SELECT * FROM image_bans WHERE hash = :hash", ["hash" => $event->hash]);
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($row) {
|
|
|
|
log_info("image_hash_ban", "Attempted to upload a blocked image ({$event->hash} - {$row['reason']})");
|
2024-01-05 13:12:44 +00:00
|
|
|
throw new UploadException("Post {$row["hash"]} has been banned, reason: {$row["reason"]}");
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $database, $page, $user;
|
|
|
|
|
2024-02-11 11:34:09 +00:00
|
|
|
if ($event->page_matches("image_hash_ban/add", method: "POST", permission: Permissions::BAN_IMAGE)) {
|
|
|
|
$input = validate_input(["c_hash" => "optional,string", "c_reason" => "string", "c_image_id" => "optional,int"]);
|
2024-08-31 20:22:54 +00:00
|
|
|
$image = isset($input['c_image_id']) ? Image::by_id_ex($input['c_image_id']) : null;
|
|
|
|
$hash = isset($input["c_hash"]) ? $input["c_hash"] : ($image ? $image->hash : null);
|
2024-02-11 11:34:09 +00:00
|
|
|
$reason = isset($input['c_reason']) ? $input['c_reason'] : "DNP";
|
|
|
|
|
|
|
|
if ($hash) {
|
|
|
|
send_event(new AddImageHashBanEvent($hash, $reason));
|
|
|
|
$page->flash("Post ban added");
|
|
|
|
|
|
|
|
if ($image) {
|
|
|
|
send_event(new ImageDeletionEvent($image));
|
|
|
|
$page->flash("Post deleted");
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2024-02-11 11:34:09 +00:00
|
|
|
|
2024-02-10 23:03:14 +00:00
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
|
|
|
$page->set_redirect(referer_or(make_link()));
|
|
|
|
}
|
2024-02-11 11:34:09 +00:00
|
|
|
}
|
|
|
|
if ($event->page_matches("image_hash_ban/remove", method: "POST", permission: Permissions::BAN_IMAGE)) {
|
|
|
|
$input = validate_input(["d_hash" => "string"]);
|
|
|
|
send_event(new RemoveImageHashBanEvent($input['d_hash']));
|
|
|
|
$page->flash("Post ban removed");
|
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
|
|
|
$page->set_redirect(referer_or(make_link()));
|
|
|
|
}
|
|
|
|
if ($event->page_matches("image_hash_ban/list", permission: Permissions::BAN_IMAGE)) {
|
|
|
|
$t = new HashBanTable($database->raw_db());
|
|
|
|
$t->token = $user->get_auth_token();
|
|
|
|
$t->inputs = $event->GET;
|
|
|
|
$this->theme->display_crud("Post Bans", $t->table($t->query()), $t->paginator());
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-02 19:54:48 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event): void
|
2019-08-02 19:54:48 +00:00
|
|
|
{
|
|
|
|
global $user;
|
2023-11-11 21:49:12 +00:00
|
|
|
if ($event->parent === "system") {
|
2019-08-02 19:54:48 +00:00
|
|
|
if ($user->can(Permissions::BAN_IMAGE)) {
|
2024-02-24 16:30:56 +00:00
|
|
|
$event->add_nav_link("image_bans", new Link('image_hash_ban/list'), "Post Bans", NavLink::is_active(["image_hash_ban"]));
|
2019-08-02 19:54:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onUserBlockBuilding(UserBlockBuildingEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $user;
|
2019-07-09 14:10:21 +00:00
|
|
|
if ($user->can(Permissions::BAN_IMAGE)) {
|
2024-02-24 16:30:56 +00:00
|
|
|
$event->add_link("Post Bans", make_link("image_hash_ban/list"));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onAddImageHashBan(AddImageHashBanEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
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())",
|
2023-11-11 21:49:12 +00:00
|
|
|
["hash" => $event->hash, "reason" => $event->reason]
|
2019-05-28 16:59:38 +00:00
|
|
|
);
|
|
|
|
log_info("image_hash_ban", "Banned hash {$event->hash} because '{$event->reason}'");
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onRemoveImageHashBan(RemoveImageHashBanEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $database;
|
2023-11-11 21:49:12 +00:00
|
|
|
$database->execute("DELETE FROM image_bans WHERE hash = :hash", ["hash" => $event->hash]);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onImageAdminBlockBuilding(ImageAdminBlockBuildingEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $user;
|
2019-07-09 14:10:21 +00:00
|
|
|
if ($user->can(Permissions::BAN_IMAGE)) {
|
2024-02-10 18:35:55 +00:00
|
|
|
$event->add_part(SHM_SIMPLE_FORM(
|
|
|
|
"image_hash_ban/add",
|
|
|
|
INPUT(["type" => 'hidden', "name" => 'c_hash', "value" => $event->image->hash]),
|
|
|
|
INPUT(["type" => 'hidden', "name" => 'c_image_id', "value" => $event->image->id]),
|
|
|
|
INPUT(["type" => 'text', "name" => 'c_reason']),
|
|
|
|
INPUT(["type" => 'submit', "value" => 'Ban Hash and Delete Post']),
|
|
|
|
));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// in before resolution limit plugin
|
|
|
|
public function get_priority(): int
|
|
|
|
{
|
|
|
|
return 30;
|
|
|
|
}
|
2007-10-22 00:13:57 +00:00
|
|
|
}
|