-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
50 lines (41 loc) · 1.44 KB
/
api.php
File metadata and controls
50 lines (41 loc) · 1.44 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
<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET');
header('Access-Control-Allow-Headers: Content-Type');
$cacheFile = __DIR__ . '/cache/hackerspaces_cache.json';
if (!file_exists($cacheFile)) {
http_response_code(503);
echo json_encode([
'error' => 'Cache not found',
'message' => 'Please run update_cache.php first'
]);
exit;
}
$cacheAge = time() - filemtime($cacheFile);
$cacheAgeHours = round($cacheAge / 3600, 1);
$cacheData = json_decode(file_get_contents($cacheFile), true);
if (!$cacheData) {
http_response_code(500);
echo json_encode([
'error' => 'Invalid cache',
'message' => 'Cache file is corrupted'
]);
exit;
}
$cacheData['cache_age_hours'] = $cacheAgeHours;
$cacheData['cache_age_text'] = $cacheAgeHours < 1
? round($cacheAge / 60) . ' minutes ago'
: $cacheAgeHours . ' hours ago';
if (isset($_GET['state'])) {
$stateFilter = $_GET['state'];
$validStates = ['open', 'closed', 'down'];
if (in_array($stateFilter, $validStates)) {
$cacheData['spaces'] = array_filter($cacheData['spaces'], function($space) use ($stateFilter) {
return $space['state'] === $stateFilter;
});
$cacheData['spaces'] = array_values($cacheData['spaces']);
$cacheData['filtered_by'] = $stateFilter;
}
}
echo json_encode($cacheData, JSON_UNESCAPED_UNICODE);