-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete-pressrelease.php
More file actions
129 lines (108 loc) · 4.87 KB
/
Copy pathdelete-pressrelease.php
File metadata and controls
129 lines (108 loc) · 4.87 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
// Start output buffering
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include('./jwt-validation.php'); // Include the JWT validation code
include './ssh_connect.php';
// Retrieve data from the request
$data = file_get_contents('php://input');
$data = json_decode($data);
function getUserRolesFromDatabase($user_email, $jwt) {
//get user info from the api get-user-data.php
try {
$url = $_ENV['URL_BACKEND'].'/get-user-data.php';
$data = array('email' => $user_email);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n" .
"Authorization: Bearer " . $jwt . "\r\n", // Add this line
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === false) {
// Handle the case where file_get_contents fails
http_response_code(500);
return 'Error checking user role';
}
$user = json_decode($result, true);
if ($user && isset($user['groups'])) {
return $user['groups'];
} else {
return 'User not found';
}
} catch (Exception $e) {
http_response_code(500);
return 'Error checking user role';
}
}
if ($data->id) {
try {
$user_roles = getUserRolesFromDatabase($user_email, $jwt);
} catch (Exception $e) {
// Log the error message for debugging
error_log("Error in getUserRoleFromDatabase: " . $e->getMessage());
http_response_code(500);
echo json_encode(['error' => 'Server error']);
}
//get file path from database and return the original file
try {
include('./db-connection.php'); // Include the database connection code
$stmt = $conn->prepare("SELECT * FROM `pressreleases` WHERE `ID`=?");
$stmt->bind_param("i", $data->id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 1) {
$pressrelease = $result->fetch_assoc();
$rolesArray = json_decode($user_roles, true);
if(in_array(0, $rolesArray) || in_array(1, $rolesArray)){
//delete the users from the database
$stmt = $conn->prepare("DELETE FROM `pressreleases` WHERE `ID`=?");
$stmt->bind_param("i", $data->id);
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();
$conn->close();
// Establish SSH connection and SFTP
$sshDetails = sshConnect();
$sftp = $sshDetails['sftp'];
if($pressrelease['image']){
$remote_directory = $_ENV['FILE_HOSTING_ROOT'] . '/pressrelease_images/';
//delete the image from the folder
$image = $pressrelease['image'];
if ($image != 'image-placeholder.jpg') {
$remote_image_path = $remote_directory . $image;
ssh2_sftp_unlink($sftp, $remote_image_path);
// Remove 'medium-' from the image name
$image = str_replace('medium-', '', $image);
$remote_image_path = $remote_directory . $image;
ssh2_sftp_unlink($sftp, $remote_image_path);
}
}
$file = $pressrelease['file'];
if($file){
$remote_file_directory = $_ENV['FILE_HOSTING_ROOT'] . '/pressrelease_files/';
$remote_file_path = $remote_file_directory . $file;
ssh2_sftp_unlink($sftp, $remote_file_path);
}
echo json_encode(['message' => 'success']);
} else {
//return error
echo json_encode(['error' => 'User not authorized']);
}
} else {
//return error
echo json_encode(['error' => 'User not found']);
}
} catch (Exception $e) {
http_response_code(500); // Server error
echo json_encode(['error' => 'Server error']);
// Log the error if needed
}
} else {
echo json_encode(['error' => 'Invalid request']);
}
?>