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

Last change on this file since 201 was 201, checked in by vengadrew, 7 years ago

Corrected issue forwarding attachments that use content-description rather than filename or name disposition headers

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.1 KB
RevLine 
[3]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, RoundCube Dev. - Switzerland                      |
[8]9 | Licensed under the GNU GPL                                            |
[3]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
23require_once('Mail/mimeDecode.php');
24
25
26$MESSAGE_FORM = NULL;
27$REPLY_MESSAGE = NULL;
28$FORWARD_MESSAGE = NULL;
29
30
31if (!is_array($_SESSION['compose']))
32  $_SESSION['compose'] = array('id' => uniqid(rand()));
33
34
[76]35// add some labels to client
[92]36rcube_add_label('nosubject', 'norecipientwarning', 'nosubjectwarning', 'nobodywarning', 'sendingmessage', 'notsentwarning');
[76]37
38
[3]39if ($_GET['_reply_uid'] || $_GET['_forward_uid'])
40  {
41  $msg_uid = $_GET['_reply_uid'] ? $_GET['_reply_uid'] : $_GET['_forward_uid'];
42
43  // similar as in program/steps/mail/show.inc
44  $MESSAGE = array();
45  $MESSAGE['headers'] = $IMAP->get_headers($msg_uid);
46 
47  $MESSAGE['source'] = rcmail_message_source($msg_uid);
48 
49  $mmd = new Mail_mimeDecode($MESSAGE['source']);
50  $MESSAGE['structure'] = $mmd->decode(array('include_bodies' => TRUE,
51                                             'decode_headers' => TRUE,
52                                             'decode_bodies' => FALSE));
[81]53
[3]54  $MESSAGE['subject'] = $IMAP->decode_header($MESSAGE['headers']->subject);
55  $MESSAGE['parts'] = $mmd->getMimeNumbers($MESSAGE['structure']);
56 
57  if ($_GET['_reply_uid'])
58    {
[81]59    $REPLY_MESSAGE = &$MESSAGE;
[3]60    $_SESSION['compose']['reply_uid'] = $_GET['_reply_uid'];
61    $_SESSION['compose']['reply_msgid'] = $REPLY_MESSAGE['headers']->messageID;
[151]62    $_SESSION['compose']['references']  = $REPLY_MESSAGE['headers']->reference;
[140]63    $_SESSION['compose']['references'] .= !empty($REPLY_MESSAGE['headers']->reference) ? ' ' : '';
64    $_SESSION['compose']['references'] .= $REPLY_MESSAGE['headers']->messageID;
[81]65
66    if ($_GET['_all'])
67      $REPLY_MESSAGE['reply_all'] = 1;
[3]68    }
69  else
70    {
71    $FORWARD_MESSAGE = $MESSAGE;
72    $_SESSION['compose']['forward_uid'] = $_GET['_forward_uid'];
73    }
74  }
75
76
77
78/****** compose mode functions ********/
79
80
81function rcmail_compose_headers($attrib)
82  {
83  global $IMAP, $REPLY_MESSAGE, $DB;
[81]84  static $sa_recipients = array();
[3]85
86  list($form_start, $form_end) = get_form_tags($attrib);
87 
88  $out = '';
89  $part = strtolower($attrib['part']);
90 
91  switch ($part)
92    {
93    case 'from':
[88]94      return rcmail_compose_header_from($attrib);
[3]95
96    case 'to':
97      $fname = '_to';
98      $header = 'to';
[88]99
[3]100      // we have contact id's as get parameters
[180]101      if (!empty($_GET['_to']) && preg_match('/^[0-9]+(,[0-9]+)*$/', $_GET['_to']))
[3]102        {
103        $a_recipients = array();
[58]104        $sql_result = $DB->query("SELECT name, email
[88]105                                  FROM ".get_table_name('contacts')."
106                                  WHERE user_id=?
107                                  AND    del<>1
[58]108                                  AND    contact_id IN (".$_GET['_to'].")",
109                                  $_SESSION['user_id']);
[3]110                                         
111        while ($sql_arr = $DB->fetch_assoc($sql_result))
112          $a_recipients[] = format_email_recipient($sql_arr['email'], $sql_arr['name']);
113         
114        if (sizeof($a_recipients))
115          $fvalue = join(', ', $a_recipients);
116        }
[20]117      else if (!empty($_GET['_to']))
[3]118        $fvalue = $_GET['_to'];
119       
120    case 'cc':
121      if (!$fname)
122        {
123        $fname = '_cc';
[81]124        $header = 'cc';
[3]125        }
126    case 'bcc':
127      if (!$fname)
128        $fname = '_bcc';
129       
[74]130      $allow_attrib = array('id', 'class', 'style', 'cols', 'rows', 'wrap', 'tabindex');
[3]131      $field_type = 'textarea';           
132      break;
133
134    case 'replyto':
135    case 'reply-to':
136      $fname = '_replyto';
[74]137      $allow_attrib = array('id', 'class', 'style', 'size', 'tabindex');
[3]138      $field_type = 'textfield';
139      break;
140   
141    }
[81]142
[3]143   
[20]144  if ($fname && !empty($_POST[$fname]))
[162]145    $fvalue = get_input_value($fname, RCUBE_INPUT_POST, TRUE);
[3]146  else if ($header && is_object($REPLY_MESSAGE['headers']))
147    {
148    // get recipent address(es) out of the message headers
149    if ($header=='to' && $REPLY_MESSAGE['headers']->replyto)
150      $fvalue = $IMAP->decode_header($REPLY_MESSAGE['headers']->replyto);
[130]151
[3]152    else if ($header=='to' && $REPLY_MESSAGE['headers']->from)
153      $fvalue = $IMAP->decode_header($REPLY_MESSAGE['headers']->from);
[130]154
[81]155    // add recipent of original message if reply to all
156    else if ($header=='cc' && $REPLY_MESSAGE['reply_all'])
157      {
158      if ($IMAP->decode_header($REPLY_MESSAGE['headers']->to))
159        $fvalue .= $IMAP->decode_header($REPLY_MESSAGE['headers']->to);
160
161      if ($IMAP->decode_header($REPLY_MESSAGE['headers']->cc))
162        {
163        if($fvalue)
164          $fvalue .= ', ';
165
166        $fvalue .= $IMAP->decode_header($REPLY_MESSAGE['headers']->cc);
167        }
168      }
169
[3]170    // split recipients and put them back together in a unique way
[81]171    if (!empty($fvalue))
172      {
173      $to_addresses = $IMAP->decode_address_list($fvalue);
174      $fvalue = '';
175      foreach ($to_addresses as $addr_part)
176        {
[130]177        if (!in_array($addr_part['mailto'], $sa_recipients) && (!$REPLY_MESSAGE['FROM'] || !in_array($addr_part['mailto'], $REPLY_MESSAGE['FROM'])))
[81]178          {
179          $fvalue .= (strlen($fvalue) ? ', ':'').$addr_part['string'];
180          $sa_recipients[] = $addr_part['mailto'];
181          }
182        }
183      }
[3]184    }
[81]185
[3]186       
187  if ($fname && $field_type)
188    {
189    // pass the following attributes to the form class
190    $field_attrib = array('name' => $fname);
191    foreach ($attrib as $attr => $value)
192      if (in_array($attr, $allow_attrib))
193        $field_attrib[$attr] = $value;
194
195    // create teaxtarea object
196    $input = new $field_type($field_attrib);
197    $out = $input->show($fvalue);   
198    }
199 
200  if ($form_start)
201    $out = $form_start.$out;
202 
203  return $out; 
204  }
205
206
[88]207
208function rcmail_compose_header_from($attrib)
[3]209  {
[88]210  global $IMAP, $REPLY_MESSAGE, $DB, $OUTPUT, $JS_OBJECT_NAME;
211   
212  // pass the following attributes to the form class
213  $field_attrib = array('name' => '_from');
214  foreach ($attrib as $attr => $value)
215    if (in_array($attr, array('id', 'class', 'style', 'size', 'tabindex')))
216      $field_attrib[$attr] = $value;
[3]217
[88]218  // extract all recipients of the reply-message
219  $a_recipients = array();
220  if ($REPLY_MESSAGE && is_object($REPLY_MESSAGE['headers']))
221    {
[130]222    $REPLY_MESSAGE['FROM'] = array();
223
[88]224    $a_to = $IMAP->decode_address_list($REPLY_MESSAGE['headers']->to);       
225    foreach ($a_to as $addr)
226      {
227      if (!empty($addr['mailto']))
228        $a_recipients[] = $addr['mailto'];
229      }
[3]230
[88]231    if (!empty($REPLY_MESSAGE['headers']->cc))
232      {
233      $a_cc = $IMAP->decode_address_list($REPLY_MESSAGE['headers']->cc);
234      foreach ($a_cc as $addr)
235        {
236        if (!empty($addr['mailto']))
237          $a_recipients[] = $addr['mailto'];
238        }
239      }
240    }
[3]241
[88]242  // get this user's identities
243  $sql_result = $DB->query("SELECT identity_id, name, email, signature
244                            FROM   ".get_table_name('identities')."
245                            WHERE user_id=?
246                            AND    del<>1
247                            ORDER BY ".$DB->quoteIdentifier('standard')." DESC, name ASC",
248                           $_SESSION['user_id']);
249                                   
250  if ($DB->num_rows($sql_result))
251    {
252    $from_id = 0;
253    $a_signatures = array();
254   
255    $field_attrib['onchange'] = "$JS_OBJECT_NAME.change_identity(this)";
256    $select_from = new select($field_attrib);
257   
258    while ($sql_arr = $DB->fetch_assoc($sql_result))
259      {
260      $select_from->add(format_email_recipient($sql_arr['email'], $sql_arr['name']), $sql_arr['identity_id']);
[3]261
[88]262      // add signature to array
263      if (!empty($sql_arr['signature']))
264        $a_signatures[$sql_arr['identity_id']] = $sql_arr['signature'];
265     
266      // set identity if it's one of the reply-message recipients
267      if (in_array($sql_arr['email'], $a_recipients))
268        $from_id = $sql_arr['identity_id'];
[130]269       
270      if ($REPLY_MESSAGE && is_array($REPLY_MESSAGE['FROM']))
271        $REPLY_MESSAGE['FROM'][] = $sql_arr['email'];
[88]272      }
[3]273
[88]274    // overwrite identity selection with post parameter
275    if (isset($_POST['_from']))
276      $from_id = $_POST['_from'];
[3]277
[88]278    $out = $select_from->show($from_id);
279   
[3]280
[88]281    // add signatures to client
282    $OUTPUT->add_script(sprintf("%s.set_env('signatures', %s);", $JS_OBJECT_NAME, array2js($a_signatures))); 
283    }
284  else
285    {
286    $input_from = new textfield($field_attrib);
287    $out = $input_from->show($_POST['_from']);
288    }
289   
290  if ($form_start)
291    $out = $form_start.$out;
[3]292
[88]293  return $out;
[3]294  }
295
[88]296 
[3]297
298function rcmail_compose_body($attrib)
299  {
300  global $CONFIG, $REPLY_MESSAGE, $FORWARD_MESSAGE;
301 
302  list($form_start, $form_end) = get_form_tags($attrib);
303  unset($attrib['form']);
304 
305  $attrib['name'] = '_message';
306  $textarea = new textarea($attrib);
307
308  $body = '';
309 
310  // use posted message body
[20]311  if (!empty($_POST['_message']))
[159]312    $body = get_input_value('_message', RCUBE_INPUT_POST, TRUE);
[3]313   
314  // compose reply-body
315  else if (is_array($REPLY_MESSAGE['parts']))
316    {
317    $body = rcmail_first_text_part($REPLY_MESSAGE['parts']);
318    if (strlen($body))
319      $body = rcmail_create_reply_body($body);
320    }
321
322  // forward message body inline
323  else if (is_array($FORWARD_MESSAGE['parts']))
324    {
325    $body = rcmail_first_text_part($FORWARD_MESSAGE['parts']);
326    if (strlen($body))
327      $body = rcmail_create_forward_body($body);
328    }
329 
330  $out = $form_start ? "$form_start\n" : '';
331  $out .= $textarea->show($body);
332  $out .= $form_end ? "\n$form_end" : '';
333         
334  return $out;
335  }
336
337
338function rcmail_create_reply_body($body)
339  {
340  global $IMAP, $REPLY_MESSAGE;
341
342  // soft-wrap message first
343  $body = wordwrap($body, 75);
344 
345  // split body into single lines
346  $a_lines = preg_split('/\r?\n/', $body);
347 
348  // add > to each line
349  for($n=0; $n<sizeof($a_lines); $n++)
350    {
351    if (strpos($a_lines[$n], '>')===0)
352      $a_lines[$n] = '>'.$a_lines[$n];
353    else
354      $a_lines[$n] = '> '.$a_lines[$n];
355    }
356 
357  $body = join("\n", $a_lines);
358
359  // add title line
360  $pefix = sprintf("\n\n\nOn %s, %s wrote:\n",
361           $REPLY_MESSAGE['headers']->date,
362           $IMAP->decode_header($REPLY_MESSAGE['headers']->from));
[88]363           
[3]364
[88]365  // try to remove the signature
[91]366  if ($sp = strrpos($body, '-- '))
[88]367    {
368    if ($body{$sp+3}==' ' || $body{$sp+3}=="\n" || $body{$sp+3}=="\r")
369      $body = substr($body, 0, $sp-1);
370    }
371
[3]372  return $pefix.$body;
373  }
374
375
376function rcmail_create_forward_body($body)
377  {
378  global $IMAP, $FORWARD_MESSAGE;
379
380  // soft-wrap message first
381  $body = wordwrap($body, 80);
382 
383  $prefix = sprintf("\n\n\n-------- Original Message --------\nSubject: %s\nDate: %s\nFrom: %s\nTo: %s\n\n",
384                   $FORWARD_MESSAGE['subject'],
385                   $FORWARD_MESSAGE['headers']->date,
386                   $IMAP->decode_header($FORWARD_MESSAGE['headers']->from),
387                   $IMAP->decode_header($FORWARD_MESSAGE['headers']->to));
388
[20]389  // add attachments
390  if (!isset($_SESSION['compose']['forward_attachments']) && is_array($FORWARD_MESSAGE['parts']) && sizeof($FORWARD_MESSAGE['parts'])>1)
391    {
392    $temp_dir = rcmail_create_compose_tempdir();
393
394    if (!is_array($_SESSION['compose']['attachments']))
395      $_SESSION['compose']['attachments'] = array();
396 
397    foreach ($FORWARD_MESSAGE['parts'] as $part)
398      {
[189]399      if ($part->disposition=='attachment' || $part->disposition=='inline' || $part->headers['content-id'] ||
400               (empty($part->disposition) && ($part->d_parameters['filename'] || $part->ctype_parameters['name'])))
401        {
402        $tmp_path = tempnam($temp_dir, 'rcmAttmnt');
403        if ($fp = fopen($tmp_path, 'w'))
404          {
405          fwrite($fp, $IMAP->mime_decode($part->body, $part->headers['content-transfer-encoding']));
406          fclose($fp);
[20]407
[189]408          if ($part->d_parameters['filename'])
409            $_SESSION['compose']['attachments'][] = array('name' => $part->d_parameters['filename'],
410                                                          'mimetype' => $part->ctype_primary . '/' . $part->ctype_secondary,
411                                                          'path' => $tmp_path);
412                                   
413          else if ($part->ctype_parameters['name'])
414            $_SESSION['compose']['attachments'][] = array('name' => $part->ctype_parameters['name'],
415                                                          'mimetype' => $part->ctype_primary . '/' . $part->ctype_secondary,
416                                                          'path' => $tmp_path);
[201]417                                                         
418          else if ($part->headers['content-description'])
419            $_SESSION['compose']['attachments'][] = array('name' => $part->headers['content-description'],
420                                                          'mimetype' => $part->ctype_primary . '/' . $part->ctype_secondary,
421                                                          'path' => $tmp_path);
[189]422          }
423        }
[20]424      }
425
426    $_SESSION['compose']['forward_attachments'] = TRUE;
427    }
428
[3]429  return $prefix.$body;
430  }
431
432
433
434function rcmail_compose_subject($attrib)
435  {
436  global $CONFIG, $REPLY_MESSAGE, $FORWARD_MESSAGE;
437 
438  list($form_start, $form_end) = get_form_tags($attrib);
439  unset($attrib['form']);
440 
441  $attrib['name'] = '_subject';
442  $textfield = new textfield($attrib);
443
444  $subject = '';
445
446  // use subject from post
[20]447  if (isset($_POST['_subject']))
[162]448    $subject = get_input_value('_subject', RCUBE_INPUT_POST, TRUE);
[3]449   
450  // create a reply-subject
451  else if (isset($REPLY_MESSAGE['subject']))
[25]452    {
[26]453    if (eregi('^re:', $REPLY_MESSAGE['subject']))
[25]454      $subject = $REPLY_MESSAGE['subject'];
455    else
456      $subject = 'Re: '.$REPLY_MESSAGE['subject'];
457    }
[3]458
459  // create a forward-subject
460  else if (isset($FORWARD_MESSAGE['subject']))
[26]461    {
462    if (eregi('^fwd:', $REPLY_MESSAGE['subject']))
463      $subject = $FORWARD_MESSAGE['subject'];
464    else
465      $subject = 'Fwd: '.$FORWARD_MESSAGE['subject'];
466    }
[3]467
468 
469  $out = $form_start ? "$form_start\n" : '';
470  $out .= $textfield->show($subject);
471  $out .= $form_end ? "\n$form_end" : '';
472         
473  return $out;
474  }
475
476
477function rcmail_compose_attachment_list($attrib)
478  {
479  global $OUTPUT, $JS_OBJECT_NAME;
480 
481  // add ID if not given
482  if (!$attrib['id'])
483    $attrib['id'] = 'rcmAttachmentList';
484 
485  // allow the following attributes to be added to the <ul> tag
486  $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style'));
487 
488  $out = '<ul'. $attrib_str . ">\n";
489 
490  if (is_array($_SESSION['compose']['attachments']))
491    {
492    foreach ($_SESSION['compose']['attachments'] as $i => $a_prop)
493      $out .= sprintf("<li>%s</li>\n", $a_prop['name']);
494    }
495
496  $OUTPUT->add_script(sprintf("%s.gui_object('attachmentlist', '%s');", $JS_OBJECT_NAME, $attrib['id'])); 
497   
498  $out .= '</ul>';
499  return $out;
500  }
501
502
503
504function rcmail_compose_attachment_form($attrib)
505  {
506  global $OUTPUT, $JS_OBJECT_NAME, $SESS_HIDDEN_FIELD;
507
508  // add ID if not given
509  if (!$attrib['id'])
510    $attrib['id'] = 'rcmUploadbox';
511 
512  // allow the following attributes to be added to the <div> tag
513  $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style'));
514  $input_field = rcmail_compose_attachment_field(array());
515  $label_send = rcube_label('upload');
516  $label_close = rcube_label('close');
517 
518  $out = <<<EOF
519<div$attrib_str>
520<form action="./" method="post" enctype="multipart/form-data">
521$SESS_HIDDEN_FIELD
522$input_field<br />
523<input type="button" value="$label_close" class="button" onclick="document.getElementById('$attrib[id]').style.visibility='hidden'" />
524<input type="button" value="$label_send" class="button" onclick="$JS_OBJECT_NAME.command('send-attachment', this.form)" />
525</form>
526</div>
527EOF;
528
529 
530  $OUTPUT->add_script(sprintf("%s.gui_object('uploadbox', '%s');", $JS_OBJECT_NAME, $attrib['id'])); 
531  return $out;
532  }
533
534
535function rcmail_compose_attachment_field($attrib)
536  {
537  // allow the following attributes to be added to the <input> tag
538  $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style', 'size'));
539 
540  $out = '<input type="file" name="_attachments[]"'. $attrib_str . " />";
541  return $out;
542  }
543
544
545function rcmail_priority_selector($attrib)
546  {
547  list($form_start, $form_end) = get_form_tags($attrib);
548  unset($attrib['form']);
549 
550  $attrib['name'] = '_priority';
551  $selector = new select($attrib);
552
553  $selector->add(array(rcube_label('lowest'),
554                       rcube_label('low'),
555                       rcube_label('normal'),
556                       rcube_label('high'),
557                       rcube_label('highest')),
[49]558                 array(5, 4, 0, 2, 1));
[3]559                 
[20]560  $sel = isset($_POST['_priority']) ? $_POST['_priority'] : 0;
[3]561
562  $out = $form_start ? "$form_start\n" : '';
563  $out .= $selector->show($sel);
564  $out .= $form_end ? "\n$form_end" : '';
565         
566  return $out;
567  }
568
569
570function get_form_tags($attrib)
571  {
572  global $CONFIG, $OUTPUT, $JS_OBJECT_NAME, $MESSAGE_FORM, $SESS_HIDDEN_FIELD; 
573
574  $form_start = '';
575  if (!strlen($MESSAGE_FORM))
576    {
577    $hiddenfields = new hiddenfield(array('name' => '_task', 'value' => $GLOBALS['_task']));
578    $hiddenfields->add(array('name' => '_action', 'value' => 'send'));
579   
[20]580    $form_start = empty($attrib['form']) ? '<form name="form" action="./" method="post">' : '';
[3]581    $form_start .= "\n$SESS_HIDDEN_FIELD\n";
582    $form_start .= $hiddenfields->show();
583    }
584   
585  $form_end = (strlen($MESSAGE_FORM) && !strlen($attrib['form'])) ? '</form>' : '';
[20]586  $form_name = !empty($attrib['form']) ? $attrib['form'] : 'form';
[3]587 
588  if (!strlen($MESSAGE_FORM))
589    $OUTPUT->add_script("$JS_OBJECT_NAME.gui_object('messageform', '$form_name');");
590 
591  $MESSAGE_FORM = $form_name;
592
593  return array($form_start, $form_end); 
594  }
595
596
597function format_email_recipient($email, $name='')
598  {
599  if ($name && $name != $email)
600    return sprintf('%s <%s>', strpos($name, ",") ? '"'.$name.'"' : $name, $email);
601  else
602    return $email;
603  }
604
605
[83]606function rcmail_charset_pulldown($selected='ISO-8859-1')
607  {
608  $select = new select();
609 
610 
611  return $select->show($selected);
612  }
613
614
[3]615/****** get contacts for this user and add them to client scripts ********/
616
[58]617$sql_result = $DB->query("SELECT name, email
618                          FROM ".get_table_name('contacts')." WHERE  user_id=?
[88]619                          AND  del<>1",$_SESSION['user_id']);
[3]620                                   
621if ($DB->num_rows($sql_result))
622  {       
623  $a_contacts = array();
624  while ($sql_arr = $DB->fetch_assoc($sql_result))
625    if ($sql_arr['email'])
626      $a_contacts[] = format_email_recipient($sql_arr['email'], rep_specialchars_output($sql_arr['name'], 'js'));
627 
628  $OUTPUT->add_script(sprintf("$JS_OBJECT_NAME.set_env('contacts', %s);", array2js($a_contacts)));
629  }
630
631
632
633
634parse_template('compose');
635?>
Note: See TracBrowser for help on using the repository browser.