source: subversion/trunk/roundcubemail/program/steps/mail/compose.inc @ 3262

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