Use error handling system in all controllers

This commit is contained in:
Thomas Rubini 2023-01-24 09:49:07 +01:00
parent ddaecac0f1
commit 66d4a89bdb
No known key found for this signature in database
GPG Key ID: C7D287C8C1CAC373
5 changed files with 26 additions and 19 deletions

View File

@ -26,8 +26,7 @@ final class ApprController
$A_appr = $O_apprModel->getApprById($I_appr_id);
if ($A_appr === null) {
echo "404";
return;
throw new HTTPSpecialCaseException(404);
}
if ($A_appr["AUTHOR_ID"] !== $_SESSION["ID"]) {

View File

@ -6,13 +6,13 @@ final class RecipeController
public function viewAction(Array $A_urlParams = null, Array $A_postParams = null)
{
if(count($A_urlParams)!=1){
return View::show("errors/404");
throw new HTTPSpecialCaseException(404);
}
$O_recipeModel = new RecipeModel();
$A_returnArray = $O_recipeModel->getFullRecipeWithApprs($A_urlParams[0]);
if ($A_returnArray === null) {
return View::show("errors/404");
throw new HTTPSpecialCaseException(404);
}
$A_returnArray["ADMIN"] = Session::is_admin();
@ -31,17 +31,17 @@ final class RecipeController
Session::login_or_die();
if(count($A_urlParams)!=1){
return View::show("errors/404");
throw new HTTPSpecialCaseException(404);
}
$O_recipeModel = new RecipeModel();
$A_returnArray = $O_recipeModel->getFullRecipe($A_urlParams[0]);
if ($A_returnArray === null) {
return View::show("errors/404");
throw new HTTPSpecialCaseException(404);
}
if ($A_returnArray["AUTHOR_ID"] !== $_SESSION["ID"]) {
die("You are not the owner of this recipe");
throw new HTTPSpecialCaseException(400, "You are not the owner of this recipe");
}
View::show("recipe/edit", $A_returnArray);

View File

@ -94,7 +94,7 @@ final class UserController
public function defaultAction(Array $A_urlParams = null, Array $A_postParams = null)
{
if(count($A_urlParams)!=0){
return View::show("errors/404");
throw new HTTPSpecialCaseException(404);
}
Session::login_or_die();
@ -111,20 +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']);
throw new HTTPSpecialCaseException(
400,
"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");
throw new HTTPSpecialCaseException(
400,
"Unable to determine image type of uploaded file"
);
}
if (($info[2] !== IMAGETYPE_JPEG) && ($info[2] !== IMAGETYPE_PNG)) {
die("Not a jpeg/png");
throw new HTTPSpecialCaseException(400, "Not a jpeg/png");
}
$fp = fopen($_FILES['profilPicture']['tmp_name'], 'rb');
@ -134,12 +139,16 @@ final class UserController
$S_email = $_POST["email"];
if (!empty($S_email) && filter_var($S_email, FILTER_VALIDATE_EMAIL)) {
$O_userModel->updateEmailByID($_SESSION["ID"], $_POST["email"]);
}else{
throw new HTTPSpecialCaseException(400, "invalid email");
}
}
if (isset($_POST["username"])) {
$S_username = $_POST["username"];
if (!empty($S_username)) {
$O_userModel->updateUsernameByID($_SESSION["ID"], $_POST["username"]);
}else{
throw new HTTPSpecialCaseException(400, "invalid username");
}
}
@ -183,13 +192,13 @@ final class UserController
public function profilePicAction(Array $A_urlParams = null, Array $A_postParams = null)
{
if (count($A_urlParams) !== 1 ) die();
if (count($A_urlParams) !== 1 ) throw new HTTPSpecialCaseException(404);
$O_userModel = new UserModel();
$A_user = $O_userModel->getUserByID($A_urlParams[0]);
if (!isset($A_user)) {
die();
throw new HTTPSpecialCaseException(404);
}
header("Content-Type: image/png");

View File

@ -9,13 +9,13 @@ final class Utils
public static function getOrDie($DICT, $key)
{
if (isset($DICT[$key])) return $DICT[$key];
else die("Key $key not present");
else throw new HTTPSpecialCaseException(400, "Key $key not present");
}
public static function intOrDie($data)
{
if (is_numeric($data)) return (int) $data;
else die("Not an int");
else throw new HTTPSpecialCaseException(400, "Not an int");
}
}

View File

@ -61,7 +61,7 @@ final class Session
{
if (!self::is_login()) {
header("Location: /user/login?return_uri=".$_SERVER["REQUEST_URI"]);
die();
throw new HTTPSpecialCaseException(403);
}
}
@ -76,8 +76,7 @@ final class Session
Session::login_or_die();
if (!self::is_admin()) {
header("Location: /");
die();
throw new HTTPSpecialCaseException(403);
}
}