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