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

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