source: github/program/steps/mail/compose.inc @ 8abe548

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