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>
|
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 {
|
|
|
|
var $user, $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.
|
|
|
|
*
|
|
|
|
* @sa TagSetEvent
|
|
|
|
* @param $user The user adding the image
|
|
|
|
* @param $image The new image to add.
|
|
|
|
*/
|
2009-01-18 14:58:49 +00:00
|
|
|
public function ImageAdditionEvent(User $user, Image $image) {
|
2009-01-03 21:00:09 +00:00
|
|
|
$this->image = $image;
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-15 01:42:18 +00:00
|
|
|
class ImageAdditionException extends SCoreException {
|
|
|
|
var $error;
|
|
|
|
|
|
|
|
public function __construct($error) {
|
|
|
|
$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 {
|
|
|
|
var $image;
|
2011-09-03 23:34:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes an image.
|
|
|
|
* Used by things like tags and comments handlers to
|
|
|
|
* clean out related rows in their tables.
|
|
|
|
*
|
|
|
|
* @param $image The image being deleted
|
|
|
|
*/
|
2009-01-18 14:58:49 +00:00
|
|
|
public function ImageDeletionEvent(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 {
|
2011-08-26 01:35:59 +00:00
|
|
|
var $id, $image;
|
2011-09-03 23:34:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces an image.
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @param $id
|
|
|
|
* The ID of the image to replace
|
|
|
|
* @param $image
|
|
|
|
* The image object of the new image to use
|
|
|
|
*/
|
2011-08-25 03:55:44 +00:00
|
|
|
public function ImageReplaceEvent($id, Image $image) {
|
|
|
|
$this->id = $id;
|
2011-08-25 00:53:53 +00:00
|
|
|
$this->image = $image;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ImageReplaceException extends SCoreException {
|
|
|
|
var $error;
|
|
|
|
|
|
|
|
public function __construct($error) {
|
|
|
|
$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 {
|
2011-09-03 23:34:46 +00:00
|
|
|
var $hash, $type;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Request a thumbnail be made for an image object
|
|
|
|
*
|
|
|
|
* @param $hash The unique hash of the image
|
|
|
|
* @param $type The type of the image
|
|
|
|
*/
|
2009-01-03 21:00:09 +00:00
|
|
|
public function ThumbnailGenerationEvent($hash, $type) {
|
|
|
|
$this->hash = $hash;
|
|
|
|
$this->type = $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ParseLinkTemplateEvent:
|
|
|
|
* $link -- the formatted link
|
|
|
|
* $original -- the formatting string, for reference
|
|
|
|
* $image -- the image who's link is being parsed
|
|
|
|
*/
|
|
|
|
class ParseLinkTemplateEvent extends Event {
|
2011-09-03 23:34:46 +00:00
|
|
|
var $link, $original, $image;
|
2009-01-03 21:00:09 +00:00
|
|
|
|
2009-01-18 14:58:49 +00:00
|
|
|
public function ParseLinkTemplateEvent($link, Image $image) {
|
2009-01-03 21:00:09 +00:00
|
|
|
$this->link = $link;
|
|
|
|
$this->original = $link;
|
|
|
|
$this->image = $image;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function replace($needle, $replace) {
|
|
|
|
$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
|
|
|
*/
|
2009-05-11 21:09:24 +00:00
|
|
|
class ImageIO extends SimpleExtension {
|
|
|
|
public function onInitExt($event) {
|
|
|
|
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'));
|
2010-01-22 17:42:00 +00:00
|
|
|
$config->set_default_string('thumb_convert_path', 'convert.exe');
|
2009-05-11 21:09:24 +00:00
|
|
|
|
2009-11-29 09:38:26 +00:00
|
|
|
$config->set_default_bool('image_show_meta', false);
|
2011-09-05 18:47:30 +00:00
|
|
|
$config->set_default_bool('image_jquery_confirm', true);
|
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');
|
2011-09-05 18:47:30 +00:00
|
|
|
$config->set_default_int('image_expires', (60*60*24*365) ); // defaults to one year
|
2009-05-11 21:09:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function onPageRequest($event) {
|
|
|
|
$num = $event->get_arg(0);
|
|
|
|
$matches = array();
|
|
|
|
if(!is_null($num) && preg_match("/(\d+)/", $num, $matches)) {
|
|
|
|
$num = $matches[1];
|
2007-07-16 13:15:56 +00:00
|
|
|
|
2009-05-11 21:09:24 +00:00
|
|
|
if($event->page_matches("image")) {
|
|
|
|
$this->send_file($num, "image");
|
|
|
|
}
|
|
|
|
else if($event->page_matches("thumb")) {
|
|
|
|
$this->send_file($num, "thumb");
|
2007-05-25 07:40:26 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2010-01-05 11:07:13 +00:00
|
|
|
if($event->page_matches("image_admin/delete")) {
|
|
|
|
global $page, $user;
|
2010-05-28 13:26:46 +00:00
|
|
|
if($user->is_admin() && 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");
|
|
|
|
$page->set_redirect(make_link("post/list"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-25 00:53:53 +00:00
|
|
|
if($event->page_matches("image_admin/replace")) {
|
|
|
|
global $page, $user;
|
|
|
|
if($user->is_admin() && isset($_POST['image_id']) && $user->check_auth_token()) {
|
|
|
|
$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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-01-05 11:07:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function onImageAdminBlockBuilding($event) {
|
|
|
|
global $user;
|
2011-09-03 21:01:27 +00:00
|
|
|
global $config;
|
|
|
|
|
2010-01-05 11:07:13 +00:00
|
|
|
if($user->is_admin()) {
|
|
|
|
$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... */
|
|
|
|
if ($user->is_admin() && $config->get_bool("upload_replace")) {
|
|
|
|
$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
|
|
|
|
2009-05-11 21:09:24 +00:00
|
|
|
public function onImageAddition($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
|
|
|
|
2009-05-11 21:09:24 +00:00
|
|
|
public function onImageDeletion($event) {
|
|
|
|
$event->image->delete();
|
|
|
|
}
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2011-08-25 00:53:53 +00:00
|
|
|
public function onImageReplace($event) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-18 21:30:52 +00:00
|
|
|
public function onUserPageBuilding($event) {
|
2011-09-03 20:03:21 +00:00
|
|
|
global $user;
|
|
|
|
global $config;
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2009-05-11 21:09:24 +00:00
|
|
|
public function onSetupBuilding($event) {
|
|
|
|
$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: ");
|
2010-07-19 12:57:27 +00:00
|
|
|
if(!in_array("OS", $_SERVER) || $_SERVER["OS"] != 'Windows_NT') {
|
|
|
|
$sb->add_bool_option("image_show_meta", "<br>Show metadata: ");
|
|
|
|
}
|
2011-09-05 18:47:30 +00:00
|
|
|
$sb->add_bool_option("image_jquery_confirm", "<br>Confirm Delete with jQuery: ");
|
|
|
|
|
|
|
|
$expires = array();
|
|
|
|
$expires['1 Minute'] = 60;
|
|
|
|
$expires['1 Hour'] = 3600;
|
|
|
|
$expires['1 Day'] = 86400;
|
|
|
|
$expires['1 Month (31 days)'] = 2678400; //(60*60*24*31)
|
|
|
|
$expires['1 Year'] = 31536000; // 365 days (60*60*24*365)
|
|
|
|
$expires['Never'] = 3153600000; // 100 years..
|
|
|
|
$sb->add_choice_option("image_expires", $expires, "<br>Image Expiration: ");
|
|
|
|
|
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 ");
|
|
|
|
|
|
|
|
$sb->add_shorthand_int_option("thumb_mem_limit", "<br>Max memory use: ");
|
|
|
|
|
|
|
|
$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 {{{
|
|
|
|
private function add_image($image) {
|
|
|
|
global $page;
|
|
|
|
global $user;
|
|
|
|
global $database;
|
|
|
|
global $config;
|
|
|
|
|
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
|
|
|
if(!empty($image->source)) {
|
|
|
|
if(!preg_match("#^(https?|ftp)://#", $image->source)) {
|
2009-07-15 01:42:18 +00:00
|
|
|
throw new ImageAdditionException("Image's source isn't a valid URL");
|
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");
|
|
|
|
if($handler == "merge") {
|
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));
|
2008-02-06 16:20:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$error = "Image <a href='".make_link("post/view/{$existing->id}")."'>{$existing->id}</a> ".
|
|
|
|
"already has hash {$image->hash}:<p>".Themelet::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(
|
|
|
|
"owner_id"=>$user->id, "owner_ip"=>$_SERVER['REMOTE_ADDR'], "filename"=>$image->filename, "filesize"=>$image->filesize,
|
|
|
|
"hash"=>$image->hash, "ext"=>$image->ext, "width"=>$image->width, "height"=>$image->height, "source"=>$image->source
|
|
|
|
)
|
|
|
|
);
|
2011-01-26 14:56:26 +00:00
|
|
|
//$database->Execute("UPDATE users SET image_count = image_count+1 WHERE id = :id ", array("id"=>$user->id));
|
2010-02-01 23:17:44 +00:00
|
|
|
if($database->engine->name == "pgsql") {
|
2011-01-26 14:56:26 +00:00
|
|
|
$image->id = $database->get_one("SELECT id FROM images WHERE hash=:hash", array("hash"=>$image->hash));
|
2010-02-01 23:17:44 +00:00
|
|
|
}
|
|
|
|
else {
|
2011-01-03 15:18:24 +00:00
|
|
|
$image->id = $database->get_last_insert_id();
|
2010-02-01 23:17:44 +00:00
|
|
|
}
|
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));
|
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 {{{
|
|
|
|
private function send_file($image_id, $type) {
|
2008-08-26 09:11:40 +00:00
|
|
|
global $config;
|
2007-04-16 11:58:25 +00:00
|
|
|
global $database;
|
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
|
|
|
$page->set_data(file_get_contents($file));
|
|
|
|
|
|
|
|
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) {
|
2011-08-30 20:24:10 +00:00
|
|
|
$page->add_http_header("HTTP/1.0 304 Not Modified",3);
|
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");
|
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 {{{
|
|
|
|
private function replace_image($id, $image) {
|
|
|
|
global $page;
|
|
|
|
global $user;
|
|
|
|
global $database;
|
|
|
|
global $config;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
if(!empty($image->source)) {
|
|
|
|
if(!preg_match("#^(https?|ftp)://#", $image->source)) {
|
|
|
|
throw new ImageReplaceException("Image's source isn't a valid URL");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
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);
|
2011-08-25 00:53:53 +00:00
|
|
|
$existing->remove_image_only(); // Actually delete the old image file from disk
|
|
|
|
|
|
|
|
// 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,
|
2011-08-25 00:53:53 +00:00
|
|
|
"ext"=>$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
|
2007-04-16 11:58:25 +00:00
|
|
|
?>
|