PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.17
Independent Analytics – WordPress Analytics Plugin v1.17
2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / vendor / masterminds / html5 / src / HTML5 / Parser / UTF8Utils.php
independent-analytics / vendor / masterminds / html5 / src / HTML5 / Parser Last commit date
CharacterReference.php 3 years ago DOMTreeBuilder.php 3 years ago EventHandler.php 3 years ago FileInputStream.php 3 years ago InputStream.php 3 years ago ParseError.php 3 years ago Scanner.php 3 years ago StringInputStream.php 3 years ago Tokenizer.php 3 years ago TreeBuildingRules.php 3 years ago UTF8Utils.php 3 years ago
UTF8Utils.php
165 lines
1 <?php
2
3 namespace IAWP\Masterminds\HTML5\Parser;
4
5 /*
6 Portions based on code from html5lib files with the following copyright:
7
8 Copyright 2009 Geoffrey Sneddon <http://gsnedders.com/>
9
10 Permission is hereby granted, free of charge, to any person obtaining a
11 copy of this software and associated documentation files (the
12 "Software"), to deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify, merge, publish,
14 distribute, sublicense, and/or sell copies of the Software, and to
15 permit persons to whom the Software is furnished to do so, subject to
16 the following conditions:
17
18 The above copyright notice and this permission notice shall be included
19 in all copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
25 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 */
29 use IAWP\Masterminds\HTML5\Exception;
30 class UTF8Utils
31 {
32 /**
33 * The Unicode replacement character.
34 */
35 const FFFD = "";
36 /**
37 * Count the number of characters in a string.
38 * UTF-8 aware. This will try (in order) iconv, MB, libxml, and finally a custom counter.
39 *
40 * @param string $string
41 *
42 * @return int
43 */
44 public static function countChars($string)
45 {
46 // Get the length for the string we need.
47 if (\function_exists('mb_strlen')) {
48 return \mb_strlen($string, 'utf-8');
49 }
50 if (\function_exists('iconv_strlen')) {
51 return \iconv_strlen($string, 'utf-8');
52 }
53 if (\function_exists('utf8_decode')) {
54 // MPB: Will this work? Won't certain decodes lead to two chars
55 // extrapolated out of 2-byte chars?
56 return \strlen(\utf8_decode($string));
57 }
58 $count = \count_chars($string);
59 // 0x80 = 0x7F - 0 + 1 (one added to get inclusive range)
60 // 0x33 = 0xF4 - 0x2C + 1 (one added to get inclusive range)
61 return \array_sum(\array_slice($count, 0, 0x80)) + \array_sum(\array_slice($count, 0xc2, 0x33));
62 }
63 /**
64 * Convert data from the given encoding to UTF-8.
65 *
66 * This has not yet been tested with charactersets other than UTF-8.
67 * It should work with ISO-8859-1/-13 and standard Latin Win charsets.
68 *
69 * @param string $data The data to convert
70 * @param string $encoding A valid encoding. Examples: http://www.php.net/manual/en/mbstring.supported-encodings.php
71 *
72 * @return string
73 */
74 public static function convertToUTF8($data, $encoding = 'UTF-8')
75 {
76 /*
77 * From the HTML5 spec: Given an encoding, the bytes in the input stream must be converted
78 * to Unicode characters for the tokeniser, as described by the rules for that encoding,
79 * except that the leading U+FEFF BYTE ORDER MARK character, if any, must not be stripped
80 * by the encoding layer (it is stripped by the rule below). Bytes or sequences of bytes
81 * in the original byte stream that could not be converted to Unicode characters must be
82 * converted to U+FFFD REPLACEMENT CHARACTER code points.
83 */
84 // mb_convert_encoding is chosen over iconv because of a bug. The best
85 // details for the bug are on http://us1.php.net/manual/en/function.iconv.php#108643
86 // which contains links to the actual but reports as well as work around
87 // details.
88 if (\function_exists('mb_convert_encoding')) {
89 // mb library has the following behaviors:
90 // - UTF-16 surrogates result in false.
91 // - Overlongs and outside Plane 16 result in empty strings.
92 // Before we run mb_convert_encoding we need to tell it what to do with
93 // characters it does not know. This could be different than the parent
94 // application executing this library so we store the value, change it
95 // to our needs, and then change it back when we are done. This feels
96 // a little excessive and it would be great if there was a better way.
97 $save = \mb_substitute_character();
98 \mb_substitute_character('none');
99 $data = \mb_convert_encoding($data, 'UTF-8', $encoding);
100 \mb_substitute_character($save);
101 } elseif (\function_exists('iconv') && 'auto' !== $encoding) {
102 // fprintf(STDOUT, "iconv found\n");
103 // iconv has the following behaviors:
104 // - Overlong representations are ignored.
105 // - Beyond Plane 16 is replaced with a lower char.
106 // - Incomplete sequences generate a warning.
107 $data = @\iconv($encoding, 'UTF-8//IGNORE', $data);
108 } else {
109 throw new Exception('Not implemented, please install mbstring or iconv');
110 }
111 /*
112 * One leading U+FEFF BYTE ORDER MARK character must be ignored if any are present.
113 */
114 if ("" === \substr($data, 0, 3)) {
115 $data = \substr($data, 3);
116 }
117 return $data;
118 }
119 /**
120 * Checks for Unicode code points that are not valid in a document.
121 *
122 * @param string $data A string to analyze
123 *
124 * @return array An array of (string) error messages produced by the scanning
125 */
126 public static function checkForIllegalCodepoints($data)
127 {
128 // Vestigal error handling.
129 $errors = array();
130 /*
131 * All U+0000 null characters in the input must be replaced by U+FFFD REPLACEMENT CHARACTERs.
132 * Any occurrences of such characters is a parse error.
133 */
134 for ($i = 0, $count = \substr_count($data, "\x00"); $i < $count; ++$i) {
135 $errors[] = 'null-character';
136 }
137 /*
138 * Any occurrences of any characters in the ranges U+0001 to U+0008, U+000B, U+000E to U+001F, U+007F
139 * to U+009F, U+D800 to U+DFFF , U+FDD0 to U+FDEF, and characters U+FFFE, U+FFFF, U+1FFFE, U+1FFFF,
140 * U+2FFFE, U+2FFFF, U+3FFFE, U+3FFFF, U+4FFFE, U+4FFFF, U+5FFFE, U+5FFFF, U+6FFFE, U+6FFFF, U+7FFFE,
141 * U+7FFFF, U+8FFFE, U+8FFFF, U+9FFFE, U+9FFFF, U+AFFFE, U+AFFFF, U+BFFFE, U+BFFFF, U+CFFFE, U+CFFFF,
142 * U+DFFFE, U+DFFFF, U+EFFFE, U+EFFFF, U+FFFFE, U+FFFFF, U+10FFFE, and U+10FFFF are parse errors.
143 * (These are all control characters or permanently undefined Unicode characters.)
144 */
145 // Check PCRE is loaded.
146 $count = \preg_match_all('/(?:
147 [\\x01-\\x08\\x0B\\x0E-\\x1F\\x7F] # U+0001 to U+0008, U+000B, U+000E to U+001F and U+007F
148 |
149 \\xC2[\\x80-\\x9F] # U+0080 to U+009F
150 |
151 \\xED(?:\\xA0[\\x80-\\xFF]|[\\xA1-\\xBE][\\x00-\\xFF]|\\xBF[\\x00-\\xBF]) # U+D800 to U+DFFFF
152 |
153 \\xEF\\xB7[\\x90-\\xAF] # U+FDD0 to U+FDEF
154 |
155 \\xEF\\xBF[\\xBE\\xBF] # U+FFFE and U+FFFF
156 |
157 [\\xF0-\\xF4][\\x8F-\\xBF]\\xBF[\\xBE\\xBF] # U+nFFFE and U+nFFFF (1 <= n <= 10_{16})
158 )/x', $data, $matches);
159 for ($i = 0; $i < $count; ++$i) {
160 $errors[] = 'invalid-codepoint';
161 }
162 return $errors;
163 }
164 }
165