source: subversion/branches/release-0.7/program/steps/mail/compose.inc @ 5402

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