misc_pterodactyl-panel/app/Http/Controllers/Admin/ServersController.php
Dane Everitt 22b0bbf6ce Model fixing, moving things around to improve code.
Adds unique UUID generator, moves functions into repositories for
adding servers and users, cleans up code, adding more comments.
2015-12-13 22:22:16 -05:00

157 lines
4.2 KiB
PHP

<?php
namespace Pterodactyl\Http\Controllers\Admin;
use Debugbar;
use Pterodactyl\Repositories\ServerRepository;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Node;
use Pterodactyl\Models\Location;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Models\Service;
use Pterodactyl\Models\ServiceOptions;
use Pterodactyl\Models\ServiceVariables;
use Pterodactyl\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ServersController extends Controller
{
/**
* Controller Constructor
*/
public function __construct()
{
// All routes in this controller are protected by the authentication middleware.
$this->middleware('auth');
$this->middleware('admin');
}
public function getIndex(Request $request)
{
return view('admin.servers.index', [
'servers' => Server::select('servers.*', 'nodes.name as a_nodeName', 'users.email as a_ownerEmail')
->join('nodes', 'servers.node', '=', 'nodes.id')
->join('users', 'servers.owner', '=', 'users.id')
->paginate(20),
]);
}
public function getNew(Request $request)
{
return view('admin.servers.new', [
'locations' => Location::all(),
'services' => Service::all()
]);
}
public function getView(Request $request, $id)
{
//
}
public function postNewServer(Request $request)
{
try {
$server = new ServerRepository;
$resp = $server->create($request->all());
echo $resp . '<br />';
} catch (\Exception $e) {
Debugbar::addException($e);
}
return json_encode($request->all());
}
/**
* Returns a JSON tree of all avaliable nodes in a given location.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\View\View
*/
public function postNewServerGetNodes(Request $request)
{
if(!$request->input('location')) {
return response()->json([
'error' => 'Missing location in request.'
], 500);
}
return response()->json(Node::select('id', 'name', 'public')->where('location', $request->input('location'))->get());
}
/**
* Returns a JSON tree of all avaliable IPs and Ports on a given node.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\View\View
*/
public function postNewServerGetIps(Request $request)
{
if(!$request->input('node')) {
return response()->json([
'error' => 'Missing node in request.'
], 500);
}
$ips = Allocation::where('node', $request->input('node'))->whereNull('assigned_to')->get();
$listing = [];
foreach($ips as &$ip) {
if (array_key_exists($ip->ip, $listing)) {
$listing[$ip->ip] = array_merge($listing[$ip->ip], [$ip->port]);
} else {
$listing[$ip->ip] = [$ip->port];
}
}
return response()->json($listing);
}
/**
* Returns a JSON tree of all avaliable options for a given service.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\View\View
*/
public function postNewServerServiceOptions(Request $request)
{
if(!$request->input('service')) {
return response()->json([
'error' => 'Missing service in request.'
], 500);
}
return response()->json(ServiceOptions::select('id', 'name', 'docker_image')->where('parent_service', $request->input('service'))->orderBy('name', 'asc')->get());
}
/**
* Returns a JSON tree of all avaliable variables for a given service option.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\View\View
*/
public function postNewServerServiceVariables(Request $request)
{
if(!$request->input('option')) {
return response()->json([
'error' => 'Missing option in request.'
], 500);
}
return response()->json(ServiceVariables::where('option_id', $request->input('option'))->get());
}
}