misc_pterodactyl-panel/app/Http/Controllers/Admin/PackController.php

216 lines
7.6 KiB
PHP
Raw Normal View History

<?php
/**
* Pterodactyl - Panel
2017-01-24 22:57:08 +00:00
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* 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
namespace Pterodactyl\Http\Controllers\Admin;
use Log;
2016-12-14 21:56:25 +00:00
use Alert;
use Storage;
2016-12-14 21:56:25 +00:00
use Illuminate\Http\Request;
use Pterodactyl\Models\Pack;
use Pterodactyl\Models\Service;
2016-12-14 21:56:25 +00:00
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Repositories\PackRepository;
use Pterodactyl\Exceptions\DisplayValidationException;
class PackController extends Controller
{
/**
* Display listing of all packs on the system.
*
2017-03-19 23:36:50 +00:00
* @param \Illuminate\Http\Request $request
* @return \Illuminate\View\View
*/
public function index(Request $request)
{
$packs = Pack::with('option')->withCount('servers');
if (! is_null($request->input('query'))) {
$packs->search($request->input('query'));
}
return view('admin.packs.index', ['packs' => $packs->paginate(50)]);
}
/**
* Display new pack creation form.
*
2017-03-19 23:36:50 +00:00
* @param \Illuminate\Http\Request $request
* @return \Illuminate\View\View
*/
public function new(Request $request)
{
return view('admin.packs.new', [
'services' => Service::with('options')->get(),
]);
}
/**
* Display new pack creation modal for use with template upload.
*
2017-03-19 23:36:50 +00:00
* @param \Illuminate\Http\Request $request
* @return \Illuminate\View\View
*/
public function newTemplate(Request $request)
{
return view('admin.packs.modal', [
'services' => Service::with('options')->get(),
]);
}
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
*/
public function create(Request $request)
{
$repo = new PackRepository;
2016-12-14 21:56:25 +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',
]));
}
Alert::success('Pack successfully created on the system.')->flash();
2016-12-14 21:56:25 +00:00
return redirect()->route('admin.packs.view', $pack->id);
2017-03-16 00:53:49 +00:00
} catch (DisplayValidationException $ex) {
return redirect()->route('admin.packs.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 pack. This error has been logged.')->flash();
}
2016-12-14 21:56:25 +00:00
return redirect()->route('admin.packs.new')->withInput();
}
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-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);
}
}
}