]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/mail/compose.inc
Imported Upstream version 0.2~alpha
[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-2008, 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 1493 2008-06-08 18:17:09Z alec $
19
20 */
21
22 // define constants for message compose mode
23 define('RCUBE_COMPOSE_REPLY', 0x0106);
24 define('RCUBE_COMPOSE_FORWARD', 0x0107);
25 define('RCUBE_COMPOSE_DRAFT', 0x0108);
26
27
28 // remove an attachment
29 if ($RCMAIL->action=='remove-attachment' && preg_match('/^rcmfile([0-9]+)$/', $_POST['_file'], $regs))
30 {
31   $id = $regs[1];
32   if (is_array($_SESSION['compose']['attachments'][$id]))
33   {
34     @unlink($_SESSION['compose']['attachments'][$id]['path']);
35     $_SESSION['compose']['attachments'][$id] = NULL;
36     $OUTPUT->command('remove_from_attachment_list', "rcmfile$id");
37     $OUTPUT->send();
38     exit;
39   }
40 }
41
42 if ($RCMAIL->action=='display-attachment' && preg_match('/^rcmfile([0-9]+)$/', $_GET['_file'], $regs))
43 {
44   $id = $regs[1];
45   if (is_array($_SESSION['compose']['attachments'][$id]))
46   {
47     $apath = $_SESSION['compose']['attachments'][$id]['path'];
48     header('Content-Type: ' . $_SESSION['compose']['attachments'][$id]['mimetype']);
49     header('Content-Length: ' . filesize($apath));
50     readfile($apath);
51   }
52   exit;
53 }
54
55 $MESSAGE_FORM = NULL;
56 $MESSAGE = NULL;
57
58 // Nothing below is called during message composition, only at "new/forward/reply/draft" initialization or
59 // if a compose-ID is given (i.e. when the compose step is opened in a new window/tab).
60 // Since there are many ways to leave the compose page improperly, it seems necessary to clean-up an old
61 // compose when a "new/forward/reply/draft" is called - otherwise the old session attachments will appear
62
63 if (!is_array($_SESSION['compose']) || $_SESSION['compose']['id'] != get_input_value('_id', RCUBE_INPUT_GET))
64 {
65   rcmail_compose_cleanup();
66   $_SESSION['compose'] = array('id' => uniqid(rand()));
67 }
68
69 // add some labels to client
70 rcube_add_label('nosubject', 'norecipientwarning', 'nosubjectwarning', 'nobodywarning', 'notsentwarning', 'savingmessage', 'sendingmessage', 'messagesaved', 'converting');
71
72 // add config parameter to client script
73 $OUTPUT->set_env('draft_autosave', !empty($CONFIG['drafts_mbox']) ? $CONFIG['draft_autosave'] : 0);
74
75
76 // get reference message and set compose mode
77 if ($msg_uid = get_input_value('_reply_uid', RCUBE_INPUT_GET))
78   $compose_mode = RCUBE_COMPOSE_REPLY;
79 else if ($msg_uid = get_input_value('_forward_uid', RCUBE_INPUT_GET))
80   $compose_mode = RCUBE_COMPOSE_FORWARD;
81 else if ($msg_uid = get_input_value('_draft_uid', RCUBE_INPUT_GET))
82   $compose_mode = RCUBE_COMPOSE_DRAFT;
83
84 if (!empty($msg_uid))
85 {
86   // similar as in program/steps/mail/show.inc
87   $MESSAGE = new rcube_message($msg_uid);
88   
89   if (!empty($MESSAGE->headers->charset))
90     $IMAP->set_charset($MESSAGE->headers->charset);
91     
92   if ($compose_mode == RCUBE_COMPOSE_REPLY)
93   {
94     $_SESSION['compose']['reply_uid'] = $msg_uid;
95     $_SESSION['compose']['reply_msgid'] = $MESSAGE->headers->messageID;
96     $_SESSION['compose']['references']  = trim($MESSAGE->headers->references . " " . $MESSAGE->headers->messageID);
97
98     if (!empty($_GET['_all']))
99       $MESSAGE->reply_all = 1;
100   }
101   else if ($compose_mode == RCUBE_COMPOSE_FORWARD)
102   {
103     $_SESSION['compose']['forward_uid'] = $msg_uid;
104   }
105   else if ($compose_mode == RCUBE_COMPOSE_DRAFT)
106   {
107     $_SESSION['compose']['draft_uid'] = $msg_uid;
108   }
109 }
110
111 /****** compose mode functions ********/
112
113
114 function rcmail_compose_headers($attrib)
115 {
116   global $IMAP, $MESSAGE, $DB, $compose_mode;
117   static $sa_recipients = array();
118
119   list($form_start, $form_end) = get_form_tags($attrib);
120   
121   $out = '';
122   $part = strtolower($attrib['part']);
123   
124   switch ($part)
125   {
126     case 'from':
127       return rcmail_compose_header_from($attrib);
128
129     case 'to':
130       $fname = '_to';
131       $header = 'to';
132       
133       // we have a set of recipients stored is session
134       if (($mailto_id = get_input_value('_mailto', RCUBE_INPUT_GET)) && $_SESSION['mailto'][$mailto_id])
135         $fvalue = $_SESSION['mailto'][$mailto_id];
136       else if (!empty($_GET['_to']))
137         $fvalue = get_input_value('_to', RCUBE_INPUT_GET);
138         
139     case 'cc':
140       if (!$fname)
141       {
142         $fname = '_cc';
143         $header = 'cc';
144       }
145     case 'bcc':
146       if (!$fname)
147       {
148         $fname = '_bcc';
149         $header = 'bcc';
150       }
151         
152       $allow_attrib = array('id', 'class', 'style', 'cols', 'rows', 'tabindex');
153       $field_type = 'html_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 = 'html_inputfield';
161       break;    
162   }
163  
164   if ($fname && !empty($_POST[$fname]))
165     $fvalue = get_input_value($fname, RCUBE_INPUT_POST, TRUE);
166
167   else if ($header && $compose_mode == RCUBE_COMPOSE_REPLY)
168   {
169     // get recipent address(es) out of the message headers
170     if ($header=='to' && !empty($MESSAGE->headers->replyto))
171       $fvalue = $MESSAGE->headers->replyto;
172
173     else if ($header=='to' && !empty($MESSAGE->headers->from))
174       $fvalue = $MESSAGE->headers->from;
175
176     // add recipent of original message if reply to all
177     else if ($header=='cc' && !empty($MESSAGE->reply_all))
178     {
179       if ($v = $MESSAGE->headers->to)
180         $fvalue .= $v;
181
182       if ($v = $MESSAGE->headers->cc)
183         $fvalue .= (!empty($fvalue) ? ', ' : '') . $v;
184     }
185
186     // split recipients and put them back together in a unique way
187     if (!empty($fvalue))
188     {
189       $to_addresses = $IMAP->decode_address_list($fvalue);
190       $fvalue = '';
191       foreach ($to_addresses as $addr_part)
192       {
193         if (!empty($addr_part['mailto']) && !in_array($addr_part['mailto'], $sa_recipients) && (!$MESSAGE->compose_from || !in_array($addr_part['mailto'], $MESSAGE->compose_from)))
194         {
195           $fvalue .= (strlen($fvalue) ? ', ':'').$addr_part['string'];
196           $sa_recipients[] = $addr_part['mailto'];
197         }
198       }
199     }
200   }
201   else if ($header && $compose_mode == RCUBE_COMPOSE_DRAFT)
202   {
203     // get drafted headers
204     if ($header=='to' && !empty($MESSAGE->headers->to))
205       $fvalue = $MESSAGE->get_header('to');
206
207     if ($header=='cc' && !empty($MESSAGE->headers->cc))
208       $fvalue = $MESSAGE->get_header('cc');
209
210     if ($header=='bcc' && !empty($MESSAGE->headers->bcc))
211       $fvalue = $MESSAGE->get_header('bcc');
212   }
213
214         
215   if ($fname && $field_type)
216   {
217     // pass the following attributes to the form class
218     $field_attrib = array('name' => $fname);
219     foreach ($attrib as $attr => $value)
220       if (in_array($attr, $allow_attrib))
221         $field_attrib[$attr] = $value;
222
223     // create teaxtarea object
224     $input = new $field_type($field_attrib);
225     $out = $input->show($fvalue);
226   }
227   
228   if ($form_start)
229     $out = $form_start.$out;
230
231   return $out;  
232 }
233
234
235
236 function rcmail_compose_header_from($attrib)
237 {
238   global $IMAP, $MESSAGE, $DB, $USER, $OUTPUT, $compose_mode;
239     
240   // pass the following attributes to the form class
241   $field_attrib = array('name' => '_from');
242   foreach ($attrib as $attr => $value)
243     if (in_array($attr, array('id', 'class', 'style', 'size', 'tabindex')))
244       $field_attrib[$attr] = $value;
245
246   // extract all recipients of the reply-message
247   $a_recipients = array();
248   if ($compose_mode == RCUBE_COMPOSE_REPLY && is_object($MESSAGE->headers))
249   {
250     $MESSAGE->compose_from = array();
251
252     $a_to = $IMAP->decode_address_list($MESSAGE->headers->to);
253     foreach ($a_to as $addr)
254     {
255       if (!empty($addr['mailto']))
256         $a_recipients[] = $addr['mailto'];
257     }
258
259     if (!empty($MESSAGE->headers->cc))
260     {
261       $a_cc = $IMAP->decode_address_list($MESSAGE->headers->cc);
262       foreach ($a_cc as $addr)
263       {
264         if (!empty($addr['mailto']))
265           $a_recipients[] = $addr['mailto'];
266       }
267     }
268   }
269
270   // get this user's identities
271   $sql_result = $USER->list_identities();
272
273   if ($DB->num_rows($sql_result))
274   {
275     $from_id = 0;
276     $a_signatures = array();
277
278     $field_attrib['onchange'] = JS_OBJECT_NAME.".change_identity(this)";
279     $select_from = new html_select($field_attrib);
280
281     while ($sql_arr = $DB->fetch_assoc($sql_result))
282     {
283       $identity_id = $sql_arr['identity_id'];
284       $select_from->add(format_email_recipient($sql_arr['email'], $sql_arr['name']), $identity_id);
285
286       // add signature to array
287       if (!empty($sql_arr['signature']))
288       {
289         $a_signatures[$identity_id]['text'] = $sql_arr['signature'];
290         $a_signatures[$identity_id]['is_html'] = ($sql_arr['html_signature'] == 1) ? true : false;
291         if ($a_signatures[$identity_id]['is_html'])
292         {
293             $h2t = new html2text($a_signatures[$identity_id]['text'], false, false);
294             $plainTextPart = $h2t->get_text();
295             $a_signatures[$identity_id]['plain_text'] = trim(html_entity_decode($plainTextPart, ENT_NOQUOTES, 'UTF-8'));
296         }
297       }
298
299       // set identity if it's one of the reply-message recipients
300       if (in_array($sql_arr['email'], $a_recipients))
301         $from_id = $sql_arr['identity_id'];
302
303       if ($compose_mode == RCUBE_COMPOSE_REPLY && is_array($MESSAGE->compose_from))
304         $MESSAGE->compose_from[] = $sql_arr['email'];
305
306       if ($compose_mode == RCUBE_COMPOSE_DRAFT && strstr($MESSAGE->headers->from, $sql_arr['email']))
307         $from_id = $sql_arr['identity_id'];
308     }
309
310     // overwrite identity selection with post parameter
311     if (isset($_POST['_from']))
312       $from_id = get_input_value('_from', RCUBE_INPUT_POST);
313
314     $out = $select_from->show($from_id);
315
316     // add signatures to client
317     $OUTPUT->set_env('signatures', $a_signatures);
318   }
319   else
320   {
321     $input_from = new html_inputfield($field_attrib);
322     $out = $input_from->show($_POST['_from']);
323   }
324   
325   if ($form_start)
326     $out = $form_start.$out;
327
328   return $out;
329 }
330
331
332 function rcmail_compose_body($attrib)
333 {
334   global $RCMAIL, $CONFIG, $OUTPUT, $MESSAGE, $compose_mode;
335   
336   list($form_start, $form_end) = get_form_tags($attrib);
337   unset($attrib['form']);
338   
339   if (empty($attrib['id']))
340     $attrib['id'] = 'rcmComposeMessage';
341
342   $attrib['name'] = '_message';
343
344   if ($CONFIG['htmleditor'])
345     $isHtml = true;
346   else
347     $isHtml = false;
348
349   $body = '';
350
351   // use posted message body
352   if (!empty($_POST['_message']))
353   {
354     $body = get_input_value('_message', RCUBE_INPUT_POST, true);
355   }
356   else if ($compose_mode)
357   {
358     if ($isHtml && $MESSAGE->has_html_part())
359     {
360       $body = $MESSAGE->first_html_part();
361       $isHtml = true;
362     }
363     else
364     {
365       $body = $MESSAGE->first_text_part();
366       $isHtml = false;
367     }
368     
369     // compose reply-body
370     if ($compose_mode == RCUBE_COMPOSE_REPLY)
371       $body = rcmail_create_reply_body($body, $isHtml);
372     // forward message body inline
373     else if ($compose_mode == RCUBE_COMPOSE_FORWARD)
374       $body = rcmail_create_forward_body($body, $isHtml);
375     // load draft message body
376     else if ($compose_mode == RCUBE_COMPOSE_DRAFT)
377       $body = rcmail_create_draft_body($body, $isHtml);
378   }
379
380   $tinylang = substr($_SESSION['language'], 0, 2);
381   if (!file_exists('program/js/tiny_mce/langs/'.$tinylang.'.js'))
382     $tinylang = 'en'; 
383
384   $OUTPUT->include_script('tiny_mce/tiny_mce.js');
385   $OUTPUT->include_script("editor.js");
386   $OUTPUT->add_script('rcmail_editor_init("$__skin_path", "'.$tinylang.'");');
387
388   $out = $form_start ? "$form_start\n" : '';
389
390   $saveid = new html_hiddenfield(array('name' => '_draft_saveid', 'value' => $compose_mode==RCUBE_COMPOSE_DRAFT ? str_replace(array('<','>'), "", $MESSAGE->headers->messageID) : ''));
391   $out .= $saveid->show();
392
393   $drafttoggle = new html_hiddenfield(array('name' => '_draft', 'value' => 'yes'));
394   $out .= $drafttoggle->show();
395
396   $msgtype = new html_hiddenfield(array('name' => '_is_html', 'value' => ($isHtml?"1":"0")));
397   $out .= $msgtype->show();
398
399   // If desired, set this text area to be editable by TinyMCE
400   if ($isHtml) $attrib['class'] = "mce_editor";
401   $textarea = new html_textarea($attrib);
402   $out .= $textarea->show($body);
403   $out .= $form_end ? "\n$form_end" : '';
404
405   // include GoogieSpell
406   if (!empty($CONFIG['enable_spellcheck']) && !$isHtml)
407     {
408     $lang_set = '';
409     if (!empty($CONFIG['spellcheck_languages']) && is_array($CONFIG['spellcheck_languages']))
410       $lang_set = "googie.setLanguages(".array2js($CONFIG['spellcheck_languages']).");\n";
411     
412     $OUTPUT->include_script('googiespell.js');
413     $OUTPUT->add_script(sprintf(
414       "var googie = new GoogieSpell('\$__skin_path/images/googiespell/','%s&_action=spell&lang=');\n".
415       "googie.lang_chck_spell = \"%s\";\n".
416       "googie.lang_rsm_edt = \"%s\";\n".
417       "googie.lang_close = \"%s\";\n".
418       "googie.lang_revert = \"%s\";\n".
419       "googie.lang_no_error_found = \"%s\";\n%s".
420       "googie.setCurrentLanguage('%s');\n".
421       "googie.decorateTextarea('%s');\n".
422       "%s.set_env('spellcheck', googie);",
423       $RCMAIL->comm_path,
424       JQ(Q(rcube_label('checkspelling'))),
425       JQ(Q(rcube_label('resumeediting'))),
426       JQ(Q(rcube_label('close'))),
427       JQ(Q(rcube_label('revertto'))),
428       JQ(Q(rcube_label('nospellerrors'))),
429       $lang_set,
430       substr($_SESSION['language'], 0, 2),
431       $attrib['id'],
432       JS_OBJECT_NAME), 'foot');
433
434     rcube_add_label('checking');
435     }
436  
437   $out .= "\n".'<iframe name="savetarget" src="program/blank.gif" style="width:0;height:0;border:none;visibility:hidden;"></iframe>';
438
439   return $out;
440 }
441
442
443 function rcmail_create_reply_body($body, $bodyIsHtml)
444 {
445   global $IMAP, $MESSAGE;
446
447   if (! $bodyIsHtml)
448   {
449     // soft-wrap message first
450     $body = wordwrap($body, 75);
451   
452     // split body into single lines
453     $a_lines = preg_split('/\r?\n/', $body);
454   
455     // add > to each line
456     for($n=0; $n<sizeof($a_lines); $n++)
457     {
458       if (strpos($a_lines[$n], '>')===0)
459         $a_lines[$n] = '>'.$a_lines[$n];
460       else
461         $a_lines[$n] = '> '.$a_lines[$n];
462     }
463  
464     $body = join("\n", $a_lines);
465
466     // add title line
467     $prefix = sprintf("\n\n\nOn %s, %s wrote:\n",
468       $MESSAGE->headers->date,
469       $MESSAGE->get_header('from'));
470
471     // try to remove the signature
472     if ($sp = strrpos($body, '-- '))
473       {
474       if ($body{$sp+3}==' ' || $body{$sp+3}=="\n" || $body{$sp+3}=="\r")
475         $body = substr($body, 0, $sp-1);
476       }
477     $suffix = '';
478   }
479   else
480   {
481     $prefix = sprintf("<br /><br />On %s, %s wrote:<br />\n",
482       $MESSAGE->headers->date,
483       Q($MESSAGE->get_header('from')));
484     $prefix .= '<blockquote type="cite" style="padding-left:5px; border-left:#1010ff 2px solid; margin-left:5px; width:100%">';
485     $suffix = "</blockquote>";
486   }
487
488   return $prefix.$body.$suffix;
489 }
490
491
492 function rcmail_create_forward_body($body, $bodyIsHtml)
493 {
494   global $IMAP, $MESSAGE;
495
496   if (!$bodyIsHtml)
497   {
498     // soft-wrap message first
499     $body = wordwrap($body, 80);
500   
501     $prefix = sprintf("\n\n\n-------- Original Message --------\nSubject: %s\nDate: %s\nFrom: %s\nTo: %s\n\n",
502       $MESSAGE->subject,
503       $MESSAGE->headers->date,
504       $MESSAGE->get_header('from'),
505       $MESSAGE->get_header('to'));
506   }
507   else
508   {
509     $prefix = sprintf(
510       "<br><br>-------- Original Message --------" .
511         "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody>" .
512         "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">Subject: </th><td>%s</td></tr>" .
513         "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">Date: </th><td>%s</td></tr>" .
514         "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">From: </th><td>%s</td></tr>" .
515         "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">To: </th><td>%s</td></tr>" .
516         "</tbody></table><br>",
517       Q($MESSAGE->subject),
518       Q($MESSAGE->headers->date),
519       Q($MESSAGE->get_header('from')),
520       Q($MESSAGE->get_header('to')));
521   }
522
523   // add attachments
524   if (!isset($_SESSION['compose']['forward_attachments']) && is_array($MESSAGE->mime_parts))
525     rcmail_write_compose_attachments($MESSAGE);
526     
527   return $prefix.$body;
528 }
529
530
531 function rcmail_create_draft_body($body, $bodyIsHtml)
532 {
533   global $MESSAGE;
534   
535   /**
536    * add attachments
537    * sizeof($MESSAGE->mime_parts can be 1 - e.g. attachment, but no text!
538    */
539   if (!isset($_SESSION['compose']['forward_attachments'])
540       && is_array($MESSAGE->mime_parts)
541       && count($MESSAGE->mime_parts) > 0)
542     rcmail_write_compose_attachments($MESSAGE);
543
544   return $body;
545 }
546   
547   
548 function rcmail_write_compose_attachments(&$message)
549 {
550   global $RCMAIL, $IMAP;
551
552   $temp_dir = unslashify($RCMAIL->config->get('temp_dir'));
553
554   if (!is_array($_SESSION['compose']['attachments']))
555     $_SESSION['compose']['attachments'] = array();
556   
557   foreach ((array)$message->mime_parts as $pid => $part)
558   {
559     if ($part->ctype_primary != 'message' &&
560         ($part->disposition=='attachment' || $part->disposition=='inline' || $part->headers['content-id'] ||
561          (empty($part->disposition) && $part->filename)))
562     {
563       $tmp_path = tempnam($temp_dir, 'rcmAttmnt');
564       if ($fp = fopen($tmp_path, 'w'))
565       {
566         fwrite($fp, $message->get_part_content($pid));
567         fclose($fp);
568         
569         $_SESSION['compose']['attachments'][] = array(
570           'mimetype' => $part->ctype_primary . '/' . $part->ctype_secondary,
571           'name' => $part->filename,
572           'path' => $tmp_path
573           );
574       }
575     }
576   }
577         
578   $_SESSION['compose']['forward_attachments'] = true;
579 }
580
581
582 function rcmail_compose_subject($attrib)
583 {
584   global $MESSAGE, $compose_mode;
585   
586   list($form_start, $form_end) = get_form_tags($attrib);
587   unset($attrib['form']);
588   
589   $attrib['name'] = '_subject';
590   $textfield = new html_inputfield($attrib);
591
592   $subject = '';
593
594   // use subject from post
595   if (isset($_POST['_subject']))
596     $subject = get_input_value('_subject', RCUBE_INPUT_POST, TRUE);
597     
598   // create a reply-subject
599   else if ($compose_mode == RCUBE_COMPOSE_REPLY)
600   {
601     if (eregi('^re:', $MESSAGE->subject))
602       $subject = $MESSAGE->subject;
603     else
604       $subject = 'Re: '.$MESSAGE->subject;
605   }
606
607   // create a forward-subject
608   else if ($compose_mode == RCUBE_COMPOSE_FORWARD)
609   {
610     if (eregi('^fwd:', $MESSAGE->subject))
611       $subject = $MESSAGE->subject;
612     else
613       $subject = 'Fwd: '.$MESSAGE->subject;
614   }
615
616   // creeate a draft-subject
617   else if ($compose_mode == RCUBE_COMPOSE_DRAFT)
618     $subject = $MESSAGE->subject;
619   
620   $out = $form_start ? "$form_start\n" : '';
621   $out .= $textfield->show($subject);
622   $out .= $form_end ? "\n$form_end" : '';
623          
624   return $out;
625 }
626
627
628 function rcmail_compose_attachment_list($attrib)
629 {
630   global $OUTPUT, $CONFIG;
631   
632   // add ID if not given
633   if (!$attrib['id'])
634     $attrib['id'] = 'rcmAttachmentList';
635   
636   $out = "\n";
637   
638   if (is_array($_SESSION['compose']['attachments']))
639   {
640     if ($attrib['deleteicon'])
641       $button = html::img(array(
642         'src' => $CONFIG['skin_path'] . $attrib['deleteicon'],
643         'alt' => rcube_label('delete'),
644         'style' => "border:0;padding-right:2px;vertical-align:middle"));
645     else
646       $button = Q(rcube_label('delete'));
647
648     foreach ($_SESSION['compose']['attachments'] as $id => $a_prop)
649       $out .= html::tag('li', array('id' => "rcmfile".$id),
650         html::a(array(
651             'href' => "#delete",
652             'title' => rcube_label('delete'),
653               'onclick' => sprintf("return %s.command(\'remove-attachment\',\'rcmfile%d\', this)", JS_OBJECT_NAME, $id)),
654           $button) . Q($a_prop['name']));
655   }
656
657   $OUTPUT->add_gui_object('attachmentlist', $attrib['id']);
658     
659   return html::tag('ul', $attrib, $out, html::$common_attrib);
660 }
661
662
663 function rcmail_compose_attachment_form($attrib)
664 {
665   global $OUTPUT;
666
667   // add ID if not given
668   if (!$attrib['id'])
669     $attrib['id'] = 'rcmUploadbox';
670   
671   $button = new html_inputfield(array('type' => 'button', 'class' => 'button'));
672   
673   $out = html::div($attrib,
674     $OUTPUT->form_tag(array('name' => 'form', 'method' => 'post', 'enctype' => 'multipart/form-data')) .
675     rcmail_compose_attachment_field(array()) . html::br() .
676     $button->show(rcube_label('close'), array('onclick' => "document.getElementById('$attrib[id]').style.visibility='hidden'")) .
677     $button->show(rcube_label('upload'), array('onclick' => JS_OBJECT_NAME . ".command('send-attachment', this.form)"))
678   );
679   
680   
681   $OUTPUT->add_gui_object('uploadbox', $attrib['id']);
682   return $out;
683 }
684
685
686 function rcmail_compose_attachment_field($attrib)
687 {
688   // allow the following attributes to be added to the <input> tag
689   $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style', 'size'));
690  
691   $out = '<input type="file" name="_attachments[]"'. $attrib_str . " />";
692   return $out;
693 }
694
695
696 function rcmail_priority_selector($attrib)
697 {
698   global $MESSAGE;
699   
700   list($form_start, $form_end) = get_form_tags($attrib);
701   unset($attrib['form']);
702   
703   $attrib['name'] = '_priority';
704   $selector = new html_select($attrib);
705
706   $selector->add(array(rcube_label('lowest'),
707                        rcube_label('low'),
708                        rcube_label('normal'),
709                        rcube_label('high'),
710                        rcube_label('highest')),
711                  array(5, 4, 0, 2, 1));
712                  
713   $sel = isset($_POST['_priority']) ? $_POST['_priority'] : intval($MESSAGE->headers->priority);
714
715   $out = $form_start ? "$form_start\n" : '';
716   $out .= $selector->show($sel);
717   $out .= $form_end ? "\n$form_end" : '';
718          
719   return $out;
720 }
721
722
723 function rcmail_receipt_checkbox($attrib)
724 {
725   global $MESSAGE, $compose_mode;
726   
727   list($form_start, $form_end) = get_form_tags($attrib);
728   unset($attrib['form']);
729   
730   if (!isset($attrib['id']))
731     $attrib['id'] = 'receipt';  
732
733   $attrib['name'] = '_receipt';
734   $attrib['value'] = '1';
735   $checkbox = new html_checkbox($attrib);
736
737   $out = $form_start ? "$form_start\n" : '';
738   $out .= $checkbox->show(
739     $compose_mode == RCUBE_COMPOSE_DRAFT && $MESSAGE->headers->mdn_to ? 1 : 0);
740   $out .= $form_end ? "\n$form_end" : '';
741
742   return $out;
743 }
744
745
746 function rcmail_editor_selector($attrib)
747 {
748   global $CONFIG, $MESSAGE, $compose_mode;
749
750   $choices = array(
751     'html'  => 'htmltoggle',
752     'plain' => 'plaintoggle'
753   );
754
755   // determine whether HTML or plain text should be checked
756   $useHtml = $CONFIG['htmleditor'] ? true : false;
757
758   if ($compose_mode)
759     $useHtml = ($useHtml && $MESSAGE->has_html_part());
760
761   $selector = '';
762   $chosenvalue = $useHtml ? 'html' : 'plain';
763   $radio = new html_radiobutton(array('name' => '_editorSelect', 'onclick' => 'return rcmail_toggle_editor(this)'));
764   foreach ($choices as $value => $text)
765   {
766     $attrib['id'] = '_' . $value;
767     $attrib['value'] = $value;
768     $selector .= $radio->show($chosenvalue, $attrib) . html::label($attrib['id'], Q(rcube_label($text)));
769   }
770
771   return $selector;
772 }
773
774
775 function get_form_tags($attrib)
776 {
777   global $RCMAIL, $MESSAGE_FORM;
778
779   $form_start = '';
780   if (!strlen($MESSAGE_FORM))
781   {
782     $hiddenfields = new html_hiddenfield(array('name' => '_task', 'value' => $RCMAIL->task));
783     $hiddenfields->add(array('name' => '_action', 'value' => 'send'));
784
785     $form_start = empty($attrib['form']) ? $RCMAIL->output->form_tag(array('name' => "form", 'method' => "post")) : '';
786     $form_start .= $hiddenfields->show();
787   }
788     
789   $form_end = (strlen($MESSAGE_FORM) && !strlen($attrib['form'])) ? '</form>' : '';
790   $form_name = !empty($attrib['form']) ? $attrib['form'] : 'form';
791   
792   if (!strlen($MESSAGE_FORM))
793     $RCMAIL->output->add_gui_object('messageform', $form_name);
794   
795   $MESSAGE_FORM = $form_name;
796
797   return array($form_start, $form_end);
798 }
799
800
801 // register UI objects
802 $OUTPUT->add_handlers(array(
803   'composeheaders' => 'rcmail_compose_headers',
804   'composesubject' => 'rcmail_compose_subject',
805   'composebody' => 'rcmail_compose_body',
806   'composeattachmentlist' => 'rcmail_compose_attachment_list',
807   'composeattachmentform' => 'rcmail_compose_attachment_form',
808   'composeattachment' => 'rcmail_compose_attachment_field',
809   'priorityselector' => 'rcmail_priority_selector',
810   'editorselector' => 'rcmail_editor_selector',
811   'receiptcheckbox' => 'rcmail_receipt_checkbox',
812 ));
813
814 /****** get contacts for this user and add them to client scripts ********/
815
816 $CONTACTS = new rcube_contacts($DB, $USER->ID);
817 $CONTACTS->set_pagesize(1000);
818
819 $a_contacts = array(); 
820                                    
821 if ($result = $CONTACTS->list_records())
822   {
823   while ($sql_arr = $result->iterate())
824     if ($sql_arr['email'])
825       $a_contacts[] = format_email_recipient($sql_arr['email'], $sql_arr['name']);
826   }
827 if (isset($CONFIG['ldap_public']))
828   {
829   /* LDAP autocompletion */ 
830   foreach ($CONFIG['ldap_public'] as $ldapserv_config) 
831     { 
832     if ($ldapserv_config['fuzzy_search'] != 1 || 
833         $ldapserv_config['global_search'] != 1)
834       { 
835       continue; 
836       }
837          
838     $LDAP = new rcube_ldap($ldapserv_config); 
839     $LDAP->connect(); 
840     $LDAP->set_pagesize(1000);
841   
842     $results = $LDAP->search($ldapserv_config['mail_field'], ""); 
843  
844     for ($i = 0; $i < $results->count; $i++) 
845           { 
846           if ($results->records[$i]['email'] != '') 
847             { 
848             $email = $results->records[$i]['email']; 
849             $name = $results->records[$i]['name']; 
850                  
851             $a_contacts[] = format_email_recipient($email, $name);
852             } 
853           }
854     $LDAP->close(); 
855     }
856   }
857 if ($a_contacts) 
858   { 
859         $OUTPUT->set_env('contacts', $a_contacts); 
860   }
861
862 $OUTPUT->send('compose');
863
864 ?>