Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,10 @@ public function connect() {
$this->connection->enableDebug();
}

if (!ClientManager::get('options.uid_cache')) {
$this->connection->disableUidCache();
}

try {
$this->connection->connect($this->host, $this->port);
} catch (ErrorException $e) {
Expand Down
37 changes: 27 additions & 10 deletions src/Connection/Protocols/ImapProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,9 @@ public function logout() {
} catch (Exception $e) {}
fclose($this->stream);
$this->stream = null;
$this->uid_cache = null;
}

return $result;
}

Expand Down Expand Up @@ -513,6 +515,8 @@ public function examineOrSelect($command = 'EXAMINE', $folder = 'INBOX') {
* @throws RuntimeException
*/
public function selectFolder($folder = 'INBOX') {
$this->uid_cache = null;

return $this->examineOrSelect('SELECT', $folder);
}

Expand Down Expand Up @@ -678,18 +682,31 @@ public function flags($uids, $uid = IMAP::ST_UID){
* @throws MessageNotFoundException
*/
public function getUid($id = null) {
try {
$uids = $this->fetch('UID', 1, INF);
if ($id == null) {
return $uids;
}

foreach ($uids as $k => $v) {
if ($k == $id) {
return $v;
}
if ($this->enable_uid_cache && $this->uid_cache) {
$uids = $this->uid_cache;
} else {
try {
$uids = $this->fetch('UID', 1, INF);
$this->setUidCache($uids); // set cache for this folder
} catch (RuntimeException $e) {}
}

if ($id == null) {
return $uids;
}

foreach ($uids as $k => $v) {
if ($k == $id) {
return $v;
}
} catch (RuntimeException $e) {}
}

// clear uid cache and run method again
if ($this->enable_uid_cache && $this->uid_cache) {
$this->setUidCache(null);
return $this->getUid($id);
}

throw new MessageNotFoundException('unique id not found');
}
Expand Down
11 changes: 10 additions & 1 deletion src/Connection/Protocols/LegacyProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ public function logout() {
if ($this->stream) {
$result = \imap_close($this->stream, IMAP::CL_EXPUNGE);
$this->stream = false;
$this->uid_cache = null;
return $result;
}
return false;
Expand Down Expand Up @@ -181,6 +182,7 @@ public function getCapabilities() {
*/
public function selectFolder($folder = 'INBOX') {
\imap_reopen($this->stream, $folder, IMAP::OP_READONLY, 3);
$this->uid_cache = null;
return $this->examineFolder($folder);
}

Expand Down Expand Up @@ -275,13 +277,20 @@ public function flags($uids, $uid = IMAP::ST_UID){
*/
public function getUid($id = null) {
if ($id === null) {
if ($this->enable_uid_cache && $this->uid_cache) {
return $this->uid_cache;
}

$overview = $this->overview("1:*");
$uids = [];
foreach($overview as $set){
$uids[$set->msgno] = $set->uid;
}

$this->setUidCache($uids);
return $uids;
}

return \imap_uid($this->stream, $id);
}

Expand Down Expand Up @@ -618,4 +627,4 @@ public function setProtocol($protocol) {
$this->protocol = $protocol;
return $this;
}
}
}
42 changes: 41 additions & 1 deletion src/Connection/Protocols/Protocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ abstract class Protocol implements ProtocolInterface {
*/
protected $debug = false;

/**
* @var boolean
*/
protected $enable_uid_cache = true;

/**
* @var false|resource
*/
Expand Down Expand Up @@ -60,6 +65,13 @@ abstract class Protocol implements ProtocolInterface {
'password' => null,
];

/**
* Cache for uid of active folder.
*
* @var null|array
*/
protected $uid_cache = null;

/**
* Get an available cryptographic method
*
Expand Down Expand Up @@ -242,4 +254,32 @@ public function buildUIDCommand($command, $uid) {
return trim($this->getUIDKey($uid)." ".$command);
}

}
/**
* Set the uid cache of current active folder
*
* @param array|null $uids
*/
public function setUidCache($uids) {
if (is_null($uids)) {
$this->uid_cache = null;
return;
}

$messageNumber = 1;

$uid_cache = [];
foreach ($uids as $uid) {
$uid_cache[$messageNumber++] = $uid;
}

$this->uid_cache = $uid_cache;
}

public function enableUidCache() {
$this->enable_uid_cache = true;
}

public function disableUidCache() {
$this->enable_uid_cache = false;
}
}
20 changes: 19 additions & 1 deletion src/Connection/Protocols/ProtocolInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Webklex\PHPIMAP\Connection\Protocols;

use ErrorException;
use Webklex\PHPIMAP\Client;
use Webklex\PHPIMAP\Exceptions\AuthFailedException;
use Webklex\PHPIMAP\Exceptions\ConnectionFailedException;
use Webklex\PHPIMAP\Exceptions\InvalidMessageDateException;
Expand Down Expand Up @@ -392,4 +393,21 @@ public function enableDebug();
* Disable the debug mode
*/
public function disableDebug();
}

/**
* Enable uid caching
*/
public function enableUidCache();

/**
* Disable uid caching
*/
public function disableUidCache();

/**
* Set the uid cache of current active folder
*
* @param array|null $uids
*/
public function setUidCache($uids);
}
1 change: 1 addition & 0 deletions src/config/imap.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
'soft_fail' => false,
'rfc822' => true,
'debug' => false,
'uid_cache' => true,
'boundary' => '/boundary=(.*?(?=;)|(.*))/i',
'message_key' => 'list',
'fetch_order' => 'asc',
Expand Down