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

177 lines
4 KiB
PHP
Raw Normal View History

<?php
2010-01-05 10:11:53 +00:00
/*
* Name: Image Viewer
* Author: Shish
* Description: Allows users to see uploaded images
2010-01-05 10:11:53 +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.
*/
class DisplayingImageEvent extends Event {
2014-04-27 23:29:36 +00:00
/** @var \Image */
public $image;
public $page, $context;
2014-04-27 23:29:36 +00:00
/**
* @param Image $image
*/
public function __construct(Image $image) {
$this->image = $image;
}
2014-04-27 23:29:36 +00:00
/**
* @return Image
*/
public function get_image() {
return $this->image;
}
}
class ImageInfoBoxBuildingEvent extends Event {
2014-04-27 23:29:36 +00:00
/** @var array */
public $parts = array();
/** @var \Image */
public $image;
/** @var \User */
public $user;
2014-04-27 23:29:36 +00:00
/**
* @param Image $image
* @param User $user
*/
2014-03-22 09:00:59 +00:00
public function __construct(Image $image, User $user) {
$this->image = $image;
$this->user = $user;
}
2014-04-27 23:29:36 +00:00
/**
* @param string $html
* @param int $position
*/
public function add_part($html, $position=50) {
while(isset($this->parts[$position])) $position++;
$this->parts[$position] = $html;
}
}
class ImageInfoSetEvent extends Event {
2014-04-27 23:29:36 +00:00
/** @var \Image */
public $image;
2014-04-27 23:29:36 +00:00
/**
* @param Image $image
*/
2014-03-22 09:00:59 +00:00
public function __construct(Image $image) {
2009-01-18 01:07:06 +00:00
$this->image = $image;
}
}
class ImageAdminBlockBuildingEvent extends Event {
2014-04-27 23:29:36 +00:00
/** @var array */
var $parts = array();
2014-04-27 23:29:36 +00:00
/** @var \Image|null */
public $image = null;
/** @var null|\User */
public $user = null;
2014-04-27 23:29:36 +00:00
/**
* @param Image $image
* @param User $user
*/
2014-03-22 09:00:59 +00:00
public function __construct(Image $image, User $user) {
$this->image = $image;
$this->user = $user;
}
2014-04-27 23:29:36 +00:00
/**
* @param string $html
* @param int $position
*/
2012-02-02 14:14:33 +00:00
public function add_part(/*string*/ $html, /*int*/ $position=50) {
while(isset($this->parts[$position])) $position++;
$this->parts[$position] = $html;
}
}
2009-05-11 21:09:24 +00:00
class ViewImage extends Extension {
public function onPageRequest(PageRequestEvent $event) {
2010-06-17 18:44:05 +00:00
global $page, $user;
2014-03-02 21:58:45 +00:00
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']);
2010-02-09 10:08:08 +00:00
$query = "#search=".url_escape($_GET['search']);
}
else {
$search_terms = array();
$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));
}
2014-03-02 21:58:45 +00:00
else if($event->page_matches("post/view")) {
$image_id = int_escape($event->get_arg(0));
2009-01-04 19:18:37 +00:00
$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");
}
}
2014-03-02 21:58:45 +00:00
else if($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)));
2009-01-04 19:18:37 +00:00
$page->set_mode("redirect");
2012-03-09 18:14:14 +00:00
$page->set_redirect(make_link("post/view/$image_id", url_escape(@$_POST['query'])));
}
}
public function onDisplayingImage(DisplayingImageEvent $event) {
2010-06-17 18:44:05 +00:00
global $user;
$iibbe = new ImageInfoBoxBuildingEvent($event->get_image(), $user);
send_event($iibbe);
ksort($iibbe->parts);
$this->theme->display_page($event->get_image(), $iibbe->parts);
}
}