Backend for delete post
This commit is contained in:
20
website/public/API/deletePost.php
Normal file
20
website/public/API/deletePost.php
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
session_start();
|
||||||
|
|
||||||
|
require_once "../../queries/post.php";
|
||||||
|
require_once "../../queries/user.php";
|
||||||
|
|
||||||
|
if (isset($_SESSION["userID"]) and
|
||||||
|
getRoleByID($_SESSION["userID"]) != 'frozen' and
|
||||||
|
getRoleByID($_SESSION["userID"]) != 'banned') {
|
||||||
|
|
||||||
|
if (empty($_POST["postID"]) or empty($_SESSION["userID"])) {
|
||||||
|
header('HTTP/1.1 500 Non enough arguments');
|
||||||
|
}
|
||||||
|
|
||||||
|
deletePost($_POST["postID"], $_SESSION["userID"]);
|
||||||
|
return;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
echo "frozen";
|
||||||
|
}
|
||||||
@@ -19,4 +19,18 @@ function postComment(buttonValue) {
|
|||||||
).done(function (data) {
|
).done(function (data) {
|
||||||
$('#modal-response').html(fancyText(data));
|
$('#modal-response').html(fancyText(data));
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function deletePost(postID) {
|
||||||
|
var formData = [{name: "postID", value: postID}];
|
||||||
|
$.post(
|
||||||
|
"API/deletePost.php",
|
||||||
|
formData
|
||||||
|
).done(function (response) {
|
||||||
|
if (response == "frozen") {
|
||||||
|
alert("Je account is bevroren, dus je kan geen posts verwijderen. Contacteer een admin als je denkt dat dit onjuist is.");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -192,3 +192,56 @@ function deleteNietSlecht(int $postID, int $userID) {
|
|||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
return $stmt->rowCount();
|
return $stmt->rowCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function deletePost(int $postID, int $userID) {
|
||||||
|
if (checkPermissionOnPost($postID, $userID)) {
|
||||||
|
$stmt = prepareQuery("
|
||||||
|
DELETE FROM
|
||||||
|
`post`
|
||||||
|
WHERE
|
||||||
|
`postID` = :postID
|
||||||
|
");
|
||||||
|
$stmt->bindParam(":postID", $postID);
|
||||||
|
$stmt->execute();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkPermissionOnPost(int $postID, int $userID) : bool {
|
||||||
|
$getGroupID = prepareQuery("
|
||||||
|
SELECT
|
||||||
|
`author`,
|
||||||
|
`groupID`
|
||||||
|
FROM
|
||||||
|
`post`
|
||||||
|
WHERE
|
||||||
|
`postID` = :postID
|
||||||
|
");
|
||||||
|
$getGroupID->bindParam(":postID", $postID);
|
||||||
|
$getGroupID->execute();
|
||||||
|
$postinfo = $getGroupID->fetch();
|
||||||
|
|
||||||
|
if ($postinfo["groupID"] == null) {
|
||||||
|
// User post
|
||||||
|
return ($userID == $postinfo["author"]);
|
||||||
|
} else {
|
||||||
|
// Group post
|
||||||
|
$roleInGroup = getRoleInGroup($userID, $postinfo["groupID"]);
|
||||||
|
return ($roleInGroup == "mod" or $roleInGroup == "admin");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getRoleInGroup(int $userID, int $groupID) {
|
||||||
|
$stmt = prepareQuery("
|
||||||
|
SELECT
|
||||||
|
`role`
|
||||||
|
FROM
|
||||||
|
`group_member`
|
||||||
|
WHERE
|
||||||
|
`userID` = :userID AND
|
||||||
|
`groupID` = :groupID
|
||||||
|
");
|
||||||
|
$stmt->bindParam(":userID", $userID);
|
||||||
|
$stmt->bindParam(":groupID", $groupID);
|
||||||
|
$stmt->execute();
|
||||||
|
return $stmt->fetch()["role"];
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,7 +6,9 @@ session_start();
|
|||||||
?>
|
?>
|
||||||
<div class='post-header header'>
|
<div class='post-header header'>
|
||||||
<h4><?=$post['title']?></h4>
|
<h4><?=$post['title']?></h4>
|
||||||
<form method="post" onclick=""><span class="delete-post">verwijder post</span><br /></form>
|
<form onsubmit="return false;" id="deletepostform">
|
||||||
|
<button onclick="deletePost('<?=$postID?>')" type="submit">verwijder post<br /></button>
|
||||||
|
</form>
|
||||||
<span class='postinfo'>
|
<span class='postinfo'>
|
||||||
gepost door <?=$fullname?>,
|
gepost door <?=$fullname?>,
|
||||||
<span class='posttime' title='<?=$post['creationdate']?>'>
|
<span class='posttime' title='<?=$post['creationdate']?>'>
|
||||||
|
|||||||
Reference in New Issue
Block a user