2006-08-16 17:25:25 +00:00
|
|
|
<?php
|
|
|
|
|
2006-08-20 21:47:15 +00:00
|
|
|
/**
|
|
|
|
* Validates a font family list according to CSS spec
|
|
|
|
*/
|
2007-02-14 20:38:51 +00:00
|
|
|
class HTMLPurifier_AttrDef_CSS_FontFamily extends HTMLPurifier_AttrDef
|
2006-08-16 17:25:25 +00:00
|
|
|
{
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-05 00:10:43 +00:00
|
|
|
public function validate($string, $config, $context) {
|
2007-05-20 17:23:09 +00:00
|
|
|
static $generic_names = array(
|
|
|
|
'serif' => true,
|
|
|
|
'sans-serif' => true,
|
|
|
|
'monospace' => true,
|
|
|
|
'fantasy' => true,
|
|
|
|
'cursive' => true
|
|
|
|
);
|
2011-03-24 22:54:39 +00:00
|
|
|
$allowed_fonts = $config->get('CSS.AllowedFonts');
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-16 17:25:25 +00:00
|
|
|
// assume that no font names contain commas in them
|
|
|
|
$fonts = explode(',', $string);
|
|
|
|
$final = '';
|
|
|
|
foreach($fonts as $font) {
|
|
|
|
$font = trim($font);
|
|
|
|
if ($font === '') continue;
|
|
|
|
// match a generic name
|
2007-05-20 17:23:09 +00:00
|
|
|
if (isset($generic_names[$font])) {
|
2011-03-24 22:54:39 +00:00
|
|
|
if ($allowed_fonts === null || isset($allowed_fonts[$font])) {
|
|
|
|
$final .= $font . ', ';
|
|
|
|
}
|
2006-08-16 17:25:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// match a quoted name
|
|
|
|
if ($font[0] === '"' || $font[0] === "'") {
|
|
|
|
$length = strlen($font);
|
|
|
|
if ($length <= 2) continue;
|
|
|
|
$quote = $font[0];
|
|
|
|
if ($font[$length - 1] !== $quote) continue;
|
|
|
|
$font = substr($font, 1, $length - 2);
|
2010-05-21 15:53:52 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2010-05-21 15:53:52 +00:00
|
|
|
$font = $this->expandCSSEscape($font);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-08-03 02:48:52 +00:00
|
|
|
// $font is a pure representation of the font name
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2011-03-24 22:54:39 +00:00
|
|
|
if ($allowed_fonts !== null && !isset($allowed_fonts[$font])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-05-26 04:35:12 +00:00
|
|
|
if (ctype_alnum($font) && $font !== '') {
|
2006-08-16 17:25:25 +00:00
|
|
|
// very simple font, allow it in unharmed
|
|
|
|
$final .= $font . ', ';
|
|
|
|
continue;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2010-05-21 15:53:52 +00:00
|
|
|
// bugger out on whitespace. form feed (0C) really
|
|
|
|
// shouldn't show up regardless
|
|
|
|
$font = str_replace(array("\n", "\t", "\r", "\x0C"), ' ', $font);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2010-05-21 15:53:52 +00:00
|
|
|
// These ugly transforms don't pose a security
|
|
|
|
// risk (as \\ and \" might). We could try to be clever and
|
|
|
|
// use single-quote wrapping when there is a double quote
|
|
|
|
// present, but I have choosen not to implement that.
|
|
|
|
// (warning: this code relies on the selection of quotation
|
|
|
|
// mark below)
|
|
|
|
$font = str_replace('\\', '\\5C ', $font);
|
|
|
|
$font = str_replace('"', '\\22 ', $font);
|
|
|
|
|
|
|
|
// complicated font, requires quoting
|
|
|
|
$final .= "\"$font\", "; // note that this will later get turned into "
|
2006-08-16 17:25:25 +00:00
|
|
|
}
|
|
|
|
$final = rtrim($final, ', ');
|
|
|
|
if ($final === '') return false;
|
|
|
|
return $final;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-16 17:25:25 +00:00
|
|
|
}
|
|
|
|
|
2008-12-06 09:24:59 +00:00
|
|
|
// vim: et sw=4 sts=4
|