thomascube
2006-03-04 f5121b5639992fc9e51fd551bac2254429b638fa
commit | author | age
4e17e6 1 <?php
T 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                      |
30233b 9  | Licensed under the GNU GPL                                            |
4e17e6 10  |                                                                       |
T 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
23 require_once('Mail/mimeDecode.php');
24
25
26 $MESSAGE_FORM = NULL;
27 $REPLY_MESSAGE = NULL;
28 $FORWARD_MESSAGE = NULL;
29
30
31 if (!is_array($_SESSION['compose']))
32   $_SESSION['compose'] = array('id' => uniqid(rand()));
33
34
10a699 35 // add some labels to client
977a29 36 rcube_add_label('nosubject', 'norecipientwarning', 'nosubjectwarning', 'nobodywarning', 'sendingmessage', 'notsentwarning');
10a699 37
T 38
4e17e6 39 if ($_GET['_reply_uid'] || $_GET['_forward_uid'])
T 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));
583f1c 53
4e17e6 54   $MESSAGE['subject'] = $IMAP->decode_header($MESSAGE['headers']->subject);
T 55   $MESSAGE['parts'] = $mmd->getMimeNumbers($MESSAGE['structure']);
56   
57   if ($_GET['_reply_uid'])
58     {
583f1c 59     $REPLY_MESSAGE = &$MESSAGE;
4e17e6 60     $_SESSION['compose']['reply_uid'] = $_GET['_reply_uid'];
T 61     $_SESSION['compose']['reply_msgid'] = $REPLY_MESSAGE['headers']->messageID;
479399 62     $_SESSION['compose']['references']  = $REPLY_MESSAGE['headers']->reference;
f88d41 63     $_SESSION['compose']['references'] .= !empty($REPLY_MESSAGE['headers']->reference) ? ' ' : '';
T 64     $_SESSION['compose']['references'] .= $REPLY_MESSAGE['headers']->messageID;
583f1c 65
T 66     if ($_GET['_all'])
67       $REPLY_MESSAGE['reply_all'] = 1;
4e17e6 68     }
T 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
81 function rcmail_compose_headers($attrib)
82   {
83   global $IMAP, $REPLY_MESSAGE, $DB;
583f1c 84   static $sa_recipients = array();
4e17e6 85
T 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':
1cded8 94       return rcmail_compose_header_from($attrib);
4e17e6 95
T 96     case 'to':
97       $fname = '_to';
98       $header = 'to';
1cded8 99
4e17e6 100       // we have contact id's as get parameters
1cded8 101       if (!empty($_GET['_to']) && preg_match('/^([0-9]+,?)+$/', $_GET['_to']))
4e17e6 102         {
T 103         $a_recipients = array();
d7cb77 104         $sql_result = $DB->query("SELECT name, email
1cded8 105                                   FROM ".get_table_name('contacts')."
T 106                                   WHERE user_id=?
107                                   AND    del<>1
d7cb77 108                                   AND    contact_id IN (".$_GET['_to'].")",
S 109                                   $_SESSION['user_id']);
4e17e6 110                                          
T 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         }
597170 117       else if (!empty($_GET['_to']))
4e17e6 118         $fvalue = $_GET['_to'];
T 119         
120     case 'cc':
121       if (!$fname)
122         {
123         $fname = '_cc';
583f1c 124         $header = 'cc';
4e17e6 125         }
T 126     case 'bcc':
127       if (!$fname)
128         $fname = '_bcc';
129         
317219 130       $allow_attrib = array('id', 'class', 'style', 'cols', 'rows', 'wrap', 'tabindex');
4e17e6 131       $field_type = 'textarea';            
T 132       break;
133
134     case 'replyto':
135     case 'reply-to':
136       $fname = '_replyto';
317219 137       $allow_attrib = array('id', 'class', 'style', 'size', 'tabindex');
4e17e6 138       $field_type = 'textfield';
T 139       break;
140     
141     }
583f1c 142
4e17e6 143     
597170 144   if ($fname && !empty($_POST[$fname]))
ea7c46 145     $fvalue = get_input_value($fname, RCUBE_INPUT_POST);
4e17e6 146   else if ($header && is_object($REPLY_MESSAGE['headers']))
T 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);
58e360 151
4e17e6 152     else if ($header=='to' && $REPLY_MESSAGE['headers']->from)
T 153       $fvalue = $IMAP->decode_header($REPLY_MESSAGE['headers']->from);
58e360 154
583f1c 155     // add recipent of original message if reply to all
T 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
4e17e6 170     // split recipients and put them back together in a unique way
583f1c 171     if (!empty($fvalue))
T 172       {
173       $to_addresses = $IMAP->decode_address_list($fvalue);
174       $fvalue = '';
175       foreach ($to_addresses as $addr_part)
176         {
58e360 177         if (!in_array($addr_part['mailto'], $sa_recipients) && (!$REPLY_MESSAGE['FROM'] || !in_array($addr_part['mailto'], $REPLY_MESSAGE['FROM'])))
583f1c 178           {
T 179           $fvalue .= (strlen($fvalue) ? ', ':'').$addr_part['string'];
180           $sa_recipients[] = $addr_part['mailto'];
181           }
182         }
183       }
4e17e6 184     }
583f1c 185
4e17e6 186         
T 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
1cded8 207
T 208 function rcmail_compose_header_from($attrib)
4e17e6 209   {
1cded8 210   global $IMAP, $REPLY_MESSAGE, $DB, $OUTPUT, $JS_OBJECT_NAME;
T 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;
4e17e6 217
1cded8 218   // extract all recipients of the reply-message
T 219   $a_recipients = array();
220   if ($REPLY_MESSAGE && is_object($REPLY_MESSAGE['headers']))
221     {
58e360 222     $REPLY_MESSAGE['FROM'] = array();
T 223
1cded8 224     $a_to = $IMAP->decode_address_list($REPLY_MESSAGE['headers']->to);        
T 225     foreach ($a_to as $addr)
226       {
227       if (!empty($addr['mailto']))
228         $a_recipients[] = $addr['mailto'];
229       }
4e17e6 230
1cded8 231     if (!empty($REPLY_MESSAGE['headers']->cc))
T 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     }
4e17e6 241
1cded8 242   // get this user's identities
T 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']);
4e17e6 261
1cded8 262       // add signature to array
T 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'];
58e360 269         
T 270       if ($REPLY_MESSAGE && is_array($REPLY_MESSAGE['FROM']))
271         $REPLY_MESSAGE['FROM'][] = $sql_arr['email'];
1cded8 272       }
4e17e6 273
1cded8 274     // overwrite identity selection with post parameter
T 275     if (isset($_POST['_from']))
276       $from_id = $_POST['_from'];
4e17e6 277
1cded8 278     $out = $select_from->show($from_id);
T 279     
4e17e6 280
1cded8 281     // add signatures to client
T 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;
4e17e6 292
1cded8 293   return $out;
4e17e6 294   }
T 295
1cded8 296   
4e17e6 297
T 298 function 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
597170 311   if (!empty($_POST['_message']))
ea7c46 312     $body = get_input_value('_message', RCUBE_INPUT_POST, TRUE);
4e17e6 313     
T 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
338 function 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));
1cded8 363            
T 364
365   // try to remove the signature
749b07 366   if ($sp = strrpos($body, '-- '))
1cded8 367     {
T 368     if ($body{$sp+3}==' ' || $body{$sp+3}=="\n" || $body{$sp+3}=="\r")
369       $body = substr($body, 0, $sp-1);
370     }
4e17e6 371
T 372   return $pefix.$body;
373   }
374
375
376 function 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
597170 389   // add attachments
T 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
4e17e6 417   return $prefix.$body;
T 418   }
419
420
421
422 function 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
597170 435   if (isset($_POST['_subject']))
ea7c46 436     $subject = get_input_value('_subject', RCUBE_INPUT_POST);
4e17e6 437     
T 438   // create a reply-subject
439   else if (isset($REPLY_MESSAGE['subject']))
520c36 440     {
09941e 441     if (eregi('^re:', $REPLY_MESSAGE['subject']))
520c36 442       $subject = $REPLY_MESSAGE['subject'];
T 443     else
444       $subject = 'Re: '.$REPLY_MESSAGE['subject'];
445     }
4e17e6 446
T 447   // create a forward-subject
448   else if (isset($FORWARD_MESSAGE['subject']))
09941e 449     {
T 450     if (eregi('^fwd:', $REPLY_MESSAGE['subject']))
451       $subject = $FORWARD_MESSAGE['subject'];
452     else
453       $subject = 'Fwd: '.$FORWARD_MESSAGE['subject'];
454     }
4e17e6 455
T 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
465 function 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
492 function 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>
515 EOF;
516
517   
518   $OUTPUT->add_script(sprintf("%s.gui_object('uploadbox', '%s');", $JS_OBJECT_NAME, $attrib['id']));  
519   return $out;
520   }
521
522
523 function 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
533 function 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')),
7902df 546                  array(5, 4, 0, 2, 1));
4e17e6 547                  
597170 548   $sel = isset($_POST['_priority']) ? $_POST['_priority'] : 0;
4e17e6 549
T 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
558 function 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     
597170 568     $form_start = empty($attrib['form']) ? '<form name="form" action="./" method="post">' : '';
4e17e6 569     $form_start .= "\n$SESS_HIDDEN_FIELD\n";
T 570     $form_start .= $hiddenfields->show();
571     }
572     
573   $form_end = (strlen($MESSAGE_FORM) && !strlen($attrib['form'])) ? '</form>' : '';
597170 574   $form_name = !empty($attrib['form']) ? $attrib['form'] : 'form';
4e17e6 575   
T 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
585 function 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
fd8c50 594 function rcmail_charset_pulldown($selected='ISO-8859-1')
T 595   {
596   $select = new select();
597   
598   
599   return $select->show($selected);
600   }
601
602
4e17e6 603 /****** get contacts for this user and add them to client scripts ********/
T 604
d7cb77 605 $sql_result = $DB->query("SELECT name, email
S 606                           FROM ".get_table_name('contacts')." WHERE  user_id=?
1cded8 607                           AND  del<>1",$_SESSION['user_id']);
4e17e6 608                                    
T 609 if ($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
622 parse_template('compose');
623 ?>