misc_pterodactyl-panel/app/Http/Controllers/Server/ServerController.php

164 lines
5.5 KiB
PHP
Raw Normal View History

<?php
2016-01-20 00:10:39 +00:00
/**
2016-01-20 21:05:16 +00:00
* Pterodactyl - Panel
2017-01-24 22:57:08 +00:00
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
2016-01-20 00:10:39 +00:00
*
2017-09-26 02:43:01 +00:00
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
2016-01-20 00:10:39 +00:00
*/
2016-12-07 22:46:38 +00:00
namespace Pterodactyl\Http\Controllers\Server;
2016-12-07 22:46:38 +00:00
use Log;
use Alert;
2016-01-23 00:40:48 +00:00
use Pterodactyl\Models;
use Illuminate\Http\Request;
2016-12-07 22:46:38 +00:00
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Exceptions\DisplayValidationException;
class ServerController extends Controller
{
2017-03-19 23:36:50 +00:00
/**
* Returns the allocation overview for a server.
*
2017-08-22 03:10:48 +00:00
* @param \Illuminate\Http\Request $request
* @param string $uuid
2017-03-19 23:36:50 +00:00
* @return \Illuminate\View\View
*/
public function getAllocation(Request $request, $uuid)
{
$server = Models\Server::byUuid($uuid);
$this->authorize('view-allocation', $server);
$server->js();
2016-01-23 00:40:48 +00:00
return view('server.settings.allocation', [
'server' => $server->load(['allocations' => function ($query) {
$query->orderBy('ip', 'asc');
$query->orderBy('port', 'asc');
}]),
'node' => $server->node,
]);
}
2017-03-19 23:36:50 +00:00
/**
* Returns the startup overview for a server.
*
2017-08-22 03:10:48 +00:00
* @param \Illuminate\Http\Request $request
* @param string $uuid
2017-03-19 23:36:50 +00:00
* @return \Illuminate\View\View
*/
public function getStartup(Request $request, $uuid)
{
$server = Models\Server::byUuid($uuid);
$this->authorize('view-startup', $server);
$server->load(['node', 'allocation', 'variables']);
$variables = Models\EggVariable::where('option_id', $server->option_id)->get();
$replacements = [
'{{SERVER_MEMORY}}' => $server->memory,
'{{SERVER_IP}}' => $server->allocation->ip,
'{{SERVER_PORT}}' => $server->allocation->port,
];
$processed = str_replace(array_keys($replacements), array_values($replacements), $server->startup);
foreach ($variables as $var) {
if ($var->user_viewable) {
$serverVar = $server->variables->where('variable_id', $var->id)->first();
$var->server_set_value = $serverVar->variable_value ?? $var->default_value;
} else {
$var->server_set_value = '[hidden]';
}
$processed = str_replace('{{' . $var->env_variable . '}}', $var->server_set_value, $processed);
}
$server->js();
return view('server.settings.startup', [
'server' => $server,
'node' => $server->node,
'variables' => $variables->where('user_viewable', 1),
'service' => $server->service,
'processedStartup' => $processed,
]);
}
2017-03-19 23:36:50 +00:00
/**
* Returns the SFTP overview for a server.
*
2017-08-22 03:10:48 +00:00
* @param \Illuminate\Http\Request $request
* @param string $uuid
2017-03-19 23:36:50 +00:00
* @return \Illuminate\View\View
*/
public function getSFTP(Request $request, $uuid)
{
$server = Models\Server::byUuid($uuid);
$this->authorize('view-sftp', $server);
$server->js();
return view('server.settings.sftp', [
'server' => $server,
'node' => $server->node,
]);
}
2017-03-19 23:36:50 +00:00
/**
* Handles changing the SFTP password for a server.
*
2017-08-22 03:10:48 +00:00
* @param \Illuminate\Http\Request $request
* @param string $uuid
2017-03-19 23:36:50 +00:00
* @return \Illuminate\Http\RedirectResponse
*/
public function postSettingsSFTP(Request $request, $uuid)
{
$server = Models\Server::byUuid($uuid);
$this->authorize('reset-sftp', $server);
try {
$repo = new ServerRepository;
$repo->updateSFTPPassword($server->id, $request->input('sftp_pass'));
Alert::success('Successfully updated this servers SFTP password.')->flash();
} catch (DisplayValidationException $ex) {
return redirect()->route('server.settings.sftp', $uuid)->withErrors(json_decode($ex->getMessage()));
} catch (DisplayException $ex) {
Alert::danger($ex->getMessage())->flash();
} catch (\Exception $ex) {
Log::error($ex);
Alert::danger('An unknown error occured while attempting to update this server\'s SFTP settings.')->flash();
}
2016-12-07 22:46:38 +00:00
return redirect()->route('server.settings.sftp', $uuid);
}
2017-03-19 23:36:50 +00:00
/**
* Handles changing the startup settings for a server.
*
2017-08-22 03:10:48 +00:00
* @param \Illuminate\Http\Request $request
* @param string $uuid
2017-03-19 23:36:50 +00:00
* @return \Illuminate\Http\RedirectResponse
*/
2016-01-23 00:40:48 +00:00
public function postSettingsStartup(Request $request, $uuid)
{
$server = Models\Server::byUuid($uuid);
2016-01-23 00:40:48 +00:00
$this->authorize('edit-startup', $server);
try {
$repo = new ServerRepository;
$repo->updateStartup($server->id, $request->except('_token'));
2016-01-23 00:40:48 +00:00
Alert::success('Server startup variables were successfully updated.')->flash();
} catch (DisplayValidationException $ex) {
return redirect()->route('server.settings.startup', $uuid)->withErrors(json_decode($ex->getMessage()));
2016-01-23 00:40:48 +00:00
} catch (DisplayException $ex) {
Alert::danger($ex->getMessage())->flash();
2016-12-07 22:46:38 +00:00
} catch (\Exception $ex) {
2016-01-23 00:40:48 +00:00
Log::error($ex);
Alert::danger('An unhandled exception occured while attemping to update startup variables for this server. Please try again.')->flash();
}
2016-12-07 22:46:38 +00:00
2017-02-16 19:04:15 +00:00
return redirect()->route('server.settings.startup', $uuid);
2016-01-23 00:40:48 +00:00
}
}