Debian Codegraph @

classes/Mail/PhpExtension/MailMessage.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\PhpExtension;
24
25 use Liuch\DmarcSrg\Core;
26 use Liuch\DmarcSrg\Mail\MailMessageInterface;
27 use Liuch\DmarcSrg\Exception\SoftException;
28 use Liuch\DmarcSrg\Exception\MailboxException;
29
30 class MailMessage extends \Liuch\DmarcSrg\Mail\MailMessage
31 {
32 private $mailbox;
33 private $number;
34 private $attachments_cnt;
35
36 public function __construct($data)
37 {
38 $this->mailbox = $data['mailbox'];
39 $this->number = $data['number'];
40 $this->attachments_cnt = -1;
41 }
42
43 public function overview(): array
44 {
45 $res = [];
46 $list = @imap_fetch_overview($this->mailbox->connection(), strval($this->number), FT_UID);
47 if (($ov = $list[0] ?? null)) {
48 if (property_exists($ov, 'from')) {
49 $res['from'] = $ov->from;
50 }
51 if (property_exists($ov, 'date')) {
52 try {
53 $res['date'] = new \DateTime($ov->date);
54 } catch (\Exception $e) {
55 }
56 }
57 } else {
58 if ($error_message = imap_last_error()) {
59 Core::instance()->logger()->error("imap_fetch_overview failed: {$error_message}");
60 }
61 MailBox::resetErrorStack();
62 }
63 return $res;
64 }
65
66 public function markSeen(): void
67 {
68 MailBox::resetErrorStack();
69 @imap_setflag_full($this->mailbox->connection(), strval($this->number), '\\Seen', ST_UID);
70 if (($error_message = imap_last_error())) {
71 MailBox::resetErrorStack();
72 Core::instance()->logger()->error("imap_setflag_full failed: {$error_message}");
73 throw new MailboxException("Failed to make a message seen: {$error_message}");
74 }
75 }
76
77 public function attachment()
78 {
79 $this->ensureAttachment();
80 return $this->attachment;
81 }
82
83 public function move(string $folder): void
84 {
85 $this->mailbox->connect();
86 $target = MailBox::utf8ToMutf7($this->mailbox->folder()) .
87 $this->mailbox->delimiter() . MailBox::utf8ToMutf7($folder);
88 try {
89 $res = imap_mail_move($this->mailbox->connection(), strval($this->number), $target, CP_UID);
90 $this->mailbox->setExpunge();
91 } catch (\ErrorException $e) {
92 $res = false;
93 }
94 $error_message = $this->mailbox->logImapError('imap_mail_move');
95 if (!$res) {
96 throw new MailboxException(
97 'Failed to move a message',
98 -1,
99 new \ErrorException($error_message ?? 'Unknown')
100 );
101 }
102 }
103
104 public function delete(): void
105 {
106 $this->mailbox->connect();
107 $err = null;
108 try {
109 imap_delete($this->mailbox->connection(), strval($this->number), FT_UID);
110 $this->mailbox->setExpunge();
111 } catch (\ErrorException $e) {
112 $err = $e;
113 }
114 if (($error_message = $this->mailbox->logImapError('imap_delete'))) {
115 $err = new MailboxException('IMAP: Cannot delete a message', -1, $err);
116 }
117 if ($err) {
118 throw $err;
119 }
120 }
121
122 protected function attachmentCount(): int
123 {
124 $this->scanAttachments();
125 return $this->attachments_cnt;
126 }
127
128 protected function ensureAttachment(): void
129 {
130 $this->scanAttachments();
131 }
132
133 private function scanAttachments(): void
134 {
135 if ($this->attachments_cnt === -1) {
136 $structure = imap_fetchstructure($this->mailbox->connection(), $this->number, FT_UID);
137 if ($structure === false) {
138 throw new MailboxException('FetchStructure failed: ' . imap_last_error());
139 }
140 $this->attachments_cnt = 0;
141 $parts = isset($structure->parts) ? $structure->parts : [ $structure ];
142
143 $allParts = [];
144 foreach ($parts as $index => &$part) {
145 $msgIndex = $index + 1;
146 // when it's an entire attached message: MESSAGE/RFC822
147 if (isset($part->parts) && count($part->parts) > 0) {
148 foreach ($part->parts as $subIndex => &$subPart) {
149 $allParts[$msgIndex . '.' . ($subIndex + 1)] = $subPart;
150 }
151 unset($subPart);// Remove the last dangling reference
152 continue;
153 }
154 $allParts[$msgIndex] = $part;
155 }
156 unset($part);// Remove the last dangling reference
157
158 foreach ($allParts as $parNbr => &$part) {
159 $params = $this->scanAttachmentPart($part, $parNbr);
160 if ($params) {
161 ++$this->attachments_cnt;
162 if (!$this->attachment) {
163 $params['mailbox'] = $this->mailbox;
164 $this->attachment = new MailAttachment($params);
165 }
166 }
167 }
168 unset($part);// Remove the last dangling reference
169 }
170 }
171
172 /**
173 * @param string $parNbr
174 * @return array|null
175 */
176 private function scanAttachmentPart(&$part, $parNbr)
177 {
178 $filename = null;
179 if ($part->ifdparameters) {
180 $filename = $this->getAttribute($part->dparameters, 'filename');
181 }
182
183 if (empty($filename) && $part->ifparameters) {
184 $filename = $this->getAttribute($part->parameters, 'name');
185 }
186
187 if (empty($filename)) {
188 return null;
189 }
190
191 return [
192 'filename' => imap_utf8($filename),
193 'bytes' => isset($part->bytes) ? $part->bytes : -1,
194 'number' => $parNbr,
195 'mnumber' => $this->number,
196 'encoding' => $part->encoding
197 ];
198 }
199
200 private function getAttribute(&$params, $name)
201 {
202 // need to check all objects as imap_fetchstructure
203 // returns multiple objects with the same attribute name,
204 // but first entry contains a truncated value
205 $value = null;
206 foreach ($params as &$obj) {
207 if (strcasecmp($obj->attribute, $name) === 0) {
208 $value = $obj->value;
209 }
210 }
211 return $value;
212 }
213 }