2015-12-06 18:58:49 +00:00
< ? php
2016-01-20 00:10:39 +00:00
/**
2016-01-20 21:05:16 +00:00
* Pterodactyl - Panel
2017-01-24 22:57:08 +00:00
* Copyright ( c ) 2015 - 2017 Dane Everitt < dane @ daneeveritt . com >.
2016-01-20 00:10:39 +00:00
*
2016-01-20 20:56:40 +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 :
2016-01-20 00:10:39 +00:00
*
2016-01-20 20:56:40 +00:00
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software .
2016-01-20 00:10:39 +00:00
*
2016-01-20 20:56:40 +00:00
* 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-01-20 00:10:39 +00:00
*/
2016-12-07 22:46:38 +00:00
2015-12-06 18:58:49 +00:00
namespace Pterodactyl\Http\Controllers\Server ;
use Log ;
2016-08-16 04:07:10 +00:00
use Pterodactyl\Models ;
2015-12-06 18:58:49 +00:00
use Illuminate\Http\Request ;
2016-12-07 22:46:38 +00:00
use Pterodactyl\Repositories ;
2015-12-06 18:58:49 +00:00
use GuzzleHttp\Exception\RequestException ;
2016-12-07 22:46:38 +00:00
use Pterodactyl\Exceptions\DisplayException ;
use Pterodactyl\Http\Controllers\Controller ;
use Pterodactyl\Exceptions\DisplayValidationException ;
2015-12-06 18:58:49 +00:00
class AjaxController extends Controller
{
/**
* @ var array
*/
protected $files = [];
/**
* @ var array
*/
protected $folders = [];
/**
* @ var string
*/
protected $directory ;
/**
* Returns a listing of files in a given directory for a server .
*
2017-03-19 23:36:50 +00:00
* @ param \Illuminate\Http\Request $request
* @ param string $uuid
* @ return \Illuminate\View\View | \Illuminate\Http\Response
2015-12-06 18:58:49 +00:00
*/
public function postDirectoryList ( Request $request , $uuid )
{
2017-02-03 00:41:38 +00:00
$server = Models\Server :: byUuid ( $uuid );
2015-12-06 18:58:49 +00:00
$this -> authorize ( 'list-files' , $server );
2017-02-03 00:41:38 +00:00
$this -> directory = '/' . trim ( urldecode ( $request -> input ( 'directory' , '/' )), '/' );
2015-12-06 18:58:49 +00:00
$prevDir = [
2016-12-07 22:46:38 +00:00
'header' => ( $this -> directory !== '/' ) ? $this -> directory : '' ,
2015-12-06 18:58:49 +00:00
];
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.
2016-10-04 01:09:20 +00:00
$goBack = explode ( '/' , trim ( $this -> directory , '/' ));
2016-12-07 22:46:38 +00:00
if ( ! empty ( array_filter ( $goBack )) && count ( $goBack ) >= 2 ) {
2015-12-06 18:58:49 +00:00
$prevDir [ 'show' ] = true ;
2016-10-04 01:09:20 +00:00
array_pop ( $goBack );
$prevDir [ 'link' ] = '/' . implode ( '/' , $goBack );
$prevDir [ 'link_show' ] = implode ( '/' , $goBack ) . '/' ;
2015-12-06 18:58:49 +00:00
}
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 );
2016-09-07 20:12:06 +00:00
} catch ( DisplayException $ex ) {
return response ( $ex -> getMessage (), 500 );
} catch ( \Exception $ex ) {
Log :: error ( $ex );
2016-12-07 22:46:38 +00:00
2016-09-07 20:12:06 +00:00
return response ( 'An error occured while attempting to load the requested directory, please try again.' , 500 );
2015-12-06 18:58:49 +00:00
}
return view ( 'server.files.list' , [
'server' => $server ,
'files' => $directoryContents -> files ,
'folders' => $directoryContents -> folders ,
2016-10-02 03:09:55 +00:00
'editableMime' => Repositories\HelperRepository :: editableFiles (),
2016-12-07 22:46:38 +00:00
'directory' => $prevDir ,
2015-12-06 18:58:49 +00:00
]);
}
/**
* Handles a POST request to save a file .
*
2017-03-19 23:36:50 +00:00
* @ param \Illuminate\Http\Request $request
* @ param string $uuid
2015-12-06 18:58:49 +00:00
* @ return \Illuminate\Http\Response
*/
public function postSaveFile ( Request $request , $uuid )
{
2017-02-03 00:41:38 +00:00
$server = Models\Server :: byUuid ( $uuid );
2015-12-06 18:58:49 +00:00
$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' ));
2016-12-07 22:46:38 +00:00
2015-12-06 18:58:49 +00:00
return response ( null , 204 );
2016-09-07 20:12:06 +00:00
} catch ( DisplayException $ex ) {
return response ( $ex -> getMessage (), 500 );
} catch ( \Exception $ex ) {
Log :: error ( $ex );
2016-12-07 22:46:38 +00:00
2016-09-07 20:12:06 +00:00
return response ( 'An error occured while attempting to save this file, please try again.' , 500 );
2015-12-06 18:58:49 +00:00
}
}
2016-01-03 20:15:14 +00:00
/**
2017-03-19 23:36:50 +00:00
* Sets the primary allocation for a server .
*
* @ param \Illuminate\Http\Request $request
* @ param string $uuid
* @ return \Illuminate\Http\JsonResponse
* @ deprecated
2016-01-03 20:15:14 +00:00
*/
2016-09-30 22:21:02 +00:00
public function postSetPrimary ( Request $request , $uuid )
2016-01-03 20:15:14 +00:00
{
2017-02-03 00:41:38 +00:00
$server = Models\Server :: byUuid ( $uuid ) -> load ( 'allocations' );
2016-01-03 20:15:14 +00:00
$this -> authorize ( 'set-connection' , $server );
2017-02-02 23:21:36 +00:00
if (( int ) $request -> input ( 'allocation' ) === $server -> allocation_id ) {
2016-01-09 03:36:57 +00:00
return response () -> json ([
2016-12-07 22:46:38 +00:00
'error' => 'You are already using this as your default connection.' ,
2016-01-09 03:36:57 +00:00
], 409 );
}
2016-01-03 20:15:14 +00:00
try {
2017-02-16 18:56:28 +00:00
$allocation = $server -> allocations -> where ( 'id' , $request -> input ( 'allocation' )) -> where ( 'server_id' , $server -> id ) -> first ();
2016-12-07 22:46:38 +00:00
if ( ! $allocation ) {
2016-09-30 22:21:02 +00:00
return response () -> json ([
2016-12-07 22:46:38 +00:00
'error' => 'No allocation matching your request was found in the system.' ,
2016-09-30 22:21:02 +00:00
], 422 );
}
2016-01-03 20:15:14 +00:00
$repo = new Repositories\ServerRepository ;
$repo -> changeBuild ( $server -> id , [
2016-09-30 22:21:02 +00:00
'default' => $allocation -> ip . ':' . $allocation -> port ,
2016-01-03 20:15:14 +00:00
]);
2016-12-07 22:46:38 +00:00
2016-01-03 20:15:14 +00:00
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.' );
2016-09-07 20:12:06 +00:00
} catch ( DisplayValidationException $ex ) {
2016-01-23 02:43:56 +00:00
return response () -> json ([
'error' => json_decode ( $ex -> getMessage (), true ),
2016-09-30 22:21:02 +00:00
], 422 );
2016-09-07 20:12:06 +00:00
} catch ( DisplayException $ex ) {
2016-01-23 02:43:56 +00:00
return response () -> json ([
'error' => $ex -> getMessage (),
], 503 );
} catch ( \Exception $ex ) {
Log :: error ( $ex );
2016-12-07 22:46:38 +00:00
2016-01-23 02:43:56 +00:00
return response () -> json ([
2016-12-07 22:46:38 +00:00
'error' => 'An unhandled exception occured while attemping to modify the default connection for this server.' ,
2016-01-23 02:43:56 +00:00
], 503 );
2016-01-03 20:15:14 +00:00
}
}
2017-03-19 23:36:50 +00:00
/**
* Resets a database password for a server .
*
* @ param \Illuminate\Http\Request $request
* @ param string $uuid
* @ return \Illuminate\Http\JsonResponse
* @ deprecated
*/
2016-08-16 04:07:10 +00:00
public function postResetDatabasePassword ( Request $request , $uuid )
{
2017-02-03 00:41:38 +00:00
$server = Models\Server :: byUuid ( $uuid );
2016-08-16 04:07:10 +00:00
$this -> authorize ( 'reset-db-password' , $server );
2017-02-03 00:41:38 +00:00
2017-03-05 21:46:44 +00:00
$database = Models\Database :: where ( 'server_id' , $server -> id ) -> findOrFail ( $request -> input ( 'database' ));
$repo = new Repositories\DatabaseRepository ;
2016-08-16 04:07:10 +00:00
try {
2017-03-05 21:46:44 +00:00
$password = str_random ( 20 );
$repo -> password ( $database -> id , $password );
2016-12-07 22:46:38 +00:00
2016-08-16 04:07:10 +00:00
return response ( $password );
2017-03-05 21:46:44 +00:00
} catch ( DisplayException $ex ) {
return response () -> json ([ 'error' => $ex -> getMessage ()], 503 );
2016-12-07 22:46:38 +00:00
} catch ( \Exception $ex ) {
2016-08-16 04:07:10 +00:00
Log :: error ( $ex );
2016-12-07 22:46:38 +00:00
2016-08-16 04:07:10 +00:00
return response () -> json ([
2016-12-07 22:46:38 +00:00
'error' => 'An unhandled error occured while attempting to modify this database\'s password.' ,
2016-08-16 04:07:10 +00:00
], 503 );
}
}
2015-12-06 18:58:49 +00:00
}