]>
Commit | Line | Data |
---|---|---|
f45a286b AD |
1 | <?php |
2 | ||
3 | class HTMLPurifier_URIDefinition extends HTMLPurifier_Definition | |
4 | { | |
5 | ||
6 | public $type = 'URI'; | |
7 | protected $filters = array(); | |
8 | protected $postFilters = array(); | |
9 | protected $registeredFilters = array(); | |
10 | ||
11 | /** | |
12 | * HTMLPurifier_URI object of the base specified at %URI.Base | |
13 | */ | |
14 | public $base; | |
15 | ||
16 | /** | |
17 | * String host to consider "home" base, derived off of $base | |
18 | */ | |
19 | public $host; | |
20 | ||
21 | /** | |
22 | * Name of default scheme based on %URI.DefaultScheme and %URI.Base | |
23 | */ | |
24 | public $defaultScheme; | |
25 | ||
26 | public function __construct() { | |
27 | $this->registerFilter(new HTMLPurifier_URIFilter_DisableExternal()); | |
28 | $this->registerFilter(new HTMLPurifier_URIFilter_DisableExternalResources()); | |
29 | $this->registerFilter(new HTMLPurifier_URIFilter_HostBlacklist()); | |
dd205fba | 30 | $this->registerFilter(new HTMLPurifier_URIFilter_SafeIframe()); |
f45a286b AD |
31 | $this->registerFilter(new HTMLPurifier_URIFilter_MakeAbsolute()); |
32 | $this->registerFilter(new HTMLPurifier_URIFilter_Munge()); | |
33 | } | |
34 | ||
35 | public function registerFilter($filter) { | |
36 | $this->registeredFilters[$filter->name] = $filter; | |
37 | } | |
38 | ||
39 | public function addFilter($filter, $config) { | |
40 | $r = $filter->prepare($config); | |
41 | if ($r === false) return; // null is ok, for backwards compat | |
42 | if ($filter->post) { | |
43 | $this->postFilters[$filter->name] = $filter; | |
44 | } else { | |
45 | $this->filters[$filter->name] = $filter; | |
46 | } | |
47 | } | |
48 | ||
49 | protected function doSetup($config) { | |
50 | $this->setupMemberVariables($config); | |
51 | $this->setupFilters($config); | |
52 | } | |
53 | ||
54 | protected function setupFilters($config) { | |
55 | foreach ($this->registeredFilters as $name => $filter) { | |
dd205fba | 56 | if ($filter->always_load) { |
f45a286b | 57 | $this->addFilter($filter, $config); |
dd205fba MK |
58 | } else { |
59 | $conf = $config->get('URI.' . $name); | |
60 | if ($conf !== false && $conf !== null) { | |
61 | $this->addFilter($filter, $config); | |
62 | } | |
f45a286b AD |
63 | } |
64 | } | |
65 | unset($this->registeredFilters); | |
66 | } | |
67 | ||
68 | protected function setupMemberVariables($config) { | |
f4f0f80d AD |
69 | $this->host = $config->get('URI.Host'); |
70 | $base_uri = $config->get('URI.Base'); | |
f45a286b AD |
71 | if (!is_null($base_uri)) { |
72 | $parser = new HTMLPurifier_URIParser(); | |
73 | $this->base = $parser->parse($base_uri); | |
74 | $this->defaultScheme = $this->base->scheme; | |
75 | if (is_null($this->host)) $this->host = $this->base->host; | |
76 | } | |
f4f0f80d | 77 | if (is_null($this->defaultScheme)) $this->defaultScheme = $config->get('URI.DefaultScheme'); |
f45a286b AD |
78 | } |
79 | ||
dd205fba MK |
80 | public function getDefaultScheme($config, $context) { |
81 | return HTMLPurifier_URISchemeRegistry::instance()->getScheme($this->defaultScheme, $config, $context); | |
82 | } | |
83 | ||
f45a286b AD |
84 | public function filter(&$uri, $config, $context) { |
85 | foreach ($this->filters as $name => $f) { | |
86 | $result = $f->filter($uri, $config, $context); | |
87 | if (!$result) return false; | |
88 | } | |
89 | return true; | |
90 | } | |
91 | ||
92 | public function postFilter(&$uri, $config, $context) { | |
93 | foreach ($this->postFilters as $name => $f) { | |
94 | $result = $f->filter($uri, $config, $context); | |
95 | if (!$result) return false; | |
96 | } | |
97 | return true; | |
98 | } | |
99 | ||
100 | } | |
101 | ||
102 | // vim: et sw=4 sts=4 |