Skip to content

Commit b509884

Browse files
committed
Client connection path handling changed to be handled inside the calling method #31
1 parent a02a925 commit b509884

File tree

5 files changed

+58
-12
lines changed

5 files changed

+58
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
88
### Fixed
99
- Problem with Message::moveToFolder() and multiple moves #31
1010
- Problem with encoding conversion #203
11+
- Message null value attribute problem fixed
12+
- Client connection path handling changed to be handled inside the calling method #31
1113

1214
### Added
1315
- Mailbox fetching exception added #201
1416
- Message::moveToFolder() fetches new Message::class afterwards #31
1517

1618
### Affected Classes
1719
- [Message::class](src/IMAP/Message.php)
20+
- [Folder::class](src/IMAP/Folder.php)
21+
- [Client::class](src/IMAP/Client.php)
1822

1923
### Breaking changes
2024
- Message::moveToFolder() returns either a Message::class instance or null and not a boolean

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ if you're just wishing a feature ;)
578578
| disconnect | | | Disconnect from server. |
579579
| getFolder | string $folder_name, int $attributes = 32, int or null $delimiter | Folder | Get a Folder instance by name |
580580
| getFolders | bool $hierarchical, string or null $parent_folder | FolderCollection | Get folders list. If hierarchical order is set to true, it will make a tree of folders, otherwise it will return flat array. |
581-
| openFolder | Folder $folder, integer $attempts | | Open a given folder. |
581+
| openFolder | string or Folder $folder, integer $attempts | | Open a given folder. |
582582
| createFolder | string $name | boolean | Create a new folder. |
583583
| renameFolder | string $old_name, string $new_name | boolean | Rename a folder. |
584584
| deleteFolder | string $name | boolean | Delete a folder. |
@@ -600,6 +600,7 @@ if you're just wishing a feature ;)
600600
| getDefaultMessageMask | | string | Get the current default message mask class name |
601601
| setDefaultAttachmentMask | string $mask | self | Set the default attachment mask class |
602602
| getDefaultAttachmentMask | | string | Get the current default attachment mask class name |
603+
| getFolderPath | | string | Get the current folder path |
603604

604605
### [Message::class](src/IMAP/Message.php)
605606

src/IMAP/Client.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -381,18 +381,22 @@ public function getFolders($hierarchical = true, $parent_folder = null) {
381381
/**
382382
* Open folder.
383383
*
384-
* @param Folder $folder
385-
* @param int $attempts
384+
* @param string|Folder $folder_path
385+
* @param int $attempts
386386
*
387387
* @throws ConnectionFailedException
388388
*/
389-
public function openFolder(Folder $folder, $attempts = 3) {
389+
public function openFolder($folder_path, $attempts = 3) {
390390
$this->checkConnection();
391391

392-
if ($this->active_folder !== $folder) {
393-
$this->active_folder = $folder;
392+
if(property_exists($folder_path, 'path')) {
393+
$folder_path = $folder_path->path;
394+
}
395+
396+
if ($this->active_folder !== $folder_path) {
397+
$this->active_folder = $folder_path;
394398

395-
imap_reopen($this->getConnection(), $folder->path, $this->getOptions(), $attempts);
399+
imap_reopen($this->getConnection(), $folder_path, $this->getOptions(), $attempts);
396400
}
397401
}
398402

@@ -722,4 +726,13 @@ public function setDefaultAttachmentMask($mask) {
722726

723727
throw new MaskNotFoundException("Unknown mask provided: ".$mask);
724728
}
729+
730+
/**
731+
* Get the current active folder
732+
*
733+
* @return Folder
734+
*/
735+
public function getFolderPath(){
736+
return $this->active_folder;
737+
}
725738
}

src/IMAP/Folder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function __construct(Client $client, $structure) {
133133
*/
134134
public function query($charset = 'UTF-8'){
135135
$this->getClient()->checkConnection();
136-
$this->getClient()->openFolder($this);
136+
$this->getClient()->openFolder($this->path);
137137

138138
return new WhereQuery($this->getClient(), $charset);
139139
}
@@ -191,6 +191,7 @@ public function setChildren($children = []) {
191191
* @throws Exceptions\InvalidMessageDateException
192192
*/
193193
public function getMessage($uid, $msglist = null, $fetch_options = null, $fetch_body = false, $fetch_attachment = false, $fetch_flags = true) {
194+
$this->client->openFolder($this->path);
194195
if (imap_msgno($this->getClient()->getConnection(), $uid) > 0) {
195196
return new Message($uid, $msglist, $this->getClient(), $fetch_options, $fetch_body, $fetch_attachment, $fetch_flags);
196197
}

src/IMAP/Message.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ class Message {
110110
'priority' => 0,
111111
];
112112

113+
/**
114+
* The message folder path
115+
*
116+
* @var string $folder_path
117+
*/
118+
protected $folder_path;
119+
113120
/**
114121
* Fetch body options
115122
*
@@ -193,6 +200,8 @@ public function __construct($uid, $msglist, Client $client, $fetch_options = nul
193200
$this->mask = $default_mask;
194201
}
195202

203+
$this->folder_path = $client->getFolderPath();
204+
196205
$this->config = config('imap.options');
197206

198207
$this->setFetchOption($fetch_options);
@@ -285,6 +294,7 @@ public function __get($name) {
285294
* @throws Exceptions\ConnectionFailedException
286295
*/
287296
public function copy($mailbox, $options = 0) {
297+
$this->client->openFolder($this->folder_path);
288298
return imap_mail_copy($this->client->getConnection(), $this->msglist, $mailbox, $options);
289299
}
290300

@@ -298,6 +308,7 @@ public function copy($mailbox, $options = 0) {
298308
* @throws Exceptions\ConnectionFailedException
299309
*/
300310
public function move($mailbox, $options = 0) {
311+
$this->client->openFolder($this->folder_path);
301312
return imap_mail_move($this->client->getConnection(), $this->msglist, $mailbox, $options);
302313
}
303314

@@ -375,6 +386,7 @@ public function getHTMLBody($replaceImages = false) {
375386
* @throws InvalidMessageDateException
376387
*/
377388
private function parseHeader() {
389+
$this->client->openFolder($this->folder_path);
378390
$this->header = $header = imap_fetchheader($this->client->getConnection(), $this->uid, IMAP::FT_UID);
379391

380392
$this->priority = $this->extractPriority($this->header);
@@ -515,6 +527,7 @@ private function parseDate($header) {
515527
private function parseFlags() {
516528
$this->flags = FlagCollection::make([]);
517529

530+
$this->client->openFolder($this->folder_path);
518531
$flags = imap_fetch_overview($this->client->getConnection(), $this->uid, IMAP::FT_UID);
519532
if (is_array($flags) && isset($flags[0])) {
520533
foreach($this->available_flags as $flag) {
@@ -548,7 +561,7 @@ private function parseFlag($flags, $flag) {
548561
*/
549562
public function getHeaderInfo() {
550563
if ($this->header_info == null) {
551-
$this->header_info =
564+
$this->client->openFolder($this->folder_path);
552565
$this->header_info = imap_headerinfo($this->client->getConnection(), $this->getMessageNo());
553566
}
554567

@@ -568,7 +581,6 @@ private function extractHeaderAddressPart($header, $part) {
568581

569582
/**
570583
* Parse Addresses
571-
*
572584
* @param $list
573585
*
574586
* @return array
@@ -612,6 +624,7 @@ private function parseAddresses($list) {
612624
* @throws Exceptions\ConnectionFailedException
613625
*/
614626
public function parseBody() {
627+
$this->client->openFolder($this->folder_path);
615628
$this->structure = imap_fetchstructure($this->client->getConnection(), $this->uid, IMAP::FT_UID);
616629

617630
if(property_exists($this->structure, 'parts')){
@@ -645,6 +658,8 @@ public function parseBody() {
645658
* @throws Exceptions\ConnectionFailedException
646659
*/
647660
private function fetchStructure($structure, $partNumber = null) {
661+
$this->client->openFolder($this->folder_path);
662+
648663
if ($structure->type == IMAP::MESSAGE_TYPE_TEXT &&
649664
($structure->ifdisposition == 0 ||
650665
($structure->ifdisposition == 1 && !isset($structure->parts) && $partNumber == null)
@@ -917,7 +932,7 @@ public function getContainingFolder(Folder $folder = null) {
917932

918933
// Try finding the message by uid in the current folder
919934
$client = new Client;
920-
$client->openFolder($folder);
935+
$client->openFolder($folder->path);
921936
$uidMatches = imap_fetch_overview($client->getConnection(), $this->uid, IMAP::FT_UID);
922937
$uidMatch = count($uidMatches)
923938
? new Message($uidMatches[0]->uid, $uidMatches[0]->msgno, $client)
@@ -961,10 +976,12 @@ public function moveToFolder($mailbox = 'INBOX', $expunge = false, $create_folde
961976
$target_folder = $this->client->getFolder($mailbox);
962977
$target_status = $target_folder->getStatus(IMAP::SA_ALL);
963978

979+
$this->client->openFolder($this->folder_path);
964980
$status = imap_mail_move($this->client->getConnection(), $this->uid, $mailbox, IMAP::CP_UID);
981+
965982
if($status === true){
966983
if($expunge) $this->client->expunge();
967-
$this->client->openFolder($target_folder);
984+
$this->client->openFolder($target_folder->path);
968985

969986
return $target_folder->getMessage($target_status->uidnext);
970987
}
@@ -980,6 +997,8 @@ public function moveToFolder($mailbox = 'INBOX', $expunge = false, $create_folde
980997
* @throws Exceptions\ConnectionFailedException
981998
*/
982999
public function delete($expunge = true) {
1000+
$this->client->openFolder($this->folder_path);
1001+
9831002
$status = imap_delete($this->client->getConnection(), $this->uid, IMAP::FT_UID);
9841003
if($expunge) $this->client->expunge();
9851004

@@ -994,6 +1013,8 @@ public function delete($expunge = true) {
9941013
* @throws Exceptions\ConnectionFailedException
9951014
*/
9961015
public function restore($expunge = true) {
1016+
$this->client->openFolder($this->folder_path);
1017+
9971018
$status = imap_undelete($this->client->getConnection(), $this->uid, IMAP::FT_UID);
9981019
if($expunge) $this->client->expunge();
9991020

@@ -1026,6 +1047,8 @@ public function hasAttachments() {
10261047
* @throws Exceptions\ConnectionFailedException
10271048
*/
10281049
public function setFlag($flag) {
1050+
$this->client->openFolder($this->folder_path);
1051+
10291052
$flag = "\\".trim(is_array($flag) ? implode(" \\", $flag) : $flag);
10301053
$status = imap_setflag_full($this->client->getConnection(), $this->getUid(), $flag, SE_UID);
10311054
$this->parseFlags();
@@ -1041,6 +1064,8 @@ public function setFlag($flag) {
10411064
* @throws Exceptions\ConnectionFailedException
10421065
*/
10431066
public function unsetFlag($flag) {
1067+
$this->client->openFolder($this->folder_path);
1068+
10441069
$flag = "\\".trim(is_array($flag) ? implode(" \\", $flag) : $flag);
10451070
$status = imap_clearflag_full($this->client->getConnection(), $this->getUid(), $flag, SE_UID);
10461071
$this->parseFlags();
@@ -1054,6 +1079,8 @@ public function unsetFlag($flag) {
10541079
*/
10551080
public function getRawBody() {
10561081
if ($this->raw_body === null) {
1082+
$this->client->openFolder($this->folder_path);
1083+
10571084
$this->raw_body = imap_fetchbody($this->client->getConnection(), $this->getUid(), '', $this->fetch_options | IMAP::FT_UID);
10581085
}
10591086

0 commit comments

Comments
 (0)