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;
|
|
|
|
|
2017-09-03 21:41:03 +00:00
|
|
|
use Illuminate\Log\Writer;
|
|
|
|
use Illuminate\Contracts\Session\Session;
|
2017-09-03 21:32:52 +00:00
|
|
|
use GuzzleHttp\Exception\RequestException;
|
|
|
|
use Illuminate\Contracts\Config\Repository;
|
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
|
|
|
use Pterodactyl\Http\Requests\FrontendUserFormRequest;
|
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-09-03 21:32:52 +00:00
|
|
|
|
|
|
|
class UpdateFileContentsFormRequest extends FrontendUserFormRequest
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
protected $stats;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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()
|
|
|
|
{
|
|
|
|
parent::authorize();
|
|
|
|
|
|
|
|
$session = app()->make(Session::class);
|
|
|
|
$server = $session->get('server_data.model');
|
|
|
|
$token = $session->get('server_data.token');
|
|
|
|
|
|
|
|
$permission = $this->user()->can('edit-files', $server);
|
|
|
|
if (! $permission) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->checkFileCanBeEdited($server, $token);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function rules()
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the file stats from the Daemon.
|
|
|
|
*
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
public function getStats()
|
|
|
|
{
|
|
|
|
return $this->stats;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
protected function checkFileCanBeEdited($server, $token)
|
|
|
|
{
|
|
|
|
$config = app()->make(Repository::class);
|
|
|
|
$repository = app()->make(FileRepositoryInterface::class);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->stats = $repository->setNode($server->node_id)
|
|
|
|
->setAccessServer($server->uuid)
|
|
|
|
->setAccessToken($token)
|
|
|
|
->getFileStat($this->route()->parameter('file'));
|
|
|
|
} catch (RequestException $exception) {
|
|
|
|
$response = $exception->getResponse();
|
|
|
|
app()->make(Writer::class)->warning($exception);
|
|
|
|
|
|
|
|
throw new DisplayException(trans('exceptions.daemon_connection_failed', [
|
|
|
|
'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(),
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $this->stats->file || ! in_array($this->stats->mime, $config->get('pterodactyl.files.editable'))) {
|
|
|
|
throw new FileTypeNotEditableException(trans('server.files.exceptions.invalid_mime'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->stats->size > $config->get('pterodactyl.files.max_edit_size')) {
|
|
|
|
throw new FileSizeTooLargeException(trans('server.files.exceptions.max_size'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|