3
require_once 'HTMLPurifier/AttrDef.php';
5
// whitelisting allowed fonts would be nice
8
* Validates a font family list according to CSS spec
10
class HTMLPurifier_AttrDef_CSS_FontFamily extends HTMLPurifier_AttrDef
13
function validate($string, $config, &$context) {
14
static $generic_names = array(
22
// assume that no font names contain commas in them
23
$fonts = explode(',', $string);
25
foreach($fonts as $font) {
27
if ($font === '') continue;
28
// match a generic name
29
if (isset($generic_names[$font])) {
30
$final .= $font . ', ';
33
// match a quoted name
34
if ($font[0] === '"' || $font[0] === "'") {
35
$length = strlen($font);
36
if ($length <= 2) continue;
38
if ($font[$length - 1] !== $quote) continue;
39
$font = substr($font, 1, $length - 2);
42
for ($i = 0, $c = strlen($font); $i < $c; $i++) {
43
if ($font[$i] === '\\') {
49
if (ctype_xdigit($font[$i])) {
51
for ($a = 1, $i++; $i < $c && $a < 6; $i++, $a++) {
52
if (!ctype_xdigit($font[$i])) break;
55
// We have to be extremely careful when adding
56
// new characters, to make sure we're not breaking
58
$char = HTMLPurifier_Encoder::unichr(hexdec($code));
59
if (HTMLPurifier_Encoder::cleanUTF8($char) === '') continue;
61
if ($i < $c && trim($font[$i]) !== '') $i--;
64
if ($font[$i] === "\n") continue;
66
$new_font .= $font[$i];
71
// $font is a pure representation of the font name
73
if (ctype_alnum($font) && $font !== '') {
74
// very simple font, allow it in unharmed
75
$final .= $font . ', ';
79
// complicated font, requires quoting
81
// armor single quotes and new lines
82
$font = str_replace("\\", "\\\\", $font);
83
$font = str_replace("'", "\\'", $font);
84
$final .= "'$font', ";
86
$final = rtrim($final, ', ');
87
if ($final === '') return false;