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)); | ||||||
|  | 
 | ||||||
|  | 			/* Check if we are replacing an image */ | ||||||
|  | 			if (array_key_exists('replace',$event->metadata) && isset($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 | ||||||
|  | 			{ | ||||||
| 				$image = $this->create_image_from_data(warehouse_path("images", $event->hash), $event->metadata); | 				$image = $this->create_image_from_data(warehouse_path("images", $event->hash), $event->metadata); | ||||||
| 				if(is_null($image)) { | 				if(is_null($image)) { | ||||||
| 					throw new UploadException("Data handler failed to create image object from data"); | 					throw new UploadException("Data handler failed to create image object from data"); | ||||||
| 				} | 				} | ||||||
| 			/* Check if we are replacing an image */ |  | ||||||
| 			if ( isset($event->metadata['replace'])) |  | ||||||
| 			{ |  | ||||||
| 				$image_id = $event->metadata['replace']; |  | ||||||
| 				$ire = new ImageReplaceEvent($image_id, $image); |  | ||||||
| 				send_event($ire); |  | ||||||
| 				$event->image_id = $image_id; |  | ||||||
| 			} else { |  | ||||||
| 				$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,10 +370,6 @@ class ImageIO extends SimpleExtension { | |||||||
| 		global $database; | 		global $database; | ||||||
| 		global $config; | 		global $config; | ||||||
| 		 | 		 | ||||||
| 		/* |  | ||||||
| 		 * Validate things |  | ||||||
| 		 */ |  | ||||||
| 		 |  | ||||||
| 		/* Check to make sure the image exists. */ | 		/* Check to make sure the image exists. */ | ||||||
| 		$existing = Image::by_id($id); | 		$existing = Image::by_id($id); | ||||||
| 		 | 		 | ||||||
| @ -380,11 +377,8 @@ class ImageIO extends SimpleExtension { | |||||||
| 			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)) { | ||||||
| @ -397,9 +391,9 @@ class ImageIO extends SimpleExtension { | |||||||
| 			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."); | ||||||
| 				} | 				} | ||||||
| @ -93,29 +95,6 @@ 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_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)) |  | ||||||
| 				{ |  | ||||||
| 					/* Upload and Replace Image */ |  | ||||||
| 					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"); |  | ||||||
| 					} |  | ||||||
| 					$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) + count($_POST) > 0) | ||||||
| 				{ | 				{ | ||||||
| 					if (count($_FILES) > 1) { | 					if (count($_FILES) > 1) { | ||||||
| @ -123,7 +102,10 @@ class Upload implements Extension { | |||||||
| 					} | 					} | ||||||
| 					if($this->can_upload($user)) { | 					if($this->can_upload($user)) { | ||||||
| 						if (count($_FILES)) { | 						if (count($_FILES)) { | ||||||
| 								$ok = $this->try_upload($_FILES, $tags, $source, $image_id); | 							foreach($_FILES as $file) { | ||||||
|  | 								$ok = $this->try_upload($file, $tags, $source, $image_id); | ||||||
|  | 								break; // leave the foreach loop.
 | ||||||
|  | 							} | ||||||
| 						} else { | 						} else { | ||||||
| 							foreach($_POST as $name => $value) { | 							foreach($_POST as $name => $value) { | ||||||
| 								if(substr($name, 0, 3) == "url" && strlen($value) > 0) { | 								if(substr($name, 0, 3) == "url" && strlen($value) > 0) { | ||||||
| @ -132,8 +114,8 @@ class Upload implements Extension { | |||||||
| 								} | 								} | ||||||
| 							} | 							} | ||||||
| 						} | 						} | ||||||
| 							/* Could replace with a page saying the image replace was successful? */ | 
 | ||||||
| 							$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); | ||||||
| 					} | 					} | ||||||
| @ -143,15 +125,18 @@ class Upload implements Extension { | |||||||
| 					if($this->can_upload($user)) { | 					if($this->can_upload($user)) { | ||||||
| 						$url = $_GET['url']; | 						$url = $_GET['url']; | ||||||
| 						$ok = $this->try_transload($url, $tags, $url, $image_id); | 						$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); | ||||||
| 							$this->theme->display_replace_upload_status($page, $ok); |  | ||||||
| 					} | 					} | ||||||
| 					else { | 					else { | ||||||
| 						$this->theme->display_permission_denied($page); | 						$this->theme->display_permission_denied($page); | ||||||
| 					} | 					} | ||||||
| 				} | 				} | ||||||
| 				} |  | ||||||
| 				else | 				else | ||||||
|  | 				{ | ||||||
|  | 					$this->theme->display_replace_page($page, $image_id); | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 			else if ($event->page_matches("upload")) | ||||||
| 			{ | 			{ | ||||||
| 				/* Regular Upload Image */ | 				/* Regular Upload Image */ | ||||||
| 				if(count($_FILES) + count($_POST) > 0) | 				if(count($_FILES) + count($_POST) > 0) | ||||||
| @ -190,14 +175,14 @@ class Upload implements Extension { | |||||||
| 						$this->theme->display_permission_denied($page); | 						$this->theme->display_permission_denied($page); | ||||||
| 					} | 					} | ||||||
| 				} | 				} | ||||||
| 					else { | 				else | ||||||
|  | 				{ | ||||||
| 					if(!$is_full) { | 					if(!$is_full) { | ||||||
| 						$this->theme->display_page($page); | 						$this->theme->display_page($page); | ||||||
| 					} | 					} | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 		} |  | ||||||
| 
 | 
 | ||||||
| 		if($event instanceof SetupBuildingEvent) { | 		if($event instanceof SetupBuildingEvent) { | ||||||
| 			$tes = array(); | 			$tes = array(); | ||||||
| @ -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