3 require_once 'HTMLPurifier/AttrDef.php';
6 * Validates the HTML attribute lang, effectively a language code.
7 * @note Built according to RFC 3066, which obsoleted RFC 1766
9 class HTMLPurifier_AttrDef_Lang
extends HTMLPurifier_AttrDef
12 function validate($string, $config, &$context) {
14 $string = trim($string);
15 if (!$string) return false;
17 $subtags = explode('-', $string);
18 $num_subtags = count($subtags);
20 if ($num_subtags == 0) return false; // sanity check
22 // process primary subtag : $subtags[0]
23 $length = strlen($subtags[0]);
28 if (! ($subtags[0] == 'x' ||
$subtags[0] == 'i') ) {
34 if (! ctype_alpha($subtags[0]) ) {
36 } elseif (! ctype_lower($subtags[0]) ) {
37 $subtags[0] = strtolower($subtags[0]);
44 $new_string = $subtags[0];
45 if ($num_subtags == 1) return $new_string;
47 // process second subtag : $subtags[1]
48 $length = strlen($subtags[1]);
49 if ($length == 0 ||
($length == 1 && $subtags[1] != 'x') ||
$length > 8 ||
!ctype_alnum($subtags[1])) {
52 if (!ctype_lower($subtags[1])) $subtags[1] = strtolower($subtags[1]);
54 $new_string .= '-' . $subtags[1];
55 if ($num_subtags == 2) return $new_string;
57 // process all other subtags, index 2 and up
58 for ($i = 2; $i < $num_subtags; $i++
) {
59 $length = strlen($subtags[$i]);
60 if ($length == 0 ||
$length > 8 ||
!ctype_alnum($subtags[$i])) {
63 if (!ctype_lower($subtags[$i])) {
64 $subtags[$i] = strtolower($subtags[$i]);
66 $new_string .= '-' . $subtags[$i];