1<?php
2
3class 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    {
28        $this->registerFilter(new HTMLPurifier_URIFilter_DisableExternal());
29        $this->registerFilter(new HTMLPurifier_URIFilter_DisableExternalResources());
30        $this->registerFilter(new HTMLPurifier_URIFilter_DisableResources());
31        $this->registerFilter(new HTMLPurifier_URIFilter_HostBlacklist());
32        $this->registerFilter(new HTMLPurifier_URIFilter_SafeIframe());
33        $this->registerFilter(new HTMLPurifier_URIFilter_MakeAbsolute());
34        $this->registerFilter(new HTMLPurifier_URIFilter_Munge());
35    }
36
37    public function registerFilter($filter)
38    {
39        $this->registeredFilters[$filter->name] = $filter;
40    }
41
42    public function addFilter($filter, $config)
43    {
44        $r = $filter->prepare($config);
45        if ($r === false) return; // null is ok, for backwards compat
46        if ($filter->post) {
47            $this->postFilters[$filter->name] = $filter;
48        } else {
49            $this->filters[$filter->name] = $filter;
50        }
51    }
52
53    protected function doSetup($config)
54    {
55        $this->setupMemberVariables($config);
56        $this->setupFilters($config);
57    }
58
59    protected function setupFilters($config)
60    {
61        foreach ($this->registeredFilters as $name => $filter) {
62            if ($filter->always_load) {
63                $this->addFilter($filter, $config);
64            } else {
65                $conf = $config->get('URI.' . $name);
66                if ($conf !== false && $conf !== null) {
67                    $this->addFilter($filter, $config);
68                }
69            }
70        }
71        unset($this->registeredFilters);
72    }
73
74    protected function setupMemberVariables($config)
75    {
76        $this->host = $config->get('URI.Host');
77        $base_uri = $config->get('URI.Base');
78        if (!is_null($base_uri)) {
79            $parser = new HTMLPurifier_URIParser();
80            $this->base = $parser->parse($base_uri);
81            $this->defaultScheme = $this->base->scheme;
82            if (is_null($this->host)) $this->host = $this->base->host;
83        }
84        if (is_null($this->defaultScheme)) $this->defaultScheme = $config->get('URI.DefaultScheme');
85    }
86
87    public function getDefaultScheme($config, $context)
88    {
89        return HTMLPurifier_URISchemeRegistry::instance()->getScheme($this->defaultScheme, $config, $context);
90    }
91
92    public function filter(&$uri, $config, $context)
93    {
94        foreach ($this->filters as $name => $f) {
95            $result = $f->filter($uri, $config, $context);
96            if (!$result) return false;
97        }
98        return true;
99    }
100
101    public function postFilter(&$uri, $config, $context)
102    {
103        foreach ($this->postFilters as $name => $f) {
104            $result = $f->filter($uri, $config, $context);
105            if (!$result) return false;
106        }
107        return true;
108    }
109
110}
111
112// vim: et sw=4 sts=4
113