2021-04-01 10:59:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Classes\File;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2021-07-18 12:10:21 +00:00
|
|
|
use Symfony\Component\HttpFoundation\File\File;
|
2021-04-01 10:59:15 +00:00
|
|
|
use Symfony\Component\HttpFoundation\File\Exception\FileException;
|
|
|
|
|
2021-07-30 14:35:52 +00:00
|
|
|
use App\Classes\FTN\InvalidPacketException;
|
2021-07-18 12:10:21 +00:00
|
|
|
use App\Classes\FTN\Packet;
|
|
|
|
use App\Jobs\ProcessPacket;
|
|
|
|
use App\Models\Address;
|
|
|
|
|
2021-04-01 10:59:15 +00:00
|
|
|
/**
|
|
|
|
* Object representing the files we are receiving
|
|
|
|
*
|
|
|
|
* @property-read resource $fd
|
|
|
|
* @property-read int total_recv
|
|
|
|
* @property-read int total_recv_bytes
|
|
|
|
*/
|
|
|
|
final class Receive extends Item
|
|
|
|
{
|
2021-08-17 13:49:39 +00:00
|
|
|
private const LOGKEY = 'IR-';
|
|
|
|
|
2021-07-18 12:10:21 +00:00
|
|
|
private Address $ao;
|
2021-04-01 10:59:15 +00:00
|
|
|
private Collection $list;
|
|
|
|
private ?Item $receiving;
|
|
|
|
|
|
|
|
private mixed $f; // File descriptor
|
|
|
|
private int $start; // Time we started receiving
|
|
|
|
private int $file_pos; // Current write pointer
|
2021-07-18 12:10:21 +00:00
|
|
|
private string $file; // Local filename for file received
|
2021-04-01 10:59:15 +00:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
// Initialise our variables
|
|
|
|
$this->list = collect();
|
|
|
|
$this->receiving = NULL;
|
|
|
|
$this->file_pos = 0;
|
|
|
|
$this->f = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __get($key)
|
|
|
|
{
|
|
|
|
switch ($key) {
|
|
|
|
case 'fd':
|
|
|
|
return is_resource($this->f);
|
|
|
|
|
|
|
|
case 'filepos':
|
|
|
|
return $this->file_pos;
|
|
|
|
|
|
|
|
case 'mtime':
|
|
|
|
case 'name':
|
|
|
|
case 'size':
|
|
|
|
return $this->receiving ? $this->receiving->{'file_'.$key} : NULL;
|
|
|
|
|
2021-08-15 14:41:43 +00:00
|
|
|
case 'name_size_time':
|
|
|
|
return sprintf('%s %lu %lu',$this->name,$this->size,$this->mtime);
|
|
|
|
|
2021-04-01 10:59:15 +00:00
|
|
|
case 'to_get':
|
|
|
|
return $this->list
|
|
|
|
->filter(function($item) { return ($item->action & self::I_RECV) && $item->received === FALSE; })
|
|
|
|
->count();
|
|
|
|
|
|
|
|
case 'total_recv':
|
|
|
|
return $this->list
|
|
|
|
->filter(function($item) { return ($item->action & self::I_RECV) && $item->received === TRUE; })
|
|
|
|
->count();
|
|
|
|
|
|
|
|
case 'total_recv_bytes':
|
|
|
|
return $this->list
|
|
|
|
->filter(function($item) { return ($item->action & self::I_RECV) && $item->received === TRUE; })
|
|
|
|
->sum(function($item) { return $item->file_size; });
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Exception('Unknown key: '.$key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close the file descriptor for our incoming file
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function close(): void
|
|
|
|
{
|
|
|
|
if (! $this->f)
|
|
|
|
throw new Exception('No file to close');
|
|
|
|
|
2021-08-12 11:59:48 +00:00
|
|
|
if ($this->file_pos != $this->receiving->file_size) {
|
2021-08-17 13:49:39 +00:00
|
|
|
Log::warning(sprintf('%s: - Closing [%s], but missing [%d] bytes',self::LOGKEY,$this->receiving->file_name,$this->receiving->file_size-$this->file_pos));
|
2021-08-12 11:59:48 +00:00
|
|
|
$this->receiving->incomplete = TRUE;
|
|
|
|
}
|
2021-04-01 10:59:15 +00:00
|
|
|
|
|
|
|
$this->receiving->received = TRUE;
|
|
|
|
|
|
|
|
$end = time()-$this->start;
|
2021-08-17 13:49:39 +00:00
|
|
|
Log::debug(sprintf('%s: - Closing [%s], received in [%d]',self::LOGKEY,$this->receiving->file_name,$end));
|
2021-04-01 10:59:15 +00:00
|
|
|
|
|
|
|
fclose($this->f);
|
|
|
|
$this->file_pos = 0;
|
|
|
|
$this->f = NULL;
|
2021-07-18 12:10:21 +00:00
|
|
|
|
2021-08-12 11:59:48 +00:00
|
|
|
// If the packet has been received but not the right size, dont process it any more.
|
|
|
|
|
2021-07-18 12:10:21 +00:00
|
|
|
// If we received a packet, we'll dispatch a job to process it
|
2021-08-12 11:59:48 +00:00
|
|
|
if (! $this->receiving->incomplete)
|
|
|
|
switch ($this->receiving->file_type) {
|
|
|
|
case self::IS_PKT:
|
2021-08-17 13:49:39 +00:00
|
|
|
Log::info(sprintf('%s: - Processing mail packet [%s]',self::LOGKEY,$this->file));
|
2021-07-18 12:10:21 +00:00
|
|
|
|
2021-08-12 11:59:48 +00:00
|
|
|
try {
|
|
|
|
$po = Packet::open(new File($this->file),$this->ao->zone->domain);
|
2021-07-30 14:35:52 +00:00
|
|
|
|
2021-08-12 11:59:48 +00:00
|
|
|
} catch (InvalidPacketException $e) {
|
2021-08-17 13:49:39 +00:00
|
|
|
Log::error(sprintf('%s: - Not deleting packet [%s], as it generated an exception',self::LOGKEY,$this->file));
|
2021-07-30 14:35:52 +00:00
|
|
|
|
2021-08-12 11:59:48 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-07-30 14:35:52 +00:00
|
|
|
|
2021-08-18 14:20:34 +00:00
|
|
|
// Check the messages are from the uplink
|
|
|
|
if ($this->ao->system->addresses->search(function($item) use ($po) { return $item->id == $po->fftn_o->id; }) === FALSE) {
|
2021-08-24 13:42:03 +00:00
|
|
|
Log::error(sprintf('%s: ! Packet [%s] is not from this link? [%d]',self::LOGKEY,$po->fftn_o->ftn,$this->ao->system_id));
|
2021-08-18 14:20:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the packet password
|
|
|
|
if ($this->ao->session('pktpass') !== $po->password) {
|
|
|
|
Log::error(sprintf('%s: ! Packet from [%s] with password [%s] is invalid (%s)',self::LOGKEY,$this->ao->ftn,$po->password));
|
|
|
|
// @todo Generate message to system advising invalid password - that message should be sent without a packet password!
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-08-24 13:42:03 +00:00
|
|
|
foreach ($po as $msg) {
|
2021-08-17 13:49:39 +00:00
|
|
|
Log::info(sprintf('%s: - Mail from [%s] to [%s]',self::LOGKEY,$msg->fftn,$msg->tftn));
|
2021-07-18 12:10:21 +00:00
|
|
|
|
2021-08-12 11:59:48 +00:00
|
|
|
// @todo Quick check that the packet should be processed by us.
|
|
|
|
// @todo validate that the packet's zone is in the domain.
|
2021-07-18 12:10:21 +00:00
|
|
|
|
2021-08-12 11:59:48 +00:00
|
|
|
// Dispatch job.
|
2021-08-24 13:42:03 +00:00
|
|
|
ProcessPacket::dispatch($msg);
|
2021-08-12 11:59:48 +00:00
|
|
|
}
|
2021-07-19 14:26:12 +00:00
|
|
|
|
2021-08-13 13:46:48 +00:00
|
|
|
if ($po->errors->count()) {
|
2021-08-17 13:49:39 +00:00
|
|
|
Log::info(sprintf('%s: - Not deleting packet [%s], as it has validation errors',self::LOGKEY,$this->file));
|
2021-07-19 14:26:12 +00:00
|
|
|
|
2021-08-12 11:59:48 +00:00
|
|
|
// If we want to keep the packet, we could do that logic here
|
|
|
|
} elseif (! config('app.packet_keep')) {
|
2021-08-17 13:49:39 +00:00
|
|
|
Log::debug(sprintf('%s: - Deleting processed packet [%s]',self::LOGKEY,$this->file));
|
2021-08-12 11:59:48 +00:00
|
|
|
unlink($this->file);
|
|
|
|
}
|
2021-07-19 14:26:12 +00:00
|
|
|
|
2021-08-12 11:59:48 +00:00
|
|
|
break;
|
2021-07-18 12:10:21 +00:00
|
|
|
|
2021-08-12 11:59:48 +00:00
|
|
|
default:
|
2021-08-17 13:49:39 +00:00
|
|
|
Log::debug(sprintf('%s: - Leaving file [%s] in the inbound dir',self::LOGKEY,$this->file));
|
2021-08-12 11:59:48 +00:00
|
|
|
}
|
2021-07-18 12:10:21 +00:00
|
|
|
|
|
|
|
$this->receiving = NULL;
|
2021-04-01 10:59:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open the file descriptor to receive a file
|
|
|
|
*
|
2021-08-18 14:20:34 +00:00
|
|
|
* @param Address $ao
|
2021-04-01 10:59:15 +00:00
|
|
|
* @param bool $check
|
|
|
|
* @return bool
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2021-07-18 12:10:21 +00:00
|
|
|
public function open(Address $ao,bool $check=FALSE): bool
|
2021-04-01 10:59:15 +00:00
|
|
|
{
|
2021-08-17 13:49:39 +00:00
|
|
|
Log::debug(sprintf('%s:+ open [%d]',self::LOGKEY,$check));
|
2021-04-01 10:59:15 +00:00
|
|
|
|
|
|
|
// Check we can open this file
|
|
|
|
// @todo
|
|
|
|
// @todo implement return 2 - SKIP file
|
|
|
|
// @todo implement return 4 - SUSPEND(?) file
|
|
|
|
if ($check) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $this->receiving)
|
|
|
|
throw new Exception('No files currently receiving');
|
|
|
|
|
2021-07-18 12:10:21 +00:00
|
|
|
$this->ao = $ao;
|
2021-04-01 10:59:15 +00:00
|
|
|
$this->file_pos = 0;
|
|
|
|
$this->start = time();
|
2021-07-18 12:10:21 +00:00
|
|
|
$this->file = sprintf('storage/app/%s/%04X-%s',config('app.fido'),$this->ao->id,$this->receiving->recvas);
|
2021-04-01 10:59:15 +00:00
|
|
|
|
2021-08-17 13:49:39 +00:00
|
|
|
Log::debug(sprintf('%s: - Opening [%s]',self::LOGKEY,$this->file));
|
2021-07-18 12:10:21 +00:00
|
|
|
$this->f = fopen($this->file,'wb');
|
2021-04-01 10:59:15 +00:00
|
|
|
if (! $this->f) {
|
2021-08-17 13:49:39 +00:00
|
|
|
Log::error(sprintf('%s:! Unable to open file [%s] for writing',self::LOGKEY,$this->receiving->file_name));
|
2021-04-01 10:59:15 +00:00
|
|
|
return 3; // @todo change to const
|
|
|
|
}
|
|
|
|
|
2021-08-17 13:49:39 +00:00
|
|
|
Log::info(sprintf('%s:= open - File [%s] opened for writing',self::LOGKEY,$this->receiving->file_name));
|
2021-04-01 10:59:15 +00:00
|
|
|
return 0; // @todo change to const
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a new file to receive
|
|
|
|
*
|
|
|
|
* @param array $file
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function new(array $file): void
|
|
|
|
{
|
2021-08-17 13:49:39 +00:00
|
|
|
Log::debug(sprintf('%s:+ new [%s]',self::LOGKEY,join('|',$file)));
|
2021-04-01 10:59:15 +00:00
|
|
|
|
|
|
|
if ($this->receiving)
|
|
|
|
throw new Exception('Can only have 1 file receiving at a time');
|
|
|
|
|
|
|
|
$o = new Item($file,self::I_RECV);
|
|
|
|
$this->list->push($o);
|
|
|
|
|
|
|
|
$this->receiving = $o;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write data to the file we are receiving
|
|
|
|
*
|
|
|
|
* @param string $buf
|
|
|
|
* @return int
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function write(string $buf): int
|
|
|
|
{
|
|
|
|
if (! $this->f)
|
|
|
|
throw new Exception('No file open for read');
|
|
|
|
|
|
|
|
if ($this->file_pos+strlen($buf) > $this->receiving->file_size)
|
|
|
|
throw new Exception(sprintf('Too many bytes received [%d] (%d)?',$this->file_pos+strlen($buf),$this->receiving->file_size));
|
|
|
|
|
|
|
|
$rc = fwrite($this->f,$buf);
|
|
|
|
|
|
|
|
if ($rc === FALSE)
|
|
|
|
throw new FileException('Error while writing to file');
|
|
|
|
|
|
|
|
$this->file_pos += $rc;
|
2021-08-17 13:49:39 +00:00
|
|
|
Log::debug(sprintf('%s:- Write [%d] bytes, file pos now [%d] of [%d] (%d)',self::LOGKEY,$rc,$this->file_pos,$this->receiving->file_size,strlen($buf)));
|
2021-04-01 10:59:15 +00:00
|
|
|
|
|
|
|
return $rc;
|
|
|
|
}
|
|
|
|
}
|