2022-03-05 16:02:12 +01:00
< ? php
namespace App\Http\Controllers ;
2022-03-05 16:58:25 +01:00
use App\Models\IPs ;
2022-03-05 16:02:12 +01:00
use App\Models\Labels ;
use App\Models\Pricing ;
use App\Models\Server ;
2022-05-14 17:21:51 +02:00
use App\Models\Settings ;
use App\Models\Yabs ;
2022-03-05 16:02:12 +01:00
use Illuminate\Http\Request ;
2022-03-05 16:58:25 +01:00
use Illuminate\Support\Facades\Cache ;
2022-03-05 16:02:12 +01:00
use Illuminate\Support\Facades\Session ;
use Illuminate\Support\Str ;
use Illuminate\Support\Facades\DB ;
class ServerController extends Controller
{
public function index ()
{
2022-07-19 05:12:51 +02:00
$servers = Server :: allActiveServers ();
2022-05-14 17:21:51 +02:00
2022-07-19 05:12:51 +02:00
$non_active_servers = Server :: allNonActiveServers ();
2022-03-05 16:58:25 +01:00
return view ( 'servers.index' , compact ([ 'servers' , 'non_active_servers' ]));
2022-03-05 16:02:12 +01:00
}
public function showServersPublic ()
{
2022-05-14 17:21:51 +02:00
$settings = Settings :: getSettings ();
2022-03-05 16:02:12 +01:00
2022-03-09 13:16:15 +01:00
Session :: put ( 'timer_version_footer' , $settings [ 0 ] -> show_versions_footer ? ? 1 );
Session :: put ( 'show_servers_public' , $settings [ 0 ] -> show_servers_public ? ? 0 );
Session :: put ( 'show_server_value_ip' , $settings [ 0 ] -> show_server_value_ip ? ? 0 );
Session :: put ( 'show_server_value_hostname' , $settings [ 0 ] -> show_server_value_hostname ? ? 0 );
Session :: put ( 'show_server_value_price' , $settings [ 0 ] -> show_server_value_price ? ? 0 );
Session :: put ( 'show_server_value_yabs' , $settings [ 0 ] -> show_server_value_yabs ? ? 0 );
Session :: put ( 'show_server_value_provider' , $settings [ 0 ] -> show_server_value_provider ? ? 0 );
Session :: put ( 'show_server_value_location' , $settings [ 0 ] -> show_server_value_location ? ? 0 );
2022-03-05 16:02:12 +01:00
Session :: save ();
2022-03-05 16:58:25 +01:00
if (( Session :: get ( 'show_servers_public' ) === 1 )) {
2022-07-19 05:12:51 +02:00
$servers = Server :: allPublicServers ();
2022-03-05 16:02:12 +01:00
return view ( 'servers.public-index' , compact ( 'servers' ));
}
return response () -> view ( 'errors.404' , array ( " status " => 404 , " title " => " Page not found " , " message " => " " ), 404 );
}
public function create ()
{
2022-05-09 08:03:17 +02:00
return view ( 'servers.create' );
2022-03-05 16:02:12 +01:00
}
public function store ( Request $request )
{
$request -> validate ([
'hostname' => 'required|min:5' ,
2022-03-05 16:58:25 +01:00
'ip1' => 'nullable|ip' ,
'ip2' => 'nullable|ip' ,
2022-03-05 16:02:12 +01:00
'service_type' => 'numeric' ,
'server_type' => 'numeric' ,
'ram' => 'numeric' ,
'disk' => 'numeric' ,
'os_id' => 'numeric' ,
'provider_id' => 'numeric' ,
'location_id' => 'numeric' ,
'price' => 'numeric' ,
'cpu' => 'numeric' ,
'was_promo' => 'numeric' ,
'next_due_date' => 'required|date'
]);
$server_id = Str :: random ( 8 );
2022-05-09 06:35:07 +02:00
$pricing = new Pricing ();
2022-08-30 15:37:05 +02:00
$pricing -> insertPricing ( 1 , $server_id , $request -> currency , $request -> price , $request -> payment_term , $request -> next_due_date );
2022-05-09 06:35:07 +02:00
if ( ! is_null ( $request -> ip1 )) {
IPs :: insertIP ( $server_id , $request -> ip1 );
}
if ( ! is_null ( $request -> ip2 )) {
IPs :: insertIP ( $server_id , $request -> ip2 );
}
2022-03-05 16:02:12 +01:00
Server :: create ([
'id' => $server_id ,
'hostname' => $request -> hostname ,
'server_type' => $request -> server_type ,
'os_id' => $request -> os_id ,
'ssh' => $request -> ssh_port ,
'provider_id' => $request -> provider_id ,
'location_id' => $request -> location_id ,
'ram' => $request -> ram ,
'ram_type' => $request -> ram_type ,
2022-03-13 12:13:45 +01:00
'ram_as_mb' => ( $request -> ram_type === 'MB' ) ? $request -> ram : ( $request -> ram * 1024 ),
2022-03-05 16:02:12 +01:00
'disk' => $request -> disk ,
'disk_type' => $request -> disk_type ,
2022-03-13 12:13:45 +01:00
'disk_as_gb' => ( $request -> disk_type === 'GB' ) ? $request -> disk : ( $request -> disk * 1024 ),
2022-03-05 16:02:12 +01:00
'owned_since' => $request -> owned_since ,
'ns1' => $request -> ns1 ,
'ns2' => $request -> ns2 ,
'bandwidth' => $request -> bandwidth ,
'cpu' => $request -> cpu ,
2022-03-05 16:58:25 +01:00
'was_promo' => $request -> was_promo ,
'show_public' => ( isset ( $request -> show_public )) ? 1 : 0
2022-03-05 16:02:12 +01:00
]);
2022-05-09 06:35:07 +02:00
Labels :: insertLabelsAssigned ([ $request -> label1 , $request -> label2 , $request -> label3 , $request -> label4 ], $server_id );
2022-03-05 16:02:12 +01:00
2022-05-14 17:21:51 +02:00
Server :: serverRelatedCacheForget ();
2022-03-05 16:58:25 +01:00
2022-03-05 16:02:12 +01:00
return redirect () -> route ( 'servers.index' )
-> with ( 'success' , 'Server Created Successfully.' );
}
public function show ( Server $server )
{
2022-09-22 05:20:23 +02:00
$server_data = Server :: server ( $server -> id );
2022-05-14 17:21:51 +02:00
2022-07-19 05:12:51 +02:00
return view ( 'servers.show' , compact ([ 'server_data' ]));
2022-03-05 16:02:12 +01:00
}
public function edit ( Server $server )
{
2022-09-22 05:20:23 +02:00
$server_data = Server :: server ( $server -> id );
2022-03-05 16:02:12 +01:00
2022-07-19 05:12:51 +02:00
return view ( 'servers.edit' , compact ([ 'server_data' ]));
2022-03-05 16:02:12 +01:00
}
public function update ( Request $request , Server $server )
{
$request -> validate ([
'hostname' => 'required|min:5' ,
'ram' => 'numeric' ,
'disk' => 'numeric' ,
'os_id' => 'numeric' ,
'provider_id' => 'numeric' ,
'location_id' => 'numeric' ,
'price' => 'numeric' ,
'cpu' => 'numeric' ,
2022-03-05 16:58:25 +01:00
'was_promo' => 'numeric' ,
'next_due_date' => 'date'
2022-03-05 16:02:12 +01:00
]);
2022-05-09 06:35:07 +02:00
$server_id = $request -> server_id ;
2022-03-05 16:02:12 +01:00
DB :: table ( 'servers' )
2022-05-09 06:35:07 +02:00
-> where ( 'id' , $server_id )
2022-03-05 16:02:12 +01:00
-> update ([
'hostname' => $request -> hostname ,
'server_type' => $request -> server_type ,
'os_id' => $request -> os_id ,
2022-09-26 18:05:57 +02:00
'ssh' => $request -> ssh_port ,
2022-03-05 16:02:12 +01:00
'provider_id' => $request -> provider_id ,
'location_id' => $request -> location_id ,
'ram' => $request -> ram ,
'ram_type' => $request -> ram_type ,
2022-03-13 12:13:45 +01:00
'ram_as_mb' => ( $request -> ram_type === 'MB' ) ? $request -> ram : ( $request -> ram * 1024 ),
2022-03-05 16:02:12 +01:00
'disk' => $request -> disk ,
'disk_type' => $request -> disk_type ,
2022-03-13 12:13:45 +01:00
'disk_as_gb' => ( $request -> disk_type === 'GB' ) ? $request -> disk : ( $request -> disk * 1024 ),
2022-03-05 16:02:12 +01:00
'owned_since' => $request -> owned_since ,
'ns1' => $request -> ns1 ,
'ns2' => $request -> ns2 ,
'bandwidth' => $request -> bandwidth ,
'cpu' => $request -> cpu ,
'was_promo' => $request -> was_promo ,
2022-03-05 16:58:25 +01:00
'active' => ( isset ( $request -> is_active )) ? 1 : 0 ,
'show_public' => ( isset ( $request -> show_public )) ? 1 : 0
2022-03-05 16:02:12 +01:00
]);
$pricing = new Pricing ();
$as_usd = $pricing -> convertToUSD ( $request -> price , $request -> currency );
2022-05-14 16:43:36 +02:00
$pricing -> updatePricing ( $server_id , $request -> currency , $request -> price , $request -> payment_term , $as_usd , $request -> next_due_date );
2022-03-05 16:02:12 +01:00
2022-05-09 06:35:07 +02:00
Labels :: deleteLabelsAssignedTo ( $server_id );
2022-03-05 16:02:12 +01:00
2022-05-09 06:35:07 +02:00
Labels :: insertLabelsAssigned ([ $request -> label1 , $request -> label2 , $request -> label3 , $request -> label4 ], $server_id );
2022-03-05 16:02:12 +01:00
2022-05-09 06:35:07 +02:00
IPs :: deleteIPsAssignedTo ( $server_id );
2022-03-05 16:58:25 +01:00
for ( $i = 1 ; $i <= 8 ; $i ++ ) { //Max of 8 ips
$obj = 'ip' . $i ;
if ( isset ( $request -> $obj ) && ! is_null ( $request -> $obj )) {
2022-05-09 06:35:07 +02:00
IPs :: insertIP ( $server_id , $request -> $obj );
2022-03-05 16:58:25 +01:00
}
}
2022-05-14 17:21:51 +02:00
Server :: serverRelatedCacheForget ();
Server :: serverSpecificCacheForget ( $server_id );
2022-03-05 16:58:25 +01:00
2022-03-05 16:02:12 +01:00
return redirect () -> route ( 'servers.index' )
-> with ( 'success' , 'Server Updated Successfully.' );
}
public function destroy ( Server $server )
{
2022-10-05 02:58:19 +02:00
$item = Server :: find ( $server -> id );
$item -> delete ();
2022-03-05 16:02:12 +01:00
$p = new Pricing ();
$p -> deletePricing ( $server -> id );
Labels :: deleteLabelsAssignedTo ( $server -> id );
2022-03-05 16:58:25 +01:00
IPs :: deleteIPsAssignedTo ( $server -> id );
2022-05-14 17:21:51 +02:00
Server :: serverRelatedCacheForget ();
2022-03-05 16:58:25 +01:00
2022-03-05 16:02:12 +01:00
return redirect () -> route ( 'servers.index' )
-> with ( 'success' , 'Server was deleted Successfully.' );
}
public function chooseCompare ()
2022-07-19 05:12:51 +02:00
{ //NOTICE: Selecting servers is not cached yet
2022-03-05 16:02:12 +01:00
$all_servers = Server :: where ( 'has_yabs' , 1 ) -> get ();
return view ( 'servers.choose-compare' , compact ( 'all_servers' ));
}
public function compareServers ( $server1 , $server2 )
{
2022-07-19 05:12:51 +02:00
$server1_data = Server :: server ( $server1 );
2022-03-05 16:02:12 +01:00
2022-07-19 05:12:51 +02:00
if ( ! isset ( $server1_data [ 0 ] -> yabs [ 0 ])) {
2022-03-05 16:02:12 +01:00
return response () -> view ( 'errors.404' , array ( " status " => 404 , " title " => " Page not found " , " message " => " No server with YABs data was found for id ' $server1 ' " ), 404 );
}
2022-07-19 05:12:51 +02:00
$server2_data = Server :: server ( $server2 );
2022-05-15 04:00:05 +02:00
2022-07-19 05:12:51 +02:00
if ( ! isset ( $server2_data [ 0 ] -> yabs [ 0 ])) {
2022-03-05 16:02:12 +01:00
return response () -> view ( 'errors.404' , array ( " status " => 404 , " title " => " Page not found " , " message " => " No server with YABs data was found for id ' $server2 ' " ), 404 );
}
2022-07-19 05:12:51 +02:00
return view ( 'servers.compare' , compact ( 'server1_data' , 'server2_data' ));
2022-03-05 16:02:12 +01:00
}
}