forked from Webklex/laravel-imap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuery.php
418 lines (353 loc) · 9.39 KB
/
Query.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
<?php
/*
* File: Query.php
* Category: -
* Author: M. Goldenbaum
* Created: 21.07.18 18:54
* Updated: -
*
* Description:
* -
*/
namespace Webklex\IMAP\Query;
use Carbon\Carbon;
use Webklex\IMAP\Client;
use Webklex\IMAP\Exceptions\GetMessagesFailedException;
use Webklex\IMAP\Exceptions\MessageSearchValidationException;
use Webklex\IMAP\Message;
use Webklex\IMAP\Support\MessageCollection;
/**
* Class Query
*
* @package Webklex\IMAP\Query
*/
class Query {
/** @var array $query */
protected $query;
/** @var string $raw_query */
protected $raw_query;
/** @var string $charset */
protected $charset;
/** @var Client $client */
protected $client;
/** @var int $limit */
protected $limit = null;
/** @var int $page */
protected $page = 1;
/** @var int $fetch_options */
protected $fetch_options = null;
/** @var int $fetch_body */
protected $fetch_body = true;
/** @var int $fetch_attachment */
protected $fetch_attachment = true;
/** @var int $fetch_flags */
protected $fetch_flags = true;
/**
* Query constructor.
* @param Client $client
* @param string $charset
*/
public function __construct(Client $client, $charset = 'UTF-8') {
$this->setClient($client);
if(config('imap.options.fetch') === FT_PEEK) $this->leaveUnread();
$this->charset = $charset;
$this->query = collect();
$this->boot();
}
/**
* Instance boot method for additional functionality
*/
protected function boot(){}
/**
* Parse a given value
* @param mixed $value
*
* @return string
*/
protected function parse_value($value){
switch(true){
case $value instanceof \Carbon\Carbon:
$value = $value->format('d M y');
break;
}
return (string) $value;
}
/**
* Check if a given date is a valid carbon object and if not try to convert it
* @param $date
*
* @return Carbon
* @throws MessageSearchValidationException
*/
protected function parse_date($date) {
if($date instanceof \Carbon\Carbon) return $date;
try {
$date = Carbon::parse($date);
} catch (\Exception $e) {
throw new MessageSearchValidationException();
}
return $date;
}
/**
* Don't mark messages as read when fetching
*
* @return $this
*/
public function leaveUnread() {
$this->setFetchOptions(FT_PEEK);
return $this;
}
/**
* Mark all messages as read when fetching
*
* @return $this
*/
public function markAsRead() {
$this->setFetchOptions(FT_UID);
return $this;
}
/**
* Fetch the current query and return all found messages
*
* @return MessageCollection
* @throws GetMessagesFailedException
*/
public function get() {
$messages = MessageCollection::make([]);
try {
$this->generate_query();
/**
* Don't set the charset if it isn't used - prevent strange outlook mail server errors
* @see https://github.com/Webklex/laravel-imap/issues/100
*/
if($this->getCharset() === null){
$available_messages = imap_search($this->getClient()->getConnection(), $this->getRawQuery(), SE_UID);
}else{
$available_messages = imap_search($this->getClient()->getConnection(), $this->getRawQuery(), SE_UID, $this->getCharset());
}
if ($available_messages !== false) {
$available_messages = collect($available_messages);
$options = config('imap.options');
if(strtolower($options['fetch_order']) === 'desc'){
$available_messages = $available_messages->reverse();
}
$available_messages->forPage($this->page, $this->limit)->each(function($msgno, $msglist) use(&$messages, $options) {
$oMessage = new Message($msgno, $msglist, $this->getClient(), $this->getFetchOptions(), $this->getFetchBody(), $this->getFetchAttachment(), $this->getFetchFlags());
switch ($options['message_key']){
case 'number':
$message_key = $oMessage->getMessageNo();
break;
case 'list':
$message_key = $msglist;
break;
default:
$message_key = $oMessage->getMessageId();
break;
}
$messages->put($message_key, $oMessage);
});
}
return $messages;
} catch (\Exception $e) {
$message = $e->getMessage();
throw new GetMessagesFailedException($message);
}
}
/**
* Get the raw IMAP search query
*
* @return string
*/
public function generate_query() {
$query = '';
$this->query->each(function($statement) use(&$query) {
if (count($statement) == 1) {
$query .= $statement[0];
} else {
if($statement[1] === null){
$query .= $statement[0];
}else{
$query .= $statement[0].' "'.$statement[1].'"';
}
}
$query .= ' ';
});
$this->raw_query = trim($query);
return $this->raw_query;
}
/**
* @return Client
* @throws \Webklex\IMAP\Exceptions\ConnectionFailedException
*/
public function getClient() {
$this->client->checkConnection();
return $this->client;
}
/**
* Set the limit and page for the current query
* @param int $limit
* @param int $page
*
* @return $this
*/
public function limit($limit, $page = 1) {
if($page >= 1) $this->page = $page;
$this->limit = $limit;
return $this;
}
/**
* @return array
*/
public function getQuery() {
return $this->query;
}
/**
* @param array $query
* @return Query
*/
public function setQuery($query) {
$this->query = $query;
return $this;
}
/**
* @return string
*/
public function getRawQuery() {
return $this->raw_query;
}
/**
* @param string $raw_query
* @return Query
*/
public function setRawQuery($raw_query) {
$this->raw_query = $raw_query;
return $this;
}
/**
* @return string
*/
public function getCharset() {
return $this->charset;
}
/**
* @param string $charset
* @return Query
*/
public function setCharset($charset) {
$this->charset = $charset;
return $this;
}
/**
* @param Client $client
* @return Query
*/
public function setClient(Client $client) {
$this->client = $client;
return $this;
}
/**
* @return int
*/
public function getLimit() {
return $this->limit;
}
/**
* @param int $limit
* @return Query
*/
public function setLimit($limit) {
$this->limit = $limit <= 0 ? null : $limit;
return $this;
}
/**
* @return int
*/
public function getPage() {
return $this->page;
}
/**
* @param int $page
* @return Query
*/
public function setPage($page) {
$this->page = $page;
return $this;
}
/**
* @param boolean $fetch_options
* @return Query
*/
public function setFetchOptions($fetch_options) {
$this->fetch_options = $fetch_options;
return $this;
}
/**
* @param boolean $fetch_options
* @return Query
*/
public function fetchOptions($fetch_options) {
return $this->setFetchOptions($fetch_options);
}
/**
* @return int
*/
public function getFetchOptions() {
return $this->fetch_options;
}
/**
* @return boolean
*/
public function getFetchBody() {
return $this->fetch_body;
}
/**
* @param boolean $fetch_body
* @return Query
*/
public function setFetchBody($fetch_body) {
$this->fetch_body = $fetch_body;
return $this;
}
/**
* @param boolean $fetch_body
* @return Query
*/
public function fetchBody($fetch_body) {
return $this->setFetchBody($fetch_body);
}
/**
* @return boolean
*/
public function getFetchAttachment() {
return $this->fetch_attachment;
}
/**
* @param boolean $fetch_attachment
* @return Query
*/
public function setFetchAttachment($fetch_attachment) {
$this->fetch_attachment = $fetch_attachment;
return $this;
}
/**
* @param boolean $fetch_attachment
* @return Query
*/
public function fetchAttachment($fetch_attachment) {
return $this->setFetchAttachment($fetch_attachment);
}
/**
* @return int
*/
public function getFetchFlags() {
return $this->fetch_flags;
}
/**
* @param int $fetch_flags
* @return Query
*/
public function setFetchFlags($fetch_flags) {
$this->fetch_flags = $fetch_flags;
return $this;
}
}