From 7690615b82c8b302616b38d28af133e748baad48 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Thu, 26 Jan 2023 20:42:45 +0100 Subject: [PATCH] move image processing to a separate function --- Controllers/UserController.php | 17 ++++------------- Kernel/Utils.php | 21 ++++++++++++++++++++- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/Controllers/UserController.php b/Controllers/UserController.php index 6c84437..57947b1 100644 --- a/Controllers/UserController.php +++ b/Controllers/UserController.php @@ -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)) { diff --git a/Kernel/Utils.php b/Kernel/Utils.php index af510f0..d88914d 100644 --- a/Kernel/Utils.php +++ b/Kernel/Utils.php @@ -17,5 +17,24 @@ final class Utils if (is_numeric($data)) return (int) $data; 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; + } }