2017-09-03 21:32:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Server\Files;
|
|
|
|
|
2017-10-28 02:42:53 +00:00
|
|
|
use Illuminate\View\View;
|
2017-09-03 21:41:03 +00:00
|
|
|
use Illuminate\Http\Request;
|
2017-10-28 02:42:53 +00:00
|
|
|
use Illuminate\Http\Response;
|
2017-09-03 21:32:52 +00:00
|
|
|
use GuzzleHttp\Exception\RequestException;
|
2017-09-03 21:41:03 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
2017-09-03 21:32:52 +00:00
|
|
|
use Illuminate\Contracts\Config\Repository as ConfigRepository;
|
|
|
|
use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface;
|
2017-10-28 02:42:53 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
2017-09-03 21:32:52 +00:00
|
|
|
|
|
|
|
class RemoteRequestController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Config\Repository
|
|
|
|
*/
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface
|
|
|
|
*/
|
2017-10-28 02:42:53 +00:00
|
|
|
protected $repository;
|
2017-09-03 21:32:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* RemoteRequestController constructor.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Contracts\Config\Repository $config
|
2017-10-28 02:42:53 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface $repository
|
2017-09-03 21:32:52 +00:00
|
|
|
*/
|
2017-10-28 02:42:53 +00:00
|
|
|
public function __construct(ConfigRepository $config, FileRepositoryInterface $repository)
|
|
|
|
{
|
2017-09-03 21:32:52 +00:00
|
|
|
$this->config = $config;
|
2017-10-28 02:42:53 +00:00
|
|
|
$this->repository = $repository;
|
2017-09-03 21:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a listing of a servers file directory.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
2017-10-28 02:42:53 +00:00
|
|
|
* @return \Illuminate\View\View
|
2017-09-03 21:32:52 +00:00
|
|
|
*
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
2017-10-28 02:42:53 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
2017-09-03 21:32:52 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
2017-10-28 02:42:53 +00:00
|
|
|
public function directory(Request $request): View
|
2017-09-03 21:32:52 +00:00
|
|
|
{
|
2017-10-28 02:42:53 +00:00
|
|
|
$server = $request->attributes->get('server');
|
2017-09-03 21:32:52 +00:00
|
|
|
$this->authorize('list-files', $server);
|
|
|
|
|
|
|
|
$requestDirectory = '/' . trim(urldecode($request->input('directory', '/')), '/');
|
|
|
|
$directory = [
|
|
|
|
'header' => $requestDirectory !== '/' ? $requestDirectory : '',
|
|
|
|
'first' => $requestDirectory !== '/',
|
|
|
|
];
|
|
|
|
|
|
|
|
$goBack = explode('/', trim($requestDirectory, '/'));
|
|
|
|
if (! empty(array_filter($goBack)) && count($goBack) >= 2) {
|
|
|
|
array_pop($goBack);
|
|
|
|
|
|
|
|
$directory['show'] = true;
|
|
|
|
$directory['link'] = '/' . implode('/', $goBack);
|
|
|
|
$directory['link_show'] = implode('/', $goBack) . '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2018-01-06 00:27:47 +00:00
|
|
|
$listing = $this->repository->setServer($server)->setToken($request->attributes->get('server_token'))->getDirectory($requestDirectory);
|
2017-09-03 21:32:52 +00:00
|
|
|
} catch (RequestException $exception) {
|
2017-10-28 02:42:53 +00:00
|
|
|
throw new DaemonConnectionException($exception);
|
2017-09-03 21:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return view('server.files.list', [
|
|
|
|
'files' => $listing['files'],
|
|
|
|
'folders' => $listing['folders'],
|
|
|
|
'editableMime' => $this->config->get('pterodactyl.files.editable'),
|
|
|
|
'directory' => $directory,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Put the contents of a file onto the daemon.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
2017-10-28 02:42:53 +00:00
|
|
|
* @return \Illuminate\Http\Response
|
2017-09-03 21:32:52 +00:00
|
|
|
*
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
2017-10-28 02:42:53 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
2017-09-03 21:32:52 +00:00
|
|
|
*/
|
2017-10-28 02:42:53 +00:00
|
|
|
public function store(Request $request): Response
|
2017-09-03 21:32:52 +00:00
|
|
|
{
|
2017-10-28 02:42:53 +00:00
|
|
|
$server = $request->attributes->get('server');
|
2017-09-03 21:32:52 +00:00
|
|
|
$this->authorize('save-files', $server);
|
|
|
|
|
|
|
|
try {
|
2018-01-06 00:27:47 +00:00
|
|
|
$this->repository->setServer($server)->setToken($request->attributes->get('server_token'))
|
2018-01-01 19:21:10 +00:00
|
|
|
->putContent($request->input('file'), $request->input('contents') ?? '');
|
2017-09-03 21:32:52 +00:00
|
|
|
|
|
|
|
return response('', 204);
|
|
|
|
} catch (RequestException $exception) {
|
2017-10-28 02:42:53 +00:00
|
|
|
throw new DaemonConnectionException($exception);
|
2017-09-03 21:32:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|