From 2a89cac244e058865e61f9417d0c62dd6cf127ae Mon Sep 17 00:00:00 2001 From: Illya Marchenko Date: Mon, 11 Nov 2024 12:35:47 +0200 Subject: [PATCH] Update easy_unsubscribe.php --- easy_unsubscribe.php | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/easy_unsubscribe.php b/easy_unsubscribe.php index 7690c08..2246f73 100644 --- a/easy_unsubscribe.php +++ b/easy_unsubscribe.php @@ -26,7 +26,7 @@ class easy_unsubscribe extends rcube_plugin { } public function storage_init($p) { - $p['fetch_body'] = true; + $p['fetch_raw_body'] = true; $p['fetch_headers'] = trim($p['fetch_headers'] . ' ' . strtoupper('List-Unsubscribe')); return $p; @@ -41,12 +41,41 @@ class easy_unsubscribe extends rcube_plugin { } public function extractUnsubscribeUrls($content) { - $pattern = '/https?:\/\/[^\s"\'<>]+\/[^\s"\'<>]*unsubscribe[^\s"\'<>]*/i'; - preg_match_all($pattern, $content, $matches); - return $matches[0]; + // Array to store all patterns we want to match + $patterns = [ + // Standard tag pattern + '/]*?\s+)?href=(["\'])((?:https?:)?\/\/[^"\']*?(?:unsubscribe|dummy-esclick)[^"\']*)\1/i', + + // Square bracket pattern [URL] common in email templates + '/\[\s*((?:https?:)?\/\/[^\]]*?(?:unsubscribe|dummy-esclick)[^\]]*)\s*\]/i', + + // Bare URLs with unsubscribe/esclick + '/((?:https?:)?\/\/[^\s<>\[\]"\']*?(?:unsubscribe|dummy-esclick)[^\s<>\[\]"\']*)/i' + ]; + + $allUrls = []; + foreach ($patterns as $pattern) { + $matches = []; + preg_match_all($pattern, $content, $matches); + + // For the first pattern, we want group 2, for others group 1 + $groupIndex = (strpos($pattern, 'href') !== false) ? 2 : 1; + if (!empty($matches[$groupIndex])) { + $allUrls = array_merge($allUrls, $matches[$groupIndex]); + } + } + + // Remove duplicates and clean URLs + $allUrls = array_unique($allUrls); + $cleanUrls = array_map(function($url) { + return trim($url); + }, $allUrls); + + return array_values(array_filter($cleanUrls)); } + public function message_headers($p) { if($this->message_headers_done === false) { @@ -56,7 +85,7 @@ class easy_unsubscribe extends rcube_plugin { $urls = []; $rcmail = rcmail::get_instance(); - $body = $rcmail->storage->get_body($p['uid']); + $body = quoted_printable_decode($rcmail->storage->get_raw_body($p['uid'])); $urls = array_merge($urls, $this->extractUnsubscribeUrls($body)); @@ -71,7 +100,7 @@ class easy_unsubscribe extends rcube_plugin { foreach ($urls as $uri) { if (str_contains($uri, 'mailto')) continue; - if (!str_contains($uri, '?')) continue; + if (!str_contains($uri, '?') && !str_contains($uri, 'esputnik')) continue; $this->unsubscribe_img .= '' . $this->gettext('unsubscribe') . ''; }