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;
|
2018-01-01 19:21:10 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
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
|
|
|
{
|
|
|
|
/**
|
2018-05-13 14:50:56 +00:00
|
|
|
* Return the permission string to validate this request against.
|
2017-10-28 02:42:53 +00:00
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
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 {
|
2018-01-06 00:27:47 +00:00
|
|
|
$stats = $repository->setServer($server)->setToken($token)->getFileStat($this->route()->parameter('file'));
|
2017-09-03 21:32:52 +00:00
|
|
|
} catch (RequestException $exception) {
|
2018-01-01 19:21:10 +00:00
|
|
|
switch ($exception->getCode()) {
|
|
|
|
case 404:
|
|
|
|
throw new NotFoundHttpException;
|
|
|
|
default:
|
|
|
|
throw new DaemonConnectionException($exception);
|
|
|
|
}
|
2017-09-03 21:32:52 +00:00
|
|
|
}
|
|
|
|
|
2018-01-21 20:24:59 +00:00
|
|
|
if ((! $stats->file && ! $stats->symlink) || ! 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;
|
|
|
|
}
|
|
|
|
}
|