-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-total-folder-content.php
More file actions
95 lines (78 loc) · 2.97 KB
/
Copy pathget-total-folder-content.php
File metadata and controls
95 lines (78 loc) · 2.97 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
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include('./check-origin.php'); // Check CORS headers
// Connect to the database
include('./db-connection.php'); // Include the database connection code
$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';
}
}
$user_roles = array(10);
$path = $data->path;
if(isset($data->email)) {
try {
include './jwt-validation.php';
$user_roles = getUserRolesFromDatabase($data->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' => $e->getMessage()]);
}
}
try {
// If $user_roles is a JSON string, decode it. Otherwise, assume it's already an array.
if (is_string($user_roles)) {
$rolesArray = json_decode($user_roles, true);
} else {
$rolesArray = $user_roles;
}
//get total count of files that match the access_levels and the path
$sql = "SELECT COUNT(*) as `total` FROM `files` WHERE (";
foreach ($rolesArray as $role) {
$sql .= "JSON_CONTAINS(`access_level`, '$role') OR ";
}
$sql = rtrim($sql, " OR "); // Remove the last " OR "
$sql .= ") AND `path` = ? ORDER BY `date` DESC";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $path);
$stmt->execute();
$result = $stmt->get_result();
$total = $result->fetch_assoc();
echo json_encode($total);
} catch (Exception $e) {
// Return an error if something goes wrong
echo json_encode(['error' => $e->getMessage()]);
}
?>