diff --git a/Controllers/RecipeController.php b/Controllers/RecipeController.php index d589cca..26bdb34 100644 --- a/Controllers/RecipeController.php +++ b/Controllers/RecipeController.php @@ -72,6 +72,11 @@ final class RecipeController $i++; } $O_recipe->S_RECIPE = substr($S_recipe, 2); + + $fp = Utils::tryProcessImg("recipeImage"); + if($fp !== null) { + $O_recipe->updateImg($fp); + } } public function createAction(Array $A_urlParams = null, Array $A_postParams = null) 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; + } } diff --git a/Models/RecipeModel.php b/Models/RecipeModel.php index ffdc71e..2c0e3c3 100644 --- a/Models/RecipeModel.php +++ b/Models/RecipeModel.php @@ -47,6 +47,7 @@ final class RecipeModel $stmt->execute(); $this->I_ID = Model::get()->lastInsertId(); } + public function update() { $O_model = Model::get(); @@ -60,6 +61,15 @@ final class RecipeModel $stmt->bindParam("author_id", $this->I_AUTHOR_ID); $stmt->execute(); } + + public function updateImg($img_fp){ + $O_model = Model::get(); + $stmt = $O_model->prepare("UPDATE RECIPE SET IMG=:img WHERE ID=:id"); + $stmt->bindParam("id", $this->I_ID); + $stmt->bindParam("img", $img_fp, PDO::PARAM_LOB); + $stmt->execute(); + } + public function delete(){ $O_model = Model::get(); $stmt = $O_model->prepare("DELETE FROM RECIPE WHERE ID=:id"); diff --git a/Views/recipe/edit.php b/Views/recipe/edit.php index c5cd745..a9e6cd4 100644 --- a/Views/recipe/edit.php +++ b/Views/recipe/edit.php @@ -27,7 +27,7 @@ if ($O_recipe === null) { -