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