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

Create api/files.php

Browse files
Files changed (1) hide show
  1. api/files.php +60 -0
api/files.php ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
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
+ $files = [];
40
+
41
+ if (is_dir($htdocs)) {
42
+ $items = scandir($htdocs);
43
+ foreach ($items as $item) {
44
+ if ($item === '.' || $item === '..') continue;
45
+ $full = "{$htdocs}/{$item}";
46
+ if (is_file($full)) {
47
+ $files[] = [
48
+ 'name' => $item,
49
+ 'size' => filesize($full),
50
+ 'updated_at' => date('Y-m-d H:i:s', filemtime($full))
51
+ ];
52
+ }
53
+ }
54
+ }
55
+
56
+ echo json_encode([
57
+ 'success' => true,
58
+ 'username' => $username,
59
+ 'files' => $files
60
+ ]);