friendica/vendor/ezyang/htmlpurifier/library/HTMLPurifier/URIDefinition.php

113 lines
3.3 KiB
PHP
Raw Normal View History

2010-09-09 03:14:17 +00:00
<?php
class HTMLPurifier_URIDefinition extends HTMLPurifier_Definition
{
public $type = 'URI';
protected $filters = array();
protected $postFilters = array();
protected $registeredFilters = array();
/**
* HTMLPurifier_URI object of the base specified at %URI.Base
*/
public $base;
/**
* String host to consider "home" base, derived off of $base
*/
public $host;
/**
* Name of default scheme based on %URI.DefaultScheme and %URI.Base
*/
public $defaultScheme;
2016-02-09 10:06:17 +00:00
public function __construct()
{
2010-09-09 03:14:17 +00:00
$this->registerFilter(new HTMLPurifier_URIFilter_DisableExternal());
$this->registerFilter(new HTMLPurifier_URIFilter_DisableExternalResources());
2016-02-09 10:06:17 +00:00
$this->registerFilter(new HTMLPurifier_URIFilter_DisableResources());
2010-09-09 03:14:17 +00:00
$this->registerFilter(new HTMLPurifier_URIFilter_HostBlacklist());
2016-02-09 10:06:17 +00:00
$this->registerFilter(new HTMLPurifier_URIFilter_SafeIframe());
2010-09-09 03:14:17 +00:00
$this->registerFilter(new HTMLPurifier_URIFilter_MakeAbsolute());
$this->registerFilter(new HTMLPurifier_URIFilter_Munge());
}
2016-02-09 10:06:17 +00:00
public function registerFilter($filter)
{
2010-09-09 03:14:17 +00:00
$this->registeredFilters[$filter->name] = $filter;
}
2016-02-09 10:06:17 +00:00
public function addFilter($filter, $config)
{
2010-09-09 03:14:17 +00:00
$r = $filter->prepare($config);
if ($r === false) return; // null is ok, for backwards compat
if ($filter->post) {
$this->postFilters[$filter->name] = $filter;
} else {
$this->filters[$filter->name] = $filter;
}
}
2016-02-09 10:06:17 +00:00
protected function doSetup($config)
{
2010-09-09 03:14:17 +00:00
$this->setupMemberVariables($config);
$this->setupFilters($config);
}
2016-02-09 10:06:17 +00:00
protected function setupFilters($config)
{
2010-09-09 03:14:17 +00:00
foreach ($this->registeredFilters as $name => $filter) {
2016-02-09 10:06:17 +00:00
if ($filter->always_load) {
2010-09-09 03:14:17 +00:00
$this->addFilter($filter, $config);
2016-02-09 10:06:17 +00:00
} else {
$conf = $config->get('URI.' . $name);
if ($conf !== false && $conf !== null) {
$this->addFilter($filter, $config);
}
2010-09-09 03:14:17 +00:00
}
}
unset($this->registeredFilters);
}
2016-02-09 10:06:17 +00:00
protected function setupMemberVariables($config)
{
2010-09-09 03:14:17 +00:00
$this->host = $config->get('URI.Host');
$base_uri = $config->get('URI.Base');
if (!is_null($base_uri)) {
$parser = new HTMLPurifier_URIParser();
$this->base = $parser->parse($base_uri);
$this->defaultScheme = $this->base->scheme;
if (is_null($this->host)) $this->host = $this->base->host;
}
if (is_null($this->defaultScheme)) $this->defaultScheme = $config->get('URI.DefaultScheme');
}
2016-02-09 10:06:17 +00:00
public function getDefaultScheme($config, $context)
{
return HTMLPurifier_URISchemeRegistry::instance()->getScheme($this->defaultScheme, $config, $context);
}
public function filter(&$uri, $config, $context)
{
2010-09-09 03:14:17 +00:00
foreach ($this->filters as $name => $f) {
$result = $f->filter($uri, $config, $context);
if (!$result) return false;
}
return true;
}
2016-02-09 10:06:17 +00:00
public function postFilter(&$uri, $config, $context)
{
2010-09-09 03:14:17 +00:00
foreach ($this->postFilters as $name => $f) {
$result = $f->filter($uri, $config, $context);
if (!$result) return false;
}
return true;
}
}
// vim: et sw=4 sts=4