alecpl
2010-11-07 c296b810bc63c91f27c4c601646d5011cae54b5f
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                     |
A 8  | Copyright (C) 2005-2009, Roundcube Dev. - Switzerland                 |
30233b 9  | Licensed under the GNU GPL                                            |
4e17e6 10  |                                                                       |
T 11  | PURPOSE:                                                              |
12  |   Compose a new mail message with all headers and attachments         |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id$
19
20 */
21
8d4bcd 22 // define constants for message compose mode
T 23 define('RCUBE_COMPOSE_REPLY', 0x0106);
24 define('RCUBE_COMPOSE_FORWARD', 0x0107);
25 define('RCUBE_COMPOSE_DRAFT', 0x0108);
069704 26 define('RCUBE_COMPOSE_EDIT', 0x0109);
8d4bcd 27
f0f98f 28 $MESSAGE_FORM = NULL;
8d4bcd 29 $MESSAGE = NULL;
f0f98f 30
86df15 31 // Nothing below is called during message composition, only at "new/forward/reply/draft" initialization or
T 32 // if a compose-ID is given (i.e. when the compose step is opened in a new window/tab).
33 // Since there are many ways to leave the compose page improperly, it seems necessary to clean-up an old
f0f98f 34 // compose when a "new/forward/reply/draft" is called - otherwise the old session attachments will appear
S 35
ace851 36 $MESSAGE_ID = get_input_value('_id', RCUBE_INPUT_GET);
A 37 if (!is_array($_SESSION['compose']) || $_SESSION['compose']['id'] != $MESSAGE_ID)
4315b0 38 {
86df15 39   rcmail_compose_cleanup();
ace851 40
A 41   // Infinite redirect prevention in case of broken session (#1487028)
42   if ($MESSAGE_ID)
43     raise_error(array('code' => 500, 'type' => 'php',
44       'file' => __FILE__, 'line' => __LINE__,
45       'message' => "Invalid session"), true, true);
46
48958e 47   $_SESSION['compose'] = array(
b48d9b 48     'id' => uniqid(mt_rand()),
759696 49     'param' => request2param(RCUBE_INPUT_GET),
7d8e16 50     'mailbox' => $IMAP->get_mailbox_name(),
48958e 51   );
c719f3 52   
5f314d 53   // process values like "mailto:foo@bar.com?subject=new+message&cc=another"
759696 54   if ($_SESSION['compose']['param']['to']) {
3e8898 55     // #1486037: remove "mailto:" prefix
A 56     $_SESSION['compose']['param']['to'] = preg_replace('/^mailto:/i', '', $_SESSION['compose']['param']['to']);
759696 57     $mailto = explode('?', $_SESSION['compose']['param']['to']);
5f314d 58     if (count($mailto) > 1) {
759696 59       $_SESSION['compose']['param']['to'] = $mailto[0];
5f314d 60       parse_str($mailto[1], $query);
T 61       foreach ($query as $f => $val)
759696 62         $_SESSION['compose']['param'][$f] = $val;
5f314d 63     }
T 64   }
759696 65   
814905 66   // select folder where to save the sent message
T 67   $_SESSION['compose']['param']['sent_mbox'] = $RCMAIL->config->get('sent_mbox');
68   
759696 69   // pipe compose parameters thru plugins
T 70   $plugin = $RCMAIL->plugins->exec_hook('message_compose', $_SESSION['compose']);
814905 71   $_SESSION['compose']['param'] = array_merge($_SESSION['compose']['param'], $plugin['param']);
ceeab9 72
76791c 73   // add attachments listed by message_compose hook
T 74   if (is_array($plugin['attachments'])) {
75     foreach ($plugin['attachments'] as $attach) {
76       // we have structured data
77       if (is_array($attach)) {
78         $attachment = $attach;
79       }
80       // only a file path is given
81       else {
82         $filename = basename($attach);
83         $attachment = array(
84           'name' => $filename,
85           'mimetype' => rc_mime_content_type($attach, $filename),
86           'path' => $attach
87         );
88       }
89       
90       // save attachment if valid
91       if (($attachment['data'] && $attachment['name']) || ($attachment['path'] && file_exists($attachment['path']))) {
e6ce00 92         $attachment = rcmail::get_instance()->plugins->exec_hook('attachment_save', $attachment);
76791c 93       }
T 94       
95       if ($attachment['status'] && !$attachment['abort']) {
96         unset($attachment['data'], $attachment['status'], $attachment['abort']);
97         $_SESSION['compose']['attachments'][$attachment['id']] = $attachment;
98       }
99     }
100   }
5f314d 101
90e708 102   // check if folder for saving sent messages exists and is subscribed (#1486802)
eeb85f 103   if ($sent_folder = $_SESSION['compose']['param']['sent_mbox']) {
A 104     rcmail_check_sent_folder($sent_folder, true);
90e708 105   }
T 106
c719f3 107   // redirect to a unique URL with all parameters stored in session
T 108   $OUTPUT->redirect(array('_action' => 'compose', '_id' => $_SESSION['compose']['id']));
4315b0 109 }
76791c 110
4e17e6 111
10a699 112 // add some labels to client
3f9712 113 $OUTPUT->add_label('nosubject', 'nosenderwarning', 'norecipientwarning', 'nosubjectwarning', 'cancel',
V 114     'nobodywarning', 'notsentwarning', 'notuploadedwarning', 'savingmessage', 'sendingmessage', 
c296b8 115     'messagesaved', 'converting', 'editorwarning', 'searching', 'uploading', 'fileuploaderror',
A 116     'autocompletechars');
10a699 117
272705 118 // add config parameters to client script
A 119 if (!empty($CONFIG['drafts_mbox'])) {
120   $OUTPUT->set_env('drafts_mailbox', $CONFIG['drafts_mbox']);
121   $OUTPUT->set_env('draft_autosave', $CONFIG['draft_autosave']);
122 }
cf6a83 123 // set current mailbox in client environment
A 124 $OUTPUT->set_env('mailbox', $IMAP->get_mailbox_name());
0207c4 125 $OUTPUT->set_env('sig_above', $CONFIG['sig_above']);
50f56d 126 $OUTPUT->set_env('top_posting', $CONFIG['top_posting']);
c296b8 127 $OUTPUT->set_env('autocomplete_min_length', $CONFIG['autocomplete_min_length']);
10a699 128
8d4bcd 129 // get reference message and set compose mode
759696 130 if ($msg_uid = $_SESSION['compose']['param']['reply_uid'])
8d4bcd 131   $compose_mode = RCUBE_COMPOSE_REPLY;
759696 132 else if ($msg_uid = $_SESSION['compose']['param']['forward_uid'])
8d4bcd 133   $compose_mode = RCUBE_COMPOSE_FORWARD;
759696 134 else if ($msg_uid = $_SESSION['compose']['param']['uid'])
069704 135   $compose_mode = RCUBE_COMPOSE_EDIT;
759696 136 else if ($msg_uid = $_SESSION['compose']['param']['draft_uid']) {
59ee68 137   $RCMAIL->imap->set_mailbox($CONFIG['drafts_mbox']);
8d4bcd 138   $compose_mode = RCUBE_COMPOSE_DRAFT;
0b21c8 139 }
50f56d 140
0207c4 141 $config_show_sig = $RCMAIL->config->get('show_sig', 1);
T 142 if ($config_show_sig == 1)
50f56d 143   $OUTPUT->set_env('show_sig', true);
0207c4 144 else if ($config_show_sig == 2 && (empty($compose_mode) || $compose_mode == RCUBE_COMPOSE_EDIT || $compose_mode == RCUBE_COMPOSE_DRAFT))
50f56d 145   $OUTPUT->set_env('show_sig', true);
0207c4 146 else if ($config_show_sig == 3 && ($compose_mode == RCUBE_COMPOSE_REPLY || $compose_mode == RCUBE_COMPOSE_FORWARD))
50f56d 147   $OUTPUT->set_env('show_sig', true);
0207c4 148 else
T 149   $OUTPUT->set_env('show_sig', false);
8d4bcd 150
b62049 151 // set line length for body wrapping
6b6f2e 152 $LINE_LENGTH = $RCMAIL->config->get('line_length', 72);
b62049 153
8d4bcd 154 if (!empty($msg_uid))
4315b0 155 {
4e17e6 156   // similar as in program/steps/mail/show.inc
aae0ad 157   // re-set 'prefer_html' to have possibility to use html part for compose
b62049 158   $CONFIG['prefer_html'] = $CONFIG['prefer_html'] || $CONFIG['htmleditor'] || $compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT;
8fa58e 159   $MESSAGE = new rcube_message($msg_uid);
17b5fb 160   
bc4960 161   // make sure message is marked as read
T 162   if ($MESSAGE && $MESSAGE->headers && !$MESSAGE->headers->seen)
163     $IMAP->set_flag($msg_uid, 'SEEN');
164
8fa58e 165   if (!empty($MESSAGE->headers->charset))
T 166     $IMAP->set_charset($MESSAGE->headers->charset);
17b5fb 167     
8d4bcd 168   if ($compose_mode == RCUBE_COMPOSE_REPLY)
4315b0 169   {
8d4bcd 170     $_SESSION['compose']['reply_uid'] = $msg_uid;
8fa58e 171     $_SESSION['compose']['reply_msgid'] = $MESSAGE->headers->messageID;
T 172     $_SESSION['compose']['references']  = trim($MESSAGE->headers->references . " " . $MESSAGE->headers->messageID);
583f1c 173
759696 174     if (!empty($_SESSION['compose']['param']['all']))
e25a35 175       $MESSAGE->reply_all = $_SESSION['compose']['param']['all'];
bc404f 176
a96183 177     $OUTPUT->set_env('compose_mode', 'reply');
eeb85f 178
A 179     // Save the sent message in the same folder of the message being replied to
180     if ($RCMAIL->config->get('reply_same_folder') && ($sent_folder = $_SESSION['compose']['mailbox'])
181       && rcmail_check_sent_folder($sent_folder, false)
182     ) {
183       $_SESSION['compose']['param']['sent_mbox'] = $sent_folder;
184     }
4e17e6 185   }
95fd38 186   else if ($compose_mode == RCUBE_COMPOSE_DRAFT)
A 187   {
bc404f 188     if ($MESSAGE->headers->others['x-draft-info'])
95fd38 189     {
bbc856 190       // get reply_uid/forward_uid to flag the original message when sending
bc404f 191       $info = rcmail_draftinfo_decode($MESSAGE->headers->others['x-draft-info']);
T 192
193       if ($info['type'] == 'reply')
194         $_SESSION['compose']['reply_uid'] = $info['uid'];
195       else if ($info['type'] == 'forward')
196         $_SESSION['compose']['forward_uid'] = $info['uid'];
197
198       $_SESSION['compose']['mailbox'] = $info['folder'];
eeb85f 199
A 200       // Save the sent message in the same folder of the message being replied to
201       if ($RCMAIL->config->get('reply_same_folder') && ($sent_folder = $info['folder'])
202         && rcmail_check_sent_folder($sent_folder, false)
203       ) {
204         $_SESSION['compose']['param']['sent_mbox'] = $sent_folder;
205       }
95fd38 206     }
eeb85f 207
bc404f 208     if ($MESSAGE->headers->in_reply_to)
T 209       $_SESSION['compose']['reply_msgid'] = '<'.$MESSAGE->headers->in_reply_to.'>';
210
95fd38 211     $_SESSION['compose']['references']  = $MESSAGE->headers->references;
A 212   }
4315b0 213   else if ($compose_mode == RCUBE_COMPOSE_FORWARD)
S 214   {
215     $_SESSION['compose']['forward_uid'] = $msg_uid;
a96183 216     $OUTPUT->set_env('compose_mode', 'forward');
4315b0 217   }
S 218 }
4e17e6 219
087c7d 220 // process $MESSAGE body/attachments, set $MESSAGE_BODY/$HTML_MODE vars and some session data
A 221 $MESSAGE_BODY = rcmail_prepare_message_body();
4e17e6 222
087c7d 223
A 224 /****** compose mode functions ********/
4e17e6 225
T 226 function rcmail_compose_headers($attrib)
4315b0 227 {
8d4bcd 228   global $IMAP, $MESSAGE, $DB, $compose_mode;
583f1c 229   static $sa_recipients = array();
4e17e6 230
T 231   list($form_start, $form_end) = get_form_tags($attrib);
8958d0 232
4e17e6 233   $out = '';
T 234   $part = strtolower($attrib['part']);
8958d0 235
4e17e6 236   switch ($part)
4315b0 237   {
4e17e6 238     case 'from':
8958d0 239       return $form_start . rcmail_compose_header_from($attrib);
4e17e6 240
T 241     case 'to':
242       $fname = '_to';
759696 243       $header = $param = 'to';
8958d0 244
f11541 245       // we have a set of recipients stored is session
759696 246       if (($mailto_id = $_SESSION['compose']['param']['mailto']) && $_SESSION['mailto'][$mailto_id])
fde466 247         $fvalue = urldecode($_SESSION['mailto'][$mailto_id]);
8958d0 248
4e17e6 249     case 'cc':
e99991 250       if (!$fname) {
4e17e6 251         $fname = '_cc';
759696 252         $header = $param = 'cc';
4315b0 253       }
4e17e6 254     case 'bcc':
e99991 255       if (!$fname) {
4e17e6 256         $fname = '_bcc';
759696 257         $header = $param = 'bcc';
4315b0 258       }
8958d0 259
bd4209 260       $allow_attrib = array('id', 'class', 'style', 'cols', 'rows', 'tabindex');
47124c 261       $field_type = 'html_textarea';
4e17e6 262       break;
T 263
264     case 'replyto':
265     case 'reply-to':
266       $fname = '_replyto';
759696 267       $param = 'replyto';
e25a35 268       $header = 'reply-to';
A 269
270     case 'mailreplyto':
cb105a 271     case 'mail-reply-to':
e25a35 272       if (!$fname) {
A 273         $fname = '_mailreplyto';
274         $param = 'mailreplyto';
cb105a 275         $header = 'mail-reply-to';
e25a35 276       }
A 277
278     case 'mailfollowupto':
cb105a 279     case 'mail-followup-to':
e25a35 280       if (!$fname) {
A 281         $fname = '_mailfollowupto';
282         $param = 'mailfollowupto';
cb105a 283         $header = 'mail-followup-to';
e25a35 284       }
A 285
317219 286       $allow_attrib = array('id', 'class', 'style', 'size', 'tabindex');
47124c 287       $field_type = 'html_inputfield';
5f314d 288       break;
4315b0 289   }
e99991 290
759696 291   if ($fname && !empty($_POST[$fname])) {
01c86f 292     $fvalue = get_input_value($fname, RCUBE_INPUT_POST, TRUE);
759696 293   }
T 294   else if ($fname && !$fvalue && !empty($_SESSION['compose']['param'][$param])) {
295     $fvalue = $_SESSION['compose']['param'][$param];
296   }
297   else if ($header && $compose_mode == RCUBE_COMPOSE_REPLY) {
4e17e6 298     // get recipent address(es) out of the message headers
e25a35 299     if ($header == 'to') {
A 300       $mailfollowup = $MESSAGE->headers->others['mail-followup-to'];
301       $mailreplyto  = $MESSAGE->headers->others['mail-reply-to'];
302
303       if ($MESSAGE->reply_all == 'list' && $mailfollowup)
304         $fvalue = $mailfollowup;
305       else if ($MESSAGE->reply_all == 'list'
306         && preg_match('/<mailto:([^>]+)>/i', $MESSAGE->headers->others['list-post'], $m))
307         $fvalue = $m[1];
308       else if ($mailreplyto)
309         $fvalue = $mailreplyto;
310       else if (!empty($MESSAGE->headers->replyto))
311         $fvalue = $MESSAGE->headers->replyto;
312       else if (!empty($MESSAGE->headers->from))
313         $fvalue = $MESSAGE->headers->from;
314     }
583f1c 315     // add recipent of original message if reply to all
e25a35 316     else if ($header == 'cc' && !empty($MESSAGE->reply_all) && $MESSAGE->reply_all != 'list') {
8fa58e 317       if ($v = $MESSAGE->headers->to)
8d4bcd 318         $fvalue .= $v;
8fa58e 319       if ($v = $MESSAGE->headers->cc)
8d4bcd 320         $fvalue .= (!empty($fvalue) ? ', ' : '') . $v;
4315b0 321     }
583f1c 322
4e17e6 323     // split recipients and put them back together in a unique way
e99991 324     if (!empty($fvalue)) {
583f1c 325       $to_addresses = $IMAP->decode_address_list($fvalue);
T 326       $fvalue = '';
51f55b 327
e99991 328       foreach ($to_addresses as $addr_part) {
A 329         if (empty($addr_part['mailto']))
330           continue;
331
332         $mailto = idn_to_utf8($addr_part['mailto']);
333
334         if (!in_array($mailto, $sa_recipients)
8e98f2 335             && (!$MESSAGE->compose_from
e99991 336                 || !in_array_nocase($mailto, $MESSAGE->compose_from)
A 337                 || (count($to_addresses)==1 && $header=='to')) // allow reply to yourself
338         ) {
339           if ($addr_part['name'] && $addr_part['mailto'] != $addr_part['name'])
340             $string = format_email_recipient($mailto, $addr_part['name']);
341           else
342             $string = $mailto;
343           $fvalue .= (strlen($fvalue) ? ', ':'') . $string;
583f1c 344           $sa_recipients[] = $addr_part['mailto'];
T 345         }
346       }
4e17e6 347     }
4315b0 348   }
e99991 349   else if ($header && in_array($compose_mode, array(RCUBE_COMPOSE_DRAFT, RCUBE_COMPOSE_EDIT))) {
1966c5 350     // get drafted headers
8fa58e 351     if ($header=='to' && !empty($MESSAGE->headers->to))
T 352       $fvalue = $MESSAGE->get_header('to');
e99991 353     else if ($header=='cc' && !empty($MESSAGE->headers->cc))
8fa58e 354       $fvalue = $MESSAGE->get_header('cc');
e99991 355     else if ($header=='bcc' && !empty($MESSAGE->headers->bcc))
8fa58e 356       $fvalue = $MESSAGE->get_header('bcc');
e25a35 357     else if ($header=='reply-to' && !empty($MESSAGE->headers->replyto))
A 358       $fvalue = $MESSAGE->get_header('reply-to');
359     else if ($header=='mail-reply-to' && !empty($MESSAGE->headers->others['mail-reply-to']))
360       $fvalue = $MESSAGE->get_header('followup-to');
361     else if ($header=='mail-followup-to' && !empty($MESSAGE->headers->others['mail-followup-to']))
362       $fvalue = $MESSAGE->get_header('mail-followup-to');
e99991 363
A 364     $addresses = $IMAP->decode_address_list($fvalue);
365     $fvalue = '';
366
367     foreach ($addresses as $addr_part) {
368       if (empty($addr_part['mailto']))
369         continue;
370
371       $mailto = idn_to_utf8($addr_part['mailto']);
372
373       if ($addr_part['name'] && $addr_part['mailto'] != $addr_part['name'])
374         $string = format_email_recipient($mailto, $addr_part['name']);
375       else
376         $string = $mailto;
377       $fvalue .= (strlen($fvalue) ? ', ':'') . $string;
378     }
4315b0 379   }
583f1c 380
4e17e6 381   if ($fname && $field_type)
4315b0 382   {
4e17e6 383     // pass the following attributes to the form class
491a6e 384     $field_attrib = array('name' => $fname, 'spellcheck' => 'false');
4e17e6 385     foreach ($attrib as $attr => $value)
T 386       if (in_array($attr, $allow_attrib))
387         $field_attrib[$attr] = $value;
388
389     // create teaxtarea object
390     $input = new $field_type($field_attrib);
47124c 391     $out = $input->show($fvalue);
4315b0 392   }
4e17e6 393   
T 394   if ($form_start)
395     $out = $form_start.$out;
1966c5 396
e99991 397   return $out;
4315b0 398 }
4e17e6 399
T 400
1cded8 401 function rcmail_compose_header_from($attrib)
4315b0 402 {
8fa58e 403   global $IMAP, $MESSAGE, $DB, $USER, $OUTPUT, $compose_mode;
1cded8 404     
T 405   // pass the following attributes to the form class
406   $field_attrib = array('name' => '_from');
407   foreach ($attrib as $attr => $value)
408     if (in_array($attr, array('id', 'class', 'style', 'size', 'tabindex')))
409       $field_attrib[$attr] = $value;
4e17e6 410
1cded8 411   // extract all recipients of the reply-message
T 412   $a_recipients = array();
8fa58e 413   if ($compose_mode == RCUBE_COMPOSE_REPLY && is_object($MESSAGE->headers))
4315b0 414   {
8fa58e 415     $MESSAGE->compose_from = array();
58e360 416
8fa58e 417     $a_to = $IMAP->decode_address_list($MESSAGE->headers->to);
1cded8 418     foreach ($a_to as $addr)
4315b0 419     {
1cded8 420       if (!empty($addr['mailto']))
e25a35 421         $a_recipients[] = strtolower($addr['mailto']);
4315b0 422     }
4e17e6 423
8fa58e 424     if (!empty($MESSAGE->headers->cc))
4315b0 425     {
8fa58e 426       $a_cc = $IMAP->decode_address_list($MESSAGE->headers->cc);
1cded8 427       foreach ($a_cc as $addr)
4315b0 428       {
1cded8 429         if (!empty($addr['mailto']))
e25a35 430           $a_recipients[] = strtolower($addr['mailto']);
1cded8 431       }
T 432     }
4315b0 433   }
4e17e6 434
1cded8 435   // get this user's identities
ed205f 436   $user_identities = $USER->list_identities();
a0109c 437
ed205f 438   if (count($user_identities))
4315b0 439   {
1cded8 440     $a_signatures = array();
a0109c 441
f11541 442     $field_attrib['onchange'] = JS_OBJECT_NAME.".change_identity(this)";
47124c 443     $select_from = new html_select($field_attrib);
a0109c 444
e25a35 445     // create SELECT element
ed205f 446     foreach ($user_identities as $sql_arr)
4315b0 447     {
e25a35 448       $email = mb_strtolower(idn_to_utf8($sql_arr['email']));
a0109c 449       $identity_id = $sql_arr['identity_id'];
e25a35 450       $select_from->add(format_email_recipient($email, $sql_arr['name']), $identity_id);
4e17e6 451
1cded8 452       // add signature to array
759696 453       if (!empty($sql_arr['signature']) && empty($_SESSION['compose']['param']['nosig']))
4315b0 454       {
a0109c 455         $a_signatures[$identity_id]['text'] = $sql_arr['signature'];
S 456         $a_signatures[$identity_id]['is_html'] = ($sql_arr['html_signature'] == 1) ? true : false;
dd792e 457         if ($a_signatures[$identity_id]['is_html'])
4315b0 458         {
dd792e 459             $h2t = new html2text($a_signatures[$identity_id]['text'], false, false);
300fc6 460             $a_signatures[$identity_id]['plain_text'] = trim($h2t->get_text());
a0109c 461         }
4315b0 462       }
a0109c 463
8fa58e 464       if ($compose_mode == RCUBE_COMPOSE_REPLY && is_array($MESSAGE->compose_from))
e25a35 465         $MESSAGE->compose_from[] = $email;
4315b0 466     }
e25a35 467
A 468     $from_id = 0;
4e17e6 469
1cded8 470     // overwrite identity selection with post parameter
6eb858 471     if (!empty($_POST['_from']))
8d4bcd 472       $from_id = get_input_value('_from', RCUBE_INPUT_POST);
416bff 473     else if (!empty($_SESSION['compose']['param']['from']))
A 474       $from_id = $_SESSION['compose']['param']['from'];
bd2e33 475     else if (count($user_identities) > 1) {
e25a35 476       $return_path = $MESSAGE->headers->others['return-path'];
A 477
478       // Set identity
479       foreach ($user_identities as $sql_arr) {
480         // set draft's identity
663534 481         if ($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT) {
A 482           if ($MESSAGE->headers->from == format_email_recipient($sql_arr['email'], $sql_arr['name'])) {
e25a35 483             $from_id = $sql_arr['identity_id'];
A 484             break;
663534 485           }
e25a35 486         }
A 487         // set identity if it's one of the reply-message recipients (with prio for default identity)
488         else if (in_array($sql_arr['email'], $a_recipients) && (empty($from_id) || $sql_arr['standard']))
489           $from_id = $sql_arr['identity_id'];
490         // set identity when replying to mailing list
491         else if (strpos($return_path, str_replace('@', '=', $sql_arr['email']).'@') !== false)
492           $from_id = $sql_arr['identity_id'];
493
494         if ($from_id)
495           break;
496       }
497     }
4e17e6 498
1cded8 499     $out = $select_from->show($from_id);
4e17e6 500
1cded8 501     // add signatures to client
f11541 502     $OUTPUT->set_env('signatures', $a_signatures);
4315b0 503   }
1cded8 504   else
4315b0 505   {
47124c 506     $input_from = new html_inputfield($field_attrib);
1cded8 507     $out = $input_from->show($_POST['_from']);
4315b0 508   }
4e17e6 509
1cded8 510   return $out;
4315b0 511 }
4e17e6 512
T 513
868deb 514 function rcmail_compose_editor_mode()
A 515 {
516   global $RCMAIL, $MESSAGE, $compose_mode;
517   static $useHtml;
518
519   if ($useHtml !== null)
520     return $useHtml;
521
522   $html_editor = intval($RCMAIL->config->get('htmleditor'));
523
524   if ($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT) {
525     $useHtml = $MESSAGE->has_html_part();
526   }
527   else if ($compose_mode == RCUBE_COMPOSE_REPLY) {
528     $useHtml = ($html_editor == 1 || ($html_editor == 2 && $MESSAGE->has_html_part()));
529   }
530   else { // RCUBE_COMPOSE_FORWARD or NEW
531     $useHtml = ($html_editor == 1);
532   }
533
534   return $useHtml;
535 }
536
537
087c7d 538 function rcmail_prepare_message_body()
4315b0 539 {
868deb 540   global $RCMAIL, $MESSAGE, $compose_mode, $LINE_LENGTH, $HTML_MODE;
a0109c 541
4e17e6 542   // use posted message body
868deb 543   if (!empty($_POST['_message'])) {
8fa58e 544     $body = get_input_value('_message', RCUBE_INPUT_POST, true);
868deb 545     $isHtml = (bool) get_input_value('_is_html', RCUBE_INPUT_POST);
8fa58e 546   }
868deb 547   else if ($_SESSION['compose']['param']['body']) {
759696 548     $body = $_SESSION['compose']['param']['body'];
T 549     $isHtml = false;
550   }
868deb 551   // reply/edit/draft/forward
A 552   else if ($compose_mode) {
b62049 553     $has_html_part = $MESSAGE->has_html_part();
868deb 554     $isHtml = rcmail_compose_editor_mode();
A 555
556     if ($isHtml) {
557       if ($has_html_part) {
558         $body = $MESSAGE->first_html_part();
559       }
560       else {
ba12c7 561         $body = $MESSAGE->first_text_part();
A 562         // try to remove the signature
563         if ($RCMAIL->config->get('strip_existing_sig', true))
564           $body = rcmail_remove_signature($body);
565         // add HTML formatting
566         $body = rcmail_plain_body($body);
868deb 567         if ($body)
A 568           $body = '<pre>' . $body . '</pre>';
569       }
b62049 570     }
868deb 571     else {
A 572       if ($has_html_part) {
573         // use html part if it has been used for message (pre)viewing
574         // decrease line length for quoting
575         $len = $compose_mode == RCUBE_COMPOSE_REPLY ? $LINE_LENGTH-2 : $LINE_LENGTH;
576         $txt = new html2text($MESSAGE->first_html_part(), false, true, $len);
577         $body = $txt->get_text();
578       }
579       else {
580         $body = $MESSAGE->first_text_part($part);
581         if ($body && $part && $part->ctype_secondary == 'plain'
582             && $part->ctype_parameters['format'] == 'flowed'
583         ) {
584           $body = rcube_message::unfold_flowed($body);
585         }
586       }
4e17e6 587     }
80815d 588
8fa58e 589     // compose reply-body
T 590     if ($compose_mode == RCUBE_COMPOSE_REPLY)
591       $body = rcmail_create_reply_body($body, $isHtml);
592     // forward message body inline
593     else if ($compose_mode == RCUBE_COMPOSE_FORWARD)
594       $body = rcmail_create_forward_body($body, $isHtml);
595     // load draft message body
069704 596     else if ($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT)
8fa58e 597       $body = rcmail_create_draft_body($body, $isHtml);
868deb 598   }
A 599   else { // new message
600     $isHtml = rcmail_compose_editor_mode();
8fa58e 601   }
a0109c 602
8abe54 603   $plugin = $RCMAIL->plugins->exec_hook('message_compose_body',
A 604     array('body' => $body, 'html' => $isHtml, 'mode' => $compose_mode));
b575fa 605   $body = $plugin['body'];
A 606   unset($plugin);
8abe54 607
b575fa 608   // add blocked.gif attachment (#1486516)
A 609   if ($isHtml && preg_match('#<img src="\./program/blocked\.gif"#', $body)) {
610     if ($attachment = rcmail_save_image('program/blocked.gif', 'image/gif')) {
611       $_SESSION['compose']['attachments'][$attachment['id']] = $attachment;
612       $body = preg_replace('#\./program/blocked\.gif#',
613         $RCMAIL->comm_path.'&_action=display-attachment&_file=rcmfile'.$attachment['id'],
614         $body);
615     }
616   }
617   
087c7d 618   $HTML_MODE = $isHtml;
A 619   
620   return $body;
621 }
622
623 function rcmail_compose_body($attrib)
624 {
625   global $RCMAIL, $CONFIG, $OUTPUT, $MESSAGE, $compose_mode, $LINE_LENGTH, $HTML_MODE, $MESSAGE_BODY;
626   
627   list($form_start, $form_end) = get_form_tags($attrib);
628   unset($attrib['form']);
629   
630   if (empty($attrib['id']))
631     $attrib['id'] = 'rcmComposeBody';
632
633   $attrib['name'] = '_message';
634
635   $isHtml = $HTML_MODE;
636   
4e17e6 637   $out = $form_start ? "$form_start\n" : '';
1966c5 638
8fa58e 639   $saveid = new html_hiddenfield(array('name' => '_draft_saveid', 'value' => $compose_mode==RCUBE_COMPOSE_DRAFT ? str_replace(array('<','>'), "", $MESSAGE->headers->messageID) : ''));
f0f98f 640   $out .= $saveid->show();
1966c5 641
47124c 642   $drafttoggle = new html_hiddenfield(array('name' => '_draft', 'value' => 'yes'));
1966c5 643   $out .= $drafttoggle->show();
S 644
47124c 645   $msgtype = new html_hiddenfield(array('name' => '_is_html', 'value' => ($isHtml?"1":"0")));
a0109c 646   $out .= $msgtype->show();
S 647
2f746d 648   // If desired, set this textarea to be editable by TinyMCE
A 649   if ($isHtml) $attrib['class'] = 'mce_editor';
47124c 650   $textarea = new html_textarea($attrib);
087c7d 651   $out .= $textarea->show($MESSAGE_BODY);
4e17e6 652   $out .= $form_end ? "\n$form_end" : '';
a01b3b 653
A 654   $OUTPUT->set_env('composebody', $attrib['id']);
a0109c 655
3e20c4 656   // include HTML editor
A 657   rcube_html_editor();
658   
dd53e2 659   // include GoogieSpell
4ca10b 660   if (!empty($CONFIG['enable_spellcheck'])) {
3e20c4 661
c287f3 662     $engine = $RCMAIL->config->get('spellcheck_engine','googie');
A 663     $spellcheck_langs = (array) $RCMAIL->config->get('spellcheck_languages',
664       array('da'=>'Dansk', 'de'=>'Deutsch', 'en' => 'English', 'es'=>'Español',
665             'fr'=>'Français', 'it'=>'Italiano', 'nl'=>'Nederlands', 'pl'=>'Polski',
666             'pt'=>'Português', 'fi'=>'Suomi', 'sv'=>'Svenska'));
667
668     // googie works only with two-letter codes
669     if ($engine == 'googie') {
670       $lang = strtolower(substr($_SESSION['language'], 0, 2));
671
672       $spellcheck_langs_googie = array();
673       foreach ($spellcheck_langs as $key => $name)
674         $spellcheck_langs_googie[strtolower(substr($key,0,2))] = $name;
675         $spellcheck_langs = $spellcheck_langs_googie;
676     }
677     else {
678       $lang = $_SESSION['language'];
679
680       // if not found in the list, try with two-letter code
681       if (!$spellcheck_langs[$lang])
682         $lang = strtolower(substr($lang, 0, 2));
683     }
684
326f3d 685     if (!$spellcheck_langs[$lang])
T 686       $lang = 'en';
c287f3 687
326f3d 688     $editor_lang_set = array();
T 689     foreach ($spellcheck_langs as $key => $name) {
690       $editor_lang_set[] = ($key == $lang ? '+' : '') . JQ($name).'='.JQ($key);
c287f3 691     }
996066 692     
ed5d29 693     $OUTPUT->include_script('googiespell.js');
2bca6e 694     $OUTPUT->add_script(sprintf(
677e1f 695       "var googie = new GoogieSpell('\$__skin_path/images/googiespell/','?_task=utils&_action=spell&lang=');\n".
2bca6e 696       "googie.lang_chck_spell = \"%s\";\n".
T 697       "googie.lang_rsm_edt = \"%s\";\n".
698       "googie.lang_close = \"%s\";\n".
699       "googie.lang_revert = \"%s\";\n".
326f3d 700       "googie.lang_no_error_found = \"%s\";\n".
T 701       "googie.setLanguages(%s);\n".
2bca6e 702       "googie.setCurrentLanguage('%s');\n".
309d2f 703       "googie.setSpellContainer('spellcheck-control');\n".
2bca6e 704       "googie.decorateTextarea('%s');\n".
T 705       "%s.set_env('spellcheck', googie);",
706       JQ(Q(rcube_label('checkspelling'))),
707       JQ(Q(rcube_label('resumeediting'))),
708       JQ(Q(rcube_label('close'))),
709       JQ(Q(rcube_label('revertto'))),
710       JQ(Q(rcube_label('nospellerrors'))),
2717f9 711       json_serialize($spellcheck_langs),
326f3d 712       $lang,
2bca6e 713       $attrib['id'],
f11541 714       JS_OBJECT_NAME), 'foot');
ed5d29 715
112c91 716     $OUTPUT->add_label('checking');
326f3d 717     $OUTPUT->set_env('spellcheck_langs', join(',', $editor_lang_set));
4ca10b 718   }
f0f98f 719  
027af3 720   $out .= "\n".'<iframe name="savetarget" src="program/blank.gif" style="width:0;height:0;border:none;visibility:hidden;"></iframe>';
41fa0b 721
4e17e6 722   return $out;
4315b0 723 }
4e17e6 724
T 725
a0109c 726 function rcmail_create_reply_body($body, $bodyIsHtml)
4315b0 727 {
b62049 728   global $RCMAIL, $MESSAGE, $LINE_LENGTH;
4e17e6 729
9f9664 730   // build reply prefix
A 731   $from = array_pop($RCMAIL->imap->decode_address_list($MESSAGE->get_header('from')));
732   $prefix = sprintf("On %s, %s wrote:",
733     $MESSAGE->headers->date, $from['name'] ? $from['name'] : idn_to_utf8($from['email']));
734
0207c4 735   if (!$bodyIsHtml) {
33dfdd 736     $body = preg_replace('/\r?\n/', "\n", $body);
9f9664 737
2b180b 738     // try to remove the signature
ba12c7 739     if ($RCMAIL->config->get('strip_existing_sig', true))
A 740       $body = rcmail_remove_signature($body);
2b180b 741
6b6f2e 742     // soft-wrap and quote message text
33dfdd 743     $body = rcmail_wrap_and_quote(rtrim($body, "\n"), $LINE_LENGTH);
4e17e6 744
9f9664 745     $prefix .= "\n";
a0109c 746     $suffix = '';
9f9664 747
0207c4 748     if ($RCMAIL->config->get('top_posting'))
50f56d 749       $prefix = "\n\n\n" . $prefix;
a0109c 750   }
0207c4 751   else {
ec603f 752     // save inline images to files
A 753     $cid_map = rcmail_write_inline_attachments($MESSAGE);
754     // set is_safe flag (we need this for html body washing)
755     rcmail_check_safe($MESSAGE);
756     // clean up html tags
757     $body = rcmail_wash_html($body, array('safe' => $MESSAGE->is_safe), $cid_map);
758
759     // build reply (quote content)
9f9664 760     $prefix = '<p>' . Q($prefix) . "</p>\n";
7a48e5 761     $prefix .= '<blockquote>';
ce4673 762
0207c4 763     if ($RCMAIL->config->get('top_posting')) {
ce4673 764       $prefix = '<br>' . $prefix;
A 765       $suffix = '</blockquote>';
50f56d 766     }
A 767     else {
ce4673 768       $suffix = '</blockquote><p></p>';
50f56d 769     }
a0109c 770   }
S 771
772   return $prefix.$body.$suffix;
4315b0 773 }
4e17e6 774
T 775
a0109c 776 function rcmail_create_forward_body($body, $bodyIsHtml)
4315b0 777 {
8c0b9e 778   global $IMAP, $MESSAGE, $OUTPUT;
ec603f 779
A 780   // add attachments
781   if (!isset($_SESSION['compose']['forward_attachments']) && is_array($MESSAGE->mime_parts))
782     $cid_map = rcmail_write_compose_attachments($MESSAGE, $bodyIsHtml);
4e17e6 783
8fa58e 784   if (!$bodyIsHtml)
a0109c 785   {
5758b9 786     $prefix = "\n\n\n-------- Original Message --------\n";
A 787     $prefix .= 'Subject: ' . $MESSAGE->subject . "\n";
788     $prefix .= 'Date: ' . $MESSAGE->headers->date . "\n";
789     $prefix .= 'From: ' . $MESSAGE->get_header('from') . "\n";
790     $prefix .= 'To: ' . $MESSAGE->get_header('to') . "\n";
a4cf45 791
A 792     if ($MESSAGE->headers->cc)
793       $prefix .= 'Cc: ' . $MESSAGE->get_header('cc') . "\n";
5758b9 794     if ($MESSAGE->headers->replyto && $MESSAGE->headers->replyto != $MESSAGE->headers->from)
A 795       $prefix .= 'Reply-To: ' . $MESSAGE->get_header('replyto') . "\n";
a4cf45 796
5758b9 797     $prefix .= "\n";
a0109c 798   }
S 799   else
800   {
ec603f 801     // set is_safe flag (we need this for html body washing)
A 802     rcmail_check_safe($MESSAGE);
803     // clean up html tags
804     $body = rcmail_wash_html($body, array('safe' => $MESSAGE->is_safe), $cid_map);
805
4315b0 806     $prefix = sprintf(
ce4673 807       "<br /><p>-------- Original Message --------</p>" .
a0109c 808         "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody>" .
S 809         "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">Subject: </th><td>%s</td></tr>" .
810         "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">Date: </th><td>%s</td></tr>" .
811         "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">From: </th><td>%s</td></tr>" .
5758b9 812         "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">To: </th><td>%s</td></tr>",
8fa58e 813       Q($MESSAGE->subject),
T 814       Q($MESSAGE->headers->date),
3e8d89 815       htmlspecialchars(Q($MESSAGE->get_header('from'), 'replace'), ENT_COMPAT, $OUTPUT->get_charset()),
7d8e16 816       htmlspecialchars(Q($MESSAGE->get_header('to'), 'replace'), ENT_COMPAT, $OUTPUT->get_charset()));
5758b9 817
a4cf45 818     if ($MESSAGE->headers->cc)
A 819       $prefix .= sprintf("<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">Cc: </th><td>%s</td></tr>",
820         htmlspecialchars(Q($MESSAGE->get_header('cc'), 'replace'), ENT_COMPAT, $OUTPUT->get_charset()));
821
5758b9 822     if ($MESSAGE->headers->replyto && $MESSAGE->headers->replyto != $MESSAGE->headers->from)
A 823       $prefix .= sprintf("<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">Reply-To: </th><td>%s</td></tr>",
7d8e16 824         htmlspecialchars(Q($MESSAGE->get_header('replyto'), 'replace'), ENT_COMPAT, $OUTPUT->get_charset()));
5758b9 825
A 826     $prefix .= "</tbody></table><br>";
a0109c 827   }
5cc4b1 828     
4e17e6 829   return $prefix.$body;
4315b0 830 }
4e17e6 831
8d4bcd 832
a0109c 833 function rcmail_create_draft_body($body, $bodyIsHtml)
4315b0 834 {
ec603f 835   global $MESSAGE, $OUTPUT;
8ecb0e 836   
T 837   /**
838    * add attachments
8fa58e 839    * sizeof($MESSAGE->mime_parts can be 1 - e.g. attachment, but no text!
8ecb0e 840    */
7d8e16 841   if (empty($_SESSION['compose']['forward_attachments'])
8fa58e 842       && is_array($MESSAGE->mime_parts)
T 843       && count($MESSAGE->mime_parts) > 0)
ec603f 844   {
A 845     $cid_map = rcmail_write_compose_attachments($MESSAGE, $bodyIsHtml);
1966c5 846
ec603f 847     // replace cid with href in inline images links
A 848     if ($cid_map)
849       $body = str_replace(array_keys($cid_map), array_values($cid_map), $body);
850   }
851   
1966c5 852   return $body;
4315b0 853 }
ba12c7 854
A 855
856 function rcmail_remove_signature($body)
857 {
858   global $RCMAIL;
859
860   $len = strlen($body);
861   $sig_max_lines = $RCMAIL->config->get('sig_max_lines', 15);
862
863   while (($sp = strrpos($body, "-- \n", $sp ? -$len+$sp-1 : 0)) !== false) {
864     if ($sp == 0 || $body[$sp-1] == "\n") {
865       // do not touch blocks with more that X lines
866       if (substr_count($body, "\n", $sp) < $sig_max_lines) {
867         $body = substr($body, 0, max(0, $sp-1));
868       }
869       break;
870     }
871   }
872
873   return $body;
874 }
875
876
1bc48e 877 function rcmail_write_compose_attachments(&$message, $bodyIsHtml)
4315b0 878 {
bde421 879   global $RCMAIL;
5503cc 880
021ef4 881   $cid_map = $messages = array();
8fa58e 882   foreach ((array)$message->mime_parts as $pid => $part)
4315b0 883   {
7d8e16 884     if (($part->ctype_primary != 'message' || !$bodyIsHtml) && $part->ctype_primary != 'multipart' && 
d311d8 885         ($part->disposition == 'attachment' || ($part->disposition == 'inline' && $bodyIsHtml) || $part->filename)
A 886         && $part->mimetype != 'application/ms-tnef'
887     ) {
021ef4 888       $skip = false;
A 889       if ($part->mimetype == 'message/rfc822') {
890         $messages[] = $part->mime_id;
891       } else if ($messages) {
892         // skip attachments included in message/rfc822 attachment (#1486487)
893         foreach ($messages as $mimeid)
894           if (strpos($part->mime_id, $mimeid.'.') === 0) {
895             $skip = true;
896             break;
897           }
898       }
899
900       if (!$skip && ($attachment = rcmail_save_attachment($message, $pid))) {
cc97ea 901         $_SESSION['compose']['attachments'][$attachment['id']] = $attachment;
8f2b46 902         if ($bodyIsHtml && ($part->content_id || $part->content_location)) {
bde421 903           $url = $RCMAIL->comm_path.'&_action=display-attachment&_file=rcmfile'.$attachment['id'];
8f2b46 904           if ($part->content_id)
A 905             $cid_map['cid:'.$part->content_id] = $url;
906           else
907             $cid_map[$part->content_location] = $url;
ec603f 908         }
A 909       }
8d4bcd 910     }
4315b0 911   }
cc97ea 912
8fa58e 913   $_SESSION['compose']['forward_attachments'] = true;
ec603f 914
A 915   return $cid_map;
4315b0 916 }
4e17e6 917
T 918
2f746d 919 function rcmail_write_inline_attachments(&$message)
A 920 {
bde421 921   global $RCMAIL;
ec603f 922
A 923   $cid_map = array();
924   foreach ((array)$message->mime_parts as $pid => $part) {
8f2b46 925     if (($part->content_id || $part->content_location) && $part->filename) {
ec603f 926       if ($attachment = rcmail_save_attachment($message, $pid)) {
cc97ea 927         $_SESSION['compose']['attachments'][$attachment['id']] = $attachment;
bde421 928         $url = $RCMAIL->comm_path.'&_action=display-attachment&_file=rcmfile'.$attachment['id'];
8f2b46 929         if ($part->content_id)
A 930           $cid_map['cid:'.$part->content_id] = $url;
931         else
932           $cid_map[$part->content_location] = $url;
ec603f 933       }
2f746d 934     }
A 935   }
8f2b46 936
ec603f 937   return $cid_map;
2f746d 938 }
A 939
940 function rcmail_save_attachment(&$message, $pid)
941 {
942   $part = $message->mime_parts[$pid];
a64064 943   $mem_limit = parse_bytes(ini_get('memory_limit'));
A 944   $curr_mem = function_exists('memory_get_usage') ? memory_get_usage() : 16*1024*1024; // safe value: 16MB
945   $data = $path = null;
946
947   // don't load too big attachments into memory
948   if ($mem_limit > 0 && $part->size > $mem_limit - $curr_mem) {
949     $rcmail = rcmail::get_instance();
950     $temp_dir = unslashify($rcmail->config->get('temp_dir'));
951     $path = tempnam($temp_dir, 'rcmAttmnt');
952     if ($fp = fopen($path, 'w')) {
953       $message->get_part_content($pid, $fp);
954       fclose($fp);
955     } else
956       return false;
957   } else {
958     $data = $message->get_part_content($pid);
959   }
960
cc97ea 961   $attachment = array(
7d8e16 962     'name' => $part->filename ? $part->filename : 'Part_'.$pid.'.'.$part->ctype_secondary,
cc97ea 963     'mimetype' => $part->ctype_primary . '/' . $part->ctype_secondary,
T 964     'content_id' => $part->content_id,
a64064 965     'data' => $data,
3b1426 966     'path' => $path,
A 967     'size' => $path ? filesize($path) : strlen($data),
cc97ea 968   );
3b1426 969
e6ce00 970   $attachment = rcmail::get_instance()->plugins->exec_hook('attachment_save', $attachment);
a64064 971
cc97ea 972   if ($attachment['status']) {
76791c 973     unset($attachment['data'], $attachment['status'], $attachment['content_id'], $attachment['abort']);
cc97ea 974     return $attachment;
a64064 975   } else if ($path) {
A 976     @unlink($path);
2f746d 977   }
a64064 978   
cc97ea 979   return false;
2f746d 980 }
A 981
b575fa 982 function rcmail_save_image($path, $mimetype='')
A 983 {
984   // handle attachments in memory
985   $data = file_get_contents($path);
986
987   $attachment = array(
988     'name' => rcmail_basename($path),
989     'mimetype' => $mimetype ? $mimetype : rc_mime_content_type($path, $name),
990     'data' => $data,
991     'size' => strlen($data),
992   );
993
e6ce00 994   $attachment = rcmail::get_instance()->plugins->exec_hook('attachment_save', $attachment);
b575fa 995
A 996   if ($attachment['status']) {
997     unset($attachment['data'], $attachment['status'], $attachment['content_id'], $attachment['abort']);
998     return $attachment;
999   }
1000   
1001   return false;
1002 }
1003
1004 function rcmail_basename($filename)
1005 {
1006   // basename() is not unicode safe and locale dependent
1007   if (stristr(PHP_OS, 'win') || stristr(PHP_OS, 'netware')) {
1008     return preg_replace('/^.*[\\\\\\/]/', '', $filename);
1009   } else {
1010     return preg_replace('/^.*[\/]/', '', $filename);
1011   }
1012 }
2f746d 1013
4e17e6 1014 function rcmail_compose_subject($attrib)
4315b0 1015 {
8fa58e 1016   global $MESSAGE, $compose_mode;
4e17e6 1017   
T 1018   list($form_start, $form_end) = get_form_tags($attrib);
1019   unset($attrib['form']);
1020   
1021   $attrib['name'] = '_subject';
491a6e 1022   $attrib['spellcheck'] = 'true';
47124c 1023   $textfield = new html_inputfield($attrib);
4e17e6 1024
T 1025   $subject = '';
1026
1027   // use subject from post
5f314d 1028   if (isset($_POST['_subject'])) {
01c86f 1029     $subject = get_input_value('_subject', RCUBE_INPUT_POST, TRUE);
5f314d 1030   }
4e17e6 1031   // create a reply-subject
5f314d 1032   else if ($compose_mode == RCUBE_COMPOSE_REPLY) {
23a2ee 1033     if (preg_match('/^re:/i', $MESSAGE->subject))
8fa58e 1034       $subject = $MESSAGE->subject;
520c36 1035     else
8fa58e 1036       $subject = 'Re: '.$MESSAGE->subject;
4315b0 1037   }
4e17e6 1038   // create a forward-subject
5f314d 1039   else if ($compose_mode == RCUBE_COMPOSE_FORWARD) {
23a2ee 1040     if (preg_match('/^fwd:/i', $MESSAGE->subject))
8fa58e 1041       $subject = $MESSAGE->subject;
09941e 1042     else
8fa58e 1043       $subject = 'Fwd: '.$MESSAGE->subject;
4315b0 1044   }
1966c5 1045   // creeate a draft-subject
069704 1046   else if ($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT) {
8fa58e 1047     $subject = $MESSAGE->subject;
5f314d 1048   }
759696 1049   else if (!empty($_SESSION['compose']['param']['subject'])) {
T 1050     $subject = $_SESSION['compose']['param']['subject'];
5f314d 1051   }
4e17e6 1052   
T 1053   $out = $form_start ? "$form_start\n" : '';
1054   $out .= $textfield->show($subject);
1055   $out .= $form_end ? "\n$form_end" : '';
e99991 1056
4e17e6 1057   return $out;
4315b0 1058 }
4e17e6 1059
T 1060
1061 function rcmail_compose_attachment_list($attrib)
4315b0 1062 {
f11541 1063   global $OUTPUT, $CONFIG;
4e17e6 1064   
T 1065   // add ID if not given
1066   if (!$attrib['id'])
1067     $attrib['id'] = 'rcmAttachmentList';
1068   
8fa58e 1069   $out = "\n";
01ffe0 1070   $jslist = array();
087c7d 1071
4e17e6 1072   if (is_array($_SESSION['compose']['attachments']))
4315b0 1073   {
991a25 1074     if ($attrib['deleteicon']) {
8fa58e 1075       $button = html::img(array(
T 1076         'src' => $CONFIG['skin_path'] . $attrib['deleteicon'],
1fb587 1077         'alt' => rcube_label('delete')
991a25 1078       ));
T 1079     }
a894ba 1080     else
8fa58e 1081       $button = Q(rcube_label('delete'));
a894ba 1082
aade7b 1083     foreach ($_SESSION['compose']['attachments'] as $id => $a_prop)
6d5dba 1084     {
T 1085       if (empty($a_prop))
1086         continue;
1087       
01ffe0 1088       $out .= html::tag('li', array('id' => 'rcmfile'.$id),
8fa58e 1089         html::a(array(
T 1090             'href' => "#delete",
1091             'title' => rcube_label('delete'),
cc97ea 1092             'onclick' => sprintf("return %s.command('remove-attachment','rcmfile%s', this)", JS_OBJECT_NAME, $id)),
8fa58e 1093           $button) . Q($a_prop['name']));
01ffe0 1094         
T 1095         $jslist['rcmfile'.$id] = array('name' => $a_prop['name'], 'complete' => true, 'mimetype' => $a_prop['mimetype']);
6d5dba 1096     }
4315b0 1097   }
4e17e6 1098
21d682 1099   if ($attrib['deleteicon'])
A 1100     $_SESSION['compose']['deleteicon'] = $CONFIG['skin_path'] . $attrib['deleteicon'];
3f9712 1101   if ($attrib['cancelicon'])
V 1102     $OUTPUT->set_env('cancelicon', $CONFIG['skin_path'] . $attrib['cancelicon']);
ebf872 1103   if ($attrib['loadingicon'])
A 1104     $OUTPUT->set_env('loadingicon', $CONFIG['skin_path'] . $attrib['loadingicon']);
21d682 1105
01ffe0 1106   $OUTPUT->set_env('attachments', $jslist);
f11541 1107   $OUTPUT->add_gui_object('attachmentlist', $attrib['id']);
4e17e6 1108     
8fa58e 1109   return html::tag('ul', $attrib, $out, html::$common_attrib);
4315b0 1110 }
4e17e6 1111
T 1112
1113 function rcmail_compose_attachment_form($attrib)
4315b0 1114 {
197601 1115   global $OUTPUT;
4e17e6 1116
T 1117   // add ID if not given
1118   if (!$attrib['id'])
1119     $attrib['id'] = 'rcmUploadbox';
7df0e3 1120
A 1121   // find max filesize value
1122   $max_filesize = parse_bytes(ini_get('upload_max_filesize'));
1123   $max_postsize = parse_bytes(ini_get('post_max_size'));
1124   if ($max_postsize && $max_postsize < $max_filesize)
1125     $max_filesize = $max_postsize;
1126   $max_filesize = show_bytes($max_filesize);
4e17e6 1127   
2b77e8 1128   $button = new html_inputfield(array('type' => 'button'));
4e17e6 1129   
197601 1130   $out = html::div($attrib,
d37e1e 1131     $OUTPUT->form_tag(array('name' => 'uploadform', 'method' => 'post', 'enctype' => 'multipart/form-data'),
2b77e8 1132       html::div(null, rcmail_compose_attachment_field(array('size' => $attrib[attachmentfieldsize]))) .
7df0e3 1133       html::div('hint', rcube_label(array('name' => 'maxuploadsize', 'vars' => array('size' => $max_filesize)))) .
c17dc6 1134       html::div('buttons',
71f60c 1135         $button->show(rcube_label('close'), array('class' => 'button', 'onclick' => "$('#$attrib[id]').hide()")) . ' ' .
2b77e8 1136         $button->show(rcube_label('upload'), array('class' => 'button mainaction', 'onclick' => JS_OBJECT_NAME . ".command('send-attachment', this.form)"))
c17dc6 1137       )
A 1138     )
197601 1139   );
4e17e6 1140   
f11541 1141   $OUTPUT->add_gui_object('uploadbox', $attrib['id']);
4e17e6 1142   return $out;
4315b0 1143 }
4e17e6 1144
T 1145
1146 function rcmail_compose_attachment_field($attrib)
4315b0 1147 {
e2c610 1148   $attrib['type'] = 'file';
A 1149   $attrib['name'] = '_attachments[]';
1150   $field = new html_inputfield($attrib);
1151   return $field->show();
4315b0 1152 }
4e17e6 1153
66e2bf 1154
4e17e6 1155 function rcmail_priority_selector($attrib)
4315b0 1156 {
ff08ee 1157   global $MESSAGE;
T 1158   
4e17e6 1159   list($form_start, $form_end) = get_form_tags($attrib);
T 1160   unset($attrib['form']);
8958d0 1161
4e17e6 1162   $attrib['name'] = '_priority';
47124c 1163   $selector = new html_select($attrib);
4e17e6 1164
T 1165   $selector->add(array(rcube_label('lowest'),
1166                        rcube_label('low'),
1167                        rcube_label('normal'),
1168                        rcube_label('high'),
1169                        rcube_label('highest')),
7902df 1170                  array(5, 4, 0, 2, 1));
8958d0 1171
79eb4e 1172   if (isset($_POST['_priority']))
A 1173     $sel = $_POST['_priority'];
1174   else if (intval($MESSAGE->headers->priority) != 3)
1175     $sel = intval($MESSAGE->headers->priority);
1176   else
1177     $sel = 0;
4e17e6 1178
T 1179   $out = $form_start ? "$form_start\n" : '';
1180   $out .= $selector->show($sel);
1181   $out .= $form_end ? "\n$form_end" : '';
8958d0 1182
4e17e6 1183   return $out;
4315b0 1184 }
4e17e6 1185
T 1186
620439 1187 function rcmail_receipt_checkbox($attrib)
4315b0 1188 {
b3660b 1189   global $RCMAIL, $MESSAGE, $compose_mode;
8958d0 1190
620439 1191   list($form_start, $form_end) = get_form_tags($attrib);
T 1192   unset($attrib['form']);
8958d0 1193
66e2bf 1194   if (!isset($attrib['id']))
T 1195     $attrib['id'] = 'receipt';  
620439 1196
T 1197   $attrib['name'] = '_receipt';
66e2bf 1198   $attrib['value'] = '1';
47124c 1199   $checkbox = new html_checkbox($attrib);
620439 1200
b3660b 1201   if ($MESSAGE && in_array($compose_mode, array(RCUBE_COMPOSE_DRAFT, RCUBE_COMPOSE_EDIT)))
A 1202     $mdn_default = (bool) $MESSAGE->headers->mdn_to;
1203   else
1204     $mdn_default = $RCMAIL->config->get('mdn_default');
1205
620439 1206   $out = $form_start ? "$form_start\n" : '';
b3660b 1207   $out .= $checkbox->show($mdn_default);
620439 1208   $out .= $form_end ? "\n$form_end" : '';
T 1209
1210   return $out;
4315b0 1211 }
620439 1212
T 1213
f22ea7 1214 function rcmail_dsn_checkbox($attrib)
A 1215 {
1216   global $RCMAIL;
1217
1218   list($form_start, $form_end) = get_form_tags($attrib);
1219   unset($attrib['form']);
1220
1221   if (!isset($attrib['id']))
1222     $attrib['id'] = 'dsn';
1223
1224   $attrib['name'] = '_dsn';
1225   $attrib['value'] = '1';
1226   $checkbox = new html_checkbox($attrib);
1227
1228   $out = $form_start ? "$form_start\n" : '';
1229   $out .= $checkbox->show($RCMAIL->config->get('dsn_default'));
1230   $out .= $form_end ? "\n$form_end" : '';
1231
1232   return $out;
1233 }
1234
1235
a0109c 1236 function rcmail_editor_selector($attrib)
S 1237 {
1238   global $CONFIG, $MESSAGE, $compose_mode;
1239
8fa58e 1240   // determine whether HTML or plain text should be checked
868deb 1241   $useHtml = rcmail_compose_editor_mode();
d9344f 1242
309d2f 1243   if (empty($attrib['editorid']))
a01b3b 1244     $attrib['editorid'] = 'rcmComposeBody';
79af0b 1245
309d2f 1246   if (empty($attrib['name']))
A 1247     $attrib['name'] = 'editorSelect';
8958d0 1248
5821ff 1249   $attrib['onchange'] = "return rcmail_toggle_editor(this, '".$attrib['editorid']."', '_is_html')";
309d2f 1250
A 1251   $select = new html_select($attrib);
1252
1253   $select->add(Q(rcube_label('htmltoggle')), 'html');
1254   $select->add(Q(rcube_label('plaintoggle')), 'plain');
1255
1256   return $select->show($useHtml ? 'html' : 'plain');
79af0b 1257
868deb 1258   foreach ($choices as $value => $text) {
6b1fc0 1259     $attrib['id'] = '_' . $value;
d9344f 1260     $attrib['value'] = $value;
8fa58e 1261     $selector .= $radio->show($chosenvalue, $attrib) . html::label($attrib['id'], Q(rcube_label($text)));
4315b0 1262   }
a0109c 1263
S 1264   return $selector;
1265 }
1266
1267
faf876 1268 function rcmail_store_target_selection($attrib)
T 1269 {
1270   $attrib['name'] = '_store_target';
a81be1 1271   $select = rcmail_mailbox_select(array_merge($attrib, array('noselection' => '- '.rcube_label('dontsave').' -')));
814905 1272   return $select->show($_SESSION['compose']['param']['sent_mbox'], $attrib);
faf876 1273 }
T 1274
1275
eeb85f 1276 function rcmail_check_sent_folder($folder, $create=false)
A 1277 {
1278   global $IMAP;
1279
1280   if ($IMAP->mailbox_exists($folder, true)) {
1281     return true;
1282   }
1283
1284   // folder may exist but isn't subscribed (#1485241)
1285   if ($create) {
1286     if (!$IMAP->mailbox_exists($folder))
1287       return $IMAP->create_mailbox($folder, true);
1288     else
1289       return $IMAP->subscribe($folder);
1290   }
1291
1292   return false;
1293 }
1294
1295
4e17e6 1296 function get_form_tags($attrib)
4315b0 1297 {
197601 1298   global $RCMAIL, $MESSAGE_FORM;
4e17e6 1299
T 1300   $form_start = '';
8958d0 1301   if (!$MESSAGE_FORM)
4315b0 1302   {
197601 1303     $hiddenfields = new html_hiddenfield(array('name' => '_task', 'value' => $RCMAIL->task));
4e17e6 1304     $hiddenfields->add(array('name' => '_action', 'value' => 'send'));
1966c5 1305
197601 1306     $form_start = empty($attrib['form']) ? $RCMAIL->output->form_tag(array('name' => "form", 'method' => "post")) : '';
4e17e6 1307     $form_start .= $hiddenfields->show();
4315b0 1308   }
eeb85f 1309
8958d0 1310   $form_end = ($MESSAGE_FORM && !strlen($attrib['form'])) ? '</form>' : '';
597170 1311   $form_name = !empty($attrib['form']) ? $attrib['form'] : 'form';
eeb85f 1312
8958d0 1313   if (!$MESSAGE_FORM)
197601 1314     $RCMAIL->output->add_gui_object('messageform', $form_name);
eeb85f 1315
4e17e6 1316   $MESSAGE_FORM = $form_name;
T 1317
47124c 1318   return array($form_start, $form_end);
4315b0 1319 }
4e17e6 1320
T 1321
f11541 1322 // register UI objects
T 1323 $OUTPUT->add_handlers(array(
1324   'composeheaders' => 'rcmail_compose_headers',
1325   'composesubject' => 'rcmail_compose_subject',
1326   'composebody' => 'rcmail_compose_body',
1327   'composeattachmentlist' => 'rcmail_compose_attachment_list',
1328   'composeattachmentform' => 'rcmail_compose_attachment_form',
1329   'composeattachment' => 'rcmail_compose_attachment_field',
1330   'priorityselector' => 'rcmail_priority_selector',
1331   'editorselector' => 'rcmail_editor_selector',
1332   'receiptcheckbox' => 'rcmail_receipt_checkbox',
f22ea7 1333   'dsncheckbox' => 'rcmail_dsn_checkbox',
faf876 1334   'storetarget' => 'rcmail_store_target_selection',
f11541 1335 ));
a0530a 1336
47124c 1337 $OUTPUT->send('compose');
a0530a 1338
b25dfd 1339