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

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