-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproject-api.php
More file actions
286 lines (243 loc) · 8.37 KB
/
project-api.php
File metadata and controls
286 lines (243 loc) · 8.37 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
<?php
// Project API for list/detail/votes/admin edits
define('WDS_SYSTEM', true);
require_once __DIR__ . '/includes/config.php';
require_once __DIR__ . '/includes/db-config.php';
require_once __DIR__ . '/includes/projects-data.php';
if (!isset($_SESSION)) {
session_start();
}
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
// Default to JSON responses
header('Content-Type: application/json; charset=utf-8');
function json_error($message, $code = 400) {
http_response_code($code);
echo json_encode(['ok' => false, 'error' => $message]);
exit;
}
function get_slug_from_request() {
$slug = isset($_REQUEST['slug']) ? $_REQUEST['slug'] : '';
$slug = preg_replace('~[^a-zA-Z0-9_-]~', '', $slug);
if ($slug === '') {
json_error('Missing or invalid slug', 400);
}
return $slug;
}
switch ($action) {
case 'detail': {
$slug = get_slug_from_request();
$projects = loadAllProjects();
$project = findProjectBySlug($projects, $slug);
if (!$project) {
json_error('Project not found', 404);
}
$isAdmin = isLoggedInAdmin();
echo json_encode([
'ok' => true,
'project' => $project,
'isAdmin' => $isAdmin,
]);
exit;
}
case 'vote': {
$slug = get_slug_from_request();
$delta = isset($_POST['delta']) ? (int)$_POST['delta'] : 0;
if ($delta === 0) {
json_error('Invalid vote delta', 400);
}
$projects = loadAllProjects();
$found = false;
foreach ($projects as &$p) {
if (isset($p['slug']) && $p['slug'] === $slug) {
if (!isset($p['votes'])) {
$p['votes'] = 0;
}
$p['votes'] = (int)$p['votes'] + $delta; // can go below zero
$newVotes = $p['votes'];
$found = true;
break;
}
}
unset($p);
if (!$found) {
json_error('Project not found', 404);
}
if (!saveAllProjects($projects)) {
json_error('Failed to save vote', 500);
}
echo json_encode(['ok' => true, 'votes' => $newVotes]);
exit;
}
case 'save': {
if (!isLoggedInAdmin()) {
json_error('Not authorized', 403);
}
$slug = get_slug_from_request();
$title = isset($_POST['title']) ? trim($_POST['title']) : '';
$category = isset($_POST['category']) ? trim($_POST['category']) : '';
$short = isset($_POST['shortDescription']) ? trim($_POST['shortDescription']) : '';
$full = isset($_POST['fullDescription']) ? trim($_POST['fullDescription']) : '';
$votes = isset($_POST['votes']) ? (int)$_POST['votes'] : 0;
$imagesText = isset($_POST['images']) ? trim($_POST['images']) : '';
$images = [];
if ($imagesText !== '') {
$lines = preg_split('/\r?\n/', $imagesText);
foreach ($lines as $line) {
$line = trim($line);
if ($line !== '') {
$images[] = basename($line);
}
}
}
$projects = loadAllProjects();
$found = false;
foreach ($projects as &$p) {
if (isset($p['slug']) && $p['slug'] === $slug) {
if ($title !== '') {
$p['title'] = $title;
}
if ($category !== '') {
$p['category'] = $category;
}
$p['shortDescription'] = $short;
$p['fullDescription'] = $full;
$p['votes'] = $votes;
$p['images'] = $images;
$found = true;
$updatedProject = $p;
break;
}
}
unset($p);
if (!$found) {
json_error('Project not found', 404);
}
if (!saveAllProjects($projects)) {
json_error('Failed to save project', 500);
}
echo json_encode(['ok' => true, 'project' => $updatedProject]);
exit;
}
case 'delete': {
if (!isLoggedInAdmin()) {
json_error('Not authorized', 403);
}
$slug = get_slug_from_request();
$projects = loadAllProjects();
$deleted = deleteProjectBySlug($projects, $slug);
if (!$deleted) {
json_error('Project not found', 404);
}
if (!saveAllProjects($projects)) {
json_error('Failed to delete project', 500);
}
// Optionally remove images directory
$baseDir = realpath(__DIR__);
$imagesDir = $baseDir . '/assets/images/projects/' . $slug;
if (is_dir($imagesDir)) {
$items = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($imagesDir, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($items as $item) {
if ($item->isDir()) {
@rmdir($item->getRealPath());
} else {
@unlink($item->getRealPath());
}
}
@rmdir($imagesDir);
}
echo json_encode(['ok' => true]);
exit;
}
case 'uploadImages': {
if (!isLoggedInAdmin()) {
json_error('Not authorized', 403);
}
$slug = get_slug_from_request();
if (!isset($_FILES['images'])) {
json_error('No files uploaded', 400);
}
$baseDir = realpath(__DIR__);
$projectImagesDir = $baseDir . '/assets/images/projects/' . $slug;
if (!is_dir($projectImagesDir)) {
mkdir($projectImagesDir, 0775, true);
}
$projects = loadAllProjects();
$project = null;
foreach ($projects as &$p) {
if (isset($p['slug']) && $p['slug'] === $slug) {
if (!isset($p['images']) || !is_array($p['images'])) {
$p['images'] = [];
}
$project = &$p;
break;
}
}
unset($p);
if ($project === null) {
json_error('Project not found', 404);
}
$files = $_FILES['images'];
$count = is_array($files['name']) ? count($files['name']) : 0;
for ($i = 0; $i < $count; $i++) {
if ($files['error'][$i] !== UPLOAD_ERR_OK) {
continue;
}
$name = basename($files['name'][$i]);
$target = $projectImagesDir . '/' . $name;
if (move_uploaded_file($files['tmp_name'][$i], $target)) {
if (!in_array($name, $project['images'], true)) {
$project['images'][] = $name;
}
}
}
if (!saveAllProjects($projects)) {
json_error('Failed to save images', 500);
}
echo json_encode(['ok' => true, 'images' => $project['images']]);
exit;
}
case 'deleteImage': {
if (!isLoggedInAdmin()) {
json_error('Not authorized', 403);
}
$slug = get_slug_from_request();
$filename = isset($_POST['filename']) ? basename($_POST['filename']) : '';
if ($filename === '') {
json_error('Missing filename', 400);
}
$baseDir = realpath(__DIR__);
$filePath = $baseDir . '/assets/images/projects/' . $slug . '/' . $filename;
if (is_file($filePath)) {
@unlink($filePath);
}
$projects = loadAllProjects();
$project = null;
foreach ($projects as &$p) {
if (isset($p['slug']) && $p['slug'] === $slug) {
if (!isset($p['images']) || !is_array($p['images'])) {
$p['images'] = [];
}
$project = &$p;
break;
}
}
unset($p);
if ($project === null) {
json_error('Project not found', 404);
}
$project['images'] = array_values(array_filter($project['images'], function ($img) use ($filename) {
return $img !== $filename;
}));
if (!saveAllProjects($projects)) {
json_error('Failed to update project images', 500);
}
echo json_encode(['ok' => true, 'images' => $project['images']]);
exit;
}
default:
json_error('Unknown action', 400);
}
?>