Aleksander Machniak
2016-04-15 ead084693499934c467ca6a9c396367ac661dd61
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']) {
ead084 646                     $text = $RCMAIL->html2text($html, array('links' => false));
AM 647                     $text = trim($text);
5e3034 648                 }
AM 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
ead084 861             $len  = $compose_mode == RCUBE_COMPOSE_REPLY ? $LINE_LENGTH-2 : $LINE_LENGTH;
AM 862             $body = $RCMAIL->html2text($body, array('width' => $len));
33423a 863         }
A 864         else {
865             if ($part->ctype_secondary == 'plain' && $part->ctype_parameters['format'] == 'flowed') {
866                 $body = rcube_mime::unfold_flowed($body);
867             }
868
869             // try to remove the signature
a9bb50 870             if ($compose_mode != RCUBE_COMPOSE_DRAFT && $compose_mode != RCUBE_COMPOSE_EDIT) {
AM 871                 if ($RCMAIL->config->get('strip_existing_sig', true)) {
872                     $body = rcmail_remove_signature($body);
873                 }
33423a 874             }
A 875         }
876     }
877
878     return $body;
087c7d 879 }
A 880
881 function rcmail_compose_body($attrib)
882 {
f5d2ee 883     global $RCMAIL, $OUTPUT, $HTML_MODE, $MESSAGE_BODY;
f52c4f 884
5e3034 885     list($form_start, $form_end) = get_form_tags($attrib);
AM 886     unset($attrib['form']);
f52c4f 887
5e3034 888     if (empty($attrib['id']))
AM 889         $attrib['id'] = 'rcmComposeBody';
087c7d 890
5e3034 891     $attrib['name'] = '_message';
087c7d 892
5e3034 893     $isHtml = $HTML_MODE;
f52c4f 894
5e3034 895     $out = $form_start ? "$form_start\n" : '';
1966c5 896
5e3034 897     $saveid = new html_hiddenfield(array('name' => '_draft_saveid', 'value' => $RCMAIL->output->get_env('draft_id')));
AM 898     $out .= $saveid->show();
1966c5 899
5e3034 900     $drafttoggle = new html_hiddenfield(array('name' => '_draft', 'value' => 'yes'));
AM 901     $out .= $drafttoggle->show();
1966c5 902
5e3034 903     $msgtype = new html_hiddenfield(array('name' => '_is_html', 'value' => ($isHtml ? "1" : "0")));
AM 904     $out .= $msgtype->show();
a0109c 905
5e3034 906     $framed = new html_hiddenfield(array('name' => '_framed', 'value' => '1'));
AM 907     $out .= $framed->show();
85e60a 908
5e3034 909     // If desired, set this textarea to be editable by TinyMCE
AM 910     if ($isHtml) {
911         $MESSAGE_BODY = htmlentities($MESSAGE_BODY, ENT_NOQUOTES, RCUBE_CHARSET);
912         $attrib['class'] = 'mce_editor';
913         $attrib['is_escaped'] = true;
914         $textarea = new html_textarea($attrib);
915         $out .= $textarea->show($MESSAGE_BODY);
916     }
917     else {
918         $textarea = new html_textarea($attrib);
919         $out .= $textarea->show('');
6084d7 920
5e3034 921         // quote plain text, inject into textarea
AM 922         $table = get_html_translation_table(HTML_SPECIALCHARS);
923         $MESSAGE_BODY = strtr($MESSAGE_BODY, $table);
924         $out = substr($out, 0, -11) . $MESSAGE_BODY . '</textarea>';
c287f3 925     }
66df08 926
5e3034 927     $out .= $form_end ? "\n$form_end" : '';
ed5d29 928
5e3034 929     $OUTPUT->set_env('composebody', $attrib['id']);
f52c4f 930
5e3034 931     // include HTML editor
AM 932     $RCMAIL->html_editor();
41fa0b 933
5e3034 934     // Set language list
f5d2ee 935     if ($RCMAIL->config->get('enable_spellcheck')) {
5e3034 936         $engine           = new rcube_spellchecker();
AM 937         $dictionary       = (bool) $RCMAIL->config->get('spellcheck_dictionary');
938         $spellcheck_langs = $engine->languages();
939         $lang             = $_SESSION['language'];
940
941         // if not found in the list, try with two-letter code
942         if (!$spellcheck_langs[$lang]) {
943             $lang = strtolower(substr($lang, 0, 2));
944         }
945
946         if (!$spellcheck_langs[$lang]) {
947             $lang = 'en';
948         }
949
950         $OUTPUT->set_env('spell_langs', $spellcheck_langs);
951         $OUTPUT->set_env('spell_lang', $lang);
952
953         $editor_lang_set = array();
954         foreach ($spellcheck_langs as $key => $name) {
955             $editor_lang_set[] = ($key == $lang ? '+' : '') . rcube::JQ($name).'='.rcube::JQ($key);
956         }
957
958         // include GoogieSpell
959         $OUTPUT->include_script('googiespell.js');
960         $OUTPUT->add_script(sprintf(
961             "var googie = new GoogieSpell('%s/images/googiespell/','%s&lang=', %s);\n".
962             "googie.lang_chck_spell = \"%s\";\n".
963             "googie.lang_rsm_edt = \"%s\";\n".
964             "googie.lang_close = \"%s\";\n".
965             "googie.lang_revert = \"%s\";\n".
966             "googie.lang_no_error_found = \"%s\";\n".
967             "googie.lang_learn_word = \"%s\";\n".
968             "googie.setLanguages(%s);\n".
969             "googie.setCurrentLanguage('%s');\n".
970             "googie.setDecoration(false);\n".
646b64 971             "googie.decorateTextarea('%s');\n",
681ba6 972             $RCMAIL->output->asset_url($RCMAIL->output->get_skin_path()),
5e3034 973             $RCMAIL->url(array('_task' => 'utils', '_action' => 'spell', '_remote' => 1)),
AM 974                 !empty($dictionary) ? 'true' : 'false',
975             rcube::JQ(rcube::Q($RCMAIL->gettext('checkspelling'))),
976             rcube::JQ(rcube::Q($RCMAIL->gettext('resumeediting'))),
977             rcube::JQ(rcube::Q($RCMAIL->gettext('close'))),
978             rcube::JQ(rcube::Q($RCMAIL->gettext('revertto'))),
979             rcube::JQ(rcube::Q($RCMAIL->gettext('nospellerrors'))),
980             rcube::JQ(rcube::Q($RCMAIL->gettext('addtodict'))),
981             rcube_output::json_serialize($spellcheck_langs),
982             $lang,
646b64 983             $attrib['id']), 'foot');
5e3034 984
AM 985         $OUTPUT->add_label('checking');
986         $OUTPUT->set_env('spellcheck_langs', join(',', $editor_lang_set));
987     }
988
b2992d 989     $out .= "\n".'<iframe name="savetarget" src="program/resources/blank.gif" style="width:0;height:0;border:none;visibility:hidden;" aria-hidden="true"></iframe>';
5e3034 990
AM 991     return $out;
4315b0 992 }
4e17e6 993
T 994
a0109c 995 function rcmail_create_reply_body($body, $bodyIsHtml)
4315b0 996 {
5e3034 997     global $RCMAIL, $MESSAGE, $LINE_LENGTH;
4e17e6 998
5e3034 999     // build reply prefix
AM 1000     $from = array_pop(rcube_mime::decode_address_list($MESSAGE->get_header('from'), 1, false, $MESSAGE->headers->charset));
1001     $prefix = $RCMAIL->gettext(array(
1002         'name' => 'mailreplyintro',
1003         'vars' => array(
1004             'date'   => $RCMAIL->format_date($MESSAGE->headers->date, $RCMAIL->config->get('date_long')),
1005             'sender' => $from['name'] ? $from['name'] : rcube_utils::idn_to_utf8($from['mailto']),
1006         )
1007     ));
9f9664 1008
0b96b1 1009     $reply_mode = intval($RCMAIL->config->get('reply_mode'));
AM 1010
5e3034 1011     if (!$bodyIsHtml) {
AM 1012         $body = preg_replace('/\r?\n/', "\n", $body);
1013         $body = trim($body, "\n");
9f9664 1014
5e3034 1015         // soft-wrap and quote message text
AM 1016         $body = rcmail_wrap_and_quote($body, $LINE_LENGTH);
4e17e6 1017
5e3034 1018         $prefix .= "\n";
9f9664 1019
0b96b1 1020         if ($reply_mode > 0) { // top-posting
5e3034 1021             $prefix = "\n\n\n" . $prefix;
0b96b1 1022             $suffix = '';
AM 1023         }
1024         else {
1025             $suffix = "\n";
5e3034 1026         }
50f56d 1027     }
A 1028     else {
5e3034 1029         // save inline images to files
AM 1030         $cid_map = rcmail_write_inline_attachments($MESSAGE);
1031         // set is_safe flag (we need this for html body washing)
1032         rcmail_check_safe($MESSAGE);
1033         // clean up html tags
1034         $body = rcmail_wash_html($body, array('safe' => $MESSAGE->is_safe), $cid_map);
a0109c 1035
5e3034 1036         // build reply (quote content)
AM 1037         $prefix = '<p>' . rcube::Q($prefix) . "</p>\n";
1038         $prefix .= '<blockquote>';
1039
0b96b1 1040         if ($reply_mode > 0) { // top-posting
5e3034 1041             $prefix = '<br>' . $prefix;
AM 1042             $suffix = '</blockquote>';
1043         }
1044         else {
ead084 1045             $suffix = '</blockquote><p><br/></p>';
5e3034 1046         }
AM 1047     }
1048
1049     return $prefix . $body . $suffix;
4315b0 1050 }
4e17e6 1051
T 1052
a0109c 1053 function rcmail_create_forward_body($body, $bodyIsHtml)
4315b0 1054 {
5e3034 1055     global $RCMAIL, $MESSAGE, $COMPOSE;
ec603f 1056
5e3034 1057     // add attachments
AM 1058     if (!isset($COMPOSE['forward_attachments']) && is_array($MESSAGE->mime_parts)) {
1059         $cid_map = rcmail_write_compose_attachments($MESSAGE, $bodyIsHtml);
1060     }
4e17e6 1061
5e3034 1062     $date = $RCMAIL->format_date($MESSAGE->headers->date, $RCMAIL->config->get('date_long'));
f5c108 1063
5e3034 1064     if (!$bodyIsHtml) {
AM 1065         $prefix = "\n\n\n-------- " . $RCMAIL->gettext('originalmessage') . " --------\n";
1066         $prefix .= $RCMAIL->gettext('subject') . ': ' . $MESSAGE->subject . "\n";
1067         $prefix .= $RCMAIL->gettext('date')    . ': ' . $date . "\n";
1068         $prefix .= $RCMAIL->gettext('from')    . ': ' . $MESSAGE->get_header('from') . "\n";
1069         $prefix .= $RCMAIL->gettext('to')      . ': ' . $MESSAGE->get_header('to') . "\n";
a4cf45 1070
5e3034 1071         if ($cc = $MESSAGE->headers->get('cc')) {
AM 1072             $prefix .= $RCMAIL->gettext('cc') . ': ' . $cc . "\n";
1073         }
1074         if (($replyto = $MESSAGE->headers->get('reply-to')) && $replyto != $MESSAGE->get_header('from')) {
1075             $prefix .= $RCMAIL->gettext('replyto') . ': ' . $replyto . "\n";
1076         }
a4cf45 1077
5e3034 1078         $prefix .= "\n";
AM 1079         $body = trim($body, "\r\n");
1080     }
1081     else {
1082         // set is_safe flag (we need this for html body washing)
1083         rcmail_check_safe($MESSAGE);
ec603f 1084
5e3034 1085         // clean up html tags
AM 1086         $body = rcmail_wash_html($body, array('safe' => $MESSAGE->is_safe), $cid_map);
5758b9 1087
5e3034 1088         $prefix = sprintf(
AM 1089             "<br /><p>-------- " . $RCMAIL->gettext('originalmessage') . " --------</p>" .
1090             "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody>" .
1091             "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">%s: </th><td>%s</td></tr>" .
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             $RCMAIL->gettext('subject'), rcube::Q($MESSAGE->subject),
1096             $RCMAIL->gettext('date'), rcube::Q($date),
1097             $RCMAIL->gettext('from'), rcube::Q($MESSAGE->get_header('from'), 'replace'),
1098             $RCMAIL->gettext('to'), rcube::Q($MESSAGE->get_header('to'), 'replace'));
a4cf45 1099
5e3034 1100         if ($cc = $MESSAGE->headers->get('cc'))
AM 1101             $prefix .= sprintf("<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">%s: </th><td>%s</td></tr>",
1102                 $RCMAIL->gettext('cc'), rcube::Q($cc, 'replace'));
5758b9 1103
5e3034 1104         if (($replyto = $MESSAGE->headers->get('reply-to')) && $replyto != $MESSAGE->get_header('from'))
AM 1105             $prefix .= sprintf("<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">%s: </th><td>%s</td></tr>",
1106                 $RCMAIL->gettext('replyto'), rcube::Q($replyto, 'replace'));
f52c4f 1107
5e3034 1108         $prefix .= "</tbody></table><br>";
AM 1109     }
1110
1111     return $prefix . $body;
4315b0 1112 }
4e17e6 1113
8d4bcd 1114
a0109c 1115 function rcmail_create_draft_body($body, $bodyIsHtml)
4315b0 1116 {
5e3034 1117     global $MESSAGE, $COMPOSE;
f52c4f 1118
5e3034 1119     // add attachments
AM 1120     // sizeof($MESSAGE->mime_parts can be 1 - e.g. attachment, but no text!
1121     if (empty($COMPOSE['forward_attachments'])
1122         && is_array($MESSAGE->mime_parts)
1123         && count($MESSAGE->mime_parts) > 0
1124     ) {
1125         $cid_map = rcmail_write_compose_attachments($MESSAGE, $bodyIsHtml);
f2a9a9 1126     }
f52c4f 1127
5e3034 1128     // clean up HTML tags - XSS prevention (#1489251)
AM 1129     if ($bodyIsHtml) {
1130         $body = rcmail_wash_html($body, array('safe' => 1), $cid_map);
1131
1132         // remove comments (produced by washtml)
1133         $body = preg_replace('/<!--[^>]+-->/', '', $body);
1134
1135         // replace cid with href in inline images links
1136         if (!empty($cid_map)) {
1137             $body = str_replace(array_keys($cid_map), array_values($cid_map), $body);
1138         }
1139     }
1140
1141     return $body;
4315b0 1142 }
ba12c7 1143
A 1144
1145 function rcmail_remove_signature($body)
1146 {
5e3034 1147     global $RCMAIL;
ba12c7 1148
5e3034 1149     $body = str_replace("\r\n", "\n", $body);
AM 1150     $len  = strlen($body);
1151     $sig_max_lines = $RCMAIL->config->get('sig_max_lines', 15);
ba12c7 1152
5e3034 1153     while (($sp = strrpos($body, "-- \n", $sp ? -$len+$sp-1 : 0)) !== false) {
AM 1154         if ($sp == 0 || $body[$sp-1] == "\n") {
1155             // do not touch blocks with more that X lines
1156             if (substr_count($body, "\n", $sp) < $sig_max_lines) {
1157                 $body = substr($body, 0, max(0, $sp-1));
1158             }
1159             break;
1160         }
ba12c7 1161     }
A 1162
5e3034 1163     return $body;
ba12c7 1164 }
A 1165
1166
1bc48e 1167 function rcmail_write_compose_attachments(&$message, $bodyIsHtml)
4315b0 1168 {
5e3034 1169     global $RCMAIL, $COMPOSE, $compose_mode;
5503cc 1170
5e3034 1171     $loaded_attachments = array();
AM 1172     foreach ((array)$COMPOSE['attachments'] as $attachment) {
1173         $loaded_attachments[$attachment['name'] . $attachment['mimetype']] = $attachment;
8d4bcd 1174     }
cc97ea 1175
5e3034 1176     $cid_map  = array();
AM 1177     $messages = array();
ec603f 1178
5e3034 1179     foreach ((array)$message->mime_parts as $pid => $part) {
c82d09 1180         if ($part->mimetype == 'message/rfc822') {
AM 1181             $messages[] = $part->mime_id;
1182         }
1183
5e3034 1184         if ($part->disposition == 'attachment' || ($part->disposition == 'inline' && $bodyIsHtml) || $part->filename) {
AM 1185             // skip parts that aren't valid attachments
1186             if ($part->ctype_primary == 'multipart' || $part->mimetype == 'application/ms-tnef') {
1187                 continue;
1188             }
1189
1190             // skip message attachments in reply mode
1191             if ($part->ctype_primary == 'message' && $compose_mode == RCUBE_COMPOSE_REPLY) {
1192                 continue;
1193             }
1194
1195             // skip inline images when forwarding in text mode
1196             if ($part->content_id && $part->disposition == 'inline' && !$bodyIsHtml && $compose_mode == RCUBE_COMPOSE_FORWARD) {
1197                 continue;
1198             }
1199
c82d09 1200             // skip attachments included in message/rfc822 attachment (#1486487, #1490607)
AM 1201             foreach ($messages as $mimeid) {
1202                 if (strpos($part->mime_id, $mimeid . '.') === 0) {
1203                     continue 2;
5e3034 1204                 }
AM 1205             }
1206
1207             if (($attachment = $loaded_attachments[rcmail_attachment_name($part) . $part->mimetype])
1208                 || ($attachment = rcmail_save_attachment($message, $pid))
1209             ) {
1210                 $COMPOSE['attachments'][$attachment['id']] = $attachment;
1211
1212                 if ($bodyIsHtml && ($part->content_id || $part->content_location)) {
1213                     $url = sprintf('%s&_id=%s&_action=display-attachment&_file=rcmfile%s',
1214                         $RCMAIL->comm_path, $COMPOSE['id'], $attachment['id']);
1215
1216                     if ($part->content_id)
1217                         $cid_map['cid:'.$part->content_id] = $url;
1218                     else
1219                         $cid_map[$part->content_location] = $url;
1220                 }
1221             }
1222         }
1223     }
1224
1225     $COMPOSE['forward_attachments'] = true;
1226
1227     return $cid_map;
4315b0 1228 }
4e17e6 1229
T 1230
2f746d 1231 function rcmail_write_inline_attachments(&$message)
A 1232 {
5e3034 1233     global $RCMAIL, $COMPOSE;
ec603f 1234
c82d09 1235     $cid_map  = array();
AM 1236     $messages = array();
1237
5e3034 1238     foreach ((array)$message->mime_parts as $pid => $part) {
c82d09 1239         if ($part->mimetype == 'message/rfc822') {
AM 1240             $messages[] = $part->mime_id;
1241         }
1242
5e3034 1243         if (($part->content_id || $part->content_location) && $part->filename) {
c82d09 1244             // skip attachments included in message/rfc822 attachment (#1486487, #1490607)
AM 1245             foreach ($messages as $mimeid) {
1246                 if (strpos($part->mime_id, $mimeid . '.') === 0) {
1247                     continue 2;
1248                 }
1249             }
1250
5e3034 1251             if ($attachment = rcmail_save_attachment($message, $pid)) {
AM 1252                 $COMPOSE['attachments'][$attachment['id']] = $attachment;
1253                 $url = sprintf('%s&_id=%s&_action=display-attachment&_file=rcmfile%s',
1254                     $RCMAIL->comm_path, $COMPOSE['id'], $attachment['id']);
1255
1256                 if ($part->content_id)
1257                     $cid_map['cid:'.$part->content_id] = $url;
1258                 else
1259                     $cid_map[$part->content_location] = $url;
1260             }
1261         }
2f746d 1262     }
8f2b46 1263
5e3034 1264     return $cid_map;
2f746d 1265 }
A 1266
d9f109 1267 // Creates attachment(s) from the forwarded message(s)
AM 1268 function rcmail_write_forward_attachments()
a208a4 1269 {
5e3034 1270     global $RCMAIL, $COMPOSE, $MESSAGE;
a208a4 1271
5e3034 1272     $storage = $RCMAIL->get_storage();
AM 1273     $names   = array();
73076d 1274     $refs    = array();
d9f109 1275
5e3034 1276     $loaded_attachments = array();
AM 1277     foreach ((array)$COMPOSE['attachments'] as $attachment) {
1278         $loaded_attachments[$attachment['name'] . $attachment['mimetype']] = $attachment;
d9f109 1279     }
AM 1280
5e3034 1281     if ($COMPOSE['forward_uid'] == '*') {
AM 1282         $index = $storage->index(null, rcmail_sort_column(), rcmail_sort_order());
1283         $COMPOSE['forward_uid'] = $index->get();
d9f109 1284     }
aafbe8 1285     else if (!is_array($COMPOSE['forward_uid']) && strpos($COMPOSE['forward_uid'], ':')) {
03de13 1286         $COMPOSE['forward_uid'] = rcube_imap_generic::uncompressMessageSet($COMPOSE['forward_uid']);
AM 1287     }
aafbe8 1288     else if (is_string($COMPOSE['forward_uid'])) {
5e3034 1289         $COMPOSE['forward_uid'] = explode(',', $COMPOSE['forward_uid']);
d9f109 1290     }
AM 1291
5e3034 1292     foreach ((array)$COMPOSE['forward_uid'] as $uid) {
AM 1293         $message = new rcube_message($uid);
d9f109 1294
5e3034 1295         if (empty($message->headers)) {
AM 1296             continue;
1297         }
d9f109 1298
5e3034 1299         if (!empty($message->headers->charset)) {
AM 1300             $storage->set_charset($message->headers->charset);
1301         }
1302
1303         if (empty($MESSAGE->subject)) {
1304             $MESSAGE->subject = $message->subject;
1305         }
1306
1307         // generate (unique) attachment name
1308         $name = strlen($message->subject) ? mb_substr($message->subject, 0, 64) : 'message_rfc822';
1309         if (!empty($names[$name])) {
1310             $names[$name]++;
1311             $name .= '_' . $names[$name];
1312         }
1313         $names[$name] = 1;
1314         $name .= '.eml';
1315
1316         $data = $path = null;
1317
1318         if (!empty($loaded_attachments[$name . 'message/rfc822'])) {
1319             continue;
1320         }
1321
1322         // don't load too big attachments into memory
1323         if (!rcube_utils::mem_check($message->size)) {
1324             $temp_dir = unslashify($RCMAIL->config->get('temp_dir'));
1325             $path     = tempnam($temp_dir, 'rcmAttmnt');
1326             if ($fp = fopen($path, 'w')) {
1327                 $storage->get_raw_body($message->uid, $fp);
1328                 fclose($fp);
1329             }
1330             else {
1331                 return false;
1332             }
1333         }
1334         else {
1335             $data = $storage->get_raw_body($message->uid);
1336         }
1337
1338         $attachment = array(
1339             'group'    => $COMPOSE['id'],
1340             'name'     => $name,
1341             'mimetype' => 'message/rfc822',
1342             'data'     => $data,
1343             'path'     => $path,
1344             'size'     => $path ? filesize($path) : strlen($data),
1345         );
1346
1347         $attachment = $RCMAIL->plugins->exec_hook('attachment_save', $attachment);
1348
1349         if ($attachment['status']) {
1350             unset($attachment['data'], $attachment['status'], $attachment['content_id'], $attachment['abort']);
1351             $COMPOSE['attachments'][$attachment['id']] = $attachment;
1352         }
1353         else if ($path) {
1354             @unlink($path);
1355         }
73076d 1356
AM 1357         if ($message->headers->messageID) {
1358             $refs[] = $message->headers->messageID;
1359         }
1360     }
1361
1362     // set In-Reply-To and References headers
1363     if (count($refs) == 1) {
1364         $COMPOSE['reply_msgid'] = $refs[0];
1365     }
1366     if (!empty($refs)) {
1367         $COMPOSE['references'] = implode(' ', $refs);
d9f109 1368     }
a208a4 1369 }
A 1370
1371
2f746d 1372 function rcmail_save_attachment(&$message, $pid)
A 1373 {
5e3034 1374     global $COMPOSE;
72ff6a 1375
5e3034 1376     $rcmail = rcmail::get_instance();
AM 1377     $part   = $message->mime_parts[$pid];
1378     $data   = $path = null;
a64064 1379
5e3034 1380     // don't load too big attachments into memory
AM 1381     if (!rcube_utils::mem_check($part->size)) {
1382         $temp_dir = unslashify($rcmail->config->get('temp_dir'));
1383         $path     = tempnam($temp_dir, 'rcmAttmnt');
a64064 1384
5e3034 1385         if ($fp = fopen($path, 'w')) {
48ba44 1386             $message->get_part_body($pid, false, 0, $fp);
5e3034 1387             fclose($fp);
AM 1388         }
1389         else {
1390             return false;
1391         }
1392     }
1393     else {
48ba44 1394         $data = $message->get_part_body($pid);
5e3034 1395     }
0c2596 1396
5e3034 1397     $mimetype = $part->ctype_primary . '/' . $part->ctype_secondary;
AM 1398     $filename = rcmail_attachment_name($part);
3b1426 1399
5e3034 1400     $attachment = array(
AM 1401         'group'      => $COMPOSE['id'],
1402         'name'       => $filename,
1403         'mimetype'   => $mimetype,
1404         'content_id' => $part->content_id,
1405         'data'       => $data,
1406         'path'       => $path,
1407         'size'       => $path ? filesize($path) : strlen($data),
d165d1 1408         'charset'    => $part->charset,
5e3034 1409     );
a64064 1410
5e3034 1411     $attachment = $rcmail->plugins->exec_hook('attachment_save', $attachment);
f52c4f 1412
5e3034 1413     if ($attachment['status']) {
AM 1414         unset($attachment['data'], $attachment['status'], $attachment['content_id'], $attachment['abort']);
1415         return $attachment;
1416     }
1417     else if ($path) {
1418         @unlink($path);
1419     }
1420
1421     return false;
2f746d 1422 }
A 1423
c6efcf 1424 function rcmail_save_image($path, $mimetype = '', $data = null)
b575fa 1425 {
5e3034 1426     global $COMPOSE;
72ff6a 1427
5e3034 1428     // handle attachments in memory
c6efcf 1429     if (empty($data)) {
AM 1430         $data    = file_get_contents($path);
1431         $is_file = true;
1432     }
1433
5e3034 1434     $name = rcmail_basename($path);
c6efcf 1435
AM 1436     if (empty($mimetype)) {
1437         if ($is_file) {
1438             $mimetype = rcube_mime::file_content_type($path, $name);
1439         }
1440         else {
1441             $mimetype = rcube_mime::file_content_type($data, $name, 'application/octet-stream', true);
1442         }
1443     }
b575fa 1444
5e3034 1445     $attachment = array(
AM 1446         'group'    => $COMPOSE['id'],
1447         'name'     => $name,
c6efcf 1448         'mimetype' => $mimetype,
5e3034 1449         'data'     => $data,
AM 1450         'size'     => strlen($data),
1451     );
b575fa 1452
5e3034 1453     $attachment = rcmail::get_instance()->plugins->exec_hook('attachment_save', $attachment);
b575fa 1454
5e3034 1455     if ($attachment['status']) {
AM 1456         unset($attachment['data'], $attachment['status'], $attachment['content_id'], $attachment['abort']);
1457         return $attachment;
1458     }
f52c4f 1459
5e3034 1460     return false;
b575fa 1461 }
A 1462
1463 function rcmail_basename($filename)
1464 {
5e3034 1465     // basename() is not unicode safe and locale dependent
AM 1466     if (stristr(PHP_OS, 'win') || stristr(PHP_OS, 'netware')) {
1467         return preg_replace('/^.*[\\\\\\/]/', '', $filename);
1468     }
1469     else {
1470         return preg_replace('/^.*[\/]/', '', $filename);
1471     }
b575fa 1472 }
2f746d 1473
4e17e6 1474 function rcmail_compose_subject($attrib)
4315b0 1475 {
5e3034 1476     global $MESSAGE, $COMPOSE, $compose_mode;
72ff6a 1477
5e3034 1478     list($form_start, $form_end) = get_form_tags($attrib);
AM 1479     unset($attrib['form']);
72ff6a 1480
5e3034 1481     $attrib['name']       = '_subject';
AM 1482     $attrib['spellcheck'] = 'true';
4e17e6 1483
5e3034 1484     $textfield = new html_inputfield($attrib);
AM 1485     $subject   = '';
4e17e6 1486
5e3034 1487     // use subject from post
AM 1488     if (isset($_POST['_subject'])) {
1489         $subject = rcube_utils::get_input_value('_subject', rcube_utils::INPUT_POST, TRUE);
1490     }
968601 1491     else if (!empty($COMPOSE['param']['subject'])) {
AM 1492         $subject = $COMPOSE['param']['subject'];
1493     }
5e3034 1494     // create a reply-subject
AM 1495     else if ($compose_mode == RCUBE_COMPOSE_REPLY) {
1496         if (preg_match('/^re:/i', $MESSAGE->subject))
1497             $subject = $MESSAGE->subject;
1498         else
1499             $subject = 'Re: '.$MESSAGE->subject;
3dbfb5 1500
AM 1501         // replace (was: ...) (#1489375)
1502         $subject = preg_replace('/\s*\([wW]as:[^\)]+\)\s*$/', '', $subject);
5e3034 1503     }
AM 1504     // create a forward-subject
1505     else if ($compose_mode == RCUBE_COMPOSE_FORWARD) {
1506         if (preg_match('/^fwd:/i', $MESSAGE->subject))
1507             $subject = $MESSAGE->subject;
1508         else
1509             $subject = 'Fwd: '.$MESSAGE->subject;
1510     }
1511     // creeate a draft-subject
1512     else if ($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT) {
1513         $subject = $MESSAGE->subject;
1514     }
72ff6a 1515
5e3034 1516     $out = $form_start ? "$form_start\n" : '';
AM 1517     $out .= $textfield->show($subject);
1518     $out .= $form_end ? "\n$form_end" : '';
e99991 1519
5e3034 1520     return $out;
4315b0 1521 }
4e17e6 1522
T 1523
1524 function rcmail_compose_attachment_list($attrib)
4315b0 1525 {
f5d2ee 1526     global $RCMAIL, $OUTPUT, $COMPOSE;
72ff6a 1527
5e3034 1528     // add ID if not given
AM 1529     if (!$attrib['id'])
1530         $attrib['id'] = 'rcmAttachmentList';
72ff6a 1531
8ccfc2 1532     $out    = "\n";
AM 1533     $jslist = array();
1534     $button = '';
087c7d 1535
5e3034 1536     if (is_array($COMPOSE['attachments'])) {
AM 1537         if ($attrib['deleteicon']) {
1538             $button = html::img(array(
8ccfc2 1539                 'src' => $RCMAIL->output->abs_url($attrib['deleteicon'], true),
5e3034 1540                 'alt' => $RCMAIL->gettext('delete')
AM 1541             ));
1542         }
1543         else if (rcube_utils::get_boolean($attrib['textbuttons'])) {
1544             $button = rcube::Q($RCMAIL->gettext('delete'));
1545         }
1546
1547         foreach ($COMPOSE['attachments'] as $id => $a_prop) {
1548             if (empty($a_prop)) {
1549                 continue;
1550             }
1551
1552             $out .= html::tag('li', array(
1553                     'id'          => 'rcmfile'.$id,
1554                     'class'       => rcube_utils::file2class($a_prop['mimetype'], $a_prop['name']),
1555                     'onmouseover' => "rcube_webmail.long_subject_title_ex(this, 0)",
1556                 ),
1557                 html::a(array(
1558                         'href'    => "#delete",
1559                         'title'   => $RCMAIL->gettext('delete'),
1560                         'onclick' => sprintf("return %s.command('remove-attachment','rcmfile%s', this)", rcmail_output::JS_OBJECT_NAME, $id),
9240c9 1561                         'class'   => 'delete',
TB 1562                         'tabindex' => $attrib['tabindex'] ?: '0',
1563                         'aria-label'   => $RCMAIL->gettext('delete') . ' ' . $a_prop['name'],
5e3034 1564                     ),
AM 1565                     $button
1566                 ) . rcube::Q($a_prop['name'])
1567             );
1568
1569             $jslist['rcmfile'.$id] = array(
1570                 'name'     => $a_prop['name'],
1571                 'complete' => true,
1572                 'mimetype' => $a_prop['mimetype']
1573             );
1574         }
2efe33 1575     }
a894ba 1576
5e3034 1577     if ($attrib['deleteicon'])
8ccfc2 1578         $COMPOSE['deleteicon'] = $RCMAIL->output->abs_url($attrib['deleteicon'], true);
5e3034 1579     else if (rcube_utils::get_boolean($attrib['textbuttons']))
AM 1580         $COMPOSE['textbuttons'] = true;
1581     if ($attrib['cancelicon'])
8ccfc2 1582         $OUTPUT->set_env('cancelicon', $RCMAIL->output->abs_url($attrib['cancelicon'], true));
5e3034 1583     if ($attrib['loadingicon'])
8ccfc2 1584         $OUTPUT->set_env('loadingicon', $RCMAIL->output->abs_url($attrib['loadingicon'], true));
72ff6a 1585
5e3034 1586     $OUTPUT->set_env('attachments', $jslist);
AM 1587     $OUTPUT->add_gui_object('attachmentlist', $attrib['id']);
7152f5 1588
9240c9 1589     // put tabindex value into data-tabindex attribute
TB 1590     if (isset($attrib['tabindex'])) {
1591         $attrib['data-tabindex'] = $attrib['tabindex'];
1592         unset($attrib['tabindex']);
1593     }
1594
5e3034 1595     return html::tag('ul', $attrib, $out, html::$common_attrib);
4315b0 1596 }
4e17e6 1597
T 1598
1599 function rcmail_compose_attachment_form($attrib)
4315b0 1600 {
5e3034 1601     global $OUTPUT, $RCMAIL;
4e17e6 1602
5e3034 1603     // set defaults
AM 1604     $attrib += array('id' => 'rcmUploadbox', 'buttons' => 'yes');
7df0e3 1605
5e3034 1606     // Get filesize, enable upload progress bar
AM 1607     $max_filesize = $RCMAIL->upload_init();
4171c5 1608
5e3034 1609     $button  = new html_inputfield(array('type' => 'button'));
AM 1610     $content = html::div(null, rcmail_compose_attachment_field())
1611         . html::div('hint', $RCMAIL->gettext(array('name' => 'maxuploadsize', 'vars' => array('size' => $max_filesize))));
fe0cb6 1612
5e3034 1613     if (rcube_utils::get_boolean($attrib['buttons'])) {
AM 1614         $content .= html::div('buttons',
1615             $button->show($RCMAIL->gettext('close'), array('class' => 'button', 'onclick' => "$('#$attrib[id]').hide()")) . ' ' .
1616             $button->show($RCMAIL->gettext('upload'), array('class' => 'button mainaction', 'onclick' => rcmail_output::JS_OBJECT_NAME . ".command('send-attachment', this.form)"))
1617         );
1618     }
fe0cb6 1619
5e3034 1620     $out = html::div($attrib, $OUTPUT->form_tag(array(
AM 1621             'id'      => $attrib['id'] . 'Frm',
1622             'name'    => 'uploadform',
1623             'method'  => 'post',
1624             'enctype' => 'multipart/form-data'
1625         ), $content
1626     ));
1627
1628     $OUTPUT->add_gui_object('uploadform', $attrib['id'] . 'Frm');
1629
1630     return $out;
4315b0 1631 }
4e17e6 1632
T 1633
5a8ee3 1634 function rcmail_compose_attachment_field($attrib = array())
4315b0 1635 {
5e3034 1636     $attrib['type']     = 'file';
AM 1637     $attrib['name']     = '_attachments[]';
1638     $attrib['multiple'] = 'multiple';
7fc056 1639
5e3034 1640     $field = new html_inputfield($attrib);
AM 1641
1642     return $field->show();
4315b0 1643 }
4e17e6 1644
66e2bf 1645
4e17e6 1646 function rcmail_priority_selector($attrib)
4315b0 1647 {
5e3034 1648     global $RCMAIL, $MESSAGE;
7152f5 1649
5e3034 1650     list($form_start, $form_end) = get_form_tags($attrib);
AM 1651     unset($attrib['form']);
8958d0 1652
5e3034 1653     $attrib['name'] = '_priority';
AM 1654     $prio_list = array(
1655         $RCMAIL->gettext('lowest')  => 5,
1656         $RCMAIL->gettext('low')     => 4,
1657         $RCMAIL->gettext('normal')  => 0,
1658         $RCMAIL->gettext('high')    => 2,
1659         $RCMAIL->gettext('highest') => 1,
1660     );
4e17e6 1661
5e3034 1662     $selector = new html_select($attrib);
AM 1663     $selector->add(array_keys($prio_list), array_values($prio_list));
8958d0 1664
5e3034 1665     if (isset($_POST['_priority']))
AM 1666         $sel = $_POST['_priority'];
1667     else if (isset($MESSAGE->headers->priority) && intval($MESSAGE->headers->priority) != 3)
1668         $sel = $MESSAGE->headers->priority;
1669     else
1670         $sel = 0;
4e17e6 1671
5e3034 1672     $out = $form_start ? "$form_start\n" : '';
ddc161 1673     $out .= $selector->show((int) $sel);
5e3034 1674     $out .= $form_end ? "\n$form_end" : '';
8958d0 1675
5e3034 1676     return $out;
4315b0 1677 }
4e17e6 1678
T 1679
f65021 1680 function rcmail_mdn_checkbox($attrib)
4315b0 1681 {
5e3034 1682     global $RCMAIL, $MESSAGE, $compose_mode;
8958d0 1683
5e3034 1684     list($form_start, $form_end) = get_form_tags($attrib);
AM 1685     unset($attrib['form']);
8958d0 1686
5e3034 1687     if (!isset($attrib['id']))
AM 1688         $attrib['id'] = 'receipt';
620439 1689
f65021 1690     $attrib['name']  = '_mdn';
5e3034 1691     $attrib['value'] = '1';
620439 1692
5e3034 1693     $checkbox = new html_checkbox($attrib);
b3660b 1694
f65021 1695     if (isset($_POST['_mdn']))
AM 1696         $mdn_default = $_POST['_mdn'];
5e3034 1697     else if (in_array($compose_mode, array(RCUBE_COMPOSE_DRAFT, RCUBE_COMPOSE_EDIT)))
AM 1698         $mdn_default = (bool) $MESSAGE->headers->mdn_to;
1699     else
1700         $mdn_default = $RCMAIL->config->get('mdn_default');
620439 1701
5e3034 1702     $out = $form_start ? "$form_start\n" : '';
AM 1703     $out .= $checkbox->show($mdn_default);
1704     $out .= $form_end ? "\n$form_end" : '';
1705
1706     return $out;
4315b0 1707 }
620439 1708
T 1709
f22ea7 1710 function rcmail_dsn_checkbox($attrib)
A 1711 {
5e3034 1712     global $RCMAIL;
f22ea7 1713
5e3034 1714     list($form_start, $form_end) = get_form_tags($attrib);
AM 1715     unset($attrib['form']);
f22ea7 1716
5e3034 1717     if (!isset($attrib['id']))
AM 1718         $attrib['id'] = 'dsn';
f22ea7 1719
5e3034 1720     $attrib['name']  = '_dsn';
AM 1721     $attrib['value'] = '1';
f22ea7 1722
5e3034 1723     $checkbox = new html_checkbox($attrib);
271efe 1724
5e3034 1725     if (isset($_POST['_dsn']))
AM 1726         $dsn_value = (int) $_POST['_dsn'];
1727     else
1728         $dsn_value = $RCMAIL->config->get('dsn_default');
f22ea7 1729
5e3034 1730     $out = $form_start ? "$form_start\n" : '';
AM 1731     $out .= $checkbox->show($dsn_value);
1732     $out .= $form_end ? "\n$form_end" : '';
1733
1734     return $out;
f22ea7 1735 }
A 1736
1737
a0109c 1738 function rcmail_editor_selector($attrib)
S 1739 {
5e3034 1740     global $RCMAIL;
6b2b2e 1741
5e3034 1742     // determine whether HTML or plain text should be checked
AM 1743     $useHtml = rcmail_compose_editor_mode();
d9344f 1744
5e3034 1745     if (empty($attrib['editorid']))
AM 1746         $attrib['editorid'] = 'rcmComposeBody';
79af0b 1747
5e3034 1748     if (empty($attrib['name']))
AM 1749         $attrib['name'] = 'editorSelect';
8958d0 1750
646b64 1751     $attrib['onchange'] = "return rcmail.command('toggle-editor', {id: '".$attrib['editorid']."', html: this.value == 'html'}, '', event)";
309d2f 1752
5e3034 1753     $select = new html_select($attrib);
309d2f 1754
5e3034 1755     $select->add(rcube::Q($RCMAIL->gettext('htmltoggle')), 'html');
AM 1756     $select->add(rcube::Q($RCMAIL->gettext('plaintoggle')), 'plain');
309d2f 1757
5e3034 1758     return $select->show($useHtml ? 'html' : 'plain');
a0109c 1759 }
S 1760
1761
faf876 1762 function rcmail_store_target_selection($attrib)
T 1763 {
5e3034 1764     global $COMPOSE, $RCMAIL;
72ff6a 1765
5e3034 1766     $attrib['name'] = '_store_target';
AM 1767     $select = $RCMAIL->folder_selector(array_merge($attrib, array(
1768         'noselection'   => '- ' . $RCMAIL->gettext('dontsave') . ' -',
1769         'folder_filter' => 'mail',
1770         'folder_rights' => 'w',
1771     )));
1772
1773     return $select->show(isset($_POST['_store_target']) ? $_POST['_store_target'] : $COMPOSE['param']['sent_mbox'], $attrib);
faf876 1774 }
T 1775
1776
eeb85f 1777 function rcmail_check_sent_folder($folder, $create=false)
A 1778 {
5e3034 1779     global $RCMAIL;
eeb85f 1780
5e3034 1781     // we'll not save the message, so it doesn't matter
AM 1782     if ($RCMAIL->config->get('no_save_sent_messages')) {
1783         return true;
1784     }
e04e31 1785
5e3034 1786     if ($RCMAIL->storage->folder_exists($folder, true)) {
AM 1787         return true;
1788     }
eeb85f 1789
5e3034 1790     // folder may exist but isn't subscribed (#1485241)
AM 1791     if ($create) {
1792         if (!$RCMAIL->storage->folder_exists($folder))
1793             return $RCMAIL->storage->create_folder($folder, true);
1794         else
1795             return $RCMAIL->storage->subscribe($folder);
1796     }
eeb85f 1797
5e3034 1798     return false;
eeb85f 1799 }
A 1800
1801
4e17e6 1802 function get_form_tags($attrib)
4315b0 1803 {
5e3034 1804     global $RCMAIL, $MESSAGE_FORM, $COMPOSE;
4e17e6 1805
5e3034 1806     $form_start = '';
AM 1807     if (!$MESSAGE_FORM) {
1808         $hiddenfields = new html_hiddenfield(array('name' => '_task', 'value' => $RCMAIL->task));
1809         $hiddenfields->add(array('name' => '_action', 'value' => 'send'));
1810         $hiddenfields->add(array('name' => '_id', 'value' => $COMPOSE['id']));
1811         $hiddenfields->add(array('name' => '_attachments'));
1966c5 1812
5e3034 1813         $form_start = empty($attrib['form']) ? $RCMAIL->output->form_tag(array('name' => "form", 'method' => "post")) : '';
AM 1814         $form_start .= $hiddenfields->show();
1815     }
eeb85f 1816
5e3034 1817     $form_end = ($MESSAGE_FORM && !strlen($attrib['form'])) ? '</form>' : '';
AM 1818     $form_name = !empty($attrib['form']) ? $attrib['form'] : 'form';
eeb85f 1819
5e3034 1820     if (!$MESSAGE_FORM)
AM 1821         $RCMAIL->output->add_gui_object('messageform', $form_name);
eeb85f 1822
5e3034 1823     $MESSAGE_FORM = $form_name;
4e17e6 1824
5e3034 1825     return array($form_start, $form_end);
4315b0 1826 }
4e17e6 1827
T 1828
635722 1829 function rcmail_addressbook_list($attrib = array())
eeb73c 1830 {
T 1831     global $RCMAIL, $OUTPUT;
1832
1833     $attrib += array('id' => 'rcmdirectorylist');
1834
1835     $out = '';
1836     $line_templ = html::tag('li', array(
1837         'id' => 'rcmli%s', 'class' => '%s'),
1838         html::a(array('href' => '#list',
1839             'rel' => '%s',
6b2b2e 1840             'onclick' => "return ".rcmail_output::JS_OBJECT_NAME.".command('list-adresses','%s',this)"), '%s'));
eeb73c 1841
65dff8 1842     foreach ($RCMAIL->get_address_sources(false, true) as $j => $source) {
eeb73c 1843         $id = strval(strlen($source['id']) ? $source['id'] : $j);
6b2b2e 1844         $js_id = rcube::JQ($id);
eeb73c 1845
T 1846         // set class name(s)
1847         $class_name = 'addressbook';
1848         if ($source['class_name'])
1849             $class_name .= ' ' . $source['class_name'];
1850
1851         $out .= sprintf($line_templ,
6b2b2e 1852             rcube_utils::html_identifier($id,true),
eeb73c 1853             $class_name,
T 1854             $source['id'],
abe164 1855             $js_id, (!empty($source['name']) ? $source['name'] : $id));
eeb73c 1856     }
T 1857
635722 1858     $OUTPUT->add_gui_object('addressbookslist', $attrib['id']);
eeb73c 1859
T 1860     return html::tag('ul', $attrib, $out, html::$common_attrib);
1861 }
1862
1863 // return the contacts list as HTML table
1864 function rcmail_contacts_list($attrib = array())
1865 {
6b2b2e 1866     global $RCMAIL, $OUTPUT;
eeb73c 1867
T 1868     $attrib += array('id' => 'rcmAddressList');
1869
1870     // set client env
1871     $OUTPUT->add_gui_object('contactslist', $attrib['id']);
1872     $OUTPUT->set_env('pagecount', 0);
1873     $OUTPUT->set_env('current_page', 0);
1874     $OUTPUT->include_script('list.js');
1875
6b2b2e 1876     return $RCMAIL->table_output($attrib, array(), array('name'), 'ID');
eeb73c 1877 }
T 1878
1879
ae6d2d 1880 /**
TB 1881  * Register a certain container as active area to drop files onto
1882  */
1883 function compose_file_drop_area($attrib)
1884 {
1885     global $OUTPUT;
1886
1887     if ($attrib['id']) {
1888         $OUTPUT->add_gui_object('filedrop', $attrib['id']);
1889         $OUTPUT->set_env('filedrop', array('action' => 'upload', 'fieldname' => '_attachments'));
1890     }
1891 }
1892
eeb73c 1893
0b1de8 1894 /**
TB 1895  *
1896  */
1897 function rcmail_compose_responses_list($attrib)
1898 {
1899     global $RCMAIL, $OUTPUT;
1900
1901     $attrib += array('id' => 'rcmresponseslist', 'tagname' => 'ul', 'cols' => 1);
1902
1903     $jsenv = array();
0ce212 1904     $list = new html_table($attrib);
TB 1905     foreach ($RCMAIL->get_compose_responses(true) as $response) {
1906         $key = $response['key'];
1907         $item = html::a(array(
b2992d 1908             'href' => '#'.urlencode($response['name']),
0b1de8 1909             'class' => rtrim('insertresponse ' . $attrib['itemclass']),
0933d6 1910             'unselectable' => 'on',
b2992d 1911             'tabindex' => '0',
0b1de8 1912             'rel' => $key,
6b2b2e 1913         ), rcube::Q($response['name']));
0b1de8 1914
TB 1915         $jsenv[$key] = $response;
1916         $list->add(array(), $item);
1917     }
1918
1919     // set client env
1920     $OUTPUT->set_env('textresponses', $jsenv);
1921     $OUTPUT->add_gui_object('responseslist', $attrib['id']);
1922
1923     return $list->show();
1924 }