2015-12-06 18:58:49 +00:00
< ? php
2016-01-20 00:10:39 +00:00
/**
* Pterodactyl Panel
* Copyright ( c ) 2015 - 2016 Dane Everitt < dane @ daneeveritt . com >
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http :// www . gnu . org / licenses />.
*/
2015-12-06 18:58:49 +00:00
namespace Pterodactyl\Http\Controllers\Server ;
use Log ;
use Debugbar ;
use Pterodactyl\Models\Server ;
use Pterodactyl\Models\Node ;
use Pterodactyl\Exceptions\DisplayException ;
2016-01-01 23:08:15 +00:00
use Pterodactyl\Repositories ;
2015-12-06 18:58:49 +00:00
use Pterodactyl\Http\Controllers\Controller ;
use Illuminate\Http\Request ;
use GuzzleHttp\Client ;
use GuzzleHttp\Exception\RequestException ;
class AjaxController extends Controller
{
/**
* @ var array
*/
protected $files = [];
/**
* @ var array
*/
protected $folders = [];
/**
* @ var string
*/
protected $directory ;
/**
* Controller Constructor
*/
public function __construct ()
{
2016-01-04 21:09:39 +00:00
//
2015-12-06 18:58:49 +00:00
}
/**
* Returns true or false depending on the power status of the requested server .
*
* @ param \Illuminate\Http\Request $request
* @ param string $uuid
* @ return \Illuminate\Contracts\View\View
*/
public function getStatus ( Request $request , $uuid )
{
$server = Server :: getByUUID ( $uuid );
2016-01-05 06:15:23 +00:00
if ( ! $server ) {
return response () -> json ([], 404 );
}
2015-12-06 18:58:49 +00:00
$client = Node :: guzzleRequest ( $server -> node );
try {
$res = $client -> request ( 'GET' , '/server' , [
'headers' => Server :: getGuzzleHeaders ( $uuid )
]);
if ( $res -> getStatusCode () === 200 ) {
2016-01-05 06:15:23 +00:00
return response () -> json ( json_decode ( $res -> getBody ()));
} else {
return response () -> json ([]);
2015-12-06 18:58:49 +00:00
}
} catch ( RequestException $e ) {
2016-01-05 06:15:23 +00:00
Log :: notice ( 'An exception was raised while attempting to contact a daemon instance to get server status information.' , [
2015-12-06 18:58:49 +00:00
'exception' => $e -> getMessage (),
'path' => $request -> path ()
]);
}
2016-01-05 06:15:23 +00:00
return response () -> json ([]);
2015-12-06 18:58:49 +00:00
}
/**
* Returns a listing of files in a given directory for a server .
*
* @ param \Illuminate\Http\Request $request
* @ param string $uuid `
* @ return \Illuminate\Contracts\View\View
*/
public function postDirectoryList ( Request $request , $uuid )
{
$server = Server :: getByUUID ( $uuid );
$this -> directory = '/' . trim ( urldecode ( $request -> input ( 'directory' , '/' )), '/' );
$this -> authorize ( 'list-files' , $server );
$prevDir = [
'header' => ( $this -> directory !== '/' ) ? $this -> directory : ''
];
if ( $this -> directory !== '/' ) {
$prevDir [ 'first' ] = true ;
}
// Determine if we should show back links in the file browser.
// This code is strange, and could probably be rewritten much better.
$goBack = explode ( '/' , rtrim ( $this -> directory , '/' ));
if ( isset ( $goBack [ 2 ]) && ! empty ( $goBack [ 2 ])) {
$prevDir [ 'show' ] = true ;
$prevDir [ 'link' ] = '/' . trim ( str_replace ( end ( $goBack ), '' , $this -> directory ), '/' );
$prevDir [ 'link_show' ] = trim ( $prevDir [ 'link' ], '/' );
}
2016-01-01 23:08:15 +00:00
$controller = new Repositories\Daemon\FileRepository ( $uuid );
2015-12-06 18:58:49 +00:00
try {
$directoryContents = $controller -> returnDirectoryListing ( $this -> directory );
} catch ( \Exception $e ) {
Debugbar :: addException ( $e );
$exception = 'An error occured while attempting to load the requested directory, please try again.' ;
if ( $e instanceof DisplayException ) {
$exception = $e -> getMessage ();
}
return response ( $exception , 500 );
}
return view ( 'server.files.list' , [
'server' => $server ,
'files' => $directoryContents -> files ,
'folders' => $directoryContents -> folders ,
2016-01-01 23:08:15 +00:00
'extensions' => Repositories\HelperRepository :: editableFiles (),
2015-12-06 18:58:49 +00:00
'directory' => $prevDir
]);
}
/**
* Handles a POST request to save a file .
*
* @ param Request $request
* @ param string $uuid
* @ return \Illuminate\Http\Response
*/
public function postSaveFile ( Request $request , $uuid )
{
$server = Server :: getByUUID ( $uuid );
$this -> authorize ( 'save-files' , $server );
2016-01-01 23:08:15 +00:00
$controller = new Repositories\Daemon\FileRepository ( $uuid );
2015-12-06 18:58:49 +00:00
try {
$controller -> saveFileContents ( $request -> input ( 'file' ), $request -> input ( 'contents' ));
return response ( null , 204 );
} catch ( \Exception $e ) {
Debugbar :: addException ( $e );
$exception = 'An error occured while attempting to save that file, please try again.' ;
if ( $e instanceof DisplayException ) {
$exception = $e -> getMessage ();
}
return response ( $exception , 500 );
}
}
2016-01-03 20:15:14 +00:00
/**
* [ postSetConnection description ]
* @ param Request $request
* @ param string $uuid
* @ return \Illuminate\Http\Response
*/
public function postSetConnection ( Request $request , $uuid )
{
$server = Server :: getByUUID ( $uuid );
$this -> authorize ( 'set-connection' , $server );
2016-01-09 03:36:57 +00:00
if ( $request -> input ( 'connection' ) === $server -> ip . ':' . $server -> port ) {
return response () -> json ([
'error' => 'You are already using this as your default connection.'
], 409 );
}
2016-01-03 20:15:14 +00:00
try {
$repo = new Repositories\ServerRepository ;
$repo -> changeBuild ( $server -> id , [
'default' => $request -> input ( 'connection' ),
]);
return response ( 'The default connection for this server has been updated. Please be aware that you will need to restart your server for this change to go into effect.' );
} catch ( \Exception $e ) {
if ( $e instanceof \Pterodactyl\Exceptions\DisplayException || $e instanceof \Pterodactyl\Exceptions\DisplayValidationException ) {
return response () -> json ([
'error' => $e -> getMessage (),
], 503 );
} else {
Log :: error ( $e );
return response () -> json ([
'error' => 'An unhandled exception occured while attemping to modify the default connection for this server.'
], 503 );
}
}
}
2015-12-06 18:58:49 +00:00
}