2017-09-03 21:32:52 +00:00
|
|
|
<?php
|
2017-09-26 02:43:01 +00:00
|
|
|
/**
|
2017-09-03 21:32:52 +00:00
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 02:43:01 +00:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-09-03 21:32:52 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Requests\Server;
|
|
|
|
|
|
|
|
use GuzzleHttp\Exception\RequestException;
|
|
|
|
use Illuminate\Contracts\Config\Repository;
|
2017-09-03 21:41:03 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\Server\FileSizeTooLargeException;
|
2017-09-03 21:32:52 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface;
|
2017-09-03 21:41:03 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\Server\FileTypeNotEditableException;
|
2017-10-28 02:42:53 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
2017-09-03 21:32:52 +00:00
|
|
|
|
2017-10-28 02:42:53 +00:00
|
|
|
class UpdateFileContentsFormRequest extends ServerFormRequest
|
2017-09-03 21:32:52 +00:00
|
|
|
{
|
|
|
|
/**
|
2017-10-28 02:42:53 +00:00
|
|
|
* Return the permission string to validate this request aganist.
|
|
|
|
*
|
|
|
|
* @return string
|
2017-09-03 21:32:52 +00:00
|
|
|
*/
|
2017-10-28 02:42:53 +00:00
|
|
|
protected function permission(): string
|
|
|
|
{
|
|
|
|
return 'edit-files';
|
|
|
|
}
|
2017-09-03 21:32:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Authorize a request to edit a file.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Server\FileSizeTooLargeException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Server\FileTypeNotEditableException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
|
|
|
public function authorize()
|
|
|
|
{
|
2017-10-28 02:42:53 +00:00
|
|
|
if (! parent::authorize()) {
|
2017-09-03 21:32:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-28 02:42:53 +00:00
|
|
|
$server = $this->attributes->get('server');
|
|
|
|
$token = $this->attributes->get('server_token');
|
|
|
|
|
2017-09-03 21:32:52 +00:00
|
|
|
return $this->checkFileCanBeEdited($server, $token);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function rules()
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-28 02:42:53 +00:00
|
|
|
* Checks if a given file can be edited by a user on this server.
|
2017-09-03 21:32:52 +00:00
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @param string $token
|
|
|
|
* @return bool
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Server\FileSizeTooLargeException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Server\FileTypeNotEditableException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
2017-10-28 02:42:53 +00:00
|
|
|
private function checkFileCanBeEdited($server, $token)
|
2017-09-03 21:32:52 +00:00
|
|
|
{
|
|
|
|
$config = app()->make(Repository::class);
|
|
|
|
$repository = app()->make(FileRepositoryInterface::class);
|
|
|
|
|
|
|
|
try {
|
2017-10-28 02:42:53 +00:00
|
|
|
$stats = $repository->setNode($server->node_id)->setAccessServer($server->uuid)
|
2017-09-03 21:32:52 +00:00
|
|
|
->setAccessToken($token)
|
|
|
|
->getFileStat($this->route()->parameter('file'));
|
|
|
|
} catch (RequestException $exception) {
|
2017-10-28 02:42:53 +00:00
|
|
|
throw new DaemonConnectionException($exception);
|
2017-09-03 21:32:52 +00:00
|
|
|
}
|
|
|
|
|
2017-10-28 02:42:53 +00:00
|
|
|
if (! $stats->file || ! in_array($stats->mime, $config->get('pterodactyl.files.editable'))) {
|
2017-09-03 21:32:52 +00:00
|
|
|
throw new FileTypeNotEditableException(trans('server.files.exceptions.invalid_mime'));
|
|
|
|
}
|
|
|
|
|
2017-10-28 02:42:53 +00:00
|
|
|
if ($stats->size > $config->get('pterodactyl.files.max_edit_size')) {
|
2017-09-03 21:32:52 +00:00
|
|
|
throw new FileSizeTooLargeException(trans('server.files.exceptions.max_size'));
|
|
|
|
}
|
|
|
|
|
2017-10-28 02:42:53 +00:00
|
|
|
$this->attributes->set('file_stats', $stats);
|
|
|
|
|
2017-09-03 21:32:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|