From c9f67353cfe608247d6b820e5f8534ab2b066227 Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Sun, 23 Jan 2011 04:57:57 -0500
Subject: [PATCH] - Add part MIME ID to message_part_* hooks (#1487718)

---
 program/steps/mail/func.inc |  169 ++++++++++++++++++++++++-------------------------------
 1 files changed, 74 insertions(+), 95 deletions(-)

diff --git a/program/steps/mail/func.inc b/program/steps/mail/func.inc
index 11df128..d56f0f3 100644
--- a/program/steps/mail/func.inc
+++ b/program/steps/mail/func.inc
@@ -5,7 +5,7 @@
  | program/steps/mail/func.inc                                           |
  |                                                                       |
  | This file is part of the Roundcube Webmail client                     |
- | Copyright (C) 2005-2010, Roundcube Dev. - Switzerland                 |
+ | Copyright (C) 2005-2010, The Roundcube Dev Team                       |
  | Licensed under the GNU GPL                                            |
  |                                                                       |
  | PURPOSE:                                                              |
@@ -46,7 +46,7 @@
 }
 
 // set imap properties and session vars
-if ($mbox = get_input_value('_mbox', RCUBE_INPUT_GPC))
+if (strlen(trim($mbox = get_input_value('_mbox', RCUBE_INPUT_GPC, true))))
   $IMAP->set_mailbox(($_SESSION['mbox'] = $mbox));
 else if ($IMAP)
   $_SESSION['mbox'] = $IMAP->get_mailbox_name();
@@ -102,14 +102,10 @@
   // set current mailbox and some other vars in client environment
   $OUTPUT->set_env('mailbox', $mbox_name);
   $OUTPUT->set_env('pagesize', $IMAP->page_size);
-  $OUTPUT->set_env('quota', $IMAP->get_capability('quota'));
+  $OUTPUT->set_env('quota', $IMAP->get_capability('QUOTA'));
   $OUTPUT->set_env('delimiter', $IMAP->get_hierarchy_delimiter());
   $OUTPUT->set_env('threading', (bool) $IMAP->threading);
-  $OUTPUT->set_env('threads', $IMAP->threading
-	|| $IMAP->get_capability('thread=references')
-        || $IMAP->get_capability('thread=orderedsubject')
-        || $IMAP->get_capability('thread=refs')  
-  );
+  $OUTPUT->set_env('threads', $IMAP->threading || $IMAP->get_capability('THREAD'));
 
   if ($CONFIG['flag_for_deletion'])
     $OUTPUT->set_env('flag_for_deletion', true);
@@ -277,7 +273,7 @@
       if (in_array($col, array('from', 'to', 'cc', 'replyto')))
         $cont = Q(rcmail_address_string($header->$col, 3), 'show');
       else if ($col=='subject') {
-        $cont = abbreviate_string(trim($IMAP->decode_header($header->$col)), 160);
+        $cont = trim($IMAP->decode_header($header->$col));
         if (!$cont) $cont = rcube_label('nosubject');
         $cont = Q($cont);
       }
@@ -423,86 +419,22 @@
 
 function rcmail_messagecount_display($attrib)
   {
-  global $IMAP, $OUTPUT;
+  global $RCMAIL;
 
   if (!$attrib['id'])
     $attrib['id'] = 'rcmcountdisplay';
 
-  $OUTPUT->add_gui_object('countdisplay', $attrib['id']);
+  $RCMAIL->output->add_gui_object('countdisplay', $attrib['id']);
 
-  return html::span($attrib, rcmail_get_messagecount_text());
-  }
+  $content =  $RCMAIL->action != 'show' ? rcmail_get_messagecount_text() : rcube_label('loading');
 
-
-function rcmail_quota_display($attrib)
-  {
-  global $OUTPUT;
-
-  if (!$attrib['id'])
-    $attrib['id'] = 'rcmquotadisplay';
-
-  if(isset($attrib['display']))
-    $_SESSION['quota_display'] = $attrib['display'];
-
-  $OUTPUT->add_gui_object('quotadisplay', $attrib['id']);
-
-  $quota = rcmail_quota_content($attrib);
-
-  $OUTPUT->add_script('$(document).ready(function(){
-	rcmail.set_quota('.json_serialize($quota).')});', 'foot');
-
-  return html::span($attrib, '');
-  }
-
-
-function rcmail_quota_content($attrib=NULL)
-  {
-  global $COMM_PATH, $RCMAIL;
-
-  $quota = $RCMAIL->imap->get_quota();
-  $quota = $RCMAIL->plugins->exec_hook('quota', $quota);
-
-  $quota_result = (array) $quota;
-  $quota_result['type'] = isset($_SESSION['quota_display']) ? $_SESSION['quota_display'] : '';
-
-  if (!$quota['total'] && $RCMAIL->config->get('quota_zero_as_unlimited')) {
-    $quota_result['title'] = rcube_label('unlimited');
-    $quota_result['percent'] = 0;
-    }
-  else if ($quota['total']) {
-    if (!isset($quota['percent']))
-      $quota_result['percent'] = min(100, round(($quota['used']/max(1,$quota['total']))*100));
-
-    $title = sprintf('%s / %s (%.0f%%)',
-        show_bytes($quota['used'] * 1024), show_bytes($quota['total'] * 1024),
-        $quota_result['percent']);
-
-    $quota_result['title'] = $title;
-
-    if ($attrib['width'])
-      $quota_result['width'] = $attrib['width'];
-    if ($attrib['height'])
-      $quota_result['height']	= $attrib['height'];
-    }
-  else {
-    $quota_result['title'] = rcube_label('unknown');
-    $quota_result['percent'] = 0;
-    }
-
-  return $quota_result;
+  return html::span($attrib, $content);
   }
 
 
 function rcmail_get_messagecount_text($count=NULL, $page=NULL)
   {
-  global $RCMAIL, $IMAP, $MESSAGE;
-
-  if (isset($MESSAGE->index))
-    {
-    return rcube_label(array('name' => 'messagenrof',
-        'vars' => array('nr'  => $MESSAGE->index+1,
-        'count' => $count!==NULL ? $count : $IMAP->messagecount(NULL, 'ALL')))); // Only messages, no threads here
-    }
+  global $RCMAIL, $IMAP;
 
   if ($page===NULL)
     $page = $IMAP->list_page;
@@ -550,7 +482,7 @@
 {
   global $RCMAIL;
 
-  $old_unseen = $_SESSION['unseen_count'][$mbox_name];
+  $old_unseen = rcmail_get_unseen_count($mbox_name);
 
   if ($count === null)
     $unseen = $RCMAIL->imap->messagecount($mbox_name, 'UNSEEN', $force);
@@ -560,10 +492,30 @@
   if ($unseen != $old_unseen || ($mbox_name == 'INBOX'))
     $RCMAIL->output->command('set_unread_count', $mbox_name, $unseen, ($mbox_name == 'INBOX'));
 
-  // @TODO: this data is doubled (session and cache tables) if caching is enabled
-  $_SESSION['unseen_count'][$mbox_name] = $unseen;
+  rcmail_set_unseen_count($mbox_name, $unseen);
 
   return $unseen;
+}
+
+
+function rcmail_set_unseen_count($mbox_name, $count)
+{
+  // @TODO: this data is doubled (session and cache tables) if caching is enabled
+
+  // Make sure we have an array here (#1487066)
+  if (!is_array($_SESSION['unseen_count']))
+    $_SESSION['unseen_count'] = array();
+
+  $_SESSION['unseen_count'][$mbox_name] = $count;
+}
+
+
+function rcmail_get_unseen_count($mbox_name)
+{
+  if (is_array($_SESSION['unseen_count']) && array_key_exists($mbox_name, $_SESSION['unseen_count']))
+    return $_SESSION['unseen_count'][$mbox_name];
+  else
+    return null;
 }
 
 
@@ -713,7 +665,8 @@
 
   // trigger plugin hook
   $data = $RCMAIL->plugins->exec_hook('message_part_before',
-    array('type' => $part->ctype_secondary, 'body' => $part->body) + $p + array('safe' => false, 'plain' => false, 'inline_html' => true));
+    array('type' => $part->ctype_secondary, 'body' => $part->body, 'id' => $part->mime_id)
+        + $p + array('safe' => false, 'plain' => false, 'inline_html' => true));
 
   // convert html to text/plain
   if ($data['type'] == 'html' && $data['plain']) {
@@ -746,7 +699,8 @@
     $body = rcmail_plain_body($body, $part->ctype_parameters['format'] == 'flowed');
 
   // allow post-processing of the message body
-  $data = $RCMAIL->plugins->exec_hook('message_part_after', array('type' => $part->ctype_secondary, 'body' => $body) + $data);
+  $data = $RCMAIL->plugins->exec_hook('message_part_after',
+    array('type' => $part->ctype_secondary, 'body' => $body, 'id' => $part->mime_id) + $data);
 
   return $data['type'] == 'html' ? $data['body'] : html::tag('pre', array(), $data['body']);
 }
@@ -836,8 +790,12 @@
     $quote_level = $q;
   }
 
-  // quote plain text
-  $body = Q(join("\n", $a_lines), 'dummy', false);
+  $body = join("\n", $a_lines);
+
+  // quote plain text (don't use Q() here, to display entities "as is")
+  $table = get_html_translation_table(HTML_SPECIALCHARS);
+  unset($table['?']);
+  $body = strtr($body, $table);
 
   // colorize signature (up to <sig_max_lines> lines)
   $len = strlen($body);
@@ -967,17 +925,21 @@
     else
       $header_value = trim($IMAP->decode_header($value));
 
-    $output_headers[$hkey] = array('title' => rcube_label($hkey), 'value' => $header_value, 'raw' => $value);
+    $output_headers[$hkey] = array(
+        'title' => rcube_label(preg_replace('/(^mail-|-)/', '', $hkey)),
+        'value' => $header_value, 'raw' => $value
+    );
   }
 
-  $plugin = $RCMAIL->plugins->exec_hook('message_headers_output', array('output' => $output_headers, 'headers' => $MESSAGE->headers));
+  $plugin = $RCMAIL->plugins->exec_hook('message_headers_output',
+    array('output' => $output_headers, 'headers' => $MESSAGE->headers));
 
   // compose html table
   $table = new html_table(array('cols' => 2));
 
   foreach ($plugin['output'] as $hkey => $row) {
     $table->add(array('class' => 'header-title'), Q($row['title']));
-    $table->add(array('class' => $hkey, 'width' => "90%"), Q($row['value'], ($hkey == 'subject' ? 'strict' : 'show')));
+    $table->add(array('class' => 'header '.$hkey), Q($row['value'], ($hkey == 'subject' ? 'strict' : 'show')));
   }
 
   // all headers division
@@ -1179,20 +1141,22 @@
   $attributes = array();
 
   // Handle body attributes that doesn't play nicely with div elements
-  if (preg_match('/<div class="' . preg_quote($body_id, '/') . '" ([^>]+)/', $body, $m)) {
+  $regexp = '/<div class="' . preg_quote($body_id, '/') . '"([^>]*)/';
+  if (preg_match($regexp, $body, $m)) {
     $attrs = $m[0];
     // Get bgcolor, we'll set it as background-color of the message container
-    if (preg_match('/bgcolor=["\']*([a-z0-9#]+)["\']*/', $attrs, $mb)) {
+    if ($m[1] && preg_match('/bgcolor=["\']*([a-z0-9#]+)["\']*/', $attrs, $mb)) {
       $attributes['background-color'] = $mb[1];
       $attrs = preg_replace('/bgcolor=["\']*([a-z0-9#]+)["\']*/', '', $attrs);
     }
     // Get background, we'll set it as background-image of the message container
-    if (preg_match('/background=["\']*([^"\'>\s]+)["\']*/', $attrs, $mb)) {
+    if ($m[1] && preg_match('/background=["\']*([^"\'>\s]+)["\']*/', $attrs, $mb)) {
       $attributes['background-image'] = 'url('.$mb[1].')';
       $attrs = preg_replace('/background=["\']*([^"\'>\s]+)["\']*/', '', $attrs);
     }
-    if (!empty($attributes))
-      $body = preg_replace('/<div class="' . preg_quote($body_id, '/') . '" [^>]+/', rtrim($attrs), $body, 1);
+    if (!empty($attributes)) {
+      $body = preg_replace($regexp, rtrim($attrs), $body, 1);
+    }
 
     // handle body styles related to background image
     if ($attributes['background-image']) {
@@ -1208,8 +1172,9 @@
   }
   // make sure there's 'rcmBody' div, we need it for proper css modification
   // its name is hardcoded in rcmail_message_body() also
-  else
+  else {
     $body = '<div class="' . $body_id . '">' . $body . '</div>';
+  }
 
   return $body;
 }
@@ -1545,6 +1510,7 @@
       $delim   = $RCMAIL->config->header_delimiter();
       $to      = $headers_enc['To'];
       $subject = $headers_enc['Subject'];
+      $header_str = rtrim($header_str);
 
       if ($delim != "\r\n") {
         $header_str = str_replace("\r\n", $delim, $header_str);
@@ -1793,4 +1759,17 @@
   'searchform' => array($OUTPUT, 'search_form'),
 ));
 
-
+// register action aliases
+$RCMAIL->register_action_map(array(
+    'preview' => 'show.inc',
+    'print'   => 'show.inc',
+    'moveto'  => 'move_del.inc',
+    'delete'  => 'move_del.inc',
+    'send'    => 'sendmail.inc',
+    'expunge' => 'folders.inc',
+    'purge'   => 'folders.inc',
+    'remove-attachment'  => 'attachments.inc',
+    'display-attachment' => 'attachments.inc',
+    'upload'             => 'attachments.inc',
+    'group-expand'       => 'autocomplete.inc',
+));

--
Gitblit v1.9.1