From 39a2bcd6bd4c89b506eb97f6c8c572f8c789e7ec Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Tue, 11 Nov 2008 01:03:21 -0700 Subject: [PATCH] Update images driver. image/* parts are now viewing correctly (only tested on popup window so far). --- imp/config/mime_drivers.php.dist | 4 +- imp/lib/Mime/Viewer/images.php | 285 +++++++++++++++++++-------------------- 2 files changed, 144 insertions(+), 145 deletions(-) diff --git a/imp/config/mime_drivers.php.dist b/imp/config/mime_drivers.php.dist index 9e71086ca..468125f23 100644 --- a/imp/config/mime_drivers.php.dist +++ b/imp/config/mime_drivers.php.dist @@ -17,7 +17,7 @@ * appledouble multipart/appledouble parts * enriched Enriched text messages * html HTML messages - * images Attached images inline + * images Images * itip iCalendar Transport-Independent Interoperability Protocol * notification Notification messages * partial message/partial parts @@ -235,9 +235,9 @@ $mime_drivers['imp']['itip'] = array( /** * multipart/alternative settings - * YOU SHOULD NOT NORMALLY ALTER THIS SETTING. */ $mime_drivers['imp']['alternative'] = array( + /* The 'inline' setting should normally not be changed. */ 'inline' => true, 'handles' => array( 'multipart/alternative' diff --git a/imp/lib/Mime/Viewer/images.php b/imp/lib/Mime/Viewer/images.php index a8c787204..44b026768 100644 --- a/imp/lib/Mime/Viewer/images.php +++ b/imp/lib/Mime/Viewer/images.php @@ -1,7 +1,7 @@ false, + 'full' => true, + 'info' => true, + 'inline' => true + ); /** - * Render out the currently set contents. + * Return the full rendered version of the Horde_Mime_Part object. * - * @param array $params An array with a reference to a MIME_Contents - * object. + * URL parameters used by this function: + *
+     * 'img_data' - (boolean) If true, output the image directly.
+     * 'img_load_convert' - (boolean) TODO
+     * 'img_view_convert' - (boolean) TODO
+     * 'img_view_thumbnail' - (boolean) TODO
+     * 
* - * @return string The rendered information. + * @return array See Horde_Mime_Viewer_Driver::render(). */ - public function render($params) + protected function _render() { - $contents = $params[0]; - - global $browser; - /* If calling page is asking us to output data, do that without any * further delay and exit. */ if (Util::getFormData('img_data')) { - return parent::render(); + return parent::_render(); } /* Convert the image to browser-viewable format and display. */ - if (Util::getFormData('images_view_convert')) { - return $this->_viewConvert(); + if (Util::getFormData('img_view_convert')) { + return $this->_viewConvert(false); } /* Create the thumbnail and display. */ - if (Util::getFormData('images_view_thumbnail')) { + if (Util::getFormData('img_view_thumbnail')) { return $this->_viewConvert(true); } - if (Util::getFormData('images_load_convert')) { + /* The browser can display the image type directly - output the JS + * code to render the auto resize popup image window. */ + if (Util::getFormData('img_load_convert') || + ($GLOBALS['browser']->hasFeature('javascript') && + $GLOBALS['browser']->isViewable($this->_getType()))) { return $this->_popupImageWindow(); } - if ($this->viewAsAttachment()) { - if (!$browser->hasFeature('javascript')) { - /* If the browser doesn't support javascript then simply - render the image data. */ - return parent::render(); - } elseif ($browser->isViewable(parent::getType())) { - /* The browser can display the image type directly - just - output the javascript code to render the auto resize popup - image window. */ - return $this->_popupImageWindow(); - } - } - - if ($browser->isViewable($this->mime_part->getType())) { - /* If we are viewing inline, and the browser can handle the image - type directly, output an tag to load the image. */ - $alt = $this->mime_part->getName(false, true); - return Horde::img($contents->urlView($this->mime_part, 'view_attach'), $alt, null, ''); - } else { - /* If we have made it this far, than the browser cannot view this - image inline. Inform the user of this and, possibly, ask user - if we should convert to another image type. */ - $msg = _("Your browser does not support inline display of this image type."); - - if ($this->viewAsAttachment()) { - $msg .= '
' . sprintf(_("Click %s to download the image."), $contents->linkView($this->mime_part, 'download_attach', _("HERE"), array('viewparams' => array('img_data' => 1)), true)); - } - - /* See if we can convert to an inline browser viewable form. */ - $img = $this->_getHordeImageOb(false); - if ($img && $browser->isViewable($img->getContentType())) { - if ($this->viewAsAttachment()) { - $convert_link = Horde::link($contents->urlView($this->mime_part, 'view_attach', array('images_load_convert' => 1))) . _("HERE") . ''; - } else { - $convert_link = $contents->linkViewJS($this->mime_part, 'view_attach', _("HERE"), null, null, array('images_load_convert' => 1)); - } - $msg .= '
' . sprintf(_("Click %s to convert the image file into a format your browser can view."), $convert_link); - } - - $this->_contentType = 'text/html; charset=' . NLS::getCharset(); - return $this->formatStatusMsg($msg); - } + return parent::_render(); } /** - * Return the content-type + * Return the rendered inline version of the Horde_Mime_Part object. * - * @return string The content-type of the output. + * @return array See Horde_Mime_Viewer_Driver::render(). */ - public function getType() + protected function _renderInline() { - return ($this->_contentType) ? $this->_contentType : parent::getType(); + /* Only display the image inline if the browser can display it and the + * size of the image is small. */ + if ($GLOBALS['browser']->isViewable($this->_getType())) { + if ($this->_mimepart->getBytes() < 51200) { + /* Viewing inline, and the browser can handle the image type + * directly. So output an tag to load the image. */ + return array( + 'data' => Horde::img($this->_params['contents']->urlView($this->_mimepart, 'view_attach'), $this->_mimepart->getName(true), null, '') + ); + } else { + return $this->_renderInfo(); + } + } + + /* The browser cannot view this image. Inform the user of this and + * ask user if we should convert to another image type. */ + $msg = _("Your browser does not support inline display of this image type."); + + /* See if we can convert to an inline browser viewable form. */ + $img = $this->_getHordeImageOb(false); + if ($img && $GLOBALS['browser']->isViewable($img->getContentType())) { + $convert_link = $contents->linkViewJS($this->_mimepart, 'view_attach', _("HERE"), null, null, array('img_load_convert' => 1)); + $msg .= '
' . sprintf(_("Click %s to convert the image file into a format your browser can view."), $convert_link); + } + + return array( + 'status' => array('text' => $msg) + ); } /** - * Render out attachment information. + * Return the rendered information about the Horde_Mime_Part object. * - * @param array $params An array with a reference to a MIME_Contents - * object. - * - * @return string The rendered text in HTML. + * @return array See Horde_Mime_Viewer_Driver::render(). */ - public function renderAttachmentInfo($params) + protected function _renderInfo() { - $contents = &$params[0]; - /* Display the thumbnail link only if we show thumbs for all images or - if image is over 50 KB. */ - if (!$this->getConfigParam('allthumbs') && - ($this->mime_part->getBytes() < 51200)) { - return ''; - } - - if (is_a($contents, 'IMP_Contents')) { - $this->mime_part = &$contents->getDecodedMIMEPart($this->mime_part->getMIMEId(), true); - } - - /* Check to see if convert utility is available. */ - if (!$this->_getHordeImageOb(false)) { - return ''; + * if image is over 50 KB. Also, check to see if convert utility is + * available. */ + if ((!$this->getConfigParam('allthumbs') && + ($this->_mimepart->getBytes() < 51200)) || + !$this->_getHordeImageOb(false)) { + return array(); } $status = array( - sprintf(_("An image named %s is attached to this message. A thumbnail is below."), - $this->mime_part->getName(true)), + sprintf(_("An image named %s is attached to this message. A thumbnail is below."), $this->_mimepart->getName(true)), ); - if (!$GLOBALS['browser']->hasFeature('javascript')) { - $status[] = Horde::link($contents->urlView($this->mime_part, - 'view_attach')) . - Horde::img($contents->urlView($this->mime_part, - 'view_attach', array('images_view_thumbnail' => 1), false), - _("View Attachment"), null, '') . ''; + if ($GLOBALS['browser']->hasFeature('javascript')) { + $status[] = $this->_params['contents']->linkViewJS($this->_mimepart, 'view_attach', Horde::img($this->_params['contents']->urlView($this->_mimepart, 'view_attach', array('img_view_thumbnail' => 1), false), _("View Attachment"), null, ''), null, null, null); } else { - $status[] = $contents->linkViewJS($this->mime_part, 'view_attach', - Horde::img($contents->urlView($this->mime_part, - 'view_attach', array('images_view_thumbnail' => 1), - false), _("View Attachment"), null, ''), null, null, - null); + $status[] = Horde::link($this->_params['contents']->urlView($this->_mimepart, 'view_attach')) . Horde::img($this->_params['contents']->urlView($this->_mimepart, 'view_attach', array('img_view_thumbnail' => 1), false), _("View Attachment"), null, '') . ''; } - return $this->formatStatusMsg($status, Horde::img('mime/image.png', - _("Thumbnail of attached image"), null, $GLOBALS['registry']->getImageDir('horde')), false); + return array( + 'status' => array('text' => implode('
', $status)) + ); } /** @@ -167,29 +143,73 @@ class IMP_Horde_Mime_Viewer_images extends Horde_Mime_Viewer_images protected function _popupImageWindow() { $params = $remove_params = array(); - if (Util::getFormData('images_load_convert')) { - $params['images_view_convert'] = 1; - $remove_params[] = 'images_load_convert'; + + if (Util::getFormData('img_load_convert')) { + $params['img_view_convert'] = 1; + $remove_params[] = 'img_load_convert'; } else { $params['img_data'] = 1; } + $self_url = Util::addParameter(Util::removeParameter(Horde::selfUrl(true), $remove_params), $params); - $title = MIME::decode($this->mime_part->getName(false, true)); - $this->_contentType = 'text/html; charset=' . NLS::getCharset(); - return parent::_popupImageWindow($self_url, $title); + $title = $this->_mimepart->getName(true); + + $str = << + +$title + +EOD; + + /* Only use javascript if we are using a DOM capable browser. */ + if ($GLOBALS['browser']->getFeature('dom')) { + /* Javascript display. */ + $loading = _("Loading..."); + $str .= << +function resizeWindow() +{ + + var h, img = document.getElementById('disp_image'), w; + document.getElementById('splash').style.display = 'none'; + img.style.display = 'block'; + window.moveTo(0, 0); + h = img.height - (self.innerHeight ? self.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight)); + w = img.width - (self.innerWidth ? self.innerWidth : (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth)); + window.resizeBy(w, h); + self.focus(); +} + +$loading +EOD; + } else { + /* Non-javascript display. */ + $img_txt = _("Image"); + $str .= << + +$img_txt + + +EOD; + } + + return array( + 'data' => $str, + 'type' => 'text/html; charset=' . NLS::getCharset() + ); } /** - * View thumbnail sized image. + * Convert image. * - * @param boolean $thumb View thumbnail size? + * @param boolean $thumb View image in thumbnail size? * * @return string The image data. */ - protected function _viewConvert($thumb = false) + protected function _viewConvert($thumb) { - $mime = $this->mime_part; - $img = $this->_getHordeImageOb(); + $img = $this->_getHordeImageOb(true); if ($img) { if ($thumb) { @@ -204,11 +224,10 @@ class IMP_Horde_Mime_Viewer_images extends Horde_Mime_Viewer_images $data = file_get_contents(IMP_BASE . '/themes/graphics/mini-error.png'); } - $mime->setType($type); - $this->_contentType = $type; - $mime->setContents($data); - - return $mime->getContents(); + return array( + 'data' => $data, + 'type' => $type + ); } /** @@ -216,26 +235,25 @@ class IMP_Horde_Mime_Viewer_images extends Horde_Mime_Viewer_images * * @param boolean $load Whether to load the image data. * - * @return Horde_Image The requested object. + * @return mixed The Horde_Image object, or false on error. */ - protected function _getHordeImageOb($load = true) + protected function _getHordeImageOb($load) { - include_once 'Horde/Image.php'; + $img = null; $params = array('temp' => Horde::getTempdir()); + if (!empty($GLOBALS['conf']['image']['convert'])) { $img = &Horde_Image::singleton('im', $params); } elseif (Util::extensionExists('gd')) { $img = &Horde_Image::singleton('gd', $params); - } else { - return false; } - if (is_a($img, 'PEAR_Error')) { + if (!$img || is_a($img, 'PEAR_Error')) { return false; } if ($load) { - $ret = $img->loadString(1, $this->mime_part->getContents()); + $ret = $img->loadString(1, $this->_mimepart->getContents()); if (is_a($ret, 'PEAR_Error')) { return false; } @@ -243,23 +261,4 @@ class IMP_Horde_Mime_Viewer_images extends Horde_Mime_Viewer_images return $img; } - - /** - * Can this driver render the the data inline? - * - * @return boolean True if the driver can display inline. - */ - public function canDisplayInline() - { - /* Only display the image inline if the configuration parameter is set, - the browser can actually display it, and the size of the image is - small. */ - global $browser; - if (($this->getConfigParam('inline')) && ($browser->isViewable($this->mime_part->getType())) && - ($this->mime_part->getBytes() < 51200)) { - return true; - } else { - return false; - } - } } -- 2.11.0