'charset' => $charset
),
array(
+ 'charset' => $charset,
'noprefetch' => !empty($options['noprefetch']),
'return_dom' => true,
'strip_styles' => (!empty($options['inline']) || $strip_style_attributes),
);
$this->_phishWarn = false;
- $this->_node($data, $data);
+ $this->_node($data->dom, $data->dom);
- return Horde_String::convertCharset($data->saveHTML(), $data->encoding, $charset);
+ return $data->returnHtml();
}
/**
--- /dev/null
+<?php
+/**
+ * @category Horde
+ * @package Support
+ * @copyright 2010 The Horde Project (http://www.horde.org/)
+ * @license http://opensource.org/licenses/bsd-license.php
+ */
+
+/**
+ * Utility class to help in loading DOM data from HTML strings.
+ *
+ * @author Michael Slusarz <slusarz@horde.org>
+ * @category Horde
+ * @package Support
+ * @copyright 2010 The Horde Project (http://www.horde.org/)
+ * @license http://opensource.org/licenses/bsd-license.php
+ */
+class Horde_Support_Domhtml
+{
+ /**
+ * DOM object.
+ *
+ * @var DOMDocument
+ */
+ public $dom;
+
+ /**
+ * Charset/encoding used in object.
+ *
+ * @var string
+ */
+ public $encoding;
+
+ /**
+ * Original charset of data.
+ *
+ * @var string
+ */
+ protected $_origCharset;
+
+ /**
+ * @param string $text
+ * @param string $charset
+ *
+ * @throws Exception
+ */
+ public function __construct($text, $charset = null)
+ {
+ if (!extension_loaded('dom')) {
+ throw new Exception('DOM extension is not available.');
+ }
+
+ $this->_origCharset = $charset;
+
+ $old_error = libxml_use_internal_errors(true);
+ $doc = new DOMDocument();
+ $doc->loadHTML($text);
+ $this->encoding = $doc->encoding;
+
+ if (!is_null($charset)) {
+ if (!$doc->encoding) {
+ $doc->loadHTML(Horde_String::convertCharset($text, $charset, 'ISO-8859-1'));
+ $this->encoding = null;
+ } elseif ($doc->encoding != $charset) {
+ /* If libxml can't auto-detect encoding, convert to what it
+ * *thinks* the encoding should be. */
+ $doc->loadHTML(Horde_String::convertCharset($text, $charset, $doc->encoding));
+ }
+ }
+
+ if ($old_error) {
+ libxml_use_internal_errors(false);
+ }
+
+ $this->dom = $doc;
+ }
+
+ /**
+ * @return string
+ */
+ public function returnHtml()
+ {
+ return Horde_String::convertCharset($this->dom->saveHTML(), $this->encoding, $this->_origCharset);
+ }
+
+}
<api>beta</api>
</stability>
<license uri="http://opensource.org/licenses/bsd-license.php">BSD</license>
- <notes> * Add Horde_Support_Randomid::.
+ <notes>* Add Horde_Support_Domhtml::.
+ * Add Horde_Support_Randomid::.
* Add Portuguese numerizer.
</notes>
<contents>
<file name="Backtrace.php" role="php" />
<file name="CombineStream.php" role="php" />
<file name="ConsistentHash.php" role="php" />
+ <file name="Domhtml.php" role="php" />
<file name="Guid.php" role="php" />
<file name="Inflector.php" role="php" />
<file name="Numerizer.php" role="php" />
<channel>pear.horde.org</channel>
</package>
</required>
+ <optional>
+ <extension>
+ <name>dom</name>
+ </extension>
+ </optional>
</dependencies>
<phprelease>
<filelist>
<install as="Horde/Support/Backtrace.php" name="lib/Horde/Support/Backtrace.php" />
<install as="Horde/Support/CombineStream.php" name="lib/Horde/Support/CombineStream.php" />
<install as="Horde/Support/ConsistentHash.php" name="lib/Horde/Support/ConsistentHash.php" />
+ <install as="Horde/Support/Domhtml.php" name="lib/Horde/Support/Domhtml.php" />
<install as="Horde/Support/Guid.php" name="lib/Horde/Support/Guid.php" />
<install as="Horde/Support/Inflector.php" name="lib/Horde/Support/Inflector.php" />
<install as="Horde/Support/Numerizer.php" name="lib/Horde/Support/Numerizer.php" />
*/
public function postProcess($text)
{
- if (extension_loaded('dom')) {
- $old_error = libxml_use_internal_errors(true);
- $doc = new DOMDocument();
- $doc->loadHTML($text);
- if (!$doc->encoding) {
- /* If libxml can't auto-detect encoding, convert to ISO-8859-1
- * manually. */
- $doc->loadHTML(Horde_String::convertCharset($text, $this->_params['charset'], 'ISO-8859-1'));
- }
- if ($old_error) {
- libxml_use_internal_errors(false);
- }
-
- $text = Horde_String::convertCharset($this->_node($doc, $doc), $doc->encoding, $this->_params['charset']);
- } else {
+ try {
+ $dom = new Horde_Support_Domhtml($text, $this->_params['charset']);
+ $text = Horde_String::convertCharset($this->_node($dom->dom, $dom->dom), $dom->encoding, $this->_params['charset']);
+ } catch (Exception $e) {
$text = strip_tags(preg_replace("/\<br\s*\/?\>/i", "\n", $text));
}
* the document.
* DEFAULT: false (returns the contents contained inside
* the BODY tag)
- * 'return_dom' - (boolean) If true, return a DOMDocument object instead of
- * HTML text (overrides return_document).
+ * 'return_dom' - (boolean) If true, return a Horde_Support_Domhtml object
+ * instead of HTML text (overrides return_document).
* DEFAULT: false
* 'strip_styles' - (boolean) Strip style tags?
* DEFAULT: true
*
* @param string $text The text after the filtering.
*
- * @return string|DOMDocument The modified text or a DOMDocument object
- * if the 'return_dom' parameter is set.
+ * @return string|Horde_Support_Domhtml The modified text or a Domhtml
+ * object if the 'return_dom'
+ * parameter is set.
*/
public function postProcess($text)
{
- if (!extension_loaded('dom')) {
+ try {
+ $dom = new Horde_Support_Domhtml($text, $this->_params['charset']);
+ } catch (Exception $e) {
return $text;
}
- $old_error = libxml_use_internal_errors(true);
- $doc = new DOMDocument();
- $doc->loadHTML($text);
- if (!$doc->encoding) {
- /* If libxml can't auto-detect encoding, convert to ISO-8859-1
- * manually. */
- $doc->loadHTML(Horde_String::convertCharset($text, $this->_params['charset'], 'ISO-8859-1'));
- }
- if ($old_error) {
- libxml_use_internal_errors(false);
- }
-
- $this->_node($doc, $doc);
+ $this->_node($dom->dom, $dom->dom);
if (!$this->_params['return_document']) {
- $body = $doc->getElementsByTagName('body')->item(0);
+ $body = $dom->dom->getElementsByTagName('body')->item(0);
}
if ($this->_params['noprefetch']) {
- $meta = $doc->createElement('meta');
+ $meta = $dom->dom->createElement('meta');
$meta->setAttribute('http-equiv', 'x-dns-prefetch-control');
$meta->setAttribute('value-equiv', 'off');
if ($this->_params['return_document']) {
- $doc->getElementsByTagName('head')->item(0)->appendChild($meta);
+ $dom->dom->getElementsByTagName('head')->item(0)->appendChild($meta);
} elseif ($body) {
$body->appendChild($meta);
}
}
if ($this->_params['return_dom']) {
- return $doc;
+ return $dom;
}
- $text = '';
if ($this->_params['return_document']) {
- $text = $doc->saveHTML();
- } elseif ($body && $body->hasChildNodes()) {
+ return $dom->returnHtml();
+ }
+
+ $text = '';
+ if ($body && $body->hasChildNodes()) {
foreach ($body->childNodes as $child) {
- $text .= $doc->saveXML($child);
+ $text .= $dom->dom->saveXML($child);
}
}
- return Horde_String::convertCharset($text, $doc->encoding, $this->_params['charset']);
+ return Horde_String::convertCharset($text, $dom->encoding, $this->_params['charset']);
}
/**
<channel>pear.horde.org</channel>
</package>
<package>
+ <name>Support</name>
+ <channel>pear.horde.org</channel>
+ </package>
+ <package>
<name>Util</name>
<channel>pear.horde.org</channel>
</package>
$type = $part->getType();
$part_charset = $part->getCharset();
$charset = $GLOBALS['registry']->getCharset();
+
$msg = Horde_String::convertCharset($part->getContents(), $part_charset, $charset);
/* Enforce reply limits. */
}
if ($mode == 'html') {
- $msg = $GLOBALS['injector']->getInstance('Horde_Text_Filter')->filter($msg, array('cleanhtml', 'xss'), array(array('body_only' => true), array('charset' => $charset, 'strip_styles' => true, 'strip_style_attributes' => false)));
+ $msg = $GLOBALS['injector']->getInstance('Horde_Text_Filter')->filter($msg, array('Cleanhtml', 'Xss'), array(array('body_only' => true), array('charset' => $charset, 'strip_styles' => true, 'strip_style_attributes' => false)));
} elseif ($type == 'text/html') {
- $msg = $GLOBALS['injector']->getInstance('Horde_Text_Filter')->filter($msg, 'html2text');
+ $msg = $GLOBALS['injector']->getInstance('Horde_Text_Filter')->filter($msg, 'Html2text', array('charset' => $charset));
$type = 'text/plain';
}
'cli' => true
));
-// Suppress DOM parsing errors.
-libxml_use_internal_errors(true);
-
require_once dirname(__FILE__) . '/../Mime/Viewer/Html.php';
class IMP_Html_Viewer_Test extends IMP_Horde_Mime_Viewer_Html
{
'target' => '_blank'
);
- $doc = DOMDocument::loadHTML($html);
- $this->_node($doc, $doc);
+ $dom = new Horde_Support_Domhtml($html);
+ $this->_node($dom->dom, $dom->dom);
- return $doc->saveXML($doc->getElementsByTagName('body')->item(0)->firstChild) . "\n";
+ return $dom->dom->saveXML($dom->dom->getElementsByTagName('body')->item(0)->firstChild) . "\n";
}
protected function _node($doc, $node)
if ($browser->isBrowser('mozilla')) {
$pstring = Horde_Mime::decodeParam('content-type', $render[$key]['type']);
- $old_error = libxml_use_internal_errors(true);
- $doc = new DOMDocument();
- $doc->loadHTML($render[$key]['data']);
- if (!$doc->encoding) {
- /* If libxml can't auto-detect encoding, convert to
- * ISO-8859-1 manually. */
- $doc->loadHTML(Horde_String::convertCharset($render[$key]['data'], $pstring['params']['charset'], 'ISO-8859-1'));
- }
- if (!$old_error) {
- libxml_use_internal_errors(false);
- }
+ $doc = new Horde_Support_Domhtml($render[$key]['data'], $pstring['params']['charset']);
- $bodyelt = $doc->getElementsByTagName('body')->item(0);
- $bodyelt->insertBefore($doc->importNode($div, true), $bodyelt->firstChild);
+ $bodyelt = $doc->dom->getElementsByTagName('body')->item(0);
+ $bodyelt->insertBefore($doc->dom->importNode($div, true), $bodyelt->firstChild);
- echo Horde_String::convertCharset($doc->saveHTML(), $doc->encoding, $pstring['params']['charset']);
+ echo $doc->returnHtml();
} else {
echo $render[$key]['data'];
}