-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbroadcast_api.php
More file actions
42 lines (36 loc) · 1.23 KB
/
broadcast_api.php
File metadata and controls
42 lines (36 loc) · 1.23 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
<?php
// Simple JSON-based broadcast system
// File: broadcast.json (created automatically)
$file = 'broadcast_data.json';
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// ADMIN: Create new broadcast
$data = json_decode(file_get_contents('php://input'), true);
if (isset($data['message'])) {
$alert = [
'message' => htmlspecialchars($data['message']), // Sanitize
'timestamp' => time(),
'active' => true
];
// Save to file
file_put_contents($file, json_encode($alert));
echo json_encode(['status' => 'success']);
} else {
echo json_encode(['status' => 'error', 'msg' => 'No message']);
}
} else {
// USER: Get latest broadcast
if (file_exists($file)) {
$data = json_decode(file_get_contents($file), true);
// Only return if it's less than 1 hour old (3600 seconds)
// This prevents old alerts from causing panic later
if (time() - $data['timestamp'] < 3600) {
echo json_encode($data);
} else {
echo json_encode(['active' => false]);
}
} else {
echo json_encode(['active' => false]);
}
}
?>