]> git.mxchange.org Git - friendica.git/blob - library/HTMLPurifier/AttrDef/CSS/FontFamily.php
make 'PHP "register_argc_argv"' easier to translate, may require fix for po2php
[friendica.git] / library / HTMLPurifier / AttrDef / CSS / FontFamily.php
1 <?php
2
3 /**
4  * Validates a font family list according to CSS spec
5  * @todo whitelisting allowed fonts would be nice
6  */
7 class HTMLPurifier_AttrDef_CSS_FontFamily extends HTMLPurifier_AttrDef
8 {
9
10     public function validate($string, $config, $context) {
11         static $generic_names = array(
12             'serif' => true,
13             'sans-serif' => true,
14             'monospace' => true,
15             'fantasy' => true,
16             'cursive' => true
17         );
18
19         // assume that no font names contain commas in them
20         $fonts = explode(',', $string);
21         $final = '';
22         foreach($fonts as $font) {
23             $font = trim($font);
24             if ($font === '') continue;
25             // match a generic name
26             if (isset($generic_names[$font])) {
27                 $final .= $font . ', ';
28                 continue;
29             }
30             // match a quoted name
31             if ($font[0] === '"' || $font[0] === "'") {
32                 $length = strlen($font);
33                 if ($length <= 2) continue;
34                 $quote = $font[0];
35                 if ($font[$length - 1] !== $quote) continue;
36                 $font = substr($font, 1, $length - 2);
37             }
38
39             $font = $this->expandCSSEscape($font);
40
41             // $font is a pure representation of the font name
42
43             if (ctype_alnum($font) && $font !== '') {
44                 // very simple font, allow it in unharmed
45                 $final .= $font . ', ';
46                 continue;
47             }
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
62             // complicated font, requires quoting
63             $final .= "\"$font\", "; // note that this will later get turned into &quot;
64         }
65         $final = rtrim($final, ', ');
66         if ($final === '') return false;
67         return $final;
68     }
69
70 }
71
72 // vim: et sw=4 sts=4