Debian Codegraph @

lib/3rdParty/composer/webklex/php-imap/src/Connection/Protocols/LegacyProtocol.php

raw symbols SCIP
1 <?php
2 /*
3 * File: LegacyProtocol.php
4 * Category: Protocol
5 * Author: M.Goldenbaum
6 * Created: 16.09.20 18:27
7 * Updated: -
8 *
9 * Description:
10 * -
11 */
12
13 namespace Webklex\PHPIMAP\Connection\Protocols;
14
15 use Webklex\PHPIMAP\ClientManager;
16 use Webklex\PHPIMAP\Config;
17 use Webklex\PHPIMAP\Exceptions\AuthFailedException;
18 use Webklex\PHPIMAP\Exceptions\ImapBadRequestException;
19 use Webklex\PHPIMAP\Exceptions\MethodNotSupportedException;
20 use Webklex\PHPIMAP\Exceptions\RuntimeException;
21 use Webklex\PHPIMAP\IMAP;
22
23 /**
24 * Class LegacyProtocol
25 *
26 * @package Webklex\PHPIMAP\Connection\Protocols
27 */
28 class LegacyProtocol extends Protocol {
29
30 protected string $protocol = "imap";
31 protected string $host = "localhost";
32 protected int $port = 993;
33
34 /**
35 * Imap constructor.
36 * @param Config $config
37 * @param bool $cert_validation set to false to skip SSL certificate validation
38 * @param mixed $encryption Connection encryption method
39 */
40 public function __construct(Config $config, bool $cert_validation = true, mixed $encryption = false) {
41 $this->config = $config;
42 $this->setCertValidation($cert_validation);
43 $this->encryption = $encryption;
44 }
45
46 /**
47 * Public destructor
48 */
49 public function __destruct() {
50 $this->logout();
51 }
52
53 /**
54 * Save the information for a nw connection
55 * @param string $host
56 * @param int|null $port
57 */
58 public function connect(string $host, ?int $port = null): void {
59 if ($this->encryption) {
60 $encryption = strtolower($this->encryption);
61 if ($encryption == "ssl") {
62 $port = $port === null ? 993 : $port;
63 }
64 }
65 $port = $port === null ? 143 : $port;
66 $this->host = $host;
67 $this->port = $port;
68 }
69
70 /**
71 * Login to a new session.
72 * @param string $user username
73 * @param string $password password
74 *
75 * @return Response
76 */
77 public function login(string $user, string $password): Response {
78 return $this->response()->wrap(function($response) use ($user, $password) {
79 /** @var Response $response */
80 try {
81 $this->stream = \imap_open(
82 $this->getAddress(),
83 $user,
84 $password,
85 0,
86 $attempts = 3,
87 $this->config->get('options.open')
88 );
89 $response->addCommand("imap_open");
90 } catch (\ErrorException $e) {
91 $errors = \imap_errors();
92 $message = $e->getMessage() . '. ' . implode("; ", (is_array($errors) ? $errors : array()));
93 throw new AuthFailedException($message);
94 }
95
96 if (!$this->stream) {
97 $errors = \imap_errors();
98 $message = implode("; ", (is_array($errors) ? $errors : array()));
99 throw new AuthFailedException($message);
100 }
101
102 $errors = \imap_errors();
103 $response->addCommand("imap_errors");
104 if (is_array($errors)) {
105 $status = $this->examineFolder();
106 $response->stack($status);
107 if ($status->data()['exists'] !== 0) {
108 $message = implode("; ", $errors);
109 throw new RuntimeException($message);
110 }
111 }
112
113 if ($this->stream !== false) {
114 return ["TAG" . $response->Noun() . " OK [] Logged in\r\n"];
115 }
116
117 $response->addError("failed to login");
118 return [];
119 });
120 }
121
122 /**
123 * Authenticate your current session.
124 * @param string $user username
125 * @param string $token access token
126 *
127 * @return Response
128 */
129 public function authenticate(string $user, string $token): Response {
130 return $this->login($user, $token);
131 }
132
133 /**
134 * Get full address of mailbox.
135 *
136 * @return string
137 */
138 protected function getAddress(): string {
139 $address = "{" . $this->host . ":" . $this->port . "/" . $this->protocol;
140 if (!$this->cert_validation) {
141 $address .= '/novalidate-cert';
142 }
143 if (in_array($this->encryption, ['tls', 'notls', 'ssl'])) {
144 $address .= '/' . $this->encryption;
145 } elseif ($this->encryption === "starttls") {
146 $address .= '/tls';
147 }
148
149 $address .= '}';
150
151 return $address;
152 }
153
154 /**
155 * Logout of the current session
156 *
157 * @return Response
158 */
159 public function logout(): Response {
160 return $this->response()->wrap(function($response) {
161 /** @var Response $response */
162 if ($this->stream) {
163 $this->uid_cache = [];
164 $response->addCommand("imap_close");
165 if (\imap_close($this->stream, IMAP::CL_EXPUNGE)) {
166 $this->stream = false;
167 return [
168 0 => "BYE Logging out\r\n",
169 1 => "TAG" . $response->Noun() . " OK Logout completed (0.001 + 0.000 secs).\r\n",
170 ];
171 }
172 $this->stream = false;
173 }
174 return [];
175 });
176 }
177
178 /**
179 * Get an array of available capabilities
180 *
181 * @throws MethodNotSupportedException
182 */
183 public function getCapabilities(): Response {
184 throw new MethodNotSupportedException();
185 }
186
187 /**
188 * Change the current folder
189 * @param string $folder change to this folder
190 *
191 * @return Response see examineOrselect()
192 * @throws RuntimeException
193 */
194 public function selectFolder(string $folder = 'INBOX'): Response {
195 $flags = IMAP::OP_READONLY;
196 if (in_array($this->protocol, ["pop3", "nntp"])) {
197 $flags = IMAP::NIL;
198 }
199 if ($this->stream === false) {
200 throw new RuntimeException("failed to reopen stream.");
201 }
202
203 return $this->response("imap_reopen")->wrap(function($response) use ($folder, $flags) {
204 /** @var Response $response */
205 \imap_reopen($this->stream, $this->getAddress() . $folder, $flags, 3);
206 $this->uid_cache = [];
207
208 $status = $this->examineFolder($folder);
209 $response->stack($status);
210
211 return $status->data();
212 });
213 }
214
215 /**
216 * Examine a given folder
217 * @param string $folder examine this folder
218 *
219 * @return Response
220 * @throws RuntimeException
221 */
222 public function examineFolder(string $folder = 'INBOX'): Response {
223 if (str_starts_with($folder, ".")) {
224 throw new RuntimeException("Segmentation fault prevented. Folders starts with an illegal char '.'.");
225 }
226 return $this->response("imap_status")->wrap(function($response) use ($folder) {
227 /** @var Response $response */
228 $status = \imap_status($this->stream, $this->getAddress() . $folder, IMAP::SA_ALL);
229
230 return $status ? [
231 "flags" => [],
232 "exists" => $status->messages,
233 "recent" => $status->recent,
234 "unseen" => $status->unseen,
235 "uidnext" => $status->uidnext,
236 ] : [];
237 });
238 }
239
240 /**
241 * Get the status of a given folder
242 *
243 * @return Response list of STATUS items
244 * @throws MethodNotSupportedException
245 */
246 public function folderStatus(string $folder = 'INBOX', $arguments = ['MESSAGES', 'UNSEEN', 'RECENT', 'UIDNEXT', 'UIDVALIDITY']): Response {
247 throw new MethodNotSupportedException();
248 }
249
250 /**
251 * Fetch message content
252 * @param int|array $uids
253 * @param string $rfc
254 * @param int|string $uid set to IMAP::ST_UID if you pass message unique identifiers instead of numbers.
255 *
256 * @return Response
257 */
258 public function content(int|array $uids, string $rfc = "RFC822", int|string $uid = IMAP::ST_UID): Response {
259 return $this->response()->wrap(function($response) use ($uids, $uid) {
260 /** @var Response $response */
261
262 $result = [];
263 $uids = is_array($uids) ? $uids : [$uids];
264 foreach ($uids as $id) {
265 $response->addCommand("imap_fetchbody");
266 $result[$id] = \imap_fetchbody($this->stream, $id, "", $uid === IMAP::ST_UID ? IMAP::ST_UID : IMAP::NIL);
267 }
268
269 return $result;
270 });
271 }
272
273 /**
274 * Fetch message headers
275 * @param int|array $uids
276 * @param string $rfc
277 * @param int|string $uid set to IMAP::ST_UID if you pass message unique identifiers instead of numbers.
278 *
279 * @return Response
280 */
281 public function headers(int|array $uids, string $rfc = "RFC822", int|string $uid = IMAP::ST_UID): Response {
282 return $this->response()->wrap(function($response) use ($uids, $uid) {
283 /** @var Response $response */
284
285 $result = [];
286 $uids = is_array($uids) ? $uids : [$uids];
287 foreach ($uids as $id) {
288 $response->addCommand("imap_fetchheader");
289 $result[$id] = \imap_fetchheader($this->stream, $id, $uid ? IMAP::ST_UID : IMAP::NIL);
290 }
291
292 return $result;
293 });
294 }
295
296 /**
297 * Fetch message flags
298 * @param int|array $uids
299 * @param int|string $uid set to IMAP::ST_UID if you pass message unique identifiers instead of numbers.
300 *
301 * @return Response
302 */
303 public function flags(int|array $uids, int|string $uid = IMAP::ST_UID): Response {
304 return $this->response()->wrap(function($response) use ($uids, $uid) {
305 /** @var Response $response */
306
307 $result = [];
308 $uids = is_array($uids) ? $uids : [$uids];
309 foreach ($uids as $id) {
310 $response->addCommand("imap_fetch_overview");
311 $raw_flags = \imap_fetch_overview($this->stream, $id, $uid ? IMAP::ST_UID : IMAP::NIL);
312 $flags = [];
313 if (is_array($raw_flags) && isset($raw_flags[0])) {
314 $raw_flags = (array)$raw_flags[0];
315 foreach ($raw_flags as $flag => $value) {
316 if ($value === 1 && in_array($flag, ["size", "uid", "msgno", "update"]) === false) {
317 $flags[] = "\\" . ucfirst($flag);
318 }
319 }
320 }
321 $result[$id] = $flags;
322 }
323
324 return $result;
325 });
326 }
327
328 /**
329 * Fetch message sizes
330 * @param int|array $uids
331 * @param int|string $uid set to IMAP::ST_UID if you pass message unique identifiers instead of numbers.
332 *
333 * @return Response
334 */
335 public function sizes(int|array $uids, int|string $uid = IMAP::ST_UID): Response {
336 return $this->response()->wrap(function($response) use ($uids, $uid) {
337 /** @var Response $response */
338 $result = [];
339 $uids = is_array($uids) ? $uids : [$uids];
340 $uid_text = implode("','", $uids);
341 $response->addCommand("imap_fetch_overview");
342 if ($uid == IMAP::ST_UID) {
343 $raw_overview = \imap_fetch_overview($this->stream, $uid_text, IMAP::FT_UID);
344 } else {
345 $raw_overview = \imap_fetch_overview($this->stream, $uid_text);
346 }
347 if ($raw_overview !== false) {
348 foreach ($raw_overview as $overview_element) {
349 $overview_element = (array)$overview_element;
350 $result[$overview_element[$uid == IMAP::ST_UID ? 'uid' : 'msgno']] = $overview_element['size'];
351 }
352 }
353 return $result;
354 });
355 }
356
357 /**
358 * Get uid for a given id
359 * @param int|null $id message number
360 *
361 * @return Response message number for given message or all messages as array
362 */
363 public function getUid(?int $id = null): Response {
364 return $this->response()->wrap(function($response) use ($id) {
365 /** @var Response $response */
366 if ($id === null) {
367 if ($this->enable_uid_cache && $this->uid_cache) {
368 return $this->uid_cache;
369 }
370
371 $overview = $this->overview("1:*");
372 $response->stack($overview);
373 $uids = [];
374 foreach ($overview->data() as $set) {
375 $uids[$set->msgno] = $set->uid;
376 }
377
378 $this->setUidCache($uids);
379 return $uids;
380 }
381
382 $response->addCommand("imap_uid");
383 $uid = \imap_uid($this->stream, $id);
384 if ($uid) {
385 return $uid;
386 }
387
388 return [];
389 });
390 }
391
392 /**
393 * Get the message number of a given uid
394 * @param string $id uid
395 *
396 * @return Response message number
397 */
398 public function getMessageNumber(string $id): Response {
399 return $this->response("imap_msgno")->wrap(function($response) use ($id) {
400 /** @var Response $response */
401 return \imap_msgno($this->stream, $id);
402 });
403 }
404
405 /**
406 * Get a message overview
407 * @param string $sequence uid sequence
408 * @param int|string $uid set to IMAP::ST_UID if you pass message unique identifiers instead of numbers.
409 *
410 * @return Response
411 */
412 public function overview(string $sequence, int|string $uid = IMAP::ST_UID): Response {
413 return $this->response("imap_fetch_overview")->wrap(function($response) use ($sequence, $uid) {
414 /** @var Response $response */
415 return \imap_fetch_overview($this->stream, $sequence, $uid ? IMAP::ST_UID : IMAP::NIL) ?: [];
416 });
417 }
418
419 /**
420 * Get a list of available folders
421 * @param string $reference mailbox reference for list
422 * @param string $folder mailbox name match with wildcards
423 *
424 * @return Response folders that matched $folder as array(name => array('delimiter' => .., 'flags' => ..))
425 */
426 public function folders(string $reference = '', string $folder = '*'): Response {
427 return $this->response("imap_getmailboxes")->wrap(function($response) use ($reference, $folder) {
428 /** @var Response $response */
429 $result = [];
430
431 $items = \imap_getmailboxes($this->stream, $this->getAddress(), $reference . $folder);
432 if (is_array($items)) {
433 foreach ($items as $item) {
434 $name = $this->decodeFolderName($item->name);
435 $result[$name] = ['delimiter' => $item->delimiter, 'flags' => []];
436 }
437 } else {
438 throw new RuntimeException(\imap_last_error());
439 }
440
441 return $result;
442 });
443 }
444
445 /**
446 * Manage flags
447 * @param array|string $flags flags to set, add or remove - see $mode
448 * @param int $from message for items or start message if $to !== null
449 * @param int|null $to if null only one message ($from) is fetched, else it's the
450 * last message, INF means last message available
451 * @param string|null $mode '+' to add flags, '-' to remove flags, everything else sets the flags as given
452 * @param bool $silent if false the return values are the new flags for the wanted messages
453 * @param int|string $uid set to IMAP::ST_UID if you pass message unique identifiers instead of numbers.
454 * @param string|null $item unused attribute
455 *
456 * @return Response new flags if $silent is false, else true or false depending on success
457 */
458 public function store(array|string $flags, int $from, ?int $to = null, ?string $mode = null, bool $silent = true, int|string $uid = IMAP::ST_UID, ?string $item = null): Response {
459 $flag = trim(is_array($flags) ? implode(" ", $flags) : $flags);
460
461 return $this->response()->wrap(function($response) use ($mode, $from, $flag, $uid, $silent) {
462 /** @var Response $response */
463
464 if ($mode == "+") {
465 $response->addCommand("imap_setflag_full");
466 $status = \imap_setflag_full($this->stream, $from, $flag, $uid ? IMAP::ST_UID : IMAP::NIL);
467 } else {
468 $response->addCommand("imap_clearflag_full");
469 $status = \imap_clearflag_full($this->stream, $from, $flag, $uid ? IMAP::ST_UID : IMAP::NIL);
470 }
471
472 if ($silent === true) {
473 if ($status) {
474 return [
475 "TAG" . $response->Noun() . " OK Store completed (0.001 + 0.000 secs).\r\n"
476 ];
477 }
478 return [];
479 }
480
481 return $this->flags($from);
482 });
483 }
484
485 /**
486 * Append a new message to given folder
487 * @param string $folder name of target folder
488 * @param string $message full message content
489 * @param array|null $flags flags for new message
490 * @param mixed $date date for new message
491 *
492 * @return Response
493 */
494 public function appendMessage(string $folder, string $message, ?array $flags = null, mixed $date = null): Response {
495 return $this->response("imap_append")->wrap(function($response) use ($folder, $message, $flags, $date) {
496 /** @var Response $response */
497 if ($date != null) {
498 if ($date instanceof \Carbon\Carbon) {
499 $date = $date->format('d-M-Y H:i:s O');
500 }
501 if (\imap_append($this->stream, $this->getAddress() . $folder, $message, $flags, $date)) {
502 return [
503 "OK Append completed (0.001 + 0.000 secs).\r\n"
504 ];
505 }
506 } else if (\imap_append($this->stream, $this->getAddress() . $folder, $message, $flags)) {
507 return [
508 "OK Append completed (0.001 + 0.000 secs).\r\n"
509 ];
510 }
511 return [];
512 });
513 }
514
515 /**
516 * Copy message set from current folder to other folder
517 * @param string $folder destination folder
518 * @param $from
519 * @param int|null $to if null only one message ($from) is fetched, else it's the
520 * last message, INF means last message available
521 * @param int|string $uid set to IMAP::ST_UID if you pass message unique identifiers instead of numbers.
522 *
523 * @return Response
524 */
525 public function copyMessage(string $folder, $from, ?int $to = null, int|string $uid = IMAP::ST_UID): Response {
526 return $this->response("imap_mail_copy")->wrap(function($response) use ($from, $folder, $uid) {
527 /** @var Response $response */
528
529 if (\imap_mail_copy($this->stream, $from, $this->getAddress() . $folder, $uid ? IMAP::ST_UID : IMAP::NIL)) {
530 return [
531 "TAG" . $response->Noun() . " OK Copy completed (0.001 + 0.000 secs).\r\n"
532 ];
533 }
534 throw new ImapBadRequestException("Invalid ID $from");
535 });
536 }
537
538 /**
539 * Copy multiple messages to the target folder
540 * @param array $messages List of message identifiers
541 * @param string $folder Destination folder
542 * @param int|string $uid set to IMAP::ST_UID if you pass message unique identifiers instead of numbers.
543 *
544 * @return Response Tokens if operation successful, false if an error occurred
545 */
546 public function copyManyMessages(array $messages, string $folder, int|string $uid = IMAP::ST_UID): Response {
547 return $this->response()->wrap(function($response) use ($messages, $folder, $uid) {
548 /** @var Response $response */
549 foreach ($messages as $msg) {
550 $copy_response = $this->copyMessage($folder, $msg, null, $uid);
551 $response->stack($copy_response);
552 if (empty($copy_response->data())) {
553 return [
554 "TAG" . $response->Noun() . " BAD Copy failed (0.001 + 0.000 secs).\r\n",
555 "Invalid ID $msg\r\n"
556 ];
557 }
558 }
559 return [
560 "TAG" . $response->Noun() . " OK Copy completed (0.001 + 0.000 secs).\r\n"
561 ];
562 });
563 }
564
565 /**
566 * Move a message set from current folder to another folder
567 * @param string $folder destination folder
568 * @param $from
569 * @param int|null $to if null only one message ($from) is fetched, else it's the
570 * last message, INF means last message available
571 * @param int|string $uid set to IMAP::ST_UID if you pass message unique identifiers instead of numbers.
572 *
573 * @return Response success
574 */
575 public function moveMessage(string $folder, $from, ?int $to = null, int|string $uid = IMAP::ST_UID): Response {
576 return $this->response("imap_mail_move")->wrap(function($response) use ($from, $folder, $uid) {
577 if (\imap_mail_move($this->stream, $from, $this->getAddress() . $folder, $uid ? IMAP::ST_UID : IMAP::NIL)) {
578 return [
579 "TAG" . $response->Noun() . " OK Move completed (0.001 + 0.000 secs).\r\n"
580 ];
581 }
582 throw new ImapBadRequestException("Invalid ID $from");
583 });
584 }
585
586 /**
587 * Move multiple messages to the target folder
588 * @param array $messages List of message identifiers
589 * @param string $folder Destination folder
590 * @param int|string $uid set to IMAP::ST_UID if you pass message unique identifiers instead of numbers.
591 *
592 * @return Response Tokens if operation successful, false if an error occurred
593 * @throws ImapBadRequestException
594 */
595 public function moveManyMessages(array $messages, string $folder, int|string $uid = IMAP::ST_UID): Response {
596 return $this->response()->wrap(function($response) use ($messages, $folder, $uid) {
597 foreach ($messages as $msg) {
598 $move_response = $this->moveMessage($folder, $msg, null, $uid);
599 $response = $response->include($response);
600 if (empty($move_response->data())) {
601 return [
602 "TAG" . $response->Noun() . " BAD Move failed (0.001 + 0.000 secs).\r\n",
603 "Invalid ID $msg\r\n"
604 ];
605 }
606 }
607 return [
608 "TAG" . $response->Noun() . " OK Move completed (0.001 + 0.000 secs).\r\n"
609 ];
610 });
611 }
612
613 /**
614 * Exchange identification information
615 * Ref.: https://datatracker.ietf.org/doc/html/rfc2971
616 *
617 * @param null $ids
618 * @return Response
619 *
620 * @throws MethodNotSupportedException
621 */
622 public function ID($ids = null): Response {
623 throw new MethodNotSupportedException();
624 }
625
626 /**
627 * Create a new folder (and parent folders if needed)
628 * @param string $folder folder name
629 *
630 * @return Response
631 */
632 public function createFolder(string $folder): Response {
633 return $this->response("imap_createmailbox")->wrap(function($response) use ($folder) {
634 return \imap_createmailbox($this->stream, $this->getAddress() . $folder) ? [
635 0 => "TAG" . $response->Noun() . " OK Create completed (0.004 + 0.000 + 0.003 secs).\r\n",
636 ] : [];
637 });
638 }
639
640 /**
641 * Rename an existing folder
642 * @param string $old old name
643 * @param string $new new name
644 *
645 * @return Response
646 */
647 public function renameFolder(string $old, string $new): Response {
648 return $this->response("imap_renamemailbox")->wrap(function($response) use ($old, $new) {
649 return \imap_renamemailbox($this->stream, $this->getAddress() . $old, $this->getAddress() . $new) ? [
650 0 => "TAG" . $response->Noun() . " OK Move completed (0.004 + 0.000 + 0.003 secs).\r\n",
651 ] : [];
652 });
653 }
654
655 /**
656 * Delete a folder
657 * @param string $folder folder name
658 *
659 * @return Response
660 */
661 public function deleteFolder(string $folder): Response {
662 return $this->response("imap_deletemailbox")->wrap(function($response) use ($folder) {
663 return \imap_deletemailbox($this->stream, $this->getAddress() . $folder) ? [
664 0 => "OK Delete completed (0.004 + 0.000 + 0.003 secs).\r\n",
665 ] : [];
666 });
667 }
668
669 /**
670 * Subscribe to a folder
671 * @param string $folder folder name
672 *
673 * @throws MethodNotSupportedException
674 */
675 public function subscribeFolder(string $folder): Response {
676 throw new MethodNotSupportedException();
677 }
678
679 /**
680 * Unsubscribe from a folder
681 * @param string $folder folder name
682 *
683 * @throws MethodNotSupportedException
684 */
685 public function unsubscribeFolder(string $folder): Response {
686 throw new MethodNotSupportedException();
687 }
688
689 /**
690 * Apply session saved changes to the server
691 *
692 * @return Response
693 */
694 public function expunge(): Response {
695 return $this->response("imap_expunge")->wrap(function($response) {
696 return \imap_expunge($this->stream) ? [
697 0 => "TAG" . $response->Noun() . " OK Expunge completed (0.001 + 0.000 secs).\r\n",
698 ] : [];
699 });
700 }
701
702 /**
703 * Send noop command
704 *
705 * @throws MethodNotSupportedException
706 */
707 public function noop(): Response {
708 throw new MethodNotSupportedException();
709 }
710
711 /**
712 * Send idle command
713 *
714 * @throws MethodNotSupportedException
715 */
716 public function idle() {
717 throw new MethodNotSupportedException();
718 }
719
720 /**
721 * Send done command
722 *
723 * @throws MethodNotSupportedException
724 */
725 public function done() {
726 throw new MethodNotSupportedException();
727 }
728
729 /**
730 * Search for matching messages
731 * @param array $params
732 * @param int|string $uid set to IMAP::ST_UID if you pass message unique identifiers instead of numbers.
733 *
734 * @return Response message ids
735 */
736 public function search(array $params, int|string $uid = IMAP::ST_UID): Response {
737 return $this->response("imap_search")->wrap(function($response) use ($params, $uid) {
738 $response->setCanBeEmpty(true);
739 $result = \imap_search($this->stream, $params[0], $uid ? IMAP::ST_UID : IMAP::NIL);
740 return $result ?: [];
741 });
742 }
743
744 /**
745 * Enable the debug mode
746 */
747 public function enableDebug() {
748 $this->debug = true;
749 }
750
751 /**
752 * Disable the debug mode
753 */
754 public function disableDebug() {
755 $this->debug = false;
756 }
757
758 /**
759 * Decode name.
760 * It converts UTF7-IMAP encoding to UTF-8.
761 *
762 * @param $name
763 *
764 * @return array|false|string|string[]|null
765 */
766 protected function decodeFolderName($name): array|bool|string|null {
767 preg_match('#\{(.*)}(.*)#', $name, $preg);
768 return mb_convert_encoding($preg[2], "UTF-8", "UTF7-IMAP");
769 }
770
771 /**
772 * @return string
773 */
774 public function getProtocol(): string {
775 return $this->protocol;
776 }
777
778 /**
779 * Retrieve the quota level settings, and usage statics per mailbox
780 * @param $username
781 *
782 * @return Response
783 */
784 public function getQuota($username): Response {
785 return $this->response("imap_get_quota")->wrap(function($response) use ($username) {
786 $result = \imap_get_quota($this->stream, 'user.' . $username);
787 return $result ?: [];
788 });
789 }
790
791 /**
792 * Retrieve the quota settings per user
793 * @param string $quota_root
794 *
795 * @return Response
796 */
797 public function getQuotaRoot(string $quota_root = 'INBOX'): Response {
798 return $this->response("imap_get_quotaroot")->wrap(function($response) use ($quota_root) {
799 $result = \imap_get_quotaroot($this->stream, $this->getAddress() . $quota_root);
800 return $result ?: [];
801 });
802 }
803
804 /**
805 * @param string $protocol
806 * @return LegacyProtocol
807 */
808 public function setProtocol(string $protocol): LegacyProtocol {
809 if (($pos = strpos($protocol, "legacy")) > 0) {
810 $protocol = substr($protocol, 0, ($pos + 2) * -1);
811 }
812 $this->protocol = $protocol;
813 return $this;
814 }
815
816 /**
817 * Create a new Response instance
818 * @param string|null $command
819 *
820 * @return Response
821 */
822 protected function response(?string $command = ""): Response {
823 return Response::make(0, $command == "" ? [] : [$command], [], $this->debug);
824 }
825 }