~ubuntu-branches/ubuntu/natty/moodle/natty

« back to all changes in this revision

Viewing changes to lib/htmlpurifier/HTMLPurifier/AttrDef/CSS/FontFamily.php

  • Committer: Bazaar Package Importer
  • Author(s): Tomasz Muras
  • Date: 2010-10-30 12:19:28 UTC
  • mfrom: (1.1.12 upstream) (3.1.10 squeeze)
  • Revision ID: james.westby@ubuntu.com-20101030121928-qzobi6mctpnk4dif
Tags: 1.9.9.dfsg2-2
* Added Romanian translation
* Updated Japanese translation (closes: #596820)
* Backporting security fixes from Moodle 1.9.10 (closes: #601384)
   - Updated embedded CAS to 1.1.3
   - Added patch for MDL-24523:
     clean_text() not filtering text in markdown format
   - Added patch for MDL-24810 and upgraded customized HTML Purifier to 4.2.0 
   - Added patch for MDL-24258:
     students can delete their forum posts later than $CFG->maxeditingtime 
     under certain conditions
   - Added patch for MDL-23377:
     Can't delete quiz attempts in course without enrolled students

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
<?php
2
2
 
3
 
require_once 'HTMLPurifier/AttrDef.php';
4
 
 
5
 
// whitelisting allowed fonts would be nice
6
 
 
7
3
/**
8
4
 * Validates a font family list according to CSS spec
 
5
 * @todo whitelisting allowed fonts would be nice
9
6
 */
10
7
class HTMLPurifier_AttrDef_CSS_FontFamily extends HTMLPurifier_AttrDef
11
8
{
12
 
    
13
 
    function validate($string, $config, &$context) {
 
9
 
 
10
    public function validate($string, $config, $context) {
14
11
        static $generic_names = array(
15
12
            'serif' => true,
16
13
            'sans-serif' => true,
18
15
            'fantasy' => true,
19
16
            'cursive' => true
20
17
        );
21
 
        
 
18
 
22
19
        // assume that no font names contain commas in them
23
20
        $fonts = explode(',', $string);
24
21
        $final = '';
37
34
                $quote = $font[0];
38
35
                if ($font[$length - 1] !== $quote) continue;
39
36
                $font = substr($font, 1, $length - 2);
40
 
                
41
 
                $new_font = '';
42
 
                for ($i = 0, $c = strlen($font); $i < $c; $i++) {
43
 
                    if ($font[$i] === '\\') {
44
 
                        $i++;
45
 
                        if ($i >= $c) {
46
 
                            $new_font .= '\\';
47
 
                            break;
48
 
                        }
49
 
                        if (ctype_xdigit($font[$i])) {
50
 
                            $code = $font[$i];
51
 
                            for ($a = 1, $i++; $i < $c && $a < 6; $i++, $a++) {
52
 
                                if (!ctype_xdigit($font[$i])) break;
53
 
                                $code .= $font[$i];
54
 
                            }
55
 
                            // We have to be extremely careful when adding
56
 
                            // new characters, to make sure we're not breaking
57
 
                            // the encoding.
58
 
                            $char = HTMLPurifier_Encoder::unichr(hexdec($code));
59
 
                            if (HTMLPurifier_Encoder::cleanUTF8($char) === '') continue;
60
 
                            $new_font .= $char;
61
 
                            if ($i < $c && trim($font[$i]) !== '') $i--;
62
 
                            continue;
63
 
                        }
64
 
                        if ($font[$i] === "\n") continue;
65
 
                    }
66
 
                    $new_font .= $font[$i];
67
 
                }
68
 
                
69
 
                $font = $new_font;
70
37
            }
 
38
 
 
39
            $font = $this->expandCSSEscape($font);
 
40
 
71
41
            // $font is a pure representation of the font name
72
 
            
 
42
 
73
43
            if (ctype_alnum($font) && $font !== '') {
74
44
                // very simple font, allow it in unharmed
75
45
                $final .= $font . ', ';
76
46
                continue;
77
47
            }
78
 
            
 
48
 
 
49
            // bugger out on whitespace.  form feed (0C) really
 
50
            // shouldn't show up regardless
 
51
            $font = str_replace(array("\n", "\t", "\r", "\x0C"), ' ', $font);
 
52
 
 
53
            // These ugly transforms don't pose a security
 
54
            // risk (as \\ and \" might).  We could try to be clever and
 
55
            // use single-quote wrapping when there is a double quote
 
56
            // present, but I have choosen not to implement that.
 
57
            // (warning: this code relies on the selection of quotation
 
58
            // mark below)
 
59
            $font = str_replace('\\', '\\5C ', $font);
 
60
            $font = str_replace('"',  '\\22 ', $font);
 
61
 
79
62
            // complicated font, requires quoting
80
 
            
81
 
            // armor single quotes and new lines
82
 
            $font = str_replace("\\", "\\\\", $font);
83
 
            $font = str_replace("'", "\\'", $font);
84
 
            $final .= "'$font', ";
 
63
            $final .= "\"$font\", "; // note that this will later get turned into &quot;
85
64
        }
86
65
        $final = rtrim($final, ', ');
87
66
        if ($final === '') return false;
88
67
        return $final;
89
68
    }
90
 
    
 
69
 
91
70
}
92
71
 
 
72
// vim: et sw=4 sts=4