2007-04-16 11:58:25 +00:00
|
|
|
<?php
|
2009-01-03 21:00:09 +00:00
|
|
|
/*
|
|
|
|
* ImageAdditionEvent:
|
|
|
|
* $user -- the user adding the image
|
|
|
|
* $image -- the image being added
|
|
|
|
*
|
|
|
|
* An image is being added to the database
|
|
|
|
*/
|
|
|
|
class ImageAdditionEvent extends Event {
|
|
|
|
var $user, $image;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ImageDeletionEvent:
|
|
|
|
* $image -- the image being deleted
|
|
|
|
*
|
|
|
|
* An image is being deleted. Used by things like tags
|
|
|
|
* and comments handlers to clean out related rows in
|
|
|
|
* their tables
|
|
|
|
*/
|
|
|
|
class ImageDeletionEvent extends Event {
|
|
|
|
var $image;
|
|
|
|
|
2009-01-18 14:58:49 +00:00
|
|
|
public function ImageDeletionEvent(Image $image) {
|
2009-01-03 21:00:09 +00:00
|
|
|
$this->image = $image;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ThumbnailGenerationEvent:
|
|
|
|
* Request a thumb be made for an image
|
|
|
|
*/
|
|
|
|
class ThumbnailGenerationEvent extends Event {
|
|
|
|
var $hash;
|
|
|
|
var $type;
|
|
|
|
|
|
|
|
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 {
|
|
|
|
var $link, $original;
|
|
|
|
var $image;
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-16 11:58:25 +00:00
|
|
|
/*
|
|
|
|
* A class to handle adding / getting / removing image
|
|
|
|
* files from the disk
|
|
|
|
*/
|
2008-08-23 12:08:19 +00:00
|
|
|
class ImageIO implements Extension {
|
2007-04-16 11:58:25 +00:00
|
|
|
// event handling {{{
|
2008-08-23 12:08:19 +00:00
|
|
|
public function receive_event(Event $event) {
|
2008-08-22 09:41:30 +00:00
|
|
|
if($event instanceof InitExtEvent) {
|
2007-07-16 13:15:56 +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);
|
2008-12-27 10:17:53 +00:00
|
|
|
$config->set_default_int('thumb_mem_limit', parse_shorthand_int('8MB'));
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2008-12-27 10:17:53 +00:00
|
|
|
$config->set_default_string('image_ilink', '');
|
|
|
|
$config->set_default_string('image_tlink', '');
|
2007-08-01 16:19:25 +00:00
|
|
|
$config->set_default_string('image_tip', '$tags // $size // $filesize');
|
2008-02-06 16:20:43 +00:00
|
|
|
$config->set_default_string('upload_collision_handler', 'error');
|
2007-07-16 13:15:56 +00:00
|
|
|
}
|
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if($event instanceof PageRequestEvent) {
|
2007-05-28 21:40:26 +00:00
|
|
|
$num = $event->get_arg(0);
|
|
|
|
$matches = array();
|
|
|
|
if(!is_null($num) && preg_match("/(\d+)/", $num, $matches)) {
|
|
|
|
$num = $matches[1];
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2008-09-06 17:48:03 +00:00
|
|
|
if($event->page_matches("image")) {
|
2007-05-28 21:40:26 +00:00
|
|
|
$this->send_file($num, "image");
|
|
|
|
}
|
2008-09-06 17:48:03 +00:00
|
|
|
else if($event->page_matches("thumb")) {
|
2007-05-28 21:40:26 +00:00
|
|
|
$this->send_file($num, "thumb");
|
|
|
|
}
|
2007-05-25 07:40:26 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if($event instanceof ImageAdditionEvent) {
|
2007-08-02 20:49:25 +00:00
|
|
|
$error = $this->add_image($event->image);
|
2009-01-04 14:01:59 +00:00
|
|
|
if(!empty($error)) throw new UploadException($error);
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if($event instanceof ImageDeletionEvent) {
|
2008-08-26 09:11:40 +00:00
|
|
|
$event->image->delete();
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if($event instanceof SetupBuildingEvent) {
|
2007-07-16 22:00:58 +00:00
|
|
|
$sb = new SetupBlock("Image Options");
|
|
|
|
$sb->position = 30;
|
2008-12-27 10:17:53 +00:00
|
|
|
// 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: ");
|
2008-02-06 16:20:43 +00:00
|
|
|
$sb->add_choice_option("upload_collision_handler", array('Error'=>'error', 'Merge'=>'merge'), "<br>Upload collision handler: ");
|
2007-07-16 22:00:58 +00:00
|
|
|
$event->panel->add_block($sb);
|
|
|
|
|
2007-04-16 11:58:25 +00:00
|
|
|
$thumbers = array();
|
|
|
|
$thumbers['Built-in GD'] = "gd";
|
|
|
|
$thumbers['ImageMagick'] = "convert";
|
|
|
|
|
|
|
|
$sb = new SetupBlock("Thumbnailing");
|
2007-05-08 20:36:02 +00:00
|
|
|
$sb->add_choice_option("thumb_engine", $thumbers, "Engine: ");
|
2007-04-16 11:58:25 +00:00
|
|
|
|
|
|
|
$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 ");
|
|
|
|
|
2007-07-05 17:14:27 +00:00
|
|
|
$sb->add_shorthand_int_option("thumb_mem_limit", "<br>Max memory use: ");
|
2007-05-08 20:36:02 +00:00
|
|
|
|
2007-06-30 01:19:11 +00:00
|
|
|
$event->panel->add_block($sb);
|
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)) {
|
|
|
|
$error = "Image's source isn't a valid URL";
|
|
|
|
return $error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-16 11:58:25 +00:00
|
|
|
/*
|
|
|
|
* Check for an existing image
|
|
|
|
*/
|
2008-10-09 03:21:18 +00:00
|
|
|
$existing = Image::by_hash($config, $database, $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);
|
|
|
|
return $error;
|
|
|
|
}
|
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,
|
|
|
|
hash, ext, width, height, posted, source)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, now(), ?)",
|
2007-04-16 11:58:25 +00:00
|
|
|
array($user->id, $_SERVER['REMOTE_ADDR'], $image->filename, $image->filesize,
|
2007-07-20 23:24:32 +00:00
|
|
|
$image->hash, $image->ext, $image->width, $image->height, $image->source));
|
2007-04-16 11:58:25 +00:00
|
|
|
$image->id = $database->db->Insert_ID();
|
|
|
|
|
2009-01-18 14:58:49 +00:00
|
|
|
send_event(new TagSetEvent($image, $image->get_tag_array()));
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2007-08-02 20:49:25 +00:00
|
|
|
return null;
|
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;
|
2008-08-26 09:11:40 +00:00
|
|
|
$image = Image::by_id($config, $database, $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';
|
|
|
|
|
|
|
|
// FIXME: should be $page->blah
|
|
|
|
if($if_modified_since == $gmdate_mod) {
|
|
|
|
header("HTTP/1.0 304 Not Modified");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
header("Last-Modified: $gmdate_mod");
|
|
|
|
header("Expires: Fri, 2 Sep 2101 12:42:42 GMT"); // War was beginning
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// }}}
|
|
|
|
}
|
|
|
|
add_event_listener(new ImageIO());
|
|
|
|
?>
|