]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/mail/compose.inc
Imported Upstream version 0.1~beta2.2~dfsg
[roundcube.git] / program / steps / mail / compose.inc
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: compose.inc 297 2006-08-06 15:55:11Z thomasb $
19
20 */
21
22
23 require_once('Mail/mimeDecode.php');
24
25 // remove an attachment
26 if ($_action=='remove-attachment' && preg_match('/^rcmfile([0-9]+)$/', $_GET['_file'], $regs))
27   {
28   $id = $regs[1];
29   if (is_array($_SESSION['compose']['attachments'][$id]))
30     {
31     @unlink($_SESSION['compose']['attachments'][$id]['path']);
32     $_SESSION['compose']['attachments'][$id] = NULL;
33     $commands = sprintf("parent.%s.remove_from_attachment_list('rcmfile%d');\n", $JS_OBJECT_NAME, $id);
34     rcube_remote_response($commands);  
35     exit;
36     }
37   }
38
39
40 $MESSAGE_FORM = NULL;
41 $REPLY_MESSAGE = NULL;
42 $FORWARD_MESSAGE = NULL;
43 $DRAFT_MESSAGE = NULL;
44
45 // nothing below is called during message composition, only at "new/forward/reply/draft" initialization
46 // since there are many ways to leave the compose page improperly, it seems necessary to clean-up an old
47 // compose when a "new/forward/reply/draft" is called - otherwise the old session attachments will appear
48
49 rcmail_compose_cleanup();
50 $_SESSION['compose'] = array('id' => uniqid(rand()));
51
52 // add some labels to client
53 rcube_add_label('nosubject', 'norecipientwarning', 'nosubjectwarning', 'nobodywarning', 'notsentwarning', 'savingmessage', 'sendingmessage', 'messagesaved');
54
55
56 if ($_GET['_reply_uid'] || $_GET['_forward_uid'] || $_GET['_draft_uid'])
57   {
58   $msg_uid = ($_GET['_reply_uid'] ? $_GET['_reply_uid'] : ($_GET['_forward_uid'] ? $_GET['_forward_uid'] : $_GET['_draft_uid']));
59
60   // similar as in program/steps/mail/show.inc
61   $MESSAGE = array();
62   $MESSAGE['headers'] = $IMAP->get_headers($msg_uid);
63   
64   $MESSAGE['source'] = rcmail_message_source($msg_uid);
65   
66   $mmd = new Mail_mimeDecode($MESSAGE['source']);
67   $MESSAGE['structure'] = $mmd->decode(array('include_bodies' => TRUE,
68                                              'decode_headers' => TRUE,
69                                              'decode_bodies' => FALSE));
70
71   $MESSAGE['subject'] = $IMAP->decode_header($MESSAGE['headers']->subject);
72   $MESSAGE['parts'] = $mmd->getMimeNumbers($MESSAGE['structure']);
73
74   if ($_GET['_reply_uid'])
75     {
76     $REPLY_MESSAGE = &$MESSAGE;
77     $_SESSION['compose']['reply_uid'] = $_GET['_reply_uid'];
78     $_SESSION['compose']['reply_msgid'] = $REPLY_MESSAGE['headers']->messageID;
79     $_SESSION['compose']['references']  = $REPLY_MESSAGE['headers']->reference;
80     $_SESSION['compose']['references'] .= !empty($REPLY_MESSAGE['headers']->reference) ? ' ' : '';
81     $_SESSION['compose']['references'] .= $REPLY_MESSAGE['headers']->messageID;
82
83     if ($_GET['_all'])
84       $REPLY_MESSAGE['reply_all'] = 1;
85
86     }
87   else if ($_GET['_forward_uid'])
88     {
89     $FORWARD_MESSAGE = $MESSAGE;
90     $_SESSION['compose']['forward_uid'] = $_GET['_forward_uid'];
91     }
92   else
93     {
94     $DRAFT_MESSAGE = $MESSAGE;
95     $_SESSION['compose']['draft_uid'] = $_GET['_draft_uid'];
96     }
97
98   }
99
100 /****** compose mode functions ********/
101
102
103 function rcmail_compose_headers($attrib)
104   {
105   global $IMAP, $REPLY_MESSAGE, $DRAFT_MESSAGE, $DB;
106   static $sa_recipients = array();
107
108   list($form_start, $form_end) = get_form_tags($attrib);
109   
110   $out = '';
111   $part = strtolower($attrib['part']);
112   
113   switch ($part)
114     {
115     case 'from':
116       return rcmail_compose_header_from($attrib);
117
118     case 'to':
119       $fname = '_to';
120       $header = 'to';
121
122       // we have contact id's as get parameters
123       if (!empty($_GET['_to']) && preg_match('/^[0-9]+(,[0-9]+)*$/', $_GET['_to']))
124         {
125         $a_recipients = array();
126         $sql_result = $DB->query("SELECT name, email
127                                   FROM ".get_table_name('contacts')."
128                                   WHERE user_id=?
129                                   AND    del<>1
130                                   AND    contact_id IN (".$_GET['_to'].")",
131                                   $_SESSION['user_id']);
132                                          
133         while ($sql_arr = $DB->fetch_assoc($sql_result))
134           $a_recipients[] = format_email_recipient($sql_arr['email'], $sql_arr['name']);
135           
136         if (sizeof($a_recipients))
137           $fvalue = join(', ', $a_recipients);
138         }
139       else if (!empty($_GET['_to']))
140         $fvalue = $_GET['_to'];
141         
142     case 'cc':
143       if (!$fname)
144         {
145         $fname = '_cc';
146         $header = 'cc';
147         }
148     case 'bcc':
149       if (!$fname)
150         $fname = '_bcc';
151         
152       $allow_attrib = array('id', 'class', 'style', 'cols', 'rows', 'wrap', 'tabindex');
153       $field_type = 'textarea';            
154       break;
155
156     case 'replyto':
157     case 'reply-to':
158       $fname = '_replyto';
159       $allow_attrib = array('id', 'class', 'style', 'size', 'tabindex');
160       $field_type = 'textfield';
161       break;
162     
163     }
164  
165   if ($fname && !empty($_POST[$fname]))
166     $fvalue = get_input_value($fname, RCUBE_INPUT_POST, TRUE);
167   else if ($header && is_object($REPLY_MESSAGE['headers']))
168     {
169     // get recipent address(es) out of the message headers
170     if ($header=='to' && $REPLY_MESSAGE['headers']->replyto)
171       $fvalue = $IMAP->decode_header($REPLY_MESSAGE['headers']->replyto);
172
173     else if ($header=='to' && $REPLY_MESSAGE['headers']->from)
174       $fvalue = $IMAP->decode_header($REPLY_MESSAGE['headers']->from);
175
176     // add recipent of original message if reply to all
177     else if ($header=='cc' && $REPLY_MESSAGE['reply_all'])
178       {
179       if ($IMAP->decode_header($REPLY_MESSAGE['headers']->to))
180         $fvalue .= $IMAP->decode_header($REPLY_MESSAGE['headers']->to);
181
182       if ($IMAP->decode_header($REPLY_MESSAGE['headers']->cc))
183         {
184         if($fvalue)
185           $fvalue .= ', ';
186
187         $fvalue .= $IMAP->decode_header($REPLY_MESSAGE['headers']->cc);
188         }
189       }
190
191     // split recipients and put them back together in a unique way
192     if (!empty($fvalue))
193       {
194       $to_addresses = $IMAP->decode_address_list($fvalue);
195       $fvalue = '';
196       foreach ($to_addresses as $addr_part)
197         {
198         if (!in_array($addr_part['mailto'], $sa_recipients) && (!$REPLY_MESSAGE['FROM'] || !in_array($addr_part['mailto'], $REPLY_MESSAGE['FROM'])))
199           {
200           $fvalue .= (strlen($fvalue) ? ', ':'').$addr_part['string'];
201           $sa_recipients[] = $addr_part['mailto'];
202           }
203         }
204       }
205     }
206   else if ($header && is_object($DRAFT_MESSAGE['headers']))
207     {
208     // get drafted headers
209     if ($header=='to' && $DRAFT_MESSAGE['headers']->to)
210       $fvalue = $IMAP->decode_header($DRAFT_MESSAGE['headers']->to);
211
212     if ($header=='cc' && $DRAFT_MESSAGE['headers']->cc)
213       $fvalue = $IMAP->decode_header($DRAFT_MESSAGE['headers']->cc);
214
215     if ($header=='bcc' && $DRAFT_MESSAGE['headers']->bcc)
216       $fvalue = $IMAP->decode_header($DRAFT_MESSAGE['headers']->bcc);
217
218     }
219
220         
221   if ($fname && $field_type)
222     {
223     // pass the following attributes to the form class
224     $field_attrib = array('name' => $fname);
225     foreach ($attrib as $attr => $value)
226       if (in_array($attr, $allow_attrib))
227         $field_attrib[$attr] = $value;
228
229     // create teaxtarea object
230     $input = new $field_type($field_attrib);
231     $out = $input->show($fvalue);    
232     }
233   
234   if ($form_start)
235     $out = $form_start.$out;
236
237   return $out;  
238   }
239
240
241
242 function rcmail_compose_header_from($attrib)
243   {
244   global $IMAP, $REPLY_MESSAGE, $DRAFT_MESSAGE, $DB, $OUTPUT, $JS_OBJECT_NAME;
245     
246   // pass the following attributes to the form class
247   $field_attrib = array('name' => '_from');
248   foreach ($attrib as $attr => $value)
249     if (in_array($attr, array('id', 'class', 'style', 'size', 'tabindex')))
250       $field_attrib[$attr] = $value;
251
252   // extract all recipients of the reply-message
253   $a_recipients = array();
254   if ($REPLY_MESSAGE && is_object($REPLY_MESSAGE['headers']))
255     {
256     $REPLY_MESSAGE['FROM'] = array();
257
258     $a_to = $IMAP->decode_address_list($REPLY_MESSAGE['headers']->to);        
259     foreach ($a_to as $addr)
260       {
261       if (!empty($addr['mailto']))
262         $a_recipients[] = $addr['mailto'];
263       }
264
265     if (!empty($REPLY_MESSAGE['headers']->cc))
266       {
267       $a_cc = $IMAP->decode_address_list($REPLY_MESSAGE['headers']->cc);
268       foreach ($a_cc as $addr)
269         {
270         if (!empty($addr['mailto']))
271           $a_recipients[] = $addr['mailto'];
272         }
273       }
274     }
275
276   // get this user's identities
277   $sql_result = $DB->query("SELECT identity_id, name, email, signature
278                             FROM   ".get_table_name('identities')."
279                             WHERE user_id=?
280                             AND    del<>1
281                             ORDER BY ".$DB->quoteIdentifier('standard')." DESC, name ASC",
282                            $_SESSION['user_id']);
283                                    
284   if ($DB->num_rows($sql_result))
285     {
286     $from_id = 0;
287     $a_signatures = array();
288     
289     $field_attrib['onchange'] = "$JS_OBJECT_NAME.change_identity(this)";
290     $select_from = new select($field_attrib);
291     
292     while ($sql_arr = $DB->fetch_assoc($sql_result))
293       {
294       $select_from->add(format_email_recipient($sql_arr['email'], $sql_arr['name']), $sql_arr['identity_id']);
295
296       // add signature to array
297       if (!empty($sql_arr['signature']))
298         $a_signatures[$sql_arr['identity_id']] = $sql_arr['signature'];
299       
300       // set identity if it's one of the reply-message recipients
301       if (in_array($sql_arr['email'], $a_recipients))
302         $from_id = $sql_arr['identity_id'];
303         
304       if ($REPLY_MESSAGE && is_array($REPLY_MESSAGE['FROM']))
305         $REPLY_MESSAGE['FROM'][] = $sql_arr['email'];
306
307       if (strstr($DRAFT_MESSAGE['headers']->from,$sql_arr['email']))
308         $from_id = $sql_arr['identity_id'];
309
310       }
311
312     // overwrite identity selection with post parameter
313     if (isset($_POST['_from']))
314       $from_id = $_POST['_from'];
315
316     $out = $select_from->show($from_id);
317     
318
319     // add signatures to client
320     $OUTPUT->add_script(sprintf("%s.set_env('signatures', %s);", $JS_OBJECT_NAME, array2js($a_signatures)));  
321     }
322   else
323     {
324     $input_from = new textfield($field_attrib);
325     $out = $input_from->show($_POST['_from']);
326     }
327   
328   if ($form_start)
329     $out = $form_start.$out;
330
331   return $out;
332   }
333
334   
335
336 function rcmail_compose_body($attrib)
337   {
338   global $CONFIG, $OUTPUT, $REPLY_MESSAGE, $FORWARD_MESSAGE, $DRAFT_MESSAGE, $JS_OBJECT_NAME;
339   
340   list($form_start, $form_end) = get_form_tags($attrib);
341   unset($attrib['form']);
342   
343   if (empty($attrib['id']))
344     $attrib['id'] = 'rcmComposeMessage';
345   
346   $attrib['name'] = '_message';
347   $textarea = new textarea($attrib);
348
349   $body = '';
350   
351   // use posted message body
352   if (!empty($_POST['_message']))
353     $body = get_input_value('_message', RCUBE_INPUT_POST, TRUE);
354     
355   // compose reply-body
356   else if (is_array($REPLY_MESSAGE['parts']))
357     {
358     $body = rcmail_first_text_part($REPLY_MESSAGE['parts']);
359     if (strlen($body))
360       $body = rcmail_create_reply_body($body);
361     }
362
363   // forward message body inline
364   else if (is_array($FORWARD_MESSAGE['parts']))
365     {
366     $body = rcmail_first_text_part($FORWARD_MESSAGE['parts']);
367     if (strlen($body))
368       $body = rcmail_create_forward_body($body);
369     }
370
371   // forward message body inline
372   else if (is_array($DRAFT_MESSAGE['parts']))
373     {
374     $body = rcmail_first_text_part($DRAFT_MESSAGE['parts']);
375     if (strlen($body))
376       $body = rcmail_create_draft_body($body);
377     }
378   
379   $out = $form_start ? "$form_start\n" : '';
380
381   $saveid = new hiddenfield(array('name' => '_draft_saveid', 'value' => str_replace(array('<','>'),"",$DRAFT_MESSAGE['headers']->messageID) ));
382   $out .= $saveid->show();
383
384   $drafttoggle = new hiddenfield(array('name' => '_draft', 'value' => 'yes'));
385   $out .= $drafttoggle->show();
386
387   $out .= $textarea->show($body);
388   $out .= $form_end ? "\n$form_end" : '';
389   
390   // include GoogieSpell
391   if (!empty($CONFIG['enable_spellcheck']))
392     {
393     $OUTPUT->include_script('googiespell.js');
394     $OUTPUT->add_script(sprintf("var googie = new GoogieSpell('\$__skin_path/images/googiespell/','%s&_action=spell&lang=');\n".
395                                 "googie.lang_chck_spell = \"%s\";\n".
396                                 "googie.lang_rsm_edt = \"%s\";\n".
397                                 "googie.lang_close = \"%s\";\n".
398                                 "googie.lang_revert = \"%s\";\n".
399                                 "googie.lang_no_error_found = \"%s\";\n".
400                                 "googie.decorateTextarea('%s');\n".
401                                 "%s.set_env('spellcheck', googie);",
402                                 $GLOBALS['COMM_PATH'],
403                                 rep_specialchars_output(rcube_label('checkspelling')),
404                                 rep_specialchars_output(rcube_label('resumeediting')),
405                                 rep_specialchars_output(rcube_label('close')),
406                                 rep_specialchars_output(rcube_label('revertto')),
407                                 rep_specialchars_output(rcube_label('nospellerrors')),
408                                 $attrib['id'],
409                                 $JS_OBJECT_NAME), 'foot');
410
411     rcube_add_label('checking');
412     }
413  
414   $out .= "\n".'<iframe name="savetarget" src="program/blank.gif" style="width:0;height:0;visibility:hidden;"></iframe>';
415
416   return $out;
417   }
418
419
420 function rcmail_create_reply_body($body)
421   {
422   global $IMAP, $REPLY_MESSAGE;
423
424   // soft-wrap message first
425   $body = wordwrap($body, 75);
426   
427   // split body into single lines
428   $a_lines = preg_split('/\r?\n/', $body);
429   
430   // add > to each line
431   for($n=0; $n<sizeof($a_lines); $n++)
432     {
433     if (strpos($a_lines[$n], '>')===0)
434       $a_lines[$n] = '>'.$a_lines[$n];
435     else
436       $a_lines[$n] = '> '.$a_lines[$n];
437     }
438  
439   $body = join("\n", $a_lines);
440
441   // add title line
442   $pefix = sprintf("\n\n\nOn %s, %s wrote:\n",
443            $REPLY_MESSAGE['headers']->date,
444            $IMAP->decode_header($REPLY_MESSAGE['headers']->from));
445            
446
447   // try to remove the signature
448   if ($sp = strrpos($body, '-- '))
449     {
450     if ($body{$sp+3}==' ' || $body{$sp+3}=="\n" || $body{$sp+3}=="\r")
451       $body = substr($body, 0, $sp-1);
452     }
453
454   return $pefix.$body;
455   }
456
457
458 function rcmail_create_forward_body($body)
459   {
460   global $IMAP, $FORWARD_MESSAGE;
461
462   // soft-wrap message first
463   $body = wordwrap($body, 80);
464   
465   $prefix = sprintf("\n\n\n-------- Original Message --------\nSubject: %s\nDate: %s\nFrom: %s\nTo: %s\n\n",
466                    $FORWARD_MESSAGE['subject'],
467                    $FORWARD_MESSAGE['headers']->date,
468                    $IMAP->decode_header($FORWARD_MESSAGE['headers']->from),
469                    $IMAP->decode_header($FORWARD_MESSAGE['headers']->to));
470
471   // add attachments
472   if (!isset($_SESSION['compose']['forward_attachments']) && is_array($FORWARD_MESSAGE['parts']) && sizeof($FORWARD_MESSAGE['parts'])>1)
473     {
474     $temp_dir = rcmail_create_compose_tempdir();
475
476     if (!is_array($_SESSION['compose']['attachments']))
477       $_SESSION['compose']['attachments'] = array();
478   
479     foreach ($FORWARD_MESSAGE['parts'] as $part)
480       {
481       if ($part->disposition=='attachment' || $part->disposition=='inline' || $part->headers['content-id'] ||
482                (empty($part->disposition) && ($part->d_parameters['filename'] || $part->ctype_parameters['name'])))
483         {
484         $tmp_path = tempnam($temp_dir, 'rcmAttmnt');
485         if ($fp = fopen($tmp_path, 'w'))
486           {
487           fwrite($fp, $IMAP->mime_decode($part->body, $part->headers['content-transfer-encoding']));
488           fclose($fp);
489
490           if ($part->d_parameters['filename'])
491             $_SESSION['compose']['attachments'][] = array('name' => $part->d_parameters['filename'],
492                                                           'mimetype' => $part->ctype_primary . '/' . $part->ctype_secondary,
493                                                           'path' => $tmp_path);
494                                    
495           else if ($part->ctype_parameters['name'])
496             $_SESSION['compose']['attachments'][] = array('name' => $part->ctype_parameters['name'],
497                                                           'mimetype' => $part->ctype_primary . '/' . $part->ctype_secondary,
498                                                           'path' => $tmp_path);
499                                                           
500           else if ($part->headers['content-description'])
501             $_SESSION['compose']['attachments'][] = array('name' => $part->headers['content-description'],
502                                                           'mimetype' => $part->ctype_primary . '/' . $part->ctype_secondary,
503                                                           'path' => $tmp_path);
504           }
505         }
506       }
507
508     $_SESSION['compose']['forward_attachments'] = TRUE;
509     }
510
511   return $prefix.$body;
512   }
513
514 function rcmail_create_draft_body($body)
515   {
516   global $IMAP, $DRAFT_MESSAGE;
517     
518   // add attachments
519   if (!isset($_SESSION['compose']['forward_attachments']) && is_array($DRAFT_MESSAGE['parts']) && sizeof($DRAFT_MESSAGE['parts'])>1)
520     { 
521     $temp_dir = rcmail_create_compose_tempdir();
522
523     if (!is_array($_SESSION['compose']['attachments']))
524       $_SESSION['compose']['attachments'] = array();
525   
526     foreach ($DRAFT_MESSAGE['parts'] as $part)
527       {
528       if ($part->disposition=='attachment' || $part->disposition=='inline' || $part->headers['content-id'] ||
529                (empty($part->disposition) && ($part->d_parameters['filename'] || $part->ctype_parameters['name'])))
530         {
531         $tmp_path = tempnam($temp_dir, 'rcmAttmnt');
532         if ($fp = fopen($tmp_path, 'w'))
533           {                     
534           fwrite($fp, $IMAP->mime_decode($part->body, $part->headers['content-transfer-encoding']));
535           fclose($fp);          
536                                 
537           if ($part->d_parameters['filename'])
538             $_SESSION['compose']['attachments'][] = array('name' => $part->d_parameters['filename'],
539                                                           'mimetype' => $part->ctype_primary . '/' . $part->ctype_secondary,
540                                                           'path' => $tmp_path);
541
542           else if ($part->ctype_parameters['name'])
543             $_SESSION['compose']['attachments'][] = array('name' => $part->ctype_parameters['name'],
544                                                           'mimetype' => $part->ctype_primary . '/' . $part->ctype_secondary,
545                                                           'path' => $tmp_path);
546
547           else if ($part->headers['content-description'])
548             $_SESSION['compose']['attachments'][] = array('name' => $part->headers['content-description'],
549                                                           'mimetype' => $part->ctype_primary . '/' . $part->ctype_secondary,
550                                                           'path' => $tmp_path);
551           }
552         }
553       }
554
555     $_SESSION['compose']['forward_attachments'] = TRUE;
556     }
557
558   return $body;
559   }
560
561
562 function rcmail_compose_subject($attrib)
563   {
564   global $CONFIG, $REPLY_MESSAGE, $FORWARD_MESSAGE, $DRAFT_MESSAGE;
565   
566   list($form_start, $form_end) = get_form_tags($attrib);
567   unset($attrib['form']);
568   
569   $attrib['name'] = '_subject';
570   $textfield = new textfield($attrib);
571
572   $subject = '';
573
574   // use subject from post
575   if (isset($_POST['_subject']))
576     $subject = get_input_value('_subject', RCUBE_INPUT_POST, TRUE);
577     
578   // create a reply-subject
579   else if (isset($REPLY_MESSAGE['subject']))
580     {
581     if (eregi('^re:', $REPLY_MESSAGE['subject']))
582       $subject = $REPLY_MESSAGE['subject'];
583     else
584       $subject = 'Re: '.$REPLY_MESSAGE['subject'];
585     }
586
587   // create a forward-subject
588   else if (isset($FORWARD_MESSAGE['subject']))
589     {
590     if (eregi('^fwd:', $REPLY_MESSAGE['subject']))
591       $subject = $FORWARD_MESSAGE['subject'];
592     else
593       $subject = 'Fwd: '.$FORWARD_MESSAGE['subject'];
594     }
595
596   // creeate a draft-subject
597   else if (isset($DRAFT_MESSAGE['subject']))
598     $subject = $DRAFT_MESSAGE['subject'];
599   
600   $out = $form_start ? "$form_start\n" : '';
601   $out .= $textfield->show($subject);
602   $out .= $form_end ? "\n$form_end" : '';
603          
604   return $out;
605   }
606
607
608 function rcmail_compose_attachment_list($attrib)
609   {
610   global $OUTPUT, $JS_OBJECT_NAME;
611   
612   // add ID if not given
613   if (!$attrib['id'])
614     $attrib['id'] = 'rcmAttachmentList';
615   
616   // allow the following attributes to be added to the <ul> tag
617   $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style'));
618  
619   $out = '<ul'. $attrib_str . ">\n";
620   
621   if (is_array($_SESSION['compose']['attachments']))
622     {
623     if ($attrib['deleteicon'])
624       $button = sprintf('<img src="%s%s" alt="%s" border="0" style="padding-right:2px;vertical-align:middle" />',
625                         $CONFIG['skin_path'],
626                         $attrib['deleteicon'],
627                         rcube_label('delete'));
628     else
629       $button = rcube_label('delete');
630
631     foreach ($_SESSION['compose']['attachments'] as $id => $a_prop)
632       $out .= sprintf('<li id="rcmfile%d"><a href="#delete" onclick="return %s.command(\'remove-attachment\',\'rcmfile%d\', this)" title="%s">%s</a>%s</li>',
633                       $id,
634                       $JS_OBJECT_NAME,
635                       $id,
636                       rcube_label('delete'), 
637                       $button,
638                       rep_specialchars_output($a_prop['name']));
639     }
640
641   $OUTPUT->add_script(sprintf("%s.gui_object('attachmentlist', '%s');", $JS_OBJECT_NAME, $attrib['id']));  
642     
643   $out .= '</ul>';
644   return $out;
645   }
646
647
648
649 function rcmail_compose_attachment_form($attrib)
650   {
651   global $OUTPUT, $JS_OBJECT_NAME, $SESS_HIDDEN_FIELD;
652
653   // add ID if not given
654   if (!$attrib['id'])
655     $attrib['id'] = 'rcmUploadbox';
656   
657   // allow the following attributes to be added to the <div> tag
658   $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style'));
659   $input_field = rcmail_compose_attachment_field(array());
660   $label_send = rcube_label('upload');
661   $label_close = rcube_label('close');
662   
663   $out = <<<EOF
664 <div$attrib_str>
665 <form action="./" method="post" enctype="multipart/form-data">
666 $SESS_HIDDEN_FIELD
667 $input_field<br />
668 <input type="button" value="$label_close" class="button" onclick="document.getElementById('$attrib[id]').style.visibility='hidden'" />
669 <input type="button" value="$label_send" class="button" onclick="$JS_OBJECT_NAME.command('send-attachment', this.form)" />
670 </form>
671 </div>
672 EOF;
673
674   
675   $OUTPUT->add_script(sprintf("%s.gui_object('uploadbox', '%s');", $JS_OBJECT_NAME, $attrib['id']));  
676   return $out;
677   }
678
679
680 function rcmail_compose_attachment_field($attrib)
681   {
682   // allow the following attributes to be added to the <input> tag
683   $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style', 'size'));
684  
685   $out = '<input type="file" name="_attachments[]"'. $attrib_str . " />";
686   return $out;
687   }
688
689
690 function rcmail_priority_selector($attrib)
691   {
692   list($form_start, $form_end) = get_form_tags($attrib);
693   unset($attrib['form']);
694   
695   $attrib['name'] = '_priority';
696   $selector = new select($attrib);
697
698   $selector->add(array(rcube_label('lowest'),
699                        rcube_label('low'),
700                        rcube_label('normal'),
701                        rcube_label('high'),
702                        rcube_label('highest')),
703                  array(5, 4, 0, 2, 1));
704                  
705   $sel = isset($_POST['_priority']) ? $_POST['_priority'] : 0;
706
707   $out = $form_start ? "$form_start\n" : '';
708   $out .= $selector->show($sel);
709   $out .= $form_end ? "\n$form_end" : '';
710          
711   return $out;
712   }
713
714
715 function rcmail_receipt_checkbox($attrib)
716   {
717   list($form_start, $form_end) = get_form_tags($attrib);
718   unset($attrib['form']);
719   
720   if (!isset($attrib['id']))
721     $attrib['id'] = 'receipt';  
722
723   $attrib['name'] = '_receipt';
724   $attrib['value'] = '1';
725   $checkbox = new checkbox($attrib);
726
727   $out = $form_start ? "$form_start\n" : '';
728   $out .= $checkbox->show(0);
729   $out .= $form_end ? "\n$form_end" : '';
730
731   return $out;
732   }
733
734
735 function get_form_tags($attrib)
736   {
737   global $CONFIG, $OUTPUT, $JS_OBJECT_NAME, $MESSAGE_FORM, $SESS_HIDDEN_FIELD;  
738
739   $form_start = '';
740   if (!strlen($MESSAGE_FORM))
741     {
742     $hiddenfields = new hiddenfield(array('name' => '_task', 'value' => $GLOBALS['_task']));
743     $hiddenfields->add(array('name' => '_action', 'value' => 'send'));
744
745     $form_start = empty($attrib['form']) ? '<form name="form" action="./" method="post">' : '';
746     $form_start .= "\n$SESS_HIDDEN_FIELD\n";
747     $form_start .= $hiddenfields->show();
748     }
749     
750   $form_end = (strlen($MESSAGE_FORM) && !strlen($attrib['form'])) ? '</form>' : '';
751   $form_name = !empty($attrib['form']) ? $attrib['form'] : 'form';
752   
753   if (!strlen($MESSAGE_FORM))
754     $OUTPUT->add_script("$JS_OBJECT_NAME.gui_object('messageform', '$form_name');");
755   
756   $MESSAGE_FORM = $form_name;
757
758   return array($form_start, $form_end);  
759   }
760
761
762 function format_email_recipient($email, $name='')
763   {
764   if ($name && $name != $email)
765     return sprintf('%s <%s>', strpos($name, ",") ? '"'.$name.'"' : $name, $email);
766   else
767     return $email;
768   }
769
770
771 function rcmail_charset_pulldown($selected='ISO-8859-1')
772   {
773   $select = new select();
774   
775   
776   return $select->show($selected);
777   }
778
779
780 /****** get contacts for this user and add them to client scripts ********/
781
782 $sql_result = $DB->query("SELECT name, email
783                           FROM ".get_table_name('contacts')." WHERE  user_id=?
784                           AND  del<>1",$_SESSION['user_id']);
785                                    
786 if ($DB->num_rows($sql_result))
787   {        
788   $a_contacts = array();
789   while ($sql_arr = $DB->fetch_assoc($sql_result))
790     if ($sql_arr['email'])
791       $a_contacts[] = format_email_recipient($sql_arr['email'], rep_specialchars_output($sql_arr['name'], 'js'));
792   
793   $OUTPUT->add_script(sprintf("$JS_OBJECT_NAME.set_env('contacts', %s);", array2js($a_contacts)));
794   }
795
796
797 parse_template('compose');
798 ?>