]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/mail/compose.inc
Imported Upstream version 0.2.1
[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-2009, 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 2301 2009-02-18 19:15:36Z alec $
19
20 */
21
22 // define constants for message compose mode
23 define('RCUBE_COMPOSE_REPLY', 0x0106);
24 define('RCUBE_COMPOSE_FORWARD', 0x0107);
25 define('RCUBE_COMPOSE_DRAFT', 0x0108);
26
27 $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   else if (!empty($_SESSION['compose']['param']['_body']))
386   {
387     $body = $_SESSION['compose']['param']['_body'];
388   }
389
390   $out = $form_start ? "$form_start\n" : '';
391
392   $saveid = new html_hiddenfield(array('name' => '_draft_saveid', 'value' => $compose_mode==RCUBE_COMPOSE_DRAFT ? str_replace(array('<','>'), "", $MESSAGE->headers->messageID) : ''));
393   $out .= $saveid->show();
394
395   $drafttoggle = new html_hiddenfield(array('name' => '_draft', 'value' => 'yes'));
396   $out .= $drafttoggle->show();
397
398   $msgtype = new html_hiddenfield(array('name' => '_is_html', 'value' => ($isHtml?"1":"0")));
399   $out .= $msgtype->show();
400
401   // If desired, set this textarea to be editable by TinyMCE
402   if ($isHtml) $attrib['class'] = 'mce_editor';
403   $textarea = new html_textarea($attrib);
404   $out .= $textarea->show($body);
405   $out .= $form_end ? "\n$form_end" : '';
406
407   // include HTML editor
408   rcube_html_editor();
409   
410   // include GoogieSpell
411   if (!empty($CONFIG['enable_spellcheck'])) {
412
413     $lang = strtolower(substr($_SESSION['language'], 0, 2));
414   
415     $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'));
416     if (!$spellcheck_langs[$lang])
417       $lang = 'en';
418     
419     $editor_lang_set = array();
420     foreach ($spellcheck_langs as $key => $name) {
421       $editor_lang_set[] = ($key == $lang ? '+' : '') . JQ($name).'='.JQ($key);
422       }
423     
424     $OUTPUT->include_script('googiespell.js');
425     $OUTPUT->add_script(sprintf(
426       "var googie = new GoogieSpell('\$__skin_path/images/googiespell/','%s&_action=spell&lang=');\n".
427       "googie.lang_chck_spell = \"%s\";\n".
428       "googie.lang_rsm_edt = \"%s\";\n".
429       "googie.lang_close = \"%s\";\n".
430       "googie.lang_revert = \"%s\";\n".
431       "googie.lang_no_error_found = \"%s\";\n".
432       "googie.setLanguages(%s);\n".
433       "googie.setCurrentLanguage('%s');\n".
434       "googie.decorateTextarea('%s');\n".
435       "%s.set_env('spellcheck', googie);",
436       $RCMAIL->comm_path,
437       JQ(Q(rcube_label('checkspelling'))),
438       JQ(Q(rcube_label('resumeediting'))),
439       JQ(Q(rcube_label('close'))),
440       JQ(Q(rcube_label('revertto'))),
441       JQ(Q(rcube_label('nospellerrors'))),
442       json_serialize($spellcheck_langs),
443       $lang,
444       $attrib['id'],
445       JS_OBJECT_NAME), 'foot');
446
447     $OUTPUT->add_label('checking');
448     $OUTPUT->set_env('spellcheck_langs', join(',', $editor_lang_set));
449   }
450  
451   $out .= "\n".'<iframe name="savetarget" src="program/blank.gif" style="width:0;height:0;border:none;visibility:hidden;"></iframe>';
452
453   return $out;
454 }
455
456
457 function rcmail_create_reply_body($body, $bodyIsHtml)
458 {
459   global $IMAP, $MESSAGE, $OUTPUT;
460
461   if (! $bodyIsHtml)
462   {
463     // try to remove the signature
464     if (($sp = strrpos($body, '-- ')) !== false && ($sp == 0 || $body{$sp-1} == "\n"))
465       {
466       if ($body{$sp+3}==' ' || $body{$sp+3}=="\n" || $body{$sp+3}=="\r")
467         $body = substr($body, 0, max(0, $sp-1));
468       }
469
470     // soft-wrap message first
471     $body = rcmail_wrap_quoted($body, 75);
472
473     $body = rtrim($body, "\r\n");
474
475     if ($body) {
476       // split body into single lines
477       $a_lines = preg_split('/\r?\n/', $body);
478
479       // add > to each line
480       for($n=0; $n<sizeof($a_lines); $n++) {
481         if (strpos($a_lines[$n], '>')===0)
482           $a_lines[$n] = '>'.$a_lines[$n];
483         else
484           $a_lines[$n] = '> '.$a_lines[$n];
485         }
486  
487       $body = join("\n", $a_lines);
488       }
489
490     // add title line(s)
491     $prefix = wordwrap(sprintf("On %s, %s wrote:\n",
492       $MESSAGE->headers->date,
493       $MESSAGE->get_header('from')), 76);
494
495     $suffix = '';
496   }
497   else
498   {
499     // save inline images to files
500     $cid_map = rcmail_write_inline_attachments($MESSAGE);
501     // set is_safe flag (we need this for html body washing)
502     rcmail_check_safe($MESSAGE);
503     // clean up html tags
504     $body = rcmail_wash_html($body, array('safe' => $MESSAGE->is_safe), $cid_map);
505
506     // build reply (quote content)
507     $prefix = sprintf("On %s, %s wrote:<br />\n",
508       $MESSAGE->headers->date,
509       htmlspecialchars(Q($MESSAGE->get_header('from'), 'replace'), ENT_COMPAT, $OUTPUT->get_charset()));
510     $prefix .= '<blockquote type="cite" style="padding-left:5px; border-left:#1010ff 2px solid; margin-left:5px; width:100%">';
511     $suffix = "</blockquote><p></p>";
512   }
513
514   return $prefix.$body.$suffix;
515 }
516
517
518 function rcmail_create_forward_body($body, $bodyIsHtml)
519 {
520   global $IMAP, $MESSAGE, $OUTPUT;
521
522   // add attachments
523   if (!isset($_SESSION['compose']['forward_attachments']) && is_array($MESSAGE->mime_parts))
524     $cid_map = rcmail_write_compose_attachments($MESSAGE, $bodyIsHtml);
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     // set is_safe flag (we need this for html body washing)
540     rcmail_check_safe($MESSAGE);
541     // clean up html tags
542     $body = rcmail_wash_html($body, array('safe' => $MESSAGE->is_safe), $cid_map);
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       Q($MESSAGE->subject),
552       Q($MESSAGE->headers->date),
553       htmlspecialchars(Q($MESSAGE->get_header('from'), 'replace'), ENT_COMPAT, $OUTPUT->get_charset(), true),
554         htmlspecialchars(Q($MESSAGE->get_header('to'), 'replace'), ENT_COMPAT, $OUTPUT->get_charset(), true));
555
556     if ($MESSAGE->headers->replyto && $MESSAGE->headers->replyto != $MESSAGE->headers->from)
557       $prefix .= sprintf("<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">Reply-To: </th><td>%s</td></tr>",
558         htmlspecialchars(Q($MESSAGE->get_header('replyto'), 'replace'), ENT_COMPAT, $OUTPUT->get_charset(), true));
559
560     $prefix .= "</tbody></table><br>";
561   }
562     
563   return $prefix.$body;
564 }
565
566
567 function rcmail_create_draft_body($body, $bodyIsHtml)
568 {
569   global $MESSAGE, $OUTPUT;
570   
571   /**
572    * add attachments
573    * sizeof($MESSAGE->mime_parts can be 1 - e.g. attachment, but no text!
574    */
575   if (!isset($_SESSION['compose']['forward_attachments'])
576       && is_array($MESSAGE->mime_parts)
577       && count($MESSAGE->mime_parts) > 0)
578   {
579     $cid_map = rcmail_write_compose_attachments($MESSAGE, $bodyIsHtml);
580
581     // replace cid with href in inline images links
582     if ($cid_map)
583       $body = str_replace(array_keys($cid_map), array_values($cid_map), $body);
584   }
585   
586   return $body;
587 }
588   
589   
590 function rcmail_write_compose_attachments(&$message, $bodyIsHtml)
591 {
592   global $OUTPUT;
593   
594   $cid_map = array();
595   $id = 0;
596   
597   foreach ((array)$message->mime_parts as $pid => $part)
598   {
599     if (($part->ctype_primary != 'message' || !$bodyIsHtml) &&
600         ($part->disposition=='attachment' || $part->disposition=='inline' || $part->headers['content-id']
601          || (empty($part->disposition) && $part->filename)))
602     {
603       if ($attachment = rcmail_save_attachment($message, $pid)) {
604         $_SESSION['compose']['attachments'][$id] = $attachment;
605         if ($bodyIsHtml && $part->filename && $part->content_id) {
606           $cid_map['cid:'.$part->content_id] = 
607             $OUTPUT->app->comm_path.'&_action=display-attachment&_file=rcmfile'.$id;
608         }
609         $id++;
610       }
611     }
612   }
613         
614   $_SESSION['compose']['forward_attachments'] = true;
615
616   return $cid_map;
617 }
618
619
620 function rcmail_write_inline_attachments(&$message)
621 {
622   global $OUTPUT;
623
624   $cid_map = array();
625   $id = 0;
626   
627   foreach ((array)$message->mime_parts as $pid => $part) {
628     if ($part->content_id && $part->filename) {
629       if ($attachment = rcmail_save_attachment($message, $pid)) {
630         $_SESSION['compose']['attachments'][$id] = $attachment;
631         $cid_map['cid:'.$part->content_id] = 
632           $OUTPUT->app->comm_path.'&_action=display-attachment&_file=rcmfile'.$id;
633         $id++;
634       }
635     }
636   }
637   
638   return $cid_map;
639 }
640
641 function rcmail_save_attachment(&$message, $pid)
642 {
643   global $RCMAIL;
644
645   $temp_dir = unslashify($RCMAIL->config->get('temp_dir'));
646   $tmp_path = tempnam($temp_dir, 'rcmAttmnt');
647   $part = $message->mime_parts[$pid];
648   
649   if ($fp = fopen($tmp_path, 'w'))
650   {
651     $message->get_part_content($pid, $fp);
652     fclose($fp);
653
654     return array(
655         'mimetype' => $part->ctype_primary . '/' . $part->ctype_secondary,
656         'name' => $part->filename,
657         'path' => $tmp_path,
658         'content_id' => $part->content_id
659     );
660   }
661 }
662
663
664 function rcmail_compose_subject($attrib)
665 {
666   global $MESSAGE, $compose_mode;
667   
668   list($form_start, $form_end) = get_form_tags($attrib);
669   unset($attrib['form']);
670   
671   $attrib['name'] = '_subject';
672   $attrib['spellcheck'] = 'true';
673   $textfield = new html_inputfield($attrib);
674
675   $subject = '';
676
677   // use subject from post
678   if (isset($_POST['_subject'])) {
679     $subject = get_input_value('_subject', RCUBE_INPUT_POST, TRUE);
680   }
681   // create a reply-subject
682   else if ($compose_mode == RCUBE_COMPOSE_REPLY) {
683     if (eregi('^re:', $MESSAGE->subject))
684       $subject = $MESSAGE->subject;
685     else
686       $subject = 'Re: '.$MESSAGE->subject;
687   }
688   // create a forward-subject
689   else if ($compose_mode == RCUBE_COMPOSE_FORWARD) {
690     if (eregi('^fwd:', $MESSAGE->subject))
691       $subject = $MESSAGE->subject;
692     else
693       $subject = 'Fwd: '.$MESSAGE->subject;
694   }
695   // creeate a draft-subject
696   else if ($compose_mode == RCUBE_COMPOSE_DRAFT) {
697     $subject = $MESSAGE->subject;
698   }
699   else if (!empty($_SESSION['compose']['param']['_subject'])) {
700     $subject = $_SESSION['compose']['param']['_subject'];
701   }
702   
703   $out = $form_start ? "$form_start\n" : '';
704   $out .= $textfield->show($subject);
705   $out .= $form_end ? "\n$form_end" : '';
706          
707   return $out;
708 }
709
710
711 function rcmail_compose_attachment_list($attrib)
712 {
713   global $OUTPUT, $CONFIG;
714   
715   // add ID if not given
716   if (!$attrib['id'])
717     $attrib['id'] = 'rcmAttachmentList';
718   
719   $out = "\n";
720   
721   if (is_array($_SESSION['compose']['attachments']))
722   {
723     if ($attrib['deleteicon'])
724       $button = html::img(array(
725         'src' => $CONFIG['skin_path'] . $attrib['deleteicon'],
726         'alt' => rcube_label('delete'),
727         'style' => "padding-right:2px;vertical-align:middle"));
728     else
729       $button = Q(rcube_label('delete'));
730
731     foreach ($_SESSION['compose']['attachments'] as $id => $a_prop)
732     {
733       if (empty($a_prop))
734         continue;
735       
736       $out .= html::tag('li', array('id' => "rcmfile".$id),
737         html::a(array(
738             'href' => "#delete",
739             'title' => rcube_label('delete'),
740             'onclick' => sprintf("return %s.command('remove-attachment','rcmfile%d', this)", JS_OBJECT_NAME, $id)),
741           $button) . Q($a_prop['name']));
742     }
743   }
744
745   $OUTPUT->add_gui_object('attachmentlist', $attrib['id']);
746     
747   return html::tag('ul', $attrib, $out, html::$common_attrib);
748 }
749
750
751 function rcmail_compose_attachment_form($attrib)
752 {
753   global $OUTPUT;
754
755   // add ID if not given
756   if (!$attrib['id'])
757     $attrib['id'] = 'rcmUploadbox';
758   
759   $button = new html_inputfield(array('type' => 'button', 'class' => 'button'));
760   
761   $out = html::div($attrib,
762     $OUTPUT->form_tag(array('name' => 'form', 'method' => 'post', 'enctype' => 'multipart/form-data'),
763       html::div(null, rcmail_compose_attachment_field(array())) .
764       html::div('hint', rcube_label(array('name' => 'maxuploadsize', 'vars' => array('size' => show_bytes(parse_bytes(ini_get('upload_max_filesize'))))))) .
765       html::div('buttons',
766         $button->show(rcube_label('close'), array('onclick' => "document.getElementById('$attrib[id]').style.visibility='hidden'")) . ' ' .
767         $button->show(rcube_label('upload'), array('onclick' => JS_OBJECT_NAME . ".command('send-attachment', this.form)"))
768       )
769     )
770   );
771   
772   $OUTPUT->add_gui_object('uploadbox', $attrib['id']);
773   return $out;
774 }
775
776
777 function rcmail_compose_attachment_field($attrib)
778 {
779   $attrib['type'] = 'file';
780   $attrib['name'] = '_attachments[]';
781   $field = new html_inputfield($attrib);
782   return $field->show();
783 }
784
785
786 function rcmail_priority_selector($attrib)
787 {
788   global $MESSAGE;
789   
790   list($form_start, $form_end) = get_form_tags($attrib);
791   unset($attrib['form']);
792   
793   $attrib['name'] = '_priority';
794   $selector = new html_select($attrib);
795
796   $selector->add(array(rcube_label('lowest'),
797                        rcube_label('low'),
798                        rcube_label('normal'),
799                        rcube_label('high'),
800                        rcube_label('highest')),
801                  array(5, 4, 0, 2, 1));
802                  
803   $sel = isset($_POST['_priority']) ? $_POST['_priority'] : intval($MESSAGE->headers->priority);
804
805   $out = $form_start ? "$form_start\n" : '';
806   $out .= $selector->show($sel);
807   $out .= $form_end ? "\n$form_end" : '';
808          
809   return $out;
810 }
811
812
813 function rcmail_receipt_checkbox($attrib)
814 {
815   global $MESSAGE, $compose_mode;
816   
817   list($form_start, $form_end) = get_form_tags($attrib);
818   unset($attrib['form']);
819   
820   if (!isset($attrib['id']))
821     $attrib['id'] = 'receipt';  
822
823   $attrib['name'] = '_receipt';
824   $attrib['value'] = '1';
825   $checkbox = new html_checkbox($attrib);
826
827   $out = $form_start ? "$form_start\n" : '';
828   $out .= $checkbox->show(
829     $compose_mode == RCUBE_COMPOSE_DRAFT && $MESSAGE->headers->mdn_to ? 1 : 0);
830   $out .= $form_end ? "\n$form_end" : '';
831
832   return $out;
833 }
834
835
836 function rcmail_editor_selector($attrib)
837 {
838   global $CONFIG, $MESSAGE, $compose_mode;
839
840   $choices = array(
841     'html'  => 'htmltoggle',
842     'plain' => 'plaintoggle'
843   );
844
845   // determine whether HTML or plain text should be checked
846   $useHtml = $CONFIG['htmleditor'] ? true : false;
847
848   if ($compose_mode)
849     $useHtml = ($useHtml && $MESSAGE->has_html_part());
850
851   $editorid = empty($attrib['editorid']) ? 'rcmComposeMessage' : $attrib['editorid'];
852
853   $selector = '';
854   $chosenvalue = $useHtml ? 'html' : 'plain';
855   $radio = new html_radiobutton(array('name' => '_editorSelect',
856     'onclick' => "return rcmail_toggle_editor(this.value=='html', '$editorid', '_is_html')"));
857
858   foreach ($choices as $value => $text)
859   {
860     $attrib['id'] = '_' . $value;
861     $attrib['value'] = $value;
862     $selector .= $radio->show($chosenvalue, $attrib) . html::label($attrib['id'], Q(rcube_label($text)));
863   }
864
865   return $selector;
866 }
867
868
869 function rcmail_store_target_selection($attrib)
870 {
871   $attrib['name'] = '_store_target';
872   $select = rcmail_mailbox_select(array_merge($attrib, array('noselection' => '- '.rcube_label('dontsave').' -')));
873   return $select->show(rcmail::get_instance()->config->get('sent_mbox'), $attrib);
874 }
875
876
877 function get_form_tags($attrib)
878 {
879   global $RCMAIL, $MESSAGE_FORM;
880
881   $form_start = '';
882   if (!strlen($MESSAGE_FORM))
883   {
884     $hiddenfields = new html_hiddenfield(array('name' => '_task', 'value' => $RCMAIL->task));
885     $hiddenfields->add(array('name' => '_action', 'value' => 'send'));
886
887     $form_start = empty($attrib['form']) ? $RCMAIL->output->form_tag(array('name' => "form", 'method' => "post")) : '';
888     $form_start .= $hiddenfields->show();
889   }
890     
891   $form_end = (strlen($MESSAGE_FORM) && !strlen($attrib['form'])) ? '</form>' : '';
892   $form_name = !empty($attrib['form']) ? $attrib['form'] : 'form';
893   
894   if (!strlen($MESSAGE_FORM))
895     $RCMAIL->output->add_gui_object('messageform', $form_name);
896   
897   $MESSAGE_FORM = $form_name;
898
899   return array($form_start, $form_end);
900 }
901
902
903 // register UI objects
904 $OUTPUT->add_handlers(array(
905   'composeheaders' => 'rcmail_compose_headers',
906   'composesubject' => 'rcmail_compose_subject',
907   'composebody' => 'rcmail_compose_body',
908   'composeattachmentlist' => 'rcmail_compose_attachment_list',
909   'composeattachmentform' => 'rcmail_compose_attachment_form',
910   'composeattachment' => 'rcmail_compose_attachment_field',
911   'priorityselector' => 'rcmail_priority_selector',
912   'editorselector' => 'rcmail_editor_selector',
913   'receiptcheckbox' => 'rcmail_receipt_checkbox',
914   'storetarget' => 'rcmail_store_target_selection',
915 ));
916
917 $OUTPUT->send('compose');
918
919 ?>