Image Replace feature working, just needs more testing.
This commit is contained in:
parent
b02c747ac1
commit
17999cade8
@ -134,20 +134,46 @@ abstract class DataHandlerExtension implements Extension {
|
|||||||
if(is_null($this->theme)) $this->theme = get_theme_object($this);
|
if(is_null($this->theme)) $this->theme = get_theme_object($this);
|
||||||
|
|
||||||
if(($event instanceof DataUploadEvent) && $this->supported_ext($event->type) && $this->check_contents($event->tmpname)) {
|
if(($event instanceof DataUploadEvent) && $this->supported_ext($event->type) && $this->check_contents($event->tmpname)) {
|
||||||
|
|
||||||
if(!move_upload_to_archive($event)) return;
|
if(!move_upload_to_archive($event)) return;
|
||||||
send_event(new ThumbnailGenerationEvent($event->hash, $event->type));
|
send_event(new ThumbnailGenerationEvent($event->hash, $event->type));
|
||||||
$image = $this->create_image_from_data(warehouse_path("images", $event->hash), $event->metadata);
|
|
||||||
if(is_null($image)) {
|
|
||||||
throw new UploadException("Data handler failed to create image object from data");
|
|
||||||
}
|
|
||||||
/* Check if we are replacing an image */
|
/* Check if we are replacing an image */
|
||||||
if ( isset($event->metadata['replace']))
|
if (array_key_exists('replace',$event->metadata) && isset($event->metadata['replace']))
|
||||||
{
|
{
|
||||||
$image_id = $event->metadata['replace'];
|
/* hax: This seems like such a dirty way to do this.. */
|
||||||
|
|
||||||
|
/* Validate things */
|
||||||
|
$image_id = int_escape($event->metadata['replace']);
|
||||||
|
|
||||||
|
/* Check to make sure the image exists. */
|
||||||
|
$existing = Image::by_id($image_id);
|
||||||
|
|
||||||
|
if(is_null($existing)) {
|
||||||
|
throw new UploadException("Image to replace does not exist!");
|
||||||
|
}
|
||||||
|
if ($existing->hash === $event->metadata['hash']) {
|
||||||
|
throw new UploadException("The uploaded image is the same as the one to replace.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// even more hax..
|
||||||
|
$event->metadata['tags'] = $existing->get_tag_list();
|
||||||
|
$image = $this->create_image_from_data(warehouse_path("images", $event->metadata['hash']), $event->metadata);
|
||||||
|
|
||||||
|
if(is_null($image)) {
|
||||||
|
throw new UploadException("Data handler failed to create image object from data");
|
||||||
|
}
|
||||||
|
|
||||||
$ire = new ImageReplaceEvent($image_id, $image);
|
$ire = new ImageReplaceEvent($image_id, $image);
|
||||||
send_event($ire);
|
send_event($ire);
|
||||||
$event->image_id = $image_id;
|
$event->image_id = $image_id;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$image = $this->create_image_from_data(warehouse_path("images", $event->hash), $event->metadata);
|
||||||
|
if(is_null($image)) {
|
||||||
|
throw new UploadException("Data handler failed to create image object from data");
|
||||||
|
}
|
||||||
$iae = new ImageAdditionEvent($event->user, $image);
|
$iae = new ImageAdditionEvent($event->user, $image);
|
||||||
send_event($iae);
|
send_event($iae);
|
||||||
$event->image_id = $iae->image->id;
|
$event->image_id = $iae->image->id;
|
||||||
|
@ -48,14 +48,15 @@ class ImageDeletionEvent extends Event {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* ImageReplaceEvent:
|
* ImageReplaceEvent:
|
||||||
* $image -- the new image to be used
|
* $id -- the ID of the image to replace
|
||||||
|
* $image -- the image object of the new image to use
|
||||||
*
|
*
|
||||||
* This function replaces an image. Effectively it only
|
* This function replaces an image. Effectively it only
|
||||||
* replaces the image file contents and leaves the tags
|
* replaces the image file contents and leaves the tags
|
||||||
* and such the same.
|
* and such the same.
|
||||||
*/
|
*/
|
||||||
class ImageReplaceEvent extends Event {
|
class ImageReplaceEvent extends Event {
|
||||||
var $image;
|
var $id, $image;
|
||||||
|
|
||||||
public function ImageReplaceEvent($id, Image $image) {
|
public function ImageReplaceEvent($id, Image $image) {
|
||||||
$this->id = $id;
|
$this->id = $id;
|
||||||
@ -369,22 +370,15 @@ class ImageIO extends SimpleExtension {
|
|||||||
global $database;
|
global $database;
|
||||||
global $config;
|
global $config;
|
||||||
|
|
||||||
/*
|
/* Check to make sure the image exists. */
|
||||||
* Validate things
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* Check to make sure the image exists. */
|
|
||||||
$existing = Image::by_id($id);
|
$existing = Image::by_id($id);
|
||||||
|
|
||||||
if(is_null($existing)) {
|
if(is_null($existing)) {
|
||||||
throw new ImageReplaceException("Image to replace does not exist!");
|
throw new ImageReplaceException("Image to replace does not exist!");
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($existing->hash === $image->hash) {
|
|
||||||
throw new ImageReplaceException("The uploaded image is the same as the one to replace.");
|
|
||||||
}
|
|
||||||
if(strlen(trim($image->source)) == 0) {
|
if(strlen(trim($image->source)) == 0) {
|
||||||
$image->source = null;
|
$image->source = $existing->get_source();
|
||||||
}
|
}
|
||||||
if(!empty($image->source)) {
|
if(!empty($image->source)) {
|
||||||
if(!preg_match("#^(https?|ftp)://#", $image->source)) {
|
if(!preg_match("#^(https?|ftp)://#", $image->source)) {
|
||||||
@ -396,10 +390,10 @@ class ImageIO extends SimpleExtension {
|
|||||||
This step could be optional, ie: perhaps move the image somewhere
|
This step could be optional, ie: perhaps move the image somewhere
|
||||||
and have it stored in a 'replaced images' list that could be
|
and have it stored in a 'replaced images' list that could be
|
||||||
inspected later by an admin?
|
inspected later by an admin?
|
||||||
*/
|
*/
|
||||||
|
log_debug("image", "Removing image with hash ".$existing->hash);
|
||||||
$existing->remove_image_only(); // Actually delete the old image file from disk
|
$existing->remove_image_only(); // Actually delete the old image file from disk
|
||||||
|
|
||||||
|
|
||||||
// Update the data in the database.
|
// Update the data in the database.
|
||||||
$database->Execute(
|
$database->Execute(
|
||||||
"UPDATE images SET
|
"UPDATE images SET
|
||||||
@ -411,11 +405,11 @@ class ImageIO extends SimpleExtension {
|
|||||||
array(
|
array(
|
||||||
"filename"=>$image_new->filename, "filesize"=>$image->filesize, "hash"=>$image->hash,
|
"filename"=>$image_new->filename, "filesize"=>$image->filesize, "hash"=>$image->hash,
|
||||||
"ext"=>$image->ext, "width"=>$image->width, "height"=>$image->height, "source"=>$image->source,
|
"ext"=>$image->ext, "width"=>$image->width, "height"=>$image->height, "source"=>$image->source,
|
||||||
"id"=>$existing->id
|
"id"=>$id
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
log_info("image", "Replaced Image #{$existing->id} with ({$image->hash})");
|
log_info("image", "Replaced Image #{$id} with ({$image->hash})");
|
||||||
}
|
}
|
||||||
// }}} end replace
|
// }}} end replace
|
||||||
|
|
||||||
|
@ -69,9 +69,11 @@ class Upload implements Extension {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if($event instanceof PageRequestEvent) {
|
if($event instanceof PageRequestEvent) {
|
||||||
|
|
||||||
if ($event->page_matches("upload/replace"))
|
if ($event->page_matches("upload/replace"))
|
||||||
{
|
{
|
||||||
/* Replace Image Request */
|
/* Upload & Replace Image Request */
|
||||||
|
|
||||||
if (!$config->get_bool("upload_replace")) {
|
if (!$config->get_bool("upload_replace")) {
|
||||||
throw new UploadException("Upload Replacing Images is not enabled.");
|
throw new UploadException("Upload Replacing Images is not enabled.");
|
||||||
}
|
}
|
||||||
@ -87,113 +89,96 @@ class Upload implements Extension {
|
|||||||
if (empty($image_id)) {
|
if (empty($image_id)) {
|
||||||
throw new UploadException("Can not replace Image: No valid Image ID given.");
|
throw new UploadException("Can not replace Image: No valid Image ID given.");
|
||||||
}
|
}
|
||||||
|
|
||||||
$image_old = Image::by_id($image_id);
|
$image_old = Image::by_id($image_id);
|
||||||
if(is_null($image_old)) {
|
if(is_null($image_old)) {
|
||||||
$this->theme->display_error($page, "Image not found", "No image in the database has the ID #$image_id");
|
$this->theme->display_error($page, "Image not found", "No image in the database has the ID #$image_id");
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->theme->display_replace_page($page, $image_id);
|
if(count($_FILES) + count($_POST) > 0)
|
||||||
|
|
||||||
}
|
|
||||||
else if ($event->page_matches("upload"))
|
|
||||||
{
|
|
||||||
// Try to get the image ID
|
|
||||||
$image_id = int_escape($event->get_arg(0));
|
|
||||||
if (empty($image_id)) {
|
|
||||||
$image_id = isset($_POST['image_id']) ? $_POST['image_id'] : null;
|
|
||||||
}
|
|
||||||
if (!empty($image_id))
|
|
||||||
{
|
{
|
||||||
/* Upload and Replace Image */
|
if (count($_FILES) > 1) {
|
||||||
if (!$config->get_bool("upload_replace")) {
|
throw new UploadException("Can not upload more than one image for replacing.");
|
||||||
throw new UploadException("Upload Replacing Images is not enabled.");
|
|
||||||
}
|
}
|
||||||
if($is_full) {
|
if($this->can_upload($user)) {
|
||||||
throw new UploadException("Can not replace Image: disk nearly full");
|
if (count($_FILES)) {
|
||||||
}
|
foreach($_FILES as $file) {
|
||||||
$image_old = Image::by_id($image_id);
|
$ok = $this->try_upload($file, $tags, $source, $image_id);
|
||||||
if(is_null($image_old)) {
|
break; // leave the foreach loop.
|
||||||
$this->theme->display_error($page, "Image not found", "No image in the database has the ID #$image_id");
|
}
|
||||||
}
|
} else {
|
||||||
if(count($_FILES) + count($_POST) > 0)
|
foreach($_POST as $name => $value) {
|
||||||
{
|
if(substr($name, 0, 3) == "url" && strlen($value) > 0) {
|
||||||
if (count($_FILES) > 1) {
|
$ok = $this->try_transload($value, $tags, $source, $image_id);
|
||||||
throw new UploadException("Can not upload more than one image for replacing.");
|
break; // leave the foreach loop.
|
||||||
}
|
|
||||||
if($this->can_upload($user)) {
|
|
||||||
if (count($_FILES)) {
|
|
||||||
$ok = $this->try_upload($_FILES, $tags, $source, $image_id);
|
|
||||||
} else {
|
|
||||||
foreach($_POST as $name => $value) {
|
|
||||||
if(substr($name, 0, 3) == "url" && strlen($value) > 0) {
|
|
||||||
$ok = $this->try_transload($value, $tags, $source, $image_id);
|
|
||||||
break; // leave the foreach loop.
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* Could replace with a page saying the image replace was successful? */
|
|
||||||
$this->theme->display_replace_upload_status($page, $ok);
|
|
||||||
} else {
|
|
||||||
$this->theme->display_permission_denied($page);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->theme->display_upload_status($page, $ok);
|
||||||
|
} else {
|
||||||
|
$this->theme->display_permission_denied($page);
|
||||||
}
|
}
|
||||||
else if(!empty($_GET['url']))
|
}
|
||||||
{
|
else if(!empty($_GET['url']))
|
||||||
if($this->can_upload($user)) {
|
{
|
||||||
$url = $_GET['url'];
|
if($this->can_upload($user)) {
|
||||||
$ok = $this->try_transload($url, $tags, $url, $image_id);
|
$url = $_GET['url'];
|
||||||
/* Replace with a page saying the image replace was successful */
|
$ok = $this->try_transload($url, $tags, $url, $image_id);
|
||||||
$this->theme->display_replace_upload_status($page, $ok);
|
$this->theme->display_upload_status($page, $ok);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$this->theme->display_permission_denied($page);
|
$this->theme->display_permission_denied($page);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* Regular Upload Image */
|
$this->theme->display_replace_page($page, $image_id);
|
||||||
if(count($_FILES) + count($_POST) > 0)
|
}
|
||||||
{
|
}
|
||||||
$tags = Tag::explode($_POST['tags']);
|
else if ($event->page_matches("upload"))
|
||||||
$source = isset($_POST['source']) ? $_POST['source'] : null;
|
{
|
||||||
if($this->can_upload($user)) {
|
/* Regular Upload Image */
|
||||||
$ok = true;
|
if(count($_FILES) + count($_POST) > 0)
|
||||||
foreach($_FILES as $file) {
|
{
|
||||||
$ok = $ok & $this->try_upload($file, $tags, $source);
|
$tags = Tag::explode($_POST['tags']);
|
||||||
}
|
$source = isset($_POST['source']) ? $_POST['source'] : null;
|
||||||
foreach($_POST as $name => $value) {
|
if($this->can_upload($user)) {
|
||||||
if(substr($name, 0, 3) == "url" && strlen($value) > 0) {
|
$ok = true;
|
||||||
$ok = $ok & $this->try_transload($value, $tags, $source);
|
foreach($_FILES as $file) {
|
||||||
}
|
$ok = $ok & $this->try_upload($file, $tags, $source);
|
||||||
|
}
|
||||||
|
foreach($_POST as $name => $value) {
|
||||||
|
if(substr($name, 0, 3) == "url" && strlen($value) > 0) {
|
||||||
|
$ok = $ok & $this->try_transload($value, $tags, $source);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$this->theme->display_upload_status($page, $ok);
|
$this->theme->display_upload_status($page, $ok);
|
||||||
}
|
|
||||||
else {
|
|
||||||
$this->theme->display_permission_denied($page);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if(!empty($_GET['url']))
|
|
||||||
{
|
|
||||||
if($this->can_upload($user)) {
|
|
||||||
$url = $_GET['url'];
|
|
||||||
$tags = array('tagme');
|
|
||||||
if(!empty($_GET['tags']) && $_GET['tags'] != "null") {
|
|
||||||
$tags = Tag::explode($_GET['tags']);
|
|
||||||
}
|
|
||||||
$ok = $this->try_transload($url, $tags, $url);
|
|
||||||
$this->theme->display_upload_status($page, $ok);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$this->theme->display_permission_denied($page);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if(!$is_full) {
|
$this->theme->display_permission_denied($page);
|
||||||
$this->theme->display_page($page);
|
}
|
||||||
|
}
|
||||||
|
else if(!empty($_GET['url']))
|
||||||
|
{
|
||||||
|
if($this->can_upload($user)) {
|
||||||
|
$url = $_GET['url'];
|
||||||
|
$tags = array('tagme');
|
||||||
|
if(!empty($_GET['tags']) && $_GET['tags'] != "null") {
|
||||||
|
$tags = Tag::explode($_GET['tags']);
|
||||||
}
|
}
|
||||||
|
$ok = $this->try_transload($url, $tags, $url);
|
||||||
|
$this->theme->display_upload_status($page, $ok);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$this->theme->display_permission_denied($page);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(!$is_full) {
|
||||||
|
$this->theme->display_page($page);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -279,7 +264,7 @@ class Upload implements Extension {
|
|||||||
if ($file['error'] !== UPLOAD_ERR_OK) {
|
if ($file['error'] !== UPLOAD_ERR_OK) {
|
||||||
throw new UploadException($this->upload_error_message($file['error']));
|
throw new UploadException($this->upload_error_message($file['error']));
|
||||||
}
|
}
|
||||||
|
|
||||||
$pathinfo = pathinfo($file['name']);
|
$pathinfo = pathinfo($file['name']);
|
||||||
$metadata['filename'] = $pathinfo['basename'];
|
$metadata['filename'] = $pathinfo['basename'];
|
||||||
$metadata['extension'] = $pathinfo['extension'];
|
$metadata['extension'] = $pathinfo['extension'];
|
||||||
@ -292,7 +277,6 @@ class Upload implements Extension {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$event = new DataUploadEvent($user, $file['tmp_name'], $metadata);
|
$event = new DataUploadEvent($user, $file['tmp_name'], $metadata);
|
||||||
|
|
||||||
send_event($event);
|
send_event($event);
|
||||||
if($event->image_id == -1) {
|
if($event->image_id == -1) {
|
||||||
throw new UploadException("File type not recognised");
|
throw new UploadException("File type not recognised");
|
||||||
|
@ -99,8 +99,13 @@ class UploadTheme extends Themelet {
|
|||||||
|
|
||||||
$max_size = $config->get_int('upload_size');
|
$max_size = $config->get_int('upload_size');
|
||||||
$max_kb = to_shorthand_int($max_size);
|
$max_kb = to_shorthand_int($max_size);
|
||||||
$html = "<p>Replacing Image ID# ".$image_id."</p>"
|
|
||||||
.make_form(make_link("upload"), "POST", $multipart=True)."
|
$image = Image::by_id($image_id);
|
||||||
|
$thumbnail = $this->build_thumb_html($image, null);
|
||||||
|
|
||||||
|
$html = "<p>Replacing Image ID ".$image_id."<br>Please note: You will have to refresh the image page, or empty your browser cache.</p>"
|
||||||
|
.$thumbnail."<br>"
|
||||||
|
.make_form(make_link("upload/replace/".$image_id), "POST", $multipart=True)."
|
||||||
<input type='hidden' name='image_id' value='$image_id'>
|
<input type='hidden' name='image_id' value='$image_id'>
|
||||||
<table id='large_upload_form'>
|
<table id='large_upload_form'>
|
||||||
$upload_list
|
$upload_list
|
||||||
@ -111,10 +116,10 @@ class UploadTheme extends Themelet {
|
|||||||
<small>(Max file size is $max_kb)</small>
|
<small>(Max file size is $max_kb)</small>
|
||||||
";
|
";
|
||||||
|
|
||||||
$page->set_title("Replace Image Upload");
|
$page->set_title("Replace Image");
|
||||||
$page->set_heading("Replace Image Upload");
|
$page->set_heading("Replace Image");
|
||||||
$page->add_block(new NavBlock());
|
$page->add_block(new NavBlock());
|
||||||
$page->add_block(new Block("Replace Image Upload", $html, "main", 20));
|
$page->add_block(new Block("Upload Replacement Image", $html, "main", 20));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function display_upload_status(Page $page, $ok) {
|
public function display_upload_status(Page $page, $ok) {
|
||||||
@ -129,22 +134,6 @@ class UploadTheme extends Themelet {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function display_replace_upload_status(Page $page, $ok) {
|
|
||||||
if($ok) {
|
|
||||||
$page->set_title("GREAT SUCCESS!");
|
|
||||||
$page->set_heading("GREAT SUCCESS!");
|
|
||||||
$page->add_block(new NavBlock());
|
|
||||||
/*
|
|
||||||
$page->set_mode("redirect");
|
|
||||||
$page->set_redirect(make_link());*/
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$page->set_title("poo");
|
|
||||||
$page->set_heading("Upload Status");
|
|
||||||
$page->add_block(new NavBlock());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function display_upload_error(Page $page, $title, $message) {
|
public function display_upload_error(Page $page, $title, $message) {
|
||||||
$page->add_block(new Block($title, $message));
|
$page->add_block(new Block($title, $message));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user