source: subversion/trunk/roundcubemail/program/steps/mail/compose.inc @ 5226

Last change on this file since 5226 was 5226, checked in by thomasb, 21 months ago

Fix session race conditions when composing new messages

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