Update easy_unsubscribe.php

This commit is contained in:
Illya Marchenko 2024-11-11 12:35:47 +02:00
parent 636d43e97e
commit 2a89cac244
Signed by: Gitea
GPG Key ID: 81CE9C258E973E3A

@ -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 <a> tag pattern
'/<a\s+(?:[^>]*?\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 .= '<a class="easy_unsubscribe_link" title="'.$uri.'" data-href="'. htmlentities($uri) .'" target="_blank" onclick="easy_unsubscribe_click(this);"><img src="plugins/easy_unsubscribe/icon.png" alt="' . $this->gettext('unsubscribe') . '" /></a>';
}