2007-04-16 11:58:25 +00:00
|
|
|
<?php
|
2010-01-05 10:11:53 +00:00
|
|
|
/*
|
|
|
|
* Name: Image Viewer
|
|
|
|
* Author: Shish
|
2010-01-05 10:32:39 +00:00
|
|
|
* Description: Allows users to see uploaded images
|
2010-01-05 10:11:53 +00:00
|
|
|
*/
|
|
|
|
|
2009-01-03 21:00:09 +00:00
|
|
|
/*
|
|
|
|
* DisplayingImageEvent:
|
|
|
|
* $image -- the image being displayed
|
|
|
|
* $page -- the page to display on
|
|
|
|
*
|
|
|
|
* Sent when an image is ready to display. Extensions who
|
|
|
|
* wish to appear on the "view" page should listen for this,
|
|
|
|
* which only appears when an image actually exists.
|
|
|
|
*/
|
2019-05-28 16:59:38 +00:00
|
|
|
class DisplayingImageEvent extends Event
|
|
|
|
{
|
2019-05-28 19:27:23 +00:00
|
|
|
/** @var Image */
|
2019-05-28 16:59:38 +00:00
|
|
|
public $image;
|
|
|
|
|
|
|
|
public function __construct(Image $image)
|
|
|
|
{
|
|
|
|
$this->image = $image;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_image(): Image
|
|
|
|
{
|
|
|
|
return $this->image;
|
|
|
|
}
|
2009-01-03 21:00:09 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class ImageInfoBoxBuildingEvent extends Event
|
|
|
|
{
|
|
|
|
/** @var array */
|
|
|
|
public $parts = [];
|
2019-05-28 19:27:23 +00:00
|
|
|
/** @var Image */
|
2019-05-28 16:59:38 +00:00
|
|
|
public $image;
|
2019-05-28 19:27:23 +00:00
|
|
|
/** @var User */
|
2019-05-28 16:59:38 +00:00
|
|
|
public $user;
|
|
|
|
|
|
|
|
public function __construct(Image $image, User $user)
|
|
|
|
{
|
|
|
|
$this->image = $image;
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function add_part(string $html, int $position=50)
|
|
|
|
{
|
|
|
|
while (isset($this->parts[$position])) {
|
|
|
|
$position++;
|
|
|
|
}
|
|
|
|
$this->parts[$position] = $html;
|
|
|
|
}
|
2007-11-14 10:11:56 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class ImageInfoSetEvent extends Event
|
|
|
|
{
|
2019-05-28 19:27:23 +00:00
|
|
|
/** @var Image */
|
2019-05-28 16:59:38 +00:00
|
|
|
public $image;
|
2007-11-14 10:11:56 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function __construct(Image $image)
|
|
|
|
{
|
|
|
|
$this->image = $image;
|
|
|
|
}
|
2007-11-14 10:11:56 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class ImageAdminBlockBuildingEvent extends Event
|
|
|
|
{
|
|
|
|
/** @var string[] */
|
|
|
|
public $parts = [];
|
2019-05-28 19:27:23 +00:00
|
|
|
/** @var ?Image */
|
2019-05-28 16:59:38 +00:00
|
|
|
public $image = null;
|
2019-05-28 19:27:23 +00:00
|
|
|
/** @var ?User */
|
2019-05-28 16:59:38 +00:00
|
|
|
public $user = null;
|
|
|
|
|
|
|
|
public function __construct(Image $image, User $user)
|
|
|
|
{
|
|
|
|
$this->image = $image;
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function add_part(string $html, int $position=50)
|
|
|
|
{
|
|
|
|
while (isset($this->parts[$position])) {
|
|
|
|
$position++;
|
|
|
|
}
|
|
|
|
$this->parts[$position] = $html;
|
|
|
|
}
|
2008-06-14 11:36:19 +00:00
|
|
|
}
|
2009-05-11 21:09:24 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class ViewImage extends Extension
|
|
|
|
{
|
|
|
|
public function onPageRequest(PageRequestEvent $event)
|
|
|
|
{
|
|
|
|
global $page, $user;
|
|
|
|
|
|
|
|
if ($event->page_matches("post/prev") || $event->page_matches("post/next")) {
|
|
|
|
$image_id = int_escape($event->get_arg(0));
|
|
|
|
|
|
|
|
if (isset($_GET['search'])) {
|
|
|
|
$search_terms = explode(' ', $_GET['search']);
|
|
|
|
$query = "#search=".url_escape($_GET['search']);
|
|
|
|
} else {
|
|
|
|
$search_terms = [];
|
|
|
|
$query = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$image = Image::by_id($image_id);
|
|
|
|
if (is_null($image)) {
|
|
|
|
$this->theme->display_error(404, "Image not found", "Image $image_id could not be found");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($event->page_matches("post/next")) {
|
|
|
|
$image = $image->get_next($search_terms);
|
|
|
|
} else {
|
|
|
|
$image = $image->get_prev($search_terms);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($image)) {
|
|
|
|
$this->theme->display_error(404, "Image not found", "No more images");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$page->set_mode("redirect");
|
|
|
|
$page->set_redirect(make_link("post/view/{$image->id}", $query));
|
|
|
|
} elseif ($event->page_matches("post/view")) {
|
2019-06-16 18:25:40 +00:00
|
|
|
if (!is_numeric($event->get_arg(0))) {
|
|
|
|
// 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'
|
2019-06-16 17:22:44 +00:00
|
|
|
$this->theme->display_error(404, "Image not found", "Invalid image ID");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$image_id = int_escape($event->get_arg(0));
|
|
|
|
|
|
|
|
$image = Image::by_id($image_id);
|
|
|
|
|
|
|
|
if (!is_null($image)) {
|
|
|
|
send_event(new DisplayingImageEvent($image));
|
|
|
|
$iabbe = new ImageAdminBlockBuildingEvent($image, $user);
|
|
|
|
send_event($iabbe);
|
|
|
|
ksort($iabbe->parts);
|
|
|
|
$this->theme->display_admin_block($page, $iabbe->parts);
|
|
|
|
} else {
|
|
|
|
$this->theme->display_error(404, "Image not found", "No image in the database has the ID #$image_id");
|
|
|
|
}
|
|
|
|
} elseif ($event->page_matches("post/set")) {
|
|
|
|
if (!isset($_POST['image_id'])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$image_id = int_escape($_POST['image_id']);
|
|
|
|
|
|
|
|
send_event(new ImageInfoSetEvent(Image::by_id($image_id)));
|
|
|
|
|
|
|
|
$page->set_mode("redirect");
|
|
|
|
$page->set_redirect(make_link("post/view/$image_id", url_escape(@$_POST['query'])));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onDisplayingImage(DisplayingImageEvent $event)
|
|
|
|
{
|
|
|
|
global $user;
|
|
|
|
$iibbe = new ImageInfoBoxBuildingEvent($event->get_image(), $user);
|
|
|
|
send_event($iibbe);
|
|
|
|
ksort($iibbe->parts);
|
|
|
|
$this->theme->display_meta_headers($event->get_image());
|
|
|
|
$this->theme->display_page($event->get_image(), $iibbe->parts);
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|