<?php
/**
- * The IMP_Horde_Mime_Viewer_images class allows images to be displayed
- * inline in a message.
+ * The IMP_Horde_Mime_Viewer_images class allows display of images attached
+ * to a message.
*
* Copyright 2002-2008 The Horde Project (http://www.horde.org/)
*
class IMP_Horde_Mime_Viewer_images extends Horde_Mime_Viewer_images
{
/**
- * The content-type of the generated data.
+ * Can this driver render various views?
*
- * @var string
+ * @var boolean
*/
- protected $_contentType;
+ protected $_capability = array(
+ 'embedded' => 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:
+ * <pre>
+ * 'img_data' - (boolean) If true, output the image directly.
+ * 'img_load_convert' - (boolean) TODO
+ * 'img_view_convert' - (boolean) TODO
+ * 'img_view_thumbnail' - (boolean) TODO
+ * </pre>
*
- * @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 <img> 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 .= '<br />' . 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") . '</a>';
- } else {
- $convert_link = $contents->linkViewJS($this->mime_part, 'view_attach', _("HERE"), null, null, array('images_load_convert' => 1));
- }
- $msg .= '<br />' . 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 <img> 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 .= '<br />' . 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, '') . '</a>';
+ 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, '') . '</a>';
}
- 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('<br />', $status))
+ );
}
/**
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 = <<<EOD
+<html>
+<head>
+<title>$title</title>
+<style type="text/css"><!-- body { margin:0px; padding:0px; } --></style>
+EOD;
+
+ /* Only use javascript if we are using a DOM capable browser. */
+ if ($GLOBALS['browser']->getFeature('dom')) {
+ /* Javascript display. */
+ $loading = _("Loading...");
+ $str .= <<<EOD
+<script type="text/javascript">
+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();
+}
+</script></head>
+<body onload="resizeWindow();"><span id="splash" style="color:gray;font-family:sans-serif;padding:2px;">$loading</span><img id="disp_image" style="display:none;" src="$self_url" /></body></html>
+EOD;
+ } else {
+ /* Non-javascript display. */
+ $img_txt = _("Image");
+ $str .= <<<EOD
+</head>
+<body bgcolor="#ffffff">
+<img border="0" src="$self_url" alt="$img_txt" />
+</body>
+</html>
+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) {
$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
+ );
}
/**
*
* @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;
}
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;
- }
- }
}