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;
|
|
|
|
|
|
|
|
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"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2020-01-26 13:19:35 +00:00
|
|
|
/** @var ImageBanTheme */
|
2023-06-27 14:56:49 +00:00
|
|
|
protected Themelet $theme;
|
2020-01-26 13:19:35 +00:00
|
|
|
|
2019-11-03 17:19:37 +00:00
|
|
|
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onDataUpload(DataUploadEvent $event)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($event->get_arg(0) == "add") {
|
2019-11-29 01:52:24 +00:00
|
|
|
$user->ensure_authed();
|
2023-11-11 21:49:12 +00:00
|
|
|
$input = validate_input(["c_hash" => "optional,string", "c_reason" => "string", "c_image_id" => "optional,int"]);
|
2019-11-29 01:52:24 +00:00
|
|
|
$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";
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
if ($hash) {
|
|
|
|
send_event(new AddImageHashBanEvent($hash, $reason));
|
2020-10-26 15:16:21 +00:00
|
|
|
$page->flash("Post ban added");
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
if ($image) {
|
|
|
|
send_event(new ImageDeletionEvent($image));
|
2020-10-26 15:16:21 +00:00
|
|
|
$page->flash("Post deleted");
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
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()));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
} elseif ($event->get_arg(0) == "remove") {
|
2019-11-29 01:52:24 +00:00
|
|
|
$user->ensure_authed();
|
2023-11-11 21:49:12 +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()));
|
2019-05-28 16:59:38 +00:00
|
|
|
} 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());
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-02 19:54:48 +00:00
|
|
|
|
|
|
|
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event)
|
|
|
|
{
|
|
|
|
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)) {
|
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"]));
|
2019-08-02 19:54:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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_IMAGE)) {
|
2020-10-26 15:16:21 +00:00
|
|
|
$event->add_link("Post Bans", make_link("image_hash_ban/list/1"));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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())",
|
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}'");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onRemoveImageHashBan(RemoveImageHashBanEvent $event)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public function onImageAdminBlockBuilding(ImageAdminBlockBuildingEvent $event)
|
|
|
|
{
|
|
|
|
global $user;
|
2019-07-09 14:10:21 +00:00
|
|
|
if ($user->can(Permissions::BAN_IMAGE)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$event->add_part($this->theme->get_buttons_html($event->image));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// in before resolution limit plugin
|
|
|
|
public function get_priority(): int
|
|
|
|
{
|
|
|
|
return 30;
|
|
|
|
}
|
2007-10-22 00:13:57 +00:00
|
|
|
}
|