diff --git a/ext/image/main.php b/ext/image/main.php index 0adf2697..6bb5ded1 100644 --- a/ext/image/main.php +++ b/ext/image/main.php @@ -57,7 +57,8 @@ class ImageDeletionEvent extends Event { class ImageReplaceEvent extends Event { var $image; - public function ImageReplaceEvent(Image $image) { + public function ImageReplaceEvent($id, Image $image) { + $this->id = $id; $this->image = $image; } } @@ -162,7 +163,7 @@ class ImageIO extends SimpleExtension { $page->set_redirect(make_link('upload/replace/'.$image->id)); } else { /* Invalid image ID */ - // fail silently? + throw new ImageReplaceException("Image to replace does not exist."); } } } @@ -190,7 +191,7 @@ class ImageIO extends SimpleExtension { public function onImageReplace($event) { try { - $this->replace_image($event->image_old, $event->image_new); + $this->replace_image($event->id, $event->image); } catch(ImageReplaceException $e) { throw new UploadException($e->error); diff --git a/ext/image/theme.php b/ext/image/theme.php index ddd00719..5af59214 100644 --- a/ext/image/theme.php +++ b/ext/image/theme.php @@ -28,7 +28,14 @@ class ImageIOTheme { "; } - $html .= "Replace Image"; + if($config->get_bool("upload_replace")) { + $html .= " + ".make_form(make_link("image_admin/replace"))." + + + + "; + } return $html; } diff --git a/ext/upload/main.php b/ext/upload/main.php index 511f9271..e2f4790d 100644 --- a/ext/upload/main.php +++ b/ext/upload/main.php @@ -69,98 +69,131 @@ class Upload implements Extension { } if($event instanceof PageRequestEvent) { - /* Upload and Replace Image */ if ($event->page_matches("upload/replace")) { + /* Replace Image Request */ + if (!$config->get_bool("upload_replace")) { + throw new UploadException("Upload Replacing Images is not enabled."); + } if($is_full) { throw new UploadException("Can not replace Image: disk nearly full"); } + // Try to get the image ID $image_id = int_escape($event->get_arg(0)); - $image_old = Image::by_id($image_id); + if (empty($image_id)) { + $image_id = isset($_POST['image_id']) ? $_POST['image_id'] : null; + } + if (empty($image_id)) { + throw new UploadException("Can not replace Image: No valid Image ID given."); + } + + $image_old = Image::by_id($image_id); if(is_null($image_old)) { $this->theme->display_error($page, "Image not found", "No image in the database has the ID #$image_id"); } - if(count($_FILES) + count($_POST) > 0) - { - if (count($_FILES) > 1) { - throw new UploadException("Can not upload more than one image for replacing."); - } - - 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_upload_status($page, $ok); - } else { - $this->theme->display_permission_denied($page); - } - } - else if(!empty($_GET['url'])) - { - if($this->can_upload($user)) { - $url = $_GET['url']; - $ok = $this->try_transload($url, $tags, $url, $image_id); - /* Replace with a page saying the image replace was successful */ - $this->theme->display_upload_status($page, $ok); - } - else { - $this->theme->display_permission_denied($page); - } - } else { - $this->theme->display_replace_page($page); - } + $this->theme->display_replace_page($page, $image_id); + } - /* Upload Image */ else if ($event->page_matches("upload")) { - if(count($_FILES) + count($_POST) > 0) { - $tags = Tag::explode($_POST['tags']); - $source = isset($_POST['source']) ? $_POST['source'] : null; - if($this->can_upload($user)) { - $ok = true; - 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); - } - else { - $this->theme->display_permission_denied($page); - } + // 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; } - else if(!empty($_GET['url'])) + if (!empty($image_id)) { - 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); + /* Upload and Replace Image */ + if (!$config->get_bool("upload_replace")) { + throw new UploadException("Upload Replacing Images is not enabled."); } - else { - $this->theme->display_permission_denied($page); + if($is_full) { + throw new UploadException("Can not replace Image: disk nearly full"); + } + $image_old = Image::by_id($image_id); + if(is_null($image_old)) { + $this->theme->display_error($page, "Image not found", "No image in the database has the ID #$image_id"); + } + if(count($_FILES) + count($_POST) > 0) + { + if (count($_FILES) > 1) { + throw new UploadException("Can not upload more than one image for replacing."); + } + 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); + } + } + else if(!empty($_GET['url'])) + { + if($this->can_upload($user)) { + $url = $_GET['url']; + $ok = $this->try_transload($url, $tags, $url, $image_id); + /* 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); + } } } - else { - if(!$is_full) { - $this->theme->display_page($page); + else + { + /* Regular Upload Image */ + if(count($_FILES) + count($_POST) > 0) + { + $tags = Tag::explode($_POST['tags']); + $source = isset($_POST['source']) ? $_POST['source'] : null; + if($this->can_upload($user)) { + $ok = true; + 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); + } + 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 { + if(!$is_full) { + $this->theme->display_page($page); + } } } } diff --git a/ext/upload/theme.php b/ext/upload/theme.php index 99a0163b..9d94b897 100644 --- a/ext/upload/theme.php +++ b/ext/upload/theme.php @@ -76,7 +76,7 @@ class UploadTheme extends Themelet { } /* only allows 1 file to be uploaded - for replacing another image file */ - public function display_replace_page(Page $page) { + public function display_replace_page(Page $page, $image_id) { global $config; $tl_enabled = ($config->get_string("transload_engine", "none") != "none"); @@ -99,7 +99,9 @@ class UploadTheme extends Themelet { $max_size = $config->get_int('upload_size'); $max_kb = to_shorthand_int($max_size); - $html = make_form(make_link("upload/replace"), "POST", $multipart=True)." + $html = "
Replacing Image ID# ".$image_id."
" + .make_form(make_link("upload"), "POST", $multipart=True)." +Source |