~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to lib/htmlpurifier/HTMLPurifier/URIFilter/Munge.php

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
 
3
 
class HTMLPurifier_URIFilter_Munge extends HTMLPurifier_URIFilter
4
 
{
5
 
    public $name = 'Munge';
6
 
    public $post = true;
7
 
    private $target, $parser, $doEmbed, $secretKey;
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
 
1
<?php
 
2
 
 
3
class HTMLPurifier_URIFilter_Munge extends HTMLPurifier_URIFilter
 
4
{
 
5
    /**
 
6
     * @type string
 
7
     */
 
8
    public $name = 'Munge';
 
9
 
 
10
    /**
 
11
     * @type bool
 
12
     */
 
13
    public $post = true;
 
14
 
 
15
    /**
 
16
     * @type string
 
17
     */
 
18
    private $target;
 
19
 
 
20
    /**
 
21
     * @type HTMLPurifier_URIParser
 
22
     */
 
23
    private $parser;
 
24
 
 
25
    /**
 
26
     * @type bool
 
27
     */
 
28
    private $doEmbed;
 
29
 
 
30
    /**
 
31
     * @type string
 
32
     */
 
33
    private $secretKey;
 
34
 
 
35
    /**
 
36
     * @type array
 
37
     */
 
38
    protected $replace = array();
 
39
 
 
40
    /**
 
41
     * @param HTMLPurifier_Config $config
 
42
     * @return bool
 
43
     */
 
44
    public function prepare($config)
 
45
    {
 
46
        $this->target = $config->get('URI.' . $this->name);
 
47
        $this->parser = new HTMLPurifier_URIParser();
 
48
        $this->doEmbed = $config->get('URI.MungeResources');
 
49
        $this->secretKey = $config->get('URI.MungeSecretKey');
 
50
        if ($this->secretKey && !function_exists('hash_hmac')) {
 
51
            throw new Exception("Cannot use %URI.MungeSecretKey without hash_hmac support.");
 
52
        }
 
53
        return true;
 
54
    }
 
55
 
 
56
    /**
 
57
     * @param HTMLPurifier_URI $uri
 
58
     * @param HTMLPurifier_Config $config
 
59
     * @param HTMLPurifier_Context $context
 
60
     * @return bool
 
61
     */
 
62
    public function filter(&$uri, $config, $context)
 
63
    {
 
64
        if ($context->get('EmbeddedURI', true) && !$this->doEmbed) {
 
65
            return true;
 
66
        }
 
67
 
 
68
        $scheme_obj = $uri->getSchemeObj($config, $context);
 
69
        if (!$scheme_obj) {
 
70
            return true;
 
71
        } // ignore unknown schemes, maybe another postfilter did it
 
72
        if (!$scheme_obj->browsable) {
 
73
            return true;
 
74
        } // ignore non-browseable schemes, since we can't munge those in a reasonable way
 
75
        if ($uri->isBenign($config, $context)) {
 
76
            return true;
 
77
        } // don't redirect if a benign URL
 
78
 
 
79
        $this->makeReplace($uri, $config, $context);
 
80
        $this->replace = array_map('rawurlencode', $this->replace);
 
81
 
 
82
        $new_uri = strtr($this->target, $this->replace);
 
83
        $new_uri = $this->parser->parse($new_uri);
 
84
        // don't redirect if the target host is the same as the
 
85
        // starting host
 
86
        if ($uri->host === $new_uri->host) {
 
87
            return true;
 
88
        }
 
89
        $uri = $new_uri; // overwrite
 
90
        return true;
 
91
    }
 
92
 
 
93
    /**
 
94
     * @param HTMLPurifier_URI $uri
 
95
     * @param HTMLPurifier_Config $config
 
96
     * @param HTMLPurifier_Context $context
 
97
     */
 
98
    protected function makeReplace($uri, $config, $context)
 
99
    {
 
100
        $string = $uri->toString();
 
101
        // always available
 
102
        $this->replace['%s'] = $string;
 
103
        $this->replace['%r'] = $context->get('EmbeddedURI', true);
 
104
        $token = $context->get('CurrentToken', true);
 
105
        $this->replace['%n'] = $token ? $token->name : null;
 
106
        $this->replace['%m'] = $context->get('CurrentAttr', true);
 
107
        $this->replace['%p'] = $context->get('CurrentCSSProperty', true);
 
108
        // not always available
 
109
        if ($this->secretKey) {
 
110
            $this->replace['%t'] = hash_hmac("sha256", $string, $this->secretKey);
 
111
        }
 
112
    }
 
113
}
 
114
 
 
115
// vim: et sw=4 sts=4