2021-05-13 12:40:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2021-06-20 13:03:20 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2021-05-13 12:40:21 +00:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
2021-06-20 13:03:20 +00:00
|
|
|
use App\Models\{Address,Domain,Zone};
|
2021-05-13 12:40:21 +00:00
|
|
|
|
|
|
|
class DomainController extends Controller
|
|
|
|
{
|
2021-06-26 00:34:49 +00:00
|
|
|
public const NODE_ACTIVE = 0; // Active
|
|
|
|
public const NODE_ZC = 1<<0; // Zone
|
|
|
|
public const NODE_RC = 1<<1; // Region
|
|
|
|
public const NODE_NC = 1<<2; // Host
|
|
|
|
public const NODE_HC = 1<<3; // Hub
|
2021-07-26 11:21:58 +00:00
|
|
|
public const NODE_POINT = 1<<4; // Point
|
|
|
|
public const NODE_PVT = 1<<5; // Pvt
|
|
|
|
public const NODE_HOLD = 1<<6; // Hold
|
|
|
|
public const NODE_DOWN = 1<<7; // Down
|
2021-06-20 13:03:20 +00:00
|
|
|
|
|
|
|
// http://ftsc.org/docs/frl-1002.001
|
|
|
|
public const NUMBER_MAX = 0x7fff;
|
|
|
|
|
2021-05-13 12:40:21 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->middleware('auth');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-26 11:21:58 +00:00
|
|
|
* Add or edit a domain
|
2021-05-13 12:40:21 +00:00
|
|
|
*/
|
|
|
|
public function add_edit(Request $request,Domain $o)
|
|
|
|
{
|
|
|
|
if ($request->post()) {
|
2021-06-18 13:01:41 +00:00
|
|
|
$this->authorize('admin',$o);
|
|
|
|
|
2021-06-14 05:46:18 +00:00
|
|
|
$request->validate([
|
2021-07-26 11:21:58 +00:00
|
|
|
// http://ftsc.org/docs/old/fsp-1028.002
|
|
|
|
'name' => 'required|max:8|regex:/^[a-z-_~]{1,8}$/|unique:domains,name,'.($o->exists ? $o->id : 0),
|
2021-06-17 14:08:30 +00:00
|
|
|
'dnsdomain' => 'nullable|regex:/^(?!:\/\/)(?=.{1,255}$)((.{1,63}\.){1,127}(?![0-9]*$)[a-z0-9-]+\.?)$/i|unique:domains,dnsdomain,'.($o->exists ? $o->id : NULL),
|
2021-06-18 13:01:41 +00:00
|
|
|
'active' => 'required|boolean',
|
|
|
|
'public' => 'required|boolean',
|
2021-06-14 05:46:18 +00:00
|
|
|
]);
|
|
|
|
|
2021-06-14 11:33:18 +00:00
|
|
|
foreach (['name','dnsdomain','active','public','homepage','notes'] as $key)
|
2021-05-13 12:40:21 +00:00
|
|
|
$o->{$key} = $request->post($key);
|
|
|
|
|
|
|
|
$o->save();
|
|
|
|
|
|
|
|
return redirect()->action([self::class,'home']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return view('domain.addedit')
|
|
|
|
->with('o',$o);
|
|
|
|
}
|
|
|
|
|
2021-06-20 13:03:20 +00:00
|
|
|
/**
|
|
|
|
* Get all the hosts for a zone of a particular region (or not)
|
|
|
|
*
|
|
|
|
* @param Zone $o
|
|
|
|
* @param int $region
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function api_hosts(Zone $o,int $region): Collection
|
|
|
|
{
|
|
|
|
$oo = Address::where('role',self::NODE_NC)
|
|
|
|
->where('zone_id',$o->id)
|
2021-08-15 06:41:04 +00:00
|
|
|
->when($region,function($query,$region) { return $query->where('region_id',$region)->where('node_id',0); })
|
2021-07-02 13:19:50 +00:00
|
|
|
->when((! $region),function($query) use ($region) { return $query->where('region_id',0); })
|
2021-06-20 13:03:20 +00:00
|
|
|
->where('point_id',0)
|
2021-07-05 13:17:00 +00:00
|
|
|
->FTNorder()
|
2021-06-20 13:03:20 +00:00
|
|
|
->with(['system'])
|
|
|
|
->get();
|
|
|
|
|
|
|
|
return $oo->map(function($item) {
|
2021-06-26 01:48:55 +00:00
|
|
|
return ['id'=>$item->host_id,'value'=>sprintf('%s %s',$item->ftn_3d,$item->system->name)];
|
2021-06-20 13:03:20 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find all the hubs for a host
|
|
|
|
*
|
|
|
|
* @param Zone $o
|
|
|
|
* @param int $host
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function api_hubs(Zone $o,int $host): Collection
|
|
|
|
{
|
|
|
|
$oo = Address::where('role',self::NODE_HC)
|
|
|
|
->where('zone_id',$o->id)
|
|
|
|
->when($host,function($query,$host) { return $query->where('host_id',$host)->where('node_id','<>',0); })
|
|
|
|
->with(['system'])
|
|
|
|
->get();
|
|
|
|
|
|
|
|
return $oo->map(function($item) {
|
2021-06-26 01:48:55 +00:00
|
|
|
return ['id'=>$item->id,'value'=>sprintf('%s %s',$item->ftn_3d,$item->system->name)];
|
2021-06-20 13:03:20 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all the regions for a zone
|
|
|
|
*
|
|
|
|
* @param Zone $o
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function api_regions(Zone $o): Collection
|
|
|
|
{
|
|
|
|
$oo = Address::where('role',self::NODE_RC)
|
|
|
|
->where('zone_id',$o->id)
|
|
|
|
->where('node_id',0)
|
|
|
|
->where('point_id',0)
|
|
|
|
->orderBy('region_id')
|
|
|
|
->with(['system'])
|
|
|
|
->get();
|
|
|
|
|
|
|
|
return $oo->map(function($item) {
|
2021-06-26 01:48:55 +00:00
|
|
|
return ['id'=>$item->region_id,'value'=>sprintf('%s %s',$item->ftn_3d,$item->system->location)];
|
2021-06-20 13:03:20 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-13 12:40:21 +00:00
|
|
|
public function home()
|
|
|
|
{
|
|
|
|
return view('domain.home');
|
|
|
|
}
|
|
|
|
}
|