. */ namespace Liuch\DmarcSrg\Mail\PhpExtension; use Liuch\DmarcSrg\Mail\MailAttachmentInterface; use Liuch\DmarcSrg\ReportFile\ReportFile; use Liuch\DmarcSrg\Exception\SoftException; class MailAttachment extends \Liuch\DmarcSrg\Mail\MailAttachment { private $mailbox; private $filename; private $bytes; private $number; private $mnumber; private $encoding; private $stream; private $mime_type; public function __construct($data) { $this->mailbox = $data['mailbox']; $this->filename = $data['filename']; $this->bytes = $data['bytes']; $this->number = $data['number']; $this->mnumber = $data['mnumber']; $this->encoding = $data['encoding']; $this->stream = null; $this->mime_type = null; } public function __destruct() { if (!is_null($this->stream) && get_resource_type($this->stream) == 'stream') { fclose($this->stream); } } public function mimeType(): string { if (is_null($this->mime_type)) { $this->mime_type = ReportFile::getMimeType($this->filename, $this->datastream()); } return $this->mime_type; } public function size(): int { return $this->bytes; } public function filename() { return $this->filename; } public function datastream() { if (is_null($this->stream)) { $this->stream = fopen('php://temp', 'r+'); fwrite($this->stream, $this->toString()); } if (stream_get_meta_data($this->stream)['seekable'] ?? false) { rewind($this->stream); } return $this->stream; } private function fetchBody() { return imap_fetchbody($this->mailbox->connection(), $this->mnumber, strval($this->number), FT_PEEK | FT_UID); } private function toString() { switch ($this->encoding) { case ENC7BIT: case ENC8BIT: case ENCBINARY: return $this->fetchBody(); case ENCBASE64: return base64_decode($this->fetchBody()); case ENCQUOTEDPRINTABLE: return imap_qprint($this->fetchBody()); } throw new SoftException('Encoding failed: Unknown encoding'); } }