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

Last change on this file since 4604 was 4604, checked in by thomasb, 2 years ago

Prepare for multiple concurrent compose windows

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 44.0 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 session"), 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', 'fileuploaderror',
114    'autocompletechars');
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$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']['draft_uid']) {
131  $RCMAIL->imap->set_mailbox($CONFIG['drafts_mbox']);
132  $compose_mode = RCUBE_COMPOSE_DRAFT;
133}
134else if ($msg_uid = $_SESSION['compose']['param']['reply_uid'])
135  $compose_mode = RCUBE_COMPOSE_REPLY;
136else if ($msg_uid = $_SESSION['compose']['param']['forward_uid'])
137  $compose_mode = RCUBE_COMPOSE_FORWARD;
138else if ($msg_uid = $_SESSION['compose']['param']['uid'])
139  $compose_mode = RCUBE_COMPOSE_EDIT;
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'].'&_id='.$_SESSION['compose']['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'].'&_id='.$_SESSION['compose']['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'].'&_id='.$_SESSION['compose']['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    'group' => $_SESSION['compose']['id'],
984    'name' => $part->filename ? $part->filename : 'Part_'.$pid.'.'.$part->ctype_secondary,
985    'mimetype' => $part->ctype_primary . '/' . $part->ctype_secondary,
986    'content_id' => $part->content_id,
987    'data' => $data,
988    'path' => $path,
989    'size' => $path ? filesize($path) : strlen($data),
990  );
991
992  $attachment = rcmail::get_instance()->plugins->exec_hook('attachment_save', $attachment);
993
994  if ($attachment['status']) {
995    unset($attachment['data'], $attachment['status'], $attachment['content_id'], $attachment['abort']);
996    return $attachment;
997  } else if ($path) {
998    @unlink($path);
999  }
1000 
1001  return false;
1002}
1003
1004function rcmail_save_image($path, $mimetype='')
1005{
1006  // handle attachments in memory
1007  $data = file_get_contents($path);
1008
1009  $attachment = array(
1010    'group' => $_SESSION['compose']['id'],
1011    'name' => rcmail_basename($path),
1012    'mimetype' => $mimetype ? $mimetype : rc_mime_content_type($path, $name),
1013    'data' => $data,
1014    'size' => strlen($data),
1015  );
1016
1017  $attachment = rcmail::get_instance()->plugins->exec_hook('attachment_save', $attachment);
1018
1019  if ($attachment['status']) {
1020    unset($attachment['data'], $attachment['status'], $attachment['content_id'], $attachment['abort']);
1021    return $attachment;
1022  }
1023 
1024  return false;
1025}
1026
1027function rcmail_basename($filename)
1028{
1029  // basename() is not unicode safe and locale dependent
1030  if (stristr(PHP_OS, 'win') || stristr(PHP_OS, 'netware')) {
1031    return preg_replace('/^.*[\\\\\\/]/', '', $filename);
1032  } else {
1033    return preg_replace('/^.*[\/]/', '', $filename);
1034  }
1035}
1036
1037function rcmail_compose_subject($attrib)
1038{
1039  global $MESSAGE, $compose_mode;
1040 
1041  list($form_start, $form_end) = get_form_tags($attrib);
1042  unset($attrib['form']);
1043 
1044  $attrib['name'] = '_subject';
1045  $attrib['spellcheck'] = 'true';
1046  $textfield = new html_inputfield($attrib);
1047
1048  $subject = '';
1049
1050  // use subject from post
1051  if (isset($_POST['_subject'])) {
1052    $subject = get_input_value('_subject', RCUBE_INPUT_POST, TRUE);
1053  }
1054  // create a reply-subject
1055  else if ($compose_mode == RCUBE_COMPOSE_REPLY) {
1056    if (preg_match('/^re:/i', $MESSAGE->subject))
1057      $subject = $MESSAGE->subject;
1058    else
1059      $subject = 'Re: '.$MESSAGE->subject;
1060  }
1061  // create a forward-subject
1062  else if ($compose_mode == RCUBE_COMPOSE_FORWARD) {
1063    if (preg_match('/^fwd:/i', $MESSAGE->subject))
1064      $subject = $MESSAGE->subject;
1065    else
1066      $subject = 'Fwd: '.$MESSAGE->subject;
1067  }
1068  // creeate a draft-subject
1069  else if ($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT) {
1070    $subject = $MESSAGE->subject;
1071  }
1072  else if (!empty($_SESSION['compose']['param']['subject'])) {
1073    $subject = $_SESSION['compose']['param']['subject'];
1074  }
1075 
1076  $out = $form_start ? "$form_start\n" : '';
1077  $out .= $textfield->show($subject);
1078  $out .= $form_end ? "\n$form_end" : '';
1079
1080  return $out;
1081}
1082
1083
1084function rcmail_compose_attachment_list($attrib)
1085{
1086  global $OUTPUT, $CONFIG;
1087 
1088  // add ID if not given
1089  if (!$attrib['id'])
1090    $attrib['id'] = 'rcmAttachmentList';
1091 
1092  $out = "\n";
1093  $jslist = array();
1094
1095  if (is_array($_SESSION['compose']['attachments']))
1096  {
1097    if ($attrib['deleteicon']) {
1098      $button = html::img(array(
1099        'src' => $CONFIG['skin_path'] . $attrib['deleteicon'],
1100        'alt' => rcube_label('delete')
1101      ));
1102    }
1103    else
1104      $button = Q(rcube_label('delete'));
1105
1106    foreach ($_SESSION['compose']['attachments'] as $id => $a_prop)
1107    {
1108      if (empty($a_prop))
1109        continue;
1110     
1111      $out .= html::tag('li', array('id' => 'rcmfile'.$id),
1112        html::a(array(
1113            'href' => "#delete",
1114            'title' => rcube_label('delete'),
1115            'onclick' => sprintf("return %s.command('remove-attachment','rcmfile%s', this)", JS_OBJECT_NAME, $id)),
1116          $button) . Q($a_prop['name']));
1117       
1118        $jslist['rcmfile'.$id] = array('name' => $a_prop['name'], 'complete' => true, 'mimetype' => $a_prop['mimetype']);
1119    }
1120  }
1121
1122  if ($attrib['deleteicon'])
1123    $_SESSION['compose']['deleteicon'] = $CONFIG['skin_path'] . $attrib['deleteicon'];
1124  if ($attrib['cancelicon'])
1125    $OUTPUT->set_env('cancelicon', $CONFIG['skin_path'] . $attrib['cancelicon']);
1126  if ($attrib['loadingicon'])
1127    $OUTPUT->set_env('loadingicon', $CONFIG['skin_path'] . $attrib['loadingicon']);
1128
1129  $OUTPUT->set_env('attachments', $jslist);
1130  $OUTPUT->add_gui_object('attachmentlist', $attrib['id']);
1131   
1132  return html::tag('ul', $attrib, $out, html::$common_attrib);
1133}
1134
1135
1136function rcmail_compose_attachment_form($attrib)
1137{
1138  global $OUTPUT;
1139
1140  // add ID if not given
1141  if (!$attrib['id'])
1142    $attrib['id'] = 'rcmUploadbox';
1143
1144  // find max filesize value
1145  $max_filesize = parse_bytes(ini_get('upload_max_filesize'));
1146  $max_postsize = parse_bytes(ini_get('post_max_size'));
1147  if ($max_postsize && $max_postsize < $max_filesize)
1148    $max_filesize = $max_postsize;
1149  $max_filesize = show_bytes($max_filesize);
1150 
1151  $button = new html_inputfield(array('type' => 'button'));
1152 
1153  $out = html::div($attrib,
1154    $OUTPUT->form_tag(array('name' => 'uploadform', 'method' => 'post', 'enctype' => 'multipart/form-data'),
1155      html::div(null, rcmail_compose_attachment_field(array('size' => $attrib['attachmentfieldsize']))) .
1156      html::div('hint', rcube_label(array('name' => 'maxuploadsize', 'vars' => array('size' => $max_filesize)))) .
1157      html::div('buttons',
1158        $button->show(rcube_label('close'), array('class' => 'button', 'onclick' => "$('#$attrib[id]').hide()")) . ' ' .
1159        $button->show(rcube_label('upload'), array('class' => 'button mainaction', 'onclick' => JS_OBJECT_NAME . ".command('send-attachment', this.form)"))
1160      )
1161    )
1162  );
1163 
1164  $OUTPUT->add_gui_object('uploadbox', $attrib['id']);
1165  return $out;
1166}
1167
1168
1169function rcmail_compose_attachment_field($attrib)
1170{
1171  $attrib['type'] = 'file';
1172  $attrib['name'] = '_attachments[]';
1173  $field = new html_inputfield($attrib);
1174  return $field->show();
1175}
1176
1177
1178function rcmail_priority_selector($attrib)
1179{
1180  global $MESSAGE;
1181 
1182  list($form_start, $form_end) = get_form_tags($attrib);
1183  unset($attrib['form']);
1184
1185  $attrib['name'] = '_priority';
1186  $selector = new html_select($attrib);
1187
1188  $selector->add(array(rcube_label('lowest'),
1189                       rcube_label('low'),
1190                       rcube_label('normal'),
1191                       rcube_label('high'),
1192                       rcube_label('highest')),
1193                 array(5, 4, 0, 2, 1));
1194
1195  if (isset($_POST['_priority']))
1196    $sel = $_POST['_priority'];
1197  else if (intval($MESSAGE->headers->priority) != 3)
1198    $sel = intval($MESSAGE->headers->priority);
1199  else
1200    $sel = 0;
1201
1202  $out = $form_start ? "$form_start\n" : '';
1203  $out .= $selector->show($sel);
1204  $out .= $form_end ? "\n$form_end" : '';
1205
1206  return $out;
1207}
1208
1209
1210function rcmail_receipt_checkbox($attrib)
1211{
1212  global $RCMAIL, $MESSAGE, $compose_mode;
1213
1214  list($form_start, $form_end) = get_form_tags($attrib);
1215  unset($attrib['form']);
1216
1217  if (!isset($attrib['id']))
1218    $attrib['id'] = 'receipt'; 
1219
1220  $attrib['name'] = '_receipt';
1221  $attrib['value'] = '1';
1222  $checkbox = new html_checkbox($attrib);
1223
1224  if ($MESSAGE && in_array($compose_mode, array(RCUBE_COMPOSE_DRAFT, RCUBE_COMPOSE_EDIT)))
1225    $mdn_default = (bool) $MESSAGE->headers->mdn_to;
1226  else
1227    $mdn_default = $RCMAIL->config->get('mdn_default');
1228
1229  $out = $form_start ? "$form_start\n" : '';
1230  $out .= $checkbox->show($mdn_default);
1231  $out .= $form_end ? "\n$form_end" : '';
1232
1233  return $out;
1234}
1235
1236
1237function rcmail_dsn_checkbox($attrib)
1238{
1239  global $RCMAIL;
1240
1241  list($form_start, $form_end) = get_form_tags($attrib);
1242  unset($attrib['form']);
1243
1244  if (!isset($attrib['id']))
1245    $attrib['id'] = 'dsn';
1246
1247  $attrib['name'] = '_dsn';
1248  $attrib['value'] = '1';
1249  $checkbox = new html_checkbox($attrib);
1250
1251  $out = $form_start ? "$form_start\n" : '';
1252  $out .= $checkbox->show($RCMAIL->config->get('dsn_default'));
1253  $out .= $form_end ? "\n$form_end" : '';
1254
1255  return $out;
1256}
1257
1258
1259function rcmail_editor_selector($attrib)
1260{
1261  global $CONFIG, $MESSAGE, $compose_mode;
1262
1263  // determine whether HTML or plain text should be checked
1264  $useHtml = rcmail_compose_editor_mode();
1265
1266  if (empty($attrib['editorid']))
1267    $attrib['editorid'] = 'rcmComposeBody';
1268
1269  if (empty($attrib['name']))
1270    $attrib['name'] = 'editorSelect';
1271
1272  $attrib['onchange'] = "return rcmail_toggle_editor(this, '".$attrib['editorid']."', '_is_html')";
1273
1274  $select = new html_select($attrib);
1275
1276  $select->add(Q(rcube_label('htmltoggle')), 'html');
1277  $select->add(Q(rcube_label('plaintoggle')), 'plain');
1278
1279  return $select->show($useHtml ? 'html' : 'plain');
1280
1281  foreach ($choices as $value => $text) {
1282    $attrib['id'] = '_' . $value;
1283    $attrib['value'] = $value;
1284    $selector .= $radio->show($chosenvalue, $attrib) . html::label($attrib['id'], Q(rcube_label($text)));
1285  }
1286
1287  return $selector;
1288}
1289
1290
1291function rcmail_store_target_selection($attrib)
1292{
1293  $attrib['name'] = '_store_target';
1294  $select = rcmail_mailbox_select(array_merge($attrib, array('noselection' => '- '.rcube_label('dontsave').' -')));
1295  return $select->show($_SESSION['compose']['param']['sent_mbox'], $attrib);
1296}
1297
1298
1299function rcmail_check_sent_folder($folder, $create=false)
1300{
1301  global $IMAP;
1302
1303  if ($IMAP->mailbox_exists($folder, true)) {
1304    return true;
1305  }
1306
1307  // folder may exist but isn't subscribed (#1485241)
1308  if ($create) {
1309    if (!$IMAP->mailbox_exists($folder))
1310      return $IMAP->create_mailbox($folder, true);
1311    else
1312      return $IMAP->subscribe($folder);
1313  }
1314
1315  return false;
1316}
1317
1318
1319function get_form_tags($attrib)
1320{
1321  global $RCMAIL, $MESSAGE_FORM;
1322
1323  $form_start = '';
1324  if (!$MESSAGE_FORM)
1325  {
1326    $hiddenfields = new html_hiddenfield(array('name' => '_task', 'value' => $RCMAIL->task));
1327    $hiddenfields->add(array('name' => '_action', 'value' => 'send'));
1328    $hiddenfields->add(array('name' => '_id', 'value' => $_SESSION['compose']['id']));
1329
1330    $form_start = empty($attrib['form']) ? $RCMAIL->output->form_tag(array('name' => "form", 'method' => "post")) : '';
1331    $form_start .= $hiddenfields->show();
1332  }
1333
1334  $form_end = ($MESSAGE_FORM && !strlen($attrib['form'])) ? '</form>' : '';
1335  $form_name = !empty($attrib['form']) ? $attrib['form'] : 'form';
1336
1337  if (!$MESSAGE_FORM)
1338    $RCMAIL->output->add_gui_object('messageform', $form_name);
1339
1340  $MESSAGE_FORM = $form_name;
1341
1342  return array($form_start, $form_end);
1343}
1344
1345
1346// register UI objects
1347$OUTPUT->add_handlers(array(
1348  'composeheaders' => 'rcmail_compose_headers',
1349  'composesubject' => 'rcmail_compose_subject',
1350  'composebody' => 'rcmail_compose_body',
1351  'composeattachmentlist' => 'rcmail_compose_attachment_list',
1352  'composeattachmentform' => 'rcmail_compose_attachment_form',
1353  'composeattachment' => 'rcmail_compose_attachment_field',
1354  'priorityselector' => 'rcmail_priority_selector',
1355  'editorselector' => 'rcmail_editor_selector',
1356  'receiptcheckbox' => 'rcmail_receipt_checkbox',
1357  'dsncheckbox' => 'rcmail_dsn_checkbox',
1358  'storetarget' => 'rcmail_store_target_selection',
1359));
1360
1361$OUTPUT->send('compose');
1362
1363
Note: See TracBrowser for help on using the repository browser.