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