diff --git a/Controllers/UserController.php b/Controllers/UserController.php index 2134123..cb34160 100644 --- a/Controllers/UserController.php +++ b/Controllers/UserController.php @@ -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; + } } diff --git a/Kernel/Controller.php b/Kernel/Controller.php index a3e8649..1e93190 100644 --- a/Kernel/Controller.php +++ b/Kernel/Controller.php @@ -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; + } } } \ No newline at end of file diff --git a/Kernel/Utils.php b/Kernel/Utils.php index 8d9486a..71d71aa 100644 --- a/Kernel/Utils.php +++ b/Kernel/Utils.php @@ -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]; diff --git a/Models/UserModel.php b/Models/UserModel.php index 511effc..5119335 100644 --- a/Models/UserModel.php +++ b/Models/UserModel.php @@ -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"); diff --git a/Views/user/edit.php b/Views/user/edit.php index ca2a9bf..3bd2f13 100644 --- a/Views/user/edit.php +++ b/Views/user/edit.php @@ -9,7 +9,7 @@ Se déconnecter -
+ diff --git a/index.php b/index.php index 27c2fe2..911db2c 100644 --- a/index.php +++ b/index.php @@ -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)); \ No newline at end of file + 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"); + } \ No newline at end of file