Add vim modelines to all files.
[htmlpurifier.git] / library / HTMLPurifier / URIFilter / Munge.php
blob29ed0ed1f27c9575273613e449044342367c59de
1 <?php
3 class HTMLPurifier_URIFilter_Munge extends HTMLPurifier_URIFilter
5 public $name = 'Munge';
6 public $post = true;
7 private $target, $parser, $doEmbed, $secretKey;
9 protected $replace = array();
11 public function prepare($config) {
12 $this->target = $config->get('URI', $this->name);
13 $this->parser = new HTMLPurifier_URIParser();
14 $this->doEmbed = $config->get('URI', 'MungeResources');
15 $this->secretKey = $config->get('URI', 'MungeSecretKey');
16 return true;
18 public function filter(&$uri, $config, $context) {
19 if ($context->get('EmbeddedURI', true) && !$this->doEmbed) return true;
21 $scheme_obj = $uri->getSchemeObj($config, $context);
22 if (!$scheme_obj) return true; // ignore unknown schemes, maybe another postfilter did it
23 if (is_null($uri->host) || empty($scheme_obj->browsable)) {
24 return true;
27 $this->makeReplace($uri, $config, $context);
28 $this->replace = array_map('rawurlencode', $this->replace);
30 $new_uri = strtr($this->target, $this->replace);
31 $new_uri = $this->parser->parse($new_uri);
32 // don't redirect if the target host is the same as the
33 // starting host
34 if ($uri->host === $new_uri->host) return true;
35 $uri = $new_uri; // overwrite
36 return true;
39 protected function makeReplace($uri, $config, $context) {
40 $string = $uri->toString();
41 // always available
42 $this->replace['%s'] = $string;
43 $this->replace['%r'] = $context->get('EmbeddedURI', true);
44 $token = $context->get('CurrentToken', true);
45 $this->replace['%n'] = $token ? $token->name : null;
46 $this->replace['%m'] = $context->get('CurrentAttr', true);
47 $this->replace['%p'] = $context->get('CurrentCSSProperty', true);
48 // not always available
49 if ($this->secretKey) $this->replace['%t'] = sha1($this->secretKey . ':' . $string);
54 // vim: et sw=4 sts=4