e136a8971b7a57d47af40efb9e408841a2a13f9a
[yaffs-website] / web / core / lib / Drupal / Component / Utility / Unicode.php
1 <?php
2
3 namespace Drupal\Component\Utility;
4
5 /**
6  * Provides Unicode-related conversions and operations.
7  *
8  * @ingroup utility
9  */
10 class Unicode {
11
12   /**
13    * Matches Unicode characters that are word boundaries.
14    *
15    * Characters with the following General_category (gc) property values are used
16    * as word boundaries. While this does not fully conform to the Word Boundaries
17    * algorithm described in http://unicode.org/reports/tr29, as PCRE does not
18    * contain the Word_Break property table, this simpler algorithm has to do.
19    * - Cc, Cf, Cn, Co, Cs: Other.
20    * - Pc, Pd, Pe, Pf, Pi, Po, Ps: Punctuation.
21    * - Sc, Sk, Sm, So: Symbols.
22    * - Zl, Zp, Zs: Separators.
23    *
24    * Non-boundary characters include the following General_category (gc) property
25    * values:
26    * - Ll, Lm, Lo, Lt, Lu: Letters.
27    * - Mc, Me, Mn: Combining Marks.
28    * - Nd, Nl, No: Numbers.
29    *
30    * Note that the PCRE property matcher is not used because we wanted to be
31    * compatible with Unicode 5.2.0 regardless of the PCRE version used (and any
32    * bugs in PCRE property tables).
33    *
34    * @see http://unicode.org/glossary
35    */
36   const PREG_CLASS_WORD_BOUNDARY = <<<'EOD'
37 \x{0}-\x{2F}\x{3A}-\x{40}\x{5B}-\x{60}\x{7B}-\x{A9}\x{AB}-\x{B1}\x{B4}
38 \x{B6}-\x{B8}\x{BB}\x{BF}\x{D7}\x{F7}\x{2C2}-\x{2C5}\x{2D2}-\x{2DF}
39 \x{2E5}-\x{2EB}\x{2ED}\x{2EF}-\x{2FF}\x{375}\x{37E}-\x{385}\x{387}\x{3F6}
40 \x{482}\x{55A}-\x{55F}\x{589}-\x{58A}\x{5BE}\x{5C0}\x{5C3}\x{5C6}
41 \x{5F3}-\x{60F}\x{61B}-\x{61F}\x{66A}-\x{66D}\x{6D4}\x{6DD}\x{6E9}
42 \x{6FD}-\x{6FE}\x{700}-\x{70F}\x{7F6}-\x{7F9}\x{830}-\x{83E}
43 \x{964}-\x{965}\x{970}\x{9F2}-\x{9F3}\x{9FA}-\x{9FB}\x{AF1}\x{B70}
44 \x{BF3}-\x{BFA}\x{C7F}\x{CF1}-\x{CF2}\x{D79}\x{DF4}\x{E3F}\x{E4F}
45 \x{E5A}-\x{E5B}\x{F01}-\x{F17}\x{F1A}-\x{F1F}\x{F34}\x{F36}\x{F38}
46 \x{F3A}-\x{F3D}\x{F85}\x{FBE}-\x{FC5}\x{FC7}-\x{FD8}\x{104A}-\x{104F}
47 \x{109E}-\x{109F}\x{10FB}\x{1360}-\x{1368}\x{1390}-\x{1399}\x{1400}
48 \x{166D}-\x{166E}\x{1680}\x{169B}-\x{169C}\x{16EB}-\x{16ED}
49 \x{1735}-\x{1736}\x{17B4}-\x{17B5}\x{17D4}-\x{17D6}\x{17D8}-\x{17DB}
50 \x{1800}-\x{180A}\x{180E}\x{1940}-\x{1945}\x{19DE}-\x{19FF}
51 \x{1A1E}-\x{1A1F}\x{1AA0}-\x{1AA6}\x{1AA8}-\x{1AAD}\x{1B5A}-\x{1B6A}
52 \x{1B74}-\x{1B7C}\x{1C3B}-\x{1C3F}\x{1C7E}-\x{1C7F}\x{1CD3}\x{1FBD}
53 \x{1FBF}-\x{1FC1}\x{1FCD}-\x{1FCF}\x{1FDD}-\x{1FDF}\x{1FED}-\x{1FEF}
54 \x{1FFD}-\x{206F}\x{207A}-\x{207E}\x{208A}-\x{208E}\x{20A0}-\x{20B8}
55 \x{2100}-\x{2101}\x{2103}-\x{2106}\x{2108}-\x{2109}\x{2114}
56 \x{2116}-\x{2118}\x{211E}-\x{2123}\x{2125}\x{2127}\x{2129}\x{212E}
57 \x{213A}-\x{213B}\x{2140}-\x{2144}\x{214A}-\x{214D}\x{214F}
58 \x{2190}-\x{244A}\x{249C}-\x{24E9}\x{2500}-\x{2775}\x{2794}-\x{2B59}
59 \x{2CE5}-\x{2CEA}\x{2CF9}-\x{2CFC}\x{2CFE}-\x{2CFF}\x{2E00}-\x{2E2E}
60 \x{2E30}-\x{3004}\x{3008}-\x{3020}\x{3030}\x{3036}-\x{3037}
61 \x{303D}-\x{303F}\x{309B}-\x{309C}\x{30A0}\x{30FB}\x{3190}-\x{3191}
62 \x{3196}-\x{319F}\x{31C0}-\x{31E3}\x{3200}-\x{321E}\x{322A}-\x{3250}
63 \x{3260}-\x{327F}\x{328A}-\x{32B0}\x{32C0}-\x{33FF}\x{4DC0}-\x{4DFF}
64 \x{A490}-\x{A4C6}\x{A4FE}-\x{A4FF}\x{A60D}-\x{A60F}\x{A673}\x{A67E}
65 \x{A6F2}-\x{A716}\x{A720}-\x{A721}\x{A789}-\x{A78A}\x{A828}-\x{A82B}
66 \x{A836}-\x{A839}\x{A874}-\x{A877}\x{A8CE}-\x{A8CF}\x{A8F8}-\x{A8FA}
67 \x{A92E}-\x{A92F}\x{A95F}\x{A9C1}-\x{A9CD}\x{A9DE}-\x{A9DF}
68 \x{AA5C}-\x{AA5F}\x{AA77}-\x{AA79}\x{AADE}-\x{AADF}\x{ABEB}
69 \x{E000}-\x{F8FF}\x{FB29}\x{FD3E}-\x{FD3F}\x{FDFC}-\x{FDFD}
70 \x{FE10}-\x{FE19}\x{FE30}-\x{FE6B}\x{FEFF}-\x{FF0F}\x{FF1A}-\x{FF20}
71 \x{FF3B}-\x{FF40}\x{FF5B}-\x{FF65}\x{FFE0}-\x{FFFD}
72 EOD;
73
74   /**
75    * Indicates that standard PHP (emulated) unicode support is being used.
76    */
77   const STATUS_SINGLEBYTE = 0;
78
79   /**
80    * Indicates that full unicode support with the PHP mbstring extension is
81    * being used.
82    */
83   const STATUS_MULTIBYTE = 1;
84
85   /**
86    * Indicates an error during check for PHP unicode support.
87    */
88   const STATUS_ERROR = -1;
89
90   /**
91    * Holds the multibyte capabilities of the current environment.
92    *
93    * @var int
94    */
95   protected static $status = 0;
96
97   /**
98    * Gets the current status of unicode/multibyte support on this environment.
99    *
100    * @return int
101    *   The status of multibyte support. It can be one of:
102    *   - \Drupal\Component\Utility\Unicode::STATUS_MULTIBYTE
103    *     Full unicode support using an extension.
104    *   - \Drupal\Component\Utility\Unicode::STATUS_SINGLEBYTE
105    *     Standard PHP (emulated) unicode support.
106    *   - \Drupal\Component\Utility\Unicode::STATUS_ERROR
107    *     An error occurred. No unicode support.
108    */
109   public static function getStatus() {
110     return static::$status;
111   }
112
113   /**
114    * Sets the value for multibyte support status for the current environment.
115    *
116    * The following status keys are supported:
117    *   - \Drupal\Component\Utility\Unicode::STATUS_MULTIBYTE
118    *     Full unicode support using an extension.
119    *   - \Drupal\Component\Utility\Unicode::STATUS_SINGLEBYTE
120    *     Standard PHP (emulated) unicode support.
121    *   - \Drupal\Component\Utility\Unicode::STATUS_ERROR
122    *     An error occurred. No unicode support.
123    *
124    * @param int $status
125    *   The new status of multibyte support.
126    */
127   public static function setStatus($status) {
128     if (!in_array($status, [static::STATUS_SINGLEBYTE, static::STATUS_MULTIBYTE, static::STATUS_ERROR])) {
129       throw new \InvalidArgumentException('Invalid status value for unicode support.');
130     }
131     static::$status = $status;
132   }
133
134   /**
135    * Checks for Unicode support in PHP and sets the proper settings if possible.
136    *
137    * Because of the need to be able to handle text in various encodings, we do
138    * not support mbstring function overloading. HTTP input/output conversion
139    * must be disabled for similar reasons.
140    *
141    * @return string
142    *   A string identifier of a failed multibyte extension check, if any.
143    *   Otherwise, an empty string.
144    */
145   public static function check() {
146     // Check for mbstring extension.
147     if (!function_exists('mb_strlen')) {
148       static::$status = static::STATUS_SINGLEBYTE;
149       return 'mb_strlen';
150     }
151
152     // Check mbstring configuration.
153     if (ini_get('mbstring.func_overload') != 0) {
154       static::$status = static::STATUS_ERROR;
155       return 'mbstring.func_overload';
156     }
157     if (ini_get('mbstring.encoding_translation') != 0) {
158       static::$status = static::STATUS_ERROR;
159       return 'mbstring.encoding_translation';
160     }
161     // mbstring.http_input and mbstring.http_output are deprecated and empty by
162     // default in PHP 5.6.
163     if (version_compare(PHP_VERSION, '5.6.0') == -1) {
164       if (ini_get('mbstring.http_input') != 'pass') {
165         static::$status = static::STATUS_ERROR;
166         return 'mbstring.http_input';
167       }
168       if (ini_get('mbstring.http_output') != 'pass') {
169         static::$status = static::STATUS_ERROR;
170         return 'mbstring.http_output';
171       }
172     }
173
174     // Set appropriate configuration.
175     mb_internal_encoding('utf-8');
176     mb_language('uni');
177     static::$status = static::STATUS_MULTIBYTE;
178     return '';
179   }
180
181   /**
182    * Decodes UTF byte-order mark (BOM) into the encoding's name.
183    *
184    * @param string $data
185    *   The data possibly containing a BOM. This can be the entire contents of
186    *   a file, or just a fragment containing at least the first five bytes.
187    *
188    * @return string|bool
189    *   The name of the encoding, or FALSE if no byte order mark was present.
190    */
191   public static function encodingFromBOM($data) {
192     static $bomMap = [
193       "\xEF\xBB\xBF" => 'UTF-8',
194       "\xFE\xFF" => 'UTF-16BE',
195       "\xFF\xFE" => 'UTF-16LE',
196       "\x00\x00\xFE\xFF" => 'UTF-32BE',
197       "\xFF\xFE\x00\x00" => 'UTF-32LE',
198       "\x2B\x2F\x76\x38" => 'UTF-7',
199       "\x2B\x2F\x76\x39" => 'UTF-7',
200       "\x2B\x2F\x76\x2B" => 'UTF-7',
201       "\x2B\x2F\x76\x2F" => 'UTF-7',
202       "\x2B\x2F\x76\x38\x2D" => 'UTF-7',
203     ];
204
205     foreach ($bomMap as $bom => $encoding) {
206       if (strpos($data, $bom) === 0) {
207         return $encoding;
208       }
209     }
210     return FALSE;
211   }
212
213   /**
214    * Converts data to UTF-8.
215    *
216    * Requires the iconv, GNU recode or mbstring PHP extension.
217    *
218    * @param string $data
219    *   The data to be converted.
220    * @param string $encoding
221    *   The encoding that the data is in.
222    *
223    * @return string|bool
224    *   Converted data or FALSE.
225    */
226   public static function convertToUtf8($data, $encoding) {
227     if (function_exists('iconv')) {
228       return @iconv($encoding, 'utf-8', $data);
229     }
230     elseif (function_exists('mb_convert_encoding')) {
231       return @mb_convert_encoding($data, 'utf-8', $encoding);
232     }
233     elseif (function_exists('recode_string')) {
234       return @recode_string($encoding . '..utf-8', $data);
235     }
236     // Cannot convert.
237     return FALSE;
238   }
239
240   /**
241    * Truncates a UTF-8-encoded string safely to a number of bytes.
242    *
243    * If the end position is in the middle of a UTF-8 sequence, it scans backwards
244    * until the beginning of the byte sequence.
245    *
246    * Use this function whenever you want to chop off a string at an unsure
247    * location. On the other hand, if you're sure that you're splitting on a
248    * character boundary (e.g. after using strpos() or similar), you can safely
249    * use substr() instead.
250    *
251    * @param string $string
252    *   The string to truncate.
253    * @param int $len
254    *   An upper limit on the returned string length.
255    *
256    * @return string
257    *   The truncated string.
258    */
259   public static function truncateBytes($string, $len) {
260     if (strlen($string) <= $len) {
261       return $string;
262     }
263     if ((ord($string[$len]) < 0x80) || (ord($string[$len]) >= 0xC0)) {
264       return substr($string, 0, $len);
265     }
266     // Scan backwards to beginning of the byte sequence.
267     // @todo Make the code more readable in https://www.drupal.org/node/2911497.
268     while (--$len >= 0 && ord($string[$len]) >= 0x80 && ord($string[$len]) < 0xC0) {
269     }
270
271     return substr($string, 0, $len);
272   }
273
274   /**
275    * Counts the number of characters in a UTF-8 string.
276    *
277    * This is less than or equal to the byte count.
278    *
279    * @param string $text
280    *   The string to run the operation on.
281    *
282    * @return int
283    *   The length of the string.
284    */
285   public static function strlen($text) {
286     if (static::getStatus() == static::STATUS_MULTIBYTE) {
287       return mb_strlen($text);
288     }
289     else {
290       // Do not count UTF-8 continuation bytes.
291       return strlen(preg_replace("/[\x80-\xBF]/", '', $text));
292     }
293   }
294
295   /**
296    * Converts a UTF-8 string to uppercase.
297    *
298    * @param string $text
299    *   The string to run the operation on.
300    *
301    * @return string
302    *   The string in uppercase.
303    */
304   public static function strtoupper($text) {
305     if (static::getStatus() == static::STATUS_MULTIBYTE) {
306       return mb_strtoupper($text);
307     }
308     else {
309       // Use C-locale for ASCII-only uppercase.
310       $text = strtoupper($text);
311       // Case flip Latin-1 accented letters.
312       $text = preg_replace_callback('/\xC3[\xA0-\xB6\xB8-\xBE]/', '\Drupal\Component\Utility\Unicode::caseFlip', $text);
313       return $text;
314     }
315   }
316
317   /**
318    * Converts a UTF-8 string to lowercase.
319    *
320    * @param string $text
321    *   The string to run the operation on.
322    *
323    * @return string
324    *   The string in lowercase.
325    */
326   public static function strtolower($text) {
327     if (static::getStatus() == static::STATUS_MULTIBYTE) {
328       return mb_strtolower($text);
329     }
330     else {
331       // Use C-locale for ASCII-only lowercase.
332       $text = strtolower($text);
333       // Case flip Latin-1 accented letters.
334       $text = preg_replace_callback('/\xC3[\x80-\x96\x98-\x9E]/', '\Drupal\Component\Utility\Unicode::caseFlip', $text);
335       return $text;
336     }
337   }
338
339   /**
340    * Capitalizes the first character of a UTF-8 string.
341    *
342    * @param string $text
343    *   The string to convert.
344    *
345    * @return string
346    *   The string with the first character as uppercase.
347    */
348   public static function ucfirst($text) {
349     return static::strtoupper(static::substr($text, 0, 1)) . static::substr($text, 1);
350   }
351
352   /**
353    * Converts the first character of a UTF-8 string to lowercase.
354    *
355    * @param string $text
356    *   The string that will be converted.
357    *
358    * @return string
359    *   The string with the first character as lowercase.
360    *
361    * @ingroup php_wrappers
362    */
363   public static function lcfirst($text) {
364     // Note: no mbstring equivalent!
365     return static::strtolower(static::substr($text, 0, 1)) . static::substr($text, 1);
366   }
367
368   /**
369    * Capitalizes the first character of each word in a UTF-8 string.
370    *
371    * @param string $text
372    *   The text that will be converted.
373    *
374    * @return string
375    *   The input $text with each word capitalized.
376    *
377    * @ingroup php_wrappers
378    */
379   public static function ucwords($text) {
380     $regex = '/(^|[' . static::PREG_CLASS_WORD_BOUNDARY . '])([^' . static::PREG_CLASS_WORD_BOUNDARY . '])/u';
381     return preg_replace_callback($regex, function (array $matches) {
382       return $matches[1] . Unicode::strtoupper($matches[2]);
383     }, $text);
384   }
385
386   /**
387    * Cuts off a piece of a string based on character indices and counts.
388    *
389    * Follows the same behavior as PHP's own substr() function. Note that for
390    * cutting off a string at a known character/substring location, the usage of
391    * PHP's normal strpos/substr is safe and much faster.
392    *
393    * @param string $text
394    *   The input string.
395    * @param int $start
396    *   The position at which to start reading.
397    * @param int $length
398    *   The number of characters to read.
399    *
400    * @return string
401    *   The shortened string.
402    */
403   public static function substr($text, $start, $length = NULL) {
404     if (static::getStatus() == static::STATUS_MULTIBYTE) {
405       return $length === NULL ? mb_substr($text, $start) : mb_substr($text, $start, $length);
406     }
407     else {
408       $strlen = strlen($text);
409       // Find the starting byte offset.
410       $bytes = 0;
411       if ($start > 0) {
412         // Count all the characters except continuation bytes from the start
413         // until we have found $start characters or the end of the string.
414         $bytes = -1; $chars = -1;
415         while ($bytes < $strlen - 1 && $chars < $start) {
416           $bytes++;
417           $c = ord($text[$bytes]);
418           if ($c < 0x80 || $c >= 0xC0) {
419             $chars++;
420           }
421         }
422       }
423       elseif ($start < 0) {
424         // Count all the characters except continuation bytes from the end
425         // until we have found abs($start) characters.
426         $start = abs($start);
427         $bytes = $strlen; $chars = 0;
428         while ($bytes > 0 && $chars < $start) {
429           $bytes--;
430           $c = ord($text[$bytes]);
431           if ($c < 0x80 || $c >= 0xC0) {
432             $chars++;
433           }
434         }
435       }
436       $istart = $bytes;
437
438       // Find the ending byte offset.
439       if ($length === NULL) {
440         $iend = $strlen;
441       }
442       elseif ($length > 0) {
443         // Count all the characters except continuation bytes from the starting
444         // index until we have found $length characters or reached the end of
445         // the string, then backtrace one byte.
446         $iend = $istart - 1;
447         $chars = -1;
448         $last_real = FALSE;
449         while ($iend < $strlen - 1 && $chars < $length) {
450           $iend++;
451           $c = ord($text[$iend]);
452           $last_real = FALSE;
453           if ($c < 0x80 || $c >= 0xC0) {
454             $chars++;
455             $last_real = TRUE;
456           }
457         }
458         // Backtrace one byte if the last character we found was a real
459         // character and we don't need it.
460         if ($last_real && $chars >= $length) {
461           $iend--;
462         }
463       }
464       elseif ($length < 0) {
465         // Count all the characters except continuation bytes from the end
466         // until we have found abs($start) characters, then backtrace one byte.
467         $length = abs($length);
468         $iend = $strlen; $chars = 0;
469         while ($iend > 0 && $chars < $length) {
470           $iend--;
471           $c = ord($text[$iend]);
472           if ($c < 0x80 || $c >= 0xC0) {
473             $chars++;
474           }
475         }
476         // Backtrace one byte if we are not at the beginning of the string.
477         if ($iend > 0) {
478           $iend--;
479         }
480       }
481       else {
482         // $length == 0, return an empty string.
483         return '';
484       }
485
486       return substr($text, $istart, max(0, $iend - $istart + 1));
487     }
488   }
489
490   /**
491    * Truncates a UTF-8-encoded string safely to a number of characters.
492    *
493    * @param string $string
494    *   The string to truncate.
495    * @param int $max_length
496    *   An upper limit on the returned string length, including trailing ellipsis
497    *   if $add_ellipsis is TRUE.
498    * @param bool $wordsafe
499    *   If TRUE, attempt to truncate on a word boundary. Word boundaries are
500    *   spaces, punctuation, and Unicode characters used as word boundaries in
501    *   non-Latin languages; see Unicode::PREG_CLASS_WORD_BOUNDARY for more
502    *   information. If a word boundary cannot be found that would make the length
503    *   of the returned string fall within length guidelines (see parameters
504    *   $max_length and $min_wordsafe_length), word boundaries are ignored.
505    * @param bool $add_ellipsis
506    *   If TRUE, add '...' to the end of the truncated string (defaults to
507    *   FALSE). The string length will still fall within $max_length.
508    * @param int $min_wordsafe_length
509    *   If $wordsafe is TRUE, the minimum acceptable length for truncation (before
510    *   adding an ellipsis, if $add_ellipsis is TRUE). Has no effect if $wordsafe
511    *   is FALSE. This can be used to prevent having a very short resulting string
512    *   that will not be understandable. For instance, if you are truncating the
513    *   string "See myverylongurlexample.com for more information" to a word-safe
514    *   return length of 20, the only available word boundary within 20 characters
515    *   is after the word "See", which wouldn't leave a very informative string. If
516    *   you had set $min_wordsafe_length to 10, though, the function would realise
517    *   that "See" alone is too short, and would then just truncate ignoring word
518    *   boundaries, giving you "See myverylongurl..." (assuming you had set
519    *   $add_ellipses to TRUE).
520    *
521    * @return string
522    *   The truncated string.
523    */
524   public static function truncate($string, $max_length, $wordsafe = FALSE, $add_ellipsis = FALSE, $min_wordsafe_length = 1) {
525     $ellipsis = '';
526     $max_length = max($max_length, 0);
527     $min_wordsafe_length = max($min_wordsafe_length, 0);
528
529     if (static::strlen($string) <= $max_length) {
530       // No truncation needed, so don't add ellipsis, just return.
531       return $string;
532     }
533
534     if ($add_ellipsis) {
535       // Truncate ellipsis in case $max_length is small.
536       $ellipsis = static::substr('…', 0, $max_length);
537       $max_length -= static::strlen($ellipsis);
538       $max_length = max($max_length, 0);
539     }
540
541     if ($max_length <= $min_wordsafe_length) {
542       // Do not attempt word-safe if lengths are bad.
543       $wordsafe = FALSE;
544     }
545
546     if ($wordsafe) {
547       $matches = [];
548       // Find the last word boundary, if there is one within $min_wordsafe_length
549       // to $max_length characters. preg_match() is always greedy, so it will
550       // find the longest string possible.
551       $found = preg_match('/^(.{' . $min_wordsafe_length . ',' . $max_length . '})[' . Unicode::PREG_CLASS_WORD_BOUNDARY . ']/us', $string, $matches);
552       if ($found) {
553         $string = $matches[1];
554       }
555       else {
556         $string = static::substr($string, 0, $max_length);
557       }
558     }
559     else {
560       $string = static::substr($string, 0, $max_length);
561     }
562
563     if ($add_ellipsis) {
564       // If we're adding an ellipsis, remove any trailing periods.
565       $string = rtrim($string, '.');
566
567       $string .= $ellipsis;
568     }
569
570     return $string;
571   }
572
573   /**
574    * Compares UTF-8-encoded strings in a binary safe case-insensitive manner.
575    *
576    * @param string $str1
577    *   The first string.
578    * @param string $str2
579    *   The second string.
580    *
581    * @return int
582    *   Returns < 0 if $str1 is less than $str2; > 0 if $str1 is greater than
583    *   $str2, and 0 if they are equal.
584    */
585   public static function strcasecmp($str1, $str2) {
586     return strcmp(static::strtoupper($str1), static::strtoupper($str2));
587   }
588
589   /**
590    * Encodes MIME/HTTP headers that contain incorrectly encoded characters.
591    *
592    * For example, Unicode::mimeHeaderEncode('tést.txt') returns
593    * "=?UTF-8?B?dMOpc3QudHh0?=".
594    *
595    * See http://www.rfc-editor.org/rfc/rfc2047.txt for more information.
596    *
597    * Notes:
598    * - Only encode strings that contain non-ASCII characters.
599    * - We progressively cut-off a chunk with self::truncateBytes(). This ensures
600    *   each chunk starts and ends on a character boundary.
601    * - Using \n as the chunk separator may cause problems on some systems and
602    *   may have to be changed to \r\n or \r.
603    *
604    * @param string $string
605    *   The header to encode.
606    * @param bool $shorten
607    *   If TRUE, only return the first chunk of a multi-chunk encoded string.
608    *
609    * @return string
610    *   The mime-encoded header.
611    */
612   public static function mimeHeaderEncode($string, $shorten = FALSE) {
613     if (preg_match('/[^\x20-\x7E]/', $string)) {
614       // floor((75 - strlen("=?UTF-8?B??=")) * 0.75);
615       $chunk_size = 47;
616       $len = strlen($string);
617       $output = '';
618       while ($len > 0) {
619         $chunk = static::truncateBytes($string, $chunk_size);
620         $output .= ' =?UTF-8?B?' . base64_encode($chunk) . "?=\n";
621         if ($shorten) {
622           break;
623         }
624         $c = strlen($chunk);
625         $string = substr($string, $c);
626         $len -= $c;
627       }
628       return trim($output);
629     }
630     return $string;
631   }
632
633   /**
634    * Decodes MIME/HTTP encoded header values.
635    *
636    * @param string $header
637    *   The header to decode.
638    *
639    * @return string
640    *   The mime-decoded header.
641    */
642   public static function mimeHeaderDecode($header) {
643     $callback = function ($matches) {
644       $data = ($matches[2] == 'B') ? base64_decode($matches[3]) : str_replace('_', ' ', quoted_printable_decode($matches[3]));
645       if (strtolower($matches[1]) != 'utf-8') {
646         $data = static::convertToUtf8($data, $matches[1]);
647       }
648       return $data;
649     };
650     // First step: encoded chunks followed by other encoded chunks (need to collapse whitespace)
651     $header = preg_replace_callback('/=\?([^?]+)\?(Q|B)\?([^?]+|\?(?!=))\?=\s+(?==\?)/', $callback, $header);
652     // Second step: remaining chunks (do not collapse whitespace)
653     return preg_replace_callback('/=\?([^?]+)\?(Q|B)\?([^?]+|\?(?!=))\?=/', $callback, $header);
654   }
655
656   /**
657    * Flip U+C0-U+DE to U+E0-U+FD and back. Can be used as preg_replace callback.
658    *
659    * @param array $matches
660    *   An array of matches by preg_replace_callback().
661    *
662    * @return string
663    *   The flipped text.
664    */
665   public static function caseFlip($matches) {
666     return $matches[0][0] . chr(ord($matches[0][1]) ^ 32);
667   }
668
669   /**
670    * Checks whether a string is valid UTF-8.
671    *
672    * All functions designed to filter input should use drupal_validate_utf8
673    * to ensure they operate on valid UTF-8 strings to prevent bypass of the
674    * filter.
675    *
676    * When text containing an invalid UTF-8 lead byte (0xC0 - 0xFF) is presented
677    * as UTF-8 to Internet Explorer 6, the program may misinterpret subsequent
678    * bytes. When these subsequent bytes are HTML control characters such as
679    * quotes or angle brackets, parts of the text that were deemed safe by filters
680    * end up in locations that are potentially unsafe; An onerror attribute that
681    * is outside of a tag, and thus deemed safe by a filter, can be interpreted
682    * by the browser as if it were inside the tag.
683    *
684    * The function does not return FALSE for strings containing character codes
685    * above U+10FFFF, even though these are prohibited by RFC 3629.
686    *
687    * @param string $text
688    *   The text to check.
689    *
690    * @return bool
691    *   TRUE if the text is valid UTF-8, FALSE if not.
692    */
693   public static function validateUtf8($text) {
694     if (strlen($text) == 0) {
695       return TRUE;
696     }
697     // With the PCRE_UTF8 modifier 'u', preg_match() fails silently on strings
698     // containing invalid UTF-8 byte sequences. It does not reject character
699     // codes above U+10FFFF (represented by 4 or more octets), though.
700     return (preg_match('/^./us', $text) == 1);
701   }
702
703   /**
704    * Finds the position of the first occurrence of a string in another string.
705    *
706    * @param string $haystack
707    *   The string to search in.
708    * @param string $needle
709    *   The string to find in $haystack.
710    * @param int $offset
711    *   If specified, start the search at this number of characters from the
712    *   beginning (default 0).
713    *
714    * @return int|false
715    *   The position where $needle occurs in $haystack, always relative to the
716    *   beginning (independent of $offset), or FALSE if not found. Note that
717    *   a return value of 0 is not the same as FALSE.
718    */
719   public static function strpos($haystack, $needle, $offset = 0) {
720     if (static::getStatus() == static::STATUS_MULTIBYTE) {
721       return mb_strpos($haystack, $needle, $offset);
722     }
723     else {
724       // Remove Unicode continuation characters, to be compatible with
725       // Unicode::strlen() and Unicode::substr().
726       $haystack = preg_replace("/[\x80-\xBF]/", '', $haystack);
727       $needle = preg_replace("/[\x80-\xBF]/", '', $needle);
728       return strpos($haystack, $needle, $offset);
729     }
730   }
731
732 }