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(($event instanceof DataUploadEvent) && $this->supported_ext($event->type) && $this->check_contents($event->tmpname)) { | ||||
| 		 | ||||
| 			if(!move_upload_to_archive($event)) return; | ||||
| 			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 */ | ||||
| 			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); | ||||
| 				send_event($ire); | ||||
| 				$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); | ||||
| 				send_event($iae); | ||||
| 				$event->image_id = $iae->image->id; | ||||
|  | ||||
| @ -48,14 +48,15 @@ class ImageDeletionEvent extends Event { | ||||
| 
 | ||||
| /* | ||||
|  * 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 | ||||
|  * replaces the image file contents and leaves the tags | ||||
|  * and such the same. | ||||
|  */ | ||||
| class ImageReplaceEvent extends Event { | ||||
| 	var $image; | ||||
| 	var $id, $image; | ||||
| 
 | ||||
| 	public function ImageReplaceEvent($id, Image $image) { | ||||
| 		$this->id = $id; | ||||
| @ -369,10 +370,6 @@ class ImageIO extends SimpleExtension { | ||||
| 		global $database; | ||||
| 		global $config; | ||||
| 		 | ||||
| 		/* | ||||
| 		 * Validate things | ||||
| 		 */ | ||||
| 		 | ||||
| 		/* Check to make sure the image exists. */ | ||||
| 		$existing = Image::by_id($id); | ||||
| 		 | ||||
| @ -380,11 +377,8 @@ class ImageIO extends SimpleExtension { | ||||
| 			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) { | ||||
| 			$image->source = null; | ||||
| 			$image->source = $existing->get_source(); | ||||
| 		} | ||||
| 		if(!empty($image->source)) { | ||||
| 			if(!preg_match("#^(https?|ftp)://#", $image->source)) { | ||||
| @ -397,9 +391,9 @@ class ImageIO extends SimpleExtension { | ||||
| 			and have it stored in a 'replaced images' list that could be  | ||||
| 			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
 | ||||
| 		 | ||||
| 		 | ||||
| 		// Update the data in the database.
 | ||||
| 		$database->Execute( | ||||
| 				"UPDATE images SET 
 | ||||
| @ -411,11 +405,11 @@ class ImageIO extends SimpleExtension { | ||||
| 				array( | ||||
| 					"filename"=>$image_new->filename, "filesize"=>$image->filesize, "hash"=>$image->hash, | ||||
| 					"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
 | ||||
| 
 | ||||
|  | ||||
| @ -69,9 +69,11 @@ class Upload implements Extension { | ||||
| 		} | ||||
| 
 | ||||
| 		if($event instanceof PageRequestEvent) { | ||||
| 		 | ||||
| 			if ($event->page_matches("upload/replace")) | ||||
| 			{ | ||||
| 				/* Replace Image Request */ | ||||
| 				/* Upload & Replace Image Request */ | ||||
| 				 | ||||
| 				if (!$config->get_bool("upload_replace")) { | ||||
| 					throw new UploadException("Upload Replacing Images is not enabled."); | ||||
| 				} | ||||
| @ -93,107 +95,90 @@ class Upload implements Extension { | ||||
| 					$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); | ||||
| 				 | ||||
| 			} | ||||
| 			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)) | ||||
| 				if(count($_FILES) + count($_POST) > 0) | ||||
| 				{ | ||||
| 					/* Upload and Replace Image */ | ||||
| 					if (!$config->get_bool("upload_replace")) { | ||||
| 						throw new UploadException("Upload Replacing Images is not enabled."); | ||||
| 					if (count($_FILES) > 1) { | ||||
| 						throw new UploadException("Can not upload more than one image for replacing."); | ||||
| 					} | ||||
| 					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.
 | ||||
| 									} | ||||
| 					if($this->can_upload($user)) { | ||||
| 						if (count($_FILES)) { | ||||
| 							foreach($_FILES as $file) { | ||||
| 								$ok = $this->try_upload($file, $tags, $source, $image_id); | ||||
| 								break; // leave the foreach loop.
 | ||||
| 							} | ||||
| 						} 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'])) | ||||
| 					{ | ||||
| 						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(!empty($_GET['url'])) | ||||
| 				{ | ||||
| 					if($this->can_upload($user)) { | ||||
| 						$url = $_GET['url']; | ||||
| 						$ok = $this->try_transload($url, $tags, $url, $image_id); | ||||
| 						$this->theme->display_upload_status($page, $ok); | ||||
| 					} | ||||
| 					else { | ||||
| 						$this->theme->display_permission_denied($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_replace_page($page, $image_id); | ||||
| 				} | ||||
| 			} | ||||
| 			else if ($event->page_matches("upload")) | ||||
| 			{ | ||||
| 				/* 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); | ||||
| 						} | ||||
| 						$this->theme->display_upload_status($page, $ok); | ||||
| 					} | ||||
| 					else { | ||||
| 						if(!$is_full) { | ||||
| 							$this->theme->display_page($page); | ||||
| 						$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); | ||||
| 					} | ||||
| 				} | ||||
| 			} | ||||
| @ -292,7 +277,6 @@ class Upload implements Extension { | ||||
| 				} | ||||
| 				 | ||||
| 				$event = new DataUploadEvent($user, $file['tmp_name'], $metadata); | ||||
| 
 | ||||
| 				send_event($event); | ||||
| 				if($event->image_id == -1) { | ||||
| 					throw new UploadException("File type not recognised"); | ||||
|  | ||||
| @ -99,8 +99,13 @@ class UploadTheme extends Themelet { | ||||
| 
 | ||||
| 		$max_size = $config->get_int('upload_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'> | ||||
| 				<table id='large_upload_form'> | ||||
| 					$upload_list | ||||
| @ -111,10 +116,10 @@ class UploadTheme extends Themelet { | ||||
| 			<small>(Max file size is $max_kb)</small> | ||||
| 		";
 | ||||
| 
 | ||||
| 		$page->set_title("Replace Image Upload"); | ||||
| 		$page->set_heading("Replace Image Upload"); | ||||
| 		$page->set_title("Replace Image"); | ||||
| 		$page->set_heading("Replace Image"); | ||||
| 		$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) { | ||||
| @ -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) { | ||||
| 		$page->add_block(new Block($title, $message)); | ||||
| 	} | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user