]> git.mxchange.org Git - friendica.git/blob - library/HTMLPurifier/URIDefinition.php
mistpark 2.0 infrasturcture lands
[friendica.git] / library / HTMLPurifier / URIDefinition.php
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());
30         $this->registerFilter(new HTMLPurifier_URIFilter_MakeAbsolute());
31         $this->registerFilter(new HTMLPurifier_URIFilter_Munge());
32     }
33
34     public function registerFilter($filter) {
35         $this->registeredFilters[$filter->name] = $filter;
36     }
37
38     public function addFilter($filter, $config) {
39         $r = $filter->prepare($config);
40         if ($r === false) return; // null is ok, for backwards compat
41         if ($filter->post) {
42             $this->postFilters[$filter->name] = $filter;
43         } else {
44             $this->filters[$filter->name] = $filter;
45         }
46     }
47
48     protected function doSetup($config) {
49         $this->setupMemberVariables($config);
50         $this->setupFilters($config);
51     }
52
53     protected function setupFilters($config) {
54         foreach ($this->registeredFilters as $name => $filter) {
55             $conf = $config->get('URI.' . $name);
56             if ($conf !== false && $conf !== null) {
57                 $this->addFilter($filter, $config);
58             }
59         }
60         unset($this->registeredFilters);
61     }
62
63     protected function setupMemberVariables($config) {
64         $this->host = $config->get('URI.Host');
65         $base_uri = $config->get('URI.Base');
66         if (!is_null($base_uri)) {
67             $parser = new HTMLPurifier_URIParser();
68             $this->base = $parser->parse($base_uri);
69             $this->defaultScheme = $this->base->scheme;
70             if (is_null($this->host)) $this->host = $this->base->host;
71         }
72         if (is_null($this->defaultScheme)) $this->defaultScheme = $config->get('URI.DefaultScheme');
73     }
74
75     public function filter(&$uri, $config, $context) {
76         foreach ($this->filters as $name => $f) {
77             $result = $f->filter($uri, $config, $context);
78             if (!$result) return false;
79         }
80         return true;
81     }
82
83     public function postFilter(&$uri, $config, $context) {
84         foreach ($this->postFilters as $name => $f) {
85             $result = $f->filter($uri, $config, $context);
86             if (!$result) return false;
87         }
88         return true;
89     }
90
91 }
92
93 // vim: et sw=4 sts=4