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

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