class Horde_MIME_Viewer_Driver
{
/**
- * Viewer configuration parameters.
+ * Viewer configuration.
*
* @var array
*/
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?
}
/**
- * 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();
}
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());
}
/**
* @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 <base> tags in the HTML, since they will screw up our own
* relative paths. */
if (preg_match('/<base href="?([^"> ]*)"? ?\/?>/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 <base> 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 <base> 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';
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);
}
* 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;
}
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 = '<span style="background-color:#ffd0af;color:black">' . String::convertCharset($phish_warning, NLS::getCharset(), $this->mime_part->getCharset()) . '</span><br />';
+ $phish_warning = '<span style="background-color:#ffd0af;color:black">' . String::convertCharset($phish_warning, NLS::getCharset(), $this->_mimepart->getCharset()) . '</span><br />';
}
$phish_warning = $this->formatStatusMsg($phish_warning, null, 'mimeStatusWarning');
- if (stristr($data, '<body') === false) {
- $data = $phish_warning . $data;
- } else {
- $data = preg_replace('/(.*<body.*?>)(.*)/i', '$1' . $phish_warning . '$2', $data);
- }
+
+ $data = (stristr($data, '<body') === false)
+ ? $phish_warning . $data
+ : preg_replace('/(.*<body.*?>)(.*)/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();
- }
}
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') {
}
require_once 'Horde/Text/Filter.php';
- return '<html><body><tt>' . Text_Filter::filter($text, 'text2html', array('parselevel' => TEXT_HTML_MICRO, 'charset' => NLS::getCharset(), 'class' => null)) . '</tt></body></html>';
+ return array(
+ 'data' => '<html><body><tt>' . Text_Filter::filter($text, 'text2html', array('parselevel' => TEXT_HTML_MICRO, 'charset' => $charset, 'class' => null)) . '</tt></body></html>',
+ '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')