From ef1e877a8dd419fcb0de772bf0c40eeca76ee7eb Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Sat, 25 Feb 2012 13:15:25 -0500
Subject: [PATCH] Connect to IMAP server before reading quota

---
 program/lib/html2text.php |  101 +++++++++++++++++++++++++++++---------------------
 1 files changed, 59 insertions(+), 42 deletions(-)

diff --git a/program/lib/html2text.php b/program/lib/html2text.php
index 0171f4b..35120a7 100644
--- a/program/lib/html2text.php
+++ b/program/lib/html2text.php
@@ -249,11 +249,11 @@
      *  @access public
      */
     var $callback_search = array(
+        '/<(a) [^>]*href=("|\')([^"\']+)\2[^>]*>(.*?)<\/a>/i',
+                                                   // <a href="">
         '/<(h)[123456][^>]*>(.*?)<\/h[123456]>/i', // H1 - H3
         '/<(b)[^>]*>(.*?)<\/b>/i',                 // <b>
         '/<(strong)[^>]*>(.*?)<\/strong>/i',       // <strong>
-        '/<(a) [^>]*href=("|\')([^"\']+)\2[^>]*>(.*?)<\/a>/i',
-                                                   // <a href="">
         '/<(th)[^>]*>(.*?)<\/th>/i',               // <th> and </th>
     );
 
@@ -317,21 +317,11 @@
     /**
      *  Contains URL addresses from links to be rendered in plain text.
      *
-     *  @var string $_link_list
+     *  @var array $_link_list
      *  @access private
      *  @see _build_link_list()
      */
-    var $_link_list = '';
-
-    /**
-     *  Number of valid links detected in the text, used for plain text
-     *  display (rendered similar to footnotes).
-     *
-     *  @var integer $_link_count
-     *  @access private
-     *  @see _build_link_list()
-     */
-    var $_link_count = 0;
+    var $_link_list = array();
 
     /**
      * Boolean flag, true if a table of link URLs should be listed after the text.
@@ -472,8 +462,7 @@
     function _convert()
     {
         // Variables used for building the link list
-        $this->_link_count = 0;
-        $this->_link_list = '';
+        $this->_link_list = array();
 
         $text = trim(stripslashes($this->html));
 
@@ -481,8 +470,11 @@
         $this->_converter($text);
 
         // Add link list
-        if ( !empty($this->_link_list) ) {
-            $text .= "\n\nLinks:\n------\n" . $this->_link_list;
+        if (!empty($this->_link_list)) {
+            $text .= "\n\nLinks:\n------\n";
+            foreach ($this->_link_list as $idx => $url) {
+                $text .= '[' . ($idx+1) . '] ' . $url . "\n";
+            }
         }
 
         $this->text = $text;
@@ -563,28 +555,32 @@
      */
     function _build_link_list( $link, $display )
     {
-	    if ( !$this->_do_links )
+	    if (!$this->_do_links || empty($link)) {
 	        return $display;
+	    }
 
-	    if ( preg_match('!^(https?://|mailto:)!', $link) ) {
-            $this->_link_count++;
-            $this->_link_list .= '[' . $this->_link_count . "] $link\n";
-            $additional = ' [' . $this->_link_count . ']';
-	    } elseif ( substr($link, 0, 11) == 'javascript:' ) {
-		    // Don't count the link; ignore it
-		    $additional = '';
-		// what about href="#anchor" ?
-        } else {
-            $this->_link_count++;
-            $this->_link_list .= '[' . $this->_link_count . '] ' . $this->url;
-            if ( substr($link, 0, 1) != '/' ) {
-                $this->_link_list .= '/';
-            }
-            $this->_link_list .= "$link\n";
-            $additional = ' [' . $this->_link_count . ']';
+        // Ignored link types
+	    if (preg_match('!^(javascript|mailto|#):!i', $link)) {
+		    return $display;
         }
 
-        return $display . $additional;
+	    if (preg_match('!^(https?://)!i', $link)) {
+            $url = $link;
+        }
+        else {
+            $url = $this->url;
+            if (substr($link, 0, 1) != '/') {
+                $url .= '/';
+            }
+            $url .= "$link";
+        }
+
+        if (($index = array_search($url, $this->_link_list)) === false) {
+            $this->_link_list[] = $url;
+            $index = count($this->_link_list);
+        }
+
+        return $display . ' [' . ($index+1) . ']';
     }
 
     /**
@@ -675,11 +671,11 @@
         switch($matches[1]) {
         case 'b':
         case 'strong':
-            return $this->_strtoupper($matches[2]);
+            return $this->_toupper($matches[2]);
         case 'th':
-            return $this->_strtoupper("\t\t". $matches[2] ."\n");
+            return $this->_toupper("\t\t". $matches[2] ."\n");
         case 'h':
-            return $this->_strtoupper("\n\n". $matches[2] ."\n\n");
+            return $this->_toupper("\n\n". $matches[2] ."\n\n");
         case 'a':
             // Remove spaces in URL (#1487805)
             $url = str_replace(' ', '', $matches[3]);
@@ -699,10 +695,31 @@
     }
 
     /**
-     *  Strtoupper multibyte wrapper function with HTML entities handling
+     * Strtoupper function with HTML tags and entities handling.
      *
-     *  @param string $str Text to convert
-     *  @return string Converted text
+     * @param string $str Text to convert
+     * @return string Converted text
+     */
+    private function _toupper($str)
+    {
+        // string can containg HTML tags
+        $chunks = preg_split('/(<[^>]*>)/', $str, null, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
+
+        // convert toupper only the text between HTML tags
+        foreach ($chunks as $idx => $chunk) {
+            if ($chunk[0] != '<') {
+                $chunks[$idx] = $this->_strtoupper($chunk);
+            }
+        }
+
+        return implode($chunks);
+    }
+
+    /**
+     * Strtoupper multibyte wrapper function with HTML entities handling.
+     *
+     * @param string $str Text to convert
+     * @return string Converted text
      */
     private function _strtoupper($str)
     {

--
Gitblit v1.9.1