Repository navigation
Expand file tree
/
Copy pathapi.php
More file actions
79 lines (68 loc) · 2.19 KB
/
Copy pathapi.php
File metadata and controls
79 lines (68 loc) · 2.19 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
<?php
// Prevent PHP warnings from breaking JSON
error_reporting(E_ERROR | E_PARSE);
header('Content-Type: application/json');
$svgDirectory = __DIR__ . '/svgs';
// Ensure directory exists
if (!is_dir($svgDirectory)) {
if (!mkdir($svgDirectory)) {
echo json_encode(['error' => 'Could not create svgs directory']);
exit;
}
}
function getSafePath($dir, $filename)
{
// Basic sanitization
$name = basename($filename);
if (!$name || $name === '.' || $name === '..') { return false;
}
return $dir . '/' . $name;
}
if (isset($_GET['action'])) {
$action = $_GET['action'];
if ($action === 'list' || $action === 'load_all') {
$files = glob($svgDirectory . '/*.svg');
if ($files === false) { $files = [];
}
$result = array_map(
function ($path) {
return [
'name' => basename($path),
'content' => file_get_contents($path),
'mtime' => filemtime($path)
];
}, $files
);
echo json_encode($result);
exit;
}
if ($action === 'save') {
$raw = file_get_contents('php://input');
$input = json_decode($raw, true);
if ($input && isset($input['file']) && isset($input['content'])) {
$path = getSafePath($svgDirectory, $input['file']);
if ($path && strpos($input['content'], '<svg') !== false) {
file_put_contents($path, $input['content']);
echo json_encode(['success' => true, 'mtime' => filemtime($path)]);
} else {
http_response_code(400);
echo json_encode(['error' => 'Invalid SVG content or filename']);
}
} else {
http_response_code(400);
echo json_encode(['error' => 'Missing data']);
}
exit;
}
if ($action === 'poll_all') {
$files = glob($svgDirectory . '/*.svg');
if ($files === false) $files = [];
$result = [];
foreach ($files as $path) {
$result[basename($path)] = filemtime($path);
}
echo json_encode($result);
exit;
}
}
?>