2016-02-15 20:21:28 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Pterodactyl - Panel
|
2017-01-24 22:57:08 +00:00
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
2016-02-15 20:21:28 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-02-15 20:21:28 +00:00
|
|
|
namespace Pterodactyl\Http\Controllers\Admin;
|
|
|
|
|
|
|
|
use DB;
|
|
|
|
use Log;
|
2016-12-07 22:46:38 +00:00
|
|
|
use Alert;
|
2016-11-09 22:58:14 +00:00
|
|
|
use Storage;
|
2016-02-15 20:21:28 +00:00
|
|
|
use Pterodactyl\Models;
|
2016-12-07 22:46:38 +00:00
|
|
|
use Illuminate\Http\Request;
|
2016-02-15 20:21:28 +00:00
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
2016-12-07 22:46:38 +00:00
|
|
|
use Pterodactyl\Repositories\ServiceRepository;
|
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException;
|
2016-02-15 20:21:28 +00:00
|
|
|
|
|
|
|
class ServiceController extends Controller
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getIndex(Request $request)
|
|
|
|
{
|
|
|
|
return view('admin.services.index', [
|
2016-02-20 21:45:13 +00:00
|
|
|
'services' => Models\Service::select(
|
|
|
|
'services.*',
|
|
|
|
DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.service = services.id) as c_servers')
|
2016-12-07 22:46:38 +00:00
|
|
|
)->get(),
|
2016-02-15 20:21:28 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getNew(Request $request)
|
|
|
|
{
|
2016-02-20 21:02:49 +00:00
|
|
|
return view('admin.services.new');
|
2016-02-15 20:21:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function postNew(Request $request)
|
|
|
|
{
|
2016-02-20 21:02:49 +00:00
|
|
|
try {
|
|
|
|
$repo = new ServiceRepository\Service;
|
|
|
|
$id = $repo->create($request->except([
|
2016-12-07 22:46:38 +00:00
|
|
|
'_token',
|
2016-02-20 21:02:49 +00:00
|
|
|
]));
|
|
|
|
Alert::success('Successfully created new service!')->flash();
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-02-20 21:02:49 +00:00
|
|
|
return redirect()->route('admin.services.service', $id);
|
|
|
|
} catch (DisplayValidationException $ex) {
|
|
|
|
return redirect()->route('admin.services.new')->withErrors(json_decode($ex->getMessage()))->withInput();
|
|
|
|
} catch (DisplayException $ex) {
|
|
|
|
Alert::danger($ex->getMessage())->flash();
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
Log::error($ex);
|
|
|
|
Alert::danger('An error occured while attempting to add a new service.')->flash();
|
|
|
|
}
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-02-20 21:02:49 +00:00
|
|
|
return redirect()->route('admin.services.new')->withInput();
|
2016-02-15 20:21:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getService(Request $request, $service)
|
|
|
|
{
|
|
|
|
return view('admin.services.view', [
|
|
|
|
'service' => Models\Service::findOrFail($service),
|
|
|
|
'options' => Models\ServiceOptions::select(
|
|
|
|
'service_options.*',
|
|
|
|
DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.option = service_options.id) as c_servers')
|
2016-12-07 22:46:38 +00:00
|
|
|
)->where('parent_service', $service)->get(),
|
2016-02-15 20:21:28 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function postService(Request $request, $service)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$repo = new ServiceRepository\Service;
|
|
|
|
$repo->update($service, $request->except([
|
2016-12-07 22:46:38 +00:00
|
|
|
'_token',
|
2016-02-15 20:21:28 +00:00
|
|
|
]));
|
|
|
|
Alert::success('Successfully updated this service.')->flash();
|
|
|
|
} catch (DisplayValidationException $ex) {
|
|
|
|
return redirect()->route('admin.services.service', $service)->withErrors(json_decode($ex->getMessage()))->withInput();
|
|
|
|
} catch (DisplayException $ex) {
|
|
|
|
Alert::danger($ex->getMessage())->flash();
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
Log::error($ex);
|
|
|
|
Alert::danger('An error occurred while attempting to update this service.')->flash();
|
|
|
|
}
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-02-15 20:21:28 +00:00
|
|
|
return redirect()->route('admin.services.service', $service)->withInput();
|
|
|
|
}
|
|
|
|
|
2016-02-20 21:23:04 +00:00
|
|
|
public function deleteService(Request $request, $service)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$repo = new ServiceRepository\Service;
|
|
|
|
$repo->delete($service);
|
|
|
|
Alert::success('Successfully deleted that service.')->flash();
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-02-20 21:23:04 +00:00
|
|
|
return redirect()->route('admin.services');
|
|
|
|
} catch (DisplayException $ex) {
|
|
|
|
Alert::danger($ex->getMessage())->flash();
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
Log::error($ex);
|
|
|
|
Alert::danger('An error was encountered while attempting to delete that service.')->flash();
|
|
|
|
}
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-02-20 21:23:04 +00:00
|
|
|
return redirect()->route('admin.services.service', $service);
|
|
|
|
}
|
|
|
|
|
2016-02-21 05:07:03 +00:00
|
|
|
public function getOption(Request $request, $service, $option)
|
2016-02-15 20:21:28 +00:00
|
|
|
{
|
|
|
|
$opt = Models\ServiceOptions::findOrFail($option);
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-02-15 20:21:28 +00:00
|
|
|
return view('admin.services.options.view', [
|
|
|
|
'service' => Models\Service::findOrFail($opt->parent_service),
|
|
|
|
'option' => $opt,
|
|
|
|
'variables' => Models\ServiceVariables::where('option_id', $option)->get(),
|
|
|
|
'servers' => Models\Server::select('servers.*', 'users.email as a_ownerEmail')
|
2017-02-02 23:21:36 +00:00
|
|
|
->join('users', 'users.id', '=', 'servers.owner_id')
|
2016-02-15 20:21:28 +00:00
|
|
|
->where('option', $option)
|
2016-12-07 22:46:38 +00:00
|
|
|
->paginate(10),
|
2016-02-15 20:21:28 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-02-21 05:07:03 +00:00
|
|
|
public function postOption(Request $request, $service, $option)
|
2016-02-15 20:21:28 +00:00
|
|
|
{
|
2016-02-20 20:59:37 +00:00
|
|
|
try {
|
|
|
|
$repo = new ServiceRepository\Option;
|
|
|
|
$repo->update($option, $request->except([
|
2016-12-07 22:46:38 +00:00
|
|
|
'_token',
|
2016-02-20 20:59:37 +00:00
|
|
|
]));
|
|
|
|
Alert::success('Option settings successfully updated.')->flash();
|
|
|
|
} catch (DisplayValidationException $ex) {
|
2016-02-21 05:07:03 +00:00
|
|
|
return redirect()->route('admin.services.option', [$service, $option])->withErrors(json_decode($ex->getMessage()))->withInput();
|
2016-02-20 20:59:37 +00:00
|
|
|
} catch (\Exception $ex) {
|
|
|
|
Log::error($ex);
|
|
|
|
Alert::danger('An error occured while attempting to modify this option.')->flash();
|
|
|
|
}
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-02-21 05:07:03 +00:00
|
|
|
return redirect()->route('admin.services.option', [$service, $option])->withInput();
|
2016-02-15 20:21:28 +00:00
|
|
|
}
|
|
|
|
|
2016-02-21 05:07:03 +00:00
|
|
|
public function deleteOption(Request $request, $service, $option)
|
2016-02-20 21:55:05 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
$service = Models\ServiceOptions::select('parent_service')->where('id', $option)->first();
|
|
|
|
$repo = new ServiceRepository\Option;
|
|
|
|
$repo->delete($option);
|
|
|
|
|
|
|
|
Alert::success('Successfully deleted that option.')->flash();
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-02-20 21:55:05 +00:00
|
|
|
return redirect()->route('admin.services.service', $service->parent_service);
|
|
|
|
} catch (DisplayException $ex) {
|
|
|
|
Alert::danger($ex->getMessage())->flash();
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
Log::error($ex);
|
|
|
|
Alert::danger('An error was encountered while attempting to delete this option.')->flash();
|
|
|
|
}
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-02-21 05:07:03 +00:00
|
|
|
return redirect()->route('admin.services.option', [$service, $option]);
|
2016-02-20 21:55:05 +00:00
|
|
|
}
|
|
|
|
|
2016-02-21 05:07:03 +00:00
|
|
|
public function postOptionVariable(Request $request, $service, $option, $variable)
|
2016-02-15 20:21:28 +00:00
|
|
|
{
|
2016-02-21 05:07:03 +00:00
|
|
|
try {
|
|
|
|
$repo = new ServiceRepository\Variable;
|
|
|
|
|
|
|
|
// Because of the way old() works on the display side we prefix all of the variables with thier ID
|
|
|
|
// We need to remove that prefix here since the repo doesn't want it.
|
2016-09-18 00:25:13 +00:00
|
|
|
$data = [
|
|
|
|
'user_viewable' => '0',
|
|
|
|
'user_editable' => '0',
|
2016-12-07 22:46:38 +00:00
|
|
|
'required' => '0',
|
2016-09-18 00:25:13 +00:00
|
|
|
];
|
2016-12-07 22:46:38 +00:00
|
|
|
foreach ($request->except(['_token']) as $id => $val) {
|
|
|
|
$data[str_replace($variable . '_', '', $id)] = $val;
|
2016-02-15 20:21:28 +00:00
|
|
|
}
|
2016-02-21 05:07:03 +00:00
|
|
|
$repo->update($variable, $data);
|
|
|
|
Alert::success('Successfully updated variable.')->flash();
|
|
|
|
} catch (DisplayValidationException $ex) {
|
|
|
|
$data = [];
|
2016-12-07 22:46:38 +00:00
|
|
|
foreach (json_decode($ex->getMessage(), true) as $id => $val) {
|
|
|
|
$data[$variable . '_' . $id] = $val;
|
2016-02-21 05:07:03 +00:00
|
|
|
}
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-02-21 05:07:03 +00:00
|
|
|
return redirect()->route('admin.services.option', [$service, $option])->withErrors((object) $data)->withInput();
|
|
|
|
} catch (DisplayException $ex) {
|
|
|
|
Alert::danger($ex->getMessage())->flash();
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
Log::error($ex);
|
|
|
|
Alert::danger('An error occurred while attempting to update this service.')->flash();
|
2016-02-15 20:21:28 +00:00
|
|
|
}
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-02-21 05:07:03 +00:00
|
|
|
return redirect()->route('admin.services.option', [$service, $option])->withInput();
|
2016-02-15 20:21:28 +00:00
|
|
|
}
|
|
|
|
|
2016-02-21 05:38:03 +00:00
|
|
|
public function getNewVariable(Request $request, $service, $option)
|
|
|
|
{
|
|
|
|
return view('admin.services.options.variable', [
|
|
|
|
'service' => Models\Service::findOrFail($service),
|
2016-12-07 22:46:38 +00:00
|
|
|
'option' => Models\ServiceOptions::where('parent_service', $service)->where('id', $option)->firstOrFail(),
|
2016-02-21 05:38:03 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function postNewVariable(Request $request, $service, $option)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$repo = new ServiceRepository\Variable;
|
|
|
|
$repo->create($option, $request->except([
|
2016-12-07 22:46:38 +00:00
|
|
|
'_token',
|
2016-02-21 05:38:03 +00:00
|
|
|
]));
|
|
|
|
Alert::success('Successfully added new variable to this option.')->flash();
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2017-01-03 22:44:48 +00:00
|
|
|
return redirect()->route('admin.services.option', [$service, $option]);
|
2016-02-21 05:38:03 +00:00
|
|
|
} catch (DisplayValidationException $ex) {
|
|
|
|
return redirect()->route('admin.services.option.variable.new', [$service, $option])->withErrors(json_decode($ex->getMessage()))->withInput();
|
|
|
|
} catch (DisplayException $ex) {
|
|
|
|
Alert::danger($ex->getMessage())->flash();
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
Log::error($ex);
|
|
|
|
Alert::danger('An error occurred while attempting to add this variable.')->flash();
|
|
|
|
}
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-02-21 05:38:03 +00:00
|
|
|
return redirect()->route('admin.services.option.variable.new', [$service, $option])->withInput();
|
|
|
|
}
|
|
|
|
|
2016-02-20 21:45:13 +00:00
|
|
|
public function newOption(Request $request, $service)
|
|
|
|
{
|
|
|
|
return view('admin.services.options.new', [
|
|
|
|
'service' => Models\Service::findOrFail($service),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function postNewOption(Request $request, $service)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$repo = new ServiceRepository\Option;
|
|
|
|
$id = $repo->create($service, $request->except([
|
2016-12-07 22:46:38 +00:00
|
|
|
'_token',
|
2016-02-20 21:45:13 +00:00
|
|
|
]));
|
|
|
|
Alert::success('Successfully created new service option.')->flash();
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-02-21 05:07:03 +00:00
|
|
|
return redirect()->route('admin.services.option', [$service, $id]);
|
2016-02-20 21:45:13 +00:00
|
|
|
} catch (DisplayValidationException $ex) {
|
|
|
|
return redirect()->route('admin.services.option.new', $service)->withErrors(json_decode($ex->getMessage()))->withInput();
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
Log::error($ex);
|
|
|
|
Alert::danger('An error occured while attempting to add this service option.')->flash();
|
|
|
|
}
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-02-20 21:45:13 +00:00
|
|
|
return redirect()->route('admin.services.option.new', $service)->withInput();
|
|
|
|
}
|
|
|
|
|
2016-02-21 05:38:03 +00:00
|
|
|
public function deleteVariable(Request $request, $service, $option, $variable)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$repo = new ServiceRepository\Variable;
|
|
|
|
$repo->delete($variable);
|
|
|
|
Alert::success('Deleted variable.')->flash();
|
|
|
|
} catch (DisplayException $ex) {
|
|
|
|
Alert::danger($ex->getMessage())->flash();
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
Log::error($ex);
|
|
|
|
Alert::danger('An error occured while attempting to delete that variable.')->flash();
|
|
|
|
}
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-02-21 05:38:03 +00:00
|
|
|
return redirect()->route('admin.services.option', [$service, $option]);
|
|
|
|
}
|
|
|
|
|
2016-11-09 22:58:14 +00:00
|
|
|
public function getConfiguration(Request $request, $serviceId)
|
|
|
|
{
|
|
|
|
$service = Models\Service::findOrFail($serviceId);
|
2016-12-14 21:56:25 +00:00
|
|
|
|
2016-11-09 22:58:14 +00:00
|
|
|
return view('admin.services.config', [
|
|
|
|
'service' => $service,
|
|
|
|
'contents' => [
|
|
|
|
'json' => Storage::get('services/' . $service->file . '/main.json'),
|
2016-12-14 21:56:25 +00:00
|
|
|
'index' => Storage::get('services/' . $service->file . '/index.js'),
|
|
|
|
],
|
2016-11-09 22:58:14 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function postConfiguration(Request $request, $serviceId)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$repo = new ServiceRepository\Service;
|
|
|
|
$repo->updateFile($serviceId, $request->except([
|
2016-12-14 21:56:25 +00:00
|
|
|
'_token',
|
2016-11-09 22:58:14 +00:00
|
|
|
]));
|
2016-12-14 21:56:25 +00:00
|
|
|
|
2016-11-09 22:58:14 +00:00
|
|
|
return response('', 204);
|
|
|
|
} catch (DisplayException $ex) {
|
|
|
|
return response()->json([
|
2016-12-14 21:56:25 +00:00
|
|
|
'error' => $ex->getMessage(),
|
2016-11-09 22:58:14 +00:00
|
|
|
], 503);
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
Log::error($ex);
|
2016-12-14 21:56:25 +00:00
|
|
|
|
2016-11-09 22:58:14 +00:00
|
|
|
return response()->json([
|
2016-12-14 21:56:25 +00:00
|
|
|
'error' => 'An error occured while attempting to save the file.',
|
2016-11-09 22:58:14 +00:00
|
|
|
], 503);
|
|
|
|
}
|
|
|
|
}
|
2016-02-15 20:21:28 +00:00
|
|
|
}
|