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

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