-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_guest_list.php
96 lines (84 loc) · 3.54 KB
/
load_guest_list.php
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
<?php
header('Content-Type: application/json');
// Define a simple token for authentication (Replace with a secure method in production)
define('API_TOKEN', 'f6210ea7848bf16861d850de1b7f5449ce1e2b4bfdf6b27bfeeb9019b0645f93');
// Initialize the response array
$response = [];
// Function to log errors
function log_error($message) {
// Customize the log file path as needed
error_log(date('[Y-m-d H:i e] ') . $message . PHP_EOL, 3, 'error_log.txt');
}
// Handle preflight requests if any (optional based on your setup)
// Uncomment the following lines if you anticipate preflight requests
/*
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit;
}
*/
// Authenticate the request using the Authorization header
$headers = getallheaders();
if (!isset($headers['Authorization']) || $headers['Authorization'] !== 'Bearer ' . API_TOKEN) {
http_response_code(401);
$response = ['status' => 'error', 'message' => 'Unauthorized'];
echo json_encode($response);
log_error("Unauthorized access attempt.");
exit;
}
$file = 'guest_list.json'; // The JSON file containing guest list data
$filePath = __DIR__ . DIRECTORY_SEPARATOR . $file;
if (file_exists($filePath)) {
// Open the file with locking to prevent concurrent reads/writes
$fp = fopen($filePath, 'r');
if ($fp) {
if (flock($fp, LOCK_SH)) { // Acquire a shared lock
// Optional: Check if file size is manageable
$fileSize = filesize($filePath);
$maxSize = 5 * 1024 * 1024; // 5 MB
if ($fileSize > $maxSize) {
http_response_code(413); // Payload Too Large
$response = ['status' => 'error', 'message' => 'Data payload is too large'];
echo json_encode($response);
log_error("Data payload too large: $filePath.");
flock($fp, LOCK_UN);
fclose($fp);
exit;
}
$data = fread($fp, $fileSize);
flock($fp, LOCK_UN); // Release the lock
fclose($fp);
if ($data !== false) {
// Validate JSON
json_decode($data);
if (json_last_error() !== JSON_ERROR_NONE) {
http_response_code(500);
$response = ['status' => 'error', 'message' => 'Corrupted JSON data'];
echo json_encode($response);
log_error("Corrupted JSON data in $filePath.");
exit;
}
$response = ['status' => 'success', 'data' => json_decode($data, true)];
} else {
http_response_code(500);
$response = ['status' => 'error', 'message' => 'Error reading data'];
log_error("Error reading data from $filePath.");
}
} else {
fclose($fp);
http_response_code(500);
$response = ['status' => 'error', 'message' => 'Could not lock the file for reading'];
log_error("Could not lock the file for reading: $filePath.");
}
} else {
http_response_code(500);
$response = ['status' => 'error', 'message' => 'Error opening file for reading'];
log_error("Failed to open file for reading: $filePath.");
}
} else {
http_response_code(404);
$response = ['status' => 'error', 'message' => 'No saved guest list found'];
log_error("File not found: $filePath.");
}
echo json_encode($response);
?>