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

HEADcourier-fixdev-browser-capabilitiespdorelease-0.6release-0.7release-0.8
Last change on this file since 6204390 was 6204390, checked in by thomascube <thomas@…>, 7 years ago

Applied patch for requesting receipts by Salvatore Ansani

  • Property mode set to 100644
File size: 19.5 KB
Line 
1<?php
2
3/*
4 +-----------------------------------------------------------------------+
5 | program/steps/mail/compose.inc                                        |
6 |                                                                       |
7 | This file is part of the RoundCube Webmail client                     |
8 | Copyright (C) 2005, 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
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
35// add some labels to client
36rcube_add_label('nosubject', 'norecipientwarning', 'nosubjectwarning', 'nobodywarning', 'sendingmessage', 'notsentwarning');
37
38
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));
53
54  $MESSAGE['subject'] = $IMAP->decode_header($MESSAGE['headers']->subject);
55  $MESSAGE['parts'] = $mmd->getMimeNumbers($MESSAGE['structure']);
56 
57  if ($_GET['_reply_uid'])
58    {
59    $REPLY_MESSAGE = &$MESSAGE;
60    $_SESSION['compose']['reply_uid'] = $_GET['_reply_uid'];
61    $_SESSION['compose']['reply_msgid'] = $REPLY_MESSAGE['headers']->messageID;
62    $_SESSION['compose']['references']  = $REPLY_MESSAGE['headers']->reference;
63    $_SESSION['compose']['references'] .= !empty($REPLY_MESSAGE['headers']->reference) ? ' ' : '';
64    $_SESSION['compose']['references'] .= $REPLY_MESSAGE['headers']->messageID;
65
66    if ($_GET['_all'])
67      $REPLY_MESSAGE['reply_all'] = 1;
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;
84  static $sa_recipients = array();
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':
94      return rcmail_compose_header_from($attrib);
95
96    case 'to':
97      $fname = '_to';
98      $header = 'to';
99
100      // we have contact id's as get parameters
101      if (!empty($_GET['_to']) && preg_match('/^[0-9]+(,[0-9]+)*$/', $_GET['_to']))
102        {
103        $a_recipients = array();
104        $sql_result = $DB->query("SELECT name, email
105                                  FROM ".get_table_name('contacts')."
106                                  WHERE user_id=?
107                                  AND    del<>1
108                                  AND    contact_id IN (".$_GET['_to'].")",
109                                  $_SESSION['user_id']);
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        }
117      else if (!empty($_GET['_to']))
118        $fvalue = $_GET['_to'];
119       
120    case 'cc':
121      if (!$fname)
122        {
123        $fname = '_cc';
124        $header = 'cc';
125        }
126    case 'bcc':
127      if (!$fname)
128        $fname = '_bcc';
129       
130      $allow_attrib = array('id', 'class', 'style', 'cols', 'rows', 'wrap', 'tabindex');
131      $field_type = 'textarea';           
132      break;
133
134    case 'replyto':
135    case 'reply-to':
136      $fname = '_replyto';
137      $allow_attrib = array('id', 'class', 'style', 'size', 'tabindex');
138      $field_type = 'textfield';
139      break;
140   
141    }
142
143   
144  if ($fname && !empty($_POST[$fname]))
145    $fvalue = get_input_value($fname, RCUBE_INPUT_POST, TRUE);
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);
151
152    else if ($header=='to' && $REPLY_MESSAGE['headers']->from)
153      $fvalue = $IMAP->decode_header($REPLY_MESSAGE['headers']->from);
154
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
170    // split recipients and put them back together in a unique way
171    if (!empty($fvalue))
172      {
173      $to_addresses = $IMAP->decode_address_list($fvalue);
174      $fvalue = '';
175      foreach ($to_addresses as $addr_part)
176        {
177        if (!in_array($addr_part['mailto'], $sa_recipients) && (!$REPLY_MESSAGE['FROM'] || !in_array($addr_part['mailto'], $REPLY_MESSAGE['FROM'])))
178          {
179          $fvalue .= (strlen($fvalue) ? ', ':'').$addr_part['string'];
180          $sa_recipients[] = $addr_part['mailto'];
181          }
182        }
183      }
184    }
185
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
207
208function rcmail_compose_header_from($attrib)
209  {
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;
217
218  // extract all recipients of the reply-message
219  $a_recipients = array();
220  if ($REPLY_MESSAGE && is_object($REPLY_MESSAGE['headers']))
221    {
222    $REPLY_MESSAGE['FROM'] = array();
223
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      }
230
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    }
241
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']);
261
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'];
269       
270      if ($REPLY_MESSAGE && is_array($REPLY_MESSAGE['FROM']))
271        $REPLY_MESSAGE['FROM'][] = $sql_arr['email'];
272      }
273
274    // overwrite identity selection with post parameter
275    if (isset($_POST['_from']))
276      $from_id = $_POST['_from'];
277
278    $out = $select_from->show($from_id);
279   
280
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;
292
293  return $out;
294  }
295
296 
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
311  if (!empty($_POST['_message']))
312    $body = get_input_value('_message', RCUBE_INPUT_POST, TRUE);
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));
363           
364
365  // try to remove the signature
366  if ($sp = strrpos($body, '-- '))
367    {
368    if ($body{$sp+3}==' ' || $body{$sp+3}=="\n" || $body{$sp+3}=="\r")
369      $body = substr($body, 0, $sp-1);
370    }
371
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
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      {
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);
407
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);
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);
422          }
423        }
424      }
425
426    $_SESSION['compose']['forward_attachments'] = TRUE;
427    }
428
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
447  if (isset($_POST['_subject']))
448    $subject = get_input_value('_subject', RCUBE_INPUT_POST, TRUE);
449   
450  // create a reply-subject
451  else if (isset($REPLY_MESSAGE['subject']))
452    {
453    if (eregi('^re:', $REPLY_MESSAGE['subject']))
454      $subject = $REPLY_MESSAGE['subject'];
455    else
456      $subject = 'Re: '.$REPLY_MESSAGE['subject'];
457    }
458
459  // create a forward-subject
460  else if (isset($FORWARD_MESSAGE['subject']))
461    {
462    if (eregi('^fwd:', $REPLY_MESSAGE['subject']))
463      $subject = $FORWARD_MESSAGE['subject'];
464    else
465      $subject = 'Fwd: '.$FORWARD_MESSAGE['subject'];
466    }
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')),
558                 array(5, 4, 0, 2, 1));
559                 
560  $sel = isset($_POST['_priority']) ? $_POST['_priority'] : 0;
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 rcmail_receipt_checkbox($attrib)
571  {
572  list($form_start, $form_end) = get_form_tags($attrib);
573  unset($attrib['form']);
574
575  $attrib['name'] = '_receipt';
576  $checkbox = new checkbox(array('name' => '_receipt', 'id' => 'receipt', 'value' => 1));
577
578  $out = $form_start ? "$form_start\n" : '';
579  $out .= $checkbox->show(0);
580  $out .= $form_end ? "\n$form_end" : '';
581
582  return $out;
583  }
584
585
586function get_form_tags($attrib)
587  {
588  global $CONFIG, $OUTPUT, $JS_OBJECT_NAME, $MESSAGE_FORM, $SESS_HIDDEN_FIELD; 
589
590  $form_start = '';
591  if (!strlen($MESSAGE_FORM))
592    {
593    $hiddenfields = new hiddenfield(array('name' => '_task', 'value' => $GLOBALS['_task']));
594    $hiddenfields->add(array('name' => '_action', 'value' => 'send'));
595   
596    $form_start = empty($attrib['form']) ? '<form name="form" action="./" method="post">' : '';
597    $form_start .= "\n$SESS_HIDDEN_FIELD\n";
598    $form_start .= $hiddenfields->show();
599    }
600   
601  $form_end = (strlen($MESSAGE_FORM) && !strlen($attrib['form'])) ? '</form>' : '';
602  $form_name = !empty($attrib['form']) ? $attrib['form'] : 'form';
603 
604  if (!strlen($MESSAGE_FORM))
605    $OUTPUT->add_script("$JS_OBJECT_NAME.gui_object('messageform', '$form_name');");
606 
607  $MESSAGE_FORM = $form_name;
608
609  return array($form_start, $form_end); 
610  }
611
612
613function format_email_recipient($email, $name='')
614  {
615  if ($name && $name != $email)
616    return sprintf('%s <%s>', strpos($name, ",") ? '"'.$name.'"' : $name, $email);
617  else
618    return $email;
619  }
620
621
622function rcmail_charset_pulldown($selected='ISO-8859-1')
623  {
624  $select = new select();
625 
626 
627  return $select->show($selected);
628  }
629
630
631/****** get contacts for this user and add them to client scripts ********/
632
633$sql_result = $DB->query("SELECT name, email
634                          FROM ".get_table_name('contacts')." WHERE  user_id=?
635                          AND  del<>1",$_SESSION['user_id']);
636                                   
637if ($DB->num_rows($sql_result))
638  {       
639  $a_contacts = array();
640  while ($sql_arr = $DB->fetch_assoc($sql_result))
641    if ($sql_arr['email'])
642      $a_contacts[] = format_email_recipient($sql_arr['email'], rep_specialchars_output($sql_arr['name'], 'js'));
643 
644  $OUTPUT->add_script(sprintf("$JS_OBJECT_NAME.set_env('contacts', %s);", array2js($a_contacts)));
645  }
646
647
648
649
650parse_template('compose');
651?>
Note: See TracBrowser for help on using the repository browser.