Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / lib / htmlpurifier / HTMLPurifier / PercentEncoder.php
blob7a12caaa76af9d1b8ab82f38a7ae9650f201ed72
1 <?php
3 /**
4 * Class that handles operations involving percent-encoding in URIs.
5 */
6 class HTMLPurifier_PercentEncoder
9 /**
10 * Fix up percent-encoding by decoding unreserved characters and normalizing
11 * @param $string String to normalize
13 function normalize($string) {
14 if ($string == '') return '';
15 $parts = explode('%', $string);
16 $ret = array_shift($parts);
17 foreach ($parts as $part) {
18 $length = strlen($part);
19 if ($length < 2) {
20 $ret .= '%25' . $part;
21 continue;
23 $encoding = substr($part, 0, 2);
24 $text = substr($part, 2);
25 if (!ctype_xdigit($encoding)) {
26 $ret .= '%25' . $part;
27 continue;
29 $int = hexdec($encoding);
30 if (
31 ($int >= 48 && $int <= 57) || // digits
32 ($int >= 65 && $int <= 90) || // uppercase letters
33 ($int >= 97 && $int <= 122) || // lowercase letters
34 $int == 126 || $int == 45 || $int == 46 || $int == 95 // ~-._
35 ) {
36 $ret .= chr($int) . $text;
37 continue;
39 $encoding = strtoupper($encoding);
40 $ret .= '%' . $encoding . $text;
42 return $ret;