File indexing completed on 2024-04-28 05:58:54

0001 <?php
0002 
0003 class HTMLPurifier_URIDefinition extends HTMLPurifier_Definition
0004 {
0005 
0006     public $type = 'URI';
0007     protected $filters = array();
0008     protected $postFilters = array();
0009     protected $registeredFilters = array();
0010 
0011     /**
0012      * HTMLPurifier_URI object of the base specified at %URI.Base
0013      */
0014     public $base;
0015 
0016     /**
0017      * String host to consider "home" base, derived off of $base
0018      */
0019     public $host;
0020 
0021     /**
0022      * Name of default scheme based on %URI.DefaultScheme and %URI.Base
0023      */
0024     public $defaultScheme;
0025 
0026     public function __construct()
0027     {
0028         $this->registerFilter(new HTMLPurifier_URIFilter_DisableExternal());
0029         $this->registerFilter(new HTMLPurifier_URIFilter_DisableExternalResources());
0030         $this->registerFilter(new HTMLPurifier_URIFilter_DisableResources());
0031         $this->registerFilter(new HTMLPurifier_URIFilter_HostBlacklist());
0032         $this->registerFilter(new HTMLPurifier_URIFilter_SafeIframe());
0033         $this->registerFilter(new HTMLPurifier_URIFilter_MakeAbsolute());
0034         $this->registerFilter(new HTMLPurifier_URIFilter_Munge());
0035     }
0036 
0037     public function registerFilter($filter)
0038     {
0039         $this->registeredFilters[$filter->name] = $filter;
0040     }
0041 
0042     public function addFilter($filter, $config)
0043     {
0044         $r = $filter->prepare($config);
0045         if ($r === false) return; // null is ok, for backwards compat
0046         if ($filter->post) {
0047             $this->postFilters[$filter->name] = $filter;
0048         } else {
0049             $this->filters[$filter->name] = $filter;
0050         }
0051     }
0052 
0053     protected function doSetup($config)
0054     {
0055         $this->setupMemberVariables($config);
0056         $this->setupFilters($config);
0057     }
0058 
0059     protected function setupFilters($config)
0060     {
0061         foreach ($this->registeredFilters as $name => $filter) {
0062             if ($filter->always_load) {
0063                 $this->addFilter($filter, $config);
0064             } else {
0065                 $conf = $config->get('URI.' . $name);
0066                 if ($conf !== false && $conf !== null) {
0067                     $this->addFilter($filter, $config);
0068                 }
0069             }
0070         }
0071         unset($this->registeredFilters);
0072     }
0073 
0074     protected function setupMemberVariables($config)
0075     {
0076         $this->host = $config->get('URI.Host');
0077         $base_uri = $config->get('URI.Base');
0078         if (!is_null($base_uri)) {
0079             $parser = new HTMLPurifier_URIParser();
0080             $this->base = $parser->parse($base_uri);
0081             $this->defaultScheme = $this->base->scheme;
0082             if (is_null($this->host)) $this->host = $this->base->host;
0083         }
0084         if (is_null($this->defaultScheme)) $this->defaultScheme = $config->get('URI.DefaultScheme');
0085     }
0086 
0087     public function getDefaultScheme($config, $context)
0088     {
0089         return HTMLPurifier_URISchemeRegistry::instance()->getScheme($this->defaultScheme, $config, $context);
0090     }
0091 
0092     public function filter(&$uri, $config, $context)
0093     {
0094         foreach ($this->filters as $name => $f) {
0095             $result = $f->filter($uri, $config, $context);
0096             if (!$result) return false;
0097         }
0098         return true;
0099     }
0100 
0101     public function postFilter(&$uri, $config, $context)
0102     {
0103         foreach ($this->postFilters as $name => $f) {
0104             $result = $f->filter($uri, $config, $context);
0105             if (!$result) return false;
0106         }
0107         return true;
0108     }
0109 
0110 }
0111 
0112 // vim: et sw=4 sts=4