PluginProbe
WebTotem Security / 2.1.2
WebTotem Security v2.1.2
3.0.1 3.0.0 trunk 1.0 1.1 1.2 1.3 1.3.1 1.3.2 1.3.3 2.0 2.1 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.1 2.2.2 2.2.3 2.2.4 All 109 releases
wt-security / library / Idn.php

Idn.php in WebTotem Security 2.1.2, at library/Idn.php

103 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php defined('ABSPATH') or die("Protected By WT!");
2
3 final class WTSEC_LIBRARY_Idn
4 {
5 private static $decodeTable = array(
6 'a' => 0, 'b' => 1, 'c' => 2, 'd' => 3, 'e' => 4, 'f' => 5,
7 'g' => 6, 'h' => 7, 'i' => 8, 'j' => 9, 'k' => 10, 'l' => 11,
8 'm' => 12, 'n' => 13, 'o' => 14, 'p' => 15, 'q' => 16, 'r' => 17,
9 's' => 18, 't' => 19, 'u' => 20, 'v' => 21, 'w' => 22, 'x' => 23,
10 'y' => 24, 'z' => 25, '0' => 26, '1' => 27, '2' => 28, '3' => 29,
11 '4' => 30, '5' => 31, '6' => 32, '7' => 33, '8' => 34, '9' => 35,
12 );
13 public static function idn_to_utf8($domain)
14 {
15 if (!function_exists('mb_strtolower')) {
16 return $domain;
17 }
18 $domain = mb_strtolower($domain);
19 $parts = explode('.', $domain);
20 foreach ($parts as &$part) {
21 $length = \strlen($part);
22 if ($length < 1 || 63 < $length) {
23 continue;
24 }
25 if (0 !== strpos($part, 'xn--')) {
26 continue;
27 }
28 $part = substr($part, 4);
29 $part = self::decodePart($part);
30 }
31 $output = implode('.', $parts);
32 return \strlen($output) > 255 ? false : mb_strtolower($output);
33 }
34 private static function calculateThreshold($k, $bias)
35 {
36 if ($k <= $bias + 1) {
37 return 1;
38 }
39 if ($k >= $bias + 26) {
40 return 26;
41 }
42 return $k - $bias;
43 }
44 private static function adapt($delta, $numPoints, $firstTime)
45 {
46 $delta = (int) ($firstTime ? $delta / 700 : $delta / 2);
47 $delta += (int) ($delta / $numPoints);
48 $k = 0;
49 while ($delta > 35 * 13) {
50 $delta = (int) ($delta / 35);
51 $k = $k + 36;
52 }
53 return $k + (int) (36 * $delta / ($delta + 38));
54 }
55 private static function decodePart($input)
56 {
57 $n = 128;
58 $i = 0;
59 $bias = 72;
60 $output = '';
61 $pos = strrpos($input, '-');
62 if (false !== $pos) {
63 $output = substr($input, 0, $pos++);
64 } else {
65 $pos = 0;
66 }
67 $outputLength = \strlen($output);
68 $inputLength = \strlen($input);
69 while ($pos < $inputLength) {
70 $oldi = $i;
71 $w = 1;
72 for ($k = 36;; $k += 36) {
73 $digit = self::$decodeTable[$input[$pos++]];
74 $i += $digit * $w;
75 $t = self::calculateThreshold($k, $bias);
76 if ($digit < $t) {
77 break;
78 }
79 $w *= 36 - $t;
80 }
81 $bias = self::adapt($i - $oldi, ++$outputLength, 0 === $oldi);
82 $n = $n + (int) ($i / $outputLength);
83 $i = $i % $outputLength;
84 $output = mb_substr($output, 0, $i, 'utf-8').mb_chr($n, 'utf-8').mb_substr($output, $i, $outputLength - 1, 'utf-8');
85 ++$i;
86 }
87 return $output;
88 }
89 }
90 if (!function_exists('mb_chr')) {
91 function mb_chr($ord, $encoding = 'UTF-8') {
92 if ($encoding === 'UCS-4BE') {
93 return pack("N", $ord);
94 } else {
95 return mb_convert_encoding(mb_chr($ord, 'UCS-4BE'), $encoding, 'UCS-4BE');
96 }
97 }
98 }
99 if (!function_exists('mb_convert_encoding')) {
100 function mb_convert_encoding($str, $to_encoding, $from_encoding = NULL) {
101 return iconv(($from_encoding === NULL) ? mb_internal_encoding() : $from_encoding, $to_encoding, $str);
102 }
103 }