From 9c7084f90f30314a5e2fe9c8aa48c9e96835b1d2 Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Thu, 6 Nov 2008 10:59:12 -0700 Subject: [PATCH] Updates from working tree --- framework/MIME/lib/Horde/MIME/Viewer/Driver.php | 31 +++------ framework/MIME/lib/Horde/MIME/Viewer/html.php | 92 +++++++++++++------------ framework/MIME/lib/Horde/MIME/Viewer/plain.php | 30 ++++---- 3 files changed, 76 insertions(+), 77 deletions(-) diff --git a/framework/MIME/lib/Horde/MIME/Viewer/Driver.php b/framework/MIME/lib/Horde/MIME/Viewer/Driver.php index a7f5abe2d..e441e2713 100644 --- a/framework/MIME/lib/Horde/MIME/Viewer/Driver.php +++ b/framework/MIME/lib/Horde/MIME/Viewer/Driver.php @@ -15,7 +15,7 @@ class Horde_MIME_Viewer_Driver { /** - * Viewer configuration parameters. + * Viewer configuration. * * @var array */ @@ -29,11 +29,11 @@ class Horde_MIME_Viewer_Driver protected $_mimepart = null; /** - * The MIME display type for this part. + * Viewer parameters. * - * @var string + * @var array */ - protected $_type = null; + protected $_params = array(); /** * Can this driver render various views? @@ -68,34 +68,25 @@ class Horde_MIME_Viewer_Driver } /** - * Return the MIME type of the rendered content. This can be - * overridden by the individual drivers, depending on what format - * they output in. By default, it passes through the MIME type of - * the object, or replaces custom extension types with - * 'text/plain' to let the browser do a best-guess render. + * Set parameters for use with this object. * - * @return string MIME type of the output content. + * @param array $params An array of params to add to the internal + * params list. */ - public function getType() + public function setParams($params = array()) { - if (is_null($this->_type)) { - return is_null(is_null($this->_mimepart) || ($this->_mimepart->getPrimaryType() == 'x-extension')) - ? 'text/plain' - : $this->_mimepart->getType(true); - } - - return $this->_type; + $this->_params = array_merge($this->_params, $params); } /** * Return the rendered version of the Horde_MIME_Part object. * - * @return string Rendered version of the Horde_MIME_Part object. + * @return array TODO */ public function render() { return (is_null($this->_mimepart) || !$this->canDisplay()) - ? '' + ? array('data' => '', 'type' => 'text/plain') : $this->_render(); } diff --git a/framework/MIME/lib/Horde/MIME/Viewer/html.php b/framework/MIME/lib/Horde/MIME/Viewer/html.php index e4229dab3..b8317b7c8 100644 --- a/framework/MIME/lib/Horde/MIME/Viewer/html.php +++ b/framework/MIME/lib/Horde/MIME/Viewer/html.php @@ -16,15 +16,32 @@ class Horde_MIME_Viewer_html extends Horde_MIME_Viewer_Driver { /** - * Render out the currently set contents. + * Can this driver render various views? * - * @param array $params Any parameters the viewer may need. + * @var boolean + */ + protected $_canrender = array( + 'full' => true, + 'info' => false, + 'inline' => true, + ); + + /** + * Render out the currently set contents. * * @return string The rendered text. */ - public function render($params = null) + public function _render() + { + return array('data' => $this->_cleanHTML($this->_mimepart->getContents(), false), 'type' => $this->_mimepart->getType(true)); + } + + /** + * TODO + */ + public function _renderInline() { - return $this->_cleanHTML($this->mime_part->getContents()); + return String::convertCharset($this->_cleanHTML($this->_mimepart->getContents(), true), $this->_mimepart->getCharset()); } /** @@ -34,41 +51,41 @@ class Horde_MIME_Viewer_html extends Horde_MIME_Viewer_Driver * @todo Use IP checks from * http://lxr.mozilla.org/mailnews/source/mail/base/content/phishingDetector.js. * - * @param string $data The HTML data. + * @param string $data The HTML data. + * @param boolean $inline Are we viewing inline? * * @return string The cleaned HTML data. */ - protected function _cleanHTML($data) + protected function _cleanHTML($data, $inline) { global $browser, $prefs; - $inline = $this->viewInline(); $phish_warn = false; /* Deal with tags in the HTML, since they will screw up our own * relative paths. */ if (preg_match('/ ]*)"? ?\/?>/i', $data, $matches)) { $base = $matches[1]; - if (substr($base, -1, 1) != '/') { + if (substr($base, -1) != '/') { $base .= '/'; } /* Recursively call _cleanHTML() to prevent clever fiends from * sneaking nasty things into the page via $base. */ - $base = $this->_cleanHTML($base); - } + $base = $this->_cleanHTML($base, $inline); - /* Attempt to fix paths that were relying on a tag. */ - if (!empty($base)) { - $pattern = array('|src=(["\'])([^:"\']+)\1|i', - '|src=([^: >"\']+)|i', - '|href= *(["\'])([^:"\']+)\1|i', - '|href=([^: >"\']+)|i'); - $replace = array('src=\1' . $base . '\2\1', - 'src=' . $base . '\1', - 'href=\1' . $base . '\2\1', - 'href=' . $base . '\1'); - $data = preg_replace($pattern, $replace, $data); + /* Attempt to fix paths that were relying on a tag. */ + if (!empty($base)) { + $pattern = array('|src=(["\'])([^:"\']+)\1|i', + '|src=([^: >"\']+)|i', + '|href= *(["\'])([^:"\']+)\1|i', + '|href=([^: >"\']+)|i'); + $replace = array('src=\1' . $base . '\2\1', + 'src=' . $base . '\1', + 'href=\1' . $base . '\2\1', + 'href=' . $base . '\1'); + $data = preg_replace($pattern, $replace, $data); + } } require_once 'Horde/Text/Filter.php'; @@ -86,7 +103,7 @@ class Horde_MIME_Viewer_html extends Horde_MIME_Viewer_Driver if (preg_match('/href\s*=\s*["\']?\s*(http|https|ftp):\/\/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?:[^>]*>\s*(?:\\1:\/\/)?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})[^<]*<\/a)?/i', $data, $m)) { /* Check 1: Check for IP address links, but ignore if the link * text has the same IP address. */ - if (!isset($m[3]) || $m[2] != $m[3]) { + if (!isset($m[3]) || ($m[2] != $m[3])) { if (isset($m[3])) { $data = preg_replace('/href\s*=\s*["\']?\s*(http|https|ftp):\/\/' . preg_quote($m[2], '/') . '(?:[^>]*>\s*(?:$1:\/\/)?' . preg_quote($m[3], '/') . '[^<]*<\/a)?/i', 'class="mimeStatusWarning" $0', $data); } @@ -97,20 +114,19 @@ class Horde_MIME_Viewer_html extends Horde_MIME_Viewer_Driver * Check 2: Check for links that point to a different host than * the target url; if target looks like a domain name, check it * against the link. */ - $links = count($m[0]); - for ($i = 0; $i < $links; $i++) { + for ($i = 0, $links = count($m[0]); $i < $links; ++$i) { $link = strtolower(urldecode($m[1][$i])); $target = strtolower(preg_replace('/^(http|https|ftp):\/\//', '', strip_tags($m[2][$i]))); if (preg_match('/^[-._\da-z]+\.[a-z]{2,}/i', $target) && - strpos($link, $target) !== 0 && - strpos($target, $link) !== 0) { + (strpos($link, $target) !== 0) && + (strpos($target, $link) !== 0)) { /* Don't consider the link a phishing link if the * domain is the same on both links (e.g. * adtracking.example.com & www.example.com). */ preg_match('/\.?([^\.\/]+\.[^\.\/]+)[\/?]/', $link, $host1); preg_match('/\.?([^\.\/]+\.[^\.\/ ]+)([\/ ].*)?$/', $target, $host2); if (!(count($host1) && count($host2)) || - strcasecmp($host1[1], $host2[1]) !== 0) { + (strcasecmp($host1[1], $host2[1]) !== 0)) { $data = preg_replace('/href\s*=\s*["\']?\s*(?:http|https|ftp):\/\/' . preg_quote($m[1][$i], '/') . '["\']?[^>]*>\s*(?:(?:http|https|ftp):\/\/)?' . preg_quote($m[2][$i], '/') . '<\/a/is', 'class="mimeStatusWarning" $0', $data); $phish_warn = true; } @@ -128,33 +144,23 @@ class Horde_MIME_Viewer_html extends Horde_MIME_Viewer_Driver if ($phish_warn) { $phish_warning = sprintf(_("%s: This message may not be from whom it claims to be. Beware of following any links in it or of providing the sender with any personal information.") . ' ' . _("The links that caused this warning have the same background color as this message."), _("Warning")); if (!$inline) { - $phish_warning = '' . String::convertCharset($phish_warning, NLS::getCharset(), $this->mime_part->getCharset()) . '
'; + $phish_warning = '' . String::convertCharset($phish_warning, NLS::getCharset(), $this->_mimepart->getCharset()) . '
'; } $phish_warning = $this->formatStatusMsg($phish_warning, null, 'mimeStatusWarning'); - if (stristr($data, ')(.*)/i', '$1' . $phish_warning . '$2', $data); - } + + $data = (stristr($data, ')(.*)/i', '$1' . $phish_warning . '$2', $data); } return $data; } /** + * TODO */ protected function _dereferExternalReferencesCallback($m) { return 'href="' . Horde::externalUrl($m[2]) . '"'; } - - /** - * Return the content-type of the rendered text. - * - * @return string The MIME Content-Type. - */ - public function getType() - { - return $this->viewAsAttachment() ? $this->mime_part->getType(true) : 'text/html; charset=' . NLS::getCharset(); - } } diff --git a/framework/MIME/lib/Horde/MIME/Viewer/plain.php b/framework/MIME/lib/Horde/MIME/Viewer/plain.php index 23c0aa896..ad0aa17b7 100644 --- a/framework/MIME/lib/Horde/MIME/Viewer/plain.php +++ b/framework/MIME/lib/Horde/MIME/Viewer/plain.php @@ -15,26 +15,25 @@ class Horde_MIME_Viewer_plain extends Horde_MIME_Viewer_Driver { /** - * Constructor. + * Can this driver render various views? * - * @param array $conf Configuration specific to the driver. + * @var boolean */ - function __construct($conf = array()) - { - $this->_canrender['full'] = true; - $this->_canrender['inline'] = true; - $this->_type = 'text/html; charset=' . NLS::getCharset(); - parent::__construct($conf); - } + protected $_canrender = array( + 'full' => true, + 'info' => false, + 'inline' => true, + ); /** - * Render out the contents. + * Render the contents. * - * @return string The rendered contents. + * @return array TODO */ protected function _render() { $text = $this->_mimepart->getContents(); + $charset = $this->_mimepart->getCharset(); /* Check for 'flowed' text data. */ if ($this->_mimepart->getContentTypeParameter('format') == 'flowed') { @@ -42,17 +41,20 @@ class Horde_MIME_Viewer_plain extends Horde_MIME_Viewer_Driver } require_once 'Horde/Text/Filter.php'; - return '' . Text_Filter::filter($text, 'text2html', array('parselevel' => TEXT_HTML_MICRO, 'charset' => NLS::getCharset(), 'class' => null)) . ''; + return array( + 'data' => '' . Text_Filter::filter($text, 'text2html', array('parselevel' => TEXT_HTML_MICRO, 'charset' => $charset, 'class' => null)) . '', + 'type' => 'text/html; charset=' . $charset; + ); } /** - * Render out the contents. + * Render the contents for inline viewing. * * @return string The rendered contents. */ protected function _renderInline() { - $text = $this->_mimepart->getContents(); + $text = String::convertCharset($this->_mimepart->getContents(), $this->_mimepart->getCharset()); /* Check for 'flowed' text data. */ return ($this->_mimepart->getContentTypeParameter('format') == 'flowed') -- 2.11.0