source: github/program/steps/mail/compose.inc @ 087c7dc

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