source: github/program/steps/mail/compose.inc @ 2f746dcd

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