-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmp3chapters-log.php
45 lines (36 loc) · 1.55 KB
/
mp3chapters-log.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
<?php
// Log export events (without identifying information) to a JSONL file
$filename = 'mp3chapters-log.jsonl';
$jsonPayload = file_get_contents('php://input');
$data = json_decode($jsonPayload, true); // Decode as an associative array
$data['date'] = date('Y-m-d'); // Gets the current date in ISO format (YYYY-MM-DD)
// Function to validate the structure of the data
function isValidDataStructure($data) {
// Define the expected keys and their value types
$expectedKeys = [
'durationMinutes' => 'integer',
'numChapters' => 'integer',
'usedImages' => 'boolean',
'usedURLs' => 'boolean',
'changedID3Fields' => 'boolean',
'changedCoverImage' => 'boolean',
];
// Check if all expected keys are present and have the correct type
foreach ($expectedKeys as $key => $type) {
if (!array_key_exists($key, $data) || gettype($data[$key]) !== $type) {
return false; // Key missing or incorrect type
}
}
return true; // Data structure is valid
}
if (json_last_error() === JSON_ERROR_NONE && isValidDataStructure($data)) {
$file = fopen($filename, 'a');
fwrite($file, json_encode($data, JSON_UNESCAPED_SLASHES) . PHP_EOL);
fclose($file);
echo json_encode(["message" => "Data appended successfully"]);
} else {
// Respond with an error message if JSON parsing fails or data structure is invalid
$errorMessage = json_last_error() !== JSON_ERROR_NONE ? 'Invalid JSON data' : 'Data structure is not as expected';
echo json_encode(["error" => $errorMessage]);
}
?>