2021-06-17 14:08:30 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
2021-08-08 07:27:22 +00:00
|
|
|
use Illuminate\Support\Arr;
|
2021-06-17 14:08:30 +00:00
|
|
|
|
2021-08-13 12:53:59 +00:00
|
|
|
use App\Models\{Address,System,SystemZone,Zone};
|
2021-07-01 14:25:41 +00:00
|
|
|
use App\Rules\{FidoInteger,TwoByteInteger};
|
2021-06-17 14:08:30 +00:00
|
|
|
|
|
|
|
class SystemController extends Controller
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->middleware('auth');
|
|
|
|
}
|
|
|
|
|
2021-06-20 13:03:20 +00:00
|
|
|
/**
|
|
|
|
* Add an address to a system
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @param System $o
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
|
|
|
public function add_address(Request $request,System $o)
|
|
|
|
{
|
2021-07-04 11:47:23 +00:00
|
|
|
// @todo This should be admin of the zone
|
2021-06-20 13:03:20 +00:00
|
|
|
$this->authorize('admin',$o);
|
|
|
|
session()->flash('add_address',TRUE);
|
|
|
|
|
|
|
|
$request->validate([
|
|
|
|
'action' => 'required|in:region,host,node',
|
|
|
|
'zone_id' => 'required|exists:zones,id',
|
|
|
|
]);
|
|
|
|
|
|
|
|
switch ($request->post('action')) {
|
|
|
|
case 'region':
|
|
|
|
$request->validate([
|
|
|
|
'region_id_new' => [
|
|
|
|
'required',
|
|
|
|
new TwoByteInteger,
|
|
|
|
function ($attribute,$value,$fail) {
|
|
|
|
// Check that the region doesnt already exist
|
|
|
|
$o = Address::where(function($query) use ($value) {
|
|
|
|
return $query->where('region_id',$value)
|
2021-06-25 06:42:12 +00:00
|
|
|
->where('host_id',0)
|
2021-06-20 13:03:20 +00:00
|
|
|
->where('node_id',0)
|
|
|
|
->where('point_id',0)
|
|
|
|
->where('role',DomainController::NODE_RC);
|
|
|
|
})
|
|
|
|
// Check that a host doesnt already exist
|
|
|
|
->orWhere(function($query) use ($value) {
|
|
|
|
return $query->where('host_id',$value)
|
|
|
|
->where('point_id',0)
|
|
|
|
->where('role',DomainController::NODE_NC);
|
|
|
|
});
|
|
|
|
|
|
|
|
if ($o->count()) {
|
|
|
|
$fail('Region or host already exists');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$oo = new Address;
|
|
|
|
$oo->zone_id = $request->post('zone_id');
|
|
|
|
$oo->region_id = $request->post('region_id_new');
|
|
|
|
$oo->node_id = 0;
|
|
|
|
$oo->point_id = 0;
|
|
|
|
$oo->role = DomainController::NODE_RC;
|
|
|
|
$oo->active = TRUE;
|
|
|
|
|
|
|
|
$o->addresses()->save($oo);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'host':
|
|
|
|
$request->validate([
|
2021-07-01 14:25:41 +00:00
|
|
|
'region_id' => ['required',new FidoInteger],
|
2021-06-20 13:03:20 +00:00
|
|
|
'host_id_new' => [
|
|
|
|
'required',
|
|
|
|
new TwoByteInteger,
|
2021-07-05 13:17:00 +00:00
|
|
|
function ($attribute,$value,$fail) use ($request) {
|
2021-06-20 13:03:20 +00:00
|
|
|
// Check that the region doesnt already exist
|
|
|
|
$o = Address::where(function($query) use ($value) {
|
2021-07-18 13:38:18 +00:00
|
|
|
return $query->where(function($query) use ($value) {
|
|
|
|
return $query->where('region_id',$value)
|
|
|
|
->where('role',DomainController::NODE_RC);
|
|
|
|
})
|
|
|
|
// Check that a host doesnt already exist
|
|
|
|
->orWhere(function($query) use ($value) {
|
|
|
|
return $query->where('host_id',$value)
|
|
|
|
->where('role',DomainController::NODE_NC);
|
|
|
|
});
|
2021-07-05 13:17:00 +00:00
|
|
|
})
|
|
|
|
->where('zone_id',$request->post('zone_id'))
|
|
|
|
->where('point_id',0)
|
|
|
|
->where('active',TRUE);
|
2021-06-20 13:03:20 +00:00
|
|
|
|
|
|
|
if ($o->count()) {
|
|
|
|
$fail('Region or host already exists');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
],
|
|
|
|
'node_id_new' => [
|
|
|
|
'required',
|
|
|
|
new TwoByteInteger,
|
|
|
|
function ($attribute,$value,$fail) use ($request) {
|
|
|
|
// Check that the region doesnt already exist
|
|
|
|
$o = Address::where(function($query) use ($request,$value) {
|
|
|
|
return $query
|
|
|
|
->where('host_id',$request->post('host_id_new'))
|
|
|
|
->where('node_id',$value)
|
|
|
|
->where('point_id',0)
|
|
|
|
->where('role',DomainController::NODE_RC);
|
|
|
|
});
|
|
|
|
|
|
|
|
if ($o->count()) {
|
|
|
|
$fail('Host already exists');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
2021-07-26 11:21:58 +00:00
|
|
|
// Find the Hub address
|
|
|
|
// Find the zones <HOST>/0 address, and assign it to this host.
|
|
|
|
$oo = Address::where('zone_id',$request->zone_id)
|
|
|
|
->where('region_id',$request->region_id)
|
|
|
|
->where('host_id',$request->host_id_new)
|
|
|
|
->where('node_id',0)
|
|
|
|
->where('point_id',0)
|
|
|
|
->single();
|
|
|
|
|
|
|
|
// Its not defined, so we'll create it.
|
|
|
|
if (! $oo) {
|
|
|
|
$oo = new Address;
|
|
|
|
$oo->forceFill([
|
|
|
|
'zone_id'=>$request->zone_id,
|
|
|
|
'region_id'=>$request->region_id,
|
|
|
|
'host_id'=>$request->host_id_new,
|
|
|
|
'node_id'=>0,
|
|
|
|
'point_id'=>0,
|
|
|
|
'role'=>DomainController::NODE_NC,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$oo->system_id = $request->system_id;
|
|
|
|
$oo->active = TRUE;
|
|
|
|
$o->addresses()->save($oo);
|
|
|
|
|
2021-06-20 13:03:20 +00:00
|
|
|
$oo = new Address;
|
|
|
|
$oo->zone_id = $request->post('zone_id');
|
2021-06-25 13:38:57 +00:00
|
|
|
$oo->region_id = $request->post('region_id');
|
2021-06-20 13:03:20 +00:00
|
|
|
$oo->host_id = $request->post('host_id_new');
|
|
|
|
$oo->node_id = $request->post('node_id_new');
|
|
|
|
$oo->point_id = 0;
|
|
|
|
$oo->role = DomainController::NODE_NC;
|
|
|
|
$oo->active = TRUE;
|
|
|
|
|
|
|
|
$o->addresses()->save($oo);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'node':
|
|
|
|
$request->validate([
|
2021-07-01 14:25:41 +00:00
|
|
|
'region_id' => ['required',new FidoInteger],
|
|
|
|
'host_id' => ['required',new FidoInteger],
|
2021-06-20 13:03:20 +00:00
|
|
|
'node_id' => [
|
|
|
|
'required',
|
|
|
|
new TwoByteInteger,
|
|
|
|
function ($attribute,$value,$fail) use ($request) {
|
|
|
|
// Check that the region doesnt already exist
|
|
|
|
$o = Address::where(function($query) use ($request,$value) {
|
|
|
|
return $query
|
2021-07-26 11:53:56 +00:00
|
|
|
->where('zone_id',$request->post('zone_id'))
|
2021-06-25 06:42:12 +00:00
|
|
|
->where('host_id',$request->post('host_id'))
|
2021-06-20 13:03:20 +00:00
|
|
|
->where('node_id',$value)
|
2021-07-17 07:15:00 +00:00
|
|
|
->where('point_id',0);
|
2021-06-20 13:03:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if ($o->count()) {
|
2021-07-26 11:53:56 +00:00
|
|
|
$fail(sprintf('Host already exists: %s',$o->get()->pluck('ftn')->join(',')));
|
2021-06-20 13:03:20 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
],
|
|
|
|
'point_id' => ['required',function($attribute,$value,$fail) {
|
|
|
|
if (! is_numeric($value) || $value > DomainController::NUMBER_MAX)
|
|
|
|
$fail(sprintf('Point numbers must be between 0 and %d',DomainController::NUMBER_MAX));
|
|
|
|
}],
|
|
|
|
'hub' => 'required|boolean',
|
2021-06-25 06:42:12 +00:00
|
|
|
'hub_id' => 'nullable|exists:addresses,id',
|
2021-06-20 13:03:20 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$oo = new Address;
|
|
|
|
$oo->zone_id = $request->post('zone_id');
|
2021-06-25 13:38:57 +00:00
|
|
|
$oo->region_id = $request->post('region_id');
|
2021-06-20 13:03:20 +00:00
|
|
|
$oo->host_id = $request->post('host_id');
|
|
|
|
$oo->node_id = $request->post('node_id');
|
|
|
|
$oo->point_id = $request->post('point_id');
|
2021-06-25 13:38:57 +00:00
|
|
|
$oo->hub_id = $request->post('hub_id') > 0 ? $request->post('hub_id') : NULL;
|
2021-06-20 13:03:20 +00:00
|
|
|
$oo->role = (! $oo->point_id) && $request->post('hub') ? DomainController::NODE_HC : NULL;
|
|
|
|
$oo->active = TRUE;
|
|
|
|
|
|
|
|
$o->addresses()->save($oo);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return redirect()->back()->withErrors(['action'=>'Unknown action: '.$request->post('action')]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->to(sprintf('ftn/system/addedit/%d',$o->id));
|
|
|
|
}
|
|
|
|
|
2021-07-04 11:47:23 +00:00
|
|
|
/**
|
|
|
|
* Add Session details
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @param System $o
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
|
|
|
public function add_session(Request $request,System $o)
|
|
|
|
{
|
|
|
|
// @todo This should be admin of the zone
|
|
|
|
$this->authorize('admin',$o);
|
|
|
|
session()->flash('add_session',TRUE);
|
|
|
|
|
|
|
|
$validate = $request->validate([
|
|
|
|
'zone_id' => 'required|exists:zones,id',
|
|
|
|
'sespass' => 'required|string|min:4',
|
2021-07-26 11:21:58 +00:00
|
|
|
'pktpass' => 'nullable|string|min:4|max:8',
|
|
|
|
'ticpass' => 'nullable|string|min:4',
|
2021-07-04 11:47:23 +00:00
|
|
|
'fixpass' => 'required|string|min:4',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$zo = Zone::findOrFail($validate['zone_id']);
|
|
|
|
|
2021-08-13 12:53:59 +00:00
|
|
|
// If this session is for the ZC, it now becomes the default.
|
|
|
|
if (in_array(DomainController::NODE_ZC,$o->match($zo)->pluck('role')->toArray())) {
|
|
|
|
SystemZone::where('default',TRUE)->update(['default'=>FALSE]);
|
|
|
|
$validate['default'] = TRUE;
|
|
|
|
}
|
|
|
|
|
2021-07-04 11:47:23 +00:00
|
|
|
$o->sessions()->attach($zo,$validate);
|
|
|
|
|
|
|
|
return redirect()->to(sprintf('ftn/system/addedit/%d',$o->id));
|
|
|
|
}
|
|
|
|
|
2021-06-17 14:08:30 +00:00
|
|
|
/**
|
|
|
|
* Add or edit a node
|
|
|
|
*/
|
|
|
|
public function add_edit(Request $request,System $o)
|
|
|
|
{
|
|
|
|
if ($request->post()) {
|
2021-06-18 13:01:41 +00:00
|
|
|
$this->authorize('admin',$o);
|
|
|
|
|
2021-06-17 14:08:30 +00:00
|
|
|
$request->validate([
|
2021-06-26 00:34:49 +00:00
|
|
|
'name' => 'required|min:3',
|
2021-06-17 14:08:30 +00:00
|
|
|
'location' => 'required|min:3',
|
|
|
|
'sysop' => 'required|min:3',
|
2021-08-21 11:15:22 +00:00
|
|
|
'phone' => 'nullable|regex:/^([0-9-]+)$/',
|
2021-06-17 14:08:30 +00:00
|
|
|
'address' => 'nullable|regex:/^(?!:\/\/)(?=.{1,255}$)((.{1,63}\.){1,127}(?![0-9]*$)[a-z0-9-]+\.?)$/i',
|
|
|
|
'port' => 'nullable|digits_between:2,5',
|
2021-06-24 10:16:37 +00:00
|
|
|
'method' => 'nullable|numeric',
|
|
|
|
'mailer_type' => 'nullable|numeric',
|
|
|
|
'mailer_address' => 'nullable|regex:/^(?!:\/\/)(?=.{1,255}$)((.{1,63}\.){1,127}(?![0-9]*$)[a-z0-9-]+\.?)$/i',
|
|
|
|
'mailer_port' => 'nullable|digits_between:2,5',
|
2021-06-18 13:01:41 +00:00
|
|
|
'active' => 'required|boolean',
|
2021-06-26 15:15:19 +00:00
|
|
|
'zt_id' => 'nullable|size:10|regex:/^([A-Fa-f0-9]){10}$/|unique:systems,zt_id,'.($o->exists ? $o->id : 0),
|
2021-06-17 14:08:30 +00:00
|
|
|
]);
|
|
|
|
|
2021-08-21 11:15:22 +00:00
|
|
|
foreach (['name','location','sysop','phone','address','port','active','method','notes','mailer_type','mailer_address','mailer_port','zt_id'] as $key)
|
2021-06-17 14:08:30 +00:00
|
|
|
$o->{$key} = $request->post($key);
|
|
|
|
|
|
|
|
$o->save();
|
|
|
|
|
|
|
|
return redirect()->action([self::class,'home']);
|
|
|
|
}
|
|
|
|
|
2021-07-26 11:21:58 +00:00
|
|
|
$o->load(['addresses.zone.domain']);
|
|
|
|
|
2021-06-17 14:08:30 +00:00
|
|
|
return view('system.addedit')
|
|
|
|
->with('o',$o);
|
|
|
|
}
|
|
|
|
|
2021-07-01 14:25:41 +00:00
|
|
|
/**
|
|
|
|
* Delete address assigned to a host
|
|
|
|
*
|
|
|
|
* @param Address $o
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
|
|
|
public function del_address(Address $o)
|
|
|
|
{
|
2021-07-04 11:47:23 +00:00
|
|
|
// @todo This should be admin of the zone
|
2021-07-01 14:25:41 +00:00
|
|
|
$this->authorize('admin',$o);
|
|
|
|
session()->flash('add_address',TRUE);
|
|
|
|
|
|
|
|
$sid = $o->system_id;
|
2021-07-02 13:19:50 +00:00
|
|
|
$o->active = FALSE;
|
|
|
|
$o->save();
|
2021-07-01 14:25:41 +00:00
|
|
|
$o->delete();
|
|
|
|
|
|
|
|
return redirect()->to(sprintf('ftn/system/addedit/%d',$sid));
|
|
|
|
}
|
|
|
|
|
2021-07-04 11:47:23 +00:00
|
|
|
/**
|
|
|
|
* Delete address assigned to a host
|
|
|
|
*
|
|
|
|
* @param Address $o
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
|
|
|
public function del_session(System $o,Zone $zo)
|
|
|
|
{
|
|
|
|
$this->authorize('admin',$zo);
|
|
|
|
session()->flash('add_session',TRUE);
|
|
|
|
|
|
|
|
$o->sessions()->detach($zo);
|
|
|
|
|
|
|
|
return redirect()->to(sprintf('ftn/system/addedit/%d',$o->id));
|
|
|
|
}
|
|
|
|
|
2021-08-08 07:27:22 +00:00
|
|
|
/**
|
|
|
|
* Move address to another system
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @param System $so
|
|
|
|
* @param Address $o
|
2021-08-13 12:53:59 +00:00
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\RedirectResponse
|
2021-08-08 07:27:22 +00:00
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
|
|
|
public function mov_address(Request $request,System $so,Address $o)
|
|
|
|
{
|
|
|
|
// Quick check that this address belongs to this system
|
|
|
|
if ($so->addresses->search(function($item) use ($o) { return $item->id == $o->id; }) === FALSE)
|
|
|
|
abort(404);
|
|
|
|
|
|
|
|
if ($request->post()) {
|
|
|
|
$this->authorize('admin',$o);
|
|
|
|
|
|
|
|
$validated = $request->validate([
|
|
|
|
'system_id' => 'required|exists:systems,id',
|
|
|
|
'remove' => 'nullable|boolean',
|
|
|
|
'remsess' => 'nullable|boolean|exclude_if:remove,1',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$o->system_id = $validated['system_id'];
|
|
|
|
$o->save();
|
|
|
|
|
|
|
|
if (Arr::get($validated,'remove')) {
|
|
|
|
$so->sessions()->detach($o->zone);
|
|
|
|
$so->delete();
|
|
|
|
|
|
|
|
} elseif (Arr::get($validated,'remsess')) {
|
|
|
|
$so->sessions()->detach($o->zone);
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->to('ftn/system/addedit/'.$validated['system_id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return view('system.moveaddr')
|
|
|
|
->with('o',$o);
|
|
|
|
}
|
|
|
|
|
2021-07-04 11:47:23 +00:00
|
|
|
public function ours()
|
|
|
|
{
|
|
|
|
return view('system.ours');
|
|
|
|
}
|
|
|
|
|
2021-07-01 14:25:41 +00:00
|
|
|
/**
|
|
|
|
* Suspend address assigned to a host
|
|
|
|
*
|
|
|
|
* @param Address $o
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
|
|
|
public function sus_address(Address $o)
|
|
|
|
{
|
2021-07-04 11:47:23 +00:00
|
|
|
// @todo This should be admin of the zone
|
2021-07-01 14:25:41 +00:00
|
|
|
$this->authorize('admin',$o);
|
|
|
|
session()->flash('add_address',TRUE);
|
|
|
|
|
|
|
|
$o->active = (! $o->active);
|
|
|
|
$o->save();
|
|
|
|
|
|
|
|
return redirect()->to(sprintf('ftn/system/addedit/%d',$o->system_id));
|
|
|
|
}
|
|
|
|
|
2021-06-17 14:08:30 +00:00
|
|
|
public function home()
|
|
|
|
{
|
|
|
|
return view('system.home');
|
|
|
|
}
|
|
|
|
}
|