]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/mail/compose.inc
Imported Upstream version 0.2~stable
[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 2161 2008-12-16 17:01:50Z thomasb $
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 $MESSAGE_FORM = NULL;
28 $MESSAGE = NULL;
29
30 // Nothing below is called during message composition, only at "new/forward/reply/draft" initialization or
31 // if a compose-ID is given (i.e. when the compose step is opened in a new window/tab).
32 // Since there are many ways to leave the compose page improperly, it seems necessary to clean-up an old
33 // compose when a "new/forward/reply/draft" is called - otherwise the old session attachments will appear
34
35 if (!is_array($_SESSION['compose']) || $_SESSION['compose']['id'] != get_input_value('_id', RCUBE_INPUT_GET))
36 {
37   rcmail_compose_cleanup();
38   $_SESSION['compose'] = array('id' => uniqid(rand()), 'param' => array_map('strip_tags', $_GET));
39   
40   // process values like "mailto:foo@bar.com?subject=new+message&cc=another"
41   if ($_SESSION['compose']['param']['_to']) {
42     $mailto = explode('?', $_SESSION['compose']['param']['_to']);
43     if (count($mailto) > 1) {
44       $_SESSION['compose']['param']['_to'] = $mailto[0];
45       parse_str($mailto[1], $query);
46       foreach ($query as $f => $val)
47         $_SESSION['compose']['param']["_$f"] = $val;
48     }
49   }
50
51   // redirect to a unique URL with all parameters stored in session
52   $OUTPUT->redirect(array('_action' => 'compose', '_id' => $_SESSION['compose']['id']));
53 }
54
55 // add some labels to client
56 $OUTPUT->add_label('nosubject', 'nosenderwarning', 'norecipientwarning', 'nosubjectwarning',
57     'nobodywarning', 'notsentwarning', 'savingmessage', 'sendingmessage', 'messagesaved',
58     'converting', 'editorwarning', 'searching');
59
60 // add config parameters to client script
61 if (!empty($CONFIG['drafts_mbox'])) {
62   $OUTPUT->set_env('drafts_mailbox', $CONFIG['drafts_mbox']);
63   $OUTPUT->set_env('draft_autosave', $CONFIG['draft_autosave']);
64 }
65 // set current mailbox in client environment
66 $OUTPUT->set_env('mailbox', $IMAP->get_mailbox_name());
67
68 // get reference message and set compose mode
69 if ($msg_uid = $_SESSION['compose']['param']['_reply_uid'])
70   $compose_mode = RCUBE_COMPOSE_REPLY;
71 else if ($msg_uid = $_SESSION['compose']['param']['_forward_uid'])
72   $compose_mode = RCUBE_COMPOSE_FORWARD;
73 else if ($msg_uid = $_SESSION['compose']['param']['_draft_uid'])
74   $compose_mode = RCUBE_COMPOSE_DRAFT;
75
76 if (!empty($msg_uid))
77 {
78   // similar as in program/steps/mail/show.inc
79   $MESSAGE = new rcube_message($msg_uid);
80   
81   if (!empty($MESSAGE->headers->charset))
82     $IMAP->set_charset($MESSAGE->headers->charset);
83     
84   if ($compose_mode == RCUBE_COMPOSE_REPLY)
85   {
86     $_SESSION['compose']['reply_uid'] = $msg_uid;
87     $_SESSION['compose']['reply_msgid'] = $MESSAGE->headers->messageID;
88     $_SESSION['compose']['references']  = trim($MESSAGE->headers->references . " " . $MESSAGE->headers->messageID);
89
90     if (!empty($_SESSION['compose']['param']['_all']))
91       $MESSAGE->reply_all = 1;
92   }
93   else if ($compose_mode == RCUBE_COMPOSE_DRAFT)
94   {
95     if($MESSAGE->headers->in_reply_to)
96     {
97       // TODO: how to get reply_uid/forward_uid value, maybe we must set X-Reply-UID/X-Forward-UID
98       // $_SESSION['compose']['reply_uid'] = ?
99       // $_SESSION['compose']['forward_uid'] = ?
100       $_SESSION['compose']['reply_msgid'] = '<'.$MESSAGE->headers->in_reply_to.'>';
101     }
102     $_SESSION['compose']['references']  = $MESSAGE->headers->references;
103   }
104   else if ($compose_mode == RCUBE_COMPOSE_FORWARD)
105   {
106     $_SESSION['compose']['forward_uid'] = $msg_uid;
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 = $_SESSION['compose']['param']['_mailto']) && $_SESSION['mailto'][$mailto_id])
134         $fvalue = urldecode($_SESSION['mailto'][$mailto_id]);
135       
136     case 'cc':
137       if (!$fname)
138       {
139         $fname = '_cc';
140         $header = 'cc';
141       }
142     case 'bcc':
143       if (!$fname)
144       {
145         $fname = '_bcc';
146         $header = 'bcc';
147       }
148         
149       $allow_attrib = array('id', 'class', 'style', 'cols', 'rows', 'tabindex');
150       $field_type = 'html_textarea';
151       break;
152
153     case 'replyto':
154     case 'reply-to':
155       $fname = '_replyto';
156       $allow_attrib = array('id', 'class', 'style', 'size', 'tabindex');
157       $field_type = 'html_inputfield';
158       break;
159   }
160  
161   if ($fname && !empty($_POST[$fname]))
162     $fvalue = get_input_value($fname, RCUBE_INPUT_POST, TRUE);
163   else if ($fname && !$fvalue && !empty($_SESSION['compose']['param'][$fname]))
164     $fvalue = $_SESSION['compose']['param'][$fname];
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
191       foreach ($to_addresses as $addr_part)
192       {
193         if (!empty($addr_part['mailto'])
194             && !in_array($addr_part['mailto'], $sa_recipients)
195             && (!$MESSAGE->compose_from
196                 || !in_array_nocase($addr_part['mailto'], $MESSAGE->compose_from)
197                 || (count($to_addresses)==1 && $header=='to'))) // allow reply to yourself
198         {
199           $fvalue .= (strlen($fvalue) ? ', ':'').$addr_part['string'];
200           $sa_recipients[] = $addr_part['mailto'];
201         }
202       }
203     }
204   }
205   else if ($header && $compose_mode == RCUBE_COMPOSE_DRAFT)
206   {
207     // get drafted headers
208     if ($header=='to' && !empty($MESSAGE->headers->to))
209       $fvalue = $MESSAGE->get_header('to');
210
211     if ($header=='cc' && !empty($MESSAGE->headers->cc))
212       $fvalue = $MESSAGE->get_header('cc');
213
214     if ($header=='bcc' && !empty($MESSAGE->headers->bcc))
215       $fvalue = $MESSAGE->get_header('bcc');
216   }
217
218         
219   if ($fname && $field_type)
220   {
221     // pass the following attributes to the form class
222     $field_attrib = array('name' => $fname, 'spellcheck' => 'false');
223     foreach ($attrib as $attr => $value)
224       if (in_array($attr, $allow_attrib))
225         $field_attrib[$attr] = $value;
226
227     // create teaxtarea object
228     $input = new $field_type($field_attrib);
229     $out = $input->show($fvalue);
230   }
231   
232   if ($form_start)
233     $out = $form_start.$out;
234
235   return $out;  
236 }
237
238
239
240 function rcmail_compose_header_from($attrib)
241 {
242   global $IMAP, $MESSAGE, $DB, $USER, $OUTPUT, $compose_mode;
243     
244   // pass the following attributes to the form class
245   $field_attrib = array('name' => '_from');
246   foreach ($attrib as $attr => $value)
247     if (in_array($attr, array('id', 'class', 'style', 'size', 'tabindex')))
248       $field_attrib[$attr] = $value;
249
250   // extract all recipients of the reply-message
251   $a_recipients = array();
252   if ($compose_mode == RCUBE_COMPOSE_REPLY && is_object($MESSAGE->headers))
253   {
254     $MESSAGE->compose_from = array();
255
256     $a_to = $IMAP->decode_address_list($MESSAGE->headers->to);
257     foreach ($a_to as $addr)
258     {
259       if (!empty($addr['mailto']))
260         $a_recipients[] = rc_strtolower($addr['mailto']);
261     }
262
263     if (!empty($MESSAGE->headers->cc))
264     {
265       $a_cc = $IMAP->decode_address_list($MESSAGE->headers->cc);
266       foreach ($a_cc as $addr)
267       {
268         if (!empty($addr['mailto']))
269           $a_recipients[] = rc_strtolower($addr['mailto']);
270       }
271     }
272   }
273
274   // get this user's identities
275   $sql_result = $USER->list_identities();
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 html_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             $a_signatures[$identity_id]['plain_text'] = trim($h2t->get_text());
299         }
300       }
301
302       if ($compose_mode == RCUBE_COMPOSE_REPLY && is_array($MESSAGE->compose_from))
303         $MESSAGE->compose_from[] = $sql_arr['email'];
304
305       if (empty($_POST['_from']))
306       {
307         // set draft's identity
308         if ($compose_mode == RCUBE_COMPOSE_DRAFT && strstr($MESSAGE->headers->from, $sql_arr['email']))
309           $from_id = $sql_arr['identity_id'];
310         // set identity if it's one of the reply-message recipients (with prio for default identity)
311         else if (in_array(rc_strtolower($sql_arr['email']), $a_recipients) && (empty($from_id) || $sql_arr['standard']))
312           $from_id = $sql_arr['identity_id'];
313       }
314     }
315
316     // overwrite identity selection with post parameter
317     if (!empty($_POST['_from']))
318       $from_id = get_input_value('_from', RCUBE_INPUT_POST);
319
320     $out = $select_from->show($from_id);
321
322     // add signatures to client
323     $OUTPUT->set_env('signatures', $a_signatures);
324   }
325   else
326   {
327     $input_from = new html_inputfield($field_attrib);
328     $out = $input_from->show($_POST['_from']);
329   }
330   
331   if ($form_start)
332     $out = $form_start.$out;
333
334   return $out;
335 }
336
337
338 function rcmail_compose_body($attrib)
339 {
340   global $RCMAIL, $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   else if ($compose_mode)
363   {
364     if ($isHtml && $MESSAGE->has_html_part())
365     {
366       $body = $MESSAGE->first_html_part();
367       $isHtml = true;
368     }
369     else
370     {
371       $body = $MESSAGE->first_text_part();
372       $isHtml = false;
373     }
374     
375     // compose reply-body
376     if ($compose_mode == RCUBE_COMPOSE_REPLY)
377       $body = rcmail_create_reply_body($body, $isHtml);
378     // forward message body inline
379     else if ($compose_mode == RCUBE_COMPOSE_FORWARD)
380       $body = rcmail_create_forward_body($body, $isHtml);
381     // load draft message body
382     else if ($compose_mode == RCUBE_COMPOSE_DRAFT)
383       $body = rcmail_create_draft_body($body, $isHtml);
384
385     if ($isHtml) {
386       // replace cid with href in inline images links
387       foreach ((array)$_SESSION['compose']['attachments'] as $pid => $attachment) {
388         if ($attachment['content_id']) {
389           $body = str_replace('cid:'. $attachment['content_id'], 
390             $OUTPUT->app->comm_path.'&_action=display-attachment&_file=rcmfile'.$pid, $body);
391         }
392       }
393     }
394   }
395   else if (!empty($_SESSION['compose']['param']['_body']))
396   {
397     $body = $_SESSION['compose']['param']['_body'];
398   }
399
400   $out = $form_start ? "$form_start\n" : '';
401
402   $saveid = new html_hiddenfield(array('name' => '_draft_saveid', 'value' => $compose_mode==RCUBE_COMPOSE_DRAFT ? str_replace(array('<','>'), "", $MESSAGE->headers->messageID) : ''));
403   $out .= $saveid->show();
404
405   $drafttoggle = new html_hiddenfield(array('name' => '_draft', 'value' => 'yes'));
406   $out .= $drafttoggle->show();
407
408   $msgtype = new html_hiddenfield(array('name' => '_is_html', 'value' => ($isHtml?"1":"0")));
409   $out .= $msgtype->show();
410
411   // If desired, set this textarea to be editable by TinyMCE
412   if ($isHtml) $attrib['class'] = 'mce_editor';
413   $textarea = new html_textarea($attrib);
414   $out .= $textarea->show($body);
415   $out .= $form_end ? "\n$form_end" : '';
416
417   // include HTML editor
418   rcube_html_editor();
419   
420   // include GoogieSpell
421   if (!empty($CONFIG['enable_spellcheck'])) {
422
423     $lang = strtolower(substr($_SESSION['language'], 0, 2));
424   
425     $spellcheck_langs = (array)$RCMAIL->config->get('spellcheck_languages', array('da'=>'Dansk', 'de'=>'Deutsch', 'en' => 'English', 'es'=>'Español', 'fr'=>'Français', 'it'=>'Italiano', 'nl'=>'Nederlands', 'pl'=>'Polski', 'pt'=>'Português', 'fi'=>'Suomi', 'sv'=>'Svenska'));
426     if (!$spellcheck_langs[$lang])
427       $lang = 'en';
428     
429     $editor_lang_set = array();
430     foreach ($spellcheck_langs as $key => $name) {
431       $editor_lang_set[] = ($key == $lang ? '+' : '') . JQ($name).'='.JQ($key);
432       }
433     
434     $OUTPUT->include_script('googiespell.js');
435     $OUTPUT->add_script(sprintf(
436       "var googie = new GoogieSpell('\$__skin_path/images/googiespell/','%s&_action=spell&lang=');\n".
437       "googie.lang_chck_spell = \"%s\";\n".
438       "googie.lang_rsm_edt = \"%s\";\n".
439       "googie.lang_close = \"%s\";\n".
440       "googie.lang_revert = \"%s\";\n".
441       "googie.lang_no_error_found = \"%s\";\n".
442       "googie.setLanguages(%s);\n".
443       "googie.setCurrentLanguage('%s');\n".
444       "googie.decorateTextarea('%s');\n".
445       "%s.set_env('spellcheck', googie);",
446       $RCMAIL->comm_path,
447       JQ(Q(rcube_label('checkspelling'))),
448       JQ(Q(rcube_label('resumeediting'))),
449       JQ(Q(rcube_label('close'))),
450       JQ(Q(rcube_label('revertto'))),
451       JQ(Q(rcube_label('nospellerrors'))),
452       json_serialize($spellcheck_langs),
453       $lang,
454       $attrib['id'],
455       JS_OBJECT_NAME), 'foot');
456
457     $OUTPUT->add_label('checking');
458     $OUTPUT->set_env('spellcheck_langs', join(',', $editor_lang_set));
459   }
460  
461   $out .= "\n".'<iframe name="savetarget" src="program/blank.gif" style="width:0;height:0;border:none;visibility:hidden;"></iframe>';
462
463   return $out;
464 }
465
466
467 function rcmail_create_reply_body($body, $bodyIsHtml)
468 {
469   global $IMAP, $MESSAGE, $OUTPUT;
470
471   if (! $bodyIsHtml)
472   {
473     // try to remove the signature
474     if (($sp = strrpos($body, '-- ')) !== false && ($sp == 0 || $body{$sp-1} == "\n"))
475       {
476       if ($body{$sp+3}==' ' || $body{$sp+3}=="\n" || $body{$sp+3}=="\r")
477         $body = substr($body, 0, max(0, $sp-1));
478       }
479
480     // soft-wrap message first
481     $body = rcmail_wrap_quoted($body, 75);
482
483     $body = rtrim($body, "\r\n");
484
485     if ($body) {
486       // split body into single lines
487       $a_lines = preg_split('/\r?\n/', $body);
488
489       // add > to each line
490       for($n=0; $n<sizeof($a_lines); $n++) {
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
500     // add title line(s)
501     $prefix = wordwrap(sprintf("On %s, %s wrote:\n",
502       $MESSAGE->headers->date,
503       $MESSAGE->get_header('from')), 76);
504
505     $suffix = '';
506   }
507   else
508   {
509     $prefix = sprintf("On %s, %s wrote:<br />\n",
510       $MESSAGE->headers->date,
511       htmlspecialchars(Q($MESSAGE->get_header('from'), 'replace'), ENT_COMPAT, $OUTPUT->get_charset()));
512     $prefix .= '<blockquote type="cite" style="padding-left:5px; border-left:#1010ff 2px solid; margin-left:5px; width:100%">';
513     $suffix = "</blockquote><p></p>";
514
515     rcmail_write_inline_attachments($MESSAGE);
516   }
517
518   return $prefix.$body.$suffix;
519 }
520
521
522 function rcmail_create_forward_body($body, $bodyIsHtml)
523 {
524   global $IMAP, $MESSAGE, $OUTPUT;
525
526   if (!$bodyIsHtml)
527   {
528     $prefix = "\n\n\n-------- Original Message --------\n";
529     $prefix .= 'Subject: ' . $MESSAGE->subject . "\n";
530     $prefix .= 'Date: ' . $MESSAGE->headers->date . "\n";
531     $prefix .= 'From: ' . $MESSAGE->get_header('from') . "\n";
532     $prefix .= 'To: ' . $MESSAGE->get_header('to') . "\n";
533     if ($MESSAGE->headers->replyto && $MESSAGE->headers->replyto != $MESSAGE->headers->from)
534       $prefix .= 'Reply-To: ' . $MESSAGE->get_header('replyto') . "\n";
535     $prefix .= "\n";
536   }
537   else
538   {
539     $prefix = sprintf(
540       "<br><br>-------- Original Message --------" .
541         "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody>" .
542         "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">Subject: </th><td>%s</td></tr>" .
543         "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">Date: </th><td>%s</td></tr>" .
544         "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">From: </th><td>%s</td></tr>" .
545         "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">To: </th><td>%s</td></tr>",
546       Q($MESSAGE->subject),
547       Q($MESSAGE->headers->date),
548       htmlspecialchars(Q($MESSAGE->get_header('from'), 'replace'), ENT_COMPAT, $OUTPUT->get_charset(), true),
549         htmlspecialchars(Q($MESSAGE->get_header('to'), 'replace'), ENT_COMPAT, $OUTPUT->get_charset(), true));
550
551     if ($MESSAGE->headers->replyto && $MESSAGE->headers->replyto != $MESSAGE->headers->from)
552       $prefix .= sprintf("<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">Reply-To: </th><td>%s</td></tr>",
553         htmlspecialchars(Q($MESSAGE->get_header('replyto'), 'replace'), ENT_COMPAT, $OUTPUT->get_charset(), true));
554
555     $prefix .= "</tbody></table><br>";
556   }
557
558   // add attachments
559   if (!isset($_SESSION['compose']['forward_attachments']) && is_array($MESSAGE->mime_parts))
560     rcmail_write_compose_attachments($MESSAGE, $bodyIsHtml);
561     
562   return $prefix.$body;
563 }
564
565
566 function rcmail_create_draft_body($body, $bodyIsHtml)
567 {
568   global $MESSAGE;
569   
570   /**
571    * add attachments
572    * sizeof($MESSAGE->mime_parts can be 1 - e.g. attachment, but no text!
573    */
574   if (!isset($_SESSION['compose']['forward_attachments'])
575       && is_array($MESSAGE->mime_parts)
576       && count($MESSAGE->mime_parts) > 0)
577     rcmail_write_compose_attachments($MESSAGE, $bodyIsHtml);
578
579   return $body;
580 }
581   
582   
583 function rcmail_write_compose_attachments(&$message, $bodyIsHtml)
584 {
585   foreach ((array)$message->mime_parts as $pid => $part)
586   {
587     if (($part->ctype_primary != 'message' || !$bodyIsHtml) &&
588         ($part->disposition=='attachment' || $part->disposition=='inline' || $part->headers['content-id']
589          || (empty($part->disposition) && $part->filename)))
590     {
591       if ($attachment = rcmail_save_attachment($message, $pid))
592         $_SESSION['compose']['attachments'][] = $attachment;
593     }
594   }
595         
596   $_SESSION['compose']['forward_attachments'] = true;
597 }
598
599
600 function rcmail_write_inline_attachments(&$message)
601 {
602   foreach ((array)$message->mime_parts as $pid => $part)
603   {
604     if ($part->content_id && $part->filename)
605     {
606       if ($attachment = rcmail_save_attachment($message, $pid))
607         $_SESSION['compose']['attachments'][] = $attachment;
608     }
609   }
610 }
611
612 function rcmail_save_attachment(&$message, $pid)
613 {
614   global $RCMAIL;
615
616   $temp_dir = unslashify($RCMAIL->config->get('temp_dir'));
617   $tmp_path = tempnam($temp_dir, 'rcmAttmnt');
618   $part = $message->mime_parts[$pid];
619   
620   if ($fp = fopen($tmp_path, 'w'))
621   {
622     $message->get_part_content($pid, $fp);
623     fclose($fp);
624
625     return array(
626         'mimetype' => $part->ctype_primary . '/' . $part->ctype_secondary,
627         'name' => $part->filename,
628         'path' => $tmp_path,
629         'content_id' => $part->content_id
630     );
631   }
632 }
633
634
635 function rcmail_compose_subject($attrib)
636 {
637   global $MESSAGE, $compose_mode;
638   
639   list($form_start, $form_end) = get_form_tags($attrib);
640   unset($attrib['form']);
641   
642   $attrib['name'] = '_subject';
643   $attrib['spellcheck'] = 'true';
644   $textfield = new html_inputfield($attrib);
645
646   $subject = '';
647
648   // use subject from post
649   if (isset($_POST['_subject'])) {
650     $subject = get_input_value('_subject', RCUBE_INPUT_POST, TRUE);
651   }
652   // create a reply-subject
653   else if ($compose_mode == RCUBE_COMPOSE_REPLY) {
654     if (eregi('^re:', $MESSAGE->subject))
655       $subject = $MESSAGE->subject;
656     else
657       $subject = 'Re: '.$MESSAGE->subject;
658   }
659   // create a forward-subject
660   else if ($compose_mode == RCUBE_COMPOSE_FORWARD) {
661     if (eregi('^fwd:', $MESSAGE->subject))
662       $subject = $MESSAGE->subject;
663     else
664       $subject = 'Fwd: '.$MESSAGE->subject;
665   }
666   // creeate a draft-subject
667   else if ($compose_mode == RCUBE_COMPOSE_DRAFT) {
668     $subject = $MESSAGE->subject;
669   }
670   else if (!empty($_SESSION['compose']['param']['_subject'])) {
671     $subject = $_SESSION['compose']['param']['_subject'];
672   }
673   
674   $out = $form_start ? "$form_start\n" : '';
675   $out .= $textfield->show($subject);
676   $out .= $form_end ? "\n$form_end" : '';
677          
678   return $out;
679 }
680
681
682 function rcmail_compose_attachment_list($attrib)
683 {
684   global $OUTPUT, $CONFIG;
685   
686   // add ID if not given
687   if (!$attrib['id'])
688     $attrib['id'] = 'rcmAttachmentList';
689   
690   $out = "\n";
691   
692   if (is_array($_SESSION['compose']['attachments']))
693   {
694     if ($attrib['deleteicon'])
695       $button = html::img(array(
696         'src' => $CONFIG['skin_path'] . $attrib['deleteicon'],
697         'alt' => rcube_label('delete'),
698         'style' => "padding-right:2px;vertical-align:middle"));
699     else
700       $button = Q(rcube_label('delete'));
701
702     foreach ($_SESSION['compose']['attachments'] as $id => $a_prop)
703     {
704       if (empty($a_prop))
705         continue;
706       
707       $out .= html::tag('li', array('id' => "rcmfile".$id),
708         html::a(array(
709             'href' => "#delete",
710             'title' => rcube_label('delete'),
711             'onclick' => sprintf("return %s.command('remove-attachment','rcmfile%d', this)", JS_OBJECT_NAME, $id)),
712           $button) . Q($a_prop['name']));
713     }
714   }
715
716   $OUTPUT->add_gui_object('attachmentlist', $attrib['id']);
717     
718   return html::tag('ul', $attrib, $out, html::$common_attrib);
719 }
720
721
722 function rcmail_compose_attachment_form($attrib)
723 {
724   global $OUTPUT;
725
726   // add ID if not given
727   if (!$attrib['id'])
728     $attrib['id'] = 'rcmUploadbox';
729   
730   $button = new html_inputfield(array('type' => 'button', 'class' => 'button'));
731   
732   $out = html::div($attrib,
733     $OUTPUT->form_tag(array('name' => 'form', 'method' => 'post', 'enctype' => 'multipart/form-data'),
734       html::div(null, rcmail_compose_attachment_field(array())) .
735       html::div('hint', rcube_label(array('name' => 'maxuploadsize', 'vars' => array('size' => show_bytes(parse_bytes(ini_get('upload_max_filesize'))))))) .
736       html::div('buttons',
737         $button->show(rcube_label('close'), array('onclick' => "document.getElementById('$attrib[id]').style.visibility='hidden'")) . ' ' .
738         $button->show(rcube_label('upload'), array('onclick' => JS_OBJECT_NAME . ".command('send-attachment', this.form)"))
739       )
740     )
741   );
742   
743   $OUTPUT->add_gui_object('uploadbox', $attrib['id']);
744   return $out;
745 }
746
747
748 function rcmail_compose_attachment_field($attrib)
749 {
750   $attrib['type'] = 'file';
751   $attrib['name'] = '_attachments[]';
752   $field = new html_inputfield($attrib);
753   return $field->show();
754 }
755
756
757 function rcmail_priority_selector($attrib)
758 {
759   global $MESSAGE;
760   
761   list($form_start, $form_end) = get_form_tags($attrib);
762   unset($attrib['form']);
763   
764   $attrib['name'] = '_priority';
765   $selector = new html_select($attrib);
766
767   $selector->add(array(rcube_label('lowest'),
768                        rcube_label('low'),
769                        rcube_label('normal'),
770                        rcube_label('high'),
771                        rcube_label('highest')),
772                  array(5, 4, 0, 2, 1));
773                  
774   $sel = isset($_POST['_priority']) ? $_POST['_priority'] : intval($MESSAGE->headers->priority);
775
776   $out = $form_start ? "$form_start\n" : '';
777   $out .= $selector->show($sel);
778   $out .= $form_end ? "\n$form_end" : '';
779          
780   return $out;
781 }
782
783
784 function rcmail_receipt_checkbox($attrib)
785 {
786   global $MESSAGE, $compose_mode;
787   
788   list($form_start, $form_end) = get_form_tags($attrib);
789   unset($attrib['form']);
790   
791   if (!isset($attrib['id']))
792     $attrib['id'] = 'receipt';  
793
794   $attrib['name'] = '_receipt';
795   $attrib['value'] = '1';
796   $checkbox = new html_checkbox($attrib);
797
798   $out = $form_start ? "$form_start\n" : '';
799   $out .= $checkbox->show(
800     $compose_mode == RCUBE_COMPOSE_DRAFT && $MESSAGE->headers->mdn_to ? 1 : 0);
801   $out .= $form_end ? "\n$form_end" : '';
802
803   return $out;
804 }
805
806
807 function rcmail_editor_selector($attrib)
808 {
809   global $CONFIG, $MESSAGE, $compose_mode;
810
811   $choices = array(
812     'html'  => 'htmltoggle',
813     'plain' => 'plaintoggle'
814   );
815
816   // determine whether HTML or plain text should be checked
817   $useHtml = $CONFIG['htmleditor'] ? true : false;
818
819   if ($compose_mode)
820     $useHtml = ($useHtml && $MESSAGE->has_html_part());
821
822   $editorid = empty($attrib['editorid']) ? 'rcmComposeMessage' : $attrib['editorid'];
823
824   $selector = '';
825   $chosenvalue = $useHtml ? 'html' : 'plain';
826   $radio = new html_radiobutton(array('name' => '_editorSelect',
827     'onclick' => "return rcmail_toggle_editor(this.value=='html', '$editorid', '_is_html')"));
828
829   foreach ($choices as $value => $text)
830   {
831     $attrib['id'] = '_' . $value;
832     $attrib['value'] = $value;
833     $selector .= $radio->show($chosenvalue, $attrib) . html::label($attrib['id'], Q(rcube_label($text)));
834   }
835
836   return $selector;
837 }
838
839
840 function rcmail_store_target_selection($attrib)
841 {
842   $attrib['name'] = '_store_target';
843   $select = rcmail_mailbox_select(array_merge($attrib, array('noselection' => '- '.rcube_label('dontsave').' -')));
844   return $select->show(rcmail::get_instance()->config->get('sent_mbox'), $attrib);
845 }
846
847
848 function get_form_tags($attrib)
849 {
850   global $RCMAIL, $MESSAGE_FORM;
851
852   $form_start = '';
853   if (!strlen($MESSAGE_FORM))
854   {
855     $hiddenfields = new html_hiddenfield(array('name' => '_task', 'value' => $RCMAIL->task));
856     $hiddenfields->add(array('name' => '_action', 'value' => 'send'));
857
858     $form_start = empty($attrib['form']) ? $RCMAIL->output->form_tag(array('name' => "form", 'method' => "post")) : '';
859     $form_start .= $hiddenfields->show();
860   }
861     
862   $form_end = (strlen($MESSAGE_FORM) && !strlen($attrib['form'])) ? '</form>' : '';
863   $form_name = !empty($attrib['form']) ? $attrib['form'] : 'form';
864   
865   if (!strlen($MESSAGE_FORM))
866     $RCMAIL->output->add_gui_object('messageform', $form_name);
867   
868   $MESSAGE_FORM = $form_name;
869
870   return array($form_start, $form_end);
871 }
872
873
874 // register UI objects
875 $OUTPUT->add_handlers(array(
876   'composeheaders' => 'rcmail_compose_headers',
877   'composesubject' => 'rcmail_compose_subject',
878   'composebody' => 'rcmail_compose_body',
879   'composeattachmentlist' => 'rcmail_compose_attachment_list',
880   'composeattachmentform' => 'rcmail_compose_attachment_form',
881   'composeattachment' => 'rcmail_compose_attachment_field',
882   'priorityselector' => 'rcmail_priority_selector',
883   'editorselector' => 'rcmail_editor_selector',
884   'receiptcheckbox' => 'rcmail_receipt_checkbox',
885   'storetarget' => 'rcmail_store_target_selection',
886 ));
887
888 $OUTPUT->send('compose');
889
890 ?>