Fix display of multipart/related images.
authorMichael M Slusarz <slusarz@curecanti.org>
Mon, 2 Aug 2010 17:28:51 +0000 (11:28 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Mon, 2 Aug 2010 17:28:51 +0000 (11:28 -0600)
Also optimize the code - no need to create MIME Part objects for each
link, only do replacement in IMG tags.

imp/lib/Contents.php
imp/lib/Mime/Viewer/Html.php

index 52972f5..bc9f016 100644 (file)
@@ -713,7 +713,8 @@ class IMP_Contents
      *
      * @return string  The URL to view.php.
      */
-    public function urlView($mime_part, $actionID, $options = array())
+    public function urlView($mime_part = null, $actionID = 'view_attach',
+                            $options = array())
     {
         $params = $this->_urlViewParams($mime_part, $actionID, isset($options['params']) ? $options['params'] : array());
 
index 484cb8a..dfe2a0d 100644 (file)
@@ -142,6 +142,7 @@ class IMP_Horde_Mime_Viewer_Html extends Horde_Mime_Viewer_Html
         } else {
             $this->_imptmp = array(
                 'blockimg' => null,
+                'cid' => null,
                 'img' => ($inline && $GLOBALS['prefs']->getValue('html_image_replacement') && !$this->_inAddressBook()),
                 'imgblock' => false,
                 'inline' => $inline,
@@ -152,6 +153,22 @@ class IMP_Horde_Mime_Viewer_Html extends Horde_Mime_Viewer_Html
             if ($this->_imptmp['img']) {
                 $this->_imptmp['blockimg'] = Horde::url(Horde_Themes::img('spacer_red.png'), true, -1);
             }
+
+            /* Search for inlined images that we can display
+             * (multipart/related parts). */
+            if (isset($this->_params['related_id'])) {
+                $cid_replace = array();
+
+                foreach ($this->_params['related_cids'] as $mime_id => $cid) {
+                    if ($cid = trim($cid, '<>')) {
+                        $cid_replace['cid:' . $cid] = $mime_id;
+                    }
+                }
+
+                if (!empty($cid_replace)) {
+                    $this->_imptmp['cid'] = $cid_replace;
+                }
+            }
         }
 
         /* Sanitize the HTML. */
@@ -194,24 +211,6 @@ class IMP_Horde_Mime_Viewer_Html extends Horde_Mime_Viewer_Html
             );
         }
 
-        /* Search for inlined links that we can display (multipart/related
-         * parts). */
-        if (isset($this->_params['related_id'])) {
-            $cid_replace = array();
-
-            foreach ($this->_params['related_cids'] as $mime_id => $cid) {
-                $cid = trim($cid, '<>');
-                if ($cid) {
-                    $cid_part = $this->_params['contents']->getMIMEPart($mime_id);
-                    $cid_replace['cid:' . $cid] = $this->_params['contents']->urlView($cid_part, 'view_attach', array('params' => array('related_data' => 1)));
-                }
-            }
-
-            if (!empty($cid_replace)) {
-                $data = str_replace(array_keys($cid_replace), array_values($cid_replace), $data);
-            }
-        }
-
         $filters = array();
         if ($GLOBALS['prefs']->getValue('emoticons')) {
             $filters['emoticons'] = array(
@@ -278,7 +277,9 @@ class IMP_Horde_Mime_Viewer_Html extends Horde_Mime_Viewer_Html
         }
 
         if ($node instanceof DOMElement) {
-            switch (strtolower($node->tagName)) {
+            $tag = strtolower($node->tagName);
+
+            switch ($tag) {
             case 'a':
             case 'area':
                 /* Convert links to open in new windows. Ignore
@@ -297,10 +298,26 @@ class IMP_Horde_Mime_Viewer_Html extends Horde_Mime_Viewer_Html
 
             case 'img':
             case 'input':
-                if ($this->_imptmp['img'] && $node->hasAttribute('src')) {
-                    $node->setAttribute('htmlimgblocked', $node->getAttribute('src'));
-                    $node->setAttribute('src', $this->_imptmp['blockimg']);
-                    $this->_imptmp['imgblock'] = true;
+                if ($this->_imptmp && $node->hasAttribute('src')) {
+                    $val = $node->getAttribute('src');
+
+                    /* Multipart/related. */
+                    if (($tag == 'img') &&
+                        $this->_imptmp['cid'] &&
+                        isset($this->_imptmp['cid'][$val])) {
+                        $val = $this->_params['contents']->urlView(null, 'view_attach', array('params' => array(
+                            'id' => $this->_imptmp['cid'][$val],
+                            'related_data' => 1
+                        )));
+                        $node->setAttribute('src', $val);
+                    }
+
+                    /* Block images.*/
+                    if ($this->_imptmp['img']) {
+                        $node->setAttribute('htmlimgblocked', $val);
+                        $node->setAttribute('src', $this->_imptmp['blockimg']);
+                        $this->_imptmp['imgblock'] = true;
+                    }
                 }
                 break;