Browse Source

Harden image upload error handling

Alan Lam 5 years ago
parent
commit
8fa0f9aa0d

+ 2 - 0
controllers/location_edit.php

@@ -77,8 +77,10 @@ if($submit) {
 					$file_path = $directory . "/$id.jpg";
 
 					$output_image = Image::normalise($photo->tmp_name);
+
 					$success = imagejpeg($output_image, $file_path);
 					imagedestroy($output_image);
+
 					if(!$success) throw new Exception("Failed to save photo.");
 				}
 				catch(Exception $e) {

+ 4 - 1
controllers/practice.php

@@ -122,6 +122,7 @@ if($submit) {
 					// Get image details
 					$size = @getimagesize($file->tmp_name);
 					if(!$size) throw new Exception(__("The uploaded file for %s is not an image.", $label));
+
 					[$width, $height, $type] = $size;
 					if($type !== IMAGETYPE_JPEG) throw new Exception(__("The uploaded file for %s is not a JPEG.", $label));
 
@@ -131,8 +132,10 @@ if($submit) {
 					// Write image to disk
 					$output_path = $test->getDirectory() . "/" . $name . ".jpg";
 
-					imagejpeg($output_image, $output_path, 60);
+					$success = imagejpeg($output_image, $output_path, 60);
 					imagedestroy($output_image);
+
+					if(!$success) throw new Exception(__("Failed to save %s.", $label));
 				}
 				catch(Exception $e) {
 					$form_exception->addError($e->getMessage());

+ 16 - 19
controllers/submit_id.php

@@ -18,32 +18,29 @@ if($submit) {
 		$id_document = (object) $_FILES["id_document"];
 		$photo = (object) $_FILES["photo"];
 
-		if($id_document->error != UPLOAD_ERR_OK) $form_exception->addError(HTTP::getUploadErrorString($id_document->error));
-		if($photo->error != UPLOAD_ERR_OK) $form_exception->addError(HTTP::getUploadErrorString($photo->error));
+		if($id_document->error !== UPLOAD_ERR_OK) $form_exception->addError(HTTP::getUploadErrorString($id_document->error));
+		if($photo->error !== UPLOAD_ERR_OK) $form_exception->addError(HTTP::getUploadErrorString($photo->error));
 
 		$size = @getimagesize($id_document->tmp_name);
 		if(!$size) $form_exception->addError(__("The uploaded ID document is not an image."));
-		list($width, $height, $type) = $size;
-		if($type != IMAGETYPE_JPEG) $form_exception->addError(__("The uploaded ID document is not a JPEG."));
+		[$width, $height, $type] = $size;
+		if($type !== IMAGETYPE_JPEG) $form_exception->addError(__("The uploaded ID document is not a JPEG."));
 
 		$size = @getimagesize($photo->tmp_name);
 		if(!$size) $form_exception->addError(__("The uploaded photo is not an image."));
-		list($width, $height, $type) = $size;
-		if($type != IMAGETYPE_JPEG) $form_exception->addError(__("The uploaded photo is not a JPEG."));
+		[$width, $height, $type] = $size;
+		if($type !== IMAGETYPE_JPEG) $form_exception->addError(__("The uploaded photo is not a JPEG."));
 
 		if($form_exception->hasErrors()) throw $form_exception;
 
-		$files = [$id_document, $photo];
-		foreach($files as $index => $file) {
+		$files = [
+			["file" => $id_document, "name" => "ID document", "type" => UserFile::TYPE_ID],
+			["file" => $photo, "name" => "photo", "type" => UserFile::TYPE_PHOTO],
+		];
 
-			if($index == 0) {
-				$name = "ID document";
-				$type = UserFile::TYPE_ID;
-			}
-			if($index == 1) {
-				$name = "photo";
-				$type = UserFile::TYPE_PHOTO;
-			}
+		foreach($files as $file_info) {
+
+			["file" => $file, "name" => $name, "type" => $type] = $file_info;
 
 			$output_image = Image::normalise($file->tmp_name);
 
@@ -57,8 +54,10 @@ if($submit) {
 
 			$output_path = $user->getDirectory() . "/" . $user_file->id . ".jpg";
 
-			imagejpeg($output_image, $output_path, 60);
+			$success = imagejpeg($output_image, $output_path, 60);
 			imagedestroy($output_image);
+
+			if(!$success) throw new Exception(__("Failed to save %s.", $name));
 		}
 
 		$user->level = User::LEVEL_UNVERIFIED;
@@ -81,5 +80,3 @@ if($submit) {
 View::includeOnce("header", compact("page_title"));
 View::include("submit_id");
 View::includeOnce("footer");
-
-?>

+ 4 - 1
controllers/test_tester_form.php

@@ -111,6 +111,7 @@ if($submit) {
 						// Get image details
 						$size = @getimagesize($file->tmp_name);
 						if(!$size) throw new Exception(sprintf(__("The uploaded file for %s is not an image."), $label));
+
 						[$width, $height, $type] = $size;
 						if($type !== IMAGETYPE_JPEG) throw new Exception(sprintf(__("The uploaded file for %s is not a JPEG."), $label));
 
@@ -120,8 +121,10 @@ if($submit) {
 						// Write image to disk
 						$output_path = $test->getDirectory() . "/" . $name . ".jpg";
 
-						imagejpeg($output_image, $output_path, 60);
+						$success = imagejpeg($output_image, $output_path, 60);
 						imagedestroy($output_image);
+
+						if(!$success) throw new Exception(__("Failed to save %s.", $label));
 					}
 					catch(Exception $e) {
 						$form_exception->addError($e->getMessage());

+ 25 - 12
engine/image.php

@@ -12,11 +12,18 @@ class Image {
 		return true;
 	}
 
-	public static function normalise($path, $max_width = 1920) {
+	/**
+	 * @param string $path
+	 * @param int $max_width
+	 * @return bool|GdImage|resource
+	 * @throws Exception
+	 */
+	public static function normalise(string $path, int $max_width = 1920) {
 
 		$size = getimagesize($path);
+		if($size === false) throw new Exception(__("Failed to determine image size."));
 
-		list($width, $height) = $size;
+		[$image_width, $image_height] = $size;
 
 		$exif = exif_read_data($path);
 		$rotate_angle = 0;
@@ -30,22 +37,26 @@ class Image {
 		}
 
 		$original_image = imagecreatefromjpeg($path);
+		if($original_image === false) throw new Exception(__("Failed to load image."));
 
-		if($width > $height) {
-			if($width > $max_width) {
-				$width = $max_width;
-			}
+		// Horizontal images
+		if($image_width > $image_height && $image_width > $max_width) {
+			$target_width = $max_width;
+		}
+		// Vertical images
+		else if($image_height > $max_width) {
+			$ratio = $image_height / $max_width;
+			$target_width = (int) floor($image_height / $ratio);
 		}
 		else {
-			if($height > $max_width) {
-				$ratio = $height / $max_width;
-				$width = floor($width / $ratio);
-			}
+			$target_width = $image_width;
 		}
 
 		// Resize
-		if($width != $size[0]) {
-			$output_image = imagescale($original_image, $width, -1, IMG_BICUBIC);
+		if($image_width !== $target_width) {
+			$output_image = imagescale($original_image, $target_width);
+			if($output_image === false) throw new Exception(__("Failed to resize image."));
+
 			imagedestroy($original_image);
 		}
 		else {
@@ -55,11 +66,13 @@ class Image {
 		// Rotate
 		if($rotate_angle) {
 			$output_image = imagerotate($output_image, $rotate_angle, 0);
+			if($output_image === false) throw new Exception(__("Failed to rotate image."));
 		}
 
 		// Flip
 		if($is_flipped) {
 			$output_image = imageflip($output_image, IMG_FLIP_HORIZONTAL);
+			if($output_image === false) throw new Exception(__("Failed to flip image."));
 		}
 
 		return $output_image;