2021-04-01 10:59:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Classes\File;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use Illuminate\Contracts\Filesystem\FileNotFoundException;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use League\Flysystem\UnreadableFileException;
|
|
|
|
|
2021-07-17 05:48:07 +00:00
|
|
|
use App\Models\Address;
|
|
|
|
|
2021-04-01 10:59:15 +00:00
|
|
|
/**
|
|
|
|
* Object representing the files we are sending
|
|
|
|
*
|
|
|
|
* @property-read resource fd
|
|
|
|
* @property-read int file_mtime
|
|
|
|
* @property-read int file_size
|
|
|
|
* @property-read string file_name
|
|
|
|
* @property-read int mail_size
|
|
|
|
* @property-read int total_count
|
|
|
|
* @property-read int total_sent
|
|
|
|
* @property-read int total_sent_bytes
|
|
|
|
*/
|
|
|
|
final class Send extends Item
|
|
|
|
{
|
|
|
|
private Collection $list;
|
|
|
|
private ?Item $sending;
|
2021-07-17 05:48:07 +00:00
|
|
|
private Collection $packets;
|
2021-04-01 10:59:15 +00:00
|
|
|
|
|
|
|
private mixed $f; // File descriptor
|
|
|
|
private int $start; // Time we started sending
|
|
|
|
private int $file_pos; // Current read pointer
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
// Initialise our variables
|
|
|
|
$this->list = collect();
|
2021-07-17 05:48:07 +00:00
|
|
|
$this->packets = collect();
|
2021-04-01 10:59:15 +00:00
|
|
|
$this->sending = NULL;
|
|
|
|
$this->file_pos = 0;
|
|
|
|
$this->f = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __get($key)
|
|
|
|
{
|
|
|
|
switch ($key) {
|
|
|
|
case 'fd':
|
2021-07-18 12:10:21 +00:00
|
|
|
return is_resource($this->f) ?: $this->f;
|
2021-04-01 10:59:15 +00:00
|
|
|
|
|
|
|
case 'file_count':
|
|
|
|
return $this->list
|
|
|
|
->filter(function($item) { return $item->isType(self::IS_FILE); })
|
|
|
|
->count();
|
|
|
|
|
|
|
|
case 'file_size':
|
|
|
|
return $this->list
|
|
|
|
->filter(function($item) { return $item->isType(self::IS_FILE); })
|
|
|
|
->sum(function($item) { return $item->file_size; });
|
|
|
|
|
|
|
|
case 'filepos':
|
|
|
|
return $this->file_pos;
|
|
|
|
|
|
|
|
case 'mail_count':
|
|
|
|
return $this->list
|
|
|
|
->filter(function($item) { return $item->isType(self::IS_ARC|self::IS_PKT); })
|
2021-07-17 05:48:07 +00:00
|
|
|
->count()
|
|
|
|
+ $this->packets->count();
|
2021-04-01 10:59:15 +00:00
|
|
|
|
|
|
|
case 'mail_size':
|
|
|
|
return $this->list
|
|
|
|
->filter(function($item) { return $item->isType(self::IS_ARC|self::IS_PKT); })
|
2021-07-17 05:48:07 +00:00
|
|
|
->sum(function($item) { return $item->file_size; })
|
|
|
|
+ $this->packets->sum(function($item) { return $item->file_size; });
|
2021-04-01 10:59:15 +00:00
|
|
|
|
|
|
|
case 'sendas':
|
|
|
|
return $this->sending ? $this->sending->{$key} : NULL;
|
|
|
|
|
|
|
|
case 'name':
|
|
|
|
case 'mtime':
|
|
|
|
case 'size':
|
|
|
|
return $this->sending ? $this->sending->{'file_'.$key} : NULL;
|
|
|
|
|
|
|
|
case 'total_sent':
|
|
|
|
return $this->list
|
|
|
|
->filter(function($item) { return ($item->action & self::I_SEND) && $item->sent === TRUE; })
|
2021-07-17 05:48:07 +00:00
|
|
|
->count()
|
|
|
|
+ $this->packets
|
|
|
|
->filter(function($item) { return ($item->action & self::I_SEND) && $item->sent === TRUE; })
|
|
|
|
->count();
|
2021-04-01 10:59:15 +00:00
|
|
|
|
|
|
|
case 'total_sent_bytes':
|
|
|
|
return $this->list
|
|
|
|
->filter(function($item) { return ($item->action & self::I_SEND) && $item->sent === TRUE; })
|
2021-07-17 05:48:07 +00:00
|
|
|
->sum(function($item) { return $item->file_size; })
|
|
|
|
+ $this->packets
|
|
|
|
->filter(function($item) { return ($item->action & self::I_SEND) && $item->sent === TRUE; })
|
|
|
|
->sum(function($item) { return $item->file_size; });
|
2021-04-01 10:59:15 +00:00
|
|
|
|
|
|
|
case 'total_count':
|
|
|
|
return $this->list
|
|
|
|
->filter(function($item) { return ($item->action & self::I_SEND) && $item->sent === FALSE; })
|
2021-07-17 05:48:07 +00:00
|
|
|
->count()
|
|
|
|
+ $this->packets
|
|
|
|
->filter(function($item) { return ($item->action & self::I_SEND) && $item->sent === FALSE; })
|
|
|
|
->count();
|
2021-04-01 10:59:15 +00:00
|
|
|
|
|
|
|
case 'total_size':
|
|
|
|
return $this->list
|
2021-07-17 05:48:07 +00:00
|
|
|
->sum(function($item) { return $item->file_size; })
|
|
|
|
+ $this->packets->sum(function($item) { return $item->file_size; });
|
2021-04-01 10:59:15 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Exception('Unknown key: '.$key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a file to the list of files to send
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function add(string $file): void
|
|
|
|
{
|
|
|
|
Log::debug(sprintf('%s: + Start [%s]',__METHOD__,$file));
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->list->push(new Item($file,self::I_SEND));
|
|
|
|
|
|
|
|
} catch (FileNotFoundException) {
|
|
|
|
Log::error(sprintf('%s: ! Item [%s] doesnt exist',__METHOD__,$file));
|
|
|
|
return;
|
|
|
|
|
|
|
|
} catch (UnreadableFileException) {
|
|
|
|
Log::error(sprintf('%s: ! Item [%s] cannot be read',__METHOD__,$file));
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Uncaught, rethrow the error
|
|
|
|
} catch (Exception $e) {
|
|
|
|
throw new Exception($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close the file descriptor of the file we are sending
|
|
|
|
*
|
|
|
|
* @param bool $successful
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function close(bool $successful): void
|
|
|
|
{
|
|
|
|
if (! $this->f)
|
|
|
|
throw new Exception('No file to close');
|
|
|
|
|
|
|
|
if ($successful) {
|
|
|
|
$this->sending->sent = TRUE;
|
|
|
|
$end = time()-$this->start;
|
|
|
|
Log::debug(sprintf('%s: - Closing [%s], sent in [%d]',__METHOD__,$this->sending->file_name,$end));
|
|
|
|
}
|
|
|
|
|
2021-07-17 05:48:07 +00:00
|
|
|
if (! $this->sending instanceof Mail)
|
|
|
|
fclose($this->f);
|
|
|
|
|
2021-04-01 10:59:15 +00:00
|
|
|
$this->sending = NULL;
|
|
|
|
$this->file_pos = 0;
|
|
|
|
$this->f = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if we are at the end of the file
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function feof(): bool
|
|
|
|
{
|
2021-07-17 05:48:07 +00:00
|
|
|
return ($this->sending instanceof Mail) ? ($this->file_pos == $this->size) : feof($this->f);
|
2021-04-01 10:59:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open a file for sending
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function open(): bool
|
|
|
|
{
|
|
|
|
Log::debug(sprintf('%s: + Start',__METHOD__));
|
|
|
|
|
2021-07-17 05:48:07 +00:00
|
|
|
// If we have mail, we'll send that first
|
|
|
|
if ($this->sending = $this->packets
|
|
|
|
->filter(function($item) { return ($item->action & self::I_SEND) && $item->sent === FALSE; })
|
|
|
|
->first())
|
|
|
|
{
|
|
|
|
$this->file_pos = 0;
|
|
|
|
$this->start = time();
|
|
|
|
$this->f = TRUE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2021-04-01 10:59:15 +00:00
|
|
|
$this->sending = $this->list
|
|
|
|
->filter(function($item) { return ($item->action & self::I_SEND) && $item->sent === FALSE; })
|
|
|
|
->first();
|
|
|
|
|
|
|
|
if (! $this->sending)
|
|
|
|
throw new Exception('No files to open');
|
|
|
|
|
|
|
|
$this->file_pos = 0;
|
|
|
|
$this->start = time();
|
|
|
|
|
|
|
|
$this->f = fopen($this->sending->file_name,'rb');
|
|
|
|
if (! $this->f) {
|
|
|
|
Log::error(sprintf('%s: ! Unable to open file [%s] for reading',__METHOD__,$this->sending->file_name));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log::info(sprintf('%s: = End - File [%s] opened with size [%d]',__METHOD__,$this->sending->file_name,$this->sending->file_size));
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2021-07-17 05:48:07 +00:00
|
|
|
/**
|
|
|
|
* Add our mail to the send queue
|
|
|
|
*
|
|
|
|
* @param Address $ao
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function mail(Address $ao): void
|
|
|
|
{
|
|
|
|
// Netmail
|
2021-07-18 12:10:21 +00:00
|
|
|
if ($x=$ao->getNetmail()) {
|
|
|
|
Log::debug(sprintf('%s: - Netmail(s) added for sending to [%s]',__METHOD__,$ao->ftn));
|
|
|
|
|
2021-07-17 05:48:07 +00:00
|
|
|
$this->packets->push(new Mail($x,self::I_SEND));
|
2021-07-18 12:10:21 +00:00
|
|
|
}
|
2021-07-17 05:48:07 +00:00
|
|
|
}
|
|
|
|
|
2021-04-01 10:59:15 +00:00
|
|
|
/**
|
|
|
|
* Read bytes of the sending file
|
|
|
|
*
|
|
|
|
* @param int $length
|
|
|
|
* @return string|null
|
|
|
|
* @throws UnreadableFileException
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function read(int $length): ?string
|
|
|
|
{
|
|
|
|
if (! $this->f)
|
|
|
|
throw new Exception('No file open for read');
|
|
|
|
|
2021-07-17 05:48:07 +00:00
|
|
|
// We are sending mail
|
|
|
|
if ($this->sending instanceof Mail) {
|
|
|
|
$data = $this->sending->read($this->file_pos,$length);
|
|
|
|
} else {
|
|
|
|
$data = fread($this->f,$length);
|
|
|
|
}
|
|
|
|
|
2021-04-01 10:59:15 +00:00
|
|
|
$this->file_pos += strlen($data);
|
|
|
|
Log::debug(sprintf('%s: - Read [%d] bytes, file pos now [%d]',__METHOD__,strlen($data),$this->file_pos));
|
|
|
|
|
|
|
|
if ($data === FALSE)
|
|
|
|
throw new UnreadableFileException('Error reading file: '.$this->sending->file_name);
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Seek to a specific position of our file
|
|
|
|
*
|
|
|
|
* @param int $pos
|
|
|
|
* @return bool
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function seek(int $pos): bool
|
|
|
|
{
|
|
|
|
if (! $this->f)
|
|
|
|
throw new Exception('No file open for seek');
|
|
|
|
|
2021-07-17 05:48:07 +00:00
|
|
|
if ($this->sending instanceof Mail) {
|
|
|
|
$rc = ($pos < $this->size) ? $pos : $this->size;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$rc = (fseek($this->f,$pos,SEEK_SET) === 0);
|
|
|
|
}
|
|
|
|
|
2021-04-01 10:59:15 +00:00
|
|
|
if ($rc)
|
|
|
|
$this->file_pos = $pos;
|
|
|
|
|
|
|
|
Log::debug(sprintf('%s: - Seeked to [%d]',__METHOD__,$this->file_pos));
|
|
|
|
|
|
|
|
return $rc;
|
|
|
|
}
|
|
|
|
}
|