HTMLPurifier 4.4.0
|
00001 <?php 00002 00003 class HTMLPurifier_URIFilter_Munge extends HTMLPurifier_URIFilter 00004 { 00005 public $name = 'Munge'; 00006 public $post = true; 00007 private $target, $parser, $doEmbed, $secretKey; 00008 00009 protected $replace = array(); 00010 00011 public function prepare($config) { 00012 $this->target = $config->get('URI.' . $this->name); 00013 $this->parser = new HTMLPurifier_URIParser(); 00014 $this->doEmbed = $config->get('URI.MungeResources'); 00015 $this->secretKey = $config->get('URI.MungeSecretKey'); 00016 return true; 00017 } 00018 public function filter(&$uri, $config, $context) { 00019 if ($context->get('EmbeddedURI', true) && !$this->doEmbed) return true; 00020 00021 $scheme_obj = $uri->getSchemeObj($config, $context); 00022 if (!$scheme_obj) return true; // ignore unknown schemes, maybe another postfilter did it 00023 if (!$scheme_obj->browsable) return true; // ignore non-browseable schemes, since we can't munge those in a reasonable way 00024 if ($uri->isBenign($config, $context)) return true; // don't redirect if a benign URL 00025 00026 $this->makeReplace($uri, $config, $context); 00027 $this->replace = array_map('rawurlencode', $this->replace); 00028 00029 $new_uri = strtr($this->target, $this->replace); 00030 $new_uri = $this->parser->parse($new_uri); 00031 // don't redirect if the target host is the same as the 00032 // starting host 00033 if ($uri->host === $new_uri->host) return true; 00034 $uri = $new_uri; // overwrite 00035 return true; 00036 } 00037 00038 protected function makeReplace($uri, $config, $context) { 00039 $string = $uri->toString(); 00040 // always available 00041 $this->replace['%s'] = $string; 00042 $this->replace['%r'] = $context->get('EmbeddedURI', true); 00043 $token = $context->get('CurrentToken', true); 00044 $this->replace['%n'] = $token ? $token->name : null; 00045 $this->replace['%m'] = $context->get('CurrentAttr', true); 00046 $this->replace['%p'] = $context->get('CurrentCSSProperty', true); 00047 // not always available 00048 if ($this->secretKey) $this->replace['%t'] = sha1($this->secretKey . ':' . $string); 00049 } 00050 00051 } 00052 00053 // vim: et sw=4 sts=4