Debian Codegraph @

lib/3rdParty/composer/webklex/php-imap/src/Client.php

raw symbols SCIP
1 <?php
2 /*
3 * File: Client.php
4 * Category: -
5 * Author: M. Goldenbaum
6 * Created: 19.01.17 22:21
7 * Updated: -
8 *
9 * Description:
10 * -
11 */
12
13 namespace Webklex\PHPIMAP;
14
15 use ErrorException;
16 use Webklex\PHPIMAP\Connection\Protocols\ImapProtocol;
17 use Webklex\PHPIMAP\Connection\Protocols\LegacyProtocol;
18 use Webklex\PHPIMAP\Connection\Protocols\ProtocolInterface;
19 use Webklex\PHPIMAP\Exceptions\AuthFailedException;
20 use Webklex\PHPIMAP\Exceptions\ConnectionFailedException;
21 use Webklex\PHPIMAP\Exceptions\EventNotFoundException;
22 use Webklex\PHPIMAP\Exceptions\FolderFetchingException;
23 use Webklex\PHPIMAP\Exceptions\ImapBadRequestException;
24 use Webklex\PHPIMAP\Exceptions\ImapServerErrorException;
25 use Webklex\PHPIMAP\Exceptions\MaskNotFoundException;
26 use Webklex\PHPIMAP\Exceptions\ProtocolNotSupportedException;
27 use Webklex\PHPIMAP\Exceptions\ResponseException;
28 use Webklex\PHPIMAP\Exceptions\RuntimeException;
29 use Webklex\PHPIMAP\Support\FolderCollection;
30 use Webklex\PHPIMAP\Support\Masks\AttachmentMask;
31 use Webklex\PHPIMAP\Support\Masks\MessageMask;
32 use Webklex\PHPIMAP\Traits\HasEvents;
33
34 /**
35 * Class Client
36 *
37 * @package Webklex\PHPIMAP
38 */
39 class Client {
40 use HasEvents;
41
42 /**
43 * Connection resource
44 *
45 * @var ?ProtocolInterface
46 */
47 public ?ProtocolInterface $connection = null;
48
49 /**
50 * Client configuration
51 *
52 * @var Config
53 */
54 protected Config $config;
55
56 /**
57 * Server hostname.
58 *
59 * @var string
60 */
61 public string $host;
62
63 /**
64 * Server port.
65 *
66 * @var int
67 */
68 public int $port;
69
70 /**
71 * Service protocol.
72 *
73 * @var string
74 */
75 public string $protocol;
76
77 /**
78 * Server encryption.
79 * Supported: none, ssl, tls, starttls or notls.
80 *
81 * @var string
82 */
83 public string $encryption;
84
85 /**
86 * If server has to validate cert.
87 *
88 * @var bool
89 */
90 public bool $validate_cert = true;
91
92 /**
93 * Proxy settings
94 * @var array
95 */
96 protected array $proxy = [
97 'socket' => null,
98 'request_fulluri' => false,
99 'username' => null,
100 'password' => null,
101 ];
102
103
104 /**
105 * SSL stream context options
106 *
107 * @see https://www.php.net/manual/en/context.ssl.php for possible options
108 *
109 * @var array
110 */
111 protected array $ssl_options = [];
112
113 /**
114 * Connection timeout
115 * @var int $timeout
116 */
117 public int $timeout;
118
119 /**
120 * Account username
121 *
122 * @var string
123 */
124 public string $username;
125
126 /**
127 * Account password.
128 *
129 * @var string
130 */
131 public string $password;
132
133 /**
134 * Additional data fetched from the server.
135 *
136 * @var array
137 */
138 public array $extensions;
139
140 /**
141 * Account rfc.
142 *
143 * @var string
144 */
145 public string $rfc;
146
147 /**
148 * Account authentication method.
149 *
150 * @var ?string
151 */
152 public ?string $authentication;
153
154 /**
155 * Active folder path.
156 *
157 * @var ?string
158 */
159 protected ?string $active_folder = null;
160
161 /**
162 * Default message mask
163 *
164 * @var string $default_message_mask
165 */
166 protected string $default_message_mask = MessageMask::class;
167
168 /**
169 * Default attachment mask
170 *
171 * @var string $default_attachment_mask
172 */
173 protected string $default_attachment_mask = AttachmentMask::class;
174
175 /**
176 * Used default account values
177 *
178 * @var array $default_account_config
179 */
180 protected array $default_account_config = [
181 'host' => 'localhost',
182 'port' => 993,
183 'protocol' => 'imap',
184 'encryption' => 'ssl',
185 'validate_cert' => true,
186 'username' => '',
187 'password' => '',
188 'rfc' => 'RFC822',
189 'authentication' => null,
190 "extensions" => [],
191 'proxy' => [
192 'socket' => null,
193 'request_fulluri' => false,
194 'username' => null,
195 'password' => null,
196 ],
197 'ssl_options' => [],
198 "timeout" => 30,
199 ];
200
201 /**
202 * Client constructor.
203 * @param Config $config
204 *
205 * @throws MaskNotFoundException
206 */
207 public function __construct(Config $config) {
208 $this->setConfig($config);
209 $this->setMaskFromConfig();
210 $this->setEventsFromConfig();
211 }
212
213 /**
214 * Client destructor
215 *
216 * @throws ImapBadRequestException
217 * @throws ImapServerErrorException
218 * @throws RuntimeException
219 */
220 public function __destruct() {
221 $this->disconnect();
222 }
223
224 /**
225 * Clone the current Client instance
226 *
227 * @return Client
228 * @throws MaskNotFoundException
229 */
230 public function clone(): Client {
231 $client = new self($this->config);
232 $client->events = $this->events;
233 $client->timeout = $this->timeout;
234 $client->active_folder = $this->active_folder;
235 $client->default_account_config = $this->default_account_config;
236 $config = $this->getAccountConfig();
237 foreach($config as $key => $value) {
238 $client->setAccountConfig($key, $config);
239 }
240 $client->default_message_mask = $this->default_message_mask;
241 $client->default_attachment_mask = $this->default_message_mask;
242 return $client;
243 }
244
245 /**
246 * Set the Client configuration
247 * @param Config $config
248 *
249 * @return self
250 */
251 public function setConfig(Config $config): Client {
252 $this->config = $config;
253 $default_account = $this->config->get('default');
254 $default_config = $this->config->get("accounts.$default_account");
255
256 foreach ($this->default_account_config as $key => $value) {
257 $this->setAccountConfig($key, $default_config);
258 }
259
260 return $this;
261 }
262
263 /**
264 * Get the current config
265 *
266 * @return Config
267 */
268 public function getConfig(): Config {
269 return $this->config;
270 }
271
272 /**
273 * Set a specific account config
274 * @param string $key
275 * @param array $default_config
276 */
277 private function setAccountConfig(string $key, array $default_config): void {
278 $value = $this->default_account_config[$key];
279 if(isset($default_config[$key])) {
280 $value = $default_config[$key];
281 }
282 $this->$key = $value;
283 }
284
285 /**
286 * Get the current account config
287 *
288 * @return array
289 */
290 public function getAccountConfig(): array {
291 $config = [];
292 foreach($this->default_account_config as $key => $value) {
293 if(property_exists($this, $key)) {
294 $config[$key] = $this->$key;
295 }
296 }
297 return $config;
298 }
299
300 /**
301 * Look for a possible events in any available config
302 */
303 protected function setEventsFromConfig(): void {
304 $this->events = $this->config->get("events");
305 if(isset($config['events'])){
306 foreach($config['events'] as $section => $events) {
307 $this->events[$section] = array_merge($this->events[$section], $events);
308 }
309 }
310 }
311
312 /**
313 * Look for a possible mask in any available config
314 *
315 * @throws MaskNotFoundException
316 */
317 protected function setMaskFromConfig(): void {
318 $masks = $this->config->get("masks");
319
320 if(isset($masks)){
321 if(isset($masks['message'])) {
322 if(class_exists($masks['message'])) {
323 $this->default_message_mask = $masks['message'];
324 }else{
325 throw new MaskNotFoundException("Unknown mask provided: ".$masks['message']);
326 }
327 }else{
328 $default_mask = $this->config->getMask("message");
329 if($default_mask != ""){
330 $this->default_message_mask = $default_mask;
331 }else{
332 throw new MaskNotFoundException("Unknown message mask provided");
333 }
334 }
335 if(isset($masks['attachment'])) {
336 if(class_exists($masks['attachment'])) {
337 $this->default_attachment_mask = $masks['attachment'];
338 }else{
339 throw new MaskNotFoundException("Unknown mask provided: ". $masks['attachment']);
340 }
341 }else{
342 $default_mask = $this->config->getMask("attachment");
343 if($default_mask != ""){
344 $this->default_attachment_mask = $default_mask;
345 }else{
346 throw new MaskNotFoundException("Unknown attachment mask provided");
347 }
348 }
349 }else{
350 $default_mask = $this->config->getMask("message");
351 if($default_mask != ""){
352 $this->default_message_mask = $default_mask;
353 }else{
354 throw new MaskNotFoundException("Unknown message mask provided");
355 }
356
357 $default_mask = $this->config->getMask("attachment");
358 if($default_mask != ""){
359 $this->default_attachment_mask = $default_mask;
360 }else{
361 throw new MaskNotFoundException("Unknown attachment mask provided");
362 }
363 }
364 }
365
366 /**
367 * Get the current imap resource
368 *
369 * @return ProtocolInterface
370 * @throws ConnectionFailedException
371 * @throws AuthFailedException
372 * @throws ImapBadRequestException
373 * @throws ImapServerErrorException
374 * @throws RuntimeException
375 * @throws ResponseException
376 */
377 public function getConnection(): ProtocolInterface {
378 $this->checkConnection();
379 return $this->connection;
380 }
381
382 /**
383 * Determine if connection was established.
384 *
385 * @return bool
386 */
387 public function isConnected(): bool {
388 return $this->connection && $this->connection->connected();
389 }
390
391 /**
392 * Determine if connection was established and connect if not.
393 * Returns true if the connection was closed and has been reopened.
394 *
395 * @throws ConnectionFailedException
396 * @throws AuthFailedException
397 * @throws ImapBadRequestException
398 * @throws ImapServerErrorException
399 * @throws RuntimeException
400 * @throws ResponseException
401 */
402 public function checkConnection(): bool {
403 try {
404 if (!$this->isConnected()) {
405 $this->connect();
406 return true;
407 }
408 } catch (\Throwable) {
409 $this->connect();
410 }
411 return false;
412 }
413
414 /**
415 * Force the connection to reconnect
416 *
417 * @throws ConnectionFailedException
418 * @throws AuthFailedException
419 * @throws ImapBadRequestException
420 * @throws ImapServerErrorException
421 * @throws RuntimeException
422 * @throws ResponseException
423 */
424 public function reconnect(): void {
425 if ($this->isConnected()) {
426 $this->disconnect();
427 }
428 $this->connect();
429 }
430
431 /**
432 * Connect to server.
433 *
434 * @return $this
435 * @throws ConnectionFailedException
436 * @throws AuthFailedException
437 * @throws ImapBadRequestException
438 * @throws ImapServerErrorException
439 * @throws RuntimeException
440 * @throws ResponseException
441 */
442 public function connect(): Client {
443 $this->disconnect();
444 $protocol = strtolower($this->protocol);
445
446 if (in_array($protocol, ['imap', 'imap4', 'imap4rev1'])) {
447 $this->connection = new ImapProtocol($this->config, $this->validate_cert, $this->encryption);
448 $this->connection->setConnectionTimeout($this->timeout);
449 $this->connection->setProxy($this->proxy);
450 $this->connection->setSslOptions($this->ssl_options);
451 }else{
452 if (extension_loaded('imap') === false) {
453 throw new ConnectionFailedException("connection setup failed", 0, new ProtocolNotSupportedException($protocol." is an unsupported protocol"));
454 }
455 $this->connection = new LegacyProtocol($this->config, $this->validate_cert, $this->encryption);
456 if (str_starts_with($protocol, "legacy-")) {
457 $protocol = substr($protocol, 7);
458 }
459 $this->connection->setProtocol($protocol);
460 }
461
462 if ($this->config->get('options.debug')) {
463 $this->connection->enableDebug();
464 }
465
466 if (!$this->config->get('options.uid_cache')) {
467 $this->connection->disableUidCache();
468 }
469
470 try {
471 $this->connection->connect($this->host, $this->port);
472 } catch (ErrorException|RuntimeException $e) {
473 throw new ConnectionFailedException("connection setup failed", 0, $e);
474 }
475 $this->authenticate();
476
477 return $this;
478 }
479
480 /**
481 * Authenticate the current session
482 *
483 * @throws AuthFailedException
484 * @throws ImapBadRequestException
485 * @throws ImapServerErrorException
486 * @throws ResponseException
487 */
488 protected function authenticate(): void {
489 if ($this->authentication == "oauth") {
490 if (!$this->connection->authenticate($this->username, $this->password)->validatedData()) {
491 throw new AuthFailedException();
492 }
493 } elseif (!$this->connection->login($this->username, $this->password)->validatedData()) {
494 throw new AuthFailedException();
495 }
496 }
497
498 /**
499 * Disconnect from server.
500 *
501 * @return $this
502 * @throws ImapBadRequestException
503 * @throws ImapServerErrorException
504 * @throws RuntimeException
505 */
506 public function disconnect(): Client {
507 if ($this->isConnected()) {
508 $this->connection->logout();
509 }
510 $this->active_folder = null;
511
512 return $this;
513 }
514
515 /**
516 * Get a folder instance by a folder name
517 * @param string $folder_name
518 * @param string|null $delimiter
519 * @param bool $utf7
520 * @return Folder|null
521 * @throws AuthFailedException
522 * @throws ConnectionFailedException
523 * @throws FolderFetchingException
524 * @throws ImapBadRequestException
525 * @throws ImapServerErrorException
526 * @throws ResponseException
527 * @throws RuntimeException
528 */
529 public function getFolder(string $folder_name, ?string $delimiter = null, bool $utf7 = false): ?Folder {
530 // Set delimiter to false to force selection via getFolderByName (maybe useful for uncommon folder names)
531 $delimiter = is_null($delimiter) ? $this->config->get('options.delimiter', "/") : $delimiter;
532
533 if (str_contains($folder_name, (string)$delimiter)) {
534 return $this->getFolderByPath($folder_name, $utf7);
535 }
536
537 return $this->getFolderByName($folder_name);
538 }
539
540 /**
541 * Get a folder instance by a folder name
542 * @param $folder_name
543 * @param bool $soft_fail If true, it will return null instead of throwing an exception
544 *
545 * @return Folder|null
546 * @throws FolderFetchingException
547 * @throws ConnectionFailedException
548 * @throws AuthFailedException
549 * @throws ImapBadRequestException
550 * @throws ImapServerErrorException
551 * @throws RuntimeException
552 * @throws ResponseException
553 */
554 public function getFolderByName($folder_name, bool $soft_fail = false): ?Folder {
555 return $this->getFolders(false, null, $soft_fail)->where("name", $folder_name)->first();
556 }
557
558 /**
559 * Get a folder instance by a folder path
560 * @param $folder_path
561 * @param bool $utf7
562 * @param bool $soft_fail If true, it will return null instead of throwing an exception
563 *
564 * @return Folder|null
565 * @throws AuthFailedException
566 * @throws ConnectionFailedException
567 * @throws FolderFetchingException
568 * @throws ImapBadRequestException
569 * @throws ImapServerErrorException
570 * @throws ResponseException
571 * @throws RuntimeException
572 */
573 public function getFolderByPath($folder_path, bool $utf7 = false, bool $soft_fail = false): ?Folder {
574 if (!$utf7) $folder_path = EncodingAliases::convert($folder_path, "utf-8", "utf7-imap");
575 return $this->getFolders(false, null, $soft_fail)->where("path", $folder_path)->first();
576 }
577
578 /**
579 * Get folders list.
580 * If hierarchical order is set to true, it will make a tree of folders, otherwise it will return flat array.
581 *
582 * @param boolean $hierarchical
583 * @param string|null $parent_folder
584 * @param bool $soft_fail If true, it will return an empty collection instead of throwing an exception
585 *
586 * @return FolderCollection
587 * @throws AuthFailedException
588 * @throws ConnectionFailedException
589 * @throws FolderFetchingException
590 * @throws ImapBadRequestException
591 * @throws ImapServerErrorException
592 * @throws ResponseException
593 * @throws RuntimeException
594 */
595 public function getFolders(bool $hierarchical = true, ?string $parent_folder = null, bool $soft_fail = false): FolderCollection {
596 $this->checkConnection();
597 $folders = FolderCollection::make([]);
598
599 $pattern = $parent_folder.($hierarchical ? '%' : '*');
600 $items = $this->connection->folders('', $pattern)->validatedData();
601
602 if(!empty($items)){
603 foreach ($items as $folder_name => $item) {
604 $folder = new Folder($this, $folder_name, $item["delimiter"], $item["flags"]);
605
606 if ($hierarchical && $folder->hasChildren()) {
607 $pattern = $folder->path.$folder->delimiter.'%';
608
609 $children = $this->getFolders(true, $pattern, true);
610 $folder->setChildren($children);
611 }
612
613 $folders->push($folder);
614 }
615
616 return $folders;
617 }else if (!$soft_fail){
618 throw new FolderFetchingException("failed to fetch any folders");
619 }
620
621 return $folders;
622 }
623
624 /**
625 * Get folders list.
626 * If hierarchical order is set to true, it will make a tree of folders, otherwise it will return flat array.
627 *
628 * @param boolean $hierarchical
629 * @param string|null $parent_folder
630 * @param bool $soft_fail If true, it will return an empty collection instead of throwing an exception
631 *
632 * @return FolderCollection
633 * @throws FolderFetchingException
634 * @throws ConnectionFailedException
635 * @throws AuthFailedException
636 * @throws ImapBadRequestException
637 * @throws ImapServerErrorException
638 * @throws RuntimeException
639 * @throws ResponseException
640 */
641 public function getFoldersWithStatus(bool $hierarchical = true, ?string $parent_folder = null, bool $soft_fail = false): FolderCollection {
642 $this->checkConnection();
643 $folders = FolderCollection::make([]);
644
645 $pattern = $parent_folder.($hierarchical ? '%' : '*');
646 $items = $this->connection->folders('', $pattern)->validatedData();
647
648 if(!empty($items)){
649 foreach ($items as $folder_name => $item) {
650 $folder = new Folder($this, $folder_name, $item["delimiter"], $item["flags"]);
651
652 if ($hierarchical && $folder->hasChildren()) {
653 $pattern = $folder->path.$folder->delimiter.'%';
654
655 $children = $this->getFoldersWithStatus(true, $pattern, true);
656 $folder->setChildren($children);
657 }
658
659 $folder->loadStatus();
660 $folders->push($folder);
661 }
662
663 return $folders;
664 }else if (!$soft_fail){
665 throw new FolderFetchingException("failed to fetch any folders");
666 }
667
668 return $folders;
669 }
670
671 /**
672 * Open a given folder.
673 * @param string $folder_path
674 * @param boolean $force_select
675 *
676 * @return array
677 * @throws ConnectionFailedException
678 * @throws AuthFailedException
679 * @throws ImapBadRequestException
680 * @throws ImapServerErrorException
681 * @throws RuntimeException
682 * @throws ResponseException
683 */
684 public function openFolder(string $folder_path, bool $force_select = false): array {
685 if ($this->active_folder == $folder_path && $this->isConnected() && $force_select === false) {
686 return [];
687 }
688 $this->checkConnection();
689 $this->active_folder = $folder_path;
690 return $this->connection->selectFolder($folder_path)->validatedData();
691 }
692
693 /**
694 * Set active folder
695 * @param string|null $folder_path
696 *
697 * @return void
698 */
699 public function setActiveFolder(?string $folder_path = null): void {
700 $this->active_folder = $folder_path;
701 }
702
703 /**
704 * Get active folder
705 *
706 * @return string|null
707 */
708 public function getActiveFolder(): ?string {
709 return $this->active_folder;
710 }
711
712 /**
713 * Create a new Folder
714 * @param string $folder_path
715 * @param boolean $expunge
716 * @param bool $utf7
717 * @return Folder
718 * @throws AuthFailedException
719 * @throws ConnectionFailedException
720 * @throws EventNotFoundException
721 * @throws FolderFetchingException
722 * @throws ImapBadRequestException
723 * @throws ImapServerErrorException
724 * @throws ResponseException
725 * @throws RuntimeException
726 */
727 public function createFolder(string $folder_path, bool $expunge = true, bool $utf7 = false): Folder {
728 $this->checkConnection();
729
730 if (!$utf7) $folder_path = EncodingAliases::convert($folder_path, "utf-8", "UTF7-IMAP");
731
732 $status = $this->connection->createFolder($folder_path)->validatedData();
733
734 if($expunge) $this->expunge();
735
736 $folder = $this->getFolderByPath($folder_path, true);
737 if($status && $folder) {
738 $this->dispatch("folder", "new", $folder);
739 }
740
741 return $folder;
742 }
743
744 /**
745 * Delete a given folder
746 * @param string $folder_path
747 * @param boolean $expunge
748 *
749 * @return array
750 * @throws AuthFailedException
751 * @throws ConnectionFailedException
752 * @throws EventNotFoundException
753 * @throws FolderFetchingException
754 * @throws ImapBadRequestException
755 * @throws ImapServerErrorException
756 * @throws RuntimeException
757 * @throws ResponseException
758 */
759 public function deleteFolder(string $folder_path, bool $expunge = true): array {
760 $this->checkConnection();
761
762 $folder = $this->getFolderByPath($folder_path);
763 if ($this->active_folder == $folder->path){
764 $this->active_folder = null;
765 }
766 $status = $this->getConnection()->deleteFolder($folder->path)->validatedData();
767 if ($expunge) $this->expunge();
768
769 $this->dispatch("folder", "deleted", $folder);
770
771 return $status;
772 }
773
774 /**
775 * Check a given folder
776 * @param string $folder_path
777 *
778 * @return array
779 * @throws ConnectionFailedException
780 * @throws AuthFailedException
781 * @throws ImapBadRequestException
782 * @throws ImapServerErrorException
783 * @throws RuntimeException
784 * @throws ResponseException
785 */
786 public function checkFolder(string $folder_path): array {
787 $this->checkConnection();
788 return $this->connection->examineFolder($folder_path)->validatedData();
789 }
790
791 /**
792 * Get the current active folder
793 *
794 * @return null|string
795 */
796 public function getFolderPath(): ?string {
797 return $this->active_folder;
798 }
799
800 /**
801 * Exchange identification information
802 * Ref.: https://datatracker.ietf.org/doc/html/rfc2971
803 *
804 * @param array|null $ids
805 * @return array
806 *
807 * @throws ConnectionFailedException
808 * @throws AuthFailedException
809 * @throws ImapBadRequestException
810 * @throws ImapServerErrorException
811 * @throws RuntimeException
812 * @throws ResponseException
813 */
814 public function Id(?array $ids = null): array {
815 $this->checkConnection();
816 return $this->connection->ID($ids)->validatedData();
817 }
818
819 /**
820 * Retrieve the quota level settings, and usage statics per mailbox
821 *
822 * @return array
823 * @throws ConnectionFailedException
824 * @throws AuthFailedException
825 * @throws ImapBadRequestException
826 * @throws ImapServerErrorException
827 * @throws RuntimeException
828 * @throws ResponseException
829 */
830 public function getQuota(): array {
831 $this->checkConnection();
832 return $this->connection->getQuota($this->username)->validatedData();
833 }
834
835 /**
836 * Retrieve the quota settings per user
837 * @param string $quota_root
838 *
839 * @return array
840 * @throws ConnectionFailedException
841 * @throws AuthFailedException
842 * @throws ImapBadRequestException
843 * @throws ImapServerErrorException
844 * @throws RuntimeException
845 * @throws ResponseException
846 */
847 public function getQuotaRoot(string $quota_root = 'INBOX'): array {
848 $this->checkConnection();
849 return $this->connection->getQuotaRoot($quota_root)->validatedData();
850 }
851
852 /**
853 * Delete all messages marked for deletion
854 *
855 * @return array
856 * @throws ConnectionFailedException
857 * @throws ImapBadRequestException
858 * @throws ImapServerErrorException
859 * @throws RuntimeException
860 * @throws AuthFailedException
861 * @throws ResponseException
862 */
863 public function expunge(): array {
864 $this->checkConnection();
865 return $this->connection->expunge()->validatedData();
866 }
867
868 /**
869 * Set the connection timeout
870 * @param integer $timeout
871 *
872 * @return ProtocolInterface
873 * @throws ConnectionFailedException
874 * @throws AuthFailedException
875 * @throws ImapBadRequestException
876 * @throws ImapServerErrorException
877 * @throws RuntimeException
878 * @throws ResponseException
879 */
880 public function setTimeout(int $timeout): ProtocolInterface {
881 $this->timeout = $timeout;
882 if ($this->isConnected()) {
883 $this->connection->setConnectionTimeout($timeout);
884 $this->reconnect();
885 }
886 return $this->connection;
887 }
888
889 /**
890 * Get the connection timeout
891 *
892 * @return int
893 * @throws ConnectionFailedException
894 * @throws AuthFailedException
895 * @throws ImapBadRequestException
896 * @throws ImapServerErrorException
897 * @throws RuntimeException
898 * @throws ResponseException
899 */
900 public function getTimeout(): int {
901 $this->checkConnection();
902 return $this->connection->getConnectionTimeout();
903 }
904
905 /**
906 * Get the default message mask
907 *
908 * @return string
909 */
910 public function getDefaultMessageMask(): string {
911 return $this->default_message_mask;
912 }
913
914 /**
915 * Get the default events for a given section
916 * @param $section
917 *
918 * @return array
919 */
920 public function getDefaultEvents($section): array {
921 if (isset($this->events[$section])) {
922 return is_array($this->events[$section]) ? $this->events[$section] : [];
923 }
924 return [];
925 }
926
927 /**
928 * Set the default message mask
929 * @param string $mask
930 *
931 * @return $this
932 * @throws MaskNotFoundException
933 */
934 public function setDefaultMessageMask(string $mask): Client {
935 if(class_exists($mask)) {
936 $this->default_message_mask = $mask;
937
938 return $this;
939 }
940
941 throw new MaskNotFoundException("Unknown mask provided: ".$mask);
942 }
943
944 /**
945 * Get the default attachment mask
946 *
947 * @return string
948 */
949 public function getDefaultAttachmentMask(): string {
950 return $this->default_attachment_mask;
951 }
952
953 /**
954 * Set the default attachment mask
955 * @param string $mask
956 *
957 * @return $this
958 * @throws MaskNotFoundException
959 */
960 public function setDefaultAttachmentMask(string $mask): Client {
961 if(class_exists($mask)) {
962 $this->default_attachment_mask = $mask;
963
964 return $this;
965 }
966
967 throw new MaskNotFoundException("Unknown mask provided: ".$mask);
968 }
969 }