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

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