diff --git a/cleanarchivedemails.xml b/cleanarchivedemails.xml index 977a5f8..cf96500 100644 --- a/cleanarchivedemails.xml +++ b/cleanarchivedemails.xml @@ -23,7 +23,7 @@ - 2.0.0 + 2.0.2 9.5 diff --git a/inc/mailcollector.class.php b/inc/mailcollector.class.php index 853020f..396f0b8 100644 --- a/inc/mailcollector.class.php +++ b/inc/mailcollector.class.php @@ -5,7 +5,7 @@ * * mailcollector description. * - * @author morono + * @author Tomolimo */ use Laminas\Mail; @@ -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) { @@ -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 ''; } @@ -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); @@ -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) { @@ -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'); + } } } diff --git a/setup.php b/setup.php index 481dccf..160930a 100644 --- a/setup.php +++ b/setup.php @@ -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