Security update for permissions_by_term
[yaffs-website] / vendor / behat / transliterator / src / Behat / Transliterator / Transliterator.php
1 <?php
2
3 namespace Behat\Transliterator;
4
5 /**
6  * This is the part taken from Doctrine 1.2.3
7  * Doctrine inflector has static methods for inflecting text.
8  *
9  * The methods in these classes are from several different sources collected
10  * across several different php projects and several different authors. The
11  * original author names and emails are not known
12  *
13  * Uses 3rd party libraries and functions:
14  *         http://sourceforge.net/projects/phputf8
15  *
16  * @license        http://www.opensource.org/licenses/lgpl-license.php LGPL
17  *
18  * @since          1.0
19  *
20  * @author         Konsta Vesterinen <kvesteri@cc.hut.fi>
21  * @author         Jonathan H. Wage <jonwage@gmail.com>
22  * @author         <hsivonen@iki.fi>
23  */
24 abstract class Transliterator
25 {
26     /**
27      * Checks whether a string has utf7 characters in it.
28      *
29      * By bmorel at ssi dot fr
30      *
31      * @param string $string
32      *
33      * @return bool
34      */
35     public static function seemsUtf8($string)
36     {
37         $stringLength = strlen($string);
38
39         for ($i = 0; $i < $stringLength; ++$i) {
40             if (ord($string[$i]) < 0x80) { // 0bbbbbbb
41                 continue;
42             }  elseif ((ord($string[$i]) & 0xE0) == 0xC0) { // 110bbbbb
43                 $n = 1;
44             } elseif ((ord($string[$i]) & 0xF0) == 0xE0) { //1110bbbb
45                 $n = 2;
46             } elseif ((ord($string[$i]) & 0xF8) == 0xF0) { // 11110bbb
47                 $n = 3;
48             } elseif ((ord($string[$i]) & 0xFC) == 0xF8) { // 111110bb
49                 $n = 4;
50             } elseif ((ord($string[$i]) & 0xFE) == 0xFC) { // 1111110b
51                 $n = 5;
52             } else {
53                 return false; // Does not match any model
54             }
55
56             for ($j = 0; $j < $n; ++$j) { // n bytes matching 10bbbbbb follow ?
57                 if (++$i === $stringLength || ((ord($string[$i]) & 0xC0) !== 0x80)) {
58                     return false;
59                 }
60             }
61         }
62
63         return true;
64     }
65
66     /**
67      * Replaces accentuated chars (and a few others) with their ASCII base char.
68      *
69      * @see Transliterator::utf8ToAscii for a full transliteration to ASCII
70      *
71      * @param string $string String to unaccent
72      *
73      * @return string Unaccented string
74      */
75     public static function unaccent($string)
76     {
77         if (!preg_match('/[\x80-\xff]/', $string)) {
78             return $string;
79         }
80
81         if (self::seemsUtf8($string)) {
82             $chars = array(
83                 // Decompositions for Latin-1 Supplement
84                 chr(195).chr(128) => 'A',
85                 chr(195).chr(129) => 'A',
86                 chr(195).chr(130) => 'A',
87                 chr(195).chr(131) => 'A',
88                 chr(195).chr(132) => 'A',
89                 chr(195).chr(133) => 'A',
90                 chr(195).chr(135) => 'C',
91                 chr(195).chr(136) => 'E',
92                 chr(195).chr(137) => 'E',
93                 chr(195).chr(138) => 'E',
94                 chr(195).chr(139) => 'E',
95                 chr(195).chr(140) => 'I',
96                 chr(195).chr(141) => 'I',
97                 chr(195).chr(142) => 'I',
98                 chr(195).chr(143) => 'I',
99                 chr(195).chr(145) => 'N',
100                 chr(195).chr(146) => 'O',
101                 chr(195).chr(147) => 'O',
102                 chr(195).chr(148) => 'O',
103                 chr(195).chr(149) => 'O',
104                 chr(195).chr(150) => 'O',
105                 chr(195).chr(153) => 'U',
106                 chr(195).chr(154) => 'U',
107                 chr(195).chr(155) => 'U',
108                 chr(195).chr(156) => 'U',
109                 chr(195).chr(157) => 'Y',
110                 chr(195).chr(159) => 's',
111                 chr(195).chr(160) => 'a',
112                 chr(195).chr(161) => 'a',
113                 chr(195).chr(162) => 'a',
114                 chr(195).chr(163) => 'a',
115                 chr(195).chr(164) => 'a',
116                 chr(195).chr(165) => 'a',
117                 chr(195).chr(167) => 'c',
118                 chr(195).chr(168) => 'e',
119                 chr(195).chr(169) => 'e',
120                 chr(195).chr(170) => 'e',
121                 chr(195).chr(171) => 'e',
122                 chr(195).chr(172) => 'i',
123                 chr(195).chr(173) => 'i',
124                 chr(195).chr(174) => 'i',
125                 chr(195).chr(175) => 'i',
126                 chr(195).chr(177) => 'n',
127                 chr(195).chr(178) => 'o',
128                 chr(195).chr(179) => 'o',
129                 chr(195).chr(180) => 'o',
130                 chr(195).chr(181) => 'o',
131                 chr(195).chr(182) => 'o',
132                 chr(195).chr(182) => 'o',
133                 chr(195).chr(185) => 'u',
134                 chr(195).chr(186) => 'u',
135                 chr(195).chr(187) => 'u',
136                 chr(195).chr(188) => 'u',
137                 chr(195).chr(189) => 'y',
138                 chr(195).chr(191) => 'y',
139                 // Decompositions for Latin Extended-A
140                 chr(196).chr(128) => 'A',
141                 chr(196).chr(129) => 'a',
142                 chr(196).chr(130) => 'A',
143                 chr(196).chr(131) => 'a',
144                 chr(196).chr(132) => 'A',
145                 chr(196).chr(133) => 'a',
146                 chr(196).chr(134) => 'C',
147                 chr(196).chr(135) => 'c',
148                 chr(196).chr(136) => 'C',
149                 chr(196).chr(137) => 'c',
150                 chr(196).chr(138) => 'C',
151                 chr(196).chr(139) => 'c',
152                 chr(196).chr(140) => 'C',
153                 chr(196).chr(141) => 'c',
154                 chr(196).chr(142) => 'D',
155                 chr(196).chr(143) => 'd',
156                 chr(196).chr(144) => 'D',
157                 chr(196).chr(145) => 'd',
158                 chr(196).chr(146) => 'E',
159                 chr(196).chr(147) => 'e',
160                 chr(196).chr(148) => 'E',
161                 chr(196).chr(149) => 'e',
162                 chr(196).chr(150) => 'E',
163                 chr(196).chr(151) => 'e',
164                 chr(196).chr(152) => 'E',
165                 chr(196).chr(153) => 'e',
166                 chr(196).chr(154) => 'E',
167                 chr(196).chr(155) => 'e',
168                 chr(196).chr(156) => 'G',
169                 chr(196).chr(157) => 'g',
170                 chr(196).chr(158) => 'G',
171                 chr(196).chr(159) => 'g',
172                 chr(196).chr(160) => 'G',
173                 chr(196).chr(161) => 'g',
174                 chr(196).chr(162) => 'G',
175                 chr(196).chr(163) => 'g',
176                 chr(196).chr(164) => 'H',
177                 chr(196).chr(165) => 'h',
178                 chr(196).chr(166) => 'H',
179                 chr(196).chr(167) => 'h',
180                 chr(196).chr(168) => 'I',
181                 chr(196).chr(169) => 'i',
182                 chr(196).chr(170) => 'I',
183                 chr(196).chr(171) => 'i',
184                 chr(196).chr(172) => 'I',
185                 chr(196).chr(173) => 'i',
186                 chr(196).chr(174) => 'I',
187                 chr(196).chr(175) => 'i',
188                 chr(196).chr(176) => 'I',
189                 chr(196).chr(177) => 'i',
190                 chr(196).chr(178) => 'IJ',
191                 chr(196).chr(179) => 'ij',
192                 chr(196).chr(180) => 'J',
193                 chr(196).chr(181) => 'j',
194                 chr(196).chr(182) => 'K',
195                 chr(196).chr(183) => 'k',
196                 chr(196).chr(184) => 'k',
197                 chr(196).chr(185) => 'L',
198                 chr(196).chr(186) => 'l',
199                 chr(196).chr(187) => 'L',
200                 chr(196).chr(188) => 'l',
201                 chr(196).chr(189) => 'L',
202                 chr(196).chr(190) => 'l',
203                 chr(196).chr(191) => 'L',
204                 chr(197).chr(128) => 'l',
205                 chr(197).chr(129) => 'L',
206                 chr(197).chr(130) => 'l',
207                 chr(197).chr(131) => 'N',
208                 chr(197).chr(132) => 'n',
209                 chr(197).chr(133) => 'N',
210                 chr(197).chr(134) => 'n',
211                 chr(197).chr(135) => 'N',
212                 chr(197).chr(136) => 'n',
213                 chr(197).chr(137) => 'N',
214                 chr(197).chr(138) => 'n',
215                 chr(197).chr(139) => 'N',
216                 chr(197).chr(140) => 'O',
217                 chr(197).chr(141) => 'o',
218                 chr(197).chr(142) => 'O',
219                 chr(197).chr(143) => 'o',
220                 chr(197).chr(144) => 'O',
221                 chr(197).chr(145) => 'o',
222                 chr(197).chr(146) => 'OE',
223                 chr(197).chr(147) => 'oe',
224                 chr(197).chr(148) => 'R',
225                 chr(197).chr(149) => 'r',
226                 chr(197).chr(150) => 'R',
227                 chr(197).chr(151) => 'r',
228                 chr(197).chr(152) => 'R',
229                 chr(197).chr(153) => 'r',
230                 chr(197).chr(154) => 'S',
231                 chr(197).chr(155) => 's',
232                 chr(197).chr(156) => 'S',
233                 chr(197).chr(157) => 's',
234                 chr(197).chr(158) => 'S',
235                 chr(197).chr(159) => 's',
236                 chr(197).chr(160) => 'S',
237                 chr(197).chr(161) => 's',
238                 chr(197).chr(162) => 'T',
239                 chr(197).chr(163) => 't',
240                 chr(197).chr(164) => 'T',
241                 chr(197).chr(165) => 't',
242                 chr(197).chr(166) => 'T',
243                 chr(197).chr(167) => 't',
244                 chr(197).chr(168) => 'U',
245                 chr(197).chr(169) => 'u',
246                 chr(197).chr(170) => 'U',
247                 chr(197).chr(171) => 'u',
248                 chr(197).chr(172) => 'U',
249                 chr(197).chr(173) => 'u',
250                 chr(197).chr(174) => 'U',
251                 chr(197).chr(175) => 'u',
252                 chr(197).chr(176) => 'U',
253                 chr(197).chr(177) => 'u',
254                 chr(197).chr(178) => 'U',
255                 chr(197).chr(179) => 'u',
256                 chr(197).chr(180) => 'W',
257                 chr(197).chr(181) => 'w',
258                 chr(197).chr(182) => 'Y',
259                 chr(197).chr(183) => 'y',
260                 chr(197).chr(184) => 'Y',
261                 chr(197).chr(185) => 'Z',
262                 chr(197).chr(186) => 'z',
263                 chr(197).chr(187) => 'Z',
264                 chr(197).chr(188) => 'z',
265                 chr(197).chr(189) => 'Z',
266                 chr(197).chr(190) => 'z',
267                 chr(197).chr(191) => 's',
268                 // Euro Sign
269                 chr(226).chr(130).chr(172) => 'E',
270                 // GBP (Pound) Sign
271                 chr(194).chr(163) => '',
272                 'Ä' => 'Ae',
273                 'ä' => 'ae',
274                 'Ãœ' => 'Ue',
275                 'ü' => 'ue',
276                 'Ö' => 'Oe',
277                 'ö' => 'oe',
278                 'ß' => 'ss',
279                 // Norwegian characters
280                 'Ã…' => 'Aa',
281                 'Æ' => 'Ae',
282                 'Ø' => 'O',
283                 'æ' => 'a',
284                 'ø' => 'o',
285                 'Ã¥' => 'aa',
286             );
287
288             $string = strtr($string, $chars);
289         } else {
290             $chars = array();
291             // Assume ISO-8859-1 if not UTF-8
292             $chars['in'] = chr(128).chr(131).chr(138).chr(142).chr(154).chr(158)
293                 .chr(159).chr(162).chr(165).chr(181).chr(192).chr(193).chr(194)
294                 .chr(195).chr(196).chr(197).chr(199).chr(200).chr(201).chr(202)
295                 .chr(203).chr(204).chr(205).chr(206).chr(207).chr(209).chr(210)
296                 .chr(211).chr(212).chr(213).chr(214).chr(216).chr(217).chr(218)
297                 .chr(219).chr(220).chr(221).chr(224).chr(225).chr(226).chr(227)
298                 .chr(228).chr(229).chr(231).chr(232).chr(233).chr(234).chr(235)
299                 .chr(236).chr(237).chr(238).chr(239).chr(241).chr(242).chr(243)
300                 .chr(244).chr(245).chr(246).chr(248).chr(249).chr(250).chr(251)
301                 .chr(252).chr(253).chr(255);
302
303             $chars['out'] = 'EfSZszYcYuAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy';
304
305             $string = strtr($string, $chars['in'], $chars['out']);
306
307             $doubleChars = array();
308             $doubleChars['in'] = array(
309                 chr(140),
310                 chr(156),
311                 chr(198),
312                 chr(208),
313                 chr(222),
314                 chr(223),
315                 chr(230),
316                 chr(240),
317                 chr(254),
318             );
319             $doubleChars['out'] = array('OE', 'oe', 'AE', 'DH', 'TH', 'ss', 'ae', 'dh', 'th');
320             $string = str_replace($doubleChars['in'], $doubleChars['out'], $string);
321         }
322
323         return $string;
324     }
325
326     /**
327      * Transliterates an UTF-8 string to ASCII.
328      *
329      * US-ASCII transliterations of Unicode text
330      * Ported Sean M. Burke's Text::Unidecode Perl module (He did all the hard work!)
331      * Warning: you should only pass this well formed UTF-8!
332      * Be aware it works by making a copy of the input string which it appends transliterated
333      * characters to - it uses a PHP output buffer to do this - it means, memory use will increase,
334      * requiring up to the same amount again as the input string.
335      *
336      * @see http://search.cpan.org/~sburke/Text-Unidecode-0.04/lib/Text/Unidecode.pm
337      *
338      * @author <hsivonen@iki.fi>
339      *
340      * @param string $str     UTF-8 string to convert
341      * @param string $unknown Character use if character unknown (default to ?)
342      *
343      * @return string US-ASCII string
344      */
345     public static function utf8ToAscii($str, $unknown = '?')
346     {
347         static $UTF8_TO_ASCII;
348
349         if (strlen($str) == 0) {
350             return '';
351         }
352
353         preg_match_all('/.{1}|[^\x00]{1,1}$/us', $str, $ar);
354         $chars = $ar[0];
355
356         foreach ($chars as $i => $c) {
357             if (ord($c{0}) >= 0 && ord($c{0}) <= 127) {
358                 continue;
359             } // ASCII - next please
360             if (ord($c{0}) >= 192 && ord($c{0}) <= 223) {
361                 $ord = (ord($c{0}) - 192) * 64 + (ord($c{1}) - 128);
362             }
363             if (ord($c{0}) >= 224 && ord($c{0}) <= 239) {
364                 $ord = (ord($c{0}) - 224) * 4096 + (ord($c{1}) - 128) * 64 + (ord($c{2}) - 128);
365             }
366             if (ord($c{0}) >= 240 && ord($c{0}) <= 247) {
367                 $ord = (ord($c{0}) - 240) * 262144 + (ord($c{1}) - 128) * 4096 + (ord($c{2}) - 128) * 64 + (ord($c{3}) - 128);
368             }
369             if (ord($c{0}) >= 248 && ord($c{0}) <= 251) {
370                 $ord = (ord($c{0}) - 248) * 16777216 + (ord($c{1}) - 128) * 262144 + (ord($c{2}) - 128) * 4096 + (ord($c{3}) - 128) * 64 + (ord($c{4}) - 128);
371             }
372             if (ord($c{0}) >= 252 && ord($c{0}) <= 253) {
373                 $ord = (ord($c{0}) - 252) * 1073741824 + (ord($c{1}) - 128) * 16777216 + (ord($c{2}) - 128) * 262144 + (ord($c{3}) - 128) * 4096 + (ord($c{4}) - 128) * 64 + (ord($c{5}) - 128);
374             }
375             if (ord($c{0}) >= 254 && ord($c{0}) <= 255) {
376                 $chars{$i} = $unknown;
377                 continue;
378             } //error
379
380             $bank = $ord >> 8;
381
382             if (!array_key_exists($bank, (array) $UTF8_TO_ASCII)) {
383                 $bankfile = __DIR__.'/data/'.sprintf('x%02x', $bank).'.php';
384                 if (file_exists($bankfile)) {
385                     include $bankfile;
386                 } else {
387                     $UTF8_TO_ASCII[$bank] = array();
388                 }
389             }
390
391             $newchar = $ord & 255;
392             if (array_key_exists($newchar, $UTF8_TO_ASCII[$bank])) {
393                 $chars{$i} = $UTF8_TO_ASCII[$bank][$newchar];
394             } else {
395                 $chars{$i} = $unknown;
396             }
397         }
398
399         return implode('', $chars);
400     }
401
402     /**
403      * Generates a slug of the text.
404      *
405      * Does not transliterate correctly eastern languages.
406      *
407      * @see Transliterator::unaccent for the transliteration logic
408      *
409      * @param string $text
410      * @param string $separator
411      *
412      * @return string
413      */
414     public static function urlize($text, $separator = '-')
415     {
416         $text = self::unaccent($text);
417
418         return self::postProcessText($text, $separator);
419     }
420
421     /**
422      * Generates a slug of the text after transliterating the UTF-8 string to ASCII.
423      *
424      * Uses transliteration tables to convert any kind of utf8 character.
425      *
426      * @param string $text
427      * @param string $separator
428      *
429      * @return string $text
430      */
431     public static function transliterate($text, $separator = '-')
432     {
433         if (preg_match('/[\x80-\xff]/', $text) && self::validUtf8($text)) {
434             $text = self::utf8ToAscii($text);
435         }
436
437         return self::postProcessText($text, $separator);
438     }
439
440     /**
441      * Tests a string as to whether it's valid UTF-8 and supported by the
442      * Unicode standard.
443      *
444      * Note: this function has been modified to simple return true or false
445      *
446      * @author <hsivonen@iki.fi>
447      *
448      * @param string $str UTF-8 encoded string
449      *
450      * @return bool
451      *
452      * @see    http://hsivonen.iki.fi/php-utf8/
453      */
454     public static function validUtf8($str)
455     {
456         $mState = 0; // cached expected number of octets after the current octet
457         // until the beginning of the next UTF8 character sequence
458         $mUcs4 = 0; // cached Unicode character
459         $mBytes = 1; // cached expected number of octets in the current sequence
460
461         $len = strlen($str);
462         for ($i = 0; $i < $len; ++$i) {
463             $in = ord($str{$i});
464             if ($mState == 0) {
465                 // When mState is zero we expect either a US-ASCII character or a
466                 // multi-octet sequence.
467                 if (0 == (0x80 & ($in))) {
468                     // US-ASCII, pass straight through.
469                     $mBytes = 1;
470                 } elseif (0xC0 == (0xE0 & ($in))) {
471                     // First octet of 2 octet sequence
472                     $mUcs4 = ($in);
473                     $mUcs4 = ($mUcs4 & 0x1F) << 6;
474                     $mState = 1;
475                     $mBytes = 2;
476                 } elseif (0xE0 == (0xF0 & ($in))) {
477                     // First octet of 3 octet sequence
478                     $mUcs4 = ($in);
479                     $mUcs4 = ($mUcs4 & 0x0F) << 12;
480                     $mState = 2;
481                     $mBytes = 3;
482                 } elseif (0xF0 == (0xF8 & ($in))) {
483                     // First octet of 4 octet sequence
484                     $mUcs4 = ($in);
485                     $mUcs4 = ($mUcs4 & 0x07) << 18;
486                     $mState = 3;
487                     $mBytes = 4;
488                 } elseif (0xF8 == (0xFC & ($in))) {
489                     /* First octet of 5 octet sequence.
490                     *
491                     * This is illegal because the encoded codepoint must be either
492                     * (a) not the shortest form or
493                     * (b) outside the Unicode range of 0-0x10FFFF.
494                     * Rather than trying to resynchronize, we will carry on until the end
495                     * of the sequence and let the later error handling code catch it.
496                     */
497                     $mUcs4 = ($in);
498                     $mUcs4 = ($mUcs4 & 0x03) << 24;
499                     $mState = 4;
500                     $mBytes = 5;
501                 } elseif (0xFC == (0xFE & ($in))) {
502                     // First octet of 6 octet sequence, see comments for 5 octet sequence.
503                     $mUcs4 = ($in);
504                     $mUcs4 = ($mUcs4 & 1) << 30;
505                     $mState = 5;
506                     $mBytes = 6;
507                 } else {
508                     /* Current octet is neither in the US-ASCII range nor a legal first
509                      * octet of a multi-octet sequence.
510                      */
511                     return false;
512                 }
513             } else {
514                 // When mState is non-zero, we expect a continuation of the multi-octet
515                 // sequence
516                 if (0x80 == (0xC0 & ($in))) {
517                     // Legal continuation.
518                     $shift = ($mState - 1) * 6;
519                     $tmp = $in;
520                     $tmp = ($tmp & 0x0000003F) << $shift;
521                     $mUcs4 |= $tmp;
522                     /*
523                      * End of the multi-octet sequence. mUcs4 now contains the final
524                      * Unicode codepoint to be output
525                      */
526                     if (0 == --$mState) {
527                         /*
528                         * Check for illegal sequences and codepoints.
529                         */
530                         // From Unicode 3.1, non-shortest form is illegal
531                         if (((2 == $mBytes) && ($mUcs4 < 0x0080)) ||
532                             ((3 == $mBytes) && ($mUcs4 < 0x0800)) ||
533                             ((4 == $mBytes) && ($mUcs4 < 0x10000)) ||
534                             (4 < $mBytes) ||
535                             // From Unicode 3.2, surrogate characters are illegal
536                             (($mUcs4 & 0xFFFFF800) == 0xD800) ||
537                             // Codepoints outside the Unicode range are illegal
538                             ($mUcs4 > 0x10FFFF)
539                         ) {
540                             return false;
541                         }
542                         //initialize UTF8 cache
543                         $mState = 0;
544                         $mUcs4 = 0;
545                         $mBytes = 1;
546                     }
547                 } else {
548                     /*
549                      *((0xC0 & (*in) != 0x80) && (mState != 0))
550                      * Incomplete multi-octet sequence.
551                      */
552                     return false;
553                 }
554             }
555         }
556
557         return true;
558     }
559
560     /**
561      * Cleans up the text and adds separator.
562      *
563      * @param string $text
564      * @param string $separator
565      *
566      * @return string
567      */
568     private static function postProcessText($text, $separator)
569     {
570         if (function_exists('mb_strtolower')) {
571             $text = mb_strtolower($text);
572         } else {
573             $text = strtolower($text);
574         }
575
576         // Remove apostrophes which are not used as quotes around a string
577         $text = preg_replace('/(\\w)\'(\\w)/', '${1}${2}', $text);
578
579         // Replace all none word characters with a space
580         $text = preg_replace('/\W/', ' ', $text);
581
582         // More stripping. Replace spaces with dashes
583         $text = strtolower(preg_replace('/[^A-Za-z0-9\/]+/', $separator,
584             preg_replace('/([a-z\d])([A-Z])/', '\1_\2',
585                 preg_replace('/([A-Z]+)([A-Z][a-z])/', '\1_\2',
586                     preg_replace('/::/', '/', $text)))));
587
588         return trim($text, $separator);
589     }
590 }