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

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