source: github/program/steps/mail/compose.inc @ c296b81

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