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

HEADcourier-fixdev-browser-capabilitiespdorelease-0.6release-0.7release-0.8
Last change on this file since b25dfd0 was b25dfd0, checked in by alecpl <alec@…>, 3 years ago
  • removed PHP closing tag
  • Property mode set to 100644
File size: 37.3 KB
Line 
1<?php
2
3/*
4 +-----------------------------------------------------------------------+
5 | program/steps/mail/compose.inc                                        |
6 |                                                                       |
7 | This file is part of the RoundCube Webmail client                     |
8 | Copyright (C) 2005-2009, RoundCube Dev. - Switzerland                 |
9 | Licensed under the GNU GPL                                            |
10 |                                                                       |
11 | PURPOSE:                                                              |
12 |   Compose a new mail message with all headers and attachments         |
13 |                                                                       |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16 +-----------------------------------------------------------------------+
17
18 $Id$
19
20*/
21
22// define constants for message compose mode
23define('RCUBE_COMPOSE_REPLY', 0x0106);
24define('RCUBE_COMPOSE_FORWARD', 0x0107);
25define('RCUBE_COMPOSE_DRAFT', 0x0108);
26define('RCUBE_COMPOSE_EDIT', 0x0109);
27
28$MESSAGE_FORM = NULL;
29$MESSAGE = NULL;
30
31// Nothing below is called during message composition, only at "new/forward/reply/draft" initialization or
32// if a compose-ID is given (i.e. when the compose step is opened in a new window/tab).
33// Since there are many ways to leave the compose page improperly, it seems necessary to clean-up an old
34// compose when a "new/forward/reply/draft" is called - otherwise the old session attachments will appear
35
36if (!is_array($_SESSION['compose']) || $_SESSION['compose']['id'] != get_input_value('_id', RCUBE_INPUT_GET))
37{
38  rcmail_compose_cleanup();
39  $_SESSION['compose'] = array(
40    'id' => uniqid(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 $form_start . 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  return $out;
416}
417
418
419function rcmail_compose_body($attrib)
420{
421  global $RCMAIL, $CONFIG, $OUTPUT, $MESSAGE, $compose_mode, $LINE_LENGTH;
422 
423  list($form_start, $form_end) = get_form_tags($attrib);
424  unset($attrib['form']);
425 
426  if (empty($attrib['id']))
427    $attrib['id'] = 'rcmComposeBody';
428
429  $attrib['name'] = '_message';
430
431  if ($CONFIG['htmleditor'] || (($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT) && $MESSAGE->has_html_part()))
432    $isHtml = true;
433  else
434    $isHtml = false;
435
436  $body = '';
437
438  // use posted message body
439  if (!empty($_POST['_message']))
440  {
441    $body = get_input_value('_message', RCUBE_INPUT_POST, true);
442  }
443  else if ($_SESSION['compose']['param']['body'])
444  {
445    $body = $_SESSION['compose']['param']['body'];
446    $isHtml = false;
447  }
448  else if ($compose_mode)
449  {
450    $has_html_part = $MESSAGE->has_html_part();
451    if (($isHtml || $compose_mode == RCUBE_COMPOSE_DRAFT) && $has_html_part)
452    {
453      $body = $MESSAGE->first_html_part();
454      $isHtml = true;
455    }
456    else if ($has_html_part)
457    {
458      // use html part if it has been used for message (pre)viewing
459      // decrease line length for quoting
460      $len = $compose_mode == RCUBE_COMPOSE_REPLY ? $LINE_LENGTH-2 : $LINE_LENGTH;
461      $txt = new html2text($MESSAGE->first_html_part(), false, true, $len);
462      $body = $txt->get_text();
463      $isHtml = false;
464    }
465    else
466    {
467      $body = $MESSAGE->first_text_part();
468      $isHtml = false;
469    }
470
471    // compose reply-body
472    if ($compose_mode == RCUBE_COMPOSE_REPLY)
473      $body = rcmail_create_reply_body($body, $isHtml);
474    // forward message body inline
475    else if ($compose_mode == RCUBE_COMPOSE_FORWARD)
476      $body = rcmail_create_forward_body($body, $isHtml);
477    // load draft message body
478    else if ($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT)
479      $body = rcmail_create_draft_body($body, $isHtml);
480  }
481
482  $plugin = $RCMAIL->plugins->exec_hook('message_compose_body',
483    array('body' => $body, 'html' => $isHtml, 'mode' => $compose_mode));
484  $body = $plugin['body'];
485  unset($plugin);
486
487  // add blocked.gif attachment (#1486516)
488  if ($isHtml && preg_match('#<img src="\./program/blocked\.gif"#', $body)) {
489    if ($attachment = rcmail_save_image('program/blocked.gif', 'image/gif')) {
490      $_SESSION['compose']['attachments'][$attachment['id']] = $attachment;
491      $body = preg_replace('#\./program/blocked\.gif#',
492        $RCMAIL->comm_path.'&_action=display-attachment&_file=rcmfile'.$attachment['id'],
493        $body);
494    }
495  }
496 
497  $out = $form_start ? "$form_start\n" : '';
498
499  $saveid = new html_hiddenfield(array('name' => '_draft_saveid', 'value' => $compose_mode==RCUBE_COMPOSE_DRAFT ? str_replace(array('<','>'), "", $MESSAGE->headers->messageID) : ''));
500  $out .= $saveid->show();
501
502  $drafttoggle = new html_hiddenfield(array('name' => '_draft', 'value' => 'yes'));
503  $out .= $drafttoggle->show();
504
505  $msgtype = new html_hiddenfield(array('name' => '_is_html', 'value' => ($isHtml?"1":"0")));
506  $out .= $msgtype->show();
507
508  // If desired, set this textarea to be editable by TinyMCE
509  if ($isHtml) $attrib['class'] = 'mce_editor';
510  $textarea = new html_textarea($attrib);
511  $out .= $textarea->show($body);
512  $out .= $form_end ? "\n$form_end" : '';
513
514  $OUTPUT->set_env('composebody', $attrib['id']);
515
516  // include HTML editor
517  rcube_html_editor();
518 
519  // include GoogieSpell
520  if (!empty($CONFIG['enable_spellcheck'])) {
521
522    $engine = $RCMAIL->config->get('spellcheck_engine','googie');
523    $spellcheck_langs = (array) $RCMAIL->config->get('spellcheck_languages',
524      array('da'=>'Dansk', 'de'=>'Deutsch', 'en' => 'English', 'es'=>'Español',
525            'fr'=>'Français', 'it'=>'Italiano', 'nl'=>'Nederlands', 'pl'=>'Polski',
526            'pt'=>'Português', 'fi'=>'Suomi', 'sv'=>'Svenska'));
527
528    // googie works only with two-letter codes
529    if ($engine == 'googie') {
530      $lang = strtolower(substr($_SESSION['language'], 0, 2));
531
532      $spellcheck_langs_googie = array();
533      foreach ($spellcheck_langs as $key => $name)
534        $spellcheck_langs_googie[strtolower(substr($key,0,2))] = $name;
535        $spellcheck_langs = $spellcheck_langs_googie;
536    }
537    else {
538      $lang = $_SESSION['language'];
539
540      // if not found in the list, try with two-letter code
541      if (!$spellcheck_langs[$lang])
542        $lang = strtolower(substr($lang, 0, 2));
543    }
544
545    if (!$spellcheck_langs[$lang])
546      $lang = 'en';
547
548    $editor_lang_set = array();
549    foreach ($spellcheck_langs as $key => $name) {
550      $editor_lang_set[] = ($key == $lang ? '+' : '') . JQ($name).'='.JQ($key);
551    }
552   
553    $OUTPUT->include_script('googiespell.js');
554    $OUTPUT->add_script(sprintf(
555      "var googie = new GoogieSpell('\$__skin_path/images/googiespell/','?_task=utils&_action=spell&lang=');\n".
556      "googie.lang_chck_spell = \"%s\";\n".
557      "googie.lang_rsm_edt = \"%s\";\n".
558      "googie.lang_close = \"%s\";\n".
559      "googie.lang_revert = \"%s\";\n".
560      "googie.lang_no_error_found = \"%s\";\n".
561      "googie.setLanguages(%s);\n".
562      "googie.setCurrentLanguage('%s');\n".
563      "googie.setSpellContainer('spellcheck-control');\n".
564      "googie.decorateTextarea('%s');\n".
565      "%s.set_env('spellcheck', googie);",
566      JQ(Q(rcube_label('checkspelling'))),
567      JQ(Q(rcube_label('resumeediting'))),
568      JQ(Q(rcube_label('close'))),
569      JQ(Q(rcube_label('revertto'))),
570      JQ(Q(rcube_label('nospellerrors'))),
571      json_serialize($spellcheck_langs),
572      $lang,
573      $attrib['id'],
574      JS_OBJECT_NAME), 'foot');
575
576    $OUTPUT->add_label('checking');
577    $OUTPUT->set_env('spellcheck_langs', join(',', $editor_lang_set));
578  }
579 
580  $out .= "\n".'<iframe name="savetarget" src="program/blank.gif" style="width:0;height:0;border:none;visibility:hidden;"></iframe>';
581
582  return $out;
583}
584
585
586function rcmail_create_reply_body($body, $bodyIsHtml)
587{
588  global $RCMAIL, $MESSAGE, $LINE_LENGTH;
589
590  if (!$bodyIsHtml) {
591    // try to remove the signature
592    if ($RCMAIL->config->get('strip_existing_sig', true) && ($sp = strrpos($body, '-- ')) !== false && ($sp == 0 || $body{$sp-1} == "\n")) {
593      if ($body{$sp+3}==' ' || $body{$sp+3}=="\n" || $body{$sp+3}=="\r")
594        $body = substr($body, 0, max(0, $sp-1));
595    }
596
597    // soft-wrap and quote message text
598    $body = rcmail_wrap_and_quote(rtrim($body, "\r\n"), $LINE_LENGTH);
599
600    // add title line(s)
601    $prefix = sprintf("On %s, %s wrote:\n",
602      $MESSAGE->headers->date,
603      $MESSAGE->get_header('from'));
604
605    $suffix = '';
606   
607    if ($RCMAIL->config->get('top_posting'))
608      $prefix = "\n\n\n" . $prefix;
609  }
610  else {
611    // save inline images to files
612    $cid_map = rcmail_write_inline_attachments($MESSAGE);
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    // build reply (quote content)
619    $prefix = sprintf("<p>On %s, %s wrote:</p>\n",
620      $MESSAGE->headers->date,
621      htmlspecialchars(Q($MESSAGE->get_header('from'), 'replace'), ENT_COMPAT, $RCMAIL->output->get_charset()));
622    $prefix .= '<blockquote>';
623
624    if ($RCMAIL->config->get('top_posting')) {
625      $prefix = '<br>' . $prefix;
626      $suffix = '</blockquote>';
627    }
628    else {
629      $suffix = '</blockquote><p></p>';
630    }
631  }
632
633  return $prefix.$body.$suffix;
634}
635
636
637function rcmail_create_forward_body($body, $bodyIsHtml)
638{
639  global $IMAP, $MESSAGE, $OUTPUT;
640
641  // add attachments
642  if (!isset($_SESSION['compose']['forward_attachments']) && is_array($MESSAGE->mime_parts))
643    $cid_map = rcmail_write_compose_attachments($MESSAGE, $bodyIsHtml);
644
645  if (!$bodyIsHtml)
646  {
647    $prefix = "\n\n\n-------- Original Message --------\n";
648    $prefix .= 'Subject: ' . $MESSAGE->subject . "\n";
649    $prefix .= 'Date: ' . $MESSAGE->headers->date . "\n";
650    $prefix .= 'From: ' . $MESSAGE->get_header('from') . "\n";
651    $prefix .= 'To: ' . $MESSAGE->get_header('to') . "\n";
652
653    if ($MESSAGE->headers->cc)
654      $prefix .= 'Cc: ' . $MESSAGE->get_header('cc') . "\n";
655    if ($MESSAGE->headers->replyto && $MESSAGE->headers->replyto != $MESSAGE->headers->from)
656      $prefix .= 'Reply-To: ' . $MESSAGE->get_header('replyto') . "\n";
657
658    $prefix .= "\n";
659  }
660  else
661  {
662    // set is_safe flag (we need this for html body washing)
663    rcmail_check_safe($MESSAGE);
664    // clean up html tags
665    $body = rcmail_wash_html($body, array('safe' => $MESSAGE->is_safe), $cid_map);
666
667    $prefix = sprintf(
668      "<br /><p>-------- Original Message --------</p>" .
669        "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody>" .
670        "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">Subject: </th><td>%s</td></tr>" .
671        "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">Date: </th><td>%s</td></tr>" .
672        "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">From: </th><td>%s</td></tr>" .
673        "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">To: </th><td>%s</td></tr>",
674      Q($MESSAGE->subject),
675      Q($MESSAGE->headers->date),
676      htmlspecialchars(Q($MESSAGE->get_header('from'), 'replace'), ENT_COMPAT, $OUTPUT->get_charset()),
677      htmlspecialchars(Q($MESSAGE->get_header('to'), 'replace'), ENT_COMPAT, $OUTPUT->get_charset()));
678
679    if ($MESSAGE->headers->cc)
680      $prefix .= sprintf("<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">Cc: </th><td>%s</td></tr>",
681        htmlspecialchars(Q($MESSAGE->get_header('cc'), 'replace'), ENT_COMPAT, $OUTPUT->get_charset()));
682
683    if ($MESSAGE->headers->replyto && $MESSAGE->headers->replyto != $MESSAGE->headers->from)
684      $prefix .= sprintf("<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">Reply-To: </th><td>%s</td></tr>",
685        htmlspecialchars(Q($MESSAGE->get_header('replyto'), 'replace'), ENT_COMPAT, $OUTPUT->get_charset()));
686
687    $prefix .= "</tbody></table><br>";
688  }
689   
690  return $prefix.$body;
691}
692
693
694function rcmail_create_draft_body($body, $bodyIsHtml)
695{
696  global $MESSAGE, $OUTPUT;
697 
698  /**
699   * add attachments
700   * sizeof($MESSAGE->mime_parts can be 1 - e.g. attachment, but no text!
701   */
702  if (empty($_SESSION['compose']['forward_attachments'])
703      && is_array($MESSAGE->mime_parts)
704      && count($MESSAGE->mime_parts) > 0)
705  {
706    $cid_map = rcmail_write_compose_attachments($MESSAGE, $bodyIsHtml);
707
708    // replace cid with href in inline images links
709    if ($cid_map)
710      $body = str_replace(array_keys($cid_map), array_values($cid_map), $body);
711  }
712 
713  return $body;
714}
715 
716 
717function rcmail_write_compose_attachments(&$message, $bodyIsHtml)
718{
719  global $OUTPUT;
720
721  $cid_map = $messages = array();
722  foreach ((array)$message->mime_parts as $pid => $part)
723  {
724    if (($part->ctype_primary != 'message' || !$bodyIsHtml) && $part->ctype_primary != 'multipart' &&
725        ($part->disposition == 'attachment' || ($part->disposition == 'inline' && $bodyIsHtml) || $part->filename)
726        && $part->mimetype != 'application/ms-tnef'
727    ) {
728      $skip = false;
729      if ($part->mimetype == 'message/rfc822') {
730        $messages[] = $part->mime_id;
731      } else if ($messages) {
732        // skip attachments included in message/rfc822 attachment (#1486487)
733        foreach ($messages as $mimeid)
734          if (strpos($part->mime_id, $mimeid.'.') === 0) {
735            $skip = true;
736            break;
737          }
738      }
739
740      if (!$skip && ($attachment = rcmail_save_attachment($message, $pid))) {
741        $_SESSION['compose']['attachments'][$attachment['id']] = $attachment;
742        if ($bodyIsHtml && ($part->content_id || $part->content_location)) {
743          $url = $OUTPUT->app->comm_path.'&_action=display-attachment&_file=rcmfile'.$attachment['id'];
744          if ($part->content_id)
745            $cid_map['cid:'.$part->content_id] = $url;
746          else
747            $cid_map[$part->content_location] = $url;
748        }
749      }
750    }
751  }
752
753  $_SESSION['compose']['forward_attachments'] = true;
754
755  return $cid_map;
756}
757
758
759function rcmail_write_inline_attachments(&$message)
760{
761  global $OUTPUT;
762
763  $cid_map = array();
764  foreach ((array)$message->mime_parts as $pid => $part) {
765    if (($part->content_id || $part->content_location) && $part->filename) {
766      if ($attachment = rcmail_save_attachment($message, $pid)) {
767        $_SESSION['compose']['attachments'][$attachment['id']] = $attachment;
768        $url = $OUTPUT->app->comm_path.'&_action=display-attachment&_file=rcmfile'.$attachment['id'];
769        if ($part->content_id)
770          $cid_map['cid:'.$part->content_id] = $url;
771        else
772          $cid_map[$part->content_location] = $url;
773      }
774    }
775  }
776
777  return $cid_map;
778}
779
780function rcmail_save_attachment(&$message, $pid)
781{
782  $part = $message->mime_parts[$pid];
783  $mem_limit = parse_bytes(ini_get('memory_limit'));
784  $curr_mem = function_exists('memory_get_usage') ? memory_get_usage() : 16*1024*1024; // safe value: 16MB
785  $data = $path = null;
786
787  // don't load too big attachments into memory
788  if ($mem_limit > 0 && $part->size > $mem_limit - $curr_mem) {
789    $rcmail = rcmail::get_instance();
790    $temp_dir = unslashify($rcmail->config->get('temp_dir'));
791    $path = tempnam($temp_dir, 'rcmAttmnt');
792    if ($fp = fopen($path, 'w')) {
793      $message->get_part_content($pid, $fp);
794      fclose($fp);
795    } else
796      return false;
797  } else {
798    $data = $message->get_part_content($pid);
799  }
800
801  $attachment = array(
802    'name' => $part->filename ? $part->filename : 'Part_'.$pid.'.'.$part->ctype_secondary,
803    'mimetype' => $part->ctype_primary . '/' . $part->ctype_secondary,
804    'content_id' => $part->content_id,
805    'data' => $data,
806    'path' => $path,
807    'size' => $path ? filesize($path) : strlen($data),
808  );
809
810  $attachment = rcmail::get_instance()->plugins->exec_hook('save_attachment', $attachment);
811
812  if ($attachment['status']) {
813    unset($attachment['data'], $attachment['status'], $attachment['content_id'], $attachment['abort']);
814    return $attachment;
815  } else if ($path) {
816    @unlink($path);
817  }
818 
819  return false;
820}
821
822function rcmail_save_image($path, $mimetype='')
823{
824  // handle attachments in memory
825  $data = file_get_contents($path);
826
827  $attachment = array(
828    'name' => rcmail_basename($path),
829    'mimetype' => $mimetype ? $mimetype : rc_mime_content_type($path, $name),
830    'data' => $data,
831    'size' => strlen($data),
832  );
833
834  $attachment = rcmail::get_instance()->plugins->exec_hook('save_attachment', $attachment);
835
836  if ($attachment['status']) {
837    unset($attachment['data'], $attachment['status'], $attachment['content_id'], $attachment['abort']);
838    return $attachment;
839  }
840 
841  return false;
842}
843
844function rcmail_basename($filename)
845{
846  // basename() is not unicode safe and locale dependent
847  if (stristr(PHP_OS, 'win') || stristr(PHP_OS, 'netware')) {
848    return preg_replace('/^.*[\\\\\\/]/', '', $filename);
849  } else {
850    return preg_replace('/^.*[\/]/', '', $filename);
851  }
852}
853
854function rcmail_compose_subject($attrib)
855{
856  global $MESSAGE, $compose_mode;
857 
858  list($form_start, $form_end) = get_form_tags($attrib);
859  unset($attrib['form']);
860 
861  $attrib['name'] = '_subject';
862  $attrib['spellcheck'] = 'true';
863  $textfield = new html_inputfield($attrib);
864
865  $subject = '';
866
867  // use subject from post
868  if (isset($_POST['_subject'])) {
869    $subject = get_input_value('_subject', RCUBE_INPUT_POST, TRUE);
870  }
871  // create a reply-subject
872  else if ($compose_mode == RCUBE_COMPOSE_REPLY) {
873    if (preg_match('/^re:/i', $MESSAGE->subject))
874      $subject = $MESSAGE->subject;
875    else
876      $subject = 'Re: '.$MESSAGE->subject;
877  }
878  // create a forward-subject
879  else if ($compose_mode == RCUBE_COMPOSE_FORWARD) {
880    if (preg_match('/^fwd:/i', $MESSAGE->subject))
881      $subject = $MESSAGE->subject;
882    else
883      $subject = 'Fwd: '.$MESSAGE->subject;
884  }
885  // creeate a draft-subject
886  else if ($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT) {
887    $subject = $MESSAGE->subject;
888  }
889  else if (!empty($_SESSION['compose']['param']['subject'])) {
890    $subject = $_SESSION['compose']['param']['subject'];
891  }
892 
893  $out = $form_start ? "$form_start\n" : '';
894  $out .= $textfield->show($subject);
895  $out .= $form_end ? "\n$form_end" : '';
896         
897  return $out;
898}
899
900
901function rcmail_compose_attachment_list($attrib)
902{
903  global $OUTPUT, $CONFIG;
904 
905  // add ID if not given
906  if (!$attrib['id'])
907    $attrib['id'] = 'rcmAttachmentList';
908 
909  $out = "\n";
910  $jslist = array();
911 
912  if (is_array($_SESSION['compose']['attachments']))
913  {
914    if ($attrib['deleteicon']) {
915      $button = html::img(array(
916        'src' => $CONFIG['skin_path'] . $attrib['deleteicon'],
917        'alt' => rcube_label('delete')
918      ));
919    }
920    else
921      $button = Q(rcube_label('delete'));
922
923    foreach ($_SESSION['compose']['attachments'] as $id => $a_prop)
924    {
925      if (empty($a_prop))
926        continue;
927     
928      $out .= html::tag('li', array('id' => 'rcmfile'.$id),
929        html::a(array(
930            'href' => "#delete",
931            'title' => rcube_label('delete'),
932            'onclick' => sprintf("return %s.command('remove-attachment','rcmfile%s', this)", JS_OBJECT_NAME, $id)),
933          $button) . Q($a_prop['name']));
934       
935        $jslist['rcmfile'.$id] = array('name' => $a_prop['name'], 'complete' => true, 'mimetype' => $a_prop['mimetype']);
936    }
937  }
938
939  if ($attrib['deleteicon'])
940    $_SESSION['compose']['deleteicon'] = $CONFIG['skin_path'] . $attrib['deleteicon'];
941  if ($attrib['cancelicon'])
942    $OUTPUT->set_env('cancelicon', $CONFIG['skin_path'] . $attrib['cancelicon']);
943  if ($attrib['loadingicon'])
944    $OUTPUT->set_env('loadingicon', $CONFIG['skin_path'] . $attrib['loadingicon']);
945
946  $OUTPUT->set_env('attachments', $jslist);
947  $OUTPUT->add_gui_object('attachmentlist', $attrib['id']);
948   
949  return html::tag('ul', $attrib, $out, html::$common_attrib);
950}
951
952
953function rcmail_compose_attachment_form($attrib)
954{
955  global $OUTPUT;
956
957  // add ID if not given
958  if (!$attrib['id'])
959    $attrib['id'] = 'rcmUploadbox';
960
961  // find max filesize value
962  $max_filesize = parse_bytes(ini_get('upload_max_filesize'));
963  $max_postsize = parse_bytes(ini_get('post_max_size'));
964  if ($max_postsize && $max_postsize < $max_filesize)
965    $max_filesize = $max_postsize;
966  $max_filesize = show_bytes($max_filesize);
967 
968  $button = new html_inputfield(array('type' => 'button'));
969 
970  $out = html::div($attrib,
971    $OUTPUT->form_tag(array('name' => 'form', 'method' => 'post', 'enctype' => 'multipart/form-data'),
972      html::div(null, rcmail_compose_attachment_field(array('size' => $attrib[attachmentfieldsize]))) .
973      html::div('hint', rcube_label(array('name' => 'maxuploadsize', 'vars' => array('size' => $max_filesize)))) .
974      html::div('buttons',
975        $button->show(rcube_label('close'), array('class' => 'button', 'onclick' => "$('#$attrib[id]').hide()")) . ' ' .
976        $button->show(rcube_label('upload'), array('class' => 'button mainaction', 'onclick' => JS_OBJECT_NAME . ".command('send-attachment', this.form)"))
977      )
978    )
979  );
980 
981  $OUTPUT->add_gui_object('uploadbox', $attrib['id']);
982  return $out;
983}
984
985
986function rcmail_compose_attachment_field($attrib)
987{
988  $attrib['type'] = 'file';
989  $attrib['name'] = '_attachments[]';
990  $field = new html_inputfield($attrib);
991  return $field->show();
992}
993
994
995function rcmail_priority_selector($attrib)
996{
997  global $MESSAGE;
998 
999  list($form_start, $form_end) = get_form_tags($attrib);
1000  unset($attrib['form']);
1001
1002  $attrib['name'] = '_priority';
1003  $selector = new html_select($attrib);
1004
1005  $selector->add(array(rcube_label('lowest'),
1006                       rcube_label('low'),
1007                       rcube_label('normal'),
1008                       rcube_label('high'),
1009                       rcube_label('highest')),
1010                 array(5, 4, 0, 2, 1));
1011
1012  if (isset($_POST['_priority']))
1013    $sel = $_POST['_priority'];
1014  else if (intval($MESSAGE->headers->priority) != 3)
1015    $sel = intval($MESSAGE->headers->priority);
1016  else
1017    $sel = 0;
1018
1019  $out = $form_start ? "$form_start\n" : '';
1020  $out .= $selector->show($sel);
1021  $out .= $form_end ? "\n$form_end" : '';
1022
1023  return $out;
1024}
1025
1026
1027function rcmail_receipt_checkbox($attrib)
1028{
1029  global $MESSAGE, $compose_mode;
1030
1031  list($form_start, $form_end) = get_form_tags($attrib);
1032  unset($attrib['form']);
1033
1034  if (!isset($attrib['id']))
1035    $attrib['id'] = 'receipt'; 
1036
1037  $attrib['name'] = '_receipt';
1038  $attrib['value'] = '1';
1039  $checkbox = new html_checkbox($attrib);
1040
1041  $out = $form_start ? "$form_start\n" : '';
1042  $out .= $checkbox->show(in_array($compose_mode, array(RCUBE_COMPOSE_DRAFT, RCUBE_COMPOSE_EDIT))
1043        && $MESSAGE->headers->mdn_to ? 1 : 0);
1044  $out .= $form_end ? "\n$form_end" : '';
1045
1046  return $out;
1047}
1048
1049
1050function rcmail_editor_selector($attrib)
1051{
1052  global $CONFIG, $MESSAGE, $compose_mode;
1053
1054  // determine whether HTML or plain text should be checked
1055  if ($compose_mode)
1056    $useHtml = (($CONFIG['htmleditor'] || $compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT)
1057        && $MESSAGE->has_html_part());
1058  else
1059    $useHtml = $CONFIG['htmleditor'] ? true : false;
1060
1061  if (empty($attrib['editorid']))
1062    $attrib['editorid'] = 'rcmComposeBody';
1063
1064  if (empty($attrib['name']))
1065    $attrib['name'] = 'editorSelect';
1066
1067  $attrib['onchange'] = "return rcmail_toggle_editor(this, '".$attrib['editorid']."', '_is_html')";
1068
1069  $select = new html_select($attrib);
1070
1071  $select->add(Q(rcube_label('htmltoggle')), 'html');
1072  $select->add(Q(rcube_label('plaintoggle')), 'plain');
1073
1074  return $select->show($useHtml ? 'html' : 'plain');
1075
1076  foreach ($choices as $value => $text)
1077  {
1078    $attrib['id'] = '_' . $value;
1079    $attrib['value'] = $value;
1080    $selector .= $radio->show($chosenvalue, $attrib) . html::label($attrib['id'], Q(rcube_label($text)));
1081  }
1082
1083  return $selector;
1084}
1085
1086
1087function rcmail_store_target_selection($attrib)
1088{
1089  $attrib['name'] = '_store_target';
1090  $select = rcmail_mailbox_select(array_merge($attrib, array('noselection' => '- '.rcube_label('dontsave').' -')));
1091  return $select->show($_SESSION['compose']['param']['sent_mbox'], $attrib);
1092}
1093
1094
1095function get_form_tags($attrib)
1096{
1097  global $RCMAIL, $MESSAGE_FORM;
1098
1099  $form_start = '';
1100  if (!$MESSAGE_FORM)
1101  {
1102    $hiddenfields = new html_hiddenfield(array('name' => '_task', 'value' => $RCMAIL->task));
1103    $hiddenfields->add(array('name' => '_action', 'value' => 'send'));
1104
1105    $form_start = empty($attrib['form']) ? $RCMAIL->output->form_tag(array('name' => "form", 'method' => "post")) : '';
1106    $form_start .= $hiddenfields->show();
1107  }
1108   
1109  $form_end = ($MESSAGE_FORM && !strlen($attrib['form'])) ? '</form>' : '';
1110  $form_name = !empty($attrib['form']) ? $attrib['form'] : 'form';
1111 
1112  if (!$MESSAGE_FORM)
1113    $RCMAIL->output->add_gui_object('messageform', $form_name);
1114 
1115  $MESSAGE_FORM = $form_name;
1116
1117  return array($form_start, $form_end);
1118}
1119
1120
1121// register UI objects
1122$OUTPUT->add_handlers(array(
1123  'composeheaders' => 'rcmail_compose_headers',
1124  'composesubject' => 'rcmail_compose_subject',
1125  'composebody' => 'rcmail_compose_body',
1126  'composeattachmentlist' => 'rcmail_compose_attachment_list',
1127  'composeattachmentform' => 'rcmail_compose_attachment_form',
1128  'composeattachment' => 'rcmail_compose_attachment_field',
1129  'priorityselector' => 'rcmail_priority_selector',
1130  'editorselector' => 'rcmail_editor_selector',
1131  'receiptcheckbox' => 'rcmail_receipt_checkbox',
1132  'storetarget' => 'rcmail_store_target_selection',
1133));
1134
1135$OUTPUT->send('compose');
1136
1137
Note: See TracBrowser for help on using the repository browser.