Source for file FontFamily.php

Documentation is available at 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. {
  8.     
  9.     public function validate($string$config$context{
  10.         static $generic_names array(
  11.             'serif' => true,
  12.             'sans-serif' => true,
  13.             'monospace' => true,
  14.             'fantasy' => true,
  15.             'cursive' => true
  16.         );
  17.         
  18.         // assume that no font names contain commas in them
  19.         $fonts explode(','$string);
  20.         $final '';
  21.         foreach($fonts as $font{
  22.             $font trim($font);
  23.             if ($font === ''continue;
  24.             // match a generic name
  25.             if (isset($generic_names[$font])) {
  26.                 $final .= $font ', ';
  27.                 continue;
  28.             }
  29.             // match a quoted name
  30.             if ($font[0=== '"' || $font[0=== "'"{
  31.                 $length strlen($font);
  32.                 if ($length <= 2continue;
  33.                 $quote $font[0];
  34.                 if ($font[$length 1!== $quotecontinue;
  35.                 $font substr($font1$length 2);
  36.                 
  37.                 $new_font '';
  38.                 for ($i 0$c strlen($font)$i $c$i++{
  39.                     if ($font[$i=== '\\'{
  40.                         $i++;
  41.                         if ($i >= $c{
  42.                             $new_font .= '\\';
  43.                             break;
  44.                         }
  45.                         if (ctype_xdigit($font[$i])) {
  46.                             $code $font[$i];
  47.                             for ($a 1$i++$i $c && $a 6$i++$a++{
  48.                                 if (!ctype_xdigit($font[$i])) break;
  49.                                 $code .= $font[$i];
  50.                             }
  51.                             // We have to be extremely careful when adding
  52.                             // new characters, to make sure we're not breaking
  53.                             // the encoding.
  54.                             $char HTMLPurifier_Encoder::unichr(hexdec($code));
  55.                             if (HTMLPurifier_Encoder::cleanUTF8($char=== ''continue;
  56.                             $new_font .= $char;
  57.                             if ($i $c && trim($font[$i]!== ''$i--;
  58.                             continue;
  59.                         }
  60.                         if ($font[$i=== "\n"continue;
  61.                     }
  62.                     $new_font .= $font[$i];
  63.                 }
  64.                 
  65.                 $font $new_font;
  66.             }
  67.             // $font is a pure representation of the font name
  68.             
  69.             if (ctype_alnum($font&& $font !== ''{
  70.                 // very simple font, allow it in unharmed
  71.                 $final .= $font ', ';
  72.                 continue;
  73.             }
  74.             
  75.             // complicated font, requires quoting
  76.             
  77.             // armor single quotes and new lines
  78.             $font str_replace("\\""\\\\"$font);
  79.             $font str_replace("'""\\'"$font);
  80.             $final .= "'$font', ";
  81.         }
  82.         $final rtrim($final', ');
  83.         if ($final === ''return false;
  84.         return $final;
  85.     }
  86.     
  87. }

Documentation generated on Thu, 19 Jun 2008 18:49:15 -0400 by phpDocumentor 1.4.2