source: github/program/steps/mail/compose.inc @ 01c86f2

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

Minor bugfixes

  • Property mode set to 100644
File size: 18.1 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]+,?)+$/', $_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')
400        continue;
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        $_SESSION['compose']['attachments'][] = array('name' => $part->d_parameters['filename'],
409                                                      'mimetype' => $part->ctype_primary . '/' . $part->ctype_secondary,
410                                                      'path' => $tmp_path);
411        }
412      }
413
414    $_SESSION['compose']['forward_attachments'] = TRUE;
415    }
416
417  return $prefix.$body;
418  }
419
420
421
422function rcmail_compose_subject($attrib)
423  {
424  global $CONFIG, $REPLY_MESSAGE, $FORWARD_MESSAGE;
425 
426  list($form_start, $form_end) = get_form_tags($attrib);
427  unset($attrib['form']);
428 
429  $attrib['name'] = '_subject';
430  $textfield = new textfield($attrib);
431
432  $subject = '';
433
434  // use subject from post
435  if (isset($_POST['_subject']))
436    $subject = get_input_value('_subject', RCUBE_INPUT_POST, TRUE);
437   
438  // create a reply-subject
439  else if (isset($REPLY_MESSAGE['subject']))
440    {
441    if (eregi('^re:', $REPLY_MESSAGE['subject']))
442      $subject = $REPLY_MESSAGE['subject'];
443    else
444      $subject = 'Re: '.$REPLY_MESSAGE['subject'];
445    }
446
447  // create a forward-subject
448  else if (isset($FORWARD_MESSAGE['subject']))
449    {
450    if (eregi('^fwd:', $REPLY_MESSAGE['subject']))
451      $subject = $FORWARD_MESSAGE['subject'];
452    else
453      $subject = 'Fwd: '.$FORWARD_MESSAGE['subject'];
454    }
455
456 
457  $out = $form_start ? "$form_start\n" : '';
458  $out .= $textfield->show($subject);
459  $out .= $form_end ? "\n$form_end" : '';
460         
461  return $out;
462  }
463
464
465function rcmail_compose_attachment_list($attrib)
466  {
467  global $OUTPUT, $JS_OBJECT_NAME;
468 
469  // add ID if not given
470  if (!$attrib['id'])
471    $attrib['id'] = 'rcmAttachmentList';
472 
473  // allow the following attributes to be added to the <ul> tag
474  $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style'));
475 
476  $out = '<ul'. $attrib_str . ">\n";
477 
478  if (is_array($_SESSION['compose']['attachments']))
479    {
480    foreach ($_SESSION['compose']['attachments'] as $i => $a_prop)
481      $out .= sprintf("<li>%s</li>\n", $a_prop['name']);
482    }
483
484  $OUTPUT->add_script(sprintf("%s.gui_object('attachmentlist', '%s');", $JS_OBJECT_NAME, $attrib['id'])); 
485   
486  $out .= '</ul>';
487  return $out;
488  }
489
490
491
492function rcmail_compose_attachment_form($attrib)
493  {
494  global $OUTPUT, $JS_OBJECT_NAME, $SESS_HIDDEN_FIELD;
495
496  // add ID if not given
497  if (!$attrib['id'])
498    $attrib['id'] = 'rcmUploadbox';
499 
500  // allow the following attributes to be added to the <div> tag
501  $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style'));
502  $input_field = rcmail_compose_attachment_field(array());
503  $label_send = rcube_label('upload');
504  $label_close = rcube_label('close');
505 
506  $out = <<<EOF
507<div$attrib_str>
508<form action="./" method="post" enctype="multipart/form-data">
509$SESS_HIDDEN_FIELD
510$input_field<br />
511<input type="button" value="$label_close" class="button" onclick="document.getElementById('$attrib[id]').style.visibility='hidden'" />
512<input type="button" value="$label_send" class="button" onclick="$JS_OBJECT_NAME.command('send-attachment', this.form)" />
513</form>
514</div>
515EOF;
516
517 
518  $OUTPUT->add_script(sprintf("%s.gui_object('uploadbox', '%s');", $JS_OBJECT_NAME, $attrib['id'])); 
519  return $out;
520  }
521
522
523function rcmail_compose_attachment_field($attrib)
524  {
525  // allow the following attributes to be added to the <input> tag
526  $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style', 'size'));
527 
528  $out = '<input type="file" name="_attachments[]"'. $attrib_str . " />";
529  return $out;
530  }
531
532
533function rcmail_priority_selector($attrib)
534  {
535  list($form_start, $form_end) = get_form_tags($attrib);
536  unset($attrib['form']);
537 
538  $attrib['name'] = '_priority';
539  $selector = new select($attrib);
540
541  $selector->add(array(rcube_label('lowest'),
542                       rcube_label('low'),
543                       rcube_label('normal'),
544                       rcube_label('high'),
545                       rcube_label('highest')),
546                 array(5, 4, 0, 2, 1));
547                 
548  $sel = isset($_POST['_priority']) ? $_POST['_priority'] : 0;
549
550  $out = $form_start ? "$form_start\n" : '';
551  $out .= $selector->show($sel);
552  $out .= $form_end ? "\n$form_end" : '';
553         
554  return $out;
555  }
556
557
558function get_form_tags($attrib)
559  {
560  global $CONFIG, $OUTPUT, $JS_OBJECT_NAME, $MESSAGE_FORM, $SESS_HIDDEN_FIELD; 
561
562  $form_start = '';
563  if (!strlen($MESSAGE_FORM))
564    {
565    $hiddenfields = new hiddenfield(array('name' => '_task', 'value' => $GLOBALS['_task']));
566    $hiddenfields->add(array('name' => '_action', 'value' => 'send'));
567   
568    $form_start = empty($attrib['form']) ? '<form name="form" action="./" method="post">' : '';
569    $form_start .= "\n$SESS_HIDDEN_FIELD\n";
570    $form_start .= $hiddenfields->show();
571    }
572   
573  $form_end = (strlen($MESSAGE_FORM) && !strlen($attrib['form'])) ? '</form>' : '';
574  $form_name = !empty($attrib['form']) ? $attrib['form'] : 'form';
575 
576  if (!strlen($MESSAGE_FORM))
577    $OUTPUT->add_script("$JS_OBJECT_NAME.gui_object('messageform', '$form_name');");
578 
579  $MESSAGE_FORM = $form_name;
580
581  return array($form_start, $form_end); 
582  }
583
584
585function format_email_recipient($email, $name='')
586  {
587  if ($name && $name != $email)
588    return sprintf('%s <%s>', strpos($name, ",") ? '"'.$name.'"' : $name, $email);
589  else
590    return $email;
591  }
592
593
594function rcmail_charset_pulldown($selected='ISO-8859-1')
595  {
596  $select = new select();
597 
598 
599  return $select->show($selected);
600  }
601
602
603/****** get contacts for this user and add them to client scripts ********/
604
605$sql_result = $DB->query("SELECT name, email
606                          FROM ".get_table_name('contacts')." WHERE  user_id=?
607                          AND  del<>1",$_SESSION['user_id']);
608                                   
609if ($DB->num_rows($sql_result))
610  {       
611  $a_contacts = array();
612  while ($sql_arr = $DB->fetch_assoc($sql_result))
613    if ($sql_arr['email'])
614      $a_contacts[] = format_email_recipient($sql_arr['email'], rep_specialchars_output($sql_arr['name'], 'js'));
615 
616  $OUTPUT->add_script(sprintf("$JS_OBJECT_NAME.set_env('contacts', %s);", array2js($a_contacts)));
617  }
618
619
620
621
622parse_template('compose');
623?>
Note: See TracBrowser for help on using the repository browser.