Aleksander Machniak
2015-12-17 c82d09a052b929be2087a73d95be8657a33027b3
commit | author | age
4e17e6 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/mail/compose.inc                                        |
6  |                                                                       |
e019f2 7  | This file is part of the Roundcube Webmail client                     |
5e3034 8  | Copyright (C) 2005-2013, The Roundcube Dev Team                       |
7fe381 9  |                                                                       |
T 10  | Licensed under the GNU General Public License version 3 or            |
11  | any later version with exceptions for skins & plugins.                |
12  | See the README file for a full license statement.                     |
4e17e6 13  |                                                                       |
T 14  | PURPOSE:                                                              |
15  |   Compose a new mail message with all headers and attachments         |
16  |                                                                       |
17  +-----------------------------------------------------------------------+
18  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
19  +-----------------------------------------------------------------------+
20 */
21
8d4bcd 22 // define constants for message compose mode
a0e3dc 23 define('RCUBE_COMPOSE_REPLY', 'reply');
AM 24 define('RCUBE_COMPOSE_FORWARD', 'forward');
25 define('RCUBE_COMPOSE_DRAFT', 'draft');
26 define('RCUBE_COMPOSE_EDIT', 'edit');
8d4bcd 27
72ff6a 28 $MESSAGE_FORM = null;
6b2b2e 29 $COMPOSE_ID   = rcube_utils::get_input_value('_id', rcube_utils::INPUT_GET);
72ff6a 30 $COMPOSE      = null;
f0f98f 31
72ff6a 32 if ($COMPOSE_ID && $_SESSION['compose_data_'.$COMPOSE_ID])
A 33   $COMPOSE =& $_SESSION['compose_data_'.$COMPOSE_ID];
4591de 34
81f5dd 35 // give replicated session storage some time to synchronize
T 36 $retries = 0;
72ff6a 37 while ($COMPOSE_ID && !is_array($COMPOSE) && $RCMAIL->db->is_replicated() && $retries++ < 5) {
5e3034 38     usleep(500000);
AM 39     $RCMAIL->session->reload();
40     if ($_SESSION['compose_data_'.$COMPOSE_ID]) {
41         $COMPOSE =& $_SESSION['compose_data_'.$COMPOSE_ID];
42     }
81f5dd 43 }
T 44
86df15 45 // Nothing below is called during message composition, only at "new/forward/reply/draft" initialization or
T 46 // if a compose-ID is given (i.e. when the compose step is opened in a new window/tab).
5e3034 47 if (!is_array($COMPOSE)) {
AM 48     // Infinite redirect prevention in case of broken session (#1487028)
49     if ($COMPOSE_ID) {
117233 50         // if we know the message with specified ID was already sent
AM 51         // we can ignore the error and compose a new message (#1490009)
52         if ($COMPOSE_ID != $_SESSION['last_compose_session']) {
53             rcube::raise_error(array('code' => 450), false, true);
54         }
76791c 55     }
5f314d 56
5e3034 57     $COMPOSE_ID = uniqid(mt_rand());
5d42a9 58     $params     = rcube_utils::request2param(rcube_utils::INPUT_GET, 'task|action', true);
AM 59
5e3034 60     $_SESSION['compose_data_'.$COMPOSE_ID] = array(
AM 61         'id'      => $COMPOSE_ID,
5d42a9 62         'param'   => $params,
AM 63         'mailbox' => $params['mbox'] ?: $RCMAIL->storage->get_folder(),
5e3034 64     );
AM 65     $COMPOSE =& $_SESSION['compose_data_'.$COMPOSE_ID];
66     rcmail_process_compose_params($COMPOSE);
67
68     // check if folder for saving sent messages exists and is subscribed (#1486802)
69     if ($sent_folder = $COMPOSE['param']['sent_mbox']) {
70         rcmail_check_sent_folder($sent_folder, true);
71     }
72
73     // redirect to a unique URL with all parameters stored in session
74     $OUTPUT->redirect(array(
75         '_action' => 'compose',
76         '_id'     => $COMPOSE['id'],
77         '_search' => $_REQUEST['_search'],
78     ));
4315b0 79 }
76791c 80
4e17e6 81
10a699 82 // add some labels to client
3f9712 83 $OUTPUT->add_label('nosubject', 'nosenderwarning', 'norecipientwarning', 'nosubjectwarning', 'cancel',
V 84     'nobodywarning', 'notsentwarning', 'notuploadedwarning', 'savingmessage', 'sendingmessage', 
1abb97 85     'messagesaved', 'converting', 'editorwarning', 'searching', 'uploading', 'uploadingmany',
6eb08d 86     'fileuploaderror', 'sendmessage', 'newresponse', 'responsename', 'responsetext', 'save',
08da30 87     'savingresponse', 'restoresavedcomposedata', 'restoremessage', 'delete', 'restore', 'ignore',
16c326 88     'selectimportfile', 'messageissent');
10a699 89
6b2b2e 90 $OUTPUT->set_pagetitle($RCMAIL->gettext('compose'));
4591de 91
5e3034 92 $OUTPUT->set_env('compose_id', $COMPOSE['id']);
AM 93 $OUTPUT->set_env('session_id', session_id());
c321a9 94 $OUTPUT->set_env('mailbox', $RCMAIL->storage->get_folder());
651c7b 95 $OUTPUT->set_env('top_posting', intval($RCMAIL->config->get('reply_mode')) > 0);
ef595a 96 $OUTPUT->set_env('sig_below', $RCMAIL->config->get('sig_below'));
62c861 97 $OUTPUT->set_env('recipients_separator', trim($RCMAIL->config->get('recipients_separator', ',')));
44b47d 98 $OUTPUT->set_env('save_localstorage', (bool)$RCMAIL->config->get('compose_save_localstorage'));
16c326 99 $OUTPUT->set_env('is_sent', false);
10a699 100
f5d2ee 101 $drafts_mbox     = $RCMAIL->config->get('drafts_mbox');
AM 102 $config_show_sig = $RCMAIL->config->get('show_sig', 1);
103
5e3034 104 // add config parameters to client script
f5d2ee 105 if (strlen($drafts_mbox)) {
AM 106     $OUTPUT->set_env('drafts_mailbox', $drafts_mbox);
107     $OUTPUT->set_env('draft_autosave', $RCMAIL->config->get('draft_autosave'));
5e3034 108 }
AM 109
7e263e 110 // default font for HTML editor
6b2b2e 111 $font = rcmail::font_defs($RCMAIL->config->get('default_font'));
7e263e 112 if ($font && !is_array($font)) {
5e3034 113     $OUTPUT->set_env('default_font', $font);
7e263e 114 }
A 115
965dea 116 // default font size for HTML editor
f7b2bf 117 if ($font_size = $RCMAIL->config->get('default_font_size')) {
5e3034 118     $OUTPUT->set_env('default_font_size', $font_size);
f7b2bf 119 }
965dea 120
8d4bcd 121 // get reference message and set compose mode
72ff6a 122 if ($msg_uid = $COMPOSE['param']['draft_uid']) {
5e3034 123     $compose_mode = RCUBE_COMPOSE_DRAFT;
AM 124     $OUTPUT->set_env('draft_id', $msg_uid);
f5d2ee 125     $RCMAIL->storage->set_folder($drafts_mbox);
4591de 126 }
d9f109 127 else if ($msg_uid = $COMPOSE['param']['reply_uid']) {
5e3034 128     $compose_mode = RCUBE_COMPOSE_REPLY;
d9f109 129 }
AM 130 else if ($msg_uid = $COMPOSE['param']['forward_uid']) {
5e3034 131     $compose_mode = RCUBE_COMPOSE_FORWARD;
AM 132     $COMPOSE['forward_uid']   = $msg_uid;
133     $COMPOSE['as_attachment'] = !empty($COMPOSE['param']['attachment']);
d9f109 134 }
AM 135 else if ($msg_uid = $COMPOSE['param']['uid']) {
5e3034 136     $compose_mode = RCUBE_COMPOSE_EDIT;
d9f109 137 }
66a549 138
fa424e 139 if ($compose_mode) {
AM 140     $COMPOSE['mode'] = $compose_mode;
141     $OUTPUT->set_env('compose_mode', $compose_mode);
142 }
50f56d 143
a9bb50 144 if ($compose_mode == RCUBE_COMPOSE_EDIT || $compose_mode == RCUBE_COMPOSE_DRAFT) {
5e3034 145     // don't add signature in draft/edit mode, we'll also not remove the old-one
AM 146     // but only on page display, later we should be able to change identity/sig (#1489229)
147     if ($config_show_sig == 1 || $config_show_sig == 2) {
148         $OUTPUT->set_env('show_sig_later', true);
149     }
a9bb50 150 }
AM 151 else if ($config_show_sig == 1)
5e3034 152     $OUTPUT->set_env('show_sig', true);
a9bb50 153 else if ($config_show_sig == 2 && empty($compose_mode))
5e3034 154     $OUTPUT->set_env('show_sig', true);
0207c4 155 else if ($config_show_sig == 3 && ($compose_mode == RCUBE_COMPOSE_REPLY || $compose_mode == RCUBE_COMPOSE_FORWARD))
5e3034 156     $OUTPUT->set_env('show_sig', true);
8d4bcd 157
b62049 158 // set line length for body wrapping
6b6f2e 159 $LINE_LENGTH = $RCMAIL->config->get('line_length', 72);
b62049 160
5e3034 161 if (!empty($msg_uid) && empty($COMPOSE['as_attachment'])) {
AM 162     $mbox_name = $RCMAIL->storage->get_folder();
a02c77 163
5e3034 164     // set format before rcube_message construction
AM 165     // use the same format as for the message view
166     if (isset($_SESSION['msg_formats'][$mbox_name.':'.$msg_uid])) {
167         $RCMAIL->config->set('prefer_html', $_SESSION['msg_formats'][$mbox_name.':'.$msg_uid]);
eeb85f 168     }
5e3034 169     else {
f5d2ee 170         $prefer_html = $RCMAIL->config->get('prefer_html') || $RCMAIL->config->get('htmleditor')
AM 171             || $compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT;
172
5e3034 173         $RCMAIL->config->set('prefer_html', $prefer_html);
AM 174     }
bc404f 175
5e3034 176     $MESSAGE = new rcube_message($msg_uid);
bc404f 177
5e3034 178     // make sure message is marked as read
AM 179     if ($MESSAGE->headers && empty($MESSAGE->headers->flags['SEEN'])) {
180         $RCMAIL->storage->set_flag($msg_uid, 'SEEN');
181     }
182
183     if (!empty($MESSAGE->headers->charset)) {
184         $RCMAIL->storage->set_charset($MESSAGE->headers->charset);
185     }
186
187     if (!$MESSAGE->headers) {
188         // error
189     }
73076d 190     else if ($compose_mode == RCUBE_COMPOSE_FORWARD || $compose_mode == RCUBE_COMPOSE_REPLY) {
AM 191         if ($compose_mode == RCUBE_COMPOSE_REPLY) {
d0cb32 192             $COMPOSE['reply_uid'] = $msg_uid;
73076d 193
AM 194             if (!empty($COMPOSE['param']['all'])) {
195                 $MESSAGE->reply_all = $COMPOSE['param']['all'];
196             }
197         }
198         else {
199             $COMPOSE['forward_uid'] = $msg_uid;
200         }
201
5e3034 202         $COMPOSE['reply_msgid'] = $MESSAGE->headers->messageID;
AM 203         $COMPOSE['references']  = trim($MESSAGE->headers->references . " " . $MESSAGE->headers->messageID);
eeb85f 204
10936f 205         // Save the sent message in the same folder of the message being replied to
5e3034 206         if ($RCMAIL->config->get('reply_same_folder') && ($sent_folder = $COMPOSE['mailbox'])
AM 207             && rcmail_check_sent_folder($sent_folder, false)
10936f 208         ) {
5e3034 209             $COMPOSE['param']['sent_mbox'] = $sent_folder;
10936f 210         }
95fd38 211     }
5e3034 212     else if ($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT) {
AM 213         if ($compose_mode == RCUBE_COMPOSE_DRAFT) {
214             if ($draft_info = $MESSAGE->headers->get('x-draft-info')) {
215                 // get reply_uid/forward_uid to flag the original message when sending
216                 $info = rcmail_draftinfo_decode($draft_info);
eeb85f 217
5e3034 218                 if ($info['type'] == 'reply')
AM 219                     $COMPOSE['reply_uid'] = $info['uid'];
220                 else if ($info['type'] == 'forward')
221                     $COMPOSE['forward_uid'] = $info['uid'];
bc404f 222
5e3034 223                 $COMPOSE['mailbox'] = $info['folder'];
AM 224
225                 // Save the sent message in the same folder of the message being replied to
226                 if ($RCMAIL->config->get('reply_same_folder') && ($sent_folder = $info['folder'])
227                     && rcmail_check_sent_folder($sent_folder, false)
228                 ) {
229                     $COMPOSE['param']['sent_mbox'] = $sent_folder;
230                 }
231             }
232
233             $COMPOSE['param']['message-id'] = $MESSAGE->headers->get('message-id');
234
235             // use message UID as draft_id
236             $OUTPUT->set_env('draft_id', $msg_uid);
237         }
238
239         if ($in_reply_to = $MESSAGE->headers->get('in-reply-to')) {
240             $COMPOSE['reply_msgid'] = '<' . $in_reply_to . '>';
241         }
242
243         $COMPOSE['references'] = $MESSAGE->headers->references;
244     }
252d27 245 }
A 246 else {
5e3034 247     $MESSAGE = new stdClass();
eafd5b 248
5e3034 249     // apply mailto: URL parameters
AM 250     if (!empty($COMPOSE['param']['in-reply-to'])) {
251         $COMPOSE['reply_msgid'] = '<' . $COMPOSE['param']['in-reply-to'] . '>';
252     }
253
254     if (!empty($COMPOSE['param']['references'])) {
255         $COMPOSE['references'] = $COMPOSE['param']['references'];
256     }
4315b0 257 }
4e17e6 258
73076d 259 if (!empty($COMPOSE['reply_msgid'])) {
AM 260     $OUTPUT->set_env('reply_msgid', $COMPOSE['reply_msgid']);
261 }
90dc9b 262
da142b 263 $MESSAGE->compose = array();
A 264
265 // get user's identities
0247b8 266 $MESSAGE->identities = $RCMAIL->user->list_identities(null, true);
da142b 267
A 268 // Set From field value
269 if (!empty($_POST['_from'])) {
5e3034 270     $MESSAGE->compose['from'] = rcube_utils::get_input_value('_from', rcube_utils::INPUT_POST);
da142b 271 }
72ff6a 272 else if (!empty($COMPOSE['param']['from'])) {
5e3034 273     $MESSAGE->compose['from'] = $COMPOSE['param']['from'];
da142b 274 }
A 275 else if (count($MESSAGE->identities)) {
5e3034 276     $ident = rcmail_identity_select($MESSAGE, $MESSAGE->identities, $compose_mode);
da142b 277
5e3034 278     $MESSAGE->compose['from_email'] = $ident['email'];
AM 279     $MESSAGE->compose['from']       = $ident['identity_id'];
da142b 280 }
A 281
282 // Set other headers
283 $a_recipients = array();
284 $parts        = array('to', 'cc', 'bcc', 'replyto', 'followupto');
62c861 285 $separator    = trim($RCMAIL->config->get('recipients_separator', ',')) . ' ';
b23b3f 286 $from_email   = @mb_strtolower($MESSAGE->compose['from_email']);
da142b 287
A 288 foreach ($parts as $header) {
5e3034 289     $fvalue        = '';
AM 290     $decode_header = true;
386e3a 291     $charset       = $MESSAGE->headers->charset;
da142b 292
5e3034 293     // we have a set of recipients stored is session
AM 294     if ($header == 'to' && ($mailto_id = $COMPOSE['param']['mailto'])
295         && $_SESSION['mailto'][$mailto_id]
296     ) {
297         $fvalue        = urldecode($_SESSION['mailto'][$mailto_id]);
298         $decode_header = false;
386e3a 299         $charset       = $RCMAIL->output->charset;
436027 300
5e3034 301         // make session to not grow up too much
AM 302         unset($_SESSION['mailto'][$mailto_id]);
303         $COMPOSE['param']['to'] = $fvalue;
da142b 304     }
5e3034 305     else if (!empty($_POST['_'.$header])) {
386e3a 306         $fvalue  = rcube_utils::get_input_value('_'.$header, rcube_utils::INPUT_POST, TRUE);
AM 307         $charset = $RCMAIL->output->charset;
5e3034 308     }
AM 309     else if (!empty($COMPOSE['param'][$header])) {
386e3a 310         $fvalue  = $COMPOSE['param'][$header];
AM 311         $charset = $RCMAIL->output->charset;
5e3034 312     }
AM 313     else if ($compose_mode == RCUBE_COMPOSE_REPLY) {
314         // get recipent address(es) out of the message headers
315         if ($header == 'to') {
316             $mailfollowup = $MESSAGE->headers->others['mail-followup-to'];
317             $mailreplyto  = $MESSAGE->headers->others['mail-reply-to'];
d2dff5 318
5e3034 319             // Reply to mailing list...
AM 320             if ($MESSAGE->reply_all == 'list' && $mailfollowup)
321                 $fvalue = $mailfollowup;
322             else if ($MESSAGE->reply_all == 'list'
323                 && preg_match('/<mailto:([^>]+)>/i', $MESSAGE->headers->others['list-post'], $m))
324                 $fvalue = $m[1];
325             // Reply to...
326             else if ($MESSAGE->reply_all && $mailfollowup)
327                 $fvalue = $mailfollowup;
328             else if ($mailreplyto)
329                 $fvalue = $mailreplyto;
38dbd8 330             else if (!empty($MESSAGE->headers->replyto)) {
AM 331                 $fvalue  = $MESSAGE->headers->replyto;
332                 $replyto = true;
333             }
5e3034 334             else if (!empty($MESSAGE->headers->from))
AM 335                 $fvalue = $MESSAGE->headers->from;
d2dff5 336
0aadd7 337             // Reply to message sent by yourself (#1487074, #1489230, #1490439)
38dbd8 338             // Reply-To address need to be unset (#1490233)
0aadd7 339             if (!empty($ident) && empty($replyto)) {
AM 340                 foreach (array($fvalue, $MESSAGE->headers->from) as $sender) {
341                     $senders = rcube_mime::decode_address_list($sender, null, false, $charset, true);
342
343                     if (in_array($ident['email_ascii'], $senders)) {
344                         $fvalue = $MESSAGE->headers->to;
345                         break;
346                     }
347                 }
5e3034 348             }
d2dff5 349         }
5e3034 350         // add recipient of original message if reply to all
AM 351         else if ($header == 'cc' && !empty($MESSAGE->reply_all) && $MESSAGE->reply_all != 'list') {
352             if ($v = $MESSAGE->headers->to)
353                 $fvalue .= $v;
354             if ($v = $MESSAGE->headers->cc)
355                 $fvalue .= (!empty($fvalue) ? $separator : '') . $v;
356             // Use Sender header (#1489011)
a9035b 357             if ($v = $MESSAGE->headers->get('Sender', false)) {
AM 358                 // Skip common mailing lists addresses: *-bounces@ and *-request@ (#1490452)
359                 if (empty($MESSAGE->headers->others['list-post']) || !preg_match('/-(bounces|request)@/', $v)) {
360                     $fvalue .= (!empty($fvalue) ? $separator : '') . $v;
361                 }
362             }
5e3034 363
AM 364             // When To: and Reply-To: are the same we add From: address to the list (#1489037)
365             if ($v = $MESSAGE->headers->from) {
386e3a 366                 $from    = rcube_mime::decode_address_list($v, null, false, $charset, true);
AM 367                 $to      = rcube_mime::decode_address_list($MESSAGE->headers->to, null, false, $charset, true);
368                 $replyto = rcube_mime::decode_address_list($MESSAGE->headers->replyto, null, false, $charset, true);
5e3034 369
AM 370                 if (count($replyto) && !count(array_diff($to, $replyto)) && count(array_diff($from, $to))) {
371                     $fvalue .= (!empty($fvalue) ? $separator : '') . $v;
372                 }
373             }
374         }
da142b 375     }
5e3034 376     else if (in_array($compose_mode, array(RCUBE_COMPOSE_DRAFT, RCUBE_COMPOSE_EDIT))) {
AM 377         // get drafted headers
378         if ($header=='to' && !empty($MESSAGE->headers->to))
379             $fvalue = $MESSAGE->get_header('to', true);
380         else if ($header=='cc' && !empty($MESSAGE->headers->cc))
381             $fvalue = $MESSAGE->get_header('cc', true);
382         else if ($header=='bcc' && !empty($MESSAGE->headers->bcc))
383             $fvalue = $MESSAGE->get_header('bcc', true);
384         else if ($header=='replyto' && !empty($MESSAGE->headers->others['mail-reply-to']))
385             $fvalue = $MESSAGE->get_header('mail-reply-to');
386         else if ($header=='replyto' && !empty($MESSAGE->headers->replyto))
387             $fvalue = $MESSAGE->get_header('reply-to');
388         else if ($header=='followupto' && !empty($MESSAGE->headers->others['mail-followup-to']))
389             $fvalue = $MESSAGE->get_header('mail-followup-to');
da142b 390     }
efc24a 391
5e3034 392     // split recipients and put them back together in a unique way
AM 393     if (!empty($fvalue) && in_array($header, array('to', 'cc', 'bcc'))) {
386e3a 394         $to_addresses = rcube_mime::decode_address_list($fvalue, null, $decode_header, $charset);
5e3034 395         $fvalue       = array();
da142b 396
5e3034 397         foreach ($to_addresses as $addr_part) {
AM 398             if (empty($addr_part['mailto'])) {
399                 continue;
400             }
401
b23b3f 402             // According to RFC5321 local part of email address is case-sensitive
AM 403             // however, here it is better to compare addresses in case-insensitive manner
404             $mailto    = format_email(rcube_utils::idn_to_utf8($addr_part['mailto']));
405             $mailto_lc = mb_strtolower($addr_part['mailto']);
5e3034 406
b23b3f 407             if (($header == 'to' || $compose_mode != RCUBE_COMPOSE_REPLY || $mailto_lc != $from_email)
AM 408                 && !in_array($mailto_lc, $a_recipients)
5e3034 409             ) {
b23b3f 410                 if ($addr_part['name'] && $mailto != $addr_part['name']) {
AM 411                     $mailto = format_email_recipient($mailto, $addr_part['name']);
412                 }
5e3034 413
b23b3f 414                 $fvalue[]       = $mailto;
AM 415                 $a_recipients[] = $mailto_lc;
5e3034 416             }
AM 417         }
418
419         $fvalue = implode($separator, $fvalue);
420     }
421
422     $MESSAGE->compose[$header] = $fvalue;
da142b 423 }
A 424 unset($a_recipients);
425
087c7d 426 // process $MESSAGE body/attachments, set $MESSAGE_BODY/$HTML_MODE vars and some session data
A 427 $MESSAGE_BODY = rcmail_prepare_message_body();
4e17e6 428
087c7d 429
5e3034 430 // register UI objects
AM 431 $OUTPUT->add_handlers(array(
432     'composeheaders'        => 'rcmail_compose_headers',
433     'composesubject'        => 'rcmail_compose_subject',
434     'composebody'           => 'rcmail_compose_body',
435     'composeattachmentlist' => 'rcmail_compose_attachment_list',
436     'composeattachmentform' => 'rcmail_compose_attachment_form',
437     'composeattachment'     => 'rcmail_compose_attachment_field',
438     'filedroparea'          => 'compose_file_drop_area',
439     'priorityselector'      => 'rcmail_priority_selector',
440     'editorselector'        => 'rcmail_editor_selector',
f65021 441     'receiptcheckbox'       => 'rcmail_mdn_checkbox', // deprecated
AM 442     'mdncheckbox'           => 'rcmail_mdn_checkbox',
5e3034 443     'dsncheckbox'           => 'rcmail_dsn_checkbox',
AM 444     'storetarget'           => 'rcmail_store_target_selection',
445     'addressbooks'          => 'rcmail_addressbook_list',
446     'addresslist'           => 'rcmail_contacts_list',
447     'responseslist'         => 'rcmail_compose_responses_list',
448 ));
449
450 $OUTPUT->send('compose');
451
452
087c7d 453 /****** compose mode functions ********/
0247b8 454
eafd5b 455 // process compose request parameters
AM 456 function rcmail_process_compose_params(&$COMPOSE)
457 {
5e3034 458     if ($COMPOSE['param']['to']) {
AM 459         $mailto = explode('?', $COMPOSE['param']['to'], 2);
eafd5b 460
5e3034 461         // #1486037: remove "mailto:" prefix
AM 462         $COMPOSE['param']['to'] = preg_replace('/^mailto:/i', '', $mailto[0]);
68f76f 463         // #1490346: decode the recipient address
8ef86f 464         // #1490510: use raw encoding for correct "+" character handling as specified in RFC6068
AM 465         $COMPOSE['param']['to'] = rawurldecode($COMPOSE['param']['to']);
eafd5b 466
5e3034 467         // Supported case-insensitive tokens in mailto URL
AM 468         $url_tokens = array('to', 'cc', 'bcc', 'reply-to', 'in-reply-to', 'references', 'subject', 'body');
eafd5b 469
5e3034 470         if (!empty($mailto[1])) {
AM 471             parse_str($mailto[1], $query);
472             foreach ($query as $f => $val) {
473                 if (($key = array_search(strtolower($f), $url_tokens)) !== false) {
474                     $f = $url_tokens[$key];
475                 }
eafd5b 476
5e3034 477                 // merge mailto: addresses with addresses from 'to' parameter
AM 478                 if ($f == 'to' && !empty($COMPOSE['param']['to'])) {
479                     $to_addresses  = rcube_mime::decode_address_list($COMPOSE['param']['to'], null, true, null, true);
480                     $add_addresses = rcube_mime::decode_address_list($val, null, true);
481
482                     foreach ($add_addresses as $addr) {
483                         if (!in_array($addr['mailto'], $to_addresses)) {
484                             $to_addresses[]         = $addr['mailto'];
485                             $COMPOSE['param']['to'] = (!empty($to_addresses) ? ', ' : '') . $addr['string'];
486                         }
487                     }
488                 }
489                 else {
490                     $COMPOSE['param'][$f] = $val;
491                 }
eafd5b 492             }
AM 493         }
494     }
495
aafbe8 496     // resolve _forward_uid=* to an absolute list of messages from a search result
TB 497     if ($COMPOSE['param']['forward_uid'] == '*' && is_object($_SESSION['search'][1])) {
498         $COMPOSE['param']['forward_uid'] = $_SESSION['search'][1]->get();
499     }
500
5e3034 501     // clean HTML message body which can be submitted by URL
AM 502     if (!empty($COMPOSE['param']['body'])) {
503         $COMPOSE['param']['body'] = rcmail_wash_html($COMPOSE['param']['body'], array('safe' => false, 'inline_html' => true), array());
504     }
2af374 505
5e3034 506     $RCMAIL = rcmail::get_instance();
eafd5b 507
5e3034 508     // select folder where to save the sent message
AM 509     $COMPOSE['param']['sent_mbox'] = $RCMAIL->config->get('sent_mbox');
eafd5b 510
5e3034 511     // pipe compose parameters thru plugins
AM 512     $plugin = $RCMAIL->plugins->exec_hook('message_compose', $COMPOSE);
513     $COMPOSE['param'] = array_merge($COMPOSE['param'], $plugin['param']);
37b9e0 514
AM 515     // add attachments listed by message_compose hook
516     if (is_array($plugin['attachments'])) {
517         foreach ($plugin['attachments'] as $attach) {
518             // we have structured data
519             if (is_array($attach)) {
8ed382 520                 $attachment = $attach + array('group' => $COMPOSE_ID);
37b9e0 521             }
AM 522             // only a file path is given
523             else {
524                 $filename   = basename($attach);
525                 $attachment = array(
526                     'group'    => $COMPOSE_ID,
527                     'name'     => $filename,
528                     'mimetype' => rcube_mime::file_content_type($attach, $filename),
529                     'path'     => $attach,
530                 );
531             }
532
533             // save attachment if valid
534             if (($attachment['data'] && $attachment['name']) || ($attachment['path'] && file_exists($attachment['path']))) {
535                 $attachment = rcmail::get_instance()->plugins->exec_hook('attachment_save', $attachment);
536             }
537
538             if ($attachment['status'] && !$attachment['abort']) {
539                 unset($attachment['data'], $attachment['status'], $attachment['abort']);
540                 $COMPOSE['attachments'][$attachment['id']] = $attachment;
541             }
542         }
543     }
eafd5b 544 }
AM 545
4e17e6 546 function rcmail_compose_headers($attrib)
4315b0 547 {
5e3034 548     global $RCMAIL, $MESSAGE;
4e17e6 549
5e3034 550     list($form_start,) = get_form_tags($attrib);
8958d0 551
5e3034 552     $out  = '';
AM 553     $part = strtolower($attrib['part']);
8958d0 554
5e3034 555     switch ($part) {
4e17e6 556     case 'from':
5e3034 557         return $form_start . rcmail_compose_header_from($attrib);
4e17e6 558
T 559     case 'to':
560     case 'cc':
561     case 'bcc':
5e3034 562         $fname  = '_' . $part;
AM 563         $header = $param = $part;
8958d0 564
5e3034 565         $allow_attrib = array('id', 'class', 'style', 'cols', 'rows', 'tabindex');
AM 566         $field_type   = 'html_textarea';
567         break;
4e17e6 568
T 569     case 'replyto':
570     case 'reply-to':
5e3034 571         $fname  = '_replyto';
AM 572         $param  = 'replyto';
573         $header = 'reply-to';
e25a35 574
3ee5a7 575     case 'followupto':
A 576     case 'followup-to':
5e3034 577         if (!$fname) {
AM 578             $fname  = '_followupto';
579             $param  = 'followupto';
580             $header = 'mail-followup-to';
581         }
e25a35 582
5e3034 583         $allow_attrib = array('id', 'class', 'style', 'size', 'tabindex');
AM 584         $field_type   = 'html_inputfield';
585         break;
586     }
e99991 587
5e3034 588     if ($fname && $field_type) {
AM 589         // pass the following attributes to the form class
590         $field_attrib = array('name' => $fname, 'spellcheck' => 'false');
591         foreach ($attrib as $attr => $value) {
592             if (in_array($attr, $allow_attrib)) {
593                 $field_attrib[$attr] = $value;
594             }
595         }
4e17e6 596
5e3034 597         // create teaxtarea object
AM 598         $input = new $field_type($field_attrib);
599         $out   = $input->show($MESSAGE->compose[$param]);
600     }
0213f8 601
5e3034 602     if ($form_start) {
AM 603         $out = $form_start . $out;
604     }
1966c5 605
5e3034 606     // configure autocompletion
AM 607     $RCMAIL->autocomplete_init();
0213f8 608
5e3034 609     return $out;
4315b0 610 }
4e17e6 611
T 612
1cded8 613 function rcmail_compose_header_from($attrib)
4315b0 614 {
5e3034 615     global $MESSAGE, $OUTPUT, $RCMAIL, $COMPOSE, $compose_mode;
da142b 616
5e3034 617     // pass the following attributes to the form class
AM 618     $field_attrib = array('name' => '_from');
619     foreach ($attrib as $attr => $value) {
620         if (in_array($attr, array('id', 'class', 'style', 'size', 'tabindex'))) {
621             $field_attrib[$attr] = $value;
a0109c 622         }
4315b0 623     }
e25a35 624
5e3034 625     if (count($MESSAGE->identities)) {
AM 626         $a_signatures = array();
627         $identities   = array();
ef595a 628         $top_posting  = intval($RCMAIL->config->get('reply_mode')) > 0
AM 629             && !$RCMAIL->config->get('sig_below')
630             && ($compose_mode == RCUBE_COMPOSE_REPLY || $compose_mode == RCUBE_COMPOSE_FORWARD);
631         $separator = $top_posting ? '---' : '-- ';
4e17e6 632
5e3034 633         $field_attrib['onchange'] = rcmail_output::JS_OBJECT_NAME.".change_identity(this)";
AM 634         $select_from = new html_select($field_attrib);
4e17e6 635
5e3034 636         // create SELECT element
AM 637         foreach ($MESSAGE->identities as $sql_arr) {
638             $identity_id = $sql_arr['identity_id'];
639             $select_from->add(format_email_recipient($sql_arr['email'], $sql_arr['name']), $identity_id);
640
641             // add signature to array
642             if (!empty($sql_arr['signature']) && empty($COMPOSE['param']['nosig'])) {
643                 $text = $html = $sql_arr['signature'];
644
645                 if ($sql_arr['html_signature']) {
61c35b 646                     $h2t  = new rcube_html2text($html, false, true);
5e3034 647                     $text = trim($h2t->get_text());
AM 648                 }
649                 else {
61c35b 650                     $t2h  = new rcube_text2html($text, false);
AM 651                     $html = $t2h->get_html();
5e3034 652                 }
AM 653
654                 if (!preg_match('/^--[ -]\r?\n/m', $text)) {
655                     $text = $separator . "\n" . $text;
656                     $html = $separator . "<br>" . $html;
657                 }
658
659                 $a_signatures[$identity_id]['text'] = $text;
660                 $a_signatures[$identity_id]['html'] = $html;
661             }
662
663             // add bcc and reply-to
664             if (!empty($sql_arr['reply-to'])) {
665                 $identities[$identity_id]['replyto'] = $sql_arr['reply-to'];
666             }
667             if (!empty($sql_arr['bcc'])) {
668                 $identities[$identity_id]['bcc'] = $sql_arr['bcc'];
669             }
670         }
671
672         $out = $select_from->show($MESSAGE->compose['from']);
673
674         // add signatures to client
675         $OUTPUT->set_env('signatures', $a_signatures);
676         $OUTPUT->set_env('identities', $identities);
677     }
678     // no identities, display text input field
679     else {
680         $field_attrib['class'] = 'from_address';
681         $input_from = new html_inputfield($field_attrib);
682         $out = $input_from->show($MESSAGE->compose['from']);
683     }
684
685     return $out;
4315b0 686 }
4e17e6 687
868deb 688 function rcmail_compose_editor_mode()
A 689 {
5e3034 690     global $RCMAIL, $compose_mode;
AM 691     static $useHtml;
868deb 692
5e3034 693     if ($useHtml !== null) {
AM 694         return $useHtml;
695     }
696
697     $html_editor = intval($RCMAIL->config->get('htmleditor'));
698
699     if (isset($_POST['_is_html'])) {
700         $useHtml = !empty($_POST['_is_html']);
701     }
702     else if ($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT) {
703         $useHtml = rcmail_message_is_html();
704     }
705     else if ($compose_mode == RCUBE_COMPOSE_REPLY) {
706         $useHtml = ($html_editor == 1 || ($html_editor >= 2 && rcmail_message_is_html()));
707     }
708     else if ($compose_mode == RCUBE_COMPOSE_FORWARD) {
709         $useHtml = ($html_editor == 1 || ($html_editor == 3 && rcmail_message_is_html()));
710     }
711     else {
712         $useHtml = ($html_editor == 1);
713     }
714
868deb 715     return $useHtml;
A 716 }
717
abf467 718 function rcmail_message_is_html()
AM 719 {
a02c77 720     global $RCMAIL, $MESSAGE;
5e3034 721
a02c77 722     return $RCMAIL->config->get('prefer_html') && ($MESSAGE instanceof rcube_message) && $MESSAGE->has_html_part(true);
abf467 723 }
868deb 724
087c7d 725 function rcmail_prepare_message_body()
4315b0 726 {
5e3034 727     global $RCMAIL, $MESSAGE, $COMPOSE, $compose_mode, $HTML_MODE;
a0109c 728
5e3034 729     // use posted message body
AM 730     if (!empty($_POST['_message'])) {
731         $body   = rcube_utils::get_input_value('_message', rcube_utils::INPUT_POST, true);
732         $isHtml = (bool) rcube_utils::get_input_value('_is_html', rcube_utils::INPUT_POST);
b62049 733     }
5e3034 734     else if ($COMPOSE['param']['body']) {
AM 735         $body   = $COMPOSE['param']['body'];
736         $isHtml = (bool) $COMPOSE['param']['html'];
737     }
738     // forward as attachment
739     else if ($compose_mode == RCUBE_COMPOSE_FORWARD && $COMPOSE['as_attachment']) {
740         $isHtml = rcmail_compose_editor_mode();
741         $body   = '';
742
743         rcmail_write_forward_attachments();
744     }
745     // reply/edit/draft/forward
746     else if ($compose_mode && ($compose_mode != RCUBE_COMPOSE_REPLY || intval($RCMAIL->config->get('reply_mode')) != -1)) {
747         $isHtml   = rcmail_compose_editor_mode();
748         $messages = array();
749
750         if (!empty($MESSAGE->parts)) {
751             // collect IDs of message/rfc822 parts
c82d09 752             foreach ($MESSAGE->mime_parts as $part) {
AM 753                 if ($part->mimetype == 'message/rfc822') {
754                     $messages[] = $part->mime_id;
5e3034 755                 }
AM 756             }
757
758             foreach ($MESSAGE->parts as $part) {
759                 // skip no-content and attachment parts (#1488557)
760                 if ($part->type != 'content' || !$part->size || $MESSAGE->is_attachment($part)) {
761                     continue;
762                 }
763
c82d09 764                 // skip all content parts inside the message/rfc822 part
5e3034 765                 foreach ($messages as $mimeid) {
AM 766                     if (strpos($part->mime_id, $mimeid . '.') === 0) {
767                         continue 2;
768                     }
769                 }
770
771                 if ($part_body = rcmail_compose_part_body($part, $isHtml)) {
772                     $body .= ($body ? ($isHtml ? '<br/>' : "\n") : '') . $part_body;
773                 }
774             }
775         }
776         else {
777             $body = rcmail_compose_part_body($MESSAGE, $isHtml);
778         }
779
780         // compose reply-body
781         if ($compose_mode == RCUBE_COMPOSE_REPLY)
782             $body = rcmail_create_reply_body($body, $isHtml);
783         // forward message body inline
784         else if ($compose_mode == RCUBE_COMPOSE_FORWARD)
785             $body = rcmail_create_forward_body($body, $isHtml);
786         // load draft message body
787         else if ($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT)
788             $body = rcmail_create_draft_body($body, $isHtml);
789     }
790     else { // new message
791         $isHtml = rcmail_compose_editor_mode();
4e17e6 792     }
80815d 793
5e3034 794     $plugin = $RCMAIL->plugins->exec_hook('message_compose_body',
AM 795         array('body' => $body, 'html' => $isHtml, 'mode' => $compose_mode));
a0109c 796
5e3034 797     $body = $plugin['body'];
AM 798     unset($plugin);
8abe54 799
5e3034 800     // add blocked.gif attachment (#1486516)
c6efcf 801     if ($isHtml && preg_match('#<img src="program/resources/blocked\.gif"#', $body)) {
AM 802         $content = $RCMAIL->get_resource_content('blocked.gif');
803         if ($content && ($attachment = rcmail_save_image('blocked.gif', 'image/gif', $content))) {
5e3034 804             $COMPOSE['attachments'][$attachment['id']] = $attachment;
AM 805             $url = sprintf('%s&_id=%s&_action=display-attachment&_file=rcmfile%s',
806                 $RCMAIL->comm_path, $COMPOSE['id'], $attachment['id']);
c6efcf 807             $body = preg_replace('#program/resources/blocked\.gif#', $url, $body);
5e3034 808         }
b575fa 809     }
f52c4f 810
5e3034 811     $HTML_MODE = $isHtml;
f52c4f 812
5e3034 813     return $body;
33423a 814 }
A 815
816 function rcmail_compose_part_body($part, $isHtml = false)
817 {
c027ba 818     global $RCMAIL, $MESSAGE, $LINE_LENGTH, $compose_mode;
33423a 819
A 820     // Check if we have enough memory to handle the message in it
821     // #1487424: we need up to 10x more memory than the body
6b2b2e 822     if (!rcube_utils::mem_check($part->size * 10)) {
33423a 823         return '';
A 824     }
825
826     // fetch part if not available
48ba44 827     $body = $MESSAGE->get_part_body($part->mime_id, true);
33423a 828
A 829     // message is cached but not exists (#1485443), or other error
48ba44 830     if ($body === false) {
33423a 831         return '';
A 832     }
833
834     if ($isHtml) {
835         if ($part->ctype_secondary == 'html') {
836         }
52d0d9 837         else if ($part->ctype_secondary == 'enriched') {
0fa54d 838             $body = rcube_enriched::to_html($body);
52d0d9 839         }
33423a 840         else {
A 841             // try to remove the signature
a9bb50 842             if ($compose_mode != RCUBE_COMPOSE_DRAFT && $compose_mode != RCUBE_COMPOSE_EDIT) {
AM 843                 if ($RCMAIL->config->get('strip_existing_sig', true)) {
844                     $body = rcmail_remove_signature($body);
845                 }
33423a 846             }
621a2e 847
33423a 848             // add HTML formatting
eda92e 849             $body = rcmail_plain_body($body, $part->ctype_parameters['format'] == 'flowed');
33423a 850         }
A 851     }
852     else {
52d0d9 853         if ($part->ctype_secondary == 'enriched') {
0fa54d 854             $body = rcube_enriched::to_html($body);
52d0d9 855             $part->ctype_secondary = 'html';
AM 856         }
857
33423a 858         if ($part->ctype_secondary == 'html') {
A 859             // use html part if it has been used for message (pre)viewing
860             // decrease line length for quoting
861             $len = $compose_mode == RCUBE_COMPOSE_REPLY ? $LINE_LENGTH-2 : $LINE_LENGTH;
66afd7 862             $txt = new rcube_html2text($body, false, true, $len);
33423a 863             $body = $txt->get_text();
A 864         }
865         else {
866             if ($part->ctype_secondary == 'plain' && $part->ctype_parameters['format'] == 'flowed') {
867                 $body = rcube_mime::unfold_flowed($body);
868             }
869
870             // try to remove the signature
a9bb50 871             if ($compose_mode != RCUBE_COMPOSE_DRAFT && $compose_mode != RCUBE_COMPOSE_EDIT) {
AM 872                 if ($RCMAIL->config->get('strip_existing_sig', true)) {
873                     $body = rcmail_remove_signature($body);
874                 }
33423a 875             }
A 876         }
877     }
878
879     return $body;
087c7d 880 }
A 881
882 function rcmail_compose_body($attrib)
883 {
f5d2ee 884     global $RCMAIL, $OUTPUT, $HTML_MODE, $MESSAGE_BODY;
f52c4f 885
5e3034 886     list($form_start, $form_end) = get_form_tags($attrib);
AM 887     unset($attrib['form']);
f52c4f 888
5e3034 889     if (empty($attrib['id']))
AM 890         $attrib['id'] = 'rcmComposeBody';
087c7d 891
5e3034 892     $attrib['name'] = '_message';
087c7d 893
5e3034 894     $isHtml = $HTML_MODE;
f52c4f 895
5e3034 896     $out = $form_start ? "$form_start\n" : '';
1966c5 897
5e3034 898     $saveid = new html_hiddenfield(array('name' => '_draft_saveid', 'value' => $RCMAIL->output->get_env('draft_id')));
AM 899     $out .= $saveid->show();
1966c5 900
5e3034 901     $drafttoggle = new html_hiddenfield(array('name' => '_draft', 'value' => 'yes'));
AM 902     $out .= $drafttoggle->show();
1966c5 903
5e3034 904     $msgtype = new html_hiddenfield(array('name' => '_is_html', 'value' => ($isHtml ? "1" : "0")));
AM 905     $out .= $msgtype->show();
a0109c 906
5e3034 907     $framed = new html_hiddenfield(array('name' => '_framed', 'value' => '1'));
AM 908     $out .= $framed->show();
85e60a 909
5e3034 910     // If desired, set this textarea to be editable by TinyMCE
AM 911     if ($isHtml) {
912         $MESSAGE_BODY = htmlentities($MESSAGE_BODY, ENT_NOQUOTES, RCUBE_CHARSET);
913         $attrib['class'] = 'mce_editor';
914         $attrib['is_escaped'] = true;
915         $textarea = new html_textarea($attrib);
916         $out .= $textarea->show($MESSAGE_BODY);
917     }
918     else {
919         $textarea = new html_textarea($attrib);
920         $out .= $textarea->show('');
6084d7 921
5e3034 922         // quote plain text, inject into textarea
AM 923         $table = get_html_translation_table(HTML_SPECIALCHARS);
924         $MESSAGE_BODY = strtr($MESSAGE_BODY, $table);
925         $out = substr($out, 0, -11) . $MESSAGE_BODY . '</textarea>';
c287f3 926     }
66df08 927
5e3034 928     $out .= $form_end ? "\n$form_end" : '';
ed5d29 929
5e3034 930     $OUTPUT->set_env('composebody', $attrib['id']);
f52c4f 931
5e3034 932     // include HTML editor
AM 933     $RCMAIL->html_editor();
41fa0b 934
5e3034 935     // Set language list
f5d2ee 936     if ($RCMAIL->config->get('enable_spellcheck')) {
5e3034 937         $engine           = new rcube_spellchecker();
AM 938         $dictionary       = (bool) $RCMAIL->config->get('spellcheck_dictionary');
939         $spellcheck_langs = $engine->languages();
940         $lang             = $_SESSION['language'];
941
942         // if not found in the list, try with two-letter code
943         if (!$spellcheck_langs[$lang]) {
944             $lang = strtolower(substr($lang, 0, 2));
945         }
946
947         if (!$spellcheck_langs[$lang]) {
948             $lang = 'en';
949         }
950
951         $OUTPUT->set_env('spell_langs', $spellcheck_langs);
952         $OUTPUT->set_env('spell_lang', $lang);
953
954         $editor_lang_set = array();
955         foreach ($spellcheck_langs as $key => $name) {
956             $editor_lang_set[] = ($key == $lang ? '+' : '') . rcube::JQ($name).'='.rcube::JQ($key);
957         }
958
959         // include GoogieSpell
960         $OUTPUT->include_script('googiespell.js');
961         $OUTPUT->add_script(sprintf(
962             "var googie = new GoogieSpell('%s/images/googiespell/','%s&lang=', %s);\n".
963             "googie.lang_chck_spell = \"%s\";\n".
964             "googie.lang_rsm_edt = \"%s\";\n".
965             "googie.lang_close = \"%s\";\n".
966             "googie.lang_revert = \"%s\";\n".
967             "googie.lang_no_error_found = \"%s\";\n".
968             "googie.lang_learn_word = \"%s\";\n".
969             "googie.setLanguages(%s);\n".
970             "googie.setCurrentLanguage('%s');\n".
971             "googie.setDecoration(false);\n".
646b64 972             "googie.decorateTextarea('%s');\n",
681ba6 973             $RCMAIL->output->asset_url($RCMAIL->output->get_skin_path()),
5e3034 974             $RCMAIL->url(array('_task' => 'utils', '_action' => 'spell', '_remote' => 1)),
AM 975                 !empty($dictionary) ? 'true' : 'false',
976             rcube::JQ(rcube::Q($RCMAIL->gettext('checkspelling'))),
977             rcube::JQ(rcube::Q($RCMAIL->gettext('resumeediting'))),
978             rcube::JQ(rcube::Q($RCMAIL->gettext('close'))),
979             rcube::JQ(rcube::Q($RCMAIL->gettext('revertto'))),
980             rcube::JQ(rcube::Q($RCMAIL->gettext('nospellerrors'))),
981             rcube::JQ(rcube::Q($RCMAIL->gettext('addtodict'))),
982             rcube_output::json_serialize($spellcheck_langs),
983             $lang,
646b64 984             $attrib['id']), 'foot');
5e3034 985
AM 986         $OUTPUT->add_label('checking');
987         $OUTPUT->set_env('spellcheck_langs', join(',', $editor_lang_set));
988     }
989
b2992d 990     $out .= "\n".'<iframe name="savetarget" src="program/resources/blank.gif" style="width:0;height:0;border:none;visibility:hidden;" aria-hidden="true"></iframe>';
5e3034 991
AM 992     return $out;
4315b0 993 }
4e17e6 994
T 995
a0109c 996 function rcmail_create_reply_body($body, $bodyIsHtml)
4315b0 997 {
5e3034 998     global $RCMAIL, $MESSAGE, $LINE_LENGTH;
4e17e6 999
5e3034 1000     // build reply prefix
AM 1001     $from = array_pop(rcube_mime::decode_address_list($MESSAGE->get_header('from'), 1, false, $MESSAGE->headers->charset));
1002     $prefix = $RCMAIL->gettext(array(
1003         'name' => 'mailreplyintro',
1004         'vars' => array(
1005             'date'   => $RCMAIL->format_date($MESSAGE->headers->date, $RCMAIL->config->get('date_long')),
1006             'sender' => $from['name'] ? $from['name'] : rcube_utils::idn_to_utf8($from['mailto']),
1007         )
1008     ));
9f9664 1009
0b96b1 1010     $reply_mode = intval($RCMAIL->config->get('reply_mode'));
AM 1011
5e3034 1012     if (!$bodyIsHtml) {
AM 1013         $body = preg_replace('/\r?\n/', "\n", $body);
1014         $body = trim($body, "\n");
9f9664 1015
5e3034 1016         // soft-wrap and quote message text
AM 1017         $body = rcmail_wrap_and_quote($body, $LINE_LENGTH);
4e17e6 1018
5e3034 1019         $prefix .= "\n";
9f9664 1020
0b96b1 1021         if ($reply_mode > 0) { // top-posting
5e3034 1022             $prefix = "\n\n\n" . $prefix;
0b96b1 1023             $suffix = '';
AM 1024         }
1025         else {
1026             $suffix = "\n";
5e3034 1027         }
50f56d 1028     }
A 1029     else {
5e3034 1030         // save inline images to files
AM 1031         $cid_map = rcmail_write_inline_attachments($MESSAGE);
1032         // set is_safe flag (we need this for html body washing)
1033         rcmail_check_safe($MESSAGE);
1034         // clean up html tags
1035         $body = rcmail_wash_html($body, array('safe' => $MESSAGE->is_safe), $cid_map);
a0109c 1036
5e3034 1037         // build reply (quote content)
AM 1038         $prefix = '<p>' . rcube::Q($prefix) . "</p>\n";
1039         $prefix .= '<blockquote>';
1040
0b96b1 1041         if ($reply_mode > 0) { // top-posting
5e3034 1042             $prefix = '<br>' . $prefix;
AM 1043             $suffix = '</blockquote>';
1044         }
1045         else {
1046             $suffix = '</blockquote><p></p>';
1047         }
1048     }
1049
1050     return $prefix . $body . $suffix;
4315b0 1051 }
4e17e6 1052
T 1053
a0109c 1054 function rcmail_create_forward_body($body, $bodyIsHtml)
4315b0 1055 {
5e3034 1056     global $RCMAIL, $MESSAGE, $COMPOSE;
ec603f 1057
5e3034 1058     // add attachments
AM 1059     if (!isset($COMPOSE['forward_attachments']) && is_array($MESSAGE->mime_parts)) {
1060         $cid_map = rcmail_write_compose_attachments($MESSAGE, $bodyIsHtml);
1061     }
4e17e6 1062
5e3034 1063     $date = $RCMAIL->format_date($MESSAGE->headers->date, $RCMAIL->config->get('date_long'));
f5c108 1064
5e3034 1065     if (!$bodyIsHtml) {
AM 1066         $prefix = "\n\n\n-------- " . $RCMAIL->gettext('originalmessage') . " --------\n";
1067         $prefix .= $RCMAIL->gettext('subject') . ': ' . $MESSAGE->subject . "\n";
1068         $prefix .= $RCMAIL->gettext('date')    . ': ' . $date . "\n";
1069         $prefix .= $RCMAIL->gettext('from')    . ': ' . $MESSAGE->get_header('from') . "\n";
1070         $prefix .= $RCMAIL->gettext('to')      . ': ' . $MESSAGE->get_header('to') . "\n";
a4cf45 1071
5e3034 1072         if ($cc = $MESSAGE->headers->get('cc')) {
AM 1073             $prefix .= $RCMAIL->gettext('cc') . ': ' . $cc . "\n";
1074         }
1075         if (($replyto = $MESSAGE->headers->get('reply-to')) && $replyto != $MESSAGE->get_header('from')) {
1076             $prefix .= $RCMAIL->gettext('replyto') . ': ' . $replyto . "\n";
1077         }
a4cf45 1078
5e3034 1079         $prefix .= "\n";
AM 1080         $body = trim($body, "\r\n");
1081     }
1082     else {
1083         // set is_safe flag (we need this for html body washing)
1084         rcmail_check_safe($MESSAGE);
ec603f 1085
5e3034 1086         // clean up html tags
AM 1087         $body = rcmail_wash_html($body, array('safe' => $MESSAGE->is_safe), $cid_map);
5758b9 1088
5e3034 1089         $prefix = sprintf(
AM 1090             "<br /><p>-------- " . $RCMAIL->gettext('originalmessage') . " --------</p>" .
1091             "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody>" .
1092             "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">%s: </th><td>%s</td></tr>" .
1093             "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">%s: </th><td>%s</td></tr>" .
1094             "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">%s: </th><td>%s</td></tr>" .
1095             "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">%s: </th><td>%s</td></tr>",
1096             $RCMAIL->gettext('subject'), rcube::Q($MESSAGE->subject),
1097             $RCMAIL->gettext('date'), rcube::Q($date),
1098             $RCMAIL->gettext('from'), rcube::Q($MESSAGE->get_header('from'), 'replace'),
1099             $RCMAIL->gettext('to'), rcube::Q($MESSAGE->get_header('to'), 'replace'));
a4cf45 1100
5e3034 1101         if ($cc = $MESSAGE->headers->get('cc'))
AM 1102             $prefix .= sprintf("<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">%s: </th><td>%s</td></tr>",
1103                 $RCMAIL->gettext('cc'), rcube::Q($cc, 'replace'));
5758b9 1104
5e3034 1105         if (($replyto = $MESSAGE->headers->get('reply-to')) && $replyto != $MESSAGE->get_header('from'))
AM 1106             $prefix .= sprintf("<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">%s: </th><td>%s</td></tr>",
1107                 $RCMAIL->gettext('replyto'), rcube::Q($replyto, 'replace'));
f52c4f 1108
5e3034 1109         $prefix .= "</tbody></table><br>";
AM 1110     }
1111
1112     return $prefix . $body;
4315b0 1113 }
4e17e6 1114
8d4bcd 1115
a0109c 1116 function rcmail_create_draft_body($body, $bodyIsHtml)
4315b0 1117 {
5e3034 1118     global $MESSAGE, $COMPOSE;
f52c4f 1119
5e3034 1120     // add attachments
AM 1121     // sizeof($MESSAGE->mime_parts can be 1 - e.g. attachment, but no text!
1122     if (empty($COMPOSE['forward_attachments'])
1123         && is_array($MESSAGE->mime_parts)
1124         && count($MESSAGE->mime_parts) > 0
1125     ) {
1126         $cid_map = rcmail_write_compose_attachments($MESSAGE, $bodyIsHtml);
f2a9a9 1127     }
f52c4f 1128
5e3034 1129     // clean up HTML tags - XSS prevention (#1489251)
AM 1130     if ($bodyIsHtml) {
1131         $body = rcmail_wash_html($body, array('safe' => 1), $cid_map);
1132
1133         // remove comments (produced by washtml)
1134         $body = preg_replace('/<!--[^>]+-->/', '', $body);
1135
1136         // replace cid with href in inline images links
1137         if (!empty($cid_map)) {
1138             $body = str_replace(array_keys($cid_map), array_values($cid_map), $body);
1139         }
1140     }
1141
1142     return $body;
4315b0 1143 }
ba12c7 1144
A 1145
1146 function rcmail_remove_signature($body)
1147 {
5e3034 1148     global $RCMAIL;
ba12c7 1149
5e3034 1150     $body = str_replace("\r\n", "\n", $body);
AM 1151     $len  = strlen($body);
1152     $sig_max_lines = $RCMAIL->config->get('sig_max_lines', 15);
ba12c7 1153
5e3034 1154     while (($sp = strrpos($body, "-- \n", $sp ? -$len+$sp-1 : 0)) !== false) {
AM 1155         if ($sp == 0 || $body[$sp-1] == "\n") {
1156             // do not touch blocks with more that X lines
1157             if (substr_count($body, "\n", $sp) < $sig_max_lines) {
1158                 $body = substr($body, 0, max(0, $sp-1));
1159             }
1160             break;
1161         }
ba12c7 1162     }
A 1163
5e3034 1164     return $body;
ba12c7 1165 }
A 1166
1167
1bc48e 1168 function rcmail_write_compose_attachments(&$message, $bodyIsHtml)
4315b0 1169 {
5e3034 1170     global $RCMAIL, $COMPOSE, $compose_mode;
5503cc 1171
5e3034 1172     $loaded_attachments = array();
AM 1173     foreach ((array)$COMPOSE['attachments'] as $attachment) {
1174         $loaded_attachments[$attachment['name'] . $attachment['mimetype']] = $attachment;
8d4bcd 1175     }
cc97ea 1176
5e3034 1177     $cid_map  = array();
AM 1178     $messages = array();
ec603f 1179
5e3034 1180     foreach ((array)$message->mime_parts as $pid => $part) {
c82d09 1181         if ($part->mimetype == 'message/rfc822') {
AM 1182             $messages[] = $part->mime_id;
1183         }
1184
5e3034 1185         if ($part->disposition == 'attachment' || ($part->disposition == 'inline' && $bodyIsHtml) || $part->filename) {
AM 1186             // skip parts that aren't valid attachments
1187             if ($part->ctype_primary == 'multipart' || $part->mimetype == 'application/ms-tnef') {
1188                 continue;
1189             }
1190
1191             // skip message attachments in reply mode
1192             if ($part->ctype_primary == 'message' && $compose_mode == RCUBE_COMPOSE_REPLY) {
1193                 continue;
1194             }
1195
1196             // skip inline images when forwarding in text mode
1197             if ($part->content_id && $part->disposition == 'inline' && !$bodyIsHtml && $compose_mode == RCUBE_COMPOSE_FORWARD) {
1198                 continue;
1199             }
1200
c82d09 1201             // skip attachments included in message/rfc822 attachment (#1486487, #1490607)
AM 1202             foreach ($messages as $mimeid) {
1203                 if (strpos($part->mime_id, $mimeid . '.') === 0) {
1204                     continue 2;
5e3034 1205                 }
AM 1206             }
1207
1208             if (($attachment = $loaded_attachments[rcmail_attachment_name($part) . $part->mimetype])
1209                 || ($attachment = rcmail_save_attachment($message, $pid))
1210             ) {
1211                 $COMPOSE['attachments'][$attachment['id']] = $attachment;
1212
1213                 if ($bodyIsHtml && ($part->content_id || $part->content_location)) {
1214                     $url = sprintf('%s&_id=%s&_action=display-attachment&_file=rcmfile%s',
1215                         $RCMAIL->comm_path, $COMPOSE['id'], $attachment['id']);
1216
1217                     if ($part->content_id)
1218                         $cid_map['cid:'.$part->content_id] = $url;
1219                     else
1220                         $cid_map[$part->content_location] = $url;
1221                 }
1222             }
1223         }
1224     }
1225
1226     $COMPOSE['forward_attachments'] = true;
1227
1228     return $cid_map;
4315b0 1229 }
4e17e6 1230
T 1231
2f746d 1232 function rcmail_write_inline_attachments(&$message)
A 1233 {
5e3034 1234     global $RCMAIL, $COMPOSE;
ec603f 1235
c82d09 1236     $cid_map  = array();
AM 1237     $messages = array();
1238
5e3034 1239     foreach ((array)$message->mime_parts as $pid => $part) {
c82d09 1240         if ($part->mimetype == 'message/rfc822') {
AM 1241             $messages[] = $part->mime_id;
1242         }
1243
5e3034 1244         if (($part->content_id || $part->content_location) && $part->filename) {
c82d09 1245             // skip attachments included in message/rfc822 attachment (#1486487, #1490607)
AM 1246             foreach ($messages as $mimeid) {
1247                 if (strpos($part->mime_id, $mimeid . '.') === 0) {
1248                     continue 2;
1249                 }
1250             }
1251
5e3034 1252             if ($attachment = rcmail_save_attachment($message, $pid)) {
AM 1253                 $COMPOSE['attachments'][$attachment['id']] = $attachment;
1254                 $url = sprintf('%s&_id=%s&_action=display-attachment&_file=rcmfile%s',
1255                     $RCMAIL->comm_path, $COMPOSE['id'], $attachment['id']);
1256
1257                 if ($part->content_id)
1258                     $cid_map['cid:'.$part->content_id] = $url;
1259                 else
1260                     $cid_map[$part->content_location] = $url;
1261             }
1262         }
2f746d 1263     }
8f2b46 1264
5e3034 1265     return $cid_map;
2f746d 1266 }
A 1267
d9f109 1268 // Creates attachment(s) from the forwarded message(s)
AM 1269 function rcmail_write_forward_attachments()
a208a4 1270 {
5e3034 1271     global $RCMAIL, $COMPOSE, $MESSAGE;
a208a4 1272
5e3034 1273     $storage = $RCMAIL->get_storage();
AM 1274     $names   = array();
73076d 1275     $refs    = array();
d9f109 1276
5e3034 1277     $loaded_attachments = array();
AM 1278     foreach ((array)$COMPOSE['attachments'] as $attachment) {
1279         $loaded_attachments[$attachment['name'] . $attachment['mimetype']] = $attachment;
d9f109 1280     }
AM 1281
5e3034 1282     if ($COMPOSE['forward_uid'] == '*') {
AM 1283         $index = $storage->index(null, rcmail_sort_column(), rcmail_sort_order());
1284         $COMPOSE['forward_uid'] = $index->get();
d9f109 1285     }
aafbe8 1286     else if (!is_array($COMPOSE['forward_uid']) && strpos($COMPOSE['forward_uid'], ':')) {
03de13 1287         $COMPOSE['forward_uid'] = rcube_imap_generic::uncompressMessageSet($COMPOSE['forward_uid']);
AM 1288     }
aafbe8 1289     else if (is_string($COMPOSE['forward_uid'])) {
5e3034 1290         $COMPOSE['forward_uid'] = explode(',', $COMPOSE['forward_uid']);
d9f109 1291     }
AM 1292
5e3034 1293     foreach ((array)$COMPOSE['forward_uid'] as $uid) {
AM 1294         $message = new rcube_message($uid);
d9f109 1295
5e3034 1296         if (empty($message->headers)) {
AM 1297             continue;
1298         }
d9f109 1299
5e3034 1300         if (!empty($message->headers->charset)) {
AM 1301             $storage->set_charset($message->headers->charset);
1302         }
1303
1304         if (empty($MESSAGE->subject)) {
1305             $MESSAGE->subject = $message->subject;
1306         }
1307
1308         // generate (unique) attachment name
1309         $name = strlen($message->subject) ? mb_substr($message->subject, 0, 64) : 'message_rfc822';
1310         if (!empty($names[$name])) {
1311             $names[$name]++;
1312             $name .= '_' . $names[$name];
1313         }
1314         $names[$name] = 1;
1315         $name .= '.eml';
1316
1317         $data = $path = null;
1318
1319         if (!empty($loaded_attachments[$name . 'message/rfc822'])) {
1320             continue;
1321         }
1322
1323         // don't load too big attachments into memory
1324         if (!rcube_utils::mem_check($message->size)) {
1325             $temp_dir = unslashify($RCMAIL->config->get('temp_dir'));
1326             $path     = tempnam($temp_dir, 'rcmAttmnt');
1327             if ($fp = fopen($path, 'w')) {
1328                 $storage->get_raw_body($message->uid, $fp);
1329                 fclose($fp);
1330             }
1331             else {
1332                 return false;
1333             }
1334         }
1335         else {
1336             $data = $storage->get_raw_body($message->uid);
1337         }
1338
1339         $attachment = array(
1340             'group'    => $COMPOSE['id'],
1341             'name'     => $name,
1342             'mimetype' => 'message/rfc822',
1343             'data'     => $data,
1344             'path'     => $path,
1345             'size'     => $path ? filesize($path) : strlen($data),
1346         );
1347
1348         $attachment = $RCMAIL->plugins->exec_hook('attachment_save', $attachment);
1349
1350         if ($attachment['status']) {
1351             unset($attachment['data'], $attachment['status'], $attachment['content_id'], $attachment['abort']);
1352             $COMPOSE['attachments'][$attachment['id']] = $attachment;
1353         }
1354         else if ($path) {
1355             @unlink($path);
1356         }
73076d 1357
AM 1358         if ($message->headers->messageID) {
1359             $refs[] = $message->headers->messageID;
1360         }
1361     }
1362
1363     // set In-Reply-To and References headers
1364     if (count($refs) == 1) {
1365         $COMPOSE['reply_msgid'] = $refs[0];
1366     }
1367     if (!empty($refs)) {
1368         $COMPOSE['references'] = implode(' ', $refs);
d9f109 1369     }
a208a4 1370 }
A 1371
1372
2f746d 1373 function rcmail_save_attachment(&$message, $pid)
A 1374 {
5e3034 1375     global $COMPOSE;
72ff6a 1376
5e3034 1377     $rcmail = rcmail::get_instance();
AM 1378     $part   = $message->mime_parts[$pid];
1379     $data   = $path = null;
a64064 1380
5e3034 1381     // don't load too big attachments into memory
AM 1382     if (!rcube_utils::mem_check($part->size)) {
1383         $temp_dir = unslashify($rcmail->config->get('temp_dir'));
1384         $path     = tempnam($temp_dir, 'rcmAttmnt');
a64064 1385
5e3034 1386         if ($fp = fopen($path, 'w')) {
48ba44 1387             $message->get_part_body($pid, false, 0, $fp);
5e3034 1388             fclose($fp);
AM 1389         }
1390         else {
1391             return false;
1392         }
1393     }
1394     else {
48ba44 1395         $data = $message->get_part_body($pid);
5e3034 1396     }
0c2596 1397
5e3034 1398     $mimetype = $part->ctype_primary . '/' . $part->ctype_secondary;
AM 1399     $filename = rcmail_attachment_name($part);
3b1426 1400
5e3034 1401     $attachment = array(
AM 1402         'group'      => $COMPOSE['id'],
1403         'name'       => $filename,
1404         'mimetype'   => $mimetype,
1405         'content_id' => $part->content_id,
1406         'data'       => $data,
1407         'path'       => $path,
1408         'size'       => $path ? filesize($path) : strlen($data),
d165d1 1409         'charset'    => $part->charset,
5e3034 1410     );
a64064 1411
5e3034 1412     $attachment = $rcmail->plugins->exec_hook('attachment_save', $attachment);
f52c4f 1413
5e3034 1414     if ($attachment['status']) {
AM 1415         unset($attachment['data'], $attachment['status'], $attachment['content_id'], $attachment['abort']);
1416         return $attachment;
1417     }
1418     else if ($path) {
1419         @unlink($path);
1420     }
1421
1422     return false;
2f746d 1423 }
A 1424
c6efcf 1425 function rcmail_save_image($path, $mimetype = '', $data = null)
b575fa 1426 {
5e3034 1427     global $COMPOSE;
72ff6a 1428
5e3034 1429     // handle attachments in memory
c6efcf 1430     if (empty($data)) {
AM 1431         $data    = file_get_contents($path);
1432         $is_file = true;
1433     }
1434
5e3034 1435     $name = rcmail_basename($path);
c6efcf 1436
AM 1437     if (empty($mimetype)) {
1438         if ($is_file) {
1439             $mimetype = rcube_mime::file_content_type($path, $name);
1440         }
1441         else {
1442             $mimetype = rcube_mime::file_content_type($data, $name, 'application/octet-stream', true);
1443         }
1444     }
b575fa 1445
5e3034 1446     $attachment = array(
AM 1447         'group'    => $COMPOSE['id'],
1448         'name'     => $name,
c6efcf 1449         'mimetype' => $mimetype,
5e3034 1450         'data'     => $data,
AM 1451         'size'     => strlen($data),
1452     );
b575fa 1453
5e3034 1454     $attachment = rcmail::get_instance()->plugins->exec_hook('attachment_save', $attachment);
b575fa 1455
5e3034 1456     if ($attachment['status']) {
AM 1457         unset($attachment['data'], $attachment['status'], $attachment['content_id'], $attachment['abort']);
1458         return $attachment;
1459     }
f52c4f 1460
5e3034 1461     return false;
b575fa 1462 }
A 1463
1464 function rcmail_basename($filename)
1465 {
5e3034 1466     // basename() is not unicode safe and locale dependent
AM 1467     if (stristr(PHP_OS, 'win') || stristr(PHP_OS, 'netware')) {
1468         return preg_replace('/^.*[\\\\\\/]/', '', $filename);
1469     }
1470     else {
1471         return preg_replace('/^.*[\/]/', '', $filename);
1472     }
b575fa 1473 }
2f746d 1474
4e17e6 1475 function rcmail_compose_subject($attrib)
4315b0 1476 {
5e3034 1477     global $MESSAGE, $COMPOSE, $compose_mode;
72ff6a 1478
5e3034 1479     list($form_start, $form_end) = get_form_tags($attrib);
AM 1480     unset($attrib['form']);
72ff6a 1481
5e3034 1482     $attrib['name']       = '_subject';
AM 1483     $attrib['spellcheck'] = 'true';
4e17e6 1484
5e3034 1485     $textfield = new html_inputfield($attrib);
AM 1486     $subject   = '';
4e17e6 1487
5e3034 1488     // use subject from post
AM 1489     if (isset($_POST['_subject'])) {
1490         $subject = rcube_utils::get_input_value('_subject', rcube_utils::INPUT_POST, TRUE);
1491     }
968601 1492     else if (!empty($COMPOSE['param']['subject'])) {
AM 1493         $subject = $COMPOSE['param']['subject'];
1494     }
5e3034 1495     // create a reply-subject
AM 1496     else if ($compose_mode == RCUBE_COMPOSE_REPLY) {
1497         if (preg_match('/^re:/i', $MESSAGE->subject))
1498             $subject = $MESSAGE->subject;
1499         else
1500             $subject = 'Re: '.$MESSAGE->subject;
3dbfb5 1501
AM 1502         // replace (was: ...) (#1489375)
1503         $subject = preg_replace('/\s*\([wW]as:[^\)]+\)\s*$/', '', $subject);
5e3034 1504     }
AM 1505     // create a forward-subject
1506     else if ($compose_mode == RCUBE_COMPOSE_FORWARD) {
1507         if (preg_match('/^fwd:/i', $MESSAGE->subject))
1508             $subject = $MESSAGE->subject;
1509         else
1510             $subject = 'Fwd: '.$MESSAGE->subject;
1511     }
1512     // creeate a draft-subject
1513     else if ($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT) {
1514         $subject = $MESSAGE->subject;
1515     }
72ff6a 1516
5e3034 1517     $out = $form_start ? "$form_start\n" : '';
AM 1518     $out .= $textfield->show($subject);
1519     $out .= $form_end ? "\n$form_end" : '';
e99991 1520
5e3034 1521     return $out;
4315b0 1522 }
4e17e6 1523
T 1524
1525 function rcmail_compose_attachment_list($attrib)
4315b0 1526 {
f5d2ee 1527     global $RCMAIL, $OUTPUT, $COMPOSE;
72ff6a 1528
5e3034 1529     // add ID if not given
AM 1530     if (!$attrib['id'])
1531         $attrib['id'] = 'rcmAttachmentList';
72ff6a 1532
8ccfc2 1533     $out    = "\n";
AM 1534     $jslist = array();
1535     $button = '';
087c7d 1536
5e3034 1537     if (is_array($COMPOSE['attachments'])) {
AM 1538         if ($attrib['deleteicon']) {
1539             $button = html::img(array(
8ccfc2 1540                 'src' => $RCMAIL->output->abs_url($attrib['deleteicon'], true),
5e3034 1541                 'alt' => $RCMAIL->gettext('delete')
AM 1542             ));
1543         }
1544         else if (rcube_utils::get_boolean($attrib['textbuttons'])) {
1545             $button = rcube::Q($RCMAIL->gettext('delete'));
1546         }
1547
1548         foreach ($COMPOSE['attachments'] as $id => $a_prop) {
1549             if (empty($a_prop)) {
1550                 continue;
1551             }
1552
1553             $out .= html::tag('li', array(
1554                     'id'          => 'rcmfile'.$id,
1555                     'class'       => rcube_utils::file2class($a_prop['mimetype'], $a_prop['name']),
1556                     'onmouseover' => "rcube_webmail.long_subject_title_ex(this, 0)",
1557                 ),
1558                 html::a(array(
1559                         'href'    => "#delete",
1560                         'title'   => $RCMAIL->gettext('delete'),
1561                         'onclick' => sprintf("return %s.command('remove-attachment','rcmfile%s', this)", rcmail_output::JS_OBJECT_NAME, $id),
9240c9 1562                         'class'   => 'delete',
TB 1563                         'tabindex' => $attrib['tabindex'] ?: '0',
1564                         'aria-label'   => $RCMAIL->gettext('delete') . ' ' . $a_prop['name'],
5e3034 1565                     ),
AM 1566                     $button
1567                 ) . rcube::Q($a_prop['name'])
1568             );
1569
1570             $jslist['rcmfile'.$id] = array(
1571                 'name'     => $a_prop['name'],
1572                 'complete' => true,
1573                 'mimetype' => $a_prop['mimetype']
1574             );
1575         }
2efe33 1576     }
a894ba 1577
5e3034 1578     if ($attrib['deleteicon'])
8ccfc2 1579         $COMPOSE['deleteicon'] = $RCMAIL->output->abs_url($attrib['deleteicon'], true);
5e3034 1580     else if (rcube_utils::get_boolean($attrib['textbuttons']))
AM 1581         $COMPOSE['textbuttons'] = true;
1582     if ($attrib['cancelicon'])
8ccfc2 1583         $OUTPUT->set_env('cancelicon', $RCMAIL->output->abs_url($attrib['cancelicon'], true));
5e3034 1584     if ($attrib['loadingicon'])
8ccfc2 1585         $OUTPUT->set_env('loadingicon', $RCMAIL->output->abs_url($attrib['loadingicon'], true));
72ff6a 1586
5e3034 1587     $OUTPUT->set_env('attachments', $jslist);
AM 1588     $OUTPUT->add_gui_object('attachmentlist', $attrib['id']);
7152f5 1589
9240c9 1590     // put tabindex value into data-tabindex attribute
TB 1591     if (isset($attrib['tabindex'])) {
1592         $attrib['data-tabindex'] = $attrib['tabindex'];
1593         unset($attrib['tabindex']);
1594     }
1595
5e3034 1596     return html::tag('ul', $attrib, $out, html::$common_attrib);
4315b0 1597 }
4e17e6 1598
T 1599
1600 function rcmail_compose_attachment_form($attrib)
4315b0 1601 {
5e3034 1602     global $OUTPUT, $RCMAIL;
4e17e6 1603
5e3034 1604     // set defaults
AM 1605     $attrib += array('id' => 'rcmUploadbox', 'buttons' => 'yes');
7df0e3 1606
5e3034 1607     // Get filesize, enable upload progress bar
AM 1608     $max_filesize = $RCMAIL->upload_init();
4171c5 1609
5e3034 1610     $button  = new html_inputfield(array('type' => 'button'));
AM 1611     $content = html::div(null, rcmail_compose_attachment_field())
1612         . html::div('hint', $RCMAIL->gettext(array('name' => 'maxuploadsize', 'vars' => array('size' => $max_filesize))));
fe0cb6 1613
5e3034 1614     if (rcube_utils::get_boolean($attrib['buttons'])) {
AM 1615         $content .= html::div('buttons',
1616             $button->show($RCMAIL->gettext('close'), array('class' => 'button', 'onclick' => "$('#$attrib[id]').hide()")) . ' ' .
1617             $button->show($RCMAIL->gettext('upload'), array('class' => 'button mainaction', 'onclick' => rcmail_output::JS_OBJECT_NAME . ".command('send-attachment', this.form)"))
1618         );
1619     }
fe0cb6 1620
5e3034 1621     $out = html::div($attrib, $OUTPUT->form_tag(array(
AM 1622             'id'      => $attrib['id'] . 'Frm',
1623             'name'    => 'uploadform',
1624             'method'  => 'post',
1625             'enctype' => 'multipart/form-data'
1626         ), $content
1627     ));
1628
1629     $OUTPUT->add_gui_object('uploadform', $attrib['id'] . 'Frm');
1630
1631     return $out;
4315b0 1632 }
4e17e6 1633
T 1634
5a8ee3 1635 function rcmail_compose_attachment_field($attrib = array())
4315b0 1636 {
5e3034 1637     $attrib['type']     = 'file';
AM 1638     $attrib['name']     = '_attachments[]';
1639     $attrib['multiple'] = 'multiple';
7fc056 1640
5e3034 1641     $field = new html_inputfield($attrib);
AM 1642
1643     return $field->show();
4315b0 1644 }
4e17e6 1645
66e2bf 1646
4e17e6 1647 function rcmail_priority_selector($attrib)
4315b0 1648 {
5e3034 1649     global $RCMAIL, $MESSAGE;
7152f5 1650
5e3034 1651     list($form_start, $form_end) = get_form_tags($attrib);
AM 1652     unset($attrib['form']);
8958d0 1653
5e3034 1654     $attrib['name'] = '_priority';
AM 1655     $prio_list = array(
1656         $RCMAIL->gettext('lowest')  => 5,
1657         $RCMAIL->gettext('low')     => 4,
1658         $RCMAIL->gettext('normal')  => 0,
1659         $RCMAIL->gettext('high')    => 2,
1660         $RCMAIL->gettext('highest') => 1,
1661     );
4e17e6 1662
5e3034 1663     $selector = new html_select($attrib);
AM 1664     $selector->add(array_keys($prio_list), array_values($prio_list));
8958d0 1665
5e3034 1666     if (isset($_POST['_priority']))
AM 1667         $sel = $_POST['_priority'];
1668     else if (isset($MESSAGE->headers->priority) && intval($MESSAGE->headers->priority) != 3)
1669         $sel = $MESSAGE->headers->priority;
1670     else
1671         $sel = 0;
4e17e6 1672
5e3034 1673     $out = $form_start ? "$form_start\n" : '';
ddc161 1674     $out .= $selector->show((int) $sel);
5e3034 1675     $out .= $form_end ? "\n$form_end" : '';
8958d0 1676
5e3034 1677     return $out;
4315b0 1678 }
4e17e6 1679
T 1680
f65021 1681 function rcmail_mdn_checkbox($attrib)
4315b0 1682 {
5e3034 1683     global $RCMAIL, $MESSAGE, $compose_mode;
8958d0 1684
5e3034 1685     list($form_start, $form_end) = get_form_tags($attrib);
AM 1686     unset($attrib['form']);
8958d0 1687
5e3034 1688     if (!isset($attrib['id']))
AM 1689         $attrib['id'] = 'receipt';
620439 1690
f65021 1691     $attrib['name']  = '_mdn';
5e3034 1692     $attrib['value'] = '1';
620439 1693
5e3034 1694     $checkbox = new html_checkbox($attrib);
b3660b 1695
f65021 1696     if (isset($_POST['_mdn']))
AM 1697         $mdn_default = $_POST['_mdn'];
5e3034 1698     else if (in_array($compose_mode, array(RCUBE_COMPOSE_DRAFT, RCUBE_COMPOSE_EDIT)))
AM 1699         $mdn_default = (bool) $MESSAGE->headers->mdn_to;
1700     else
1701         $mdn_default = $RCMAIL->config->get('mdn_default');
620439 1702
5e3034 1703     $out = $form_start ? "$form_start\n" : '';
AM 1704     $out .= $checkbox->show($mdn_default);
1705     $out .= $form_end ? "\n$form_end" : '';
1706
1707     return $out;
4315b0 1708 }
620439 1709
T 1710
f22ea7 1711 function rcmail_dsn_checkbox($attrib)
A 1712 {
5e3034 1713     global $RCMAIL;
f22ea7 1714
5e3034 1715     list($form_start, $form_end) = get_form_tags($attrib);
AM 1716     unset($attrib['form']);
f22ea7 1717
5e3034 1718     if (!isset($attrib['id']))
AM 1719         $attrib['id'] = 'dsn';
f22ea7 1720
5e3034 1721     $attrib['name']  = '_dsn';
AM 1722     $attrib['value'] = '1';
f22ea7 1723
5e3034 1724     $checkbox = new html_checkbox($attrib);
271efe 1725
5e3034 1726     if (isset($_POST['_dsn']))
AM 1727         $dsn_value = (int) $_POST['_dsn'];
1728     else
1729         $dsn_value = $RCMAIL->config->get('dsn_default');
f22ea7 1730
5e3034 1731     $out = $form_start ? "$form_start\n" : '';
AM 1732     $out .= $checkbox->show($dsn_value);
1733     $out .= $form_end ? "\n$form_end" : '';
1734
1735     return $out;
f22ea7 1736 }
A 1737
1738
a0109c 1739 function rcmail_editor_selector($attrib)
S 1740 {
5e3034 1741     global $RCMAIL;
6b2b2e 1742
5e3034 1743     // determine whether HTML or plain text should be checked
AM 1744     $useHtml = rcmail_compose_editor_mode();
d9344f 1745
5e3034 1746     if (empty($attrib['editorid']))
AM 1747         $attrib['editorid'] = 'rcmComposeBody';
79af0b 1748
5e3034 1749     if (empty($attrib['name']))
AM 1750         $attrib['name'] = 'editorSelect';
8958d0 1751
646b64 1752     $attrib['onchange'] = "return rcmail.command('toggle-editor', {id: '".$attrib['editorid']."', html: this.value == 'html'}, '', event)";
309d2f 1753
5e3034 1754     $select = new html_select($attrib);
309d2f 1755
5e3034 1756     $select->add(rcube::Q($RCMAIL->gettext('htmltoggle')), 'html');
AM 1757     $select->add(rcube::Q($RCMAIL->gettext('plaintoggle')), 'plain');
309d2f 1758
5e3034 1759     return $select->show($useHtml ? 'html' : 'plain');
a0109c 1760 }
S 1761
1762
faf876 1763 function rcmail_store_target_selection($attrib)
T 1764 {
5e3034 1765     global $COMPOSE, $RCMAIL;
72ff6a 1766
5e3034 1767     $attrib['name'] = '_store_target';
AM 1768     $select = $RCMAIL->folder_selector(array_merge($attrib, array(
1769         'noselection'   => '- ' . $RCMAIL->gettext('dontsave') . ' -',
1770         'folder_filter' => 'mail',
1771         'folder_rights' => 'w',
1772     )));
1773
1774     return $select->show(isset($_POST['_store_target']) ? $_POST['_store_target'] : $COMPOSE['param']['sent_mbox'], $attrib);
faf876 1775 }
T 1776
1777
eeb85f 1778 function rcmail_check_sent_folder($folder, $create=false)
A 1779 {
5e3034 1780     global $RCMAIL;
eeb85f 1781
5e3034 1782     // we'll not save the message, so it doesn't matter
AM 1783     if ($RCMAIL->config->get('no_save_sent_messages')) {
1784         return true;
1785     }
e04e31 1786
5e3034 1787     if ($RCMAIL->storage->folder_exists($folder, true)) {
AM 1788         return true;
1789     }
eeb85f 1790
5e3034 1791     // folder may exist but isn't subscribed (#1485241)
AM 1792     if ($create) {
1793         if (!$RCMAIL->storage->folder_exists($folder))
1794             return $RCMAIL->storage->create_folder($folder, true);
1795         else
1796             return $RCMAIL->storage->subscribe($folder);
1797     }
eeb85f 1798
5e3034 1799     return false;
eeb85f 1800 }
A 1801
1802
4e17e6 1803 function get_form_tags($attrib)
4315b0 1804 {
5e3034 1805     global $RCMAIL, $MESSAGE_FORM, $COMPOSE;
4e17e6 1806
5e3034 1807     $form_start = '';
AM 1808     if (!$MESSAGE_FORM) {
1809         $hiddenfields = new html_hiddenfield(array('name' => '_task', 'value' => $RCMAIL->task));
1810         $hiddenfields->add(array('name' => '_action', 'value' => 'send'));
1811         $hiddenfields->add(array('name' => '_id', 'value' => $COMPOSE['id']));
1812         $hiddenfields->add(array('name' => '_attachments'));
1966c5 1813
5e3034 1814         $form_start = empty($attrib['form']) ? $RCMAIL->output->form_tag(array('name' => "form", 'method' => "post")) : '';
AM 1815         $form_start .= $hiddenfields->show();
1816     }
eeb85f 1817
5e3034 1818     $form_end = ($MESSAGE_FORM && !strlen($attrib['form'])) ? '</form>' : '';
AM 1819     $form_name = !empty($attrib['form']) ? $attrib['form'] : 'form';
eeb85f 1820
5e3034 1821     if (!$MESSAGE_FORM)
AM 1822         $RCMAIL->output->add_gui_object('messageform', $form_name);
eeb85f 1823
5e3034 1824     $MESSAGE_FORM = $form_name;
4e17e6 1825
5e3034 1826     return array($form_start, $form_end);
4315b0 1827 }
4e17e6 1828
T 1829
635722 1830 function rcmail_addressbook_list($attrib = array())
eeb73c 1831 {
T 1832     global $RCMAIL, $OUTPUT;
1833
1834     $attrib += array('id' => 'rcmdirectorylist');
1835
1836     $out = '';
1837     $line_templ = html::tag('li', array(
1838         'id' => 'rcmli%s', 'class' => '%s'),
1839         html::a(array('href' => '#list',
1840             'rel' => '%s',
6b2b2e 1841             'onclick' => "return ".rcmail_output::JS_OBJECT_NAME.".command('list-adresses','%s',this)"), '%s'));
eeb73c 1842
65dff8 1843     foreach ($RCMAIL->get_address_sources(false, true) as $j => $source) {
eeb73c 1844         $id = strval(strlen($source['id']) ? $source['id'] : $j);
6b2b2e 1845         $js_id = rcube::JQ($id);
eeb73c 1846
T 1847         // set class name(s)
1848         $class_name = 'addressbook';
1849         if ($source['class_name'])
1850             $class_name .= ' ' . $source['class_name'];
1851
1852         $out .= sprintf($line_templ,
6b2b2e 1853             rcube_utils::html_identifier($id,true),
eeb73c 1854             $class_name,
T 1855             $source['id'],
abe164 1856             $js_id, (!empty($source['name']) ? $source['name'] : $id));
eeb73c 1857     }
T 1858
635722 1859     $OUTPUT->add_gui_object('addressbookslist', $attrib['id']);
eeb73c 1860
T 1861     return html::tag('ul', $attrib, $out, html::$common_attrib);
1862 }
1863
1864 // return the contacts list as HTML table
1865 function rcmail_contacts_list($attrib = array())
1866 {
6b2b2e 1867     global $RCMAIL, $OUTPUT;
eeb73c 1868
T 1869     $attrib += array('id' => 'rcmAddressList');
1870
1871     // set client env
1872     $OUTPUT->add_gui_object('contactslist', $attrib['id']);
1873     $OUTPUT->set_env('pagecount', 0);
1874     $OUTPUT->set_env('current_page', 0);
1875     $OUTPUT->include_script('list.js');
1876
6b2b2e 1877     return $RCMAIL->table_output($attrib, array(), array('name'), 'ID');
eeb73c 1878 }
T 1879
1880
ae6d2d 1881 /**
TB 1882  * Register a certain container as active area to drop files onto
1883  */
1884 function compose_file_drop_area($attrib)
1885 {
1886     global $OUTPUT;
1887
1888     if ($attrib['id']) {
1889         $OUTPUT->add_gui_object('filedrop', $attrib['id']);
1890         $OUTPUT->set_env('filedrop', array('action' => 'upload', 'fieldname' => '_attachments'));
1891     }
1892 }
1893
eeb73c 1894
0b1de8 1895 /**
TB 1896  *
1897  */
1898 function rcmail_compose_responses_list($attrib)
1899 {
1900     global $RCMAIL, $OUTPUT;
1901
1902     $attrib += array('id' => 'rcmresponseslist', 'tagname' => 'ul', 'cols' => 1);
1903
1904     $jsenv = array();
0ce212 1905     $list = new html_table($attrib);
TB 1906     foreach ($RCMAIL->get_compose_responses(true) as $response) {
1907         $key = $response['key'];
1908         $item = html::a(array(
b2992d 1909             'href' => '#'.urlencode($response['name']),
0b1de8 1910             'class' => rtrim('insertresponse ' . $attrib['itemclass']),
0933d6 1911             'unselectable' => 'on',
b2992d 1912             'tabindex' => '0',
0b1de8 1913             'rel' => $key,
6b2b2e 1914         ), rcube::Q($response['name']));
0b1de8 1915
TB 1916         $jsenv[$key] = $response;
1917         $list->add(array(), $item);
1918     }
1919
1920     // set client env
1921     $OUTPUT->set_env('textresponses', $jsenv);
1922     $OUTPUT->add_gui_object('responseslist', $attrib['id']);
1923
1924     return $list->show();
1925 }