2023-11-29 12:41:51 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class easy_unsubscribe extends rcube_plugin {
|
|
|
|
|
|
|
|
public $task = 'mail';
|
|
|
|
private $message_headers_done = false;
|
|
|
|
private $unsubscribe_img;
|
|
|
|
|
|
|
|
function init() {
|
2024-11-07 11:31:12 +02:00
|
|
|
ini_set('display_errors', '1');
|
|
|
|
ini_set('display_startup_errors', '1');
|
|
|
|
error_reporting(E_ALL);
|
2023-11-29 12:41:51 -05:00
|
|
|
|
|
|
|
$rcmail = rcmail::get_instance();
|
|
|
|
$layout = $rcmail->config->get('layout');
|
|
|
|
|
|
|
|
$this->add_hook('message_headers_output', array($this, 'message_headers'));
|
|
|
|
$this->add_hook('storage_init', array($this, 'storage_init'));
|
|
|
|
|
|
|
|
$this->include_stylesheet('easy_unsubscribe.css');
|
|
|
|
$this->include_script('easy_unsubscribe.js');
|
|
|
|
|
|
|
|
$this->add_texts('localization/');
|
|
|
|
|
|
|
|
$rcmail->output->add_label('easy_unsubscribe.confirm');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function storage_init($p) {
|
2024-11-11 12:35:47 +02:00
|
|
|
$p['fetch_raw_body'] = true;
|
2023-11-29 12:41:51 -05:00
|
|
|
$p['fetch_headers'] = trim($p['fetch_headers'] . ' ' . strtoupper('List-Unsubscribe'));
|
|
|
|
return $p;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-11-07 11:31:12 +02:00
|
|
|
public function decodeMimeHeader($header) {
|
|
|
|
$elements = imap_mime_header_decode($header);
|
|
|
|
|
|
|
|
return array_reduce($elements, function ($carry, $element) {
|
|
|
|
return $carry . $element->text;
|
|
|
|
}, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function extractUnsubscribeUrls($content) {
|
2024-11-11 12:35:47 +02:00
|
|
|
// Array to store all patterns we want to match
|
|
|
|
$patterns = [
|
|
|
|
// Standard <a> tag pattern
|
2024-11-11 21:18:29 +02:00
|
|
|
'/<a\s+(?:[^>]*?\s+)?href=(["\'])((?:https?:)?\/\/[^"\']*?(?:unsubscribe)[^"\']*)\1/i',
|
2024-11-11 12:35:47 +02:00
|
|
|
|
|
|
|
// Square bracket pattern [URL] common in email templates
|
2024-11-11 21:18:29 +02:00
|
|
|
'/\[\s*((?:https?:)?\/\/[^\]]*?(?:unsubscribe)[^\]]*)\s*\]/i',
|
2024-11-11 12:35:47 +02:00
|
|
|
|
|
|
|
// Bare URLs with unsubscribe/esclick
|
2024-11-11 21:18:29 +02:00
|
|
|
'/((?:https?:)?\/\/[^\s<>\[\]"\']*?(?:unsubscribe)[^\s<>\[\]"\']*)/i'
|
2024-11-11 12:35:47 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
$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));
|
2024-11-07 11:31:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-11-11 12:35:47 +02:00
|
|
|
|
2024-11-07 11:31:12 +02:00
|
|
|
public function message_headers($p) {
|
|
|
|
if($this->message_headers_done === false) {
|
2023-11-29 12:41:51 -05:00
|
|
|
|
|
|
|
$this->message_headers_done = true;
|
|
|
|
|
2024-11-07 11:31:12 +02:00
|
|
|
$urls = [];
|
2023-11-29 12:41:51 -05:00
|
|
|
|
2024-11-07 11:31:12 +02:00
|
|
|
$rcmail = rcmail::get_instance();
|
2024-11-11 12:35:47 +02:00
|
|
|
$body = quoted_printable_decode($rcmail->storage->get_raw_body($p['uid']));
|
2023-11-29 12:41:51 -05:00
|
|
|
|
2024-11-07 11:31:12 +02:00
|
|
|
$urls = array_merge($urls, $this->extractUnsubscribeUrls($body));
|
|
|
|
|
2024-11-11 21:18:29 +02:00
|
|
|
$ListUnsubscribe = $this->decodeMimeHeader($p['headers']->others['list-unsubscribe'] ?? '');
|
2024-11-07 11:31:12 +02:00
|
|
|
if (preg_match_all('/<(.+)>/mU', $ListUnsubscribe, $items, PREG_PATTERN_ORDER)) {
|
|
|
|
foreach ( $items[1] as $uri ) {
|
|
|
|
$urls[] = $uri;
|
2023-11-29 12:41:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-07 11:31:12 +02:00
|
|
|
$urls = array_unique($urls);
|
|
|
|
|
2024-11-11 21:18:29 +02:00
|
|
|
$exclude_strings = ['?', 'esputnik', 'unsubscribe/'];
|
|
|
|
|
2024-11-07 11:31:12 +02:00
|
|
|
foreach ($urls as $uri) {
|
|
|
|
if (str_contains($uri, 'mailto')) continue;
|
2024-11-11 21:18:29 +02:00
|
|
|
|
|
|
|
$should_continue = false;
|
|
|
|
foreach ($exclude_strings as $exclude_string) {
|
|
|
|
if (str_contains($uri, $exclude_string)) {
|
|
|
|
$should_continue = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$should_continue) continue;
|
|
|
|
|
2024-11-07 11:31:12 +02:00
|
|
|
|
|
|
|
$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>';
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:41:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($p['output']['subject'])) {
|
|
|
|
$p['output']['subject']['value'] = $p['output']['subject']['value'] . $this->unsubscribe_img;
|
|
|
|
$p['output']['subject']['html'] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $p;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-01-21 12:15:09 -05:00
|
|
|
}
|