Unicode.php 23 KB

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