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