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/view/main.php

115 lines
4.1 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
namespace Shimmie2;
require_once "events/displaying_image_event.php";
require_once "events/image_info_box_building_event.php";
require_once "events/image_info_set_event.php";
require_once "events/image_admin_block_building_event.php";
2020-07-31 14:50:57 +00:00
use function MicroHTML\TR;
use function MicroHTML\TH;
use function MicroHTML\TD;
2009-05-11 21:09:24 +00:00
2023-12-31 01:20:36 +00:00
class ViewPost extends Extension
{
2023-12-31 01:20:36 +00:00
/** @var ViewPostTheme */
2023-06-27 14:56:49 +00:00
protected Themelet $theme;
2020-01-26 13:19:35 +00:00
public function onPageRequest(PageRequestEvent $event): void
{
global $page, $user;
2024-02-11 11:34:09 +00:00
if ($event->page_matches("post/prev/{image_id}") || $event->page_matches("post/next/{image_id}")) {
$image_id = $event->get_iarg('image_id');
2024-02-09 16:36:57 +00:00
$search = $event->get_GET('search');
if ($search) {
$search_terms = Tag::explode($search);
$query = "#search=".url_escape($search);
} else {
$search_terms = [];
$query = null;
}
$image = Image::by_id_ex($image_id);
2024-02-11 11:34:09 +00:00
if ($event->page_matches("post/next/{image_id}")) {
$image = $image->get_next($search_terms);
} else {
$image = $image->get_prev($search_terms);
}
if (is_null($image)) {
2020-10-26 15:13:47 +00:00
$this->theme->display_error(404, "Post not found", "No more posts");
return;
}
2019-06-19 01:58:28 +00:00
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("post/view/{$image->id}", $query));
2024-02-11 11:34:09 +00:00
} elseif ($event->page_matches("post/view/{image_id}")) {
if (!is_numeric($event->get_arg('image_id'))) {
// For some reason there exists some very broken mobile client
// who follows up every request to '/post/view/123' with
// '/post/view/12300000000000Image 123: tags' which spams the
// database log with 'integer out of range'
2020-10-26 15:13:47 +00:00
$this->theme->display_error(404, "Post not found", "Invalid post ID");
2019-06-16 17:22:44 +00:00
return;
}
2024-02-11 11:34:09 +00:00
$image_id = $event->get_iarg('image_id');
$image = Image::by_id_ex($image_id);
send_event(new DisplayingImageEvent($image));
} elseif ($event->page_matches("post/set", method: "POST")) {
2024-02-10 00:08:55 +00:00
$image_id = int_escape($event->req_POST('image_id'));
$image = Image::by_id_ex($image_id);
if (!$image->is_locked() || $user->can(Permissions::EDIT_IMAGE_LOCK)) {
send_event(new ImageInfoSetEvent($image, 0, only_strings($event->POST)));
$page->set_mode(PageMode::REDIRECT);
2024-02-09 16:36:57 +00:00
if ($event->get_GET('search')) {
$query = "search=" . url_escape($event->get_GET('search'));
} else {
$query = null;
}
2021-01-18 14:37:32 +00:00
$page->set_redirect(make_link("post/view/$image_id", null, $query));
} else {
2020-10-26 15:13:47 +00:00
$this->theme->display_error(403, "Post Locked", "An admin has locked this post");
}
}
}
public function onRobotsBuilding(RobotsBuildingEvent $event): void
{
// next and prev are just CPU-heavier ways of getting
// to the same images that the index shows
$event->add_disallow("post/next");
$event->add_disallow("post/prev");
}
public function onDisplayingImage(DisplayingImageEvent $event): void
{
2020-03-23 00:24:37 +00:00
global $page, $user;
$image = $event->get_image();
2020-03-23 00:21:35 +00:00
$this->theme->display_meta_headers($image);
2023-02-04 20:50:26 +00:00
$iibbe = send_event(new ImageInfoBoxBuildingEvent($image, $user));
$this->theme->display_page($image, $iibbe->get_parts());
2023-02-04 20:50:26 +00:00
$iabbe = send_event(new ImageAdminBlockBuildingEvent($image, $user, "view"));
$this->theme->display_admin_block($page, $iabbe->get_parts());
}
2020-07-31 14:50:57 +00:00
public function onImageInfoBoxBuilding(ImageInfoBoxBuildingEvent $event): void
2020-07-31 14:50:57 +00:00
{
global $config;
$image_info = $config->get_string(ImageConfig::INFO);
if ($image_info) {
$event->add_part(SHM_POST_INFO("Info", $event->image->get_info()), 85);
2020-07-31 14:50:57 +00:00
}
}
}