Debian Codegraph @

classes/Mail/ImapEngine/MailAttachment.php

raw symbols SCIP
1 <?php
2
3 /**
4 * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
5 * Copyright (C) 2020-2024 Aleksey Andreev (liuch)
6 *
7 * Available at:
8 * https://github.com/liuch/dmarc-srg
9 *
10 * This program is free software: you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation, either version 3 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
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 }