Merge pull request #145 from ThomasRubini/recipe_img

This commit is contained in:
Thomas Rubini 2023-01-26 20:54:12 +01:00 committed by GitHub
commit 916cb5fcb5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 15 deletions

View File

@ -72,6 +72,11 @@ final class RecipeController
$i++; $i++;
} }
$O_recipe->S_RECIPE = substr($S_recipe, 2); $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) public function createAction(Array $A_urlParams = null, Array $A_postParams = null)

View File

@ -130,20 +130,11 @@ final class UserController
$O_user = UserModel::getByID($_SESSION["ID"]); $O_user = UserModel::getByID($_SESSION["ID"]);
if (isset($_FILES["profilPicture"]) && !empty($_FILES["profilPicture"]["name"])) { $fp = Utils::tryProcessImg("profilPicture");
if ($_FILES['profilPicture']['error'] === UPLOAD_ERR_OK) { if($fp !== null) {
$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); $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");
} }
}
if (isset($_POST["email"]) && !empty($S_email)) { if (isset($_POST["email"]) && !empty($S_email)) {
$S_email = $_POST["email"]; $S_email = $_POST["email"];
if (filter_var($S_email, FILTER_VALIDATE_EMAIL)) { if (filter_var($S_email, FILTER_VALIDATE_EMAIL)) {

View File

@ -18,4 +18,23 @@ final class Utils
else throw new HTTPSpecialCaseException(400, "Not an int"); 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;
}
} }

View File

@ -47,6 +47,7 @@ final class RecipeModel
$stmt->execute(); $stmt->execute();
$this->I_ID = Model::get()->lastInsertId(); $this->I_ID = Model::get()->lastInsertId();
} }
public function update() public function update()
{ {
$O_model = Model::get(); $O_model = Model::get();
@ -60,6 +61,15 @@ final class RecipeModel
$stmt->bindParam("author_id", $this->I_AUTHOR_ID); $stmt->bindParam("author_id", $this->I_AUTHOR_ID);
$stmt->execute(); $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(){ public function delete(){
$O_model = Model::get(); $O_model = Model::get();
$stmt = $O_model->prepare("DELETE FROM RECIPE WHERE ID=:id"); $stmt = $O_model->prepare("DELETE FROM RECIPE WHERE ID=:id");

View File

@ -27,7 +27,7 @@ if ($O_recipe === null) {
<?php } ?> <?php } ?>
<form action="<?= $A_view["POST_URI"] ?>" method="post"> <form action="<?= $A_view["POST_URI"] ?>" method="post" enctype="multipart/form-data">
<label for="recipeImage">Ajoutez l'image de haut de page&nbsp;:</label> <label for="recipeImage">Ajoutez l'image de haut de page&nbsp;:</label>
<input type="file" name="recipeImage" id="recipeImage"> <input type="file" name="recipeImage" id="recipeImage">