thomascube
2005-11-06 10a699759d4f106f29c077a6d65d3b8d212825e5
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
T 36 rcube_add_label('nosubject', 'norecipientwarning', 'nosubjectwarning', 'nobodywarning', 'sendingmessage');
37
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));
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     }
63   else
64     {
65     $FORWARD_MESSAGE = $MESSAGE;
66     $_SESSION['compose']['forward_uid'] = $_GET['_forward_uid'];
67     }
68   }
69
70
71
72 /****** compose mode functions ********/
73
74
75 function rcmail_compose_headers($attrib)
76   {
77   global $IMAP, $REPLY_MESSAGE, $DB;
78
79   list($form_start, $form_end) = get_form_tags($attrib);
80   
81   $out = '';
82   $part = strtolower($attrib['part']);
83   
84   switch ($part)
85     {
86     case 'from':
87       // pass the following attributes to the form class
88       $field_attrib = array('name' => '_from');
89       foreach ($attrib as $attr => $value)
317219 90         if (in_array($attr, array('id', 'class', 'style', 'size', 'tabindex')))
4e17e6 91           $field_attrib[$attr] = $value;
T 92     
93       // get this user's identities
d7cb77 94       $sql_result = $DB->query("SELECT identity_id, name, email
S 95                                 FROM   ".get_table_name('identities')." WHERE  user_id=?
96                                 AND    del<>'1'
97                                 ORDER BY ".$DB->quoteIdentifier('default')." DESC, name ASC",
98                                 $_SESSION['user_id']);
4e17e6 99                                    
T 100       if ($DB->num_rows($sql_result))
101         {        
102         $select_from = new select($field_attrib);
103         while ($sql_arr = $DB->fetch_assoc($sql_result))
104           $select_from->add(format_email_recipient($sql_arr['email'], $sql_arr['name']), $sql_arr['identity_id']);
105         
106         $out = $select_from->show($_POST['_from']);
107         }
108       else
109         {
110         $input_from = new textfield($field_attrib);
111         $out = $input_from->show($_POST['_from']);
112         }
113         
114       if ($form_start)
115         $out = $form_start.$out;
116
117       return $out;
118     
119
120     case 'to':
121       $fname = '_to';
122       $header = 'to';
123       
124       // we have contact id's as get parameters
597170 125       if (!empty($_GET['_to']) && preg_match('/[0-9]+,?/', $_GET['_to']))
4e17e6 126         {
T 127         $a_recipients = array();
d7cb77 128         $sql_result = $DB->query("SELECT name, email
S 129                                   FROM ".get_table_name('contacts')." WHERE user_id=?
130                                   AND    del<>'1'
131                                   AND    contact_id IN (".$_GET['_to'].")",
132                                   $_SESSION['user_id']);
4e17e6 133                                          
T 134         while ($sql_arr = $DB->fetch_assoc($sql_result))
135           $a_recipients[] = format_email_recipient($sql_arr['email'], $sql_arr['name']);
136           
137         if (sizeof($a_recipients))
138           $fvalue = join(', ', $a_recipients);
139         }
597170 140       else if (!empty($_GET['_to']))
4e17e6 141         $fvalue = $_GET['_to'];
T 142         
143     case 'cc':
144       if (!$fname)
145         {
146         $fname = '_cc';
147         //$header = 'cc';
148         }
149     case 'bcc':
150       if (!$fname)
151         $fname = '_bcc';
152         
317219 153       $allow_attrib = array('id', 'class', 'style', 'cols', 'rows', 'wrap', 'tabindex');
4e17e6 154       $field_type = 'textarea';            
T 155       break;
156
157     case 'replyto':
158     case 'reply-to':
159       $fname = '_replyto';
317219 160       $allow_attrib = array('id', 'class', 'style', 'size', 'tabindex');
4e17e6 161       $field_type = 'textfield';
T 162       break;
163     
164     }
165     
166     
597170 167   if ($fname && !empty($_POST[$fname]))
4e17e6 168     $fvalue = $_POST[$fname];
T 169   else if ($header && is_object($REPLY_MESSAGE['headers']))
170     {
171     // get recipent address(es) out of the message headers
172     if ($header=='to' && $REPLY_MESSAGE['headers']->replyto)
173       $fvalue = $IMAP->decode_header($REPLY_MESSAGE['headers']->replyto);
174     else if ($header=='to' && $REPLY_MESSAGE['headers']->from)
175       $fvalue = $IMAP->decode_header($REPLY_MESSAGE['headers']->from);
176     
177     // split recipients and put them back together in a unique way
178     $to_addresses = $IMAP->decode_address_list($fvalue);
179     $fvalue = '';
180     foreach ($to_addresses as $addr_part)
181       $fvalue .= (strlen($fvalue) ? ', ':'').$addr_part['string'];
182     }
183     
184         
185   if ($fname && $field_type)
186     {
187     // pass the following attributes to the form class
188     $field_attrib = array('name' => $fname);
189     foreach ($attrib as $attr => $value)
190       if (in_array($attr, $allow_attrib))
191         $field_attrib[$attr] = $value;
192
193     // create teaxtarea object
194     $input = new $field_type($field_attrib);
195     $out = $input->show($fvalue);    
196     }
197   
198   if ($form_start)
199     $out = $form_start.$out;
200   
201   return $out;  
202   }
203
204
205 /*function rcube_compose_headers($attrib)
206   {
207   global $CONFIG, $OUTPUT;
208
209   list($form_start, $form_end) = get_form_tags($attrib);
210
211   // allow the following attributes to be added to the headers table
212   $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id', 'cellpadding', 'cellspacing', 'border'));
213   
214   $labels = array();
215   $labels['from'] = rcube_label('from');
216   $labels['to'] = rcube_label('to');
217   $labels['cc'] = rcube_label('cc');
218   $labels['bcc'] = rcube_label('bcc');
219   $labels['replyto'] = rcube_label('replyto');
220
221   $input_from = new textfield(array('name' => '_from', 'size' => 30));
222   $input_to = new textfield(array('name' => '_to', 'size' => 30));
223   $input_cc = new textfield(array('name' => '_cc', 'size' => 30));
224   $input_bcc = new textfield(array('name' => '_bcc', 'size' => 30));
225   $input_replyto = new textfield(array('name' => '_replyto', 'size' => 30));
226
227   $fields = array();
228   $fields['from'] = $input_from->show($_POST['_from']);
229   $fields['to'] = $input_to->show($_POST['_to']);
230   $fields['cc'] = $input_cc->show($_POST['_cc']);
231   $fields['bcc'] = $input_bcc->show($_POST['_bcc']);
232   $fields['replyto'] = $input_replyto->show($_POST['_replyto']);
233
234
235   $out = <<<EOF
236 $form_start
237 <table$attrib_str><tr>
238
239 <td class="title">$labels[from]</td>
240 <td>$fields[from]</td>
241
242 </tr><tr>
243
244 <td class="title">$labels[to]</td>
245 <td>$fields[to]</td>
246
247 </tr><tr>
248
249 <td class="title">$labels[cc]</td>
250 <td>$fields[cc]</td>
251
252 </tr><tr>
253
254 <td class="title">$labels[bcc]</td>
255 <td>$fields[bcc]</td>
256
257 </tr><tr>
258
259 <td class="title">$labels[replyto]</td>
260 <td>$fields[replyto]</td>
261
262 </tr></table>
263 $form_end
264 EOF;
265
266   return $out;  
267   }
268 */
269
270
271 function rcmail_compose_body($attrib)
272   {
273   global $CONFIG, $REPLY_MESSAGE, $FORWARD_MESSAGE;
274   
275   list($form_start, $form_end) = get_form_tags($attrib);
276   unset($attrib['form']);
277   
278   $attrib['name'] = '_message';
279   $textarea = new textarea($attrib);
280
281   $body = '';
282   
283   // use posted message body
597170 284   if (!empty($_POST['_message']))
30233b 285     $body = stripslashes($_POST['_message']);
4e17e6 286     
T 287   // compose reply-body
288   else if (is_array($REPLY_MESSAGE['parts']))
289     {
290     $body = rcmail_first_text_part($REPLY_MESSAGE['parts']);
291     if (strlen($body))
292       $body = rcmail_create_reply_body($body);
293     }
294
295   // forward message body inline
296   else if (is_array($FORWARD_MESSAGE['parts']))
297     {
298     $body = rcmail_first_text_part($FORWARD_MESSAGE['parts']);
299     if (strlen($body))
300       $body = rcmail_create_forward_body($body);
301     }
302   
303   $out = $form_start ? "$form_start\n" : '';
304   $out .= $textarea->show($body);
305   $out .= $form_end ? "\n$form_end" : '';
306          
307   return $out;
308   }
309
310
311 function rcmail_create_reply_body($body)
312   {
313   global $IMAP, $REPLY_MESSAGE;
314
315   // soft-wrap message first
316   $body = wordwrap($body, 75);
317   
318   // split body into single lines
319   $a_lines = preg_split('/\r?\n/', $body);
320   
321   // add > to each line
322   for($n=0; $n<sizeof($a_lines); $n++)
323     {
324     if (strpos($a_lines[$n], '>')===0)
325       $a_lines[$n] = '>'.$a_lines[$n];
326     else
327       $a_lines[$n] = '> '.$a_lines[$n];
328     }
329  
330   $body = join("\n", $a_lines);
331
332   // add title line
333   $pefix = sprintf("\n\n\nOn %s, %s wrote:\n",
334            $REPLY_MESSAGE['headers']->date,
335            $IMAP->decode_header($REPLY_MESSAGE['headers']->from));
336
337   return $pefix.$body;
338   }
339
340
341 function rcmail_create_forward_body($body)
342   {
343   global $IMAP, $FORWARD_MESSAGE;
344
345   // soft-wrap message first
346   $body = wordwrap($body, 80);
347   
348   $prefix = sprintf("\n\n\n-------- Original Message --------\nSubject: %s\nDate: %s\nFrom: %s\nTo: %s\n\n",
349                    $FORWARD_MESSAGE['subject'],
350                    $FORWARD_MESSAGE['headers']->date,
351                    $IMAP->decode_header($FORWARD_MESSAGE['headers']->from),
352                    $IMAP->decode_header($FORWARD_MESSAGE['headers']->to));
353
597170 354   // add attachments
T 355   if (!isset($_SESSION['compose']['forward_attachments']) && is_array($FORWARD_MESSAGE['parts']) && sizeof($FORWARD_MESSAGE['parts'])>1)
356     {
357     $temp_dir = rcmail_create_compose_tempdir();
358
359     if (!is_array($_SESSION['compose']['attachments']))
360       $_SESSION['compose']['attachments'] = array();
361   
362     foreach ($FORWARD_MESSAGE['parts'] as $part)
363       {
364       if ($part->disposition != 'attachment')
365         continue;
366
367       $tmp_path = tempnam($temp_dir, 'rcmAttmnt');
368       if ($fp = fopen($tmp_path, 'w'))
369         {
370         fwrite($fp, $IMAP->mime_decode($part->body, $part->headers['content-transfer-encoding']));
371         fclose($fp);
372
373         $_SESSION['compose']['attachments'][] = array('name' => $part->d_parameters['filename'],
374                                                       'mimetype' => $part->ctype_primary . '/' . $part->ctype_secondary,
375                                                       'path' => $tmp_path);
376         }
377       }
378
379     $_SESSION['compose']['forward_attachments'] = TRUE;
380     }
381
4e17e6 382   return $prefix.$body;
T 383   }
384
385
386
387 function rcmail_compose_subject($attrib)
388   {
389   global $CONFIG, $REPLY_MESSAGE, $FORWARD_MESSAGE;
390   
391   list($form_start, $form_end) = get_form_tags($attrib);
392   unset($attrib['form']);
393   
394   $attrib['name'] = '_subject';
395   $textfield = new textfield($attrib);
396
397   $subject = '';
398
399   // use subject from post
597170 400   if (isset($_POST['_subject']))
30233b 401     $subject = stripslashes($_POST['_subject']);
4e17e6 402     
T 403   // create a reply-subject
404   else if (isset($REPLY_MESSAGE['subject']))
520c36 405     {
09941e 406     if (eregi('^re:', $REPLY_MESSAGE['subject']))
520c36 407       $subject = $REPLY_MESSAGE['subject'];
T 408     else
409       $subject = 'Re: '.$REPLY_MESSAGE['subject'];
410     }
4e17e6 411
T 412   // create a forward-subject
413   else if (isset($FORWARD_MESSAGE['subject']))
09941e 414     {
T 415     if (eregi('^fwd:', $REPLY_MESSAGE['subject']))
416       $subject = $FORWARD_MESSAGE['subject'];
417     else
418       $subject = 'Fwd: '.$FORWARD_MESSAGE['subject'];
419     }
4e17e6 420
T 421   
422   $out = $form_start ? "$form_start\n" : '';
423   $out .= $textfield->show($subject);
424   $out .= $form_end ? "\n$form_end" : '';
425          
426   return $out;
427   }
428
429
430 function rcmail_compose_attachment_list($attrib)
431   {
432   global $OUTPUT, $JS_OBJECT_NAME;
433   
434   // add ID if not given
435   if (!$attrib['id'])
436     $attrib['id'] = 'rcmAttachmentList';
437   
438   // allow the following attributes to be added to the <ul> tag
439   $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style'));
440  
441   $out = '<ul'. $attrib_str . ">\n";
442   
443   if (is_array($_SESSION['compose']['attachments']))
444     {
445     foreach ($_SESSION['compose']['attachments'] as $i => $a_prop)
446       $out .= sprintf("<li>%s</li>\n", $a_prop['name']);
447     }
448
449   $OUTPUT->add_script(sprintf("%s.gui_object('attachmentlist', '%s');", $JS_OBJECT_NAME, $attrib['id']));  
450     
451   $out .= '</ul>';
452   return $out;
453   }
454
455
456
457 function rcmail_compose_attachment_form($attrib)
458   {
459   global $OUTPUT, $JS_OBJECT_NAME, $SESS_HIDDEN_FIELD;
460
461   // add ID if not given
462   if (!$attrib['id'])
463     $attrib['id'] = 'rcmUploadbox';
464   
465   // allow the following attributes to be added to the <div> tag
466   $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style'));
467   $input_field = rcmail_compose_attachment_field(array());
468   $label_send = rcube_label('upload');
469   $label_close = rcube_label('close');
470   
471   $out = <<<EOF
472 <div$attrib_str>
473 <form action="./" method="post" enctype="multipart/form-data">
474 $SESS_HIDDEN_FIELD
475 $input_field<br />
476 <input type="button" value="$label_close" class="button" onclick="document.getElementById('$attrib[id]').style.visibility='hidden'" />
477 <input type="button" value="$label_send" class="button" onclick="$JS_OBJECT_NAME.command('send-attachment', this.form)" />
478 </form>
479 </div>
480 EOF;
481
482   
483   $OUTPUT->add_script(sprintf("%s.gui_object('uploadbox', '%s');", $JS_OBJECT_NAME, $attrib['id']));  
484   return $out;
485   }
486
487
488 function rcmail_compose_attachment_field($attrib)
489   {
490   // allow the following attributes to be added to the <input> tag
491   $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style', 'size'));
492  
493   $out = '<input type="file" name="_attachments[]"'. $attrib_str . " />";
494   return $out;
495   }
496
497
498 function rcmail_priority_selector($attrib)
499   {
500   list($form_start, $form_end) = get_form_tags($attrib);
501   unset($attrib['form']);
502   
503   $attrib['name'] = '_priority';
504   $selector = new select($attrib);
505
506   $selector->add(array(rcube_label('lowest'),
507                        rcube_label('low'),
508                        rcube_label('normal'),
509                        rcube_label('high'),
510                        rcube_label('highest')),
7902df 511                  array(5, 4, 0, 2, 1));
4e17e6 512                  
597170 513   $sel = isset($_POST['_priority']) ? $_POST['_priority'] : 0;
4e17e6 514
T 515   $out = $form_start ? "$form_start\n" : '';
516   $out .= $selector->show($sel);
517   $out .= $form_end ? "\n$form_end" : '';
518          
519   return $out;
520   }
521
522
523 function get_form_tags($attrib)
524   {
525   global $CONFIG, $OUTPUT, $JS_OBJECT_NAME, $MESSAGE_FORM, $SESS_HIDDEN_FIELD;  
526
527   $form_start = '';
528   if (!strlen($MESSAGE_FORM))
529     {
530     $hiddenfields = new hiddenfield(array('name' => '_task', 'value' => $GLOBALS['_task']));
531     $hiddenfields->add(array('name' => '_action', 'value' => 'send'));
532     
597170 533     $form_start = empty($attrib['form']) ? '<form name="form" action="./" method="post">' : '';
4e17e6 534     $form_start .= "\n$SESS_HIDDEN_FIELD\n";
T 535     $form_start .= $hiddenfields->show();
536     }
537     
538   $form_end = (strlen($MESSAGE_FORM) && !strlen($attrib['form'])) ? '</form>' : '';
597170 539   $form_name = !empty($attrib['form']) ? $attrib['form'] : 'form';
4e17e6 540   
T 541   if (!strlen($MESSAGE_FORM))
542     $OUTPUT->add_script("$JS_OBJECT_NAME.gui_object('messageform', '$form_name');");
543   
544   $MESSAGE_FORM = $form_name;
545
546   return array($form_start, $form_end);  
547   }
548
549
550 function format_email_recipient($email, $name='')
551   {
552   if ($name && $name != $email)
553     return sprintf('%s <%s>', strpos($name, ",") ? '"'.$name.'"' : $name, $email);
554   else
555     return $email;
556   }
557
558
559 /****** get contacts for this user and add them to client scripts ********/
560
d7cb77 561 $sql_result = $DB->query("SELECT name, email
S 562                           FROM ".get_table_name('contacts')." WHERE  user_id=?
563                           AND  del<>'1'",$_SESSION['user_id']);
4e17e6 564                                    
T 565 if ($DB->num_rows($sql_result))
566   {        
567   $a_contacts = array();
568   while ($sql_arr = $DB->fetch_assoc($sql_result))
569     if ($sql_arr['email'])
570       $a_contacts[] = format_email_recipient($sql_arr['email'], rep_specialchars_output($sql_arr['name'], 'js'));
571   
572   $OUTPUT->add_script(sprintf("$JS_OBJECT_NAME.set_env('contacts', %s);", array2js($a_contacts)));
573   }
574
575
576
577
578 parse_template('compose');
579 ?>