2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2019-06-25 23:47:06 +00:00
|
|
|
|
2023-01-10 22:44:09 +00:00
|
|
|
namespace Shimmie2;
|
|
|
|
|
2019-06-25 23:47:06 +00:00
|
|
|
abstract class TrashConfig
|
|
|
|
{
|
2021-12-14 18:32:47 +00:00
|
|
|
public const VERSION = "ext_trash_version";
|
2019-06-25 23:47:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class Trash extends Extension
|
|
|
|
{
|
2020-02-04 00:46:36 +00:00
|
|
|
/** @var TrashTheme */
|
2023-06-27 14:56:49 +00:00
|
|
|
protected Themelet $theme;
|
2020-02-04 00:46:36 +00:00
|
|
|
|
2019-06-25 23:47:06 +00:00
|
|
|
public function get_priority(): int
|
|
|
|
{
|
|
|
|
// Needs to be early to intercept delete events
|
|
|
|
return 10;
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onInitExt(InitExtEvent $event): void
|
2020-01-30 14:45:32 +00:00
|
|
|
{
|
2024-01-15 17:32:56 +00:00
|
|
|
Image::$prop_types["trash"] = ImagePropType::BOOL;
|
2020-01-30 14:45:32 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event): void
|
2019-06-25 23:47:06 +00:00
|
|
|
{
|
|
|
|
global $page, $user;
|
|
|
|
|
2024-02-11 11:34:09 +00:00
|
|
|
if ($event->page_matches("trash_restore/{image_id}", method: "POST", permission: Permissions::VIEW_TRASH)) {
|
|
|
|
$image_id = $event->get_iarg('image_id');
|
2019-06-25 23:47:06 +00:00
|
|
|
self::set_trash($image_id, false);
|
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
|
|
|
$page->set_redirect(make_link("post/view/".$image_id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-16 23:40:13 +00:00
|
|
|
private function check_permissions(Image $image): bool
|
|
|
|
{
|
|
|
|
global $user;
|
|
|
|
|
2024-01-15 17:12:36 +00:00
|
|
|
if ($image['trash'] === true && !$user->can(Permissions::VIEW_TRASH)) {
|
2020-06-16 23:40:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onImageDownloading(ImageDownloadingEvent $event): void
|
2020-06-16 23:40:13 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Deny images upon insufficient permissions.
|
|
|
|
**/
|
|
|
|
if (!$this->check_permissions($event->image)) {
|
2024-02-11 15:47:40 +00:00
|
|
|
throw new PermissionDenied("Access denied");
|
2020-06-16 23:40:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onDisplayingImage(DisplayingImageEvent $event): void
|
2019-06-25 23:47:06 +00:00
|
|
|
{
|
2020-06-16 23:40:13 +00:00
|
|
|
global $page;
|
2019-06-25 23:47:06 +00:00
|
|
|
|
2020-06-16 23:40:13 +00:00
|
|
|
if (!$this->check_permissions(($event->image))) {
|
2019-06-25 23:47:06 +00:00
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
2023-08-18 11:42:42 +00:00
|
|
|
$page->set_redirect(make_link());
|
2019-06-25 23:47:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onImageDeletion(ImageDeletionEvent $event): void
|
2019-06-25 23:47:06 +00:00
|
|
|
{
|
2024-01-15 17:12:36 +00:00
|
|
|
if ($event->force !== true && $event->image['trash'] !== true) {
|
2019-06-25 23:47:06 +00:00
|
|
|
self::set_trash($event->image->id, true);
|
|
|
|
$event->stop_processing = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event): void
|
2019-10-10 15:28:10 +00:00
|
|
|
{
|
|
|
|
global $user;
|
2023-11-11 21:49:12 +00:00
|
|
|
if ($event->parent == "posts") {
|
2019-11-02 19:57:34 +00:00
|
|
|
if ($user->can(Permissions::VIEW_TRASH)) {
|
|
|
|
$event->add_nav_link("posts_trash", new Link('/post/list/in%3Atrash/1'), "Trash", null, 60);
|
2019-10-10 15:28:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onUserBlockBuilding(UserBlockBuildingEvent $event): void
|
2023-03-20 16:47:59 +00:00
|
|
|
{
|
|
|
|
global $user;
|
|
|
|
if ($user->can(Permissions::VIEW_TRASH)) {
|
2023-08-18 12:38:55 +00:00
|
|
|
$event->add_link("Trash", search_link(["in:trash"]), 60);
|
2023-03-20 16:47:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 18:32:47 +00:00
|
|
|
public const SEARCH_REGEXP = "/^in:trash$/";
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onSearchTermParse(SearchTermParseEvent $event): void
|
2019-06-25 23:47:06 +00:00
|
|
|
{
|
2020-10-26 21:40:26 +00:00
|
|
|
global $user;
|
2019-06-25 23:47:06 +00:00
|
|
|
|
|
|
|
$matches = [];
|
|
|
|
|
|
|
|
if (is_null($event->term) && $this->no_trash_query($event->context)) {
|
2023-11-11 21:49:12 +00:00
|
|
|
$event->add_querylet(new Querylet("trash != :true", ["true" => true]));
|
2019-06-25 23:47:06 +00:00
|
|
|
}
|
|
|
|
|
2020-01-26 16:38:26 +00:00
|
|
|
if (is_null($event->term)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-06-25 23:47:06 +00:00
|
|
|
if (preg_match(self::SEARCH_REGEXP, strtolower($event->term), $matches)) {
|
2019-09-29 13:30:55 +00:00
|
|
|
if ($user->can(Permissions::VIEW_TRASH)) {
|
2023-11-11 21:49:12 +00:00
|
|
|
$event->add_querylet(new Querylet("trash = :true", ["true" => true]));
|
2019-06-25 23:47:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onHelpPageBuilding(HelpPageBuildingEvent $event): void
|
2019-08-02 20:05:49 +00:00
|
|
|
{
|
|
|
|
global $user;
|
2023-11-11 21:49:12 +00:00
|
|
|
if ($event->key === HelpPages::SEARCH) {
|
2019-09-29 13:30:55 +00:00
|
|
|
if ($user->can(Permissions::VIEW_TRASH)) {
|
2019-08-02 20:05:49 +00:00
|
|
|
$block = new Block();
|
|
|
|
$block->header = "Trash";
|
|
|
|
$block->body = $this->theme->get_help_html();
|
|
|
|
$event->add_block($block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
/**
|
|
|
|
* @param string[] $context
|
|
|
|
*/
|
2019-06-25 23:47:06 +00:00
|
|
|
private function no_trash_query(array $context): bool
|
|
|
|
{
|
|
|
|
foreach ($context as $term) {
|
|
|
|
if (preg_match(self::SEARCH_REGEXP, $term)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
public static function set_trash(int $image_id, bool $trash): void
|
2019-09-29 13:30:55 +00:00
|
|
|
{
|
2019-06-25 23:47:06 +00:00
|
|
|
global $database;
|
|
|
|
|
2019-09-29 13:30:55 +00:00
|
|
|
$database->execute(
|
|
|
|
"UPDATE images SET trash = :trash WHERE id = :id",
|
2023-11-11 21:49:12 +00:00
|
|
|
["trash" => $trash,"id" => $image_id]
|
2019-09-29 13:30:55 +00:00
|
|
|
);
|
2019-06-25 23:47:06 +00:00
|
|
|
}
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onImageAdminBlockBuilding(ImageAdminBlockBuildingEvent $event): void
|
2019-06-25 23:47:06 +00:00
|
|
|
{
|
2019-10-02 10:23:57 +00:00
|
|
|
global $user;
|
2024-01-15 17:12:36 +00:00
|
|
|
if ($event->image['trash'] === true && $user->can(Permissions::VIEW_TRASH)) {
|
2024-02-10 18:35:55 +00:00
|
|
|
$event->add_button("Restore From Trash", "trash_restore/".$event->image->id);
|
2019-06-25 23:47:06 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onBulkActionBlockBuilding(BulkActionBlockBuildingEvent $event): void
|
2019-06-25 23:47:06 +00:00
|
|
|
{
|
|
|
|
global $user;
|
|
|
|
|
2023-11-11 21:49:12 +00:00
|
|
|
if ($user->can(Permissions::VIEW_TRASH) && in_array("in:trash", $event->search_terms)) {
|
2019-09-29 13:30:55 +00:00
|
|
|
$event->add_action("bulk_trash_restore", "(U)ndelete", "u");
|
2019-06-25 23:47:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onBulkAction(BulkActionEvent $event): void
|
2019-06-25 23:47:06 +00:00
|
|
|
{
|
2019-12-15 19:47:18 +00:00
|
|
|
global $page, $user;
|
2019-06-25 23:47:06 +00:00
|
|
|
|
|
|
|
switch ($event->action) {
|
|
|
|
case "bulk_trash_restore":
|
2019-07-09 14:10:21 +00:00
|
|
|
if ($user->can(Permissions::VIEW_TRASH)) {
|
2019-06-25 23:47:06 +00:00
|
|
|
$total = 0;
|
2019-07-05 15:24:46 +00:00
|
|
|
foreach ($event->items as $image) {
|
|
|
|
self::set_trash($image->id, false);
|
2019-06-25 23:47:06 +00:00
|
|
|
$total++;
|
|
|
|
}
|
2019-12-15 19:47:18 +00:00
|
|
|
$page->flash("Restored $total items from trash");
|
2019-06-25 23:47:06 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event): void
|
2019-06-25 23:47:06 +00:00
|
|
|
{
|
2019-11-03 19:04:57 +00:00
|
|
|
global $database;
|
2019-06-25 23:47:06 +00:00
|
|
|
|
2019-11-03 19:04:57 +00:00
|
|
|
if ($this->get_version(TrashConfig::VERSION) < 1) {
|
2020-10-26 22:37:25 +00:00
|
|
|
$database->execute("ALTER TABLE images ADD COLUMN trash BOOLEAN NOT NULL DEFAULT FALSE");
|
2020-10-25 21:34:52 +00:00
|
|
|
$database->execute("CREATE INDEX images_trash_idx ON images(trash)");
|
2020-10-26 21:40:26 +00:00
|
|
|
$this->set_version(TrashConfig::VERSION, 2);
|
|
|
|
}
|
|
|
|
if ($this->get_version(TrashConfig::VERSION) < 2) {
|
|
|
|
$database->standardise_boolean("images", "trash");
|
|
|
|
$this->set_version(TrashConfig::VERSION, 2);
|
2019-06-25 23:47:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|