2021-06-20 13:03:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2021-07-18 12:10:21 +00:00
|
|
|
use Carbon\Carbon;
|
2021-06-24 10:16:37 +00:00
|
|
|
use Exception;
|
2021-06-20 13:03:20 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2021-06-25 06:42:12 +00:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2021-06-20 13:03:20 +00:00
|
|
|
|
2021-07-15 14:54:23 +00:00
|
|
|
use App\Classes\FTN\Packet;
|
2021-06-20 13:03:20 +00:00
|
|
|
use App\Http\Controllers\DomainController;
|
2021-06-24 10:16:37 +00:00
|
|
|
use App\Traits\ScopeActive;
|
2021-06-20 13:03:20 +00:00
|
|
|
|
|
|
|
class Address extends Model
|
|
|
|
{
|
2021-06-25 06:42:12 +00:00
|
|
|
use ScopeActive,SoftDeletes;
|
2021-06-24 10:16:37 +00:00
|
|
|
|
2021-06-24 13:09:09 +00:00
|
|
|
/* SCOPES */
|
|
|
|
|
|
|
|
public function scopeFTNOrder($query)
|
|
|
|
{
|
|
|
|
return $query
|
|
|
|
->orderBy('region_id')
|
2021-06-25 06:42:12 +00:00
|
|
|
->orderBy('host_id')
|
2021-06-24 13:09:09 +00:00
|
|
|
->orderBy('node_id')
|
|
|
|
->orderBy('point_id');
|
|
|
|
}
|
|
|
|
|
2021-06-20 13:03:20 +00:00
|
|
|
/* RELATIONS */
|
|
|
|
|
2021-07-04 11:47:23 +00:00
|
|
|
/**
|
|
|
|
* Find children dependant on this record
|
|
|
|
*/
|
2021-07-02 13:19:50 +00:00
|
|
|
public function children()
|
|
|
|
{
|
2021-07-15 14:54:23 +00:00
|
|
|
switch (strtolower($this->role)) {
|
|
|
|
case 'region':
|
|
|
|
return $this->hasMany(self::class,'region_id','region_id')
|
|
|
|
->where('zone_id',$this->zone_id)
|
|
|
|
->where(function($q) {
|
|
|
|
return $q->where('host_id',0)
|
|
|
|
->orWhere('role',DomainController::NODE_NC);
|
|
|
|
})
|
|
|
|
->where('id','<>',$this->id);
|
|
|
|
|
|
|
|
case 'host':
|
|
|
|
return $this->hasMany(self::class,'host_id','host_id')
|
|
|
|
->where('zone_id',$this->zone_id)
|
|
|
|
->where('region_id',$this->region_id)
|
|
|
|
->whereNull('hub_id')
|
|
|
|
->where('id','<>',$this->id);
|
|
|
|
|
|
|
|
case 'hub':
|
|
|
|
return $this->hasMany(self::class,'hub_id','id');
|
|
|
|
|
|
|
|
case 'node':
|
2021-07-17 05:48:07 +00:00
|
|
|
// Nodes dont have children, but must return a relationship instance
|
|
|
|
return $this->hasOne(self::class,NULL,'void');
|
2021-07-15 14:54:23 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Exception('Unknown role: '.$this->role);
|
|
|
|
}
|
2021-07-02 13:19:50 +00:00
|
|
|
}
|
|
|
|
|
2021-06-20 13:03:20 +00:00
|
|
|
public function system()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(System::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function zone()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Zone::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ATTRIBUTES */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render the node name in full 5D
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2021-07-15 14:54:23 +00:00
|
|
|
public function getFTNAttribute(): string
|
2021-06-20 13:03:20 +00:00
|
|
|
{
|
2021-06-26 01:48:55 +00:00
|
|
|
return sprintf('%s@%s',$this->getFTN4DAttribute(),$this->zone->domain->name);
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:54:23 +00:00
|
|
|
public function getFTN3DAttribute(): string
|
2021-06-26 01:48:55 +00:00
|
|
|
{
|
|
|
|
return sprintf('%d:%d/%d',$this->zone->zone_id,$this->host_id ?: $this->region_id,$this->node_id);
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:54:23 +00:00
|
|
|
public function getFTN4DAttribute(): string
|
2021-06-26 01:48:55 +00:00
|
|
|
{
|
|
|
|
return sprintf('%s.%d',$this->getFTN3DAttribute(),$this->point_id);
|
2021-06-20 13:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getRoleAttribute($value)
|
|
|
|
{
|
|
|
|
switch ($value) {
|
|
|
|
case DomainController::NODE_ZC;
|
|
|
|
return 'Zone';
|
|
|
|
case DomainController::NODE_RC;
|
|
|
|
return 'Region';
|
|
|
|
case DomainController::NODE_NC;
|
|
|
|
return 'Host';
|
|
|
|
case DomainController::NODE_HC;
|
|
|
|
return 'Hub';
|
2021-06-25 11:31:57 +00:00
|
|
|
case DomainController::NODE_PVT;
|
|
|
|
return 'PVT';
|
|
|
|
case DomainController::NODE_DOWN;
|
|
|
|
return 'DOWN';
|
2021-06-20 13:03:20 +00:00
|
|
|
case NULL:
|
|
|
|
return 'Node';
|
|
|
|
default:
|
|
|
|
return '?';
|
|
|
|
}
|
|
|
|
}
|
2021-06-24 10:16:37 +00:00
|
|
|
|
2021-07-15 14:54:23 +00:00
|
|
|
/* METHODS */
|
2021-06-24 10:16:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a record in the DB for a node string, eg: 10:1/1.0
|
|
|
|
*
|
|
|
|
* @param string $ftn
|
2021-06-29 10:43:29 +00:00
|
|
|
* @return Address|null
|
2021-06-24 10:16:37 +00:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public static function findFTN(string $ftn): ?self
|
|
|
|
{
|
2021-06-29 10:43:29 +00:00
|
|
|
$ftn = self::parseFTN($ftn);
|
2021-06-24 10:16:37 +00:00
|
|
|
|
|
|
|
$o = (new self)->active()
|
|
|
|
->select('addresses.*')
|
2021-06-29 10:43:29 +00:00
|
|
|
->where('zones.zone_id',$ftn['z'])
|
2021-07-01 15:09:16 +00:00
|
|
|
->where('host_id',$ftn['n'])
|
2021-06-24 10:16:37 +00:00
|
|
|
->join('zones',['zones.id'=>'addresses.zone_id'])
|
|
|
|
->join('domains',['domains.id'=>'zones.domain_id'])
|
|
|
|
->where('zones.active',TRUE)
|
|
|
|
->where('domains.active',TRUE)
|
|
|
|
->where('addresses.active',TRUE)
|
2021-06-29 10:43:29 +00:00
|
|
|
->where('node_id',$ftn['f'])
|
|
|
|
->where('point_id',$ftn['p'])
|
|
|
|
->when($ftn['d'],function($query,$domain) {
|
|
|
|
$query->where('domains.name',$domain);
|
2021-06-24 10:16:37 +00:00
|
|
|
})
|
2021-06-29 10:43:29 +00:00
|
|
|
->when((! $ftn['d']),function($query) {
|
2021-06-24 10:16:37 +00:00
|
|
|
$query->where('domains.default',TRUE);
|
|
|
|
})
|
|
|
|
->single();
|
|
|
|
|
|
|
|
return ($o && $o->system->active) ? $o : NULL;
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:54:23 +00:00
|
|
|
/**
|
|
|
|
* Get netmail for this node (including it's children)
|
2021-07-17 05:48:07 +00:00
|
|
|
*
|
|
|
|
* @return Packet|null
|
2021-07-15 14:54:23 +00:00
|
|
|
*/
|
2021-07-17 05:48:07 +00:00
|
|
|
public function getNetmail(): ?Packet
|
2021-07-15 14:54:23 +00:00
|
|
|
{
|
2021-07-18 12:10:21 +00:00
|
|
|
if (($x=Netmail::whereIn('tftn_id',$this->children->pluck('id')->push($this->id))
|
|
|
|
->where(function($q) {
|
|
|
|
return $q->whereNull('sent')
|
|
|
|
->orWhere('sent',FALSE);
|
|
|
|
}))
|
|
|
|
->count())
|
|
|
|
{
|
2021-07-17 05:48:07 +00:00
|
|
|
$o = new Packet($this);
|
|
|
|
|
|
|
|
foreach ($x->get() as $oo) {
|
|
|
|
$o->addNetmail($oo->packet());
|
2021-07-15 14:54:23 +00:00
|
|
|
|
2021-07-18 12:10:21 +00:00
|
|
|
$oo->packet = $o->name;
|
|
|
|
$oo->sent = TRUE;
|
|
|
|
$oo->sent_at = Carbon::now();
|
|
|
|
$oo->save();
|
2021-07-17 05:48:07 +00:00
|
|
|
}
|
2021-07-15 14:54:23 +00:00
|
|
|
|
2021-07-17 05:48:07 +00:00
|
|
|
return $o;
|
2021-07-15 14:54:23 +00:00
|
|
|
}
|
|
|
|
|
2021-07-17 05:48:07 +00:00
|
|
|
return NULL;
|
2021-07-15 14:54:23 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 10:43:29 +00:00
|
|
|
/**
|
|
|
|
* Parse a string and split it out as an FTN array
|
|
|
|
*
|
|
|
|
* @param string $ftn
|
|
|
|
* @return array
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public static function parseFTN(string $ftn): array
|
|
|
|
{
|
|
|
|
// http://ftsc.org/docs/frl-1028.002
|
|
|
|
if (! preg_match('#^([0-9]+):([0-9]+)/([0-9]+)(.([0-9]+))?(@([a-z0-9\-_~]{0,8}))?$#',strtolower($ftn),$matches))
|
|
|
|
throw new Exception('Invalid FTN: '.$ftn);
|
|
|
|
|
|
|
|
// Check our numbers are correct.
|
|
|
|
foreach ([1,2,3] as $i) {
|
2021-07-01 11:56:55 +00:00
|
|
|
if ((! is_numeric($matches[$i])) || ($matches[$i] > DomainController::NUMBER_MAX))
|
2021-06-29 10:43:29 +00:00
|
|
|
throw new Exception('Invalid FTN: '.$ftn);
|
|
|
|
}
|
|
|
|
|
2021-07-01 11:56:55 +00:00
|
|
|
if (isset($matches[5]) AND ((! is_numeric($matches[$i])) || ($matches[5] > DomainController::NUMBER_MAX)))
|
2021-06-29 10:43:29 +00:00
|
|
|
throw new Exception('Invalid FTN: '.$ftn);
|
|
|
|
|
|
|
|
return [
|
|
|
|
'z'=>(int)$matches[1],
|
|
|
|
'n'=>(int)$matches[2],
|
|
|
|
'f'=>(int)$matches[3],
|
|
|
|
'p'=>isset($matches[5]) && $matches[5] ? (int)$matches[5] : 0,
|
|
|
|
'd'=>$matches[7] ?? NULL
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-06-24 13:09:09 +00:00
|
|
|
/**
|
|
|
|
* Retrieve the address session details/passwords
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2021-06-24 10:16:37 +00:00
|
|
|
public function session(string $type): ?string
|
|
|
|
{
|
|
|
|
static $session = NULL;
|
|
|
|
|
|
|
|
if (is_null($session)) {
|
2021-07-04 11:47:23 +00:00
|
|
|
$session = (new SystemZone)
|
2021-06-24 10:16:37 +00:00
|
|
|
->where('zone_id',$this->zone_id)
|
|
|
|
->where('system_id',$this->system_id)
|
|
|
|
->single();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $session ? $session->{$type} : NULL;
|
|
|
|
}
|
2021-06-20 13:03:20 +00:00
|
|
|
}
|