HTMLPurifier 4.4.0
|
00001 <?php 00002 00007 class HTMLPurifier_AttrDef_Lang extends HTMLPurifier_AttrDef 00008 { 00009 00010 public function validate($string, $config, $context) { 00011 00012 $string = trim($string); 00013 if (!$string) return false; 00014 00015 $subtags = explode('-', $string); 00016 $num_subtags = count($subtags); 00017 00018 if ($num_subtags == 0) return false; // sanity check 00019 00020 // process primary subtag : $subtags[0] 00021 $length = strlen($subtags[0]); 00022 switch ($length) { 00023 case 0: 00024 return false; 00025 case 1: 00026 if (! ($subtags[0] == 'x' || $subtags[0] == 'i') ) { 00027 return false; 00028 } 00029 break; 00030 case 2: 00031 case 3: 00032 if (! ctype_alpha($subtags[0]) ) { 00033 return false; 00034 } elseif (! ctype_lower($subtags[0]) ) { 00035 $subtags[0] = strtolower($subtags[0]); 00036 } 00037 break; 00038 default: 00039 return false; 00040 } 00041 00042 $new_string = $subtags[0]; 00043 if ($num_subtags == 1) return $new_string; 00044 00045 // process second subtag : $subtags[1] 00046 $length = strlen($subtags[1]); 00047 if ($length == 0 || ($length == 1 && $subtags[1] != 'x') || $length > 8 || !ctype_alnum($subtags[1])) { 00048 return $new_string; 00049 } 00050 if (!ctype_lower($subtags[1])) $subtags[1] = strtolower($subtags[1]); 00051 00052 $new_string .= '-' . $subtags[1]; 00053 if ($num_subtags == 2) return $new_string; 00054 00055 // process all other subtags, index 2 and up 00056 for ($i = 2; $i < $num_subtags; $i++) { 00057 $length = strlen($subtags[$i]); 00058 if ($length == 0 || $length > 8 || !ctype_alnum($subtags[$i])) { 00059 return $new_string; 00060 } 00061 if (!ctype_lower($subtags[$i])) { 00062 $subtags[$i] = strtolower($subtags[$i]); 00063 } 00064 $new_string .= '-' . $subtags[$i]; 00065 } 00066 00067 return $new_string; 00068 00069 } 00070 00071 } 00072 00073 // vim: et sw=4 sts=4