Updates from my local tree.
authorMichael M Slusarz <slusarz@curecanti.org>
Mon, 10 Nov 2008 06:42:43 +0000 (23:42 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Mon, 10 Nov 2008 06:42:43 +0000 (23:42 -0700)
framework/Mime/lib/Horde/Mime/Viewer.php
framework/Mime/lib/Horde/Mime/Viewer/Driver.php
framework/Mime/lib/Horde/Mime/Viewer/html.php
framework/Mime/lib/Horde/Mime/Viewer/plain.php

index 4772d90..e450189 100644 (file)
@@ -25,7 +25,8 @@ class Horde_Mime_Viewer
     static protected $_config = array();
 
     /**
-     * The driver cache array.
+     * The driver cache array. This array is shared between all instances of
+     * Horde_MIME_Viewer.
      *
      * @var array
      */
@@ -53,6 +54,36 @@ class Horde_Mime_Viewer
     }
 
     /**
+     * Given a MIME type, this function will return an appropriate icon.
+     *
+     * @param string $mime_type  The MIME type that we need an icon for.
+     *
+     * @return string  The URL to the appropriate icon.
+     */
+    static final public function getIcon($mime_type)
+    {
+        $app = $GLOBALS['registry']->getApp();
+        $ob = self::_getIcon($mime_type, $app);
+
+        if (is_null($ob)) {
+            if ($app == 'horde') {
+                return null;
+            }
+
+            $obHorde = self::_getIcon($mime_type, 'horde');
+            return is_null($obHorde) ? null : $obHorde['url'];
+        } elseif (($ob['match'] !== 0) && ($app != 'horde')) {
+            $obHorde = self::_getIcon($mime_type, 'horde');
+            if (!is_null($ob['match']) &&
+                ($obHorde['match'] <= $ob['match'])) {
+                return $obHorde['url'];
+            }
+        }
+
+        return $ob['url'];
+    }
+
+    /**
      * Given a MIME type and an app name, determine which driver can best
      * handle the data.
      *
@@ -191,88 +222,6 @@ class Horde_Mime_Viewer
     }
 
     /**
-     * Prints out the status message for a given MIME Part.
-     *
-     * @param string $msg     The message to output.
-     * @param string $img     An image link to add to the beginning of the
-     *                        message.
-     * @param string $class   An optional style for the status box.
-     *
-     * @return string  The formatted status message string.
-     */
-    static public function formatStatusMsg($msg, $img = null, $class = null)
-    {
-        if (empty($msg)) {
-            return '';
-        }
-
-        if (!is_array($msg)) {
-            $msg = array($msg);
-        }
-
-        /* If we are viewing as an attachment, don't print HTML code. */
-        if (self::viewAsAttachment()) {
-            return implode("\n", $msg);
-        }
-
-        if (is_null($class)) {
-            $class = 'mimeStatusMessage';
-        }
-        $text = '<table class="' . $class . '">';
-
-        /* If no image, simply print out the message. */
-        if (is_null($img)) {
-            foreach ($msg as $val) {
-                $text .= '<tr><td>' . $val . '</td></tr>' . "\n";
-            }
-        } else {
-            $text .= '<tr><td class="mimeStatusIcon">' . $img . '</td><td>';
-            if (count($msg) == 1) {
-                $text .= $msg[0];
-            } else {
-                $text .= '<table>';
-                foreach ($msg as $val) {
-                    $text .= '<tr><td>' . $val . '</td></tr>' . "\n";
-                }
-                $text .= '</table>';
-            }
-            $text .= '</td></tr>' . "\n";
-        }
-
-        return $text . '</table>';
-    }
-
-    /**
-     * Given a MIME type, this function will return an appropriate icon.
-     *
-     * @param string $mime_type  The MIME type that we need an icon for.
-     *
-     * @return string  The URL to the appropriate icon.
-     */
-    static final public function getIcon($mime_type)
-    {
-        $app = $GLOBALS['registry']->getApp();
-        $ob = self::_getIcon($mime_type, $app);
-
-        if (is_null($ob)) {
-            if ($app == 'horde') {
-                return null;
-            }
-
-            $obHorde = self::_getIcon($mime_type, 'horde');
-            return is_null($obHorde) ? null : $obHorde['url'];
-        } elseif (($ob['match'] !== 0) && ($app != 'horde')) {
-            $obHorde = self::_getIcon($mime_type, 'horde');
-            if (!is_null($ob['match']) &&
-                ($obHorde['match'] <= $ob['match'])) {
-                return $obHorde['url'];
-            }
-        }
-
-        return $ob['url'];
-    }
-
-    /**
      * Given an input MIME type and app, this function returns the URL of an
      * icon that can be associated with it
      *
index e441e27..f12bc56 100644 (file)
  */
 class Horde_MIME_Viewer_Driver
 {
+    /* 'type' constants for status info. */
+    const WARNING = 1;
+    const INFO = 2;
+
     /**
      * Viewer configuration.
      *
@@ -86,7 +90,7 @@ class Horde_MIME_Viewer_Driver
     public function render()
     {
         return (is_null($this->_mimepart) || !$this->canDisplay())
-            ? array('data' => '', 'type' => 'text/plain')
+            ? array('data' => '', 'status' => array(), 'type' => 'text/plain')
             : $this->_render();
     }
 
@@ -107,7 +111,7 @@ class Horde_MIME_Viewer_Driver
     public function renderInline()
     {
         return (is_null($this->_mimepart) || !$this->canDisplayInline())
-            ? ''
+            ? array('data' => '', 'status' => array()),
             : $this->_renderInline();
     }
 
@@ -128,7 +132,7 @@ class Horde_MIME_Viewer_Driver
     public function renderInfo()
     {
         return (is_null($this->_mimepart) || !$this->canDisplayInfo())
-            ? ''
+            ? array('data' => '', 'status' => array()),
             : $this->_renderInfo();
     }
 
index b8317b7..9467187 100644 (file)
@@ -33,7 +33,13 @@ class Horde_MIME_Viewer_html extends Horde_MIME_Viewer_Driver
      */
     public function _render()
     {
-        return array('data' => $this->_cleanHTML($this->_mimepart->getContents(), false), 'type' => $this->_mimepart->getType(true));
+        $html = $this->_cleanHTML($this->_mimepart->getContents(), false);
+
+        return array(
+            'data' => $html['data'],
+            'status' => $html['status'],
+            'type' => $this->_mimepart->getType(true)
+        );
     }
 
     /**
@@ -41,7 +47,12 @@ class Horde_MIME_Viewer_html extends Horde_MIME_Viewer_Driver
      */
     public function _renderInline()
     {
-        return String::convertCharset($this->_cleanHTML($this->_mimepart->getContents(), true), $this->_mimepart->getCharset());
+        $html = $this->_cleanHTML($this->_mimepart->getContents(), true);
+
+        return array(
+            'data' => String::convertCharset($html['data'], $this->_mimepart->getCharset()),
+            'status' => $html['status']
+        );
     }
 
     /**
@@ -54,7 +65,7 @@ class Horde_MIME_Viewer_html extends Horde_MIME_Viewer_Driver
      * @param string $data     The HTML data.
      * @param boolean $inline  Are we viewing inline?
      *
-     * @return string  The cleaned HTML data.
+     * @return array  Two elements: 'html' and 'status'.
      */
     protected function _cleanHTML($data, $inline)
     {
@@ -136,30 +147,33 @@ class Horde_MIME_Viewer_html extends Horde_MIME_Viewer_Driver
         }
 
         /* Try to derefer all external references. */
-        $data = preg_replace_callback('/href\s*=\s*(["\'])?((?(1)[^\1]*?|[^\s>]+))(?(1)\1|)/i',
-                                      array($this, '_dereferExternalReferencesCallback'),
-                                      $data);
+        $data = preg_replace_callback('/href\s*=\s*(["\'])?((?(1)[^\1]*?|[^\s>]+))(?(1)\1|)/i', array($this, '_dereferCallback'), $data);
 
-        /* Prepend phishing warning. */
+        /* Get phishing warning. */
+        $status = array();
         if ($phish_warn) {
             $phish_warning = sprintf(_("%s: This message may not be from whom it claims to be. Beware of following any links in it or of providing the sender with any personal information.") . ' ' . _("The links that caused this warning have the same background color as this message."), _("Warning"));
             if (!$inline) {
-                $phish_warning = '<span style="background-color:#ffd0af;color:black">' . String::convertCharset($phish_warning, NLS::getCharset(), $this->_mimepart->getCharset()) . '</span><br />';
+                $phish_warning = String::convertCharset($phish_warning, NLS::getCharset(), $this->_mimepart->getCharset());
             }
-            $phish_warning = $this->formatStatusMsg($phish_warning, null, 'mimeStatusWarning');
 
-            $data = (stristr($data, '<body') === false)
-                ? $phish_warning . $data
-                : preg_replace('/(.*<body.*?>)(.*)/i', '$1' . $phish_warning . '$2', $data);
+            $status[] = array(
+                'text' => $phish_warning,
+                'type' => self::WARNING
+            );
         }
 
-        return $data;
+        return array('html' => $data, 'status' => $status);
     }
 
     /**
      * TODO
+     *
+     * @param string $m  TODO
+     *
+     * @return string  TODO
      */
-    protected function _dereferExternalReferencesCallback($m)
+    protected function _dereferCallback($m)
     {
         return 'href="' . Horde::externalUrl($m[2]) . '"';
     }
index ad0aa17..202f5cc 100644 (file)
@@ -43,7 +43,8 @@ class Horde_MIME_Viewer_plain extends Horde_MIME_Viewer_Driver
         require_once 'Horde/Text/Filter.php';
         return array(
             'data' => '<html><body><tt>' . Text_Filter::filter($text, 'text2html', array('parselevel' => TEXT_HTML_MICRO, 'charset' => $charset, 'class' => null)) . '</tt></body></html>',
-            'type' => 'text/html; charset=' . $charset;
+            'status' => array(),
+            'type' => 'text/html; charset=' . $charset
         );
     }
 
@@ -57,9 +58,14 @@ class Horde_MIME_Viewer_plain extends Horde_MIME_Viewer_Driver
         $text = String::convertCharset($this->_mimepart->getContents(), $this->_mimepart->getCharset());
 
         /* Check for 'flowed' text data. */
-        return ($this->_mimepart->getContentTypeParameter('format') == 'flowed')
+        $data = ($this->_mimepart->getContentTypeParameter('format') == 'flowed')
             ? $this->_formatFlowed($text, $this->_mimepart->getContentTypeParameter('delsp'))
             : $text;
+
+        return array(
+            'data' => $data,
+            'status' => array()
+        );
     }
 
     /**