/usr/share/php/Horde/Domhtml.php is in php-horde-util 2.5.1-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | <?php
/**
* Copyright 2010-2014 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @category Horde
* @copyright 2010-2014 Horde LLC
* @package Util
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
*/
/**
* Parse DOM data from HTML strings.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2010-2014 Horde LLC
* @package Util
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
*/
class Horde_Domhtml implements Iterator
{
/**
* DOM object.
*
* @var DOMDocument
*/
public $dom;
/**
* Iterator status.
*
* @var array
*/
protected $_iterator = null;
/**
* Original charset of data.
*
* @var string
*/
protected $_origCharset;
/**
* Encoding tag added to beginning of output.
*
* @var string
*/
protected $_xmlencoding = '';
/**
* Constructor.
*
* @param string $text The text of the HTML document.
* @param string $charset The charset of the HTML document.
*
* @throws Exception
*/
public function __construct($text, $charset = null)
{
if (!extension_loaded('dom')) {
throw new Exception('DOM extension is not available.');
}
// Bug #9616: Make sure we have valid HTML input.
if (!strlen($text)) {
$text = '<html></html>';
}
$old_error = libxml_use_internal_errors(true);
$this->dom = new DOMDocument();
if (is_null($charset)) {
/* If no charset given, charset is whatever libxml tells us the
* encoding should be defaulting to 'iso-8859-1'. */
$this->_loadHTML($text);
$this->_origCharset = $this->dom->encoding
? $this->dom->encoding
: 'iso-8859-1';
} else {
/* Convert/try with UTF-8 first. */
$this->_origCharset = Horde_String::lower($charset);
$this->_xmlencoding = '<?xml encoding="UTF-8"?>';
$this->_loadHTML(
$this->_xmlencoding . Horde_String::convertCharset($text, $charset, 'UTF-8')
);
if ($this->dom->encoding &&
(Horde_String::lower($this->dom->encoding) != 'utf-8')) {
/* Convert charset to what the HTML document says it SHOULD
* be. */
$this->_loadHTML(
Horde_String::convertCharset($text, $charset, $this->dom->encoding)
);
$this->_xmlencoding = '';
}
}
if ($old_error) {
libxml_use_internal_errors(false);
}
/* Sanity checking: make sure we have the documentElement object. */
if (!$this->dom->documentElement) {
$this->dom->appendChild($this->dom->createElement('html'));
}
/* Remove old charset information. */
$xpath = new DOMXPath($this->dom);
$domlist = $xpath->query('/html/head/meta[@http-equiv="content-type"]');
for ($i = $domlist->length; $i > 0; --$i) {
$meta = $domlist->item($i - 1);
$meta->parentNode->removeChild($meta);
}
}
/**
* Returns the HEAD element, or creates one if it doesn't exist.
*
* @return DOMElement HEAD element.
*/
public function getHead()
{
$head = $this->dom->getElementsByTagName('head');
if ($head->length) {
return $head->item(0);
}
$headelt = $this->dom->createElement('head');
$this->dom->documentElement->insertBefore($headelt, $this->dom->documentElement->firstChild);
return $headelt;
}
/**
* Returns the BODY element, or creates one if it doesn't exist.
*
* @since 2.2.0
*
* @return DOMElement BODY element.
*/
public function getBody()
{
$body = $this->dom->getElementsByTagName('body');
if ($body->length) {
return $body->item(0);
}
$bodyelt = $this->dom->createElement('body');
$this->dom->documentElement->appendChild($bodyelt);
return $bodyelt;
}
/**
* Returns the full HTML text in the original charset.
*
* @param array $opts Additional options: (since 2.1.0)
* - charset: (string) Return using this charset. If set but empty, will
* return as currently stored in the DOM object.
* - metacharset: (boolean) If true, will add a META tag containing the
* charset information.
*
* @return string HTML text.
*/
public function returnHtml(array $opts = array())
{
$curr_charset = $this->getCharset();
if (strcasecmp($curr_charset, 'US-ASCII') === 0) {
$curr_charset = 'UTF-8';
}
$charset = array_key_exists('charset', $opts)
? (empty($opts['charset']) ? $curr_charset : $opts['charset'])
: $this->_origCharset;
if (empty($opts['metacharset'])) {
$text = $this->dom->saveHTML();
} else {
/* Add placeholder for META tag. Can't add charset yet because DOM
* extension will alter output if it exists. */
$meta = $this->dom->createElement('meta');
$meta->setAttribute('http-equiv', 'content-type');
$meta->setAttribute('horde_dom_html_charset', '');
$head = $this->getHead();
$head->insertBefore($meta, $head->firstChild);
$text = str_replace(
'horde_dom_html_charset=""',
'content="text/html; charset=' . $charset . '"',
$this->dom->saveHTML()
);
$head->removeChild($meta);
}
if (strcasecmp($curr_charset, $charset) !== 0) {
$text = Horde_String::convertCharset($text, $curr_charset, $charset);
}
if (!$this->_xmlencoding ||
(($pos = strpos($text, $this->_xmlencoding)) === false)) {
return $text;
}
return substr_replace($text, '', $pos, strlen($this->_xmlencoding));
}
/**
* Returns the body text in the original charset.
*
* @return string HTML text.
*/
public function returnBody()
{
$body = $this->getBody();
$text = '';
if ($body->hasChildNodes()) {
foreach ($body->childNodes as $child) {
$text .= $this->dom->saveXML($child);
}
}
return Horde_String::convertCharset($text, 'UTF-8', $this->_origCharset);
}
/**
* Get the charset of the DOM data.
*
* @since 2.1.0
*
* @return string Charset of DOM data.
*/
public function getCharset()
{
return $this->dom->encoding
? $this->dom->encoding
: ($this->_xmlencoding ? 'UTF-8' : $this->_origCharset);
}
/**
* Loads the HTML data.
*
* @param string $html HTML data.
*/
protected function _loadHTML($html)
{
if (PHP_MINOR_VERSION >= 4) {
$mask = defined('LIBXML_PARSEHUGE')
? LIBXML_PARSEHUGE
: 0;
$mask |= defined('LIBXML_COMPACT')
? LIBXML_COMPACT
: 0;
$this->dom->loadHTML($html, $mask);
} else {
$this->dom->loadHTML($html);
}
}
/* Iterator methods. */
/**
*/
public function current()
{
if ($this->_iterator instanceof DOMDocument) {
return $this->_iterator;
}
$curr = end($this->_iterator);
return $curr['list']->item($curr['i']);
}
/**
*/
public function key()
{
return 0;
}
/**
*/
public function next()
{
/* Iterate in the reverse direction through the node list. This allows
* alteration of the original list without breaking things (foreach()
* w/removeChild() may exit iteration after removal is complete. */
if ($this->_iterator instanceof DOMDocument) {
$this->_iterator = array();
$curr = array();
$node = $this->dom;
} elseif (empty($this->_iterator)) {
$this->_iterator = null;
return;
} else {
$curr = &$this->_iterator[count($this->_iterator) - 1];
$node = $curr['list']->item($curr['i']);
}
if (empty($curr['child']) &&
($node instanceof DOMNode) &&
$node->hasChildNodes()) {
$curr['child'] = true;
$this->_iterator[] = array(
'child' => false,
'i' => $node->childNodes->length - 1,
'list' => $node->childNodes
);
} elseif (--$curr['i'] < 0) {
array_pop($this->_iterator);
$this->next();
} else {
$curr['child'] = false;
}
}
/**
*/
public function rewind()
{
$this->_iterator = $this->dom;
}
/**
*/
public function valid()
{
return !is_null($this->_iterator);
}
}
|