Skip to content

Commit b6f07fa

Browse files
committed
connection validation added
1 parent 9c984af commit b6f07fa

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/IMAP/Client.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public function isReadOnly() {
188188
* @throws ConnectionFailedException
189189
*/
190190
public function checkConnection() {
191-
if (!$this->isConnected()) {
191+
if (!$this->isConnected() || $this->connection === false) {
192192
$this->connect();
193193
}
194194
}
@@ -230,7 +230,7 @@ public function connect($attempts = 3) {
230230
* @return $this
231231
*/
232232
public function disconnect() {
233-
if ($this->isConnected()) {
233+
if ($this->isConnected() && $this->connection !== false) {
234234
$this->errors = array_merge($this->errors, imap_errors() ?: []);
235235
$this->connected = !imap_close($this->connection, CL_EXPUNGE);
236236
}
@@ -318,8 +318,11 @@ public function openFolder(Folder $folder, $attempts = 3) {
318318
* @param string $name
319319
*
320320
* @return bool
321+
* @throws ConnectionFailedException
321322
*/
322323
public function createFolder($name) {
324+
$this->checkConnection();
325+
323326
return imap_createmailbox($this->connection, imap_utf7_encode($name));
324327
}
325328

@@ -420,8 +423,10 @@ protected function getAddress() {
420423
* Retrieve the quota level settings, and usage statics per mailbox
421424
*
422425
* @return array
426+
* @throws ConnectionFailedException
423427
*/
424428
public function getQuota() {
429+
$this->checkConnection();
425430
return imap_get_quota($this->connection, 'user.'.$this->username);
426431
}
427432

@@ -431,26 +436,32 @@ public function getQuota() {
431436
* @param string $quota_root
432437
*
433438
* @return array
439+
* @throws ConnectionFailedException
434440
*/
435441
public function getQuotaRoot($quota_root = 'INBOX') {
442+
$this->checkConnection();
436443
return imap_get_quotaroot($this->connection, $quota_root);
437444
}
438445

439446
/**
440447
* Gets the number of messages in the current mailbox
441448
*
442449
* @return int
450+
* @throws ConnectionFailedException
443451
*/
444452
public function countMessages() {
453+
$this->checkConnection();
445454
return imap_num_msg($this->connection);
446455
}
447456

448457
/**
449458
* Gets the number of recent messages in current mailbox
450459
*
451460
* @return int
461+
* @throws ConnectionFailedException
452462
*/
453463
public function countRecentMessages() {
464+
$this->checkConnection();
454465
return imap_num_recent($this->connection);
455466
}
456467

@@ -487,8 +498,10 @@ public function getLastError() {
487498
* Delete all messages marked for deletion
488499
*
489500
* @return bool
501+
* @throws ConnectionFailedException
490502
*/
491503
public function expunge() {
504+
$this->checkConnection();
492505
return imap_expunge($this->connection);
493506
}
494507

@@ -502,8 +515,10 @@ public function expunge() {
502515
* Nmsgs [int(1)] number of messages in the mailbox
503516
* Recent [int(0)] number of recent messages in the mailbox
504517
* }
518+
* @throws ConnectionFailedException
505519
*/
506520
public function checkCurrentMailbox() {
521+
$this->checkConnection();
507522
return imap_check($this->connection);
508523
}
509524
}

0 commit comments

Comments
 (0)