forked from Webklex/laravel-imap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Folder.php
452 lines (407 loc) · 14.6 KB
/
Folder.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?php
/*
* File: Folder.php
* Category: -
* Author: M. Goldenbaum
* Created: 19.01.17 22:21
* Updated: -
*
* Description:
* -
*/
namespace Webklex\IMAP;
use Webklex\IMAP\Exceptions\GetMessagesFailedException;
use Webklex\IMAP\Exceptions\MessageSearchValidationException;
use Webklex\IMAP\Query\WhereQuery;
use Webklex\IMAP\Support\FolderCollection;
use Webklex\IMAP\Support\MessageCollection;
/**
* Class Folder
*
* @package Webklex\IMAP
*/
class Folder {
/**
* Client instance
*
* @var \Webklex\IMAP\Client
*/
protected $client;
/**
* Folder full path
*
* @var string
*/
public $path;
/**
* Folder name
*
* @var string
*/
public $name;
/**
* Folder fullname
*
* @var string
*/
public $fullName;
/**
* Children folders
*
* @var FolderCollection|array
*/
public $children = [];
/**
* Delimiter for folder
*
* @var string
*/
public $delimiter;
/**
* Indicates if folder can't containg any "children".
* CreateFolder won't work on this folder.
*
* @var boolean
*/
public $no_inferiors;
/**
* Indicates if folder is only container, not a mailbox - you can't open it.
*
* @var boolean
*/
public $no_select;
/**
* Indicates if folder is marked. This means that it may contain new messages since the last time it was checked.
* Not provided by all IMAP servers.
*
* @var boolean
*/
public $marked;
/**
* Indicates if folder containg any "children".
* Not provided by all IMAP servers.
*
* @var boolean
*/
public $has_children;
/**
* Indicates if folder refers to other.
* Not provided by all IMAP servers.
*
* @var boolean
*/
public $referal;
/**
* Folder constructor.
*
* @param \Webklex\IMAP\Client $client
*
* @param object $structure
*/
public function __construct(Client $client, $structure) {
$this->client = $client;
$this->setDelimiter($structure->delimiter);
$this->path = $structure->name;
$this->fullName = $this->decodeName($structure->name);
$this->name = $this->getSimpleName($this->delimiter, $this->fullName);
$this->parseAttributes($structure->attributes);
}
/**
* Get a new search query instance
* @param string $charset
*
* @return WhereQuery
* @throws Exceptions\ConnectionFailedException
*/
public function query($charset = 'UTF-8'){
$this->getClient()->checkConnection();
$this->getClient()->openFolder($this);
return new WhereQuery($this->getClient(), $charset);
}
/**
* @inheritdoc self::query($charset = 'UTF-8')
* @throws Exceptions\ConnectionFailedException
*/
public function search($charset = 'UTF-8'){
return $this->query($charset);
}
/**
* @inheritdoc self::query($charset = 'UTF-8')
* @throws Exceptions\ConnectionFailedException
*/
public function messages($charset = 'UTF-8'){
return $this->query($charset);
}
/**
* Determine if folder has children.
*
* @return bool
*/
public function hasChildren() {
return $this->has_children;
}
/**
* Set children.
*
* @param FolderCollection|array $children
*
* @return self
*/
public function setChildren($children = []) {
$this->children = $children;
return $this;
}
/**
* Get a specific message by UID
*
* @param integer $uid Please note that the uid is not unique and can change
* @param integer|null $msglist
* @param integer|null $fetch_options
* @param boolean $fetch_body
* @param boolean $fetch_attachment
* @param boolean $fetch_flags
*
* @return Message|null
*
* @throws Exceptions\ConnectionFailedException
* @throws Exceptions\InvalidWhereQueryCriteriaException
* @throws GetMessagesFailedException
* @throws MessageSearchValidationException
*/
public function getMessage($uid, $msglist = null, $fetch_options = null, $fetch_body = false, $fetch_attachment = false, $fetch_flags = true) {
if (imap_msgno($this->getClient()->getConnection(), $uid) > 0) {
return new Message($uid, $msglist, $this->getClient(), $fetch_options, $fetch_body, $fetch_attachment, $fetch_flags);
}
return null;
}
/**
* Get all messages
*
* @param string $criteria
* @param int|null $fetch_options
* @param boolean $fetch_body
* @param boolean $fetch_attachment
* @param boolean $fetch_flags
* @param int|null $limit
* @param int $page
* @param string $charset
*
* @return MessageCollection
* @throws Exceptions\ConnectionFailedException
* @throws Exceptions\InvalidWhereQueryCriteriaException
* @throws GetMessagesFailedException
*/
public function getMessages($criteria = 'ALL', $fetch_options = null, $fetch_body = true, $fetch_attachment = true, $fetch_flags = true, $limit = null, $page = 1, $charset = "UTF-8") {
return $this->query($charset)->where($criteria)->setFetchOptions($fetch_options)->setFetchBody($fetch_body)
->setFetchAttachment($fetch_attachment)->setFetchFlags($fetch_flags)
->limit($limit, $page)->get();
}
/**
* Get all unseen messages
*
* @param string $criteria
* @param int|null $fetch_options
* @param boolean $fetch_body
* @param boolean $fetch_attachment
* @param boolean $fetch_flags
* @param int|null $limit
* @param int $page
* @param string $charset
*
* @return MessageCollection
* @throws Exceptions\ConnectionFailedException
* @throws Exceptions\InvalidWhereQueryCriteriaException
* @throws GetMessagesFailedException
* @throws MessageSearchValidationException
*
* @deprecated 1.0.5:2.0.0 No longer needed. Use Folder::getMessages('UNSEEN') instead
* @see Folder::getMessages()
*/
public function getUnseenMessages($criteria = 'UNSEEN', $fetch_options = null, $fetch_body = true, $fetch_attachment = true, $fetch_flags = true, $limit = null, $page = 1, $charset = "UTF-8") {
return $this->getMessages($criteria, $fetch_options, $fetch_body, $fetch_attachment, $fetch_flags, $limit, $page, $charset);
}
/**
* Search messages by a given search criteria
*
* @param array $where Is a two dimensional array where each array represents a criteria set:
* ---------------------------------------------------------------------------------------
* The following sample would search for all messages received from [email protected] or
* contain the text "Hello world!":
* [['FROM' => '[email protected]'],[' TEXT' => 'Hello world!']]
* ---------------------------------------------------------------------------------------
* The following sample would search for all messages received since march 15 2018:
* [['SINCE' => Carbon::parse('15.03.2018')]]
* ---------------------------------------------------------------------------------------
* The following sample would search for all flagged messages:
* [['FLAGGED']]
* ---------------------------------------------------------------------------------------
* @param int|null $fetch_options
* @param boolean $fetch_body
* @param boolean $fetch_attachment
* @param boolean $fetch_flags
* @param int|null $limit
* @param int $page
* @param string $charset
*
* @return MessageCollection
*
* @throws Exceptions\ConnectionFailedException
* @throws Exceptions\InvalidWhereQueryCriteriaException
* @throws GetMessagesFailedException
*
* @doc http://php.net/manual/en/function.imap-search.php
* imap_search() only supports IMAP2 search criterias, because the function mail_criteria() (from c-client lib)
* is used in ext/imap/php_imap.c for parsing the search string.
* IMAP2 search criteria is defined in RFC 1176, section "tag SEARCH search_criteria".
*
* https://tools.ietf.org/html/rfc1176 - INTERACTIVE MAIL ACCESS PROTOCOL - VERSION 2
* https://tools.ietf.org/html/rfc1064 - INTERACTIVE MAIL ACCESS PROTOCOL - VERSION 2
* https://tools.ietf.org/html/rfc822 - STANDARD FOR THE FORMAT OF ARPA INTERNET TEXT MESSAGES
* Date and time example from RFC822:
* date-time = [ day "," ] date time ; dd mm yy
* ; hh:mm:ss zzz
*
* day = "Mon" / "Tue" / "Wed" / "Thu" / "Fri" / "Sat" / "Sun"
*
* date = 1*2DIGIT month 2DIGIT ; day month year
* ; e.g. 20 Jun 82
*
* month = "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" / "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"
*
* time = hour zone ; ANSI and Military
*
* hour = 2DIGIT ":" 2DIGIT [":" 2DIGIT] ; 00:00:00 - 23:59:59
*
* zone = "UT" / "GMT" ; Universal Time
* ; North American : UT
* = "EST" / "EDT" ; Eastern: - 5/ - 4
* = "CST" / "CDT" ; Central: - 6/ - 5
* = "MST" / "MDT" ; Mountain: - 7/ - 6
* = "PST" / "PDT" ; Pacific: - 8/ - 7
* = 1ALPHA ; Military: Z = UT;
* ; A:-1; (J not used)
* ; M:-12; N:+1; Y:+12
* / ( ("+" / "-") 4DIGIT ) ; Local differential
* ; hours+min. (HHMM)
*
* @deprecated 1.2.1:2.0.0 No longer needed. Use Folder::query() instead
* @see Folder::query()
*/
public function searchMessages(array $where, $fetch_options = null, $fetch_body = true, $fetch_attachment = true, $fetch_flags = true, $limit = null, $page = 1, $charset = "UTF-8") {
$this->getClient()->checkConnection();
return $this->query($charset)->where($where)->setFetchOptions($fetch_options)->setFetchBody($fetch_body)
->setFetchAttachment($fetch_attachment)->setFetchFlags($fetch_flags)
->limit($limit, $page)->get();
}
/**
* Decode name.
* It converts UTF7-IMAP encoding to UTF-8.
*
* @param $name
*
* @return mixed|string
*/
protected function decodeName($name) {
preg_match('#\{(.*)\}(.*)#', $name, $preg);
return mb_convert_encoding($preg[2], "UTF-8", "UTF7-IMAP");
}
/**
* Get simple name (without parent folders).
*
* @param $delimiter
* @param $fullName
*
* @return mixed
*/
protected function getSimpleName($delimiter, $fullName) {
$arr = explode($delimiter, $fullName);
return end($arr);
}
/**
* Parse attributes and set it to object properties.
*
* @param $attributes
*/
protected function parseAttributes($attributes) {
$this->no_inferiors = ($attributes & LATT_NOINFERIORS) ? true : false;
$this->no_select = ($attributes & LATT_NOSELECT) ? true : false;
$this->marked = ($attributes & LATT_MARKED) ? true : false;
$this->referal = ($attributes & LATT_REFERRAL) ? true : false;
$this->has_children = ($attributes & LATT_HASCHILDREN) ? true : false;
}
/**
* Delete the current Mailbox
* @param boolean $expunge
*
* @return bool
*
* @throws Exceptions\ConnectionFailedException
*/
public function delete($expunge = true) {
$status = imap_deletemailbox($this->client->getConnection(), $this->path);
if($expunge) $this->client->expunge();
return $status;
}
/**
* Move or Rename the current Mailbox
*
* @param string $target_mailbox
* @param boolean $expunge
*
* @return bool
*
* @throws Exceptions\ConnectionFailedException
*/
public function move($target_mailbox, $expunge = true) {
$status = imap_renamemailbox($this->client->getConnection(), $this->path, $target_mailbox);
if($expunge) $this->client->expunge();
return $status;
}
/**
* Returns status information on a mailbox
*
* @param integer $options
* SA_MESSAGES - set $status->messages to the number of messages in the mailbox
* SA_RECENT - set $status->recent to the number of recent messages in the mailbox
* SA_UNSEEN - set $status->unseen to the number of unseen (new) messages in the mailbox
* SA_UIDNEXT - set $status->uidnext to the next uid to be used in the mailbox
* SA_UIDVALIDITY - set $status->uidvalidity to a constant that changes when uids for the mailbox may no longer be valid
* SA_ALL - set all of the above
*
* @return object
* @throws Exceptions\ConnectionFailedException
*/
public function getStatus($options) {
return imap_status($this->client->getConnection(), $this->path, $options);
}
/**
* Append a string message to the current mailbox
*
* @param string $message
* @param string $options
* @param string $internal_date
*
* @return bool
* @throws Exceptions\ConnectionFailedException
*/
public function appendMessage($message, $options = null, $internal_date = null) {
return imap_append($this->client->getConnection(), $this->path, $message, $options, $internal_date);
}
/**
* Get the current Client instance
*
* @return Client
*/
public function getClient() {
return $this->client;
}
/**
* @param $delimiter
*/
public function setDelimiter($delimiter){
if(in_array($delimiter, [null, '', ' ', false]) === true) {
$delimiter = config('imap.options.delimiter', '/');
}
$this->delimiter = $delimiter;
}
}