~ubuntu-branches/ubuntu/maverick/mahara/maverick-updates

« back to all changes in this revision

Viewing changes to htdocs/lib/htmlpurifier/HTMLPurifier/Filter/ExtractStyleBlocks.php

  • Committer: Bazaar Package Importer
  • Author(s): Francois Marier
  • Date: 2009-10-30 13:46:40 UTC
  • mfrom: (6.2.6 sid)
  • Revision ID: james.westby@ubuntu.com-20091030134640-13srv45w4660pmic
Tags: 1.1.7-1
* New upstream release
  - Privilege escalation fix (CVE-2009-3298)
  - XSS fix (CVE-2009-3299)

* Bump Standards-Version up to 3.8.3
* Switch packaging license to refer to GPL-3
* debian/mahara.config: Move -e to a separate line to silence lintian

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 * This filter extracts <style> blocks from input HTML, cleans them up
5
5
 * using CSSTidy, and then places them in $purifier->context->get('StyleBlocks')
6
6
 * so they can be used elsewhere in the document.
7
 
 * 
 
7
 *
8
8
 * @note
9
9
 *      See tests/HTMLPurifier/Filter/ExtractStyleBlocksTest.php for
10
10
 *      sample usage.
11
 
 * 
 
11
 *
12
12
 * @note
13
13
 *      This filter can also be used on stylesheets not included in the
14
14
 *      document--something purists would probably prefer. Just directly
16
16
 */
17
17
class HTMLPurifier_Filter_ExtractStyleBlocks extends HTMLPurifier_Filter
18
18
{
19
 
    
 
19
 
20
20
    public $name = 'ExtractStyleBlocks';
21
21
    private $_styleMatches = array();
22
22
    private $_tidy;
23
 
    
 
23
 
24
24
    public function __construct() {
25
25
        $this->_tidy = new csstidy();
26
26
    }
27
 
    
 
27
 
28
28
    /**
29
29
     * Save the contents of CSS blocks to style matches
30
30
     * @param $matches preg_replace style $matches array
32
32
    protected function styleCallback($matches) {
33
33
        $this->_styleMatches[] = $matches[1];
34
34
    }
35
 
    
 
35
 
36
36
    /**
37
37
     * Removes inline <style> tags from HTML, saves them for later use
38
38
     * @todo Extend to indicate non-text/css style blocks
39
39
     */
40
40
    public function preFilter($html, $config, $context) {
41
 
        $tidy = $config->get('FilterParam', 'ExtractStyleBlocksTidyImpl');
 
41
        $tidy = $config->get('Filter.ExtractStyleBlocks.TidyImpl');
42
42
        if ($tidy !== null) $this->_tidy = $tidy;
43
43
        $html = preg_replace_callback('#<style(?:\s.*)?>(.+)</style>#isU', array($this, 'styleCallback'), $html);
44
44
        $style_blocks = $this->_styleMatches;
51
51
        }
52
52
        return $html;
53
53
    }
54
 
    
 
54
 
55
55
    /**
56
56
     * Takes CSS (the stuff found in <style>) and cleans it.
57
57
     * @warning Requires CSSTidy <http://csstidy.sourceforge.net/>
62
62
     */
63
63
    public function cleanCSS($css, $config, $context) {
64
64
        // prepare scope
65
 
        $scope = $config->get('FilterParam', 'ExtractStyleBlocksScope');
 
65
        $scope = $config->get('Filter.ExtractStyleBlocks.Scope');
66
66
        if ($scope !== null) {
67
67
            $scopes = array_map('trim', explode(',', $scope));
68
68
        } else {
120
120
        $css = $this->_tidy->print->plain();
121
121
        // we are going to escape any special characters <>& to ensure
122
122
        // that no funny business occurs (i.e. </style> in a font-family prop).
123
 
        if ($config->get('FilterParam', 'ExtractStyleBlocksEscaping')) {
 
123
        if ($config->get('Filter.ExtractStyleBlocks.Escaping')) {
124
124
            $css = str_replace(
125
125
                array('<',    '>',    '&'),
126
126
                array('\3C ', '\3E ', '\26 '),
129
129
        }
130
130
        return $css;
131
131
    }
132
 
    
 
132
 
133
133
}
134
134
 
 
135
// vim: et sw=4 sts=4