source: github/program/steps/mail/compose.inc @ 69cb80b

release-0.7
Last change on this file since 69cb80b was e750d1b, checked in by thomascube <thomas@…>, 20 months ago

Restrict folders list to write-only in selectors for special folders and save-message-to option

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