|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pterodactyl\Repositories\Daemon; |
| 4 | + |
| 5 | +use \Exception; |
| 6 | +use Log; |
| 7 | + |
| 8 | +use Pterodactyl\Models\Server; |
| 9 | +use Pterodactyl\Models\Node; |
| 10 | +use Pterodactyl\Repositories\HelperRepository; |
| 11 | +use Pterodactyl\Exceptions\DisplayException; |
| 12 | + |
| 13 | +use GuzzleHttp\Client; |
| 14 | +use GuzzleHttp\Exception\RequestException; |
| 15 | + |
| 16 | +class FileRepository |
| 17 | +{ |
| 18 | + |
| 19 | + /** |
| 20 | + * The Eloquent Model associated with the requested server. |
| 21 | + * |
| 22 | + * @var \Illuminate\Database\Eloquent\Model |
| 23 | + */ |
| 24 | + protected $server; |
| 25 | + |
| 26 | + /** |
| 27 | + * The Eloquent Model for the node corresponding with the requested server. |
| 28 | + * |
| 29 | + * @var \Illuminate\Database\Eloquent\Model |
| 30 | + */ |
| 31 | + protected $node; |
| 32 | + |
| 33 | + /** |
| 34 | + * The Guzzle Client associated with the requested server and node. |
| 35 | + * |
| 36 | + * @var \GuzzleHttp\Client |
| 37 | + */ |
| 38 | + protected $client; |
| 39 | + |
| 40 | + /** |
| 41 | + * The Guzzle Client headers associated with the requested server and node. |
| 42 | + * (non-administrative headers) |
| 43 | + * |
| 44 | + * @var array |
| 45 | + */ |
| 46 | + protected $headers; |
| 47 | + |
| 48 | + /** |
| 49 | + * Constructor |
| 50 | + * |
| 51 | + * @param string $server The server Short UUID |
| 52 | + */ |
| 53 | + public function __construct($uuid) |
| 54 | + { |
| 55 | + |
| 56 | + $this->server = Server::getByUUID($uuid); |
| 57 | + $this->node = Node::getByID($this->server->node); |
| 58 | + $this->client = Node::guzzleRequest($this->server->node); |
| 59 | + $this->headers = Server::getGuzzleHeaders($uuid); |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Get the contents of a requested file for the server. |
| 65 | + * |
| 66 | + * @param string $file |
| 67 | + * @return string |
| 68 | + */ |
| 69 | + public function returnFileContents($file) |
| 70 | + { |
| 71 | + |
| 72 | + if (empty($file)) { |
| 73 | + throw new Exception('Not all parameters were properly passed to the function.'); |
| 74 | + } |
| 75 | + |
| 76 | + $file = (object) pathinfo($file); |
| 77 | + if (!in_array($file->extension, HelperRepository::editableFiles())) { |
| 78 | + throw new DisplayException('You do not have permission to edit this type of file.'); |
| 79 | + } |
| 80 | + |
| 81 | + $file->dirname = (in_array($file->dirname, ['.', './', '/'])) ? null : trim($file->dirname, '/') . '/'; |
| 82 | + |
| 83 | + $res = $this->client->request('GET', '/server/file/' . rawurlencode($file->dirname.$file->basename), [ |
| 84 | + 'headers' => $this->headers |
| 85 | + ]); |
| 86 | + |
| 87 | + $json = json_decode($res->getBody()); |
| 88 | + if($res->getStatusCode() !== 200 || !isset($json->content)) { |
| 89 | + throw new DisplayException('Scales provided a non-200 error code: HTTP\\' . $res->getStatusCode()); |
| 90 | + } |
| 91 | + |
| 92 | + return $json; |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Save the contents of a requested file on the Scales instance. |
| 98 | + * |
| 99 | + * @param string $file |
| 100 | + * @param string $content |
| 101 | + * @return boolean |
| 102 | + */ |
| 103 | + public function saveFileContents($file, $content) |
| 104 | + { |
| 105 | + |
| 106 | + if (empty($file)) { |
| 107 | + throw new Exception('A valid file and path must be specified to save a file.'); |
| 108 | + } |
| 109 | + |
| 110 | + $file = (object) pathinfo($file); |
| 111 | + |
| 112 | + if(!in_array($file->extension, HelperRepository::editableFiles())) { |
| 113 | + throw new DisplayException('You do not have permission to edit this type of file.'); |
| 114 | + } |
| 115 | + |
| 116 | + $file->dirname = (in_array($file->dirname, ['.', './', '/'])) ? null : trim($file->dirname, '/') . '/'; |
| 117 | + |
| 118 | + $res = $this->client->request('POST', '/server/file/' . rawurlencode($file->dirname.$file->basename), [ |
| 119 | + 'headers' => $this->headers, |
| 120 | + 'json' => [ |
| 121 | + 'content' => $content |
| 122 | + ] |
| 123 | + ]); |
| 124 | + |
| 125 | + if ($res->getStatusCode() !== 204) { |
| 126 | + throw new DisplayException('An error occured while attempting to save this file. ' . $res->getBody()); |
| 127 | + } |
| 128 | + |
| 129 | + return true; |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Returns a listing of all files and folders within a specified Scales directory. |
| 135 | + * |
| 136 | + * @param string $directory |
| 137 | + * @return object |
| 138 | + */ |
| 139 | + public function returnDirectoryListing($directory) |
| 140 | + { |
| 141 | + |
| 142 | + if (empty($directory)) { |
| 143 | + throw new Exception('A valid directory must be specified in order to list its contents.'); |
| 144 | + } |
| 145 | + |
| 146 | + $res = $this->client->request('GET', '/server/directory/' . $directory, [ |
| 147 | + 'headers' => $this->headers |
| 148 | + ]); |
| 149 | + |
| 150 | + $json = json_decode($res->getBody()); |
| 151 | + if($res->getStatusCode() !== 200) { |
| 152 | + throw new DisplayException('An error occured while attempting to save this file. ' . $res->getBody()); |
| 153 | + } |
| 154 | + |
| 155 | + // Iterate through results |
| 156 | + $files = []; |
| 157 | + $folders = []; |
| 158 | + foreach($json as &$value) { |
| 159 | + |
| 160 | + if ($value->directory === true) { |
| 161 | + |
| 162 | + // @TODO Handle Symlinks |
| 163 | + $folders = array_merge($folders, [[ |
| 164 | + 'entry' => $value->name, |
| 165 | + 'directory' => trim($directory, '/'), |
| 166 | + 'size' => null, |
| 167 | + 'date' => strtotime($value->modified) |
| 168 | + ]]); |
| 169 | + |
| 170 | + } else if ($value->file === true) { |
| 171 | + |
| 172 | + $files = array_merge($files, [[ |
| 173 | + 'entry' => $value->name, |
| 174 | + 'directory' => trim($directory, '/'), |
| 175 | + 'extension' => pathinfo($value->name, PATHINFO_EXTENSION), |
| 176 | + 'size' => HelperRepository::bytesToHuman($value->size), |
| 177 | + 'date' => strtotime($value->modified) |
| 178 | + ]]); |
| 179 | + |
| 180 | + } |
| 181 | + |
| 182 | + } |
| 183 | + |
| 184 | + return (object) [ |
| 185 | + 'files' => $files, |
| 186 | + 'folders' => $folders, |
| 187 | + ]; |
| 188 | + |
| 189 | + } |
| 190 | + |
| 191 | +} |
0 commit comments