Skip to content

Commit

Permalink
Fixed issue for "IMAP folder purge" tab that was not visible in case …
Browse files Browse the repository at this point in the history
…of oauth imap.

Fixed issue with null password that was not accepted by the connect method
Fixed the SEARCH method to SENTBEFORE instead of SEARCH BEFORE
Set version to 2.0.2
  • Loading branch information
tomolimo committed Jan 20, 2022
1 parent d3d5af7 commit 97bb9d7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion cleanarchivedemails.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</authors>
<versions>
<version>
<num>2.0.0</num>
<num>2.0.2</num>
<compatibility>9.5</compatibility>
</version>
<version>
Expand Down
68 changes: 34 additions & 34 deletions inc/mailcollector.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* mailcollector description.
*
* @author morono
* @author Tomolimo
*/

use Laminas\Mail;
Expand All @@ -18,26 +18,26 @@ class PluginCleanarchivedemailsMailCollector extends CommonDBTM {
/**
* Summary of connect
* @param array $config is the array returned by Toolbox::parseMailServerConnectString($host)
* @param string $login
* @param string $passwd
* @throws Exception
* @param string $login
* @param string|null $passwd
* @throws Exception
* @return bool|Mail\Protocol\Imap|null
*/
static function connect(array $config, string $login, string $passwd) {
static function connect(array $config, string $login, ?string $passwd) {

try {
$protocol = Toolbox::getMailServerProtocolInstance($config['type']);
if ($config['validate-cert'] === false) {
$protocol->setNoValidateCert(true);
}
$ssl = false;
if ($config['ssl']) {
$ssl = 'SSL';
}
if ($config['tls']) {
$ssl = 'TLS';
}
$protocol->connect($config['address'], $config['port'], $ssl);
if ($config['validate-cert'] === false) {
$protocol->setNoValidateCert(true);
}
$ssl = false;
if ($config['ssl']) {
$ssl = 'SSL';
}
if ($config['tls']) {
$ssl = 'TLS';
}
$protocol->connect($config['address'], $config['port'], $ssl);
$protocol->login($login, $passwd);

if ($protocol === null) {
Expand All @@ -57,11 +57,10 @@ static function connect(array $config, string $login, string $passwd) {
*/
function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) {

$host = Toolbox::parseMailServerConnectString($item->fields["host"]);

if ($host['type'] == "imap") {
return __('IMAP folder purge', 'cleanarchivedemails', 'cleanarchivedemails');
if (!strstr($item->fields['host'], "/pop")) {
return __('IMAP folder purge', 'cleanarchivedemails');
}

return '';
}

Expand Down Expand Up @@ -161,9 +160,9 @@ static function cronCleanArchivedEmails($task) {
$mailcollector->getFromDB($clean['mailcollectors_id']);
if ($mailcollector->fields['is_active'] == 1) {
$config = Toolbox::parseMailServerConnectString($mailcollector->fields["host"]);
if ($config['type'] == 'imap') {
if (!strstr($mailcollector->fields["host"], "/pop")) {
$protocol = self::connect($config, $mailcollector->fields["login"], Toolbox::sodiumDecrypt($mailcollector->fields['passwd']));

$volume = self::deleteEmailsFromFolder($protocol, $mailcollector->fields[MailCollector::ACCEPTED_FOLDER], $clean['days_before_clean_accepted_folder']);
if ($volume > 0) {
$task->addVolume($volume);
Expand All @@ -189,10 +188,10 @@ static function cronCleanArchivedEmails($task) {

/**
* Summary of deleteEmailsFromFolder
* @param Mail\Protocol\Imap $protocol
* @param string $folder
* @param int $days
* @throws Exception
* @param Mail\Protocol\Imap $protocol
* @param string $folder
* @param int $days
* @throws Exception
* @return int
*/
static function deleteEmailsFromFolder(Mail\Protocol\Imap $protocol, string $folder, int $days) {
Expand All @@ -202,21 +201,22 @@ static function deleteEmailsFromFolder(Mail\Protocol\Imap $protocol, string $fol
// select folder to perform the search
$protocol->select($folder);

$emails_to_delete = $protocol->search(['BEFORE', date("d-M-Y", strToTime("-$days days"))]);
// https://datatracker.ietf.org/doc/html/rfc9051#section-6.4.4
$emails_to_delete = $protocol->search(['SENTBEFORE', date("d-M-Y", strToTime("-$days days"))]);
if (is_array($emails_to_delete)) {
foreach ($emails_to_delete as $msg_id) {
// mark email as deleted
if (!$protocol->store([Mail\Storage::FLAG_DELETED], $msg_id, null, '+')) {
throw new \Exception('cannot set deleted flag');
}
if (!$protocol->store([Mail\Storage::FLAG_DELETED], $msg_id, null, '+')) {
throw new \Exception('cannot set deleted flag');
}

$volume++;
}

// expunge here
if (! $protocol->expunge()) {
throw new \Exception('message marked as deleted, but could not expunge');
}
// expunge here
if (!$protocol->expunge()) {
throw new \Exception('message marked as deleted, but could not expunge');
}
}

}
Expand Down
2 changes: 1 addition & 1 deletion setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
// ----------------------------------------------------------------------
// Original Author of file: Olivier Moron
// ----------------------------------------------------------------------
define ("PLUGIN_CLEANARCHIVESEMAILS_VERSION", "2.0.0");
define ("PLUGIN_CLEANARCHIVESEMAILS_VERSION", "2.0.2");

/**
* Init the hooks of the plugin
Expand Down

0 comments on commit 97bb9d7

Please sign in to comment.