Merge pull request #59 from ThomasRubini/profile_pic

This commit is contained in:
Thomas Rubini 2023-01-23 20:34:59 +01:00 committed by GitHub
commit 5986083c3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 61 additions and 5 deletions

View File

@ -111,6 +111,25 @@ final class UserController
$O_userModel = new UserModel();
// TODO harmonize error handling here
if (isset($_FILES["profilPicture"])) {
if ($_FILES['profilPicture']['error'] !== UPLOAD_ERR_OK) {
die("Upload failed with error code " . $_FILES['profilPicture']['error']);
}
$info = getimagesize($_FILES['profilPicture']['tmp_name']);
if ($info === false) {
die("Unable to determine image type of uploaded file");
}
if (($info[2] !== IMAGETYPE_JPEG) && ($info[2] !== IMAGETYPE_PNG)) {
die("Not a jpeg/png");
}
$fp = fopen($_FILES['profilPicture']['tmp_name'], 'rb');
$O_userModel->updateProfilePicByID($_SESSION["ID"], $fp);
}
if (isset($_POST["email"])) {
$S_email = $_POST["email"];
if (!empty($S_email) && filter_var($S_email, FILTER_VALIDATE_EMAIL)) {
@ -161,5 +180,19 @@ final class UserController
echo "Le compte à été supprimé avec succès";
}
public function profilePicAction(Array $A_urlParams = null, Array $A_postParams = null)
{
if (count($A_urlParams) !== 1 ) die();
$O_userModel = new UserModel();
$A_user = $O_userModel->getUserByID($A_urlParams[0]);
header("Content-Type: image/png");
echo $A_user["PROFILE_PIC"];
return Utils::RETURN_RAW;
}
}

View File

@ -64,15 +64,21 @@ final class controller
}
$B_called = call_user_func_array(array(
$called = call_user_func_array(array(
new $this->_A_urlParts['controller'],
$this->_A_urlParts['action']),
array($this->_A_urlParams, $this->_A_postParams, $this->_A_getParams)
);
if (false === $B_called) {
if (false === $called) {
throw new ControllerException("Action " . $this->_A_urlParts['action'] .
" of controller " . $this->_A_urlParts['controller'] . " failed.");
}
if(Utils::RETURN_RAW === $called){
return Utils::RETURN_RAW;
}else{
return Utils::RETURN_HTML;
}
}
}

View File

@ -3,6 +3,9 @@
final class Utils
{
public const RETURN_HTML = 2;
public const RETURN_RAW = 3;
public static function getOrDie($DICT, $key)
{
if (isset($DICT[$key])) return $DICT[$key];

View File

@ -58,6 +58,14 @@ final class UserModel extends UserSessionModel
return $row["USERNAME"];
}
public function updateProfilePicByID($I_id, $profile_pic_fp){
$O_model = Model::get();
$stmt = $O_model->prepare("UPDATE USER SET PROFILE_PIC=:profile_pic WHERE ID=:id");
$stmt->bindParam("id", $I_id);
$stmt->bindParam("profile_pic", $profile_pic_fp, PDO::PARAM_LOB);
$stmt->execute();
}
public function updateEmailByID($I_id, $S_newEmail){
$O_model = Model::get();
$stmt = $O_model->prepare("UPDATE USER SET EMAIL=:new_email WHERE ID=:id");

View File

@ -9,7 +9,7 @@
<a href="/user/logout">Se déconnecter</a>
<form action="/user/update" method="post">
<form action="/user/update" method="post" enctype="multipart/form-data">
<label for="profilPicture">Changer l'image de profil&nbsp;</label>
<input type="file" name="profilPicture" id="profilPicture" accept="image/*">

View File

@ -19,7 +19,7 @@
try
{
$O_controller = new Controller($S_url, $A_postParams, $A_getParams);
$O_controller->execute();
$ret = $O_controller->execute();
}
catch (ControleurException $O_exception)
{
@ -29,4 +29,10 @@
$content = View::closeBuffer();
View::show('html', array('body' => $content));
if($ret === Utils::RETURN_HTML){
View::show('html', array('body' => $content));
}else if($ret === Utils::RETURN_RAW){
echo $content;
}else{
throw new Exception("Invalid return value: $ret");
}