2007-04-16 11:58:25 +00:00
|
|
|
<?php
|
2010-01-05 10:11:53 +00:00
|
|
|
/*
|
|
|
|
* Name: Image Manager
|
2011-09-03 23:34:46 +00:00
|
|
|
* Author: Shish <webmaster@shishnet.org>
|
|
|
|
* Modified by: jgen <jgen.tech@gmail.com>
|
2012-02-10 05:08:03 +00:00
|
|
|
* Link: http://code.shishnet.org/shimmie2/
|
2010-01-05 10:11:53 +00:00
|
|
|
* Description: Handle the image database
|
2010-01-05 13:13:11 +00:00
|
|
|
* Visibility: admin
|
2010-01-05 10:11:53 +00:00
|
|
|
*/
|
|
|
|
|
2011-09-03 23:34:46 +00:00
|
|
|
/**
|
|
|
|
* An image is being added to the database.
|
2009-01-03 21:00:09 +00:00
|
|
|
*/
|
|
|
|
class ImageAdditionEvent extends Event {
|
2016-06-19 22:05:57 +00:00
|
|
|
/** @var User */
|
|
|
|
public $user;
|
|
|
|
|
|
|
|
/** @var Image */
|
2014-04-27 19:33:57 +00:00
|
|
|
public $image;
|
2011-09-03 23:34:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Inserts a new image into the database with its associated
|
|
|
|
* information. Also calls TagSetEvent to set the tags for
|
|
|
|
* this new image.
|
|
|
|
*
|
2014-04-27 19:33:57 +00:00
|
|
|
* @see TagSetEvent
|
|
|
|
* @param Image $image The new image to add.
|
2011-09-03 23:34:46 +00:00
|
|
|
*/
|
2014-03-22 09:00:59 +00:00
|
|
|
public function __construct(Image $image) {
|
2009-01-03 21:00:09 +00:00
|
|
|
$this->image = $image;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-15 01:42:18 +00:00
|
|
|
class ImageAdditionException extends SCoreException {
|
2016-06-19 22:05:57 +00:00
|
|
|
public $error;
|
2009-07-15 01:42:18 +00:00
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function __construct(string $error) {
|
2009-07-15 01:42:18 +00:00
|
|
|
$this->error = $error;
|
|
|
|
}
|
|
|
|
}
|
2009-01-03 21:00:09 +00:00
|
|
|
|
2011-09-03 23:34:46 +00:00
|
|
|
/**
|
|
|
|
* An image is being deleted.
|
2009-01-03 21:00:09 +00:00
|
|
|
*/
|
|
|
|
class ImageDeletionEvent extends Event {
|
2014-04-27 19:33:57 +00:00
|
|
|
/** @var \Image */
|
|
|
|
public $image;
|
2011-09-03 23:34:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes an image.
|
2014-04-27 19:33:57 +00:00
|
|
|
*
|
2011-09-03 23:34:46 +00:00
|
|
|
* Used by things like tags and comments handlers to
|
|
|
|
* clean out related rows in their tables.
|
|
|
|
*
|
2014-04-27 19:33:57 +00:00
|
|
|
* @param Image $image The image being deleted.
|
2011-09-03 23:34:46 +00:00
|
|
|
*/
|
2014-03-22 09:00:59 +00:00
|
|
|
public function __construct(Image $image) {
|
2009-01-03 21:00:09 +00:00
|
|
|
$this->image = $image;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-03 23:34:46 +00:00
|
|
|
/**
|
|
|
|
* An image is being replaced.
|
2011-08-25 00:53:53 +00:00
|
|
|
*/
|
|
|
|
class ImageReplaceEvent extends Event {
|
2014-04-27 19:33:57 +00:00
|
|
|
/** @var int */
|
|
|
|
public $id;
|
|
|
|
/** @var \Image */
|
|
|
|
public $image;
|
2011-09-03 23:34:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces an image.
|
2014-04-27 19:33:57 +00:00
|
|
|
*
|
2011-09-03 23:34:46 +00:00
|
|
|
* Updates an existing ID in the database to use a new image
|
|
|
|
* file, leaving the tags and such unchanged. Also removes
|
|
|
|
* the old image file and thumbnail from the disk.
|
|
|
|
*
|
2014-04-27 19:33:57 +00:00
|
|
|
* @param int $id The ID of the image to replace.
|
|
|
|
* @param Image $image The image object of the new image to use.
|
2011-09-03 23:34:46 +00:00
|
|
|
*/
|
2017-09-19 17:55:43 +00:00
|
|
|
public function __construct(int $id, Image $image) {
|
2011-08-25 03:55:44 +00:00
|
|
|
$this->id = $id;
|
2011-08-25 00:53:53 +00:00
|
|
|
$this->image = $image;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ImageReplaceException extends SCoreException {
|
2014-04-27 19:33:57 +00:00
|
|
|
/** @var string */
|
|
|
|
public $error;
|
2011-08-25 00:53:53 +00:00
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function __construct(string $error) {
|
2011-08-25 00:53:53 +00:00
|
|
|
$this->error = $error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-03 23:34:46 +00:00
|
|
|
/**
|
|
|
|
* Request a thumbnail be made for an image object.
|
2009-01-03 21:00:09 +00:00
|
|
|
*/
|
|
|
|
class ThumbnailGenerationEvent extends Event {
|
2014-04-27 19:33:57 +00:00
|
|
|
/** @var string */
|
|
|
|
public $hash;
|
|
|
|
/** @var string */
|
|
|
|
public $type;
|
|
|
|
/** @var bool */
|
|
|
|
public $force;
|
2012-01-14 09:15:13 +00:00
|
|
|
|
2011-09-03 23:34:46 +00:00
|
|
|
/**
|
|
|
|
* Request a thumbnail be made for an image object
|
|
|
|
*
|
2014-04-27 19:33:57 +00:00
|
|
|
* @param string $hash The unique hash of the image
|
|
|
|
* @param string $type The type of the image
|
|
|
|
* @param bool $force Regenerate the thumbnail even if one already exists
|
2011-09-03 23:34:46 +00:00
|
|
|
*/
|
2017-09-19 17:55:43 +00:00
|
|
|
public function __construct(string $hash, string $type, bool $force=false) {
|
2009-01-03 21:00:09 +00:00
|
|
|
$this->hash = $hash;
|
|
|
|
$this->type = $type;
|
2012-01-12 01:54:27 +00:00
|
|
|
$this->force = $force;
|
2009-01-03 21:00:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ParseLinkTemplateEvent:
|
|
|
|
* $link -- the formatted link
|
|
|
|
* $original -- the formatting string, for reference
|
|
|
|
* $image -- the image who's link is being parsed
|
|
|
|
*/
|
|
|
|
class ParseLinkTemplateEvent extends Event {
|
2014-04-27 19:33:57 +00:00
|
|
|
/** @var string */
|
|
|
|
public $link;
|
|
|
|
/** @var string */
|
|
|
|
public $original;
|
|
|
|
/** @var \Image */
|
|
|
|
public $image;
|
2009-01-03 21:00:09 +00:00
|
|
|
|
2014-04-27 19:33:57 +00:00
|
|
|
/**
|
|
|
|
* @param string $link The formatted link
|
|
|
|
* @param Image $image The image who's link is being parsed
|
|
|
|
*/
|
2017-09-19 17:55:43 +00:00
|
|
|
public function __construct(string $link, Image $image) {
|
2009-01-03 21:00:09 +00:00
|
|
|
$this->link = $link;
|
|
|
|
$this->original = $link;
|
|
|
|
$this->image = $image;
|
|
|
|
}
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function replace(string $needle, string $replace) {
|
2009-01-03 21:00:09 +00:00
|
|
|
$this->link = str_replace($needle, $replace, $this->link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-03 23:34:46 +00:00
|
|
|
/**
|
|
|
|
* A class to handle adding / getting / removing image files from the disk.
|
2007-04-16 11:58:25 +00:00
|
|
|
*/
|
2012-02-08 12:07:01 +00:00
|
|
|
class ImageIO extends Extension {
|
2012-02-02 08:07:57 +00:00
|
|
|
public function onInitExt(InitExtEvent $event) {
|
2009-05-11 21:09:24 +00:00
|
|
|
global $config;
|
|
|
|
$config->set_default_int('thumb_width', 192);
|
|
|
|
$config->set_default_int('thumb_height', 192);
|
|
|
|
$config->set_default_int('thumb_quality', 75);
|
|
|
|
$config->set_default_int('thumb_mem_limit', parse_shorthand_int('8MB'));
|
2012-08-22 01:46:34 +00:00
|
|
|
$config->set_default_string('thumb_convert_path', 'convert');
|
2009-05-11 21:09:24 +00:00
|
|
|
|
2012-03-14 20:24:34 +00:00
|
|
|
if(function_exists("exif_read_data")) {
|
|
|
|
$config->set_default_bool('image_show_meta', false);
|
|
|
|
}
|
2009-05-11 21:09:24 +00:00
|
|
|
$config->set_default_string('image_ilink', '');
|
|
|
|
$config->set_default_string('image_tlink', '');
|
|
|
|
$config->set_default_string('image_tip', '$tags // $size // $filesize');
|
|
|
|
$config->set_default_string('upload_collision_handler', 'error');
|
2012-04-01 15:58:42 +00:00
|
|
|
$config->set_default_int('image_expires', (60*60*24*31) ); // defaults to one month
|
2009-05-11 21:09:24 +00:00
|
|
|
}
|
|
|
|
|
2012-02-02 08:07:57 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event) {
|
2012-04-01 15:06:11 +00:00
|
|
|
if($event->page_matches("image/delete")) {
|
2010-01-05 11:07:13 +00:00
|
|
|
global $page, $user;
|
2012-02-07 15:15:18 +00:00
|
|
|
if($user->can("delete_image") && isset($_POST['image_id']) && $user->check_auth_token()) {
|
2010-01-05 11:07:13 +00:00
|
|
|
$image = Image::by_id($_POST['image_id']);
|
|
|
|
if($image) {
|
|
|
|
send_event(new ImageDeletionEvent($image));
|
|
|
|
$page->set_mode("redirect");
|
2012-03-15 05:48:42 +00:00
|
|
|
if(isset($_SERVER['HTTP_REFERER']) && !strstr($_SERVER['HTTP_REFERER'], 'post/view')) {
|
|
|
|
$page->set_redirect($_SERVER['HTTP_REFERER']);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$page->set_redirect(make_link("post/list"));
|
|
|
|
}
|
2010-01-05 11:07:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-01 15:06:11 +00:00
|
|
|
else if($event->page_matches("image/replace")) {
|
2011-08-25 00:53:53 +00:00
|
|
|
global $page, $user;
|
2012-02-07 15:15:18 +00:00
|
|
|
if($user->can("replace_image") && isset($_POST['image_id']) && $user->check_auth_token()) {
|
2011-08-25 00:53:53 +00:00
|
|
|
$image = Image::by_id($_POST['image_id']);
|
|
|
|
if($image) {
|
|
|
|
$page->set_mode("redirect");
|
|
|
|
$page->set_redirect(make_link('upload/replace/'.$image->id));
|
|
|
|
} else {
|
|
|
|
/* Invalid image ID */
|
2011-08-25 03:55:44 +00:00
|
|
|
throw new ImageReplaceException("Image to replace does not exist.");
|
2011-08-25 00:53:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-01 15:06:11 +00:00
|
|
|
else if($event->page_matches("image")) {
|
|
|
|
$num = int_escape($event->get_arg(0));
|
|
|
|
$this->send_file($num, "image");
|
|
|
|
}
|
|
|
|
else if($event->page_matches("thumb")) {
|
|
|
|
$num = int_escape($event->get_arg(0));
|
|
|
|
$this->send_file($num, "thumb");
|
|
|
|
}
|
2010-01-05 11:07:13 +00:00
|
|
|
}
|
|
|
|
|
2012-02-02 08:07:57 +00:00
|
|
|
public function onImageAdminBlockBuilding(ImageAdminBlockBuildingEvent $event) {
|
2015-09-12 10:43:28 +00:00
|
|
|
global $user;
|
2011-09-03 21:01:27 +00:00
|
|
|
|
2012-02-07 15:15:18 +00:00
|
|
|
if($user->can("delete_image")) {
|
2010-01-05 11:07:13 +00:00
|
|
|
$event->add_part($this->theme->get_deleter_html($event->image->id));
|
|
|
|
}
|
2011-09-03 20:48:33 +00:00
|
|
|
/* In the future, could perhaps allow users to replace images that they own as well... */
|
2012-03-19 11:07:13 +00:00
|
|
|
if ($user->can("replace_image")) {
|
2011-09-03 20:48:33 +00:00
|
|
|
$event->add_part($this->theme->get_replace_html($event->image->id));
|
|
|
|
}
|
2009-05-11 21:09:24 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2012-02-02 08:07:57 +00:00
|
|
|
public function onImageAddition(ImageAdditionEvent $event) {
|
2009-07-15 01:42:18 +00:00
|
|
|
try {
|
|
|
|
$this->add_image($event->image);
|
|
|
|
}
|
|
|
|
catch(ImageAdditionException $e) {
|
|
|
|
throw new UploadException($e->error);
|
|
|
|
}
|
2009-05-11 21:09:24 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2012-02-02 08:07:57 +00:00
|
|
|
public function onImageDeletion(ImageDeletionEvent $event) {
|
2009-05-11 21:09:24 +00:00
|
|
|
$event->image->delete();
|
|
|
|
}
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2012-02-02 08:07:57 +00:00
|
|
|
public function onImageReplace(ImageReplaceEvent $event) {
|
2011-08-25 00:53:53 +00:00
|
|
|
try {
|
2011-08-25 03:55:44 +00:00
|
|
|
$this->replace_image($event->id, $event->image);
|
2011-08-25 00:53:53 +00:00
|
|
|
}
|
|
|
|
catch(ImageReplaceException $e) {
|
|
|
|
throw new UploadException($e->error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-02 08:07:57 +00:00
|
|
|
public function onUserPageBuilding(UserPageBuildingEvent $event) {
|
2009-08-18 21:30:52 +00:00
|
|
|
$u_id = url_escape($event->display_user->id);
|
|
|
|
$i_image_count = Image::count_images(array("user_id={$event->display_user->id}"));
|
|
|
|
$i_days_old = ((time() - strtotime($event->display_user->join_date)) / 86400) + 1;
|
|
|
|
$h_image_rate = sprintf("%.1f", ($i_image_count / $i_days_old));
|
|
|
|
$images_link = make_link("post/list/user_id=$u_id/1");
|
|
|
|
$event->add_stats("<a href='$images_link'>Images uploaded</a>: $i_image_count, $h_image_rate per day");
|
|
|
|
}
|
|
|
|
|
2012-02-02 08:07:57 +00:00
|
|
|
public function onSetupBuilding(SetupBuildingEvent $event) {
|
2012-03-19 10:25:36 +00:00
|
|
|
global $config;
|
|
|
|
|
2009-05-11 21:09:24 +00:00
|
|
|
$sb = new SetupBlock("Image Options");
|
|
|
|
$sb->position = 30;
|
|
|
|
// advanced only
|
|
|
|
//$sb->add_text_option("image_ilink", "Image link: ");
|
|
|
|
//$sb->add_text_option("image_tlink", "<br>Thumbnail link: ");
|
|
|
|
$sb->add_text_option("image_tip", "Image tooltip: ");
|
|
|
|
$sb->add_choice_option("upload_collision_handler", array('Error'=>'error', 'Merge'=>'merge'), "<br>Upload collision handler: ");
|
2012-03-15 05:34:55 +00:00
|
|
|
if(function_exists("exif_read_data")) {
|
2010-07-19 12:57:27 +00:00
|
|
|
$sb->add_bool_option("image_show_meta", "<br>Show metadata: ");
|
|
|
|
}
|
2012-03-19 10:25:36 +00:00
|
|
|
|
2009-05-11 21:09:24 +00:00
|
|
|
$event->panel->add_block($sb);
|
|
|
|
|
|
|
|
$thumbers = array();
|
|
|
|
$thumbers['Built-in GD'] = "gd";
|
|
|
|
$thumbers['ImageMagick'] = "convert";
|
|
|
|
|
|
|
|
$sb = new SetupBlock("Thumbnailing");
|
|
|
|
$sb->add_choice_option("thumb_engine", $thumbers, "Engine: ");
|
|
|
|
|
|
|
|
$sb->add_label("<br>Size ");
|
|
|
|
$sb->add_int_option("thumb_width");
|
|
|
|
$sb->add_label(" x ");
|
|
|
|
$sb->add_int_option("thumb_height");
|
|
|
|
$sb->add_label(" px at ");
|
|
|
|
$sb->add_int_option("thumb_quality");
|
|
|
|
$sb->add_label(" % quality ");
|
2012-08-22 02:00:25 +00:00
|
|
|
|
|
|
|
if($config->get_string("thumb_engine") == "convert") {
|
|
|
|
$sb->add_label("<br>ImageMagick Binary: ");
|
|
|
|
$sb->add_text_option("thumb_convert_path");
|
|
|
|
}
|
2009-05-11 21:09:24 +00:00
|
|
|
|
2012-05-22 11:26:47 +00:00
|
|
|
if($config->get_string("thumb_engine") == "gd") {
|
|
|
|
$sb->add_shorthand_int_option("thumb_mem_limit", "<br>Max memory use: ");
|
|
|
|
}
|
2009-05-11 21:09:24 +00:00
|
|
|
|
|
|
|
$event->panel->add_block($sb);
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2009-05-11 21:09:24 +00:00
|
|
|
|
|
|
|
|
2007-04-16 11:58:25 +00:00
|
|
|
// add image {{{
|
2014-03-30 12:26:48 +00:00
|
|
|
private function add_image(Image $image) {
|
2015-09-12 10:43:28 +00:00
|
|
|
global $user, $database, $config;
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2007-08-06 18:45:27 +00:00
|
|
|
/*
|
|
|
|
* Validate things
|
|
|
|
*/
|
2007-10-27 23:53:48 +00:00
|
|
|
if(strlen(trim($image->source)) == 0) {
|
|
|
|
$image->source = null;
|
|
|
|
}
|
2007-08-06 18:45:27 +00:00
|
|
|
|
2007-04-16 11:58:25 +00:00
|
|
|
/*
|
|
|
|
* Check for an existing image
|
|
|
|
*/
|
2009-05-11 14:04:33 +00:00
|
|
|
$existing = Image::by_hash($image->hash);
|
2007-05-01 12:38:28 +00:00
|
|
|
if(!is_null($existing)) {
|
2008-02-06 16:20:43 +00:00
|
|
|
$handler = $config->get_string("upload_collision_handler");
|
2012-03-05 09:31:32 +00:00
|
|
|
if($handler == "merge" || isset($_GET['update'])) {
|
2008-04-11 05:17:51 +00:00
|
|
|
$merged = array_merge($image->get_tag_array(), $existing->get_tag_array());
|
2009-01-18 14:58:49 +00:00
|
|
|
send_event(new TagSetEvent($existing, $merged));
|
2015-09-20 17:37:36 +00:00
|
|
|
if(isset($_GET['rating']) && isset($_GET['update']) && ext_is_live("Ratings")){
|
2012-08-18 18:45:39 +00:00
|
|
|
send_event(new RatingSetEvent($existing, $_GET['rating']));
|
2012-03-08 19:13:53 +00:00
|
|
|
}
|
|
|
|
if(isset($_GET['source']) && isset($_GET['update'])){
|
|
|
|
send_event(new SourceSetEvent($existing, $_GET['source']));
|
|
|
|
}
|
2008-02-06 16:20:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$error = "Image <a href='".make_link("post/view/{$existing->id}")."'>{$existing->id}</a> ".
|
2012-08-18 18:47:57 +00:00
|
|
|
"already has hash {$image->hash}:<p>".$this->theme->build_thumb_html($existing);
|
2009-07-15 01:42:18 +00:00
|
|
|
throw new ImageAdditionException($error);
|
2008-02-06 16:20:43 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// actually insert the info
|
2007-05-23 22:19:12 +00:00
|
|
|
$database->Execute(
|
2007-07-20 23:24:32 +00:00
|
|
|
"INSERT INTO images(
|
|
|
|
owner_id, owner_ip, filename, filesize,
|
2011-01-01 16:28:04 +00:00
|
|
|
hash, ext, width, height, posted, source
|
|
|
|
)
|
|
|
|
VALUES (
|
|
|
|
:owner_id, :owner_ip, :filename, :filesize,
|
|
|
|
:hash, :ext, :width, :height, now(), :source
|
|
|
|
)",
|
|
|
|
array(
|
2012-11-24 20:01:14 +00:00
|
|
|
"owner_id"=>$user->id, "owner_ip"=>$_SERVER['REMOTE_ADDR'], "filename"=>substr($image->filename, 0, 60), "filesize"=>$image->filesize,
|
2014-02-23 22:27:52 +00:00
|
|
|
"hash"=>$image->hash, "ext"=>strtolower($image->ext), "width"=>$image->width, "height"=>$image->height, "source"=>$image->source
|
2011-01-01 16:28:04 +00:00
|
|
|
)
|
|
|
|
);
|
2012-03-08 02:55:04 +00:00
|
|
|
$image->id = $database->get_last_insert_id('images_id_seq');
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2009-05-08 12:19:52 +00:00
|
|
|
log_info("image", "Uploaded Image #{$image->id} ({$image->hash})");
|
2009-05-08 10:52:29 +00:00
|
|
|
|
2010-02-03 23:54:43 +00:00
|
|
|
# at this point in time, the image's tags haven't really been set,
|
|
|
|
# and so, having $image->tag_array set to something is a lie (but
|
|
|
|
# a useful one, as we want to know what the tags are /supposed/ to
|
|
|
|
# be). Here we correct the lie, by first nullifying the wrong tags
|
|
|
|
# then using the standard mechanism to set them properly.
|
|
|
|
$tags_to_set = $image->get_tag_array();
|
|
|
|
$image->tag_array = array();
|
|
|
|
send_event(new TagSetEvent($image, $tags_to_set));
|
2012-02-26 15:24:02 +00:00
|
|
|
|
2017-03-12 08:29:10 +00:00
|
|
|
if($image->source !== null) {
|
2012-02-26 15:24:02 +00:00
|
|
|
log_info("core-image", "Source for Image #{$image->id} set to: {$image->source}");
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2011-08-25 00:53:53 +00:00
|
|
|
// }}} end add
|
|
|
|
|
2007-04-16 11:58:25 +00:00
|
|
|
// fetch image {{{
|
2017-09-19 17:55:43 +00:00
|
|
|
private function send_file(int $image_id, string $type) {
|
2008-08-26 09:11:40 +00:00
|
|
|
global $config;
|
2009-05-11 14:04:33 +00:00
|
|
|
$image = Image::by_id($image_id);
|
2007-04-16 11:58:25 +00:00
|
|
|
|
|
|
|
global $page;
|
|
|
|
if(!is_null($image)) {
|
|
|
|
$page->set_mode("data");
|
|
|
|
if($type == "thumb") {
|
|
|
|
$page->set_type("image/jpeg");
|
|
|
|
$file = $image->get_thumb_filename();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$page->set_type($image->get_mime_type());
|
|
|
|
$file = $image->get_image_filename();
|
|
|
|
}
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2007-04-16 11:58:25 +00:00
|
|
|
if(isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
|
|
|
|
$if_modified_since = preg_replace('/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$if_modified_since = "";
|
|
|
|
}
|
|
|
|
$gmdate_mod = gmdate('D, d M Y H:i:s', filemtime($file)) . ' GMT';
|
|
|
|
|
|
|
|
if($if_modified_since == $gmdate_mod) {
|
2015-08-02 18:07:11 +00:00
|
|
|
$page->set_code(304);
|
2012-09-22 22:10:29 +00:00
|
|
|
$page->set_data("");
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
else {
|
2011-08-30 20:24:10 +00:00
|
|
|
$page->add_http_header("Last-Modified: $gmdate_mod");
|
2012-09-22 22:10:29 +00:00
|
|
|
$page->set_data(file_get_contents($file));
|
2011-09-05 18:47:30 +00:00
|
|
|
|
|
|
|
if ( $config->get_int("image_expires") ) {
|
|
|
|
$expires = date(DATE_RFC1123, time() + $config->get_int("image_expires"));
|
|
|
|
} else {
|
|
|
|
$expires = 'Fri, 2 Sep 2101 12:42:42 GMT'; // War was beginning
|
|
|
|
}
|
|
|
|
$page->add_http_header('Expires: '.$expires);
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$page->set_title("Not Found");
|
|
|
|
$page->set_heading("Not Found");
|
2008-04-30 12:39:40 +00:00
|
|
|
$page->add_block(new Block("Navigation", "<a href='".make_link()."'>Index</a>", "left", 0));
|
2007-06-30 01:19:11 +00:00
|
|
|
$page->add_block(new Block("Image not in database",
|
2007-04-16 11:58:25 +00:00
|
|
|
"The requested image was not found in the database"));
|
|
|
|
}
|
|
|
|
}
|
2011-08-25 00:53:53 +00:00
|
|
|
// }}} end fetch
|
|
|
|
|
|
|
|
// replace image {{{
|
2017-09-19 17:55:43 +00:00
|
|
|
private function replace_image(int $id, Image $image) {
|
2011-08-25 00:53:53 +00:00
|
|
|
global $database;
|
2014-03-30 12:26:48 +00:00
|
|
|
|
2011-08-26 01:35:59 +00:00
|
|
|
/* Check to make sure the image exists. */
|
2011-08-25 00:53:53 +00:00
|
|
|
$existing = Image::by_id($id);
|
|
|
|
|
|
|
|
if(is_null($existing)) {
|
|
|
|
throw new ImageReplaceException("Image to replace does not exist!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(strlen(trim($image->source)) == 0) {
|
2011-08-26 01:35:59 +00:00
|
|
|
$image->source = $existing->get_source();
|
2011-08-25 00:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
This step could be optional, ie: perhaps move the image somewhere
|
|
|
|
and have it stored in a 'replaced images' list that could be
|
|
|
|
inspected later by an admin?
|
2011-08-26 01:35:59 +00:00
|
|
|
*/
|
|
|
|
log_debug("image", "Removing image with hash ".$existing->hash);
|
2014-04-25 02:29:29 +00:00
|
|
|
$existing->remove_image_only(); // Actually delete the old image file from disk
|
2011-08-25 00:53:53 +00:00
|
|
|
|
|
|
|
// Update the data in the database.
|
|
|
|
$database->Execute(
|
|
|
|
"UPDATE images SET
|
|
|
|
filename = :filename, filesize = :filesize, hash = :hash,
|
|
|
|
ext = :ext, width = :width, height = :height, source = :source
|
|
|
|
WHERE
|
|
|
|
id = :id
|
|
|
|
",
|
|
|
|
array(
|
2011-09-04 14:30:20 +00:00
|
|
|
"filename"=>$image->filename, "filesize"=>$image->filesize, "hash"=>$image->hash,
|
2014-02-23 22:27:52 +00:00
|
|
|
"ext"=>strtolower($image->ext), "width"=>$image->width, "height"=>$image->height, "source"=>$image->source,
|
2011-08-26 01:35:59 +00:00
|
|
|
"id"=>$id
|
2011-08-25 00:53:53 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2011-08-26 01:35:59 +00:00
|
|
|
log_info("image", "Replaced Image #{$id} with ({$image->hash})");
|
2011-08-25 00:53:53 +00:00
|
|
|
}
|
|
|
|
// }}} end replace
|
|
|
|
|
|
|
|
|
|
|
|
} // end of class ImageIO
|
2014-04-25 02:28:53 +00:00
|
|
|
|