Update status Viewer.
authorMichael M Slusarz <slusarz@curecanti.org>
Thu, 13 Nov 2008 20:10:29 +0000 (13:10 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Thu, 13 Nov 2008 20:10:29 +0000 (13:10 -0700)
imp/lib/Mime/Viewer/status.php

index 17ab230..c5963bc 100644 (file)
 class IMP_Horde_Mime_Viewer_status extends Horde_Mime_Viewer_Driver
 {
     /**
-     * Render out the currently set contents.
+     * Can this driver render various views?
      *
-     * @param array $params  An array with a reference to a MIME_Contents
-     *                       object.
+     * @var boolean
+     */
+    protected $_capability = array(
+        'embedded' => false,
+        'full' => false,
+        'info' => true,
+        'inline' => true,
+    );
+
+    /**
+     * 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 render($params)
+    protected function _renderInline()
     {
-        $contents = &$params[0];
-
-        /* If this is a straight message/delivery-status part, just output
-           the text. */
-        if ($this->mime_part->getType() == 'message/delivery-status') {
-            $part = new Horde_Mime_Part('text/plain');
-            $part->setContents($this->mime_part->getContents());
-            return '<pre>' . $contents->renderMIMEPart($part) . '</pre>';
+        /* If this is a straight message/disposition-notification part, just
+         * output the text. */
+        if ($this->_mimepart->getType() == 'message/delivery-status') {
+            return $this->_params['contents']->renderMIMEPart($this->_mimepart->getMIMEId(), IMP_Contents::RENDER_FULL, array('type' => 'text/plain', 'params' => $this->_params));
         }
 
-        global $registry;
+        return $this->_renderInfo();
+    }
+
+    /**
+     * Return the rendered information about the Horde_Mime_Part object.
+     *
+     * @return array  See Horde_Mime_Viewer_Driver::render().
+     */
+    protected function _renderInfo()
+    {
+        $parts = array_keys($this->_mimepart->contentTypeMap());
+
+        reset($parts);
+        $part1_id = next($parts);
+        $part2_id = Horde_Mime::mimeIdArithmetic($part1_id, 'next');
+        $part3_id = Horde_Mime::mimeIdArithmetic($part2_id, 'next');
 
         /* RFC 3464 [2]: There are three parts to a delivery status
-           multipart/report message:
-             (1) Human readable message
-             (2) Machine parsable body part (message/delivery-status)
-             (3) Returned message (optional)
-
-           Information on the message status is found in the 'Action' field
-           located in part #2 (RFC 2464 [2.3.3]). It can be either 'failed',
-           'delayed', 'delivered', 'relayed', or 'expanded'. */
+         * multipart/report message:
+         *   (1) Human readable message
+         *   (2) Machine parsable body part (message/delivery-status)
+         *   (3) Returned message (optional)
+         *
+         * Information on the message status is found in the 'Action' field
+         * located in part #2 (RFC 3464 [2.3.3]). It can be either 'failed',
+         * 'delayed', 'delivered', 'relayed', or 'expanded'. */
+
+        /* Get the action first - it appears in the second part. */
         $action = null;
-        $part2 = $contents->getDecodedMIMEPart($this->mime_part->getRelativeMIMEId(2));
-        if (empty($part2)) {
-            return $this->_errorMsg($contents);
-        }
+        $part2 = $this->_params['contents']->getMIMEPart($part2_id);
 
         foreach (explode("\n", $part2->getContents()) as $line) {
-            if (strstr($line, 'Action:') !== false) {
-                $pos = strpos($line, ':') + 1;
-                $action = strtolower(trim(substr($line, $pos)));
+            if (stristr($line, 'Action:') !== false) {
+                $action = strtolower(trim(substr($line, strpos($line, ':') + 1)));
+                if (strpos($action, ' ') !== false) {
+                    $action = substr($action, 0, strpos($action, ' '));
+                }
                 break;
             }
         }
-        if (strpos($action, ' ') !== false) {
-            $action = substr($action, 0, strpos($action, ' '));
+
+        if (is_null($action)) {
+            return array();
         }
 
         /* Get the correct text strings for the action type. */
         switch ($action) {
         case 'failed':
         case 'delayed':
-            $graphic = 'alerts/error.png';
-            $alt = _("Error");
-            $msg = array(
-                _("ERROR: Your message could not be delivered."),
-                _("The mail server generated the following error message:")
+            $status = array(
+                array(
+                    'icon' => Horde::img('alerts/error.png', _("Error"), null, $GLOBALS['registry']->getImageDir('horde')),
+                    'text' => array(
+                        _("ERROR: Your message could not be delivered."),
+                        sprintf(_("Additional error message details can be viewed %s."), $this->_params['contents']->linkViewJS($part2, 'view_attach', _("HERE"), array('jstext' => _("Additional message details"), 'params' => array('mode' => IMP_Contents::RENDER_INLINE))))
+                    )
+                )
             );
-            $detail_msg =  _("Additional message error details can be viewed %s.");
-            $detail_msg_status = _("Additional message error details");
             $msg_link = _("The text of the returned message can be viewed %s.");
             $msg_link_status = _("The text of the returned message");
             break;
@@ -80,100 +103,45 @@ class IMP_Horde_Mime_Viewer_status extends Horde_Mime_Viewer_Driver
         case 'delivered':
         case 'expanded':
         case 'relayed':
-            $graphic = 'alerts/success.png';
-            $alt = _("Success");
-            $msg = array(
-                _("Your message was successfully delivered."),
-                _("The mail server generated the following message:")
+            $status = array(
+                array(
+                    'icon' => Horde::img('alerts/success.png', _("Success"), null, $GLOBALS['registry']->getImageDir('horde')),
+                    'text' => array(
+                        _("Your message was successfully delivered."),
+                        sprintf(_("Additional message details can be viewed %s."), $this->_params['contents']->linkViewJS($part2, 'view_attach', _("HERE"), array('jstext' => _("Additional message details"), 'params' => array('mode' => IMP_Contents::RENDER_INLINE))))
+                    )
+                )
             );
-            $detail_msg =  _("Additional message details can be viewed %s.");
-            $detail_msg_status = _("Additional message details");
             $msg_link = _("The text of the message can be viewed %s.");
             $msg_link_status = _("The text of the message");
             break;
-
-        default:
-            $graphic = '';
-            $alt = '';
-            $msg = '';
-            $detail_msg = '';
-            $detail_msg_status = '';
-            $msg_link = '';
-            $msg_link_status = '';
         }
 
         /* Print the human readable message. */
-        $part = $contents->getDecodedMIMEPart($this->mime_part->getRelativeMIMEId(1));
-        if (empty($part)) {
-            return $this->_errorMsg($contents);
-        }
+        $first_part = $this->_params['contents']->renderMIMEPart($part1_id, IMP_Contents::RENDER_INLINE_AUTO, array('params' => $this->_params));
 
-        $statusimg = Horde::img($graphic, $alt, 'height="16" width="16"', $registry->getImageDir('horde'));
-        $text = $this->formatStatusMsg($msg, $statusimg);
-        $text .= '<blockquote>' . $contents->renderMIMEPart($part) . '</blockquote>' . "\n";
-
-        /* Display a link to more detailed error message. */
-        $detailed_msg = array(
-            sprintf($detail_msg, $contents->linkViewJS($part2, 'view_attach', _("HERE"), $detail_msg_status))
-        );
-
-        /* Display a link to the returned message. Try to download the
-           text of the message/rfc822 part first, if it exists.
-           TODO: Retrieving by part ID 3.0 is deprecated.  Remove this once
-           Horde 4.0 is released. */
-        if (($part = $contents->getDecodedMIMEPart($this->mime_part->getRelativeMIMEId(3))) ||
-            ($part = $contents->getDecodedMIMEPart($this->mime_part->getRelativeMIMEId('3.0')))) {
-            $detailed_msg[] = sprintf($msg_link, $contents->linkViewJS($part, 'view_attach', _("HERE"), $msg_link_status, null, array('ctype' => 'message/rfc822')));
+        /* Display a link to the returned message, if it exists. */
+        $part3 = $this->_params['contents']->getMIMEPart($part3_id);
+        if ($part3) {
+            $status[0]['text'][] = sprintf($msg_link, $this->_params['contents']->linkViewJS($part3, 'view_attach', _("HERE"), array('jstext' => $msg_link_status, 'ctype' => 'message/rfc822')));
         }
 
-        $infoimg = Horde::img('info_icon.png', _("Info"), 'height="16" width="16"', $registry->getImageDir('horde'));
-        $text .= $this->formatStatusMsg($detailed_msg, $infoimg, false);
-
-        return $text;
-    }
-
+        if (empty($first_part)) {
+            $data = '';
+        } else {
+            $status[0]['text'][] = _("The mail server generated the following informational message:");
+            $status = array_merge($status, $first_part[$part1_id]['status']);
+            $data = $first_part[$part1_id]['data'];
+        }
 
-    /**
-     * Return the content-type.
-     *
-     * @return string  The content-type of the message.
-     */
-    public function getType()
-    {
-        return 'text/html; charset=' . NLS::getCharset();
-    }
+        $ret = array_combine($parts, array_fill(0, count($parts), null));
 
-    /**
-     * Returns an error string for a malformed RFC 3464 message.
-     *
-     * @param MIME_Contents &$contents  The MIME_Contents object for this
-     *                                  message.
-     *
-     * @return string  The error message string.
-     */
-    protected function _errorMsg(&$contents)
-    {
-        $err_msg = array(
-            _("This message contains mail delivery status information, but the format of this message is unknown."),
-            _("Below is the raw text of the status information message.")
+        $ret[$this->_mimepart->getMimeId()] = array(
+            'data' => $data,
+            'status' => $status,
+            'type' => 'text/html; charset=' . NLS::getCharset()
         );
-        $img = Horde::img('alerts/error.png', _("Error"), 'height="16" width="16"', $GLOBALS['registry']->getImageDir('horde'));
-
-        $text = $this->formatStatusMsg($err_msg, $img);
-
-        /* There is currently no BC way to obtain all parts from a message
-         * and display the summary of each part.  So instead, display the
-         * entire raw contents of the message. See Bug 3757. */
-        $text .= '<pre>';
-        if (is_a($contents, 'IMP_Contents')) {
-            $contents->rebuildMessage();
-            $message = $contents->getMIMEMessage();
-            $text .= $contents->toString($message, true);
-        } else {
-            $text .= htmlspecialchars($this->mime_part->getContents());
-        }
 
-        return $text . '</pre>';
+        return $ret;
     }
-
 }