-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin_api.php
More file actions
302 lines (262 loc) · 10.6 KB
/
admin_api.php
File metadata and controls
302 lines (262 loc) · 10.6 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
/**
* Admin API Handler - UnicornXMedia (Root Version)
*/
session_start();
$config = require __DIR__ . '/config.php';
header('Content-Type: application/json');
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
$rawInput = file_get_contents('php://input');
$jsonData = json_decode($rawInput, true) ?? [];
$action = $_GET['action'] ?? $jsonData['action'] ?? '';
// Handle Status Check (Must be allowed even if not logged in to check session)
if ($action === 'check_status') {
echo json_encode(['logged_in' => isset($_SESSION['admin_logged_in'])]);
exit;
}
// Handle Login
if ($action === 'login') {
$data = !empty($jsonData) ? $jsonData : $_POST;
if (($data['username'] ?? '') === ($config['username'] ?? 'admin') &&
($data['password'] ?? '') === ($config['password'] ?? 'admin123')) {
$_SESSION['admin_logged_in'] = true;
echo json_encode(['success' => true]);
} else {
http_response_code(401);
echo json_encode(['success' => false, 'message' => 'Invalid credentials']);
}
exit;
}
// Handle Fetch Events (Public)
if ($action === 'fetch_events') {
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
if (file_exists($config['events_json_path'])) {
$json = file_get_contents($config['events_json_path']);
$events = json_decode($json, true) ?? [];
echo json_encode(['success' => true, 'events' => $events]);
} else {
echo json_encode(['success' => true, 'events' => []]);
}
exit;
}
// --- Protected Actions ---
if (!isset($_SESSION['admin_logged_in'])) {
http_response_code(403);
echo json_encode(['success' => false, 'message' => 'Unauthorized']);
exit;
}
if ($action === 'logout') {
session_destroy();
echo json_encode(['success' => true]);
exit;
}
if ($action === 'fetch_submissions') {
if (file_exists($config['json_path'])) {
$json = file_get_contents($config['json_path']);
$submissions = json_decode($json, true) ?? [];
echo json_encode(['success' => true, 'submissions' => array_reverse($submissions)]);
} else {
echo json_encode(['success' => true, 'submissions' => [], 'message' => 'No submissions yet']);
}
exit;
}
if ($action === 'save_event') {
$eventData = $jsonData['event'] ?? null;
if (!$eventData) {
echo json_encode(['success' => false, 'message' => 'Missing event data']);
exit;
}
$events = [];
if (file_exists($config['events_json_path'])) {
$json = file_get_contents($config['events_json_path']);
$events = json_decode($json, true) ?? [];
}
if (isset($eventData['id']) && $eventData['id']) {
// Update existing
foreach ($events as &$e) {
if ($e['id'] === $eventData['id']) {
$e = array_merge($e, $eventData);
break;
}
}
} else {
// Create new
$eventData['id'] = 'event_' . time();
$events[] = $eventData;
}
file_put_contents($config['events_json_path'], json_encode($events, JSON_PRETTY_PRINT));
echo json_encode(['success' => true, 'message' => 'Event saved successfully']);
exit;
}
if ($action === 'delete_event') {
$eventId = $jsonData['id'] ?? '';
if (file_exists($config['events_json_path'])) {
$json = file_get_contents($config['events_json_path']);
$events = json_decode($json, true) ?? [];
$events = array_filter($events, fn($e) => $e['id'] !== $eventId);
file_put_contents($config['events_json_path'], json_encode(array_values($events), JSON_PRETTY_PRINT));
}
echo json_encode(['success' => true]);
exit;
}
if ($action === 'set_live') {
$eventId = $jsonData['id'] ?? '';
if (file_exists($config['events_json_path'])) {
$json = file_get_contents($config['events_json_path']);
$events = json_decode($json, true) ?? [];
foreach ($events as &$e) {
$e['is_live'] = ($e['id'] === $eventId);
}
file_put_contents($config['events_json_path'], json_encode($events, JSON_PRETTY_PRINT));
}
echo json_encode(['success' => true]);
exit;
}
if ($action === 'upload_media') {
if (!isset($_FILES['file'])) {
echo json_encode(['success' => false, 'message' => 'No file uploaded']);
exit;
}
$file = $_FILES['file'];
$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
$type = strpos($file['type'], 'video') !== false ? 'videos' : 'images';
$targetDir = __DIR__ . "/assets/$type/events/";
if (!is_dir($targetDir)) mkdir($targetDir, 0777, true);
$fileName = time() . '_' . basename($file['name']);
$targetPath = $targetDir . $fileName;
if (move_uploaded_file($file['tmp_name'], $targetPath)) {
echo json_encode(['success' => true, 'path' => "assets/$type/events/$fileName", 'type' => ($type === 'videos' ? 'video' : 'image')]);
} else {
echo json_encode(['success' => false, 'message' => 'Failed to move uploaded file']);
}
exit;
}
if ($action === 'fetch_settings') {
if (file_exists($config['settings_json_path'])) {
$json = file_get_contents($config['settings_json_path']);
echo json_encode(['success' => true, 'settings' => json_decode($json, true)]);
} else {
echo json_encode(['success' => false, 'message' => 'Settings file not found']);
}
exit;
}
if ($action === 'save_settings') {
$settingsData = $jsonData['settings'] ?? null;
if ($settingsData) {
file_put_contents($config['settings_json_path'], json_encode($settingsData, JSON_PRETTY_PRINT));
echo json_encode(['success' => true]);
} else {
echo json_encode(['success' => false, 'message' => 'Missing settings data']);
}
exit;
}
if ($action === 'list_media') {
$media = [];
// Recursive iterator for images
$imgDir = __DIR__ . '/assets/images/events/';
if (is_dir($imgDir)) {
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($imgDir));
foreach ($iterator as $file) {
if ($file->isFile() && preg_match('/\.(jpg|jpeg|png|gif)$/i', $file->getFilename())) {
$relativePath = str_replace(__DIR__ . '/', '', $file->getPathname());
$media[] = ['path' => $relativePath, 'type' => 'image'];
}
}
}
// Videos
$vidDir = __DIR__ . '/assets/videos/';
if (is_dir($vidDir)) {
$vids = glob($vidDir . "*.mp4");
foreach ($vids as $vid) {
$media[] = ['path' => 'assets/videos/' . basename($vid), 'type' => 'video'];
}
}
echo json_encode(['success' => true, 'media' => $media]);
exit;
}
if ($action === 'sync_events') {
// 1. Fetch current events to preserve existing metadata
$currentEvents = [];
if (file_exists($config['events_json_path'])) {
$json = file_get_contents($config['events_json_path']);
$currentEvents = json_decode($json, true) ?? [];
}
// 2. Map existing events for quick lookup
$eventsMap = [];
foreach ($currentEvents as $e) {
$eventsMap['title_' . $e['title']] = $e;
}
$newEvents = [];
$baseDir = __DIR__ . '/assets/images/events/';
// Mappings for categories
$categoryMap = [
'Events I Covered' => 'Events Covered',
'Events I Organized' => 'Events Organized',
'👉 Public Campaigns & Crowd Engagement' => 'Public Campaigns',
'Sponsor Experience' => 'Sponsor Experience'
];
if (is_dir($baseDir)) {
$categoryFolders = array_diff(scandir($baseDir), ['.', '..']);
foreach ($categoryFolders as $catFolder) {
$catPath = $baseDir . $catFolder;
if (!is_dir($catPath)) continue;
$category = $categoryMap[$catFolder] ?? 'Events Organized';
$eventFolders = array_diff(scandir($catPath), ['.', '..']);
foreach ($eventFolders as $eventFolder) {
$eventPath = $catPath . '/' . $eventFolder;
if (!is_dir($eventPath)) continue;
$title = $eventFolder;
$existing = $eventsMap['title_' . $title] ?? null;
$id = $existing['id'] ?? 'event_' . substr(md5($title), 0, 8);
// Get all images in this folder
$images = [];
$files = array_diff(scandir($eventPath), ['.', '..']);
foreach ($files as $f) {
$ext = strtolower(pathinfo($f, PATHINFO_EXTENSION));
if (in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'webp'])) {
$images[] = 'assets/images/events/' . $catFolder . '/' . $eventFolder . '/' . $f;
}
}
if (empty($images)) continue;
$newEvents[] = [
'id' => $id,
'title' => $title,
'description' => $existing['description'] ?? 'Event description for ' . $title,
'date' => $existing['date'] ?? date('Y-m-d'),
'display_date' => $existing['display_date'] ?? $title,
'location' => $existing['location'] ?? 'Location',
'category' => $category,
'media_path' => $images[0],
'media_type' => 'image',
'images' => $images,
'is_live' => $existing['is_live'] ?? false,
'edition' => $existing['edition'] ?? '',
'guests' => $existing['guests'] ?? [],
'features' => $existing['features'] ?? []
];
}
}
}
// Preserve video events (like Pinkvilla)
foreach ($currentEvents as $e) {
if ($e['media_type'] === 'video') {
$found = false;
foreach ($newEvents as $ne) {
if ($ne['title'] === $e['title']) { $found = true; break; }
}
if (!$found) $newEvents[] = $e;
}
}
if (file_put_contents($config['events_json_path'], json_encode($newEvents, JSON_PRETTY_PRINT)) === false) {
echo json_encode(['success' => false, 'message' => 'Failed to write to events.json. Check file permissions.']);
} else {
echo json_encode(['success' => true, 'events' => $newEvents, 'message' => 'Synced ' . count($newEvents) . ' events successfully.']);
}
exit;
}
http_response_code(400);
echo json_encode(['success' => false, 'message' => 'Invalid action']);