4 +-----------------------------------------------------------------------+
5 | program/steps/mail/compose.inc |
7 | This file is part of the RoundCube Webmail client |
8 | Copyright (C) 2005-2009, RoundCube Dev. - Switzerland |
9 | Licensed under the GNU GPL |
12 | Compose a new mail message with all headers and attachments |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com> |
16 +-----------------------------------------------------------------------+
18 $Id: compose.inc 3021 2009-10-06 06:55:08Z thomasb $
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 define('RCUBE_COMPOSE_EDIT', 0x0109);
31 // Nothing below is called during message composition, only at "new/forward/reply/draft" initialization or
32 // if a compose-ID is given (i.e. when the compose step is opened in a new window/tab).
33 // Since there are many ways to leave the compose page improperly, it seems necessary to clean-up an old
34 // compose when a "new/forward/reply/draft" is called - otherwise the old session attachments will appear
36 if (!is_array($_SESSION['compose']) || $_SESSION['compose']['id'] != get_input_value('_id', RCUBE_INPUT_GET))
38 rcmail_compose_cleanup();
39 $_SESSION['compose'] = array(
40 'id' => uniqid(mt_rand()),
41 'param' => request2param(RCUBE_INPUT_GET),
42 'mailbox' => $IMAP->get_mailbox_name(),
45 // process values like "mailto:foo@bar.com?subject=new+message&cc=another"
46 if ($_SESSION['compose']['param']['to']) {
47 $mailto = explode('?', $_SESSION['compose']['param']['to']);
48 if (count($mailto) > 1) {
49 $_SESSION['compose']['param']['to'] = $mailto[0];
50 parse_str($mailto[1], $query);
51 foreach ($query as $f => $val)
52 $_SESSION['compose']['param'][$f] = $val;
56 // pipe compose parameters thru plugins
57 $plugin = $RCMAIL->plugins->exec_hook('message_compose', $_SESSION['compose']);
58 $_SESSION['compose']['param'] = $plugin['param'];
60 // add attachments listed by message_compose hook
61 if (is_array($plugin['attachments'])) {
62 foreach ($plugin['attachments'] as $attach) {
63 // we have structured data
64 if (is_array($attach)) {
65 $attachment = $attach;
67 // only a file path is given
69 $filename = basename($attach);
72 'mimetype' => rc_mime_content_type($attach, $filename),
77 // save attachment if valid
78 if (($attachment['data'] && $attachment['name']) || ($attachment['path'] && file_exists($attachment['path']))) {
79 $attachment = rcmail::get_instance()->plugins->exec_hook('save_attachment', $attachment);
82 if ($attachment['status'] && !$attachment['abort']) {
83 unset($attachment['data'], $attachment['status'], $attachment['abort']);
84 $_SESSION['compose']['attachments'][$attachment['id']] = $attachment;
89 // redirect to a unique URL with all parameters stored in session
90 $OUTPUT->redirect(array('_action' => 'compose', '_id' => $_SESSION['compose']['id']));
94 // add some labels to client
95 $OUTPUT->add_label('nosubject', 'nosenderwarning', 'norecipientwarning', 'nosubjectwarning', 'cancel',
96 'nobodywarning', 'notsentwarning', 'notuploadedwarning', 'savingmessage', 'sendingmessage',
97 'messagesaved', 'converting', 'editorwarning', 'searching', 'uploading', 'fileuploaderror');
99 // add config parameters to client script
100 if (!empty($CONFIG['drafts_mbox'])) {
101 $OUTPUT->set_env('drafts_mailbox', $CONFIG['drafts_mbox']);
102 $OUTPUT->set_env('draft_autosave', $CONFIG['draft_autosave']);
104 // set current mailbox in client environment
105 $OUTPUT->set_env('mailbox', $IMAP->get_mailbox_name());
107 // get reference message and set compose mode
108 if ($msg_uid = $_SESSION['compose']['param']['reply_uid'])
109 $compose_mode = RCUBE_COMPOSE_REPLY;
110 else if ($msg_uid = $_SESSION['compose']['param']['forward_uid'])
111 $compose_mode = RCUBE_COMPOSE_FORWARD;
112 else if ($msg_uid = $_SESSION['compose']['param']['uid'])
113 $compose_mode = RCUBE_COMPOSE_EDIT;
114 else if ($msg_uid = $_SESSION['compose']['param']['draft_uid']) {
115 $RCMAIL->imap->set_mailbox($CONFIG['drafts_mbox']);
116 $compose_mode = RCUBE_COMPOSE_DRAFT;
119 if (!empty($msg_uid))
121 // similar as in program/steps/mail/show.inc
122 // re-set 'prefer_html' to have possibility to use html part for compose
123 $CONFIG['prefer_html'] = $CONFIG['htmleditor'] || $compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT;
124 $MESSAGE = new rcube_message($msg_uid);
126 if (!empty($MESSAGE->headers->charset))
127 $IMAP->set_charset($MESSAGE->headers->charset);
129 if ($compose_mode == RCUBE_COMPOSE_REPLY)
131 $_SESSION['compose']['reply_uid'] = $msg_uid;
132 $_SESSION['compose']['reply_msgid'] = $MESSAGE->headers->messageID;
133 $_SESSION['compose']['references'] = trim($MESSAGE->headers->references . " " . $MESSAGE->headers->messageID);
135 if (!empty($_SESSION['compose']['param']['all']))
136 $MESSAGE->reply_all = 1;
138 else if ($compose_mode == RCUBE_COMPOSE_DRAFT)
140 if($MESSAGE->headers->in_reply_to)
142 // TODO: how to get reply_uid/forward_uid value, maybe we must set X-Reply-UID/X-Forward-UID
143 // $_SESSION['compose']['reply_uid'] = ?
144 // $_SESSION['compose']['forward_uid'] = ?
145 $_SESSION['compose']['reply_msgid'] = '<'.$MESSAGE->headers->in_reply_to.'>';
147 $_SESSION['compose']['references'] = $MESSAGE->headers->references;
149 else if ($compose_mode == RCUBE_COMPOSE_FORWARD)
151 $_SESSION['compose']['forward_uid'] = $msg_uid;
155 /****** compose mode functions ********/
158 function rcmail_compose_headers($attrib)
160 global $IMAP, $MESSAGE, $DB, $compose_mode;
161 static $sa_recipients = array();
163 list($form_start, $form_end) = get_form_tags($attrib);
166 $part = strtolower($attrib['part']);
171 return rcmail_compose_header_from($attrib);
175 $header = $param = 'to';
177 // we have a set of recipients stored is session
178 if (($mailto_id = $_SESSION['compose']['param']['mailto']) && $_SESSION['mailto'][$mailto_id])
179 $fvalue = urldecode($_SESSION['mailto'][$mailto_id]);
185 $header = $param = 'cc';
191 $header = $param = 'bcc';
194 $allow_attrib = array('id', 'class', 'style', 'cols', 'rows', 'tabindex');
195 $field_type = 'html_textarea';
202 $allow_attrib = array('id', 'class', 'style', 'size', 'tabindex');
203 $field_type = 'html_inputfield';
207 if ($fname && !empty($_POST[$fname])) {
208 $fvalue = get_input_value($fname, RCUBE_INPUT_POST, TRUE);
210 else if ($fname && !$fvalue && !empty($_SESSION['compose']['param'][$param])) {
211 $fvalue = $_SESSION['compose']['param'][$param];
213 else if ($header && $compose_mode == RCUBE_COMPOSE_REPLY) {
214 // get recipent address(es) out of the message headers
215 if ($header=='to' && !empty($MESSAGE->headers->replyto))
216 $fvalue = $MESSAGE->headers->replyto;
218 else if ($header=='to' && !empty($MESSAGE->headers->from))
219 $fvalue = $MESSAGE->headers->from;
221 // add recipent of original message if reply to all
222 else if ($header=='cc' && !empty($MESSAGE->reply_all))
224 if ($v = $MESSAGE->headers->to)
227 if ($v = $MESSAGE->headers->cc)
228 $fvalue .= (!empty($fvalue) ? ', ' : '') . $v;
231 // split recipients and put them back together in a unique way
234 $to_addresses = $IMAP->decode_address_list($fvalue);
237 foreach ($to_addresses as $addr_part)
239 if (!empty($addr_part['mailto'])
240 && !in_array($addr_part['mailto'], $sa_recipients)
241 && (!$MESSAGE->compose_from
242 || !in_array_nocase($addr_part['mailto'], $MESSAGE->compose_from)
243 || (count($to_addresses)==1 && $header=='to'))) // allow reply to yourself
245 $fvalue .= (strlen($fvalue) ? ', ':'').$addr_part['string'];
246 $sa_recipients[] = $addr_part['mailto'];
251 else if ($header && in_array($compose_mode, array(RCUBE_COMPOSE_DRAFT, RCUBE_COMPOSE_EDIT)))
253 // get drafted headers
254 if ($header=='to' && !empty($MESSAGE->headers->to))
255 $fvalue = $MESSAGE->get_header('to');
257 if ($header=='cc' && !empty($MESSAGE->headers->cc))
258 $fvalue = $MESSAGE->get_header('cc');
260 if ($header=='bcc' && !empty($MESSAGE->headers->bcc))
261 $fvalue = $MESSAGE->get_header('bcc');
265 if ($fname && $field_type)
267 // pass the following attributes to the form class
268 $field_attrib = array('name' => $fname, 'spellcheck' => 'false');
269 foreach ($attrib as $attr => $value)
270 if (in_array($attr, $allow_attrib))
271 $field_attrib[$attr] = $value;
273 // create teaxtarea object
274 $input = new $field_type($field_attrib);
275 $out = $input->show($fvalue);
279 $out = $form_start.$out;
286 function rcmail_compose_header_from($attrib)
288 global $IMAP, $MESSAGE, $DB, $USER, $OUTPUT, $compose_mode;
290 // pass the following attributes to the form class
291 $field_attrib = array('name' => '_from');
292 foreach ($attrib as $attr => $value)
293 if (in_array($attr, array('id', 'class', 'style', 'size', 'tabindex')))
294 $field_attrib[$attr] = $value;
296 // extract all recipients of the reply-message
297 $a_recipients = array();
298 if ($compose_mode == RCUBE_COMPOSE_REPLY && is_object($MESSAGE->headers))
300 $MESSAGE->compose_from = array();
302 $a_to = $IMAP->decode_address_list($MESSAGE->headers->to);
303 foreach ($a_to as $addr)
305 if (!empty($addr['mailto']))
306 $a_recipients[] = mb_strtolower($addr['mailto']);
309 if (!empty($MESSAGE->headers->cc))
311 $a_cc = $IMAP->decode_address_list($MESSAGE->headers->cc);
312 foreach ($a_cc as $addr)
314 if (!empty($addr['mailto']))
315 $a_recipients[] = mb_strtolower($addr['mailto']);
320 // get this user's identities
321 $user_identities = $USER->list_identities();
323 if (count($user_identities))
326 $a_signatures = array();
328 $field_attrib['onchange'] = JS_OBJECT_NAME.".change_identity(this)";
329 $select_from = new html_select($field_attrib);
331 foreach ($user_identities as $sql_arr)
333 $identity_id = $sql_arr['identity_id'];
334 $select_from->add(format_email_recipient($sql_arr['email'], $sql_arr['name']), $identity_id);
336 // add signature to array
337 if (!empty($sql_arr['signature']) && empty($_SESSION['compose']['param']['nosig']))
339 $a_signatures[$identity_id]['text'] = $sql_arr['signature'];
340 $a_signatures[$identity_id]['is_html'] = ($sql_arr['html_signature'] == 1) ? true : false;
341 if ($a_signatures[$identity_id]['is_html'])
343 $h2t = new html2text($a_signatures[$identity_id]['text'], false, false);
344 $a_signatures[$identity_id]['plain_text'] = trim($h2t->get_text());
348 if ($compose_mode == RCUBE_COMPOSE_REPLY && is_array($MESSAGE->compose_from))
349 $MESSAGE->compose_from[] = $sql_arr['email'];
351 if (empty($_POST['_from']))
353 // set draft's identity
354 if ($compose_mode == RCUBE_COMPOSE_DRAFT && strstr($MESSAGE->headers->from, $sql_arr['email']))
355 $from_id = $sql_arr['identity_id'];
356 // set identity if it's one of the reply-message recipients (with prio for default identity)
357 else if (in_array(mb_strtolower($sql_arr['email']), $a_recipients) && (empty($from_id) || $sql_arr['standard']))
358 $from_id = $sql_arr['identity_id'];
362 // overwrite identity selection with post parameter
363 if (!empty($_POST['_from']))
364 $from_id = get_input_value('_from', RCUBE_INPUT_POST);
366 $out = $select_from->show($from_id);
368 // add signatures to client
369 $OUTPUT->set_env('signatures', $a_signatures);
373 $input_from = new html_inputfield($field_attrib);
374 $out = $input_from->show($_POST['_from']);
378 $out = $form_start.$out;
384 function rcmail_compose_body($attrib)
386 global $RCMAIL, $CONFIG, $OUTPUT, $MESSAGE, $compose_mode;
388 list($form_start, $form_end) = get_form_tags($attrib);
389 unset($attrib['form']);
391 if (empty($attrib['id']))
392 $attrib['id'] = 'rcmComposeBody';
394 $attrib['name'] = '_message';
396 if ($CONFIG['htmleditor'] || (($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT) && $MESSAGE->has_html_part()))
403 // use posted message body
404 if (!empty($_POST['_message']))
406 $body = get_input_value('_message', RCUBE_INPUT_POST, true);
408 else if ($_SESSION['compose']['param']['body'])
410 $body = $_SESSION['compose']['param']['body'];
413 else if ($compose_mode)
415 if (($isHtml || $compose_mode == RCUBE_COMPOSE_DRAFT) && $MESSAGE->has_html_part())
417 $body = $MESSAGE->first_html_part();
422 $body = $MESSAGE->first_text_part();
426 // compose reply-body
427 if ($compose_mode == RCUBE_COMPOSE_REPLY)
428 $body = rcmail_create_reply_body($body, $isHtml);
429 // forward message body inline
430 else if ($compose_mode == RCUBE_COMPOSE_FORWARD)
431 $body = rcmail_create_forward_body($body, $isHtml);
432 // load draft message body
433 else if ($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT)
434 $body = rcmail_create_draft_body($body, $isHtml);
436 else if (!empty($_SESSION['compose']['param']['body']))
438 $body = $_SESSION['compose']['param']['body'];
441 $out = $form_start ? "$form_start\n" : '';
443 $saveid = new html_hiddenfield(array('name' => '_draft_saveid', 'value' => $compose_mode==RCUBE_COMPOSE_DRAFT ? str_replace(array('<','>'), "", $MESSAGE->headers->messageID) : ''));
444 $out .= $saveid->show();
446 $drafttoggle = new html_hiddenfield(array('name' => '_draft', 'value' => 'yes'));
447 $out .= $drafttoggle->show();
449 $msgtype = new html_hiddenfield(array('name' => '_is_html', 'value' => ($isHtml?"1":"0")));
450 $out .= $msgtype->show();
452 // If desired, set this textarea to be editable by TinyMCE
453 if ($isHtml) $attrib['class'] = 'mce_editor';
454 $textarea = new html_textarea($attrib);
455 $out .= $textarea->show($body);
456 $out .= $form_end ? "\n$form_end" : '';
458 $OUTPUT->set_env('composebody', $attrib['id']);
460 // include HTML editor
463 // include GoogieSpell
464 if (!empty($CONFIG['enable_spellcheck'])) {
466 $lang = strtolower(substr($_SESSION['language'], 0, 2));
468 $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'));
469 if (!$spellcheck_langs[$lang])
472 $editor_lang_set = array();
473 foreach ($spellcheck_langs as $key => $name) {
474 $editor_lang_set[] = ($key == $lang ? '+' : '') . JQ($name).'='.JQ($key);
477 $OUTPUT->include_script('googiespell.js');
478 $OUTPUT->add_script(sprintf(
479 "var googie = new GoogieSpell('\$__skin_path/images/googiespell/','%s&_action=spell&lang=');\n".
480 "googie.lang_chck_spell = \"%s\";\n".
481 "googie.lang_rsm_edt = \"%s\";\n".
482 "googie.lang_close = \"%s\";\n".
483 "googie.lang_revert = \"%s\";\n".
484 "googie.lang_no_error_found = \"%s\";\n".
485 "googie.setLanguages(%s);\n".
486 "googie.setCurrentLanguage('%s');\n".
487 "googie.setSpellContainer('spellcheck-control');\n".
488 "googie.decorateTextarea('%s');\n".
489 "%s.set_env('spellcheck', googie);",
491 JQ(Q(rcube_label('checkspelling'))),
492 JQ(Q(rcube_label('resumeediting'))),
493 JQ(Q(rcube_label('close'))),
494 JQ(Q(rcube_label('revertto'))),
495 JQ(Q(rcube_label('nospellerrors'))),
496 json_serialize($spellcheck_langs),
499 JS_OBJECT_NAME), 'foot');
501 $OUTPUT->add_label('checking');
502 $OUTPUT->set_env('spellcheck_langs', join(',', $editor_lang_set));
505 $out .= "\n".'<iframe name="savetarget" src="program/blank.gif" style="width:0;height:0;border:none;visibility:hidden;"></iframe>';
511 function rcmail_create_reply_body($body, $bodyIsHtml)
513 global $IMAP, $MESSAGE, $OUTPUT;
517 // try to remove the signature
518 if (($sp = strrpos($body, '-- ')) !== false && ($sp == 0 || $body{$sp-1} == "\n"))
520 if ($body{$sp+3}==' ' || $body{$sp+3}=="\n" || $body{$sp+3}=="\r")
521 $body = substr($body, 0, max(0, $sp-1));
524 // soft-wrap message first
525 $body = rcmail_wrap_quoted($body, 75);
527 $body = rtrim($body, "\r\n");
530 // split body into single lines
531 $a_lines = preg_split('/\r?\n/', $body);
533 // add > to each line
534 for($n=0; $n<sizeof($a_lines); $n++) {
535 if (strpos($a_lines[$n], '>')===0)
536 $a_lines[$n] = '>'.$a_lines[$n];
538 $a_lines[$n] = '> '.$a_lines[$n];
541 $body = join("\n", $a_lines);
545 $prefix = rc_wordwrap(sprintf("On %s, %s wrote:\n",
546 $MESSAGE->headers->date,
547 $MESSAGE->get_header('from')), 76);
553 // save inline images to files
554 $cid_map = rcmail_write_inline_attachments($MESSAGE);
555 // set is_safe flag (we need this for html body washing)
556 rcmail_check_safe($MESSAGE);
557 // clean up html tags
558 $body = rcmail_wash_html($body, array('safe' => $MESSAGE->is_safe), $cid_map);
560 // build reply (quote content)
561 $prefix = sprintf("On %s, %s wrote:<br />\n",
562 $MESSAGE->headers->date,
563 htmlspecialchars(Q($MESSAGE->get_header('from'), 'replace'), ENT_COMPAT, $OUTPUT->get_charset()));
564 $prefix .= '<blockquote type="cite" style="padding-left:5px; border-left:#1010ff 2px solid; margin-left:5px; width:100%">';
565 $suffix = "</blockquote><p></p>";
568 return $prefix.$body.$suffix;
572 function rcmail_create_forward_body($body, $bodyIsHtml)
574 global $IMAP, $MESSAGE, $OUTPUT;
577 if (!isset($_SESSION['compose']['forward_attachments']) && is_array($MESSAGE->mime_parts))
578 $cid_map = rcmail_write_compose_attachments($MESSAGE, $bodyIsHtml);
582 $prefix = "\n\n\n-------- Original Message --------\n";
583 $prefix .= 'Subject: ' . $MESSAGE->subject . "\n";
584 $prefix .= 'Date: ' . $MESSAGE->headers->date . "\n";
585 $prefix .= 'From: ' . $MESSAGE->get_header('from') . "\n";
586 $prefix .= 'To: ' . $MESSAGE->get_header('to') . "\n";
587 if ($MESSAGE->headers->replyto && $MESSAGE->headers->replyto != $MESSAGE->headers->from)
588 $prefix .= 'Reply-To: ' . $MESSAGE->get_header('replyto') . "\n";
593 // set is_safe flag (we need this for html body washing)
594 rcmail_check_safe($MESSAGE);
595 // clean up html tags
596 $body = rcmail_wash_html($body, array('safe' => $MESSAGE->is_safe), $cid_map);
599 "<br><br>-------- Original Message --------" .
600 "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody>" .
601 "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">Subject: </th><td>%s</td></tr>" .
602 "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">Date: </th><td>%s</td></tr>" .
603 "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">From: </th><td>%s</td></tr>" .
604 "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">To: </th><td>%s</td></tr>",
605 Q($MESSAGE->subject),
606 Q($MESSAGE->headers->date),
607 htmlspecialchars(Q($MESSAGE->get_header('from'), 'replace'), ENT_COMPAT, $OUTPUT->get_charset()),
608 htmlspecialchars(Q($MESSAGE->get_header('to'), 'replace'), ENT_COMPAT, $OUTPUT->get_charset()));
610 if ($MESSAGE->headers->replyto && $MESSAGE->headers->replyto != $MESSAGE->headers->from)
611 $prefix .= sprintf("<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">Reply-To: </th><td>%s</td></tr>",
612 htmlspecialchars(Q($MESSAGE->get_header('replyto'), 'replace'), ENT_COMPAT, $OUTPUT->get_charset()));
614 $prefix .= "</tbody></table><br>";
617 return $prefix.$body;
621 function rcmail_create_draft_body($body, $bodyIsHtml)
623 global $MESSAGE, $OUTPUT;
627 * sizeof($MESSAGE->mime_parts can be 1 - e.g. attachment, but no text!
629 if (empty($_SESSION['compose']['forward_attachments'])
630 && is_array($MESSAGE->mime_parts)
631 && count($MESSAGE->mime_parts) > 0)
633 $cid_map = rcmail_write_compose_attachments($MESSAGE, $bodyIsHtml);
635 // replace cid with href in inline images links
637 $body = str_replace(array_keys($cid_map), array_values($cid_map), $body);
644 function rcmail_write_compose_attachments(&$message, $bodyIsHtml)
649 foreach ((array)$message->mime_parts as $pid => $part)
651 if (($part->ctype_primary != 'message' || !$bodyIsHtml) && $part->ctype_primary != 'multipart' &&
652 ($part->disposition == 'attachment' || ($part->disposition == 'inline' && $bodyIsHtml) || $part->filename))
654 if ($attachment = rcmail_save_attachment($message, $pid)) {
655 $_SESSION['compose']['attachments'][$attachment['id']] = $attachment;
656 if ($bodyIsHtml && $part->content_id) {
657 $cid_map['cid:'.$part->content_id] = $OUTPUT->app->comm_path.'&_action=display-attachment&_file=rcmfile'.$attachment['id'];
663 $_SESSION['compose']['forward_attachments'] = true;
669 function rcmail_write_inline_attachments(&$message)
674 foreach ((array)$message->mime_parts as $pid => $part) {
675 if ($part->content_id && $part->filename) {
676 if ($attachment = rcmail_save_attachment($message, $pid)) {
677 $_SESSION['compose']['attachments'][$attachment['id']] = $attachment;
678 $cid_map['cid:'.$part->content_id] = $OUTPUT->app->comm_path.'&_action=display-attachment&_file=rcmfile'.$attachment['id'];
686 function rcmail_save_attachment(&$message, $pid)
688 $part = $message->mime_parts[$pid];
689 $mem_limit = parse_bytes(ini_get('memory_limit'));
690 $curr_mem = function_exists('memory_get_usage') ? memory_get_usage() : 16*1024*1024; // safe value: 16MB
691 $data = $path = null;
693 // don't load too big attachments into memory
694 if ($mem_limit > 0 && $part->size > $mem_limit - $curr_mem) {
695 $rcmail = rcmail::get_instance();
696 $temp_dir = unslashify($rcmail->config->get('temp_dir'));
697 $path = tempnam($temp_dir, 'rcmAttmnt');
698 if ($fp = fopen($path, 'w')) {
699 $message->get_part_content($pid, $fp);
704 $data = $message->get_part_content($pid);
708 'name' => $part->filename ? $part->filename : 'Part_'.$pid.'.'.$part->ctype_secondary,
709 'mimetype' => $part->ctype_primary . '/' . $part->ctype_secondary,
710 'content_id' => $part->content_id,
715 $attachment = rcmail::get_instance()->plugins->exec_hook('save_attachment', $attachment);
717 if ($attachment['status']) {
718 unset($attachment['data'], $attachment['status'], $attachment['content_id'], $attachment['abort']);
728 function rcmail_compose_subject($attrib)
730 global $MESSAGE, $compose_mode;
732 list($form_start, $form_end) = get_form_tags($attrib);
733 unset($attrib['form']);
735 $attrib['name'] = '_subject';
736 $attrib['spellcheck'] = 'true';
737 $textfield = new html_inputfield($attrib);
741 // use subject from post
742 if (isset($_POST['_subject'])) {
743 $subject = get_input_value('_subject', RCUBE_INPUT_POST, TRUE);
745 // create a reply-subject
746 else if ($compose_mode == RCUBE_COMPOSE_REPLY) {
747 if (preg_match('/^re:/i', $MESSAGE->subject))
748 $subject = $MESSAGE->subject;
750 $subject = 'Re: '.$MESSAGE->subject;
752 // create a forward-subject
753 else if ($compose_mode == RCUBE_COMPOSE_FORWARD) {
754 if (preg_match('/^fwd:/i', $MESSAGE->subject))
755 $subject = $MESSAGE->subject;
757 $subject = 'Fwd: '.$MESSAGE->subject;
759 // creeate a draft-subject
760 else if ($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT) {
761 $subject = $MESSAGE->subject;
763 else if (!empty($_SESSION['compose']['param']['subject'])) {
764 $subject = $_SESSION['compose']['param']['subject'];
767 $out = $form_start ? "$form_start\n" : '';
768 $out .= $textfield->show($subject);
769 $out .= $form_end ? "\n$form_end" : '';
775 function rcmail_compose_attachment_list($attrib)
777 global $OUTPUT, $CONFIG;
779 // add ID if not given
781 $attrib['id'] = 'rcmAttachmentList';
786 if (is_array($_SESSION['compose']['attachments']))
788 if ($attrib['deleteicon']) {
789 $button = html::img(array(
790 'src' => $CONFIG['skin_path'] . $attrib['deleteicon'],
791 'alt' => rcube_label('delete')
795 $button = Q(rcube_label('delete'));
797 foreach ($_SESSION['compose']['attachments'] as $id => $a_prop)
802 $out .= html::tag('li', array('id' => 'rcmfile'.$id),
805 'title' => rcube_label('delete'),
806 'onclick' => sprintf("return %s.command('remove-attachment','rcmfile%s', this)", JS_OBJECT_NAME, $id)),
807 $button) . Q($a_prop['name']));
809 $jslist['rcmfile'.$id] = array('name' => $a_prop['name'], 'complete' => true, 'mimetype' => $a_prop['mimetype']);
813 if ($attrib['deleteicon'])
814 $_SESSION['compose']['deleteicon'] = $CONFIG['skin_path'] . $attrib['deleteicon'];
815 if ($attrib['cancelicon'])
816 $OUTPUT->set_env('cancelicon', $CONFIG['skin_path'] . $attrib['cancelicon']);
817 if ($attrib['loadingicon'])
818 $OUTPUT->set_env('loadingicon', $CONFIG['skin_path'] . $attrib['loadingicon']);
820 $OUTPUT->set_env('attachments', $jslist);
821 $OUTPUT->add_gui_object('attachmentlist', $attrib['id']);
823 return html::tag('ul', $attrib, $out, html::$common_attrib);
827 function rcmail_compose_attachment_form($attrib)
831 // add ID if not given
833 $attrib['id'] = 'rcmUploadbox';
835 // find max filesize value
836 $max_filesize = parse_bytes(ini_get('upload_max_filesize'));
837 $max_postsize = parse_bytes(ini_get('post_max_size'));
838 if ($max_postsize && $max_postsize < $max_filesize)
839 $max_filesize = $max_postsize;
840 $max_filesize = show_bytes($max_filesize);
842 $button = new html_inputfield(array('type' => 'button'));
844 $out = html::div($attrib,
845 $OUTPUT->form_tag(array('name' => 'form', 'method' => 'post', 'enctype' => 'multipart/form-data'),
846 html::div(null, rcmail_compose_attachment_field(array('size' => $attrib[attachmentfieldsize]))) .
847 html::div('hint', rcube_label(array('name' => 'maxuploadsize', 'vars' => array('size' => $max_filesize)))) .
849 $button->show(rcube_label('close'), array('class' => 'button', 'onclick' => "document.getElementById('$attrib[id]').style.visibility='hidden'")) . ' ' .
850 $button->show(rcube_label('upload'), array('class' => 'button mainaction', 'onclick' => JS_OBJECT_NAME . ".command('send-attachment', this.form)"))
855 $OUTPUT->add_gui_object('uploadbox', $attrib['id']);
860 function rcmail_compose_attachment_field($attrib)
862 $attrib['type'] = 'file';
863 $attrib['name'] = '_attachments[]';
864 $field = new html_inputfield($attrib);
865 return $field->show();
869 function rcmail_priority_selector($attrib)
873 list($form_start, $form_end) = get_form_tags($attrib);
874 unset($attrib['form']);
876 $attrib['name'] = '_priority';
877 $selector = new html_select($attrib);
879 $selector->add(array(rcube_label('lowest'),
881 rcube_label('normal'),
883 rcube_label('highest')),
884 array(5, 4, 0, 2, 1));
886 if (isset($_POST['_priority']))
887 $sel = $_POST['_priority'];
888 else if (intval($MESSAGE->headers->priority) != 3)
889 $sel = intval($MESSAGE->headers->priority);
893 $out = $form_start ? "$form_start\n" : '';
894 $out .= $selector->show($sel);
895 $out .= $form_end ? "\n$form_end" : '';
901 function rcmail_receipt_checkbox($attrib)
903 global $MESSAGE, $compose_mode;
905 list($form_start, $form_end) = get_form_tags($attrib);
906 unset($attrib['form']);
908 if (!isset($attrib['id']))
909 $attrib['id'] = 'receipt';
911 $attrib['name'] = '_receipt';
912 $attrib['value'] = '1';
913 $checkbox = new html_checkbox($attrib);
915 $out = $form_start ? "$form_start\n" : '';
916 $out .= $checkbox->show(in_array($compose_mode, array(RCUBE_COMPOSE_DRAFT, RCUBE_COMPOSE_EDIT))
917 && $MESSAGE->headers->mdn_to ? 1 : 0);
918 $out .= $form_end ? "\n$form_end" : '';
924 function rcmail_editor_selector($attrib)
926 global $CONFIG, $MESSAGE, $compose_mode;
928 // determine whether HTML or plain text should be checked
930 $useHtml = (($CONFIG['htmleditor'] || $compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT)
931 && $MESSAGE->has_html_part());
933 $useHtml = $CONFIG['htmleditor'] ? true : false;
935 if (empty($attrib['editorid']))
936 $attrib['editorid'] = 'rcmComposeBody';
938 if (empty($attrib['name']))
939 $attrib['name'] = 'editorSelect';
941 $attrib['onchange'] = "return rcmail_toggle_editor(this.value=='html', '".$attrib['editorid']."', '_is_html')";
943 $select = new html_select($attrib);
945 $select->add(Q(rcube_label('htmltoggle')), 'html');
946 $select->add(Q(rcube_label('plaintoggle')), 'plain');
948 return $select->show($useHtml ? 'html' : 'plain');
950 foreach ($choices as $value => $text)
952 $attrib['id'] = '_' . $value;
953 $attrib['value'] = $value;
954 $selector .= $radio->show($chosenvalue, $attrib) . html::label($attrib['id'], Q(rcube_label($text)));
961 function rcmail_store_target_selection($attrib)
963 $attrib['name'] = '_store_target';
964 $select = rcmail_mailbox_select(array_merge($attrib, array('noselection' => '- '.rcube_label('dontsave').' -')));
965 return $select->show(rcmail::get_instance()->config->get('sent_mbox'), $attrib);
969 function get_form_tags($attrib)
971 global $RCMAIL, $MESSAGE_FORM;
974 if (!strlen($MESSAGE_FORM))
976 $hiddenfields = new html_hiddenfield(array('name' => '_task', 'value' => $RCMAIL->task));
977 $hiddenfields->add(array('name' => '_action', 'value' => 'send'));
979 $form_start = empty($attrib['form']) ? $RCMAIL->output->form_tag(array('name' => "form", 'method' => "post")) : '';
980 $form_start .= $hiddenfields->show();
983 $form_end = (strlen($MESSAGE_FORM) && !strlen($attrib['form'])) ? '</form>' : '';
984 $form_name = !empty($attrib['form']) ? $attrib['form'] : 'form';
986 if (!strlen($MESSAGE_FORM))
987 $RCMAIL->output->add_gui_object('messageform', $form_name);
989 $MESSAGE_FORM = $form_name;
991 return array($form_start, $form_end);
995 // register UI objects
996 $OUTPUT->add_handlers(array(
997 'composeheaders' => 'rcmail_compose_headers',
998 'composesubject' => 'rcmail_compose_subject',
999 'composebody' => 'rcmail_compose_body',
1000 'composeattachmentlist' => 'rcmail_compose_attachment_list',
1001 'composeattachmentform' => 'rcmail_compose_attachment_form',
1002 'composeattachment' => 'rcmail_compose_attachment_field',
1003 'priorityselector' => 'rcmail_priority_selector',
1004 'editorselector' => 'rcmail_editor_selector',
1005 'receiptcheckbox' => 'rcmail_receipt_checkbox',
1006 'storetarget' => 'rcmail_store_target_selection',
1009 $OUTPUT->send('compose');