62 var
$strip_htmltags = array(
'base',
'blink',
'body',
'doctype',
'embed',
'font',
'form',
'frame',
'frameset',
'html',
'iframe',
'input',
'marquee',
'meta',
'noscript',
'object',
'param',
'script',
'style');
64 var
$strip_attributes = array(
'bgsound',
'class',
'expr',
'id',
'style',
'onclick',
'onerror',
'onfinish',
'onmouseover',
'onmouseout',
'onfocus',
'onblur',
'lowsrc',
'dynsrc');
90 $this->image_handler = (string) $page;
94 $this->image_handler =
false;
100 $this->registry = $registry;
139 public function strip_htmltags($tags = array(
'base',
'blink',
'body',
'doctype',
'embed',
'font',
'form',
'frame',
'frameset',
'html',
'iframe',
'input',
'marquee',
'meta',
'noscript',
'object',
'param',
'script',
'style'))
163 public function strip_attributes($attribs = array(
'bgsound',
'class',
'expr',
'id',
'style',
'onclick',
'onerror',
'onfinish',
'onmouseover',
'onmouseout',
'onfocus',
'onblur',
'lowsrc',
'dynsrc'))
167 if (is_array($attribs))
189 $this->output_encoding = (string) $encoding;
205 if ($element_attribute === null)
207 $element_attribute = array(
210 'blockquote' =>
'cite',
222 $this->replace_url_attributes = (array) $element_attribute;
232 if (preg_match(
'/(&(#(x[0-9a-fA-F]+|[0-9]+)|[a-zA-Z0-9]+)|<\/[A-Za-z][^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3E]*' .
SIMPLEPIE_PCRE_HTML_ATTRIBUTE .
'>)/', $data))
244 $data = base64_decode($data);
250 $document =
new DOMDocument();
251 $document->encoding =
'UTF-8';
254 set_error_handler(array(
'SimplePie_Misc',
'silence_errors'));
255 $document->loadHTML($data);
256 restore_error_handler();
261 $xpath =
new DOMXPath($document);
262 $comments = $xpath->query(
'//comment()');
264 foreach ($comments as $comment)
266 $comment->parentNode->removeChild($comment);
277 $this->
strip_tag($tag, $document, $type);
291 foreach ($this->replace_url_attributes as $element => $attributes)
297 if (isset($this->image_handler) && ((
string) $this->image_handler) !==
'' && $this->enable_cache)
299 $images = $document->getElementsByTagName(
'img');
300 foreach ($images as $img)
302 if ($img->hasAttribute(
'src'))
304 $image_url = call_user_func($this->cache_name_function, $img->getAttribute(
'src'));
305 $cache = $this->registry->call(
'Cache',
'get_handler', array($this->cache_location, $image_url,
'spi'));
309 $img->setAttribute(
'src', $this->image_handler . $image_url);
313 $file = $this->registry->create(
'File', array($img[
'attribs'][
'src'][
'data'], $this->timeout, 5, array(
'X-FORWARDED-FOR' => $_SERVER[
'REMOTE_ADDR']), $this->useragent, $this->force_fsockopen));
314 $headers = $file->headers;
316 if ($file->success && ($file->method &
SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($file->status_code === 200 || $file->status_code > 206 && $file->status_code < 300)))
318 if ($cache->save(array(
'headers' => $file->headers,
'body' => $file->body)))
320 $img->setAttribute(
'src', $this->image_handler . $image_url);
324 trigger_error(
"$this->cache_location is not writeable. Make sure you've set the correct relative or absolute path, and that the location is server-writable.", E_USER_WARNING);
334 if ($document->firstChild instanceof DOMDocumentType)
336 $document->removeChild($document->firstChild);
340 $real_body = $document->getElementsByTagName(
'body')->item(0)->childNodes->item(0);
341 $document->replaceChild($real_body, $document->firstChild);
344 $data = trim($document->saveHTML());
349 $data = preg_replace(
'/<\/div>$/',
'', $data);
357 if ($type & SIMPLEPIE_CONSTRUCT_IRI)
359 $absolute = $this->registry->call(
'Misc',
'absolutize_url', array($data,
$base));
360 if ($absolute !==
false)
368 $data = htmlspecialchars($data, ENT_COMPAT,
'UTF-8');
371 if ($this->output_encoding !==
'UTF-8')
373 $data = $this->registry->call(
'Misc',
'change_encoding', array($data,
'UTF-8', $this->output_encoding));
386 $html =
'<div>' . $html .
'</div>';
387 $ret .=
'<!DOCTYPE html>';
388 $content_type =
'text/html';
392 $ret .=
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
393 $content_type =
'application/xhtml+xml';
396 $ret .=
'<html><head>';
397 $ret .=
'<meta http-equiv="Content-Type" content="' . $content_type .
'; charset=utf-8" />';
398 $ret .=
'</head><body>' . $html .
'</body></html>';
404 if (!is_array($attributes))
406 $attributes = array($attributes);
411 $elements = $document->getElementsByTagName($tag);
412 foreach ($elements as $element)
414 foreach ($attributes as $attribute)
416 if ($element->hasAttribute($attribute))
418 $value = $this->registry->call(
'Misc',
'absolutize_url', array($element->getAttribute($attribute),
$this->base));
419 if ($value !==
false)
421 $element->setAttribute($attribute, $value);
433 if (isset($match[4]) && !in_array(strtolower($match[1]), array(
'script',
'style')))
435 $match[1] = htmlspecialchars($match[1], ENT_COMPAT,
'UTF-8');
436 $match[2] = htmlspecialchars($match[2], ENT_COMPAT,
'UTF-8');
437 return "<$match[1]$match[2]>$match[3]</$match[1]>";
441 return htmlspecialchars($match[0], ENT_COMPAT,
'UTF-8');
444 elseif (isset($match[4]) && !in_array(strtolower($match[1]), array(
'script',
'style')))
456 $xpath =
new DOMXPath($document);
457 $elements = $xpath->query(
'body//' . $tag);
460 foreach ($elements as $element)
462 $fragment = $document->createDocumentFragment();
465 if (!in_array($tag, array(
'script',
'style')))
468 if ($element->hasAttributes())
471 foreach ($element->attributes as $name => $attr)
473 $value = $attr->value;
488 $attrs[] = $name .
'="' . $attr->value .
'"';
490 $text .=
' ' . implode(
' ', $attrs);
493 $fragment->appendChild(
new DOMText($text));
496 $number = $element->childNodes->length;
497 for ($i = $number; $i > 0; $i--)
499 $child = $element->childNodes->item(0);
500 $fragment->appendChild($child);
503 if (!in_array($tag, array(
'script',
'style')))
505 $fragment->appendChild(
new DOMText(
'</' . $tag .
'>'));
508 $element->parentNode->replaceChild($fragment, $element);
513 elseif (in_array($tag, array(
'script',
'style')))
515 foreach ($elements as $element)
517 $element->parentNode->removeChild($element);
524 foreach ($elements as $element)
526 $fragment = $document->createDocumentFragment();
527 $number = $element->childNodes->length;
528 for ($i = $number; $i > 0; $i--)
530 $child = $element->childNodes->item(0);
531 $fragment->appendChild($child);
534 $element->parentNode->replaceChild($fragment, $element);
541 $xpath =
new DOMXPath($document);
542 $elements = $xpath->query(
'//*[@' . $attrib .
']');
544 foreach ($elements as $element)
546 $element->removeAttribute($attrib);