| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace Liuch\DmarcSrg\Mail\ImapEngine; |
| 24 |
|
| 25 |
use Liuch\DmarcSrg\ReportFile\ReportFile; |
| 26 |
|
| 27 |
class MailAttachment extends \Liuch\DmarcSrg\Mail\MailAttachment |
| 28 |
{ |
| 29 |
private $bytes = -1; |
| 30 |
private $stream = null; |
| 31 |
private $mime_type = null; |
| 32 |
private $attachment; |
| 33 |
|
| 34 |
public function __construct($data) |
| 35 |
{ |
| 36 |
$this->attachment = $data; |
| 37 |
} |
| 38 |
|
| 39 |
public function __destruct() |
| 40 |
{ |
| 41 |
if (!is_null($this->stream) && get_resource_type($this->stream) === 'stream') { |
| 42 |
fclose($this->stream); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
public function mimeType(): string |
| 47 |
{ |
| 48 |
if (is_null($this->mime_type)) { |
| 49 |
$this->mime_type = $this->attachment->contentType(); |
| 50 |
if ($this->mime_type === 'application/octet-stream') { |
| 51 |
$this->mime_type = ReportFile::getMimeType($this->attachment->filename(), $this->datastream()); |
| 52 |
} |
| 53 |
} |
| 54 |
return $this->mime_type; |
| 55 |
} |
| 56 |
|
| 57 |
public function size(): int |
| 58 |
{ |
| 59 |
if ($this->bytes === -1) { |
| 60 |
$bytes = $this->attachment->contentStream()->getSize(); |
| 61 |
if (is_null($bytes)) { |
| 62 |
$this->makeTemporaryFile(); |
| 63 |
$stat = fstat($this->stream); |
| 64 |
$bytes = $stat['size'] ?? -1; |
| 65 |
} |
| 66 |
$this->bytes = $bytes; |
| 67 |
} |
| 68 |
return $this->bytes; |
| 69 |
} |
| 70 |
|
| 71 |
public function filename() |
| 72 |
{ |
| 73 |
return $this->attachment->filename(); |
| 74 |
} |
| 75 |
|
| 76 |
public function datastream() |
| 77 |
{ |
| 78 |
if (is_null($this->stream)) { |
| 79 |
$this->makeTemporaryFile(); |
| 80 |
} |
| 81 |
if (stream_get_meta_data($this->stream)['seekable'] ?? false) { |
| 82 |
rewind($this->stream); |
| 83 |
} |
| 84 |
return $this->stream; |
| 85 |
} |
| 86 |
|
| 87 |
private function makeTemporaryFile() |
| 88 |
{ |
| 89 |
$this->stream = fopen('php://temp', 'r+'); |
| 90 |
fwrite($this->stream, $this->attachment->contentStream()); |
| 91 |
} |
| 92 |
} |