1<?php
2
3/**
4 * Validates the HTML attribute lang, effectively a language code.
5 * @note Built according to RFC 3066, which obsoleted RFC 1766
6 */
7class HTMLPurifier_AttrDef_Lang extends HTMLPurifier_AttrDef
8{
9
10    /**
11     * @param string $string
12     * @param HTMLPurifier_Config $config
13     * @param HTMLPurifier_Context $context
14     * @return bool|string
15     */
16    public function validate($string, $config, $context)
17    {
18        $string = trim($string);
19        if (!$string) {
20            return false;
21        }
22
23        $subtags = explode('-', $string);
24        $num_subtags = count($subtags);
25
26        if ($num_subtags == 0) { // sanity check
27            return false;
28        }
29
30        // process primary subtag : $subtags[0]
31        $length = strlen($subtags[0]);
32        switch ($length) {
33            case 0:
34                return false;
35            case 1:
36                if (!($subtags[0] == 'x' || $subtags[0] == 'i')) {
37                    return false;
38                }
39                break;
40            case 2:
41            case 3:
42                if (!ctype_alpha($subtags[0])) {
43                    return false;
44                } elseif (!ctype_lower($subtags[0])) {
45                    $subtags[0] = strtolower($subtags[0]);
46                }
47                break;
48            default:
49                return false;
50        }
51
52        $new_string = $subtags[0];
53        if ($num_subtags == 1) {
54            return $new_string;
55        }
56
57        // process second subtag : $subtags[1]
58        $length = strlen($subtags[1]);
59        if ($length == 0 || ($length == 1 && $subtags[1] != 'x') || $length > 8 || !ctype_alnum($subtags[1])) {
60            return $new_string;
61        }
62        if (!ctype_lower($subtags[1])) {
63            $subtags[1] = strtolower($subtags[1]);
64        }
65
66        $new_string .= '-' . $subtags[1];
67        if ($num_subtags == 2) {
68            return $new_string;
69        }
70
71        // process all other subtags, index 2 and up
72        for ($i = 2; $i < $num_subtags; $i++) {
73            $length = strlen($subtags[$i]);
74            if ($length == 0 || $length > 8 || !ctype_alnum($subtags[$i])) {
75                return $new_string;
76            }
77            if (!ctype_lower($subtags[$i])) {
78                $subtags[$i] = strtolower($subtags[$i]);
79            }
80            $new_string .= '-' . $subtags[$i];
81        }
82        return $new_string;
83    }
84}
85
86// vim: et sw=4 sts=4
87