~ubuntu-branches/ubuntu/jaunty/moodle/jaunty

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Jordan Mantha, Matt Oquist
  • Date: 2009-02-25 15:16:22 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20090225151622-0ekt1liwhv2obfza
Tags: 1.9.4.dfsg-0ubuntu1
* Merge with Debian git (Closes LP: #322961, #239481, #334611):
  - use Ubuntu's smarty lib directory for linking
  - use internal yui library 
  - add update-notifier support back in

[Matt Oquist]
  * renamed prerm script
  * significantly rewrote postinst and other maintainer scripts to improve
    user experience and package maintainability
    (Closes LP: #225662, #325450, #327843, #303078, #234609)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
require_once 'HTMLPurifier/AttrDef.php';
 
4
 
 
5
// whitelisting allowed fonts would be nice
 
6
 
 
7
/**
 
8
 * Validates a font family list according to CSS spec
 
9
 */
 
10
class HTMLPurifier_AttrDef_CSS_FontFamily extends HTMLPurifier_AttrDef
 
11
{
 
12
    
 
13
    function validate($string, $config, &$context) {
 
14
        static $generic_names = array(
 
15
            'serif' => true,
 
16
            'sans-serif' => true,
 
17
            'monospace' => true,
 
18
            'fantasy' => true,
 
19
            'cursive' => true
 
20
        );
 
21
        
 
22
        // assume that no font names contain commas in them
 
23
        $fonts = explode(',', $string);
 
24
        $final = '';
 
25
        foreach($fonts as $font) {
 
26
            $font = trim($font);
 
27
            if ($font === '') continue;
 
28
            // match a generic name
 
29
            if (isset($generic_names[$font])) {
 
30
                $final .= $font . ', ';
 
31
                continue;
 
32
            }
 
33
            // match a quoted name
 
34
            if ($font[0] === '"' || $font[0] === "'") {
 
35
                $length = strlen($font);
 
36
                if ($length <= 2) continue;
 
37
                $quote = $font[0];
 
38
                if ($font[$length - 1] !== $quote) continue;
 
39
                $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
            }
 
71
            // $font is a pure representation of the font name
 
72
            
 
73
            if (ctype_alnum($font) && $font !== '') {
 
74
                // very simple font, allow it in unharmed
 
75
                $final .= $font . ', ';
 
76
                continue;
 
77
            }
 
78
            
 
79
            // 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', ";
 
85
        }
 
86
        $final = rtrim($final, ', ');
 
87
        if ($final === '') return false;
 
88
        return $final;
 
89
    }
 
90
    
 
91
}
 
92