Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / lib / htmlpurifier / HTMLPurifier / AttrDef / Lang.php
blob72d67f643c8324d11a985e90b61ad640b3a808ad
1 <?php
3 require_once 'HTMLPurifier/AttrDef.php';
5 /**
6 * Validates the HTML attribute lang, effectively a language code.
7 * @note Built according to RFC 3066, which obsoleted RFC 1766
8 */
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]);
24 switch ($length) {
25 case 0:
26 return false;
27 case 1:
28 if (! ($subtags[0] == 'x' || $subtags[0] == 'i') ) {
29 return false;
31 break;
32 case 2:
33 case 3:
34 if (! ctype_alpha($subtags[0]) ) {
35 return false;
36 } elseif (! ctype_lower($subtags[0]) ) {
37 $subtags[0] = strtolower($subtags[0]);
39 break;
40 default:
41 return false;
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])) {
50 return $new_string;
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])) {
61 return $new_string;
63 if (!ctype_lower($subtags[$i])) {
64 $subtags[$i] = strtolower($subtags[$i]);
66 $new_string .= '-' . $subtags[$i];
69 return $new_string;