diff --git a/app/Http/Controllers/Scales/FileController.php b/app/Http/Controllers/Scales/FileController.php index e29939dfb..380b8e742 100644 --- a/app/Http/Controllers/Scales/FileController.php +++ b/app/Http/Controllers/Scales/FileController.php @@ -1,6 +1,6 @@ extension, Helpers::editableFiles())) { + if (!in_array($file->extension, HelperRepository::editableFiles())) { throw new DisplayException('You do not have permission to edit this type of file.'); } @@ -111,7 +111,7 @@ class FileController extends Controller $file = (object) pathinfo($file); - if(!in_array($file->extension, Helpers::editableFiles())) { + if(!in_array($file->extension, HelperRepository::editableFiles())) { throw new DisplayException('You do not have permission to edit this type of file.'); } @@ -175,7 +175,7 @@ class FileController extends Controller 'entry' => $value->name, 'directory' => trim($directory, '/'), 'extension' => pathinfo($value->name, PATHINFO_EXTENSION), - 'size' => Helpers::bytesToHuman($value->size), + 'size' => HelperRepository::bytesToHuman($value->size), 'date' => strtotime($value->modified) ]]); diff --git a/app/Http/Controllers/Server/AjaxController.php b/app/Http/Controllers/Server/AjaxController.php index 3a719ac30..fe353a3a6 100644 --- a/app/Http/Controllers/Server/AjaxController.php +++ b/app/Http/Controllers/Server/AjaxController.php @@ -6,10 +6,9 @@ use Log; use Debugbar; use Pterodactyl\Models\Server; use Pterodactyl\Models\Node; -use Pterodactyl\Http\Helpers; use Pterodactyl\Exceptions\DisplayException; -use Pterodactyl\Http\Controllers\Scales\FileController; +use Pterodactyl\Repositories; use Pterodactyl\Http\Controllers\Controller; use Illuminate\Http\Request; @@ -119,7 +118,7 @@ class AjaxController extends Controller $prevDir['link_show'] = trim($prevDir['link'], '/'); } - $controller = new FileController($uuid); + $controller = new Repositories\Daemon\FileRepository($uuid); try { $directoryContents = $controller->returnDirectoryListing($this->directory); @@ -140,7 +139,7 @@ class AjaxController extends Controller 'server' => $server, 'files' => $directoryContents->files, 'folders' => $directoryContents->folders, - 'extensions' => Helpers::editableFiles(), + 'extensions' => Repositories\HelperRepository::editableFiles(), 'directory' => $prevDir ]); @@ -159,7 +158,7 @@ class AjaxController extends Controller $server = Server::getByUUID($uuid); $this->authorize('save-files', $server); - $controller = new FileController($uuid); + $controller = new Repositories\Daemon\FileRepository($uuid); try { $controller->saveFileContents($request->input('file'), $request->input('contents')); diff --git a/app/Http/Controllers/Server/ServerController.php b/app/Http/Controllers/Server/ServerController.php index a1494f412..9785c0316 100644 --- a/app/Http/Controllers/Server/ServerController.php +++ b/app/Http/Controllers/Server/ServerController.php @@ -11,7 +11,7 @@ use Uuid; use Alert; use Pterodactyl\Exceptions\DisplayException; -use Pterodactyl\Http\Controllers\Scales\FileController; +use Pterodactyl\Repositories; use Pterodactyl\Http\Controllers\Controller; use Illuminate\Http\Request; @@ -102,7 +102,7 @@ class ServerController extends Controller $this->authorize('edit-files', $server); $fileInfo = (object) pathinfo($file); - $controller = new FileController($uuid); + $controller = new Repositories\Daemon\FileRepository($uuid); try { $fileContent = $controller->returnFileContents($file); @@ -124,7 +124,7 @@ class ServerController extends Controller 'server' => $server, 'node' => Node::find($server->node), 'file' => $file, - 'contents' => $fileContent->contents, + 'contents' => $fileContent->content, 'directory' => (in_array($fileInfo->dirname, ['.', './', '/'])) ? '/' : trim($fileInfo->dirname, '/') . '/', 'extension' => $fileInfo->extension ]); @@ -155,7 +155,7 @@ class ServerController extends Controller $download->save(); - return redirect('https://' . $node->fqdn . ':' . $node->daemonListen . '/server/download/' . $download->token); + return redirect( $node->scheme . '://' . $node->fqdn . ':' . $node->daemonListen . '/server/download/' . $download->token); } diff --git a/app/Models/Node.php b/app/Models/Node.php index 8297cc103..1f605070f 100644 --- a/app/Models/Node.php +++ b/app/Models/Node.php @@ -69,7 +69,7 @@ class Node extends Model // @TODO: Better solution to disabling verification. Security risk. self::$guzzle[$node] = new Client([ - 'base_uri' => sprintf('http%s://%s:%s/', ($nodeData->https === true) ? 's' : '', $nodeData->fqdn, $nodeData->daemonListen), + 'base_uri' => sprintf('%s://%s:%s/', $nodeData->scheme, $nodeData->fqdn, $nodeData->daemonListen), 'timeout' => 10.0, 'connect_timeout' => 5.0, 'verify' => false, diff --git a/app/Repositories/Daemon/FileRepository.php b/app/Repositories/Daemon/FileRepository.php new file mode 100644 index 000000000..9f32029bd --- /dev/null +++ b/app/Repositories/Daemon/FileRepository.php @@ -0,0 +1,191 @@ +server = Server::getByUUID($uuid); + $this->node = Node::getByID($this->server->node); + $this->client = Node::guzzleRequest($this->server->node); + $this->headers = Server::getGuzzleHeaders($uuid); + + } + + /** + * Get the contents of a requested file for the server. + * + * @param string $file + * @return string + */ + public function returnFileContents($file) + { + + if (empty($file)) { + throw new Exception('Not all parameters were properly passed to the function.'); + } + + $file = (object) pathinfo($file); + if (!in_array($file->extension, HelperRepository::editableFiles())) { + throw new DisplayException('You do not have permission to edit this type of file.'); + } + + $file->dirname = (in_array($file->dirname, ['.', './', '/'])) ? null : trim($file->dirname, '/') . '/'; + + $res = $this->client->request('GET', '/server/file/' . rawurlencode($file->dirname.$file->basename), [ + 'headers' => $this->headers + ]); + + $json = json_decode($res->getBody()); + if($res->getStatusCode() !== 200 || !isset($json->content)) { + throw new DisplayException('Scales provided a non-200 error code: HTTP\\' . $res->getStatusCode()); + } + + return $json; + + } + + /** + * Save the contents of a requested file on the Scales instance. + * + * @param string $file + * @param string $content + * @return boolean + */ + public function saveFileContents($file, $content) + { + + if (empty($file)) { + throw new Exception('A valid file and path must be specified to save a file.'); + } + + $file = (object) pathinfo($file); + + if(!in_array($file->extension, HelperRepository::editableFiles())) { + throw new DisplayException('You do not have permission to edit this type of file.'); + } + + $file->dirname = (in_array($file->dirname, ['.', './', '/'])) ? null : trim($file->dirname, '/') . '/'; + + $res = $this->client->request('POST', '/server/file/' . rawurlencode($file->dirname.$file->basename), [ + 'headers' => $this->headers, + 'json' => [ + 'content' => $content + ] + ]); + + if ($res->getStatusCode() !== 204) { + throw new DisplayException('An error occured while attempting to save this file. ' . $res->getBody()); + } + + return true; + + } + + /** + * Returns a listing of all files and folders within a specified Scales directory. + * + * @param string $directory + * @return object + */ + public function returnDirectoryListing($directory) + { + + if (empty($directory)) { + throw new Exception('A valid directory must be specified in order to list its contents.'); + } + + $res = $this->client->request('GET', '/server/directory/' . $directory, [ + 'headers' => $this->headers + ]); + + $json = json_decode($res->getBody()); + if($res->getStatusCode() !== 200) { + throw new DisplayException('An error occured while attempting to save this file. ' . $res->getBody()); + } + + // Iterate through results + $files = []; + $folders = []; + foreach($json as &$value) { + + if ($value->directory === true) { + + // @TODO Handle Symlinks + $folders = array_merge($folders, [[ + 'entry' => $value->name, + 'directory' => trim($directory, '/'), + 'size' => null, + 'date' => strtotime($value->modified) + ]]); + + } else if ($value->file === true) { + + $files = array_merge($files, [[ + 'entry' => $value->name, + 'directory' => trim($directory, '/'), + 'extension' => pathinfo($value->name, PATHINFO_EXTENSION), + 'size' => HelperRepository::bytesToHuman($value->size), + 'date' => strtotime($value->modified) + ]]); + + } + + } + + return (object) [ + 'files' => $files, + 'folders' => $folders, + ]; + + } + +} diff --git a/app/Http/Helpers.php b/app/Repositories/HelperRepository.php similarity index 94% rename from app/Http/Helpers.php rename to app/Repositories/HelperRepository.php index 7ce95eb3a..499f89f2c 100644 --- a/app/Http/Helpers.php +++ b/app/Repositories/HelperRepository.php @@ -1,8 +1,8 @@ string('https', 5)->default('http')->change(); + $table->renameColumn('https', 'scheme'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('nodes', function (Blueprint $table) { + $table->boolean('scheme')->default(false)->change(); + $table->renameColumn('scheme', 'https'); + }); + } +} diff --git a/resources/views/server/files/index.blade.php b/resources/views/server/files/index.blade.php index 1c5b05fc4..6265733bb 100644 --- a/resources/views/server/files/index.blade.php +++ b/resources/views/server/files/index.blade.php @@ -6,8 +6,8 @@ @section('content')