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