-
-
Notifications
You must be signed in to change notification settings - Fork 254
/
Mailbox.php
254 lines (212 loc) · 7.63 KB
/
Mailbox.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
declare(strict_types=1);
namespace Ddeboer\Imap;
use Ddeboer\Imap\Exception\ImapNumMsgException;
use Ddeboer\Imap\Exception\ImapStatusException;
use Ddeboer\Imap\Exception\InvalidSearchCriteriaException;
use Ddeboer\Imap\Exception\MessageCopyException;
use Ddeboer\Imap\Exception\MessageMoveException;
use Ddeboer\Imap\Exception\RenameMailboxException;
use Ddeboer\Imap\Search\ConditionInterface;
use Ddeboer\Imap\Search\LogicalOperator\All;
/**
* An IMAP mailbox (commonly referred to as a 'folder').
*/
final class Mailbox implements MailboxInterface
{
private ImapResourceInterface $resource;
private string $name;
private \stdClass $info;
/**
* Constructor.
*
* @param ImapResourceInterface $resource IMAP resource
* @param string $name Mailbox decoded name
* @param \stdClass $info Mailbox info
*/
public function __construct(ImapResourceInterface $resource, string $name, \stdClass $info)
{
$this->resource = new ImapResource($resource->getStream(), $this);
$this->name = $name;
$this->info = $info;
}
public function getName(): string
{
return $this->name;
}
public function renameTo(string $name): bool
{
$encodedName = \mb_convert_encoding($name, 'UTF7-IMAP', 'UTF-8');
$oldFullName = $this->getFullEncodedName();
$newFullName = \preg_replace('/' . \preg_quote(\mb_convert_encoding($this->name, 'UTF7-IMAP', 'UTF-8')) . '$/', $encodedName, $oldFullName);
\assert(null !== $newFullName);
$return = \imap_renamemailbox($this->resource->getStream(), $oldFullName, $newFullName);
if (false === $return) {
throw new RenameMailboxException('Could not rename mailbox');
}
$this->name = $name;
$this->info->name = $newFullName;
return true;
}
public function getEncodedName(): string
{
/** @var string $name */
$name = $this->info->name;
return (string) \preg_replace('/^{.+}/', '', $name);
}
public function getFullEncodedName(): string
{
return $this->info->name;
}
public function getAttributes(): int
{
return $this->info->attributes;
}
public function getDelimiter(): string
{
return $this->info->delimiter;
}
#[\ReturnTypeWillChange]
public function count()
{
$return = \imap_num_msg($this->resource->getStream());
if (false === $return) {
throw new ImapNumMsgException('imap_num_msg failed');
}
return $return;
}
public function getStatus(?int $flags = null): \stdClass
{
$return = \imap_status($this->resource->getStream(), $this->getFullEncodedName(), $flags ?? \SA_ALL);
if (false === $return) {
throw new ImapStatusException('imap_status failed');
}
return $return;
}
public function setFlag(string $flag, $numbers): bool
{
return \imap_setflag_full($this->resource->getStream(), $this->prepareMessageIds($numbers), $flag, \ST_UID);
}
public function clearFlag(string $flag, $numbers): bool
{
return \imap_clearflag_full($this->resource->getStream(), $this->prepareMessageIds($numbers), $flag, \ST_UID);
}
public function getMessages(?ConditionInterface $search = null, ?int $sortCriteria = null, bool $descending = false, ?string $charset = null): MessageIteratorInterface
{
if (null === $search) {
$search = new All();
}
$query = $search->toString();
// We need to clear the stack to know whether imap_last_error()
// is related to this imap_search
\imap_errors();
if (null !== $sortCriteria) {
$params = [
$this->resource->getStream(),
$sortCriteria,
$descending,
\SE_UID,
$query,
];
if (null !== $charset) {
$params[] = $charset;
}
$messageNumbers = \imap_sort(...$params);
} else {
$params = [
$this->resource->getStream(),
$query,
\SE_UID,
];
if (null !== $charset) {
$params[] = $charset;
}
$messageNumbers = \imap_search(...$params);
}
if (false !== \imap_last_error()) {
// this way all errors occurred during search will be reported
throw new InvalidSearchCriteriaException(
\sprintf('Invalid search criteria [%s]', $query)
);
}
if (false === $messageNumbers) {
// imap_search can also return false
$messageNumbers = [];
}
return new MessageIterator($this->resource, $messageNumbers);
}
public function getMessageSequence(string $sequence): MessageIteratorInterface
{
\imap_errors();
$overview = \imap_fetch_overview($this->resource->getStream(), $sequence, \FT_UID);
if (false !== \imap_last_error()) {
throw new InvalidSearchCriteriaException(
\sprintf('Invalid sequence [%s]', $sequence)
);
}
if (\is_array($overview) && [] !== $overview) {
$messageNumbers = \array_column($overview, 'uid');
} else {
$messageNumbers = [];
}
return new MessageIterator($this->resource, $messageNumbers);
}
public function getMessage(int $number): MessageInterface
{
return new Message($this->resource, $number);
}
public function getIterator(): MessageIteratorInterface
{
return $this->getMessages();
}
public function addMessage(string $message, ?string $options = null, ?\DateTimeInterface $internalDate = null): bool
{
$arguments = [
$this->resource->getStream(),
$this->getFullEncodedName(),
$message,
$options ?? '',
];
if (null !== $internalDate) {
$arguments[] = $internalDate->format('d-M-Y H:i:s O');
}
return \imap_append(...$arguments);
}
public function getThread(): array
{
\set_error_handler(static function (): bool {
return true;
});
/** @var array<string, int>|false $tree */
$tree = \imap_thread($this->resource->getStream(), \SE_UID);
\restore_error_handler();
return false !== $tree ? $tree : [];
}
public function move($numbers, MailboxInterface $mailbox): void
{
if (!\imap_mail_copy($this->resource->getStream(), $this->prepareMessageIds($numbers), $mailbox->getEncodedName(), \CP_UID | \CP_MOVE)) {
throw new MessageMoveException(\sprintf('Messages cannot be moved to "%s"', $mailbox->getName()));
}
}
public function copy($numbers, MailboxInterface $mailbox): void
{
if (!\imap_mail_copy($this->resource->getStream(), $this->prepareMessageIds($numbers), $mailbox->getEncodedName(), \CP_UID)) {
throw new MessageCopyException(\sprintf('Messages cannot be copied to "%s"', $mailbox->getName()));
}
}
/**
* Prepare message ids for the use with bulk functions.
*
* @param array<int, int|string>|MessageIterator|string $messageIds Message numbers
*/
private function prepareMessageIds($messageIds): string
{
if ($messageIds instanceof MessageIterator) {
$messageIds = $messageIds->getArrayCopy();
}
if (\is_array($messageIds)) {
$messageIds = \implode(',', $messageIds);
}
return $messageIds;
}
}