move image processing to a separate function
This commit is contained in:
		
							parent
							
								
									84ebcd13a4
								
							
						
					
					
						commit
						7690615b82
					
				| @ -130,20 +130,11 @@ final class UserController | ||||
| 
 | ||||
|         $O_user = UserModel::getByID($_SESSION["ID"]); | ||||
| 
 | ||||
|         if (isset($_FILES["profilPicture"]) && !empty($_FILES["profilPicture"]["name"])) { | ||||
|             if ($_FILES['profilPicture']['error'] === UPLOAD_ERR_OK) { | ||||
|                 $info = getimagesize($_FILES['profilPicture']['tmp_name']); | ||||
|                 if ($info !== false && ($info[2] === IMAGETYPE_JPEG || $info[2] === IMAGETYPE_PNG)) { | ||||
|                     $fp = fopen($_FILES['profilPicture']['tmp_name'], 'rb'); | ||||
|                     $O_user->updateProfilePic($fp); | ||||
|                 } else { | ||||
|                     throw new HTTPSpecialCaseException(400, "Profile picture submitted is not jpeg/png"); | ||||
|                 } | ||||
|             } else { | ||||
|                 throw new HTTPSpecialCaseException(400, "Profile picture upload error"); | ||||
|             } | ||||
|              | ||||
|         $fp = Utils::tryProcessImg("profilPicture"); | ||||
|         if($fp !== null) { | ||||
|             $O_user->updateProfilePic($fp); | ||||
|         } | ||||
| 
 | ||||
|         if (isset($_POST["email"]) && !empty($S_email)) { | ||||
|             $S_email = $_POST["email"]; | ||||
|             if (filter_var($S_email, FILTER_VALIDATE_EMAIL)) { | ||||
|  | ||||
| @ -18,4 +18,23 @@ final class Utils | ||||
|         else throw new HTTPSpecialCaseException(400, "Not an int"); | ||||
|     } | ||||
|      | ||||
|     public static function tryProcessImg($filename) { | ||||
|         if (isset($_FILES[$filename])) { | ||||
|             $file = $_FILES[$filename]; | ||||
|             if(!empty($file["name"])) { | ||||
|                 if ($file['error'] === UPLOAD_ERR_OK) { | ||||
|                     $info = getimagesize($file['tmp_name']); | ||||
|                     if ($info !== false && ($info[2] === IMAGETYPE_JPEG || $info[2] === IMAGETYPE_PNG)) { | ||||
|                         $fp = fopen($file['tmp_name'], 'rb'); | ||||
|                         return $fp; | ||||
|                     } else { | ||||
|                         throw new HTTPSpecialCaseException(400, "Image submitted is not jpeg/png"); | ||||
|                     } | ||||
|                 } else { | ||||
|                     throw new HTTPSpecialCaseException(400, "Image upload error"); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|         return null; | ||||
|     } | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user