-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathClient.php
executable file
·969 lines (869 loc) · 27.5 KB
/
Client.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
<?php
/*
* File: Client.php
* Category: -
* Author: M. Goldenbaum
* Created: 19.01.17 22:21
* Updated: -
*
* Description:
* -
*/
namespace Webklex\PHPIMAP;
use ErrorException;
use Webklex\PHPIMAP\Connection\Protocols\ImapProtocol;
use Webklex\PHPIMAP\Connection\Protocols\LegacyProtocol;
use Webklex\PHPIMAP\Connection\Protocols\ProtocolInterface;
use Webklex\PHPIMAP\Exceptions\AuthFailedException;
use Webklex\PHPIMAP\Exceptions\ConnectionFailedException;
use Webklex\PHPIMAP\Exceptions\EventNotFoundException;
use Webklex\PHPIMAP\Exceptions\FolderFetchingException;
use Webklex\PHPIMAP\Exceptions\ImapBadRequestException;
use Webklex\PHPIMAP\Exceptions\ImapServerErrorException;
use Webklex\PHPIMAP\Exceptions\MaskNotFoundException;
use Webklex\PHPIMAP\Exceptions\ProtocolNotSupportedException;
use Webklex\PHPIMAP\Exceptions\ResponseException;
use Webklex\PHPIMAP\Exceptions\RuntimeException;
use Webklex\PHPIMAP\Support\FolderCollection;
use Webklex\PHPIMAP\Support\Masks\AttachmentMask;
use Webklex\PHPIMAP\Support\Masks\MessageMask;
use Webklex\PHPIMAP\Traits\HasEvents;
/**
* Class Client
*
* @package Webklex\PHPIMAP
*/
class Client {
use HasEvents;
/**
* Connection resource
*
* @var ?ProtocolInterface
*/
public ?ProtocolInterface $connection = null;
/**
* Client configuration
*
* @var Config
*/
protected Config $config;
/**
* Server hostname.
*
* @var string
*/
public string $host;
/**
* Server port.
*
* @var int
*/
public int $port;
/**
* Service protocol.
*
* @var string
*/
public string $protocol;
/**
* Server encryption.
* Supported: none, ssl, tls, starttls or notls.
*
* @var string
*/
public string $encryption;
/**
* If server has to validate cert.
*
* @var bool
*/
public bool $validate_cert = true;
/**
* Proxy settings
* @var array
*/
protected array $proxy = [
'socket' => null,
'request_fulluri' => false,
'username' => null,
'password' => null,
];
/**
* SSL stream context options
*
* @see https://www.php.net/manual/en/context.ssl.php for possible options
*
* @var array
*/
protected array $ssl_options = [];
/**
* Connection timeout
* @var int $timeout
*/
public int $timeout;
/**
* Account username
*
* @var string
*/
public string $username;
/**
* Account password.
*
* @var string
*/
public string $password;
/**
* Additional data fetched from the server.
*
* @var array
*/
public array $extensions;
/**
* Account rfc.
*
* @var string
*/
public string $rfc;
/**
* Account authentication method.
*
* @var ?string
*/
public ?string $authentication;
/**
* Active folder path.
*
* @var ?string
*/
protected ?string $active_folder = null;
/**
* Default message mask
*
* @var string $default_message_mask
*/
protected string $default_message_mask = MessageMask::class;
/**
* Default attachment mask
*
* @var string $default_attachment_mask
*/
protected string $default_attachment_mask = AttachmentMask::class;
/**
* Used default account values
*
* @var array $default_account_config
*/
protected array $default_account_config = [
'host' => 'localhost',
'port' => 993,
'protocol' => 'imap',
'encryption' => 'ssl',
'validate_cert' => true,
'username' => '',
'password' => '',
'rfc' => 'RFC822',
'authentication' => null,
"extensions" => [],
'proxy' => [
'socket' => null,
'request_fulluri' => false,
'username' => null,
'password' => null,
],
'ssl_options' => [],
"timeout" => 30,
];
/**
* Client constructor.
* @param Config $config
*
* @throws MaskNotFoundException
*/
public function __construct(Config $config) {
$this->setConfig($config);
$this->setMaskFromConfig();
$this->setEventsFromConfig();
}
/**
* Client destructor
*
* @throws ImapBadRequestException
* @throws ImapServerErrorException
* @throws RuntimeException
*/
public function __destruct() {
$this->disconnect();
}
/**
* Clone the current Client instance
*
* @return Client
* @throws MaskNotFoundException
*/
public function clone(): Client {
$client = new self($this->config);
$client->events = $this->events;
$client->timeout = $this->timeout;
$client->active_folder = $this->active_folder;
$client->default_account_config = $this->default_account_config;
$config = $this->getAccountConfig();
foreach($config as $key => $value) {
$client->setAccountConfig($key, $config);
}
$client->default_message_mask = $this->default_message_mask;
$client->default_attachment_mask = $this->default_message_mask;
return $client;
}
/**
* Set the Client configuration
* @param Config $config
*
* @return self
*/
public function setConfig(Config $config): Client {
$this->config = $config;
$default_account = $this->config->get('default');
$default_config = $this->config->get("accounts.$default_account");
foreach ($this->default_account_config as $key => $value) {
$this->setAccountConfig($key, $default_config);
}
return $this;
}
/**
* Get the current config
*
* @return Config
*/
public function getConfig(): Config {
return $this->config;
}
/**
* Set a specific account config
* @param string $key
* @param array $default_config
*/
private function setAccountConfig(string $key, array $default_config): void {
$value = $this->default_account_config[$key];
if(isset($default_config[$key])) {
$value = $default_config[$key];
}
$this->$key = $value;
}
/**
* Get the current account config
*
* @return array
*/
public function getAccountConfig(): array {
$config = [];
foreach($this->default_account_config as $key => $value) {
if(property_exists($this, $key)) {
$config[$key] = $this->$key;
}
}
return $config;
}
/**
* Look for a possible events in any available config
*/
protected function setEventsFromConfig(): void {
$this->events = $this->config->get("events");
if(isset($config['events'])){
foreach($config['events'] as $section => $events) {
$this->events[$section] = array_merge($this->events[$section], $events);
}
}
}
/**
* Look for a possible mask in any available config
*
* @throws MaskNotFoundException
*/
protected function setMaskFromConfig(): void {
$masks = $this->config->get("masks");
if(isset($masks)){
if(isset($masks['message'])) {
if(class_exists($masks['message'])) {
$this->default_message_mask = $masks['message'];
}else{
throw new MaskNotFoundException("Unknown mask provided: ".$masks['message']);
}
}else{
$default_mask = $this->config->getMask("message");
if($default_mask != ""){
$this->default_message_mask = $default_mask;
}else{
throw new MaskNotFoundException("Unknown message mask provided");
}
}
if(isset($masks['attachment'])) {
if(class_exists($masks['attachment'])) {
$this->default_attachment_mask = $masks['attachment'];
}else{
throw new MaskNotFoundException("Unknown mask provided: ". $masks['attachment']);
}
}else{
$default_mask = $this->config->getMask("attachment");
if($default_mask != ""){
$this->default_attachment_mask = $default_mask;
}else{
throw new MaskNotFoundException("Unknown attachment mask provided");
}
}
}else{
$default_mask = $this->config->getMask("message");
if($default_mask != ""){
$this->default_message_mask = $default_mask;
}else{
throw new MaskNotFoundException("Unknown message mask provided");
}
$default_mask = $this->config->getMask("attachment");
if($default_mask != ""){
$this->default_attachment_mask = $default_mask;
}else{
throw new MaskNotFoundException("Unknown attachment mask provided");
}
}
}
/**
* Get the current imap resource
*
* @return ProtocolInterface
* @throws ConnectionFailedException
* @throws AuthFailedException
* @throws ImapBadRequestException
* @throws ImapServerErrorException
* @throws RuntimeException
* @throws ResponseException
*/
public function getConnection(): ProtocolInterface {
$this->checkConnection();
return $this->connection;
}
/**
* Determine if connection was established.
*
* @return bool
*/
public function isConnected(): bool {
return $this->connection && $this->connection->connected();
}
/**
* Determine if connection was established and connect if not.
* Returns true if the connection was closed and has been reopened.
*
* @throws ConnectionFailedException
* @throws AuthFailedException
* @throws ImapBadRequestException
* @throws ImapServerErrorException
* @throws RuntimeException
* @throws ResponseException
*/
public function checkConnection(): bool {
try {
if (!$this->isConnected()) {
$this->connect();
return true;
}
} catch (\Throwable) {
$this->connect();
}
return false;
}
/**
* Force the connection to reconnect
*
* @throws ConnectionFailedException
* @throws AuthFailedException
* @throws ImapBadRequestException
* @throws ImapServerErrorException
* @throws RuntimeException
* @throws ResponseException
*/
public function reconnect(): void {
if ($this->isConnected()) {
$this->disconnect();
}
$this->connect();
}
/**
* Connect to server.
*
* @return $this
* @throws ConnectionFailedException
* @throws AuthFailedException
* @throws ImapBadRequestException
* @throws ImapServerErrorException
* @throws RuntimeException
* @throws ResponseException
*/
public function connect(): Client {
$this->disconnect();
$protocol = strtolower($this->protocol);
if (in_array($protocol, ['imap', 'imap4', 'imap4rev1'])) {
$this->connection = new ImapProtocol($this->config, $this->validate_cert, $this->encryption);
$this->connection->setConnectionTimeout($this->timeout);
$this->connection->setProxy($this->proxy);
$this->connection->setSslOptions($this->ssl_options);
}else{
if (extension_loaded('imap') === false) {
throw new ConnectionFailedException("connection setup failed", 0, new ProtocolNotSupportedException($protocol." is an unsupported protocol"));
}
$this->connection = new LegacyProtocol($this->config, $this->validate_cert, $this->encryption);
if (str_starts_with($protocol, "legacy-")) {
$protocol = substr($protocol, 7);
}
$this->connection->setProtocol($protocol);
}
if ($this->config->get('options.debug')) {
$this->connection->enableDebug();
}
if (!$this->config->get('options.uid_cache')) {
$this->connection->disableUidCache();
}
try {
$this->connection->connect($this->host, $this->port);
} catch (ErrorException|RuntimeException $e) {
throw new ConnectionFailedException("connection setup failed", 0, $e);
}
$this->authenticate();
return $this;
}
/**
* Authenticate the current session
*
* @throws AuthFailedException
* @throws ImapBadRequestException
* @throws ImapServerErrorException
* @throws ResponseException
*/
protected function authenticate(): void {
if ($this->authentication == "oauth") {
if (!$this->connection->authenticate($this->username, $this->password)->validatedData()) {
throw new AuthFailedException();
}
} elseif (!$this->connection->login($this->username, $this->password)->validatedData()) {
throw new AuthFailedException();
}
}
/**
* Disconnect from server.
*
* @return $this
* @throws ImapBadRequestException
* @throws ImapServerErrorException
* @throws RuntimeException
*/
public function disconnect(): Client {
if ($this->isConnected()) {
$this->connection->logout();
}
$this->active_folder = null;
return $this;
}
/**
* Get a folder instance by a folder name
* @param string $folder_name
* @param string|null $delimiter
* @param bool $utf7
* @return Folder|null
* @throws AuthFailedException
* @throws ConnectionFailedException
* @throws FolderFetchingException
* @throws ImapBadRequestException
* @throws ImapServerErrorException
* @throws ResponseException
* @throws RuntimeException
*/
public function getFolder(string $folder_name, ?string $delimiter = null, bool $utf7 = false): ?Folder {
// Set delimiter to false to force selection via getFolderByName (maybe useful for uncommon folder names)
$delimiter = is_null($delimiter) ? $this->config->get('options.delimiter', "/") : $delimiter;
if (str_contains($folder_name, (string)$delimiter)) {
return $this->getFolderByPath($folder_name, $utf7);
}
return $this->getFolderByName($folder_name);
}
/**
* Get a folder instance by a folder name
* @param $folder_name
* @param bool $soft_fail If true, it will return null instead of throwing an exception
*
* @return Folder|null
* @throws FolderFetchingException
* @throws ConnectionFailedException
* @throws AuthFailedException
* @throws ImapBadRequestException
* @throws ImapServerErrorException
* @throws RuntimeException
* @throws ResponseException
*/
public function getFolderByName($folder_name, bool $soft_fail = false): ?Folder {
return $this->getFolders(false, null, $soft_fail)->where("name", $folder_name)->first();
}
/**
* Get a folder instance by a folder path
* @param $folder_path
* @param bool $utf7
* @param bool $soft_fail If true, it will return null instead of throwing an exception
*
* @return Folder|null
* @throws AuthFailedException
* @throws ConnectionFailedException
* @throws FolderFetchingException
* @throws ImapBadRequestException
* @throws ImapServerErrorException
* @throws ResponseException
* @throws RuntimeException
*/
public function getFolderByPath($folder_path, bool $utf7 = false, bool $soft_fail = false): ?Folder {
if (!$utf7) $folder_path = EncodingAliases::convert($folder_path, "utf-8", "utf7-imap");
return $this->getFolders(false, null, $soft_fail)->where("path", $folder_path)->first();
}
/**
* Get folders list.
* If hierarchical order is set to true, it will make a tree of folders, otherwise it will return flat array.
*
* @param boolean $hierarchical
* @param string|null $parent_folder
* @param bool $soft_fail If true, it will return an empty collection instead of throwing an exception
*
* @return FolderCollection
* @throws AuthFailedException
* @throws ConnectionFailedException
* @throws FolderFetchingException
* @throws ImapBadRequestException
* @throws ImapServerErrorException
* @throws ResponseException
* @throws RuntimeException
*/
public function getFolders(bool $hierarchical = true, ?string $parent_folder = null, bool $soft_fail = false): FolderCollection {
$this->checkConnection();
$folders = FolderCollection::make([]);
$pattern = $parent_folder.($hierarchical ? '%' : '*');
$items = $this->connection->folders('', $pattern)->validatedData();
if(!empty($items)){
foreach ($items as $folder_name => $item) {
$folder = new Folder($this, $folder_name, $item["delimiter"], $item["flags"]);
if ($hierarchical && $folder->hasChildren()) {
$pattern = $folder->full_name.$folder->delimiter.'%';
$children = $this->getFolders(true, $pattern, true);
$folder->setChildren($children);
}
$folders->push($folder);
}
return $folders;
}else if (!$soft_fail){
throw new FolderFetchingException("failed to fetch any folders");
}
return $folders;
}
/**
* Get folders list.
* If hierarchical order is set to true, it will make a tree of folders, otherwise it will return flat array.
*
* @param boolean $hierarchical
* @param string|null $parent_folder
* @param bool $soft_fail If true, it will return an empty collection instead of throwing an exception
*
* @return FolderCollection
* @throws FolderFetchingException
* @throws ConnectionFailedException
* @throws AuthFailedException
* @throws ImapBadRequestException
* @throws ImapServerErrorException
* @throws RuntimeException
* @throws ResponseException
*/
public function getFoldersWithStatus(bool $hierarchical = true, ?string $parent_folder = null, bool $soft_fail = false): FolderCollection {
$this->checkConnection();
$folders = FolderCollection::make([]);
$pattern = $parent_folder.($hierarchical ? '%' : '*');
$items = $this->connection->folders('', $pattern)->validatedData();
if(!empty($items)){
foreach ($items as $folder_name => $item) {
$folder = new Folder($this, $folder_name, $item["delimiter"], $item["flags"]);
if ($hierarchical && $folder->hasChildren()) {
$pattern = $folder->full_name.$folder->delimiter.'%';
$children = $this->getFoldersWithStatus(true, $pattern, true);
$folder->setChildren($children);
}
$folder->loadStatus();
$folders->push($folder);
}
return $folders;
}else if (!$soft_fail){
throw new FolderFetchingException("failed to fetch any folders");
}
return $folders;
}
/**
* Open a given folder.
* @param string $folder_path
* @param boolean $force_select
*
* @return array
* @throws ConnectionFailedException
* @throws AuthFailedException
* @throws ImapBadRequestException
* @throws ImapServerErrorException
* @throws RuntimeException
* @throws ResponseException
*/
public function openFolder(string $folder_path, bool $force_select = false): array {
if ($this->active_folder == $folder_path && $this->isConnected() && $force_select === false) {
return [];
}
$this->checkConnection();
$this->active_folder = $folder_path;
return $this->connection->selectFolder($folder_path)->validatedData();
}
/**
* Set active folder
* @param string|null $folder_path
*
* @return void
*/
public function setActiveFolder(?string $folder_path = null): void {
$this->active_folder = $folder_path;
}
/**
* Get active folder
*
* @return string|null
*/
public function getActiveFolder(): ?string {
return $this->active_folder;
}
/**
* Create a new Folder
* @param string $folder_path
* @param boolean $expunge
* @param bool $utf7
* @return Folder
* @throws AuthFailedException
* @throws ConnectionFailedException
* @throws EventNotFoundException
* @throws FolderFetchingException
* @throws ImapBadRequestException
* @throws ImapServerErrorException
* @throws ResponseException
* @throws RuntimeException
*/
public function createFolder(string $folder_path, bool $expunge = true, bool $utf7 = false): Folder {
$this->checkConnection();
if (!$utf7) $folder_path = EncodingAliases::convert($folder_path, "utf-8", "UTF7-IMAP");
$status = $this->connection->createFolder($folder_path)->validatedData();
if($expunge) $this->expunge();
$folder = $this->getFolderByPath($folder_path, true);
if($status && $folder) {
$this->dispatch("folder", "new", $folder);
}
return $folder;
}
/**
* Delete a given folder
* @param string $folder_path
* @param boolean $expunge
*
* @return array
* @throws AuthFailedException
* @throws ConnectionFailedException
* @throws EventNotFoundException
* @throws FolderFetchingException
* @throws ImapBadRequestException
* @throws ImapServerErrorException
* @throws RuntimeException
* @throws ResponseException
*/
public function deleteFolder(string $folder_path, bool $expunge = true): array {
$this->checkConnection();
$folder = $this->getFolderByPath($folder_path);
if ($this->active_folder == $folder->path){
$this->active_folder = null;
}
$status = $this->getConnection()->deleteFolder($folder->path)->validatedData();
if ($expunge) $this->expunge();
$this->dispatch("folder", "deleted", $folder);
return $status;
}
/**
* Check a given folder
* @param string $folder_path
*
* @return array
* @throws ConnectionFailedException
* @throws AuthFailedException
* @throws ImapBadRequestException
* @throws ImapServerErrorException
* @throws RuntimeException
* @throws ResponseException
*/
public function checkFolder(string $folder_path): array {
$this->checkConnection();
return $this->connection->examineFolder($folder_path)->validatedData();
}
/**
* Get the current active folder
*
* @return null|string
*/
public function getFolderPath(): ?string {
return $this->active_folder;
}
/**
* Exchange identification information
* Ref.: https://datatracker.ietf.org/doc/html/rfc2971
*
* @param array|null $ids
* @return array
*
* @throws ConnectionFailedException
* @throws AuthFailedException
* @throws ImapBadRequestException
* @throws ImapServerErrorException
* @throws RuntimeException
* @throws ResponseException
*/
public function Id(?array $ids = null): array {
$this->checkConnection();
return $this->connection->ID($ids)->validatedData();
}
/**
* Retrieve the quota level settings, and usage statics per mailbox
*
* @return array
* @throws ConnectionFailedException
* @throws AuthFailedException
* @throws ImapBadRequestException
* @throws ImapServerErrorException
* @throws RuntimeException
* @throws ResponseException
*/
public function getQuota(): array {
$this->checkConnection();
return $this->connection->getQuota($this->username)->validatedData();
}
/**
* Retrieve the quota settings per user
* @param string $quota_root
*
* @return array
* @throws ConnectionFailedException
* @throws AuthFailedException
* @throws ImapBadRequestException
* @throws ImapServerErrorException
* @throws RuntimeException
* @throws ResponseException
*/
public function getQuotaRoot(string $quota_root = 'INBOX'): array {
$this->checkConnection();
return $this->connection->getQuotaRoot($quota_root)->validatedData();
}
/**
* Delete all messages marked for deletion
*
* @return array
* @throws ConnectionFailedException
* @throws ImapBadRequestException
* @throws ImapServerErrorException
* @throws RuntimeException
* @throws AuthFailedException
* @throws ResponseException
*/
public function expunge(): array {
$this->checkConnection();
return $this->connection->expunge()->validatedData();
}
/**
* Set the connection timeout
* @param integer $timeout
*
* @return ProtocolInterface
* @throws ConnectionFailedException
* @throws AuthFailedException
* @throws ImapBadRequestException
* @throws ImapServerErrorException
* @throws RuntimeException
* @throws ResponseException
*/
public function setTimeout(int $timeout): ProtocolInterface {
$this->timeout = $timeout;
if ($this->isConnected()) {
$this->connection->setConnectionTimeout($timeout);
$this->reconnect();
}
return $this->connection;
}
/**
* Get the connection timeout
*
* @return int
* @throws ConnectionFailedException
* @throws AuthFailedException
* @throws ImapBadRequestException
* @throws ImapServerErrorException
* @throws RuntimeException
* @throws ResponseException
*/
public function getTimeout(): int {
$this->checkConnection();
return $this->connection->getConnectionTimeout();
}
/**
* Get the default message mask
*
* @return string
*/
public function getDefaultMessageMask(): string {
return $this->default_message_mask;
}
/**
* Get the default events for a given section
* @param $section
*
* @return array
*/
public function getDefaultEvents($section): array {
if (isset($this->events[$section])) {
return is_array($this->events[$section]) ? $this->events[$section] : [];
}
return [];
}
/**
* Set the default message mask
* @param string $mask
*
* @return $this
* @throws MaskNotFoundException
*/
public function setDefaultMessageMask(string $mask): Client {
if(class_exists($mask)) {
$this->default_message_mask = $mask;
return $this;
}
throw new MaskNotFoundException("Unknown mask provided: ".$mask);
}
/**
* Get the default attachment mask
*
* @return string
*/
public function getDefaultAttachmentMask(): string {
return $this->default_attachment_mask;
}
/**
* Set the default attachment mask
* @param string $mask
*
* @return $this
* @throws MaskNotFoundException
*/
public function setDefaultAttachmentMask(string $mask): Client {
if(class_exists($mask)) {
$this->default_attachment_mask = $mask;
return $this;
}
throw new MaskNotFoundException("Unknown mask provided: ".$mask);
}
}