source: github/program/steps/mail/compose.inc @ e5d60d6

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