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) { $p['fetch_body'] = true; $p['fetch_headers'] = trim($p['fetch_headers'] . ' ' . strtoupper('List-Unsubscribe')); return $p; } 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) { $pattern = '/https?:\/\/[^\s"\'<>]+\/[^\s"\'<>]*unsubscribe[^\s"\'<>]*/i'; preg_match_all($pattern, $content, $matches); return $matches[0]; } public function message_headers($p) { if($this->message_headers_done === false) { $this->message_headers_done = true; if(!isset($p['headers']->others['list-unsubscribe'])) return $p; $urls = []; $rcmail = rcmail::get_instance(); $body = $rcmail->storage->get_body($p['uid']); $urls = array_merge($urls, $this->extractUnsubscribeUrls($body)); $ListUnsubscribe = $this->decodeMimeHeader($p['headers']->others['list-unsubscribe']); if (preg_match_all('/<(.+)>/mU', $ListUnsubscribe, $items, PREG_PATTERN_ORDER)) { foreach ( $items[1] as $uri ) { $urls[] = $uri; } } $urls = array_unique($urls); foreach ($urls as $uri) { if (str_contains($uri, 'mailto')) continue; if (!str_contains($uri, '?')) continue; $this->unsubscribe_img .= '' . $this->gettext('unsubscribe') . ''; } } if(isset($p['output']['subject'])) { $p['output']['subject']['value'] = $p['output']['subject']['value'] . $this->unsubscribe_img; $p['output']['subject']['html'] = 1; } return $p; } }