comparison plugins/identity_select/identity_select.php @ 0:1e000243b222

vanilla 1.3.3 distro, I hope
author Charlie Root
date Thu, 04 Jan 2018 15:50:29 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1e000243b222
1 <?php
2
3 /**
4 * Identity selection based on additional message headers.
5 *
6 * On reply to a message user identity selection is based on
7 * content of standard headers i.e. From, To, Cc and Return-Path.
8 * Here you can add header(s) set by your SMTP server (e.g.
9 * Delivered-To, Envelope-To, X-Envelope-To, X-RCPT-TO) to make
10 * identity selection more accurate.
11 *
12 * Enable the plugin in config.inc.php and add your desired headers:
13 * $config['identity_select_headers'] = array('Delivered-To');
14 *
15 * Note: 'Received' header is also supported, but has bigger impact
16 * on performance, as it's body is potentially much bigger
17 * than other headers used by Roundcube
18 *
19 * @author Aleksander Machniak <alec@alec.pl>
20 * @license GNU GPLv3+
21 */
22 class identity_select extends rcube_plugin
23 {
24 public $task = 'mail';
25
26
27 function init()
28 {
29 $this->add_hook('identity_select', array($this, 'select'));
30 $this->add_hook('storage_init', array($this, 'storage_init'));
31 }
32
33 /**
34 * Adds additional headers to supported headers list
35 */
36 function storage_init($p)
37 {
38 $rcmail = rcmail::get_instance();
39
40 if ($add_headers = (array)$rcmail->config->get('identity_select_headers', array())) {
41 $p['fetch_headers'] = trim($p['fetch_headers'] . ' ' . strtoupper(join(' ', $add_headers)));
42 }
43
44 return $p;
45 }
46
47 /**
48 * Identity selection
49 */
50 function select($p)
51 {
52 if ($p['selected'] !== null || !is_object($p['message']->headers)) {
53 return $p;
54 }
55
56 $rcmail = rcmail::get_instance();
57
58 foreach ((array)$rcmail->config->get('identity_select_headers', array()) as $header) {
59 if ($emails = $this->get_email_from_header($p['message'], $header)) {
60 foreach ($p['identities'] as $idx => $ident) {
61 if (in_array($ident['email_ascii'], $emails)) {
62 $p['selected'] = $idx;
63 break 2;
64 }
65 }
66 }
67 }
68
69 return $p;
70 }
71
72 /**
73 * Extract email address from specified message header
74 */
75 protected function get_email_from_header($message, $header)
76 {
77 $value = $message->headers->get($header, false);
78
79 if (strtolower($header) == 'received') {
80 // find first email address in all Received headers
81 $email = null;
82 foreach ((array) $value as $entry) {
83 if (preg_match('/[\s\t]+for[\s\t]+<([^>]+)>/', $entry, $matches)) {
84 $email = $matches[1];
85 break;
86 }
87 }
88
89 $value = $email;
90 }
91
92 return (array) $value;
93 }
94 }