From 9354e6005c567b0b1f2f15bfa73c15c4b0f3d4b1 Mon Sep 17 00:00:00 2001 From: Mikhail Sazanov Date: Tue, 25 Jul 2023 15:11:15 +0300 Subject: [PATCH] Add attributes and special flags Signed-off-by: Mikhail Sazanov --- src/Folder.php | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/src/Folder.php b/src/Folder.php index 5bfe2f5..58211e8 100755 --- a/src/Folder.php +++ b/src/Folder.php @@ -121,6 +121,22 @@ class Folder { /** @var array */ public array $status; + /** @var array */ + public array $attributes = []; + + + const SPECIAL_ATTRIBUTES = [ + 'haschildren' => ['\haschildren'], + 'hasnochildren' => ['\hasnochildren'], + 'template' => ['\template', '\templates'], + 'inbox' => ['\inbox'], + 'sent' => ['\sent'], + 'drafts' => ['\draft', '\drafts'], + 'archive' => ['\archive', '\archives'], + 'trash' => ['\trash'], + 'junk' => ['\junk', '\spam'], + ]; + /** * Folder constructor. * @param Client $client @@ -235,7 +251,7 @@ public function getChildren(): FolderCollection { */ protected function decodeName($name): string|array|bool|null { $parts = []; - foreach (explode($this->delimiter, $name) as $item) { + foreach(explode($this->delimiter, $name) as $item) { $parts[] = EncodingAliases::convert($item, "UTF7-IMAP", "UTF-8"); } @@ -264,6 +280,14 @@ protected function parseAttributes($attributes): void { $this->marked = in_array('\Marked', $attributes); $this->referral = in_array('\Referral', $attributes); $this->has_children = in_array('\HasChildren', $attributes); + + array_map(function($el) { + foreach(self::SPECIAL_ATTRIBUTES as $key => $attribute) { + if(in_array(strtolower($el), $attribute)){ + $this->attributes[] = $key; + } + } + }, $attributes); } /** @@ -284,7 +308,7 @@ protected function parseAttributes($attributes): void { public function move(string $new_name, bool $expunge = true): array { $this->client->checkConnection(); $status = $this->client->getConnection()->renameFolder($this->full_name, $new_name)->validatedData(); - if ($expunge) $this->client->expunge(); + if($expunge) $this->client->expunge(); $folder = $this->client->getFolder($new_name); $event = $this->getEvent("folder", "moved"); @@ -336,7 +360,7 @@ public function appendMessage(string $message, array $options = null, Carbon|str * date string that conforms to the rfc2060 specifications for a date_time value or be a Carbon object. */ - if ($internal_date instanceof Carbon) { + if($internal_date instanceof Carbon){ $internal_date = $internal_date->format('d-M-Y H:i:s O'); } @@ -377,11 +401,11 @@ public function rename(string $new_name, bool $expunge = true): array { */ public function delete(bool $expunge = true): array { $status = $this->client->getConnection()->deleteFolder($this->path)->validatedData(); - if ($this->client->getActiveFolder() == $this->path){ + if($this->client->getActiveFolder() == $this->path){ $this->client->setActiveFolder(null); } - if ($expunge) $this->client->expunge(); + if($expunge) $this->client->expunge(); $event = $this->getEvent("folder", "deleted"); $event::dispatch($this); @@ -437,7 +461,7 @@ public function unsubscribe(): array { public function idle(callable $callback, int $timeout = 300): void { $this->client->setTimeout($timeout); - if (!in_array("IDLE", $this->client->getConnection()->getCapabilities()->validatedData())) { + if(!in_array("IDLE", $this->client->getConnection()->getCapabilities()->validatedData())){ throw new Exceptions\NotSupportedCapabilityException("IMAP server does not support IDLE"); } @@ -450,15 +474,15 @@ public function idle(callable $callback, int $timeout = 300): void { $sequence = ClientManager::get('options.sequence', IMAP::ST_MSGN); - while (true) { + while(true) { // This polymorphic call is fine - Protocol::idle() will throw an exception beforehand $line = $idle_client->getConnection()->nextLine(Response::empty()); - if (($pos = strpos($line, "EXISTS")) !== false) { + if(($pos = strpos($line, "EXISTS")) !== false){ $msgn = (int)substr($line, 2, $pos - 2); // Check if the stream is still alive or should be considered stale - if (!$this->client->isConnected() || $last_action->isBefore(Carbon::now())) { + if(!$this->client->isConnected() || $last_action->isBefore(Carbon::now())){ // Reset the connection before interacting with it. Otherwise, the resource might be stale which // would result in a stuck interaction. If you know of a way of detecting a stale resource, please // feel free to improve this logic. I tried a lot but nothing seem to work reliably... @@ -582,7 +606,7 @@ public function getClient(): Client { * @param $delimiter */ public function setDelimiter($delimiter): void { - if (in_array($delimiter, [null, '', ' ', false]) === true) { + if(in_array($delimiter, [null, '', ' ', false]) === true){ $delimiter = ClientManager::get('options.delimiter', '/'); }