multipart/related now working (with HTML msgs)
authorMichael M Slusarz <slusarz@curecanti.org>
Tue, 11 Nov 2008 09:01:13 +0000 (02:01 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Tue, 11 Nov 2008 09:01:13 +0000 (02:01 -0700)
imp/lib/Mime/Viewer/html.php
imp/lib/Mime/Viewer/related.php

index 831b48c..0d665f1 100644 (file)
@@ -134,14 +134,21 @@ class IMP_Horde_Mime_Viewer_html extends Horde_Mime_Viewer_html
             $data = preg_replace('/(style\s*=\s*)(["\'])?([^>"\']*)position\s*:\s*absolute([^>"\']*)\2/i', '$1"$3$4"', $data);
         }
 
-        /* Search for inlined images that we can display. */
-        // TODO
-        if (false) {
-            $relatedPart = $this->_params['contents']->getMIMEPart($related);
-            foreach ($relatedPart->getCIDList() as $ref => $id) {
-                $id = trim($id, '<>');
-                $cid_part = $this->_params['contents']->getDecodedMIMEPart($ref);
-                $data = str_replace("cid:$id", $this->_params['contents']->urlView($cid_part, 'view_attach'), $data);
+        /* Search for inlined links that we can display (multipart/related
+         * parts). */
+        if (!empty($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('img_data' => 1)));
+                }
+            }
+
+            if (!empty($cid_replace)) {
+                $data = str_replace(array_keys($cid_replace), array_values($cid_replace), $data);
             }
         }
 
@@ -214,7 +221,7 @@ class IMP_Horde_Mime_Viewer_html extends Horde_Mime_Viewer_html
         /* If we are viewing inline, give option to view in separate window. */
         if ($inline && $this->getConfigParam('external')) {
             $cleanhtml['status'][] = array(
-                'data' => $this->_params['contents']->linkViewJS($this->mime_part, 'view_attach', _("Show this HTML in a new window?")),
+                'data' => $this->_params['contents']->linkViewJS($this->_mimepart, 'view_attach', _("Show this HTML in a new window?")),
                 'type' => 'info'
             );
         }
index 2879b6d..c874e62 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /**
- * The IMP_Horde_Mime_Viewer_related class handles multipart/related messages
- * as defined by RFC 2387.
+ * The IMP_Horde_Mime_Viewer_related class handles multipart/related
+ * (RFC 2387) messages.
  *
  * Copyright 2002-2008 The Horde Project (http://www.horde.org/)
  *
 class IMP_Horde_Mime_Viewer_related extends Horde_Mime_Viewer_Driver
 {
     /**
-     * The character set of the rendered HTML part.
+     * Can this driver render various views?
      *
-     * @var string
+     * @var boolean
      */
-    protected $_charset = null;
+    protected $_capability = array(
+        'embedded' => false,
+        'full' => true,
+        'info' => true,
+        'inline' => true,
+    );
 
     /**
-     * The mime type of the message part that has been chosen to be displayed.
+     * Return the full rendered version of the Horde_Mime_Part object.
      *
-     * @var string
+     * @return array  See Horde_Mime_Viewer_Driver::render().
      */
-    protected $_viewerType = 'text/html';
-
-    /**
-     * Render out the currently set contents.
-     *
-     * @param array $params  An array with a reference to a MIME_Contents
-     *                       object.
-     *
-     * @return string  The rendered text in HTML.
-     */
-    public function render($params)
+    protected function _render()
     {
-        $contents = &$params[0];
-
-        /* Look at the 'start' parameter to determine which part to start
-           with. If no 'start' parameter, use the first part.
-           RFC 2387 [3.1] */
-        if ($this->mime_part->getContentTypeParameter('start') &&
-            ($key = array_search($this->mime_part->getContentTypeParameter('start'), $this->mime_part->getCIDList()))) {
-            if (($pos = strrpos($key, '.'))) {
-                $id = substr($key, $pos + 1);
-            } else {
-                $id = $key;
-            }
-        } else {
-            $id = 1;
-        }
-        $start = $this->mime_part->getPart($this->mime_part->getRelativeMimeID($id));
-
-        /* Only display if the start part (normally text/html) can be displayed
-           inline -OR- we are viewing this part as an attachment. */
-        if ($contents->canDisplayInline($start) ||
-            $this->viewAsAttachment()) {
-            $text = $contents->renderMIMEPart($start);
-            $this->_viewerType = $contents->getMIMEViewerType($start);
-            $this->_charset = $start->getCharset();
-        } else {
-            $text = '';
-        }
-
-        return $text;
-     }
+        return $this->_IMPrender(false);
+    }
 
     /**
-     * Render out attachment information.
-     *
-     * @param array $params  An array with a reference to a MIME_Contents
-     *                       object.
+     * Return the rendered inline version of the Horde_Mime_Part object.
      *
-     * @return string  The rendered text in HTML.
+     * @return array  See Horde_Mime_Viewer_Driver::render().
      */
-    public function renderAttachmentInfo($params)
+    protected function _renderInline()
     {
-        $contents = &$params[0];
-
-        $msg = sprintf(_("Click %s to view this multipart/related part in a separate window."), $contents->linkViewJS($this->mime_part, 'view_attach', _("HERE"), _("View content in a separate window")));
-        return $this->formatStatusMsg($msg, Horde::img('mime/html.png', _("HTML")), false);
+        $ret = $this->_IMPrender(true);
+        return empty($ret['ids']) ? $this->_renderInfo() : $ret;
     }
 
     /**
-     * Return the content-type.
+     * Return the rendered information about the Horde_Mime_Part object.
      *
-     * @return string  The content-type of the message.
+     * @return array  See Horde_Mime_Viewer_Driver::render().
      */
-    public function getType()
+    protected function _renderInfo()
     {
-        return $this->_viewerType . '; charset=' . $this->getCharset();
+        return array(
+            'status' => array(
+                'text' => sprintf(_("Click %s to view this multipart/related part in a separate window."), $contents->linkViewJS($this->mime_part, 'view_attach', _("HERE"), _("View content in a separate window"))),
+                'icon' => Horde::img('mime/html.png', _("HTML"))
+            )
+        );
     }
 
     /**
-     * Returns the character set used for the Viewer.
+     * Render out the currently set contents.
      *
-     * @return string  The character set used by this Viewer.
+     * @param boolean $inline  Are we viewing inline?
+     *
+     * @return array  Two elements: html and status.
      */
-    public function getCharset()
+    protected function _IMPrender($inline)
     {
-        return ($this->_charset === null) ? NLS::getCharset() : $this->_charset;
+        $can_display = !$inline;
+        $text = '';
+
+        $subparts = $this->_mimepart->contentTypeMap();
+        unset($subparts[key($subparts)]);
+
+        /* Look at the 'start' parameter to determine which part to start
+         * with. If no 'start' parameter, use the first part. RFC 2387
+         * [3.1] */
+        $id = $this->_mimepart->getContentTypeParameter('start');
+        if (is_null($id)) {
+            reset($subparts);
+            $id = key($subparts);
+        }
+
+        /* Only display if the start part (normally text/html) can be
+         * displayed inline -OR- we are viewing this part as an attachment. */
+        if (!$can_display) {
+            $viewer = Horde_Mime_Viewer::factory($subparts[$id]);
+            if ($viewer->canRender('inline')) {
+                $mime_part = $this->_mimepart->getPart($id);
+                $can_display = ($mime_part->getDisposition() == 'inline');
+            }
+        }
+
+        if ($can_display) {
+            /* Build a list of parts -> CIDs. */
+            $cids = array();
+            foreach (array_keys($subparts) as $val) {
+                $part = $this->_mimepart->getPart($val);
+                $cids[$val] = $part->getContentId();
+            }
+
+            $ret = $this->_params['contents']->renderMIMEPart($id, $inline ? 'inline' : 'full', array('params' => array('related_id' => $id, 'related_cids' => $cids)));
+            $ret['ids'] = array_merge($ret['ids'], array_keys($subparts));
+            unset($ret['summary_id']);
+            return $ret;
+        }
+
+        return array();
     }
 }