-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdelete.php
More file actions
91 lines (77 loc) · 2.31 KB
/
delete.php
File metadata and controls
91 lines (77 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
session_start();
include_once("config.php");
// Function declarations
function softDeletePost($db, $pid, $uid) {
$sResp = array();
$query = "UPDATE Posts SET isHidden = true WHERE postId = $pid AND creatorId = $uid";
$result = mysqli_query($db, $query);
if ($result) {
$sRow["postId"] = $pid;
$sRow["postHidden"] = true;
$rResp[] = $sRow;
echo json_encode($rResp);
}
}
function hardDeletePost($db, $pid, $uid) {
$sResp = array();
// TODO: Need to delete file here
$query = "DELETE FROM Posts WHERE postId = '$pid';";
$result = mysqli_query($db, $query);
if ($result) {
$sRow["postId"] = $pid;
$sRow["postDeleted"] = true;
$rResp[] = $sRow;
echo json_encode($rResp);
}
}
function softDeleteNotes($db, $nid, $uid) {
$sResp = array();
$query = "UPDATE Notes SET isHidden = true WHERE notesId = $nid AND creatorId = $uid";
$result = mysqli_query($db, $query);
if ($result) {
$sRow["notesId"] = $nid;
$sRow["notesHidden"] = true;
$rResp[] = $sRow;
echo json_encode($rResp);
}
}
function hardDeleteNotes($db, $nid, $uid) {
$sResp = array();
// TODO: Need to delete file here
$query = "DELETE FROM Notes WHERE notesId = '$nid';";
$result = mysqli_query($db, $query);
if ($result) {
$sRow["notesId"] = $nid;
$sRow["notesDeleted"] = true;
$rResp[] = $sRow;
echo json_encode($rResp);
}
}
// Business code
if (isset($_SESSION["login_user"])) {
$uid = $_SESSION["login_user"];
$pid = $_POST["pid"];
$nid = $_POST["nid"];
$db = mysqli_connect(DB_HOST_NAME, DB_USER, DB_PASS, DB_NAME);
if (!$db) {
die ("Failed to connect to database: " . mysqli_connect_error());
}
// Check for Admin status
$isAdmin = false;
$query = "SELECT * FROM Administrators WHERE userId = $uid;";
$result = mysqli_query($db, $query);
if (mysqli_num_rows($result) > 0) {
$isAdmin = true;
}
if (!$isAdmin) {
if (!is_null($pid) && !is_null($nid)) break;
else if (!is_null($pid)) softDeletePost($db, $pid, $uid);
else if (!is_null($nid)) softDeleteNotes($db, $nid, $uid);
} else {
if (!is_null($pid) && !is_null($nid)) break;
else if (!is_null($pid)) hardDeletePost($db, $pid, $uid);
else if (!is_null($nid)) hardDeleteNotes($db, $nid, $uid);
}
mysqli_close($db);
}