ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
Munge.php
Go to the documentation of this file.
1 <?php
2 
4 {
5  public $name = 'Munge';
6  public $post = true;
8 
9  protected $replace = array();
10 
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;
17  }
18  public function filter(&$uri, $config, $context) {
19  if ($context->get('EmbeddedURI', true) && !$this->doEmbed) return true;
20 
21  $scheme_obj = $uri->getSchemeObj($config, $context);
22  if (!$scheme_obj) return true; // ignore unknown schemes, maybe another postfilter did it
23  if (!$scheme_obj->browsable) return true; // ignore non-browseable schemes, since we can't munge those in a reasonable way
24  if ($uri->isBenign($config, $context)) return true; // don't redirect if a benign URL
25 
26  $this->makeReplace($uri, $config, $context);
27  $this->replace = array_map('rawurlencode', $this->replace);
28 
29  $new_uri = strtr($this->target, $this->replace);
30  $new_uri = $this->parser->parse($new_uri);
31  // don't redirect if the target host is the same as the
32  // starting host
33  if ($uri->host === $new_uri->host) return true;
34  $uri = $new_uri; // overwrite
35  return true;
36  }
37 
38  protected function makeReplace($uri, $config, $context) {
39  $string = $uri->toString();
40  // always available
41  $this->replace['%s'] = $string;
42  $this->replace['%r'] = $context->get('EmbeddedURI', true);
43  $token = $context->get('CurrentToken', true);
44  $this->replace['%n'] = $token ? $token->name : null;
45  $this->replace['%m'] = $context->get('CurrentAttr', true);
46  $this->replace['%p'] = $context->get('CurrentCSSProperty', true);
47  // not always available
48  if ($this->secretKey) $this->replace['%t'] = sha1($this->secretKey . ':' . $string);
49  }
50 
51 }
52 
53 // vim: et sw=4 sts=4