Ah, I see - the stuff after the ? needs to be handed to src/compose.php as-is, no...
[squirrelmail.git] / plugins / listcommands / functions.php
1 <?php
2
3 /**
4 * functions.php
5 *
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 *
11 * @copyright 1999-2015 The SquirrelMail Project Team
12 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
13 * @version $Id$
14 * @package plugins
15 * @subpackage listcommands
16 */
17
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 */
23 function 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 */
43 function plugin_listcommands_optpage_register_block_do()
44 {
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
62 /**
63 * internal function that builds mailing list links
64 */
65 function plugin_listcommands_menu_do() {
66 global $passed_id, $passed_ent_id, $mailbox, $message,
67 $startMessage, $oTemplate, $listcommands_allow_non_rfc_list_management;
68
69 @include_once(SM_PATH . 'plugins/listcommands/config.php');
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();
76 $links = array();
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} */
86 $aActions = array_keys($actions);
87 // note that we only use the first cmd/action, ignore the rest
88 $proto = array_shift($aActions);
89 $act = array_shift($actions);
90
91 if ($proto == 'mailto') {
92
93 if (($cmd == 'post') || ($cmd == 'owner')) {
94 $url = 'src/compose.php?'.
95 (isset($startMessage)?'startMessage='.$startMessage.'&amp;':'');
96 } else {
97 $url = "plugins/listcommands/mailout.php?action=$cmd&amp;";
98 }
99
100 // if things like subject are given, peel them off and give
101 // them to src/compose.php as is (not encoded)
102 if (strpos($act, '?') > 0) {
103 list($act, $parameters) = explode('?', $act, 2);
104 $parameters = '&amp;' . $parameters;
105 } else {
106 $parameters = '';
107 }
108
109 $url .= 'send_to=' . urlencode($act) . $parameters;
110
111 $links[$cmd] = makeComposeLink($url, $fieldsdescr[$cmd]);
112
113 if ($cmd == 'post') {
114 if (!isset($mailbox))
115 $mailbox = 'INBOX';
116 $url .= '&amp;passed_id='.$passed_id.
117 '&amp;mailbox='.urlencode($mailbox).
118 (isset($passed_ent_id)?'&amp;passed_ent_id='.$passed_ent_id:'');
119 $url .= '&amp;smaction=reply';
120
121 $links['reply'] = makeComposeLink($url, $fieldsdescr['reply']);
122 }
123 } else if ($proto == 'href') {
124 $links[$cmd] = create_hyperlink($act, $fieldsdescr[$cmd], '_blank');
125 }
126 }
127
128
129 // allow non-rfc reply link if admin allows and message is from
130 // non-rfc list the user has configured
131 //
132 if ($listcommands_allow_non_rfc_list_management) {
133
134 $non_rfc_lists = get_non_rfc_lists();
135
136 $recipients = formatRecipientString($message->rfc822_header->to, "to") . ' '
137 . formatRecipientString($message->rfc822_header->cc, "cc") . ' '
138 . formatRecipientString($message->rfc822_header->bcc, "bcc");
139
140 if (!in_array('post', array_keys($links))) {
141
142 foreach ($non_rfc_lists as $non_rfc_list) {
143 if (preg_match('/(^|,|<|\s)' . preg_quote($non_rfc_list) . '($|,|>|\s)/', $recipients)) {
144 $url = 'src/compose.php?'
145 . (isset($startMessage)?'startMessage='.$startMessage.'&amp;':'')
146 . 'send_to=' . str_replace('?','&amp;', $non_rfc_list);
147
148 $links['post'] = makeComposeLink($url, $fieldsdescr['post']);
149
150 break;
151 }
152 }
153
154 }
155
156 if (!in_array('reply', array_keys($links))) {
157
158 foreach ($non_rfc_lists as $non_rfc_list) {
159 if (preg_match('/(^|,|\s)' . preg_quote($non_rfc_list) . '($|,|\s)/', $recipients)) {
160 if (!isset($mailbox))
161 $mailbox = 'INBOX';
162 $url = 'src/compose.php?'
163 . (isset($startMessage)?'startMessage='.$startMessage.'&amp;':'')
164 . 'send_to=' . str_replace('?','&amp;', $non_rfc_list)
165 . '&amp;passed_id='.$passed_id
166 . '&amp;mailbox='.urlencode($mailbox)
167 . (isset($passed_ent_id)?'&amp;passed_ent_id='.$passed_ent_id:'')
168 . '&amp;smaction=reply';
169
170 $links['reply'] = makeComposeLink($url, $fieldsdescr['reply']);
171
172 break;
173 }
174 }
175
176 }
177
178 }
179
180
181 if (count($links) > 0) {
182 $oTemplate->assign('links', $links);
183 $output = $oTemplate->fetch('plugins/listcommands/read_body_header.tpl');
184 return array('read_body_header' => $output);
185 }
186
187 }
188
189 /**
190 * Returns an array with the actions as translated strings.
191 * @return array action as key, translated string as value
192 */
193 function listcommands_fieldsdescr() {
194 return array('post' => _("Post to List"),
195 'reply' => _("Reply to List"),
196 'subscribe' => _("Subscribe"),
197 'unsubscribe' => _("Unsubscribe"),
198 'archive' => _("List Archives"),
199 'owner' => _("Contact Listowner"),
200 'help' => _("Help"));
201 }
202