From 6084d782f2e6e57248463bf10b99eeee543e0049 Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Sun, 14 Nov 2010 06:35:38 -0500
Subject: [PATCH] - Fix hanling of HTML entity strings in plai text messages

---
 program/steps/mail/compose.inc |  450 ++++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 325 insertions(+), 125 deletions(-)

diff --git a/program/steps/mail/compose.inc b/program/steps/mail/compose.inc
index f59582b..3f2b8c5 100644
--- a/program/steps/mail/compose.inc
+++ b/program/steps/mail/compose.inc
@@ -4,8 +4,8 @@
  +-----------------------------------------------------------------------+
  | program/steps/mail/compose.inc                                        |
  |                                                                       |
- | This file is part of the RoundCube Webmail client                     |
- | Copyright (C) 2005-2009, RoundCube Dev. - Switzerland                 |
+ | This file is part of the Roundcube Webmail client                     |
+ | Copyright (C) 2005-2009, Roundcube Dev. - Switzerland                 |
  | Licensed under the GNU GPL                                            |
  |                                                                       |
  | PURPOSE:                                                              |
@@ -33,9 +33,17 @@
 // Since there are many ways to leave the compose page improperly, it seems necessary to clean-up an old
 // compose when a "new/forward/reply/draft" is called - otherwise the old session attachments will appear
 
-if (!is_array($_SESSION['compose']) || $_SESSION['compose']['id'] != get_input_value('_id', RCUBE_INPUT_GET))
+$MESSAGE_ID = get_input_value('_id', RCUBE_INPUT_GET);
+if (!is_array($_SESSION['compose']) || $_SESSION['compose']['id'] != $MESSAGE_ID)
 {
   rcmail_compose_cleanup();
+
+  // Infinite redirect prevention in case of broken session (#1487028)
+  if ($MESSAGE_ID)
+    raise_error(array('code' => 500, 'type' => 'php',
+      'file' => __FILE__, 'line' => __LINE__,
+      'message' => "Invalid session"), true, true);
+
   $_SESSION['compose'] = array(
     'id' => uniqid(mt_rand()),
     'param' => request2param(RCUBE_INPUT_GET),
@@ -81,7 +89,7 @@
       
       // save attachment if valid
       if (($attachment['data'] && $attachment['name']) || ($attachment['path'] && file_exists($attachment['path']))) {
-        $attachment = rcmail::get_instance()->plugins->exec_hook('save_attachment', $attachment);
+        $attachment = rcmail::get_instance()->plugins->exec_hook('attachment_save', $attachment);
       }
       
       if ($attachment['status'] && !$attachment['abort']) {
@@ -89,6 +97,11 @@
         $_SESSION['compose']['attachments'][$attachment['id']] = $attachment;
       }
     }
+  }
+
+  // check if folder for saving sent messages exists and is subscribed (#1486802)
+  if ($sent_folder = $_SESSION['compose']['param']['sent_mbox']) {
+    rcmail_check_sent_folder($sent_folder, true);
   }
 
   // redirect to a unique URL with all parameters stored in session
@@ -99,7 +112,8 @@
 // add some labels to client
 $OUTPUT->add_label('nosubject', 'nosenderwarning', 'norecipientwarning', 'nosubjectwarning', 'cancel',
     'nobodywarning', 'notsentwarning', 'notuploadedwarning', 'savingmessage', 'sendingmessage', 
-    'messagesaved', 'converting', 'editorwarning', 'searching', 'uploading', 'fileuploaderror');
+    'messagesaved', 'converting', 'editorwarning', 'searching', 'uploading', 'fileuploaderror',
+    'autocompletechars');
 
 // add config parameters to client script
 if (!empty($CONFIG['drafts_mbox'])) {
@@ -110,6 +124,7 @@
 $OUTPUT->set_env('mailbox', $IMAP->get_mailbox_name());
 $OUTPUT->set_env('sig_above', $CONFIG['sig_above']);
 $OUTPUT->set_env('top_posting', $CONFIG['top_posting']);
+$OUTPUT->set_env('autocomplete_min_length', $CONFIG['autocomplete_min_length']);
 
 // get reference message and set compose mode
 if ($msg_uid = $_SESSION['compose']['param']['reply_uid'])
@@ -157,9 +172,16 @@
     $_SESSION['compose']['references']  = trim($MESSAGE->headers->references . " " . $MESSAGE->headers->messageID);
 
     if (!empty($_SESSION['compose']['param']['all']))
-      $MESSAGE->reply_all = 1;
+      $MESSAGE->reply_all = $_SESSION['compose']['param']['all'];
 
     $OUTPUT->set_env('compose_mode', 'reply');
+
+    // Save the sent message in the same folder of the message being replied to
+    if ($RCMAIL->config->get('reply_same_folder') && ($sent_folder = $_SESSION['compose']['mailbox'])
+      && rcmail_check_sent_folder($sent_folder, false)
+    ) {
+      $_SESSION['compose']['param']['sent_mbox'] = $sent_folder;
+    }
   }
   else if ($compose_mode == RCUBE_COMPOSE_DRAFT)
   {
@@ -174,8 +196,15 @@
         $_SESSION['compose']['forward_uid'] = $info['uid'];
 
       $_SESSION['compose']['mailbox'] = $info['folder'];
+
+      // Save the sent message in the same folder of the message being replied to
+      if ($RCMAIL->config->get('reply_same_folder') && ($sent_folder = $info['folder'])
+        && rcmail_check_sent_folder($sent_folder, false)
+      ) {
+        $_SESSION['compose']['param']['sent_mbox'] = $sent_folder;
+      }
     }
-    
+
     if ($MESSAGE->headers->in_reply_to)
       $_SESSION['compose']['reply_msgid'] = '<'.$MESSAGE->headers->in_reply_to.'>';
 
@@ -218,14 +247,12 @@
         $fvalue = urldecode($_SESSION['mailto'][$mailto_id]);
 
     case 'cc':
-      if (!$fname)
-      {
+      if (!$fname) {
         $fname = '_cc';
         $header = $param = 'cc';
       }
     case 'bcc':
-      if (!$fname)
-      {
+      if (!$fname) {
         $fname = '_bcc';
         $header = $param = 'bcc';
       }
@@ -238,11 +265,29 @@
     case 'reply-to':
       $fname = '_replyto';
       $param = 'replyto';
+      $header = 'reply-to';
+
+    case 'mailreplyto':
+    case 'mail-reply-to':
+      if (!$fname) {
+        $fname = '_mailreplyto';
+        $param = 'mailreplyto';
+        $header = 'mail-reply-to';
+      }
+
+    case 'mailfollowupto':
+    case 'mail-followup-to':
+      if (!$fname) {
+        $fname = '_mailfollowupto';
+        $param = 'mailfollowupto';
+        $header = 'mail-followup-to';
+      }
+
       $allow_attrib = array('id', 'class', 'style', 'size', 'tabindex');
       $field_type = 'html_inputfield';
       break;
   }
- 
+
   if ($fname && !empty($_POST[$fname])) {
     $fvalue = get_input_value($fname, RCUBE_INPUT_POST, TRUE);
   }
@@ -251,56 +296,88 @@
   }
   else if ($header && $compose_mode == RCUBE_COMPOSE_REPLY) {
     // get recipent address(es) out of the message headers
-    if ($header=='to' && !empty($MESSAGE->headers->replyto))
-      $fvalue = $MESSAGE->headers->replyto;
+    if ($header == 'to') {
+      $mailfollowup = $MESSAGE->headers->others['mail-followup-to'];
+      $mailreplyto  = $MESSAGE->headers->others['mail-reply-to'];
 
-    else if ($header=='to' && !empty($MESSAGE->headers->from))
-      $fvalue = $MESSAGE->headers->from;
-
+      if ($MESSAGE->reply_all == 'list' && $mailfollowup)
+        $fvalue = $mailfollowup;
+      else if ($MESSAGE->reply_all == 'list'
+        && preg_match('/<mailto:([^>]+)>/i', $MESSAGE->headers->others['list-post'], $m))
+        $fvalue = $m[1];
+      else if ($mailreplyto)
+        $fvalue = $mailreplyto;
+      else if (!empty($MESSAGE->headers->replyto))
+        $fvalue = $MESSAGE->headers->replyto;
+      else if (!empty($MESSAGE->headers->from))
+        $fvalue = $MESSAGE->headers->from;
+    }
     // add recipent of original message if reply to all
-    else if ($header=='cc' && !empty($MESSAGE->reply_all))
-    {
+    else if ($header == 'cc' && !empty($MESSAGE->reply_all) && $MESSAGE->reply_all != 'list') {
       if ($v = $MESSAGE->headers->to)
         $fvalue .= $v;
-
       if ($v = $MESSAGE->headers->cc)
         $fvalue .= (!empty($fvalue) ? ', ' : '') . $v;
     }
 
     // split recipients and put them back together in a unique way
-    if (!empty($fvalue))
-    {
+    if (!empty($fvalue)) {
       $to_addresses = $IMAP->decode_address_list($fvalue);
       $fvalue = '';
 
-      foreach ($to_addresses as $addr_part)
-      {
-        if (!empty($addr_part['mailto'])
-            && !in_array($addr_part['mailto'], $sa_recipients)
+      foreach ($to_addresses as $addr_part) {
+        if (empty($addr_part['mailto']))
+          continue;
+
+        $mailto = idn_to_utf8($addr_part['mailto']);
+
+        if (!in_array($mailto, $sa_recipients)
             && (!$MESSAGE->compose_from
-                || !in_array_nocase($addr_part['mailto'], $MESSAGE->compose_from)
-                || (count($to_addresses)==1 && $header=='to'))) // allow reply to yourself
-        {
-          $fvalue .= (strlen($fvalue) ? ', ':'').$addr_part['string'];
+                || !in_array_nocase($mailto, $MESSAGE->compose_from)
+                || (count($to_addresses)==1 && $header=='to')) // allow reply to yourself
+        ) {
+          if ($addr_part['name'] && $addr_part['mailto'] != $addr_part['name'])
+            $string = format_email_recipient($mailto, $addr_part['name']);
+          else
+            $string = $mailto;
+          $fvalue .= (strlen($fvalue) ? ', ':'') . $string;
           $sa_recipients[] = $addr_part['mailto'];
         }
       }
     }
   }
-  else if ($header && in_array($compose_mode, array(RCUBE_COMPOSE_DRAFT, RCUBE_COMPOSE_EDIT)))
-  {
+  else if ($header && in_array($compose_mode, array(RCUBE_COMPOSE_DRAFT, RCUBE_COMPOSE_EDIT))) {
     // get drafted headers
     if ($header=='to' && !empty($MESSAGE->headers->to))
       $fvalue = $MESSAGE->get_header('to');
-
-    if ($header=='cc' && !empty($MESSAGE->headers->cc))
+    else if ($header=='cc' && !empty($MESSAGE->headers->cc))
       $fvalue = $MESSAGE->get_header('cc');
-
-    if ($header=='bcc' && !empty($MESSAGE->headers->bcc))
+    else if ($header=='bcc' && !empty($MESSAGE->headers->bcc))
       $fvalue = $MESSAGE->get_header('bcc');
+    else if ($header=='reply-to' && !empty($MESSAGE->headers->replyto))
+      $fvalue = $MESSAGE->get_header('reply-to');
+    else if ($header=='mail-reply-to' && !empty($MESSAGE->headers->others['mail-reply-to']))
+      $fvalue = $MESSAGE->get_header('followup-to');
+    else if ($header=='mail-followup-to' && !empty($MESSAGE->headers->others['mail-followup-to']))
+      $fvalue = $MESSAGE->get_header('mail-followup-to');
+
+    $addresses = $IMAP->decode_address_list($fvalue);
+    $fvalue = '';
+
+    foreach ($addresses as $addr_part) {
+      if (empty($addr_part['mailto']))
+        continue;
+
+      $mailto = idn_to_utf8($addr_part['mailto']);
+
+      if ($addr_part['name'] && $addr_part['mailto'] != $addr_part['name'])
+        $string = format_email_recipient($mailto, $addr_part['name']);
+      else
+        $string = $mailto;
+      $fvalue .= (strlen($fvalue) ? ', ':'') . $string;
+    }
   }
 
-        
   if ($fname && $field_type)
   {
     // pass the following attributes to the form class
@@ -317,7 +394,7 @@
   if ($form_start)
     $out = $form_start.$out;
 
-  return $out;  
+  return $out;
 }
 
 
@@ -341,7 +418,7 @@
     foreach ($a_to as $addr)
     {
       if (!empty($addr['mailto']))
-        $a_recipients[] = mb_strtolower($addr['mailto']);
+        $a_recipients[] = strtolower($addr['mailto']);
     }
 
     if (!empty($MESSAGE->headers->cc))
@@ -350,7 +427,7 @@
       foreach ($a_cc as $addr)
       {
         if (!empty($addr['mailto']))
-          $a_recipients[] = mb_strtolower($addr['mailto']);
+          $a_recipients[] = strtolower($addr['mailto']);
       }
     }
   }
@@ -360,16 +437,17 @@
 
   if (count($user_identities))
   {
-    $from_id = 0;
     $a_signatures = array();
 
     $field_attrib['onchange'] = JS_OBJECT_NAME.".change_identity(this)";
     $select_from = new html_select($field_attrib);
 
+    // create SELECT element
     foreach ($user_identities as $sql_arr)
     {
+      $email = mb_strtolower(idn_to_utf8($sql_arr['email']));
       $identity_id = $sql_arr['identity_id'];
-      $select_from->add(format_email_recipient($sql_arr['email'], $sql_arr['name']), $identity_id);
+      $select_from->add(format_email_recipient($email, $sql_arr['name']), $identity_id);
 
       // add signature to array
       if (!empty($sql_arr['signature']) && empty($_SESSION['compose']['param']['nosig']))
@@ -384,24 +462,39 @@
       }
 
       if ($compose_mode == RCUBE_COMPOSE_REPLY && is_array($MESSAGE->compose_from))
-        $MESSAGE->compose_from[] = $sql_arr['email'];
-
-      if (empty($_POST['_from']) && empty($_SESSION['compose']['param']['from']))
-      {
-        // set draft's identity
-        if ($compose_mode == RCUBE_COMPOSE_DRAFT && strstr($MESSAGE->headers->from, $sql_arr['email']))
-          $from_id = $sql_arr['identity_id'];
-        // set identity if it's one of the reply-message recipients (with prio for default identity)
-        else if (in_array(mb_strtolower($sql_arr['email']), $a_recipients) && (empty($from_id) || $sql_arr['standard']))
-          $from_id = $sql_arr['identity_id'];
-      }
+        $MESSAGE->compose_from[] = $email;
     }
+
+    $from_id = 0;
 
     // overwrite identity selection with post parameter
     if (!empty($_POST['_from']))
       $from_id = get_input_value('_from', RCUBE_INPUT_POST);
     else if (!empty($_SESSION['compose']['param']['from']))
       $from_id = $_SESSION['compose']['param']['from'];
+    else if (count($user_identities) > 1) {
+      $return_path = $MESSAGE->headers->others['return-path'];
+
+      // Set identity
+      foreach ($user_identities as $sql_arr) {
+        // set draft's identity
+        if ($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT) {
+          if ($MESSAGE->headers->from == format_email_recipient($sql_arr['email'], $sql_arr['name'])) {
+            $from_id = $sql_arr['identity_id'];
+            break;
+          }
+        }
+        // set identity if it's one of the reply-message recipients (with prio for default identity)
+        else if (in_array($sql_arr['email'], $a_recipients) && (empty($from_id) || $sql_arr['standard']))
+          $from_id = $sql_arr['identity_id'];
+        // set identity when replying to mailing list
+        else if (strpos($return_path, str_replace('@', '=', $sql_arr['email']).'@') !== false)
+          $from_id = $sql_arr['identity_id'];
+
+        if ($from_id)
+          break;
+      }
+    }
 
     $out = $select_from->show($from_id);
 
@@ -418,48 +511,79 @@
 }
 
 
+function rcmail_compose_editor_mode()
+{
+  global $RCMAIL, $MESSAGE, $compose_mode;
+  static $useHtml;
+
+  if ($useHtml !== null)
+    return $useHtml;
+
+  $html_editor = intval($RCMAIL->config->get('htmleditor'));
+
+  if ($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT) {
+    $useHtml = $MESSAGE->has_html_part();
+  }
+  else if ($compose_mode == RCUBE_COMPOSE_REPLY) {
+    $useHtml = ($html_editor == 1 || ($html_editor == 2 && $MESSAGE->has_html_part()));
+  }
+  else { // RCUBE_COMPOSE_FORWARD or NEW
+    $useHtml = ($html_editor == 1);
+  }
+
+  return $useHtml;
+}
+
+
 function rcmail_prepare_message_body()
 {
-  global $RCMAIL, $CONFIG, $MESSAGE, $compose_mode, $LINE_LENGTH, $HTML_MODE;
-
-  if ($CONFIG['htmleditor'] || (($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT) && $MESSAGE->has_html_part()))
-    $isHtml = true;
-  else
-    $isHtml = false;
-
-  $body = '';
+  global $RCMAIL, $MESSAGE, $compose_mode, $LINE_LENGTH, $HTML_MODE;
 
   // use posted message body
-  if (!empty($_POST['_message']))
-  {
+  if (!empty($_POST['_message'])) {
     $body = get_input_value('_message', RCUBE_INPUT_POST, true);
+    $isHtml = (bool) get_input_value('_is_html', RCUBE_INPUT_POST);
   }
-  else if ($_SESSION['compose']['param']['body'])
-  {
+  else if ($_SESSION['compose']['param']['body']) {
     $body = $_SESSION['compose']['param']['body'];
     $isHtml = false;
   }
-  else if ($compose_mode)
-  {
+  // reply/edit/draft/forward
+  else if ($compose_mode) {
     $has_html_part = $MESSAGE->has_html_part();
-    if (($isHtml || $compose_mode == RCUBE_COMPOSE_DRAFT) && $has_html_part)
-    {
-      $body = $MESSAGE->first_html_part();
-      $isHtml = true;
+    $isHtml = rcmail_compose_editor_mode();
+
+    if ($isHtml) {
+      if ($has_html_part) {
+        $body = $MESSAGE->first_html_part();
+      }
+      else {
+        $body = $MESSAGE->first_text_part();
+        // try to remove the signature
+        if ($RCMAIL->config->get('strip_existing_sig', true))
+          $body = rcmail_remove_signature($body);
+        // add HTML formatting
+        $body = rcmail_plain_body($body);
+        if ($body)
+          $body = '<pre>' . $body . '</pre>';
+      }
     }
-    else if ($has_html_part)
-    {
-      // use html part if it has been used for message (pre)viewing
-      // decrease line length for quoting
-      $len = $compose_mode == RCUBE_COMPOSE_REPLY ? $LINE_LENGTH-2 : $LINE_LENGTH;
-      $txt = new html2text($MESSAGE->first_html_part(), false, true, $len);
-      $body = $txt->get_text();
-      $isHtml = false;
-    }
-    else
-    {
-      $body = $MESSAGE->first_text_part();
-      $isHtml = false;
+    else {
+      if ($has_html_part) {
+        // use html part if it has been used for message (pre)viewing
+        // decrease line length for quoting
+        $len = $compose_mode == RCUBE_COMPOSE_REPLY ? $LINE_LENGTH-2 : $LINE_LENGTH;
+        $txt = new html2text($MESSAGE->first_html_part(), false, true, $len);
+        $body = $txt->get_text();
+      }
+      else {
+        $body = $MESSAGE->first_text_part($part);
+        if ($body && $part && $part->ctype_secondary == 'plain'
+            && $part->ctype_parameters['format'] == 'flowed'
+        ) {
+          $body = rcube_message::unfold_flowed($body);
+        }
+      }
     }
 
     // compose reply-body
@@ -471,6 +595,9 @@
     // load draft message body
     else if ($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT)
       $body = rcmail_create_draft_body($body, $isHtml);
+  }
+  else { // new message
+    $isHtml = rcmail_compose_editor_mode();
   }
 
   $plugin = $RCMAIL->plugins->exec_hook('message_compose_body',
@@ -519,9 +646,20 @@
   $out .= $msgtype->show();
 
   // If desired, set this textarea to be editable by TinyMCE
-  if ($isHtml) $attrib['class'] = 'mce_editor';
-  $textarea = new html_textarea($attrib);
-  $out .= $textarea->show($MESSAGE_BODY);
+  if ($isHtml) {
+    $attrib['class'] = 'mce_editor';
+    $textarea = new html_textarea($attrib);
+    $out .= $textarea->show($MESSAGE_BODY);
+  }
+  else {
+    $textarea = new html_textarea($attrib);
+    $out .= $textarea->show('');
+    // quote plain text, inject into textarea
+    $table = get_html_translation_table(HTML_SPECIALCHARS);
+    $MESSAGE_BODY = strtr($MESSAGE_BODY, $table);
+    $out = substr($out, 0, -11) . $MESSAGE_BODY . '</textarea>';
+  }
+
   $out .= $form_end ? "\n$form_end" : '';
 
   $OUTPUT->set_env('composebody', $attrib['id']);
@@ -600,23 +738,24 @@
 {
   global $RCMAIL, $MESSAGE, $LINE_LENGTH;
 
+  // build reply prefix
+  $from = array_pop($RCMAIL->imap->decode_address_list($MESSAGE->get_header('from')));
+  $prefix = sprintf("On %s, %s wrote:",
+    $MESSAGE->headers->date, $from['name'] ? $from['name'] : idn_to_utf8($from['email']));
+
   if (!$bodyIsHtml) {
+    $body = preg_replace('/\r?\n/', "\n", $body);
+
     // try to remove the signature
-    if ($RCMAIL->config->get('strip_existing_sig', true) && ($sp = strrpos($body, '-- ')) !== false && ($sp == 0 || $body{$sp-1} == "\n")) {
-      if ($body{$sp+3}==' ' || $body{$sp+3}=="\n" || $body{$sp+3}=="\r")
-        $body = substr($body, 0, max(0, $sp-1));
-    }
+    if ($RCMAIL->config->get('strip_existing_sig', true))
+      $body = rcmail_remove_signature($body);
 
     // soft-wrap and quote message text
-    $body = rcmail_wrap_and_quote(rtrim($body, "\r\n"), $LINE_LENGTH);
+    $body = rcmail_wrap_and_quote(rtrim($body, "\n"), $LINE_LENGTH);
 
-    // add title line(s)
-    $prefix = sprintf("On %s, %s wrote:\n",
-      $MESSAGE->headers->date,
-      $MESSAGE->get_header('from'));
-
+    $prefix .= "\n";
     $suffix = '';
-    
+
     if ($RCMAIL->config->get('top_posting'))
       $prefix = "\n\n\n" . $prefix;
   }
@@ -629,9 +768,7 @@
     $body = rcmail_wash_html($body, array('safe' => $MESSAGE->is_safe), $cid_map);
 
     // build reply (quote content)
-    $prefix = sprintf("<p>On %s, %s wrote:</p>\n",
-      $MESSAGE->headers->date,
-      htmlspecialchars(Q($MESSAGE->get_header('from'), 'replace'), ENT_COMPAT, $RCMAIL->output->get_charset()));
+    $prefix = '<p>' . Q($prefix) . "</p>\n";
     $prefix .= '<blockquote>';
 
     if ($RCMAIL->config->get('top_posting')) {
@@ -725,11 +862,32 @@
   
   return $body;
 }
-  
-  
+
+
+function rcmail_remove_signature($body)
+{
+  global $RCMAIL;
+
+  $len = strlen($body);
+  $sig_max_lines = $RCMAIL->config->get('sig_max_lines', 15);
+
+  while (($sp = strrpos($body, "-- \n", $sp ? -$len+$sp-1 : 0)) !== false) {
+    if ($sp == 0 || $body[$sp-1] == "\n") {
+      // do not touch blocks with more that X lines
+      if (substr_count($body, "\n", $sp) < $sig_max_lines) {
+        $body = substr($body, 0, max(0, $sp-1));
+      }
+      break;
+    }
+  }
+
+  return $body;
+}
+
+
 function rcmail_write_compose_attachments(&$message, $bodyIsHtml)
 {
-  global $OUTPUT;
+  global $RCMAIL;
 
   $cid_map = $messages = array();
   foreach ((array)$message->mime_parts as $pid => $part)
@@ -753,7 +911,7 @@
       if (!$skip && ($attachment = rcmail_save_attachment($message, $pid))) {
         $_SESSION['compose']['attachments'][$attachment['id']] = $attachment;
         if ($bodyIsHtml && ($part->content_id || $part->content_location)) {
-          $url = $OUTPUT->app->comm_path.'&_action=display-attachment&_file=rcmfile'.$attachment['id'];
+          $url = $RCMAIL->comm_path.'&_action=display-attachment&_file=rcmfile'.$attachment['id'];
           if ($part->content_id)
             $cid_map['cid:'.$part->content_id] = $url;
           else
@@ -771,14 +929,14 @@
 
 function rcmail_write_inline_attachments(&$message)
 {
-  global $OUTPUT;
+  global $RCMAIL;
 
   $cid_map = array();
   foreach ((array)$message->mime_parts as $pid => $part) {
     if (($part->content_id || $part->content_location) && $part->filename) {
       if ($attachment = rcmail_save_attachment($message, $pid)) {
         $_SESSION['compose']['attachments'][$attachment['id']] = $attachment;
-        $url = $OUTPUT->app->comm_path.'&_action=display-attachment&_file=rcmfile'.$attachment['id'];
+        $url = $RCMAIL->comm_path.'&_action=display-attachment&_file=rcmfile'.$attachment['id'];
         if ($part->content_id)
           $cid_map['cid:'.$part->content_id] = $url;
         else
@@ -820,7 +978,7 @@
     'size' => $path ? filesize($path) : strlen($data),
   );
 
-  $attachment = rcmail::get_instance()->plugins->exec_hook('save_attachment', $attachment);
+  $attachment = rcmail::get_instance()->plugins->exec_hook('attachment_save', $attachment);
 
   if ($attachment['status']) {
     unset($attachment['data'], $attachment['status'], $attachment['content_id'], $attachment['abort']);
@@ -844,7 +1002,7 @@
     'size' => strlen($data),
   );
 
-  $attachment = rcmail::get_instance()->plugins->exec_hook('save_attachment', $attachment);
+  $attachment = rcmail::get_instance()->plugins->exec_hook('attachment_save', $attachment);
 
   if ($attachment['status']) {
     unset($attachment['data'], $attachment['status'], $attachment['content_id'], $attachment['abort']);
@@ -906,7 +1064,7 @@
   $out = $form_start ? "$form_start\n" : '';
   $out .= $textfield->show($subject);
   $out .= $form_end ? "\n$form_end" : '';
-         
+
   return $out;
 }
 
@@ -981,11 +1139,11 @@
   $button = new html_inputfield(array('type' => 'button'));
   
   $out = html::div($attrib,
-    $OUTPUT->form_tag(array('name' => 'form', 'method' => 'post', 'enctype' => 'multipart/form-data'),
+    $OUTPUT->form_tag(array('name' => 'uploadform', 'method' => 'post', 'enctype' => 'multipart/form-data'),
       html::div(null, rcmail_compose_attachment_field(array('size' => $attrib[attachmentfieldsize]))) .
       html::div('hint', rcube_label(array('name' => 'maxuploadsize', 'vars' => array('size' => $max_filesize)))) .
       html::div('buttons',
-//        $button->show(rcube_label('close'), array('class' => 'button', 'onclick' => "$('#$attrib[id]').hide()")) . ' ' .
+        $button->show(rcube_label('close'), array('class' => 'button', 'onclick' => "$('#$attrib[id]').hide()")) . ' ' .
         $button->show(rcube_label('upload'), array('class' => 'button mainaction', 'onclick' => JS_OBJECT_NAME . ".command('send-attachment', this.form)"))
       )
     )
@@ -1039,7 +1197,7 @@
 
 function rcmail_receipt_checkbox($attrib)
 {
-  global $MESSAGE, $compose_mode;
+  global $RCMAIL, $MESSAGE, $compose_mode;
 
   list($form_start, $form_end) = get_form_tags($attrib);
   unset($attrib['form']);
@@ -1051,9 +1209,35 @@
   $attrib['value'] = '1';
   $checkbox = new html_checkbox($attrib);
 
+  if ($MESSAGE && in_array($compose_mode, array(RCUBE_COMPOSE_DRAFT, RCUBE_COMPOSE_EDIT)))
+    $mdn_default = (bool) $MESSAGE->headers->mdn_to;
+  else
+    $mdn_default = $RCMAIL->config->get('mdn_default');
+
   $out = $form_start ? "$form_start\n" : '';
-  $out .= $checkbox->show(in_array($compose_mode, array(RCUBE_COMPOSE_DRAFT, RCUBE_COMPOSE_EDIT))
-	&& $MESSAGE->headers->mdn_to ? 1 : 0);
+  $out .= $checkbox->show($mdn_default);
+  $out .= $form_end ? "\n$form_end" : '';
+
+  return $out;
+}
+
+
+function rcmail_dsn_checkbox($attrib)
+{
+  global $RCMAIL;
+
+  list($form_start, $form_end) = get_form_tags($attrib);
+  unset($attrib['form']);
+
+  if (!isset($attrib['id']))
+    $attrib['id'] = 'dsn';
+
+  $attrib['name'] = '_dsn';
+  $attrib['value'] = '1';
+  $checkbox = new html_checkbox($attrib);
+
+  $out = $form_start ? "$form_start\n" : '';
+  $out .= $checkbox->show($RCMAIL->config->get('dsn_default'));
   $out .= $form_end ? "\n$form_end" : '';
 
   return $out;
@@ -1065,11 +1249,7 @@
   global $CONFIG, $MESSAGE, $compose_mode;
 
   // determine whether HTML or plain text should be checked
-  if ($compose_mode)
-    $useHtml = (($CONFIG['htmleditor'] || $compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT)
-	&& $MESSAGE->has_html_part());
-  else
-    $useHtml = $CONFIG['htmleditor'] ? true : false;
+  $useHtml = rcmail_compose_editor_mode();
 
   if (empty($attrib['editorid']))
     $attrib['editorid'] = 'rcmComposeBody';
@@ -1086,8 +1266,7 @@
 
   return $select->show($useHtml ? 'html' : 'plain');
 
-  foreach ($choices as $value => $text)
-  {
+  foreach ($choices as $value => $text) {
     $attrib['id'] = '_' . $value;
     $attrib['value'] = $value;
     $selector .= $radio->show($chosenvalue, $attrib) . html::label($attrib['id'], Q(rcube_label($text)));
@@ -1105,6 +1284,26 @@
 }
 
 
+function rcmail_check_sent_folder($folder, $create=false)
+{
+  global $IMAP;
+
+  if ($IMAP->mailbox_exists($folder, true)) {
+    return true;
+  }
+
+  // folder may exist but isn't subscribed (#1485241)
+  if ($create) {
+    if (!$IMAP->mailbox_exists($folder))
+      return $IMAP->create_mailbox($folder, true);
+    else
+      return $IMAP->subscribe($folder);
+  }
+
+  return false;
+}
+
+
 function get_form_tags($attrib)
 {
   global $RCMAIL, $MESSAGE_FORM;
@@ -1118,13 +1317,13 @@
     $form_start = empty($attrib['form']) ? $RCMAIL->output->form_tag(array('name' => "form", 'method' => "post")) : '';
     $form_start .= $hiddenfields->show();
   }
-    
+
   $form_end = ($MESSAGE_FORM && !strlen($attrib['form'])) ? '</form>' : '';
   $form_name = !empty($attrib['form']) ? $attrib['form'] : 'form';
-  
+
   if (!$MESSAGE_FORM)
     $RCMAIL->output->add_gui_object('messageform', $form_name);
-  
+
   $MESSAGE_FORM = $form_name;
 
   return array($form_start, $form_end);
@@ -1142,6 +1341,7 @@
   'priorityselector' => 'rcmail_priority_selector',
   'editorselector' => 'rcmail_editor_selector',
   'receiptcheckbox' => 'rcmail_receipt_checkbox',
+  'dsncheckbox' => 'rcmail_dsn_checkbox',
   'storetarget' => 'rcmail_store_target_selection',
 ));
 

--
Gitblit v1.9.1