Working on image replace.

Getting unknown 404 errors.
This commit is contained in:
green-ponies (jgen) 2011-08-24 23:55:44 -04:00
parent 0d863c898e
commit b02c747ac1
4 changed files with 139 additions and 88 deletions

View File

@ -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);

View File

@ -28,7 +28,14 @@ class ImageIOTheme {
";
}
$html .= "<a href='".make_link("upload/replace/".$i_image_id)."'>Replace Image</a>";
if($config->get_bool("upload_replace")) {
$html .= "
".make_form(make_link("image_admin/replace"))."
<input type='hidden' name='image_id' value='$i_image_id' />
<input type='submit' value='Replace' />
</form>
";
}
return $html;
}

View File

@ -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);
}
}
}
}

View File

@ -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 = "<p>Replacing Image ID# ".$image_id."</p>"
.make_form(make_link("upload"), "POST", $multipart=True)."
<input type='hidden' name='image_id' value='$image_id'>
<table id='large_upload_form'>
$upload_list
<tr><td>Source</td><td colspan='3'><input name='source' type='text'></td></tr>
@ -108,15 +110,7 @@ class UploadTheme extends Themelet {
</form>
<small>(Max file size is $max_kb)</small>
";
/*
if($tl_enabled) {
$link = make_http(make_link("upload"));
$title = "Upload to " . $config->get_string('title');
$html .= '<p><a href="javascript:location.href=&quot;' .
$link . '?url=&quot;+location.href+&quot;&amp;tags=&quot;+prompt(&quot;enter tags&quot;)">' .
$title . '</a> (Drag & drop onto your bookmarks toolbar, then click when looking at an image)';
}
*/
$page->set_title("Replace Image Upload");
$page->set_heading("Replace Image Upload");
$page->add_block(new NavBlock());
@ -135,6 +129,22 @@ 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) {
$page->add_block(new Block($title, $message));
}