2022-11-01 11:24:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Classes\FTN;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Contracts\Filesystem\FileNotFoundException;
|
2023-06-22 07:36:22 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2022-11-01 11:24:36 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2022-11-02 10:20:02 +00:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2022-11-01 11:24:36 +00:00
|
|
|
use League\Flysystem\UnableToWriteFile;
|
|
|
|
|
|
|
|
use App\Classes\FTN as FTNBase;
|
2023-06-22 07:36:22 +00:00
|
|
|
use App\Models\{Address,File,Filearea,Setup};
|
2022-11-01 11:24:36 +00:00
|
|
|
use App\Traits\EncodeUTF8;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class TIC
|
2023-06-22 07:36:22 +00:00
|
|
|
* Used create the structure of TIC files
|
2022-11-01 11:24:36 +00:00
|
|
|
*
|
|
|
|
* @package App\Classes
|
|
|
|
*/
|
|
|
|
class Tic extends FTNBase
|
|
|
|
{
|
|
|
|
use EncodeUTF8;
|
|
|
|
|
2022-11-15 11:01:05 +00:00
|
|
|
private const LOGKEY = 'FT-';
|
2022-11-01 11:24:36 +00:00
|
|
|
|
|
|
|
private const cast_utf8 = [
|
|
|
|
];
|
|
|
|
|
|
|
|
// Single value kludge items and whether they are required
|
|
|
|
// http://ftsc.org/docs/fts-5006.001
|
|
|
|
private array $_kludge = [
|
|
|
|
'AREA' => TRUE,
|
|
|
|
'areadesc' => FALSE,
|
|
|
|
'ORIGIN' => TRUE,
|
|
|
|
'FROM' => TRUE,
|
|
|
|
'to' => FALSE,
|
|
|
|
'FILE' => TRUE, // 8.3 DOS format
|
|
|
|
'lfile' => FALSE, // alias fullname
|
|
|
|
'fullname' => FALSE,
|
|
|
|
'size' => FALSE,
|
|
|
|
'date' => FALSE, // File creation date
|
|
|
|
'desc' => FALSE, // One line description of file
|
|
|
|
'ldesc' => FALSE, // Can have multiple
|
|
|
|
'created' => FALSE,
|
|
|
|
'magic' => FALSE,
|
|
|
|
'replaces' => FALSE, // ? and * are wildcards, as per DOS
|
|
|
|
'CRC' => TRUE, // crc-32
|
|
|
|
'PATH' => TRUE, // can have multiple: [FTN] [unix timestamp] [datetime human readable] [signature]
|
|
|
|
'SEENBY' => TRUE,
|
|
|
|
'pw' => FALSE, // Password
|
|
|
|
];
|
|
|
|
|
2023-06-22 07:36:22 +00:00
|
|
|
private File $fo;
|
2022-11-01 11:24:36 +00:00
|
|
|
private Filearea $area;
|
2023-06-22 07:36:22 +00:00
|
|
|
private Collection $values;
|
2022-11-01 11:24:36 +00:00
|
|
|
|
|
|
|
private Address $origin; // Should be first address in Path
|
|
|
|
private Address $from; // Should be last address in Path
|
|
|
|
private Address $to; // Should be me
|
|
|
|
|
2023-06-22 07:36:22 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->fo = new File;
|
|
|
|
|
|
|
|
$this->fo->kludges = collect();
|
|
|
|
$this->fo->set_path = collect();
|
|
|
|
$this->fo->set_seenby = collect();
|
|
|
|
$this->fo->rogue_path = collect();
|
|
|
|
$this->fo->rogue_seenby = collect();
|
|
|
|
|
|
|
|
$this->values = collect();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does this TIC file bring us a nodelist
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isNodelist(): bool
|
|
|
|
{
|
|
|
|
return (($this->fo->nodelist_filearea_id === $this->fo->filearea->domain->filearea_id)
|
2023-06-23 07:33:47 +00:00
|
|
|
&& (preg_match(str_replace(['.','?'],['\.','.'],'#^'.$this->fo->filearea->domain->nodelist_filename.'$#i'),$this->fo->name)));
|
2023-06-22 07:36:22 +00:00
|
|
|
}
|
2022-11-03 11:05:49 +00:00
|
|
|
|
2023-06-22 07:36:22 +00:00
|
|
|
/**
|
|
|
|
* Generate a TIC file for an address
|
|
|
|
*
|
|
|
|
* @param Address $ao
|
|
|
|
* @param File $fo
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function generate(Address $ao,File $fo): string
|
|
|
|
{
|
|
|
|
$sysaddress = Setup::findOrFail(config('app.id'))->system->match($ao->zone)->first();
|
|
|
|
|
|
|
|
$result = collect();
|
|
|
|
|
|
|
|
// Origin is the first address in our path
|
|
|
|
$result->put('ORIGIN',$fo->path->first()->ftn3d);
|
|
|
|
$result->put('FROM',$sysaddress->ftn3d);
|
|
|
|
$result->put('TO',$ao->ftn3d);
|
2023-06-23 07:33:47 +00:00
|
|
|
$result->put('FILE',$fo->name);
|
2023-06-22 07:36:22 +00:00
|
|
|
$result->put('SIZE',$fo->size);
|
|
|
|
if ($fo->description)
|
|
|
|
$result->put('DESC',$fo->description);
|
|
|
|
$result->put('AREA',$fo->filearea->name);
|
|
|
|
$result->put('AREADESC',$fo->filearea->description);
|
|
|
|
$result->put('PW',$ao->session('ticpass'));
|
|
|
|
$result->put('CRC',sprintf("%X",$fo->crc));
|
|
|
|
|
|
|
|
$out = '';
|
|
|
|
foreach ($result as $key=>$value)
|
|
|
|
$out .= sprintf("%s %s\r\n",$key,$value);
|
|
|
|
|
|
|
|
foreach ($fo->path as $o)
|
|
|
|
$out .= sprintf("PATH %s %s %s\r\n",$o->ftn3d,$o->pivot->datetime,$o->pivot->extra);
|
|
|
|
|
|
|
|
foreach ($fo->seenby as $o)
|
|
|
|
$out .= sprintf("SEENBY %s\r\n",$o->ftn3d);
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
}
|
2022-11-01 11:24:36 +00:00
|
|
|
|
2023-06-22 07:36:22 +00:00
|
|
|
/**
|
|
|
|
* Load a TIC file from an existing filename
|
|
|
|
*
|
|
|
|
* @param string $filename
|
|
|
|
* @return void
|
|
|
|
* @throws FileNotFoundException
|
|
|
|
*/
|
|
|
|
public function load(string $filename): void
|
|
|
|
{
|
|
|
|
Log::info(sprintf('%s:Processing TIC file [%s]',self::LOGKEY,$filename));
|
2022-11-01 11:24:36 +00:00
|
|
|
|
|
|
|
list($hex,$name) = explode('-',$filename);
|
2022-11-03 11:05:49 +00:00
|
|
|
$hex = basename($hex);
|
2022-11-01 11:24:36 +00:00
|
|
|
|
2022-11-03 11:05:49 +00:00
|
|
|
if (! file_exists($filename))
|
|
|
|
throw new FileNotFoundException(sprintf('%s:File [%s] doesnt exist',self::LOGKEY,realpath($filename)));
|
2022-11-01 11:24:36 +00:00
|
|
|
|
2023-06-22 07:36:22 +00:00
|
|
|
if (! is_readable($filename))
|
|
|
|
throw new UnableToWriteFile(sprintf('%s:File [%s] is not readable',self::LOGKEY,realpath($filename)));
|
2022-11-01 11:24:36 +00:00
|
|
|
|
2022-11-03 11:05:49 +00:00
|
|
|
$f = fopen($filename,'rb');
|
2022-11-01 11:24:36 +00:00
|
|
|
if (! $f) {
|
2022-11-03 11:05:49 +00:00
|
|
|
Log::error(sprintf('%s:! Unable to open file [%s] for writing',self::LOGKEY,$filename));
|
2022-11-01 11:24:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (! feof($f)) {
|
|
|
|
$line = chop(fgets($f));
|
|
|
|
$matches = [];
|
|
|
|
|
|
|
|
if (! $line)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
preg_match('/([a-zA-Z]+)\ (.*)/',$line,$matches);
|
|
|
|
|
|
|
|
if (in_array(strtolower($matches[1]),$this->_kludge)) {
|
|
|
|
switch ($k=strtolower($matches[1])) {
|
|
|
|
case 'area':
|
|
|
|
$this->{$k} = Filearea::singleOrNew(['name'=>strtoupper($matches[2])]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'origin':
|
|
|
|
case 'from':
|
|
|
|
case 'to':
|
|
|
|
$this->{$k} = Address::findFTN($matches[2]);
|
|
|
|
|
|
|
|
// @todo If $this->{$k} is null, we have discovered the system and it should be created
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'file':
|
2022-11-02 10:20:02 +00:00
|
|
|
if (! Storage::disk('local')->exists($x=sprintf('%s/%s-%s',config('app.fido'),$hex,$matches[2])))
|
2022-11-01 11:24:36 +00:00
|
|
|
throw new FileNotFoundException(sprintf('File not found? [%s]',$x));
|
|
|
|
|
2023-06-23 07:33:47 +00:00
|
|
|
$this->fo->name = $matches[2];
|
2023-06-22 07:36:22 +00:00
|
|
|
$this->fo->fullname = $x;
|
2022-11-01 11:24:36 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'areadesc':
|
2023-06-22 07:36:22 +00:00
|
|
|
$areadesc = $matches[2];
|
|
|
|
break;
|
|
|
|
|
2022-11-01 11:24:36 +00:00
|
|
|
case 'created':
|
2023-06-22 07:36:22 +00:00
|
|
|
// ignored
|
2022-11-01 11:24:36 +00:00
|
|
|
break;
|
|
|
|
|
2023-06-22 07:36:22 +00:00
|
|
|
case 'pw':
|
|
|
|
$pw = $matches[2];
|
|
|
|
|
|
|
|
case 'lfile':
|
2023-06-23 07:33:47 +00:00
|
|
|
$this->fo->lname = $matches[2];
|
|
|
|
|
|
|
|
case 'desc':
|
2022-11-01 11:24:36 +00:00
|
|
|
case 'magic':
|
|
|
|
case 'replaces':
|
2023-06-22 07:36:22 +00:00
|
|
|
case 'size':
|
|
|
|
$this->fo->{$k} = $matches[2];
|
2022-11-01 11:24:36 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'fullname':
|
2023-06-22 07:36:22 +00:00
|
|
|
$this->fo->lfile = $matches[2];
|
2022-11-01 11:24:36 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'date':
|
2023-06-22 07:36:22 +00:00
|
|
|
$this->fo->datetime = Carbon::create($matches[2]);
|
2022-11-01 11:24:36 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'ldesc':
|
2023-06-22 07:36:22 +00:00
|
|
|
$this->fo->{$k} .= $matches[2];
|
2022-11-01 11:24:36 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'crc':
|
2023-06-22 07:36:22 +00:00
|
|
|
$this->fo->{$k} = hexdec($matches[2]);
|
2022-11-01 11:24:36 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'path':
|
|
|
|
$x = [];
|
|
|
|
preg_match(sprintf('#^[Pp]ath (%s)\ ?([0-9]+)\ ?(.*)$#',Address::ftn_regex),$line,$x);
|
|
|
|
$ao = Address::findFTN($x[1]);
|
|
|
|
|
|
|
|
if (! $ao) {
|
2023-06-22 07:36:22 +00:00
|
|
|
$this->fo->rogue_path->push($matches[2]);
|
2022-11-01 11:24:36 +00:00
|
|
|
} else {
|
2023-06-22 07:36:22 +00:00
|
|
|
$this->fo->set_path->push(['address'=>$ao,'datetime'=>Carbon::createFromTimestamp($x[8]),'extra'=>$x[9]]);
|
2022-11-01 11:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'seenby':
|
|
|
|
$ao = Address::findFTN($matches[2]);
|
|
|
|
|
|
|
|
if (! $ao) {
|
2023-06-22 07:36:22 +00:00
|
|
|
$this->fo->rogue_seenby->push($matches[2]);
|
2022-11-01 11:24:36 +00:00
|
|
|
} else {
|
2023-06-22 07:36:22 +00:00
|
|
|
$this->fo->set_seenby->push($ao->id);
|
2022-11-01 11:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2023-06-22 07:36:22 +00:00
|
|
|
$this->fo->kludges->push($line);
|
2022-11-01 11:24:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose($f);
|
|
|
|
|
2023-06-22 07:36:22 +00:00
|
|
|
$f = fopen($x=Storage::disk('local')->path($this->fo->fullname),'rb');
|
2022-11-01 11:24:36 +00:00
|
|
|
$stat = fstat($f);
|
|
|
|
fclose($f);
|
|
|
|
|
|
|
|
// Validate Size
|
2023-06-22 07:36:22 +00:00
|
|
|
if ($this->fo->size !== ($y=$stat['size']))
|
|
|
|
throw new \Exception(sprintf('TIC file size [%d] doesnt match file [%s] (%d)',$this->fo->size,$this->fo->fullname,$y));
|
2022-11-01 11:24:36 +00:00
|
|
|
|
|
|
|
// Validate CRC
|
2023-06-22 07:36:22 +00:00
|
|
|
if (sprintf('%08x',$this->fo->crc) !== ($y=hash_file('crc32b',$x)))
|
|
|
|
throw new \Exception(sprintf('TIC file CRC [%08x] doesnt match file [%s] (%s)',$this->fo->crc,$this->fo->fullname,$y));
|
2022-11-01 11:24:36 +00:00
|
|
|
|
|
|
|
// Validate Password
|
2023-06-22 07:36:22 +00:00
|
|
|
if ($pw !== ($y=$this->from->session('ticpass')))
|
|
|
|
throw new \Exception(sprintf('TIC file PASSWORD [%s] doesnt match system [%s] (%s)',$pw,$this->from->ftn,$y));
|
2022-11-01 11:24:36 +00:00
|
|
|
|
|
|
|
// Validate Sender is linked (and permitted to send)
|
|
|
|
if ($this->from->fileareas->search(function($item) { return $item->id === $this->area->id; }) === FALSE)
|
|
|
|
throw new \Exception(sprintf('Node [%s] is not subscribed to [%s]',$this->from->ftn,$this->area->name));
|
|
|
|
|
|
|
|
// If the filearea is to be autocreated, create it
|
|
|
|
if (! $this->area->exists) {
|
2023-06-22 07:36:22 +00:00
|
|
|
$this->area->description = $areadesc;
|
2022-11-01 11:24:36 +00:00
|
|
|
$this->area->active = TRUE;
|
|
|
|
$this->area->public = FALSE;
|
|
|
|
$this->area->notes = 'Autocreated';
|
|
|
|
$this->area->domain_id = $this->from->zone->domain_id;
|
|
|
|
$this->area->save();
|
|
|
|
}
|
|
|
|
|
2023-06-22 07:36:22 +00:00
|
|
|
$this->fo->filearea_id = $this->area->id;
|
|
|
|
$this->fo->fftn_id = $this->origin->id;
|
2022-11-01 11:24:36 +00:00
|
|
|
|
|
|
|
// If the file create time is blank, we'll take the files
|
2023-06-22 07:36:22 +00:00
|
|
|
if (! $this->fo->datetime)
|
|
|
|
$this->fo->datetime = Carbon::createFromTimestamp($stat['ctime']);
|
2022-11-01 11:24:36 +00:00
|
|
|
|
2023-06-22 07:36:22 +00:00
|
|
|
$this->fo->save();
|
2022-11-01 11:24:36 +00:00
|
|
|
}
|
|
|
|
}
|