2016-11-16 01:20:32 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Pterodactyl - Panel
|
2017-01-24 22:57:08 +00:00
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
2016-11-16 01:20:32 +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-14 21:56:25 +00:00
|
|
|
|
2016-11-16 01:20:32 +00:00
|
|
|
namespace Pterodactyl\Http\Controllers\Admin;
|
|
|
|
|
|
|
|
use Log;
|
2016-12-14 21:56:25 +00:00
|
|
|
use Alert;
|
2016-11-16 01:20:32 +00:00
|
|
|
use Storage;
|
2016-12-14 21:56:25 +00:00
|
|
|
use Illuminate\Http\Request;
|
2017-03-15 01:18:36 +00:00
|
|
|
use Pterodactyl\Models\Pack;
|
|
|
|
use Pterodactyl\Models\Service;
|
2016-12-14 21:56:25 +00:00
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
2016-11-16 01:20:32 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
2017-03-15 01:18:36 +00:00
|
|
|
use Pterodactyl\Repositories\PackRepository;
|
2016-11-16 01:20:32 +00:00
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException;
|
|
|
|
|
|
|
|
class PackController extends Controller
|
|
|
|
{
|
2017-03-15 01:18:36 +00:00
|
|
|
/**
|
|
|
|
* Display listing of all packs on the system.
|
|
|
|
*
|
2017-03-19 23:36:50 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2017-03-15 01:18:36 +00:00
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function index(Request $request)
|
2016-11-16 01:20:32 +00:00
|
|
|
{
|
2017-03-15 01:18:36 +00:00
|
|
|
$packs = Pack::with('option')->withCount('servers');
|
2016-11-16 01:20:32 +00:00
|
|
|
|
2017-03-15 01:18:36 +00:00
|
|
|
if (! is_null($request->input('query'))) {
|
|
|
|
$packs->search($request->input('query'));
|
|
|
|
}
|
2016-11-16 04:12:47 +00:00
|
|
|
|
2017-03-15 01:18:36 +00:00
|
|
|
return view('admin.packs.index', ['packs' => $packs->paginate(50)]);
|
2016-11-16 04:12:47 +00:00
|
|
|
}
|
|
|
|
|
2017-03-15 01:18:36 +00:00
|
|
|
/**
|
|
|
|
* Display new pack creation form.
|
|
|
|
*
|
2017-03-19 23:36:50 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2017-03-15 01:18:36 +00:00
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function new(Request $request)
|
2016-11-16 04:12:47 +00:00
|
|
|
{
|
2017-03-15 01:18:36 +00:00
|
|
|
return view('admin.packs.new', [
|
|
|
|
'services' => Service::with('options')->get(),
|
2016-11-16 04:12:47 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-03-15 01:18:36 +00:00
|
|
|
/**
|
|
|
|
* Display new pack creation modal for use with template upload.
|
|
|
|
*
|
2017-03-19 23:36:50 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2017-03-15 01:18:36 +00:00
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function newTemplate(Request $request)
|
2016-11-16 04:12:47 +00:00
|
|
|
{
|
2017-03-15 01:18:36 +00:00
|
|
|
return view('admin.packs.modal', [
|
|
|
|
'services' => Service::with('options')->get(),
|
2016-11-16 01:20:32 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-03-16 00:52:37 +00:00
|
|
|
/**
|
|
|
|
* Handle create pack request and route user to location.
|
|
|
|
*
|
2017-03-19 23:36:50 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2017-03-16 00:52:37 +00:00
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
2016-11-16 01:20:32 +00:00
|
|
|
public function create(Request $request)
|
|
|
|
{
|
2017-03-15 01:18:36 +00:00
|
|
|
$repo = new PackRepository;
|
2016-12-14 21:56:25 +00:00
|
|
|
|
2017-03-15 01:18:36 +00:00
|
|
|
try {
|
|
|
|
if ($request->input('action') === 'from_template') {
|
|
|
|
$pack = $repo->createWithTemplate($request->intersect(['option_id', 'file_upload']));
|
|
|
|
} else {
|
|
|
|
$pack = $repo->create($request->intersect([
|
|
|
|
'name', 'description', 'version', 'option_id',
|
|
|
|
'selectable', 'visible', 'locked', 'file_upload',
|
2016-11-18 22:31:57 +00:00
|
|
|
]));
|
|
|
|
}
|
2017-03-15 01:18:36 +00:00
|
|
|
Alert::success('Pack successfully created on the system.')->flash();
|
2016-12-14 21:56:25 +00:00
|
|
|
|
2017-03-15 01:18:36 +00:00
|
|
|
return redirect()->route('admin.packs.view', $pack->id);
|
2017-03-16 00:53:49 +00:00
|
|
|
} catch (DisplayValidationException $ex) {
|
2017-03-15 01:18:36 +00:00
|
|
|
return redirect()->route('admin.packs.new')->withErrors(json_decode($ex->getMessage()))->withInput();
|
2016-11-16 22:22:22 +00:00
|
|
|
} catch (DisplayException $ex) {
|
|
|
|
Alert::danger($ex->getMessage())->flash();
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
Log::error($ex);
|
2017-03-15 01:18:36 +00:00
|
|
|
Alert::danger('An error occured while attempting to add a new service pack. This error has been logged.')->flash();
|
2016-11-16 22:22:22 +00:00
|
|
|
}
|
2016-12-14 21:56:25 +00:00
|
|
|
|
2017-03-15 01:18:36 +00:00
|
|
|
return redirect()->route('admin.packs.new')->withInput();
|
2016-11-16 22:22:22 +00:00
|
|
|
}
|
2017-03-15 01:18:36 +00:00
|
|
|
|
2017-03-16 00:52:37 +00:00
|
|
|
/**
|
|
|
|
* Display pack view template to user.
|
|
|
|
*
|
2017-03-19 23:36:50 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param int $id
|
2017-03-16 00:52:37 +00:00
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function view(Request $request, $id)
|
|
|
|
{
|
|
|
|
return view('admin.packs.view', [
|
|
|
|
'pack' => Pack::with('servers.node', 'servers.user')->findOrFail($id),
|
|
|
|
'services' => Service::with('options')->get(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle updating or deleting pack information.
|
|
|
|
*
|
2017-03-19 23:36:50 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2017-03-16 00:52:37 +00:00
|
|
|
*/
|
|
|
|
public function update(Request $request, $id)
|
|
|
|
{
|
|
|
|
$repo = new PackRepository;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if ($request->input('action') !== 'delete') {
|
|
|
|
$pack = $repo->update($id, $request->intersect([
|
|
|
|
'name', 'description', 'version',
|
2017-03-16 00:53:49 +00:00
|
|
|
'option_id', 'selectable', 'visible', 'locked',
|
2017-03-16 00:52:37 +00:00
|
|
|
]));
|
|
|
|
Alert::success('Pack successfully updated.')->flash();
|
|
|
|
} else {
|
|
|
|
$repo->delete($id);
|
|
|
|
Alert::success('Pack was successfully deleted from the system.')->flash();
|
|
|
|
|
|
|
|
return redirect()->route('admin.packs');
|
|
|
|
}
|
2017-03-16 00:53:49 +00:00
|
|
|
} catch (DisplayValidationException $ex) {
|
2017-03-16 00:52:37 +00:00
|
|
|
return redirect()->route('admin.packs.view', $id)->withErrors(json_decode($ex->getMessage()));
|
|
|
|
} catch (DisplayException $ex) {
|
|
|
|
Alert::danger($ex->getMessage())->flash();
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
Log::error($ex);
|
|
|
|
Alert::danger('An error occured while attempting to edit this service pack. This error has been logged.')->flash();
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->route('admin.packs.view', $id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an archive of the pack and downloads it to the browser.
|
|
|
|
*
|
2017-03-19 23:36:50 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param int $id
|
|
|
|
* @param bool $files
|
|
|
|
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
|
2017-03-16 00:52:37 +00:00
|
|
|
*/
|
|
|
|
public function export(Request $request, $id, $files = false)
|
|
|
|
{
|
|
|
|
$pack = Pack::findOrFail($id);
|
|
|
|
$json = [
|
|
|
|
'name' => $pack->name,
|
|
|
|
'version' => $pack->version,
|
|
|
|
'description' => $pack->description,
|
|
|
|
'selectable' => $pack->selectable,
|
|
|
|
'visible' => $pack->visible,
|
|
|
|
'locked' => $pack->locked,
|
|
|
|
];
|
|
|
|
|
|
|
|
$filename = tempnam(sys_get_temp_dir(), 'pterodactyl_');
|
|
|
|
if ($files === 'with-files') {
|
|
|
|
$zip = new \ZipArchive;
|
|
|
|
if (! $zip->open($filename, \ZipArchive::CREATE)) {
|
|
|
|
abort(503, 'Unable to open file for writing.');
|
|
|
|
}
|
2017-03-15 01:18:36 +00:00
|
|
|
|
2017-03-16 00:52:37 +00:00
|
|
|
$files = Storage::files('packs/' . $pack->uuid);
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$zip->addFile(storage_path('app/' . $file), basename(storage_path('app/' . $file)));
|
|
|
|
}
|
|
|
|
|
|
|
|
$zip->addFromString('import.json', json_encode($json, JSON_PRETTY_PRINT));
|
|
|
|
$zip->close();
|
|
|
|
|
|
|
|
return response()->download($filename, 'pack-' . $pack->name . '.zip')->deleteFileAfterSend(true);
|
|
|
|
} else {
|
2017-03-19 23:36:50 +00:00
|
|
|
|
2017-03-16 00:52:37 +00:00
|
|
|
$fp = fopen($filename, 'a+');
|
|
|
|
fwrite($fp, json_encode($json, JSON_PRETTY_PRINT));
|
|
|
|
fclose($fp);
|
|
|
|
|
|
|
|
return response()->download($filename, 'pack-' . $pack->name . '.json', [
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
])->deleteFileAfterSend(true);
|
|
|
|
}
|
|
|
|
}
|
2016-11-16 01:20:32 +00:00
|
|
|
}
|