Emalawi19 commited on
Commit
36d9100
·
verified ·
1 Parent(s): 861aee8

Create api/upload.php

Browse files
Files changed (1) hide show
  1. api/upload.php +104 -0
api/upload.php ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ header('Content-Type: application/json');
3
+ header('Access-Control-Allow-Origin: *');
4
+ header('Access-Control-Allow-Methods: POST, OPTIONS');
5
+ header('Access-Control-Allow-Headers: Content-Type');
6
+
7
+ if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
8
+ http_response_code(204);
9
+ exit;
10
+ }
11
+
12
+ if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
13
+ http_response_code(405);
14
+ echo json_encode(['error' => 'Method not allowed']);
15
+ exit;
16
+ }
17
+
18
+ $username = trim($_POST['username'] ?? '');
19
+
20
+ // Validate username
21
+ if (!preg_match('/^[a-z0-9]{3,32}$/', $username)) {
22
+ http_response_code(400);
23
+ echo json_encode(['error' => 'Invalid username']);
24
+ exit;
25
+ }
26
+
27
+ // Check user exists
28
+ $db = new PDO('sqlite:/data/db/platform.sqlite');
29
+ $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
30
+ $stmt = $db->prepare('SELECT id FROM users WHERE username = ?');
31
+ $stmt->execute([$username]);
32
+ if (!$stmt->fetch()) {
33
+ http_response_code(404);
34
+ echo json_encode(['error' => 'User not found']);
35
+ exit;
36
+ }
37
+
38
+ $htdocs = "/data/sites/{$username}/htdocs";
39
+
40
+ // Allowed file extensions
41
+ $allowed = [
42
+ 'php','html','htm','css','js','json','txt','xml',
43
+ 'png','jpg','jpeg','gif','webp','svg','ico',
44
+ 'woff','woff2','ttf','eot','pdf','zip'
45
+ ];
46
+
47
+ if (empty($_FILES['files'])) {
48
+ http_response_code(400);
49
+ echo json_encode(['error' => 'No files uploaded']);
50
+ exit;
51
+ }
52
+
53
+ $uploaded = [];
54
+ $errors = [];
55
+
56
+ // Normalise $_FILES array for multiple uploads
57
+ $files = $_FILES['files'];
58
+ $count = is_array($files['name']) ? count($files['name']) : 1;
59
+
60
+ for ($i = 0; $i < $count; $i++) {
61
+ $name = is_array($files['name']) ? $files['name'][$i] : $files['name'];
62
+ $tmp = is_array($files['tmp_name']) ? $files['tmp_name'][$i] : $files['tmp_name'];
63
+ $error = is_array($files['error']) ? $files['error'][$i] : $files['error'];
64
+
65
+ if ($error !== UPLOAD_ERR_OK) {
66
+ $errors[] = "{$name}: upload error code {$error}";
67
+ continue;
68
+ }
69
+
70
+ // Sanitise filename — strip any path components
71
+ $name = basename($name);
72
+ $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
73
+
74
+ if (!in_array($ext, $allowed)) {
75
+ $errors[] = "{$name}: file type .{$ext} not allowed";
76
+ continue;
77
+ }
78
+
79
+ // Enforce 10 MB per file
80
+ if (filesize($tmp) > 10 * 1024 * 1024) {
81
+ $errors[] = "{$name}: exceeds 10 MB limit";
82
+ continue;
83
+ }
84
+
85
+ $dest = "{$htdocs}/{$name}";
86
+ if (move_uploaded_file($tmp, $dest)) {
87
+ // Update files table
88
+ $stmt = $db->prepare('
89
+ INSERT INTO files (username, filepath, updated_at)
90
+ VALUES (?, ?, datetime("now"))
91
+ ON CONFLICT(username, filepath) DO UPDATE SET updated_at=datetime("now")
92
+ ');
93
+ $stmt->execute([$username, $name]);
94
+ $uploaded[] = $name;
95
+ } else {
96
+ $errors[] = "{$name}: failed to save file";
97
+ }
98
+ }
99
+
100
+ echo json_encode([
101
+ 'success' => count($uploaded) > 0,
102
+ 'uploaded' => $uploaded,
103
+ 'errors' => $errors
104
+ ]);