Happy New Year
[squirrelmail.git] / plugins / listcommands / functions.php
CommitLineData
793566f0 1<?php
2
3/**
4 * functions.php
5 *
793566f0 6 * Implementation of RFC 2369 for SquirrelMail.
7 * When viewing a message from a mailinglist complying with this RFC,
8 * this plugin displays a menu which gives the user a choice of mailinglist
9 * commands such as (un)subscribe, help and list archives.
10 *
f197ec88 11 * @copyright 1999-2016 The SquirrelMail Project Team
4b4abf93 12 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
793566f0 13 * @version $Id$
14 * @package plugins
15 * @subpackage listcommands
16 */
17
e5f21a91 18/**
19 * Get current list of subscribed non-RFC-compliant mailing lists for logged-in user
20 *
21 * @return array The list of mailing list addresses, keyed by integer index
22 */
23function get_non_rfc_lists() {
24 global $username, $data_dir;
25 $lists = getPref($data_dir, $username, 'non_rfc_lists', array());
26 $new_lists = array();
27 if (!empty($lists)) {
28 $lists = explode(':', $lists);
29 foreach ($lists as $list) {
30 list($index, $list_addr) = explode('_', $list);
31 if ((!empty($index) || $index === '0') && !empty($list_addr))
32 $new_lists[$index] = $list_addr;
33 }
34 }
35 $lists = $new_lists;
36 sort($lists);
37 return $lists;
38}
39
40/**
41 * Show mailing list management option section on options page
42 */
43function plugin_listcommands_optpage_register_block_do()
44{
e5f21a91 45 global $optpage_blocks, $listcommands_allow_non_rfc_list_management;
46
47 // only allow management of non-RFC lists if admin deems necessary
48 //
49 @include_once(SM_PATH . 'plugins/listcommands/config.php');
50 if (!$listcommands_allow_non_rfc_list_management)
51 return;
52
53 $optpage_blocks[] = array(
54 'name' => _("Mailing Lists"),
55 'url' => '../plugins/listcommands/options.php',
56 'desc' => _("Manage the (non-RFC-compliant) mailing lists that you are subscribed to for the purpose of providing one-click list replies when responding to list messages."),
57 'js' => false
58 );
59
60}
61
f4f2d73f 62/**
63 * internal function that builds mailing list links
64 */
793566f0 65function plugin_listcommands_menu_do() {
fb0bcfdb 66 global $passed_id, $passed_ent_id, $mailbox, $message,
e5f21a91 67 $startMessage, $oTemplate, $listcommands_allow_non_rfc_list_management;
68
69 @include_once(SM_PATH . 'plugins/listcommands/config.php');
793566f0 70
71 /**
72 * Array of commands we can deal with from the header. The Reply option
73 * is added later because we generate it using the Post information.
74 */
75 $fieldsdescr = listcommands_fieldsdescr();
2e58d6af 76 $links = array();
793566f0 77
78 foreach ($message->rfc822_header->mlist as $cmd => $actions) {
79
80 /* I don't know this action... skip it */
81 if ( !array_key_exists($cmd, $fieldsdescr) ) {
82 continue;
83 }
84
85 /* proto = {mailto,href} */
ba17b6c7 86 $aActions = array_keys($actions);
d47243ef 87 // note that we only use the first cmd/action, ignore the rest
ba17b6c7 88 $proto = array_shift($aActions);
2253d920 89 $act = array_shift($actions);
793566f0 90
6aef76f3 91 if ($proto == 'mailto') {
793566f0 92
dd9c9e85 93 $identity = '';
94
793566f0 95 if (($cmd == 'post') || ($cmd == 'owner')) {
96 $url = 'src/compose.php?'.
97 (isset($startMessage)?'startMessage='.$startMessage.'&amp;':'');
98 } else {
99 $url = "plugins/listcommands/mailout.php?action=$cmd&amp;";
dd9c9e85 100
101 // try to find which identity the mail should come from
102 include_once(SM_PATH . 'functions/identity.php');
103 $idents = get_identities();
104 // ripped from src/compose.php
105 $identities = array();
106 if (count($idents) > 1) {
107 foreach($idents as $nr=>$data) {
108 $enc_from_name = '"'.$data['full_name'].'" <'. $data['email_address'].'>';
109 $identities[] = $enc_from_name;
110 }
111
112 $identity_match = $message->rfc822_header->findAddress($identities);
113 if ($identity_match !== FALSE) {
114 $identity = $identity_match;
115 }
116 }
793566f0 117 }
d47243ef 118
119 // if things like subject are given, peel them off and give
120 // them to src/compose.php as is (not encoded)
121 if (strpos($act, '?') > 0) {
122 list($act, $parameters) = explode('?', $act, 2);
dd9c9e85 123 $parameters = '&amp;identity=' . $identity . '&amp;' . $parameters;
d47243ef 124 } else {
dd9c9e85 125 $parameters = '&amp;identity=' . $identity;
d47243ef 126 }
127
128 $url .= 'send_to=' . urlencode($act) . $parameters;
793566f0 129
e5f21a91 130 $links[$cmd] = makeComposeLink($url, $fieldsdescr[$cmd]);
793566f0 131
132 if ($cmd == 'post') {
133 if (!isset($mailbox))
134 $mailbox = 'INBOX';
135 $url .= '&amp;passed_id='.$passed_id.
136 '&amp;mailbox='.urlencode($mailbox).
137 (isset($passed_ent_id)?'&amp;passed_ent_id='.$passed_ent_id:'');
138 $url .= '&amp;smaction=reply';
139
e5f21a91 140 $links['reply'] = makeComposeLink($url, $fieldsdescr['reply']);
793566f0 141 }
142 } else if ($proto == 'href') {
e5f21a91 143 $links[$cmd] = create_hyperlink($act, $fieldsdescr[$cmd], '_blank');
144 }
145 }
146
147
148 // allow non-rfc reply link if admin allows and message is from
149 // non-rfc list the user has configured
150 //
151 if ($listcommands_allow_non_rfc_list_management) {
152
153 $non_rfc_lists = get_non_rfc_lists();
154
155 $recipients = formatRecipientString($message->rfc822_header->to, "to") . ' '
156 . formatRecipientString($message->rfc822_header->cc, "cc") . ' '
157 . formatRecipientString($message->rfc822_header->bcc, "bcc");
158
159 if (!in_array('post', array_keys($links))) {
160
161 foreach ($non_rfc_lists as $non_rfc_list) {
b5f6b945 162 if (preg_match('/(^|,|<|\s)' . preg_quote($non_rfc_list) . '($|,|>|\s)/', $recipients)) {
e5f21a91 163 $url = 'src/compose.php?'
164 . (isset($startMessage)?'startMessage='.$startMessage.'&amp;':'')
165 . 'send_to=' . str_replace('?','&amp;', $non_rfc_list);
166
167 $links['post'] = makeComposeLink($url, $fieldsdescr['post']);
168
169 break;
170 }
171 }
172
793566f0 173 }
e5f21a91 174
175 if (!in_array('reply', array_keys($links))) {
176
177 foreach ($non_rfc_lists as $non_rfc_list) {
178 if (preg_match('/(^|,|\s)' . preg_quote($non_rfc_list) . '($|,|\s)/', $recipients)) {
179 if (!isset($mailbox))
180 $mailbox = 'INBOX';
181 $url = 'src/compose.php?'
182 . (isset($startMessage)?'startMessage='.$startMessage.'&amp;':'')
183 . 'send_to=' . str_replace('?','&amp;', $non_rfc_list)
184 . '&amp;passed_id='.$passed_id
185 . '&amp;mailbox='.urlencode($mailbox)
186 . (isset($passed_ent_id)?'&amp;passed_ent_id='.$passed_ent_id:'')
187 . '&amp;smaction=reply';
188
189 $links['reply'] = makeComposeLink($url, $fieldsdescr['reply']);
190
191 break;
192 }
193 }
194
195 }
196
793566f0 197 }
198
e5f21a91 199
2e58d6af 200 if (count($links) > 0) {
857b1ca4 201 $oTemplate->assign('links', $links);
2e58d6af 202 $output = $oTemplate->fetch('plugins/listcommands/read_body_header.tpl');
203 return array('read_body_header' => $output);
793566f0 204 }
2e58d6af 205
793566f0 206}
207
208/**
209 * Returns an array with the actions as translated strings.
210 * @return array action as key, translated string as value
211 */
212function listcommands_fieldsdescr() {
213 return array('post' => _("Post to List"),
214 'reply' => _("Reply to List"),
215 'subscribe' => _("Subscribe"),
216 'unsubscribe' => _("Unsubscribe"),
217 'archive' => _("List Archives"),
218 'owner' => _("Contact Listowner"),
219 'help' => _("Help"));
220}
221