|
| 1 | +<?php |
| 2 | + ini_set('display_errors', 1); |
| 3 | + ini_set('display_startup_errors', 1); |
| 4 | + error_reporting(E_ALL); |
| 5 | + require_once('../../lib/config.php'); |
| 6 | + |
| 7 | + $request = http::request(); |
| 8 | + $requestTarget = $request['target']; |
| 9 | + $requestTarget = preg_replace("/^\/api/", "", $requestTarget); |
| 10 | + $path = filesystem::path($requestTarget); |
| 11 | + $dirname = dirname($path); |
| 12 | + $filename = basename($path); |
| 13 | + |
| 14 | + function guid() { |
| 15 | + return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535)); |
| 16 | + } |
| 17 | + function tagId() { |
| 18 | + return sprintf('%03d-%03d', mt_rand(0, 999), mt_rand(0, 999)); |
| 19 | + } |
| 20 | + |
| 21 | + function readRecursive($path) { |
| 22 | + $dirname = dirname($path); |
| 23 | + $filename = basename($path); |
| 24 | + if (filesystem::is_dir($path)) { |
| 25 | + $files = filesystem::scandir($path); |
| 26 | + $files = array_filter($files, function($file) { |
| 27 | + if ($file == '.' || $file == '..') { |
| 28 | + return false; |
| 29 | + } |
| 30 | + return true; |
| 31 | + }); |
| 32 | + $result = []; |
| 33 | + foreach ($files as $filename) { |
| 34 | + if (filesystem::exists($path . '/' . $filename . '/meta')) { |
| 35 | + $data = json_decode(filesystem::read($path . '/' . $filename, "meta"), true); |
| 36 | + } |
| 37 | + if (filesystem::exists($path . '/' . $filename . '/meta.json')) { |
| 38 | + $data = json_decode(filesystem::read($path . '/' . $filename, "meta.json"), true); |
| 39 | + } |
| 40 | + |
| 41 | + $data['id'] = $filename; |
| 42 | + $data['ctime'] = filesystem::ctime($path, $filename); |
| 43 | + $data['ctime-human'] = date("c", $data['ctime']); |
| 44 | + $data['contents'] = readRecursive($path . "/" . $filename); |
| 45 | + $result[] = $data; |
| 46 | + } |
| 47 | + return $result; |
| 48 | + } else { |
| 49 | + return filesystem::read($dirname, $filename); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + function deleteRecursive($path) { |
| 54 | + $dirname = dirname($path); |
| 55 | + $filename = basename($path); |
| 56 | + if (filesystem::is_dir($path)) { |
| 57 | + $files = filesystem::scandir($path); |
| 58 | + $files = array_filter($files, function($file) { |
| 59 | + if ($file == '.' || $file == '..') { |
| 60 | + return false; |
| 61 | + } |
| 62 | + return true; |
| 63 | + }); |
| 64 | + foreach ($files as $file) { |
| 65 | + deleteRecursive($path . "/" . $file); |
| 66 | + } |
| 67 | + filesystem::delete($dirname, $filename); |
| 68 | + } else { |
| 69 | + filesystem::delete($dirname, $filename); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + try { |
| 74 | + switch($request['method']) { |
| 75 | + case 'OPTIONS': |
| 76 | + output('ok',200); |
| 77 | + break; |
| 78 | + case 'GET': |
| 79 | + if (filesystem::exists($dirname . "/" . $filename)) { |
| 80 | + $result = readRecursive($path); |
| 81 | + output($result, 200); |
| 82 | + } else { |
| 83 | + filenotfound($path); |
| 84 | + } |
| 85 | + break; |
| 86 | + case 'POST': |
| 87 | + if (filesystem::exists($path)) { |
| 88 | + if (filesystem::is_dir($path)) { |
| 89 | + $exists = true; |
| 90 | + while ($exists) { |
| 91 | + $newId = tagId(); |
| 92 | + $newFilename = $newId; |
| 93 | + $exists = filesystem::exists($path . $newFilename); |
| 94 | + } |
| 95 | + |
| 96 | + // at this point, we are sure that the generated filename does not yet exists, so we can write it; |
| 97 | + filesystem::put($path, $newFilename); |
| 98 | + output($newId, 200); |
| 99 | + } |
| 100 | + } |
| 101 | + break; |
| 102 | + case 'PUT': |
| 103 | + if (filesystem::put($dirname, $filename)) { |
| 104 | + output('ok',200); |
| 105 | + } else { |
| 106 | + error(new \Exception("unexpected result from write",501)); |
| 107 | + } |
| 108 | + break; |
| 109 | + case 'DELETE': |
| 110 | + if (filesystem::exists($path) || filesystem::exists($dirname . "/" . $filename)) { |
| 111 | + deleteRecursive($path); |
| 112 | + output("deleted", 200); |
| 113 | + } else { |
| 114 | + filenotfound($path); |
| 115 | + } |
| 116 | + break; |
| 117 | + } |
| 118 | + } catch( \Exception $e) { |
| 119 | + error($e); |
| 120 | + } |
| 121 | + |
| 122 | + function output($data, $status) { |
| 123 | + http::response($status, $data); |
| 124 | + } |
| 125 | + |
| 126 | + function error($e) { |
| 127 | + output(["error" => $e->getCode(), "message" => $e->getMessage()], 501); |
| 128 | + } |
| 129 | + |
| 130 | + function filenotfound($path) { |
| 131 | + output(["error" => 404, "message" => "File not found"], 404); |
| 132 | + } |
0 commit comments