-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-bin-content.php
More file actions
145 lines (115 loc) · 5.06 KB
/
Copy pathget-bin-content.php
File metadata and controls
145 lines (115 loc) · 5.06 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include('./jwt-validation.php'); // Include the JWT validation code
// Connect to the database
include('./db-connection.php'); // Include the database connection code
include './ssh_connect.php';
$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->email) {
$user_role;
try {
$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 {
// check if the user role is 0
$rolesArray = json_decode($user_roles, true);
if(in_array(0, $rolesArray)){
$offset = $data->page * 20;
$stmt = $conn->prepare("SELECT * FROM `files_in_trash` ORDER BY `date` DESC LIMIT 20 OFFSET $offset");
$stmt->execute();
$result = $stmt->get_result();
$files = array();
// Establish SSH connection
$ssh = sshConnect();
$sftp = $ssh['sftp'];
while($row = $result->fetch_assoc()) {
$remote_file_path = $row[$data->size];
// Fetch file contents from the remote server
$file_data = @file_get_contents("ssh2.sftp://$sftp$remote_file_path");
if ($file_data === FALSE) {
error_log("Failed to get file contents for: " .$row[$data->size]);
$pdf_file_path = $row['file_path'];
// Check if the file is a PDF
if (mime_content_type("ssh2.sftp://$sftp$pdf_file_path") === 'application/pdf') {
try {
// Create a local temporary file
$temp_file = tempnam(sys_get_temp_dir(), 'pdf_');
file_put_contents($temp_file, file_get_contents("ssh2.sftp://$sftp$pdf_file_path"));
// Create a new Imagick instance
$imagick = new Imagick();
// Read the first page of the PDF
$imagick->readImage($temp_file . '[0]');
// Set the format to PNG (or any other image format)
$imagick->setImageFormat('png');
// Get the image as a string
$imageData = $imagick->getImageBlob();
// Encode the image data to base64
$base64 = base64_encode($imageData);
// Clear the Imagick instance
$imagick->clear();
$row['base64'] = $base64;
} catch (Exception $e) {
error_log('Error processing PDF: ' . $e->getMessage());
$row['base64'] = null;
}
} else {
$row['base64'] = null;
}
} else {
$base64 = base64_encode($file_data);
$row['base64'] = $base64;
}
$files[] = $row;
}
echo json_encode($files);
} else {
echo json_encode(['error' => 'User not authorized']);
}
} catch (Exception $e) {
// Return an error if something goes wrong
echo json_encode(['error' => $e->getMessage()]);
}
} else {
// Return an error if the request is not POST
echo json_encode(['error' => 'Invalid request']);
}
?>