Emalawi19 commited on
Commit
d05c39e
·
verified ·
1 Parent(s): 0494aaf

Create api/file.php

Browse files
Files changed (1) hide show
  1. api/file.php +62 -0
api/file.php ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ header('Content-Type: application/json');
3
+ header('Access-Control-Allow-Origin: *');
4
+ header('Access-Control-Allow-Methods: GET, 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'] !== 'GET') {
13
+ http_response_code(405);
14
+ echo json_encode(['error' => 'Method not allowed']);
15
+ exit;
16
+ }
17
+
18
+ $username = trim($_GET['username'] ?? '');
19
+ $filepath = trim($_GET['filepath'] ?? '');
20
+
21
+ // Validate username
22
+ if (!preg_match('/^[a-z0-9]{3,32}$/', $username)) {
23
+ http_response_code(400);
24
+ echo json_encode(['error' => 'Invalid username']);
25
+ exit;
26
+ }
27
+
28
+ // Sanitise filepath — no directory traversal
29
+ $filepath = basename($filepath);
30
+ if (empty($filepath)) {
31
+ http_response_code(400);
32
+ echo json_encode(['error' => 'Invalid filepath']);
33
+ exit;
34
+ }
35
+
36
+ // Check user exists
37
+ $db = new PDO('sqlite:/data/db/platform.sqlite');
38
+ $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
39
+ $stmt = $db->prepare('SELECT id FROM users WHERE username = ?');
40
+ $stmt->execute([$username]);
41
+ if (!$stmt->fetch()) {
42
+ http_response_code(404);
43
+ echo json_encode(['error' => 'User not found']);
44
+ exit;
45
+ }
46
+
47
+ $dest = "/data/sites/{$username}/htdocs/{$filepath}";
48
+
49
+ if (!file_exists($dest)) {
50
+ http_response_code(404);
51
+ echo json_encode(['error' => 'File not found']);
52
+ exit;
53
+ }
54
+
55
+ $content = file_get_contents($dest);
56
+
57
+ echo json_encode([
58
+ 'success' => true,
59
+ 'username' => $username,
60
+ 'filepath' => $filepath,
61
+ 'content' => $content
62
+ ]);