Unicode.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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 will be removed before 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. // mbstring.http_input and mbstring.http_output are deprecated and empty by
  155. // default in PHP 5.6.
  156. if (version_compare(PHP_VERSION, '5.6.0') == -1) {
  157. if (ini_get('mbstring.http_input') != 'pass') {
  158. return 'mbstring.http_input';
  159. }
  160. if (ini_get('mbstring.http_output') != 'pass') {
  161. return 'mbstring.http_output';
  162. }
  163. }
  164. return '';
  165. }
  166. /**
  167. * Decodes UTF byte-order mark (BOM) into the encoding's name.
  168. *
  169. * @param string $data
  170. * The data possibly containing a BOM. This can be the entire contents of
  171. * a file, or just a fragment containing at least the first five bytes.
  172. *
  173. * @return string|bool
  174. * The name of the encoding, or FALSE if no byte order mark was present.
  175. */
  176. public static function encodingFromBOM($data) {
  177. static $bomMap = [
  178. "\xEF\xBB\xBF" => 'UTF-8',
  179. "\xFE\xFF" => 'UTF-16BE',
  180. "\xFF\xFE" => 'UTF-16LE',
  181. "\x00\x00\xFE\xFF" => 'UTF-32BE',
  182. "\xFF\xFE\x00\x00" => 'UTF-32LE',
  183. "\x2B\x2F\x76\x38" => 'UTF-7',
  184. "\x2B\x2F\x76\x39" => 'UTF-7',
  185. "\x2B\x2F\x76\x2B" => 'UTF-7',
  186. "\x2B\x2F\x76\x2F" => 'UTF-7',
  187. "\x2B\x2F\x76\x38\x2D" => 'UTF-7',
  188. ];
  189. foreach ($bomMap as $bom => $encoding) {
  190. if (strpos($data, $bom) === 0) {
  191. return $encoding;
  192. }
  193. }
  194. return FALSE;
  195. }
  196. /**
  197. * Converts data to UTF-8.
  198. *
  199. * Requires the iconv, GNU recode or mbstring PHP extension.
  200. *
  201. * @param string $data
  202. * The data to be converted.
  203. * @param string $encoding
  204. * The encoding that the data is in.
  205. *
  206. * @return string|bool
  207. * Converted data or FALSE.
  208. */
  209. public static function convertToUtf8($data, $encoding) {
  210. return @iconv($encoding, 'utf-8', $data);
  211. }
  212. /**
  213. * Truncates a UTF-8-encoded string safely to a number of bytes.
  214. *
  215. * If the end position is in the middle of a UTF-8 sequence, it scans backwards
  216. * until the beginning of the byte sequence.
  217. *
  218. * Use this function whenever you want to chop off a string at an unsure
  219. * location. On the other hand, if you're sure that you're splitting on a
  220. * character boundary (e.g. after using strpos() or similar), you can safely
  221. * use substr() instead.
  222. *
  223. * @param string $string
  224. * The string to truncate.
  225. * @param int $len
  226. * An upper limit on the returned string length.
  227. *
  228. * @return string
  229. * The truncated string.
  230. */
  231. public static function truncateBytes($string, $len) {
  232. if (strlen($string) <= $len) {
  233. return $string;
  234. }
  235. if ((ord($string[$len]) < 0x80) || (ord($string[$len]) >= 0xC0)) {
  236. return substr($string, 0, $len);
  237. }
  238. // Scan backwards to beginning of the byte sequence.
  239. // @todo Make the code more readable in https://www.drupal.org/node/2911497.
  240. while (--$len >= 0 && ord($string[$len]) >= 0x80 && ord($string[$len]) < 0xC0) {
  241. }
  242. return substr($string, 0, $len);
  243. }
  244. /**
  245. * Counts the number of characters in a UTF-8 string.
  246. *
  247. * This is less than or equal to the byte count.
  248. *
  249. * @param string $text
  250. * The string to run the operation on.
  251. *
  252. * @return int
  253. * The length of the string.
  254. *
  255. * @deprecated in Drupal 8.6.0, will be removed before Drupal 9.0.0. Use
  256. * mb_strlen() instead.
  257. *
  258. * @see https://www.drupal.org/node/2850048
  259. */
  260. public static function strlen($text) {
  261. @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);
  262. return mb_strlen($text);
  263. }
  264. /**
  265. * Converts a UTF-8 string to uppercase.
  266. *
  267. * @param string $text
  268. * The string to run the operation on.
  269. *
  270. * @return string
  271. * The string in uppercase.
  272. *
  273. * @deprecated in Drupal 8.6.0, will be removed before Drupal 9.0.0. Use
  274. * mb_strtoupper() instead.
  275. *
  276. * @see https://www.drupal.org/node/2850048
  277. */
  278. public static function strtoupper($text) {
  279. @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);
  280. return mb_strtoupper($text);
  281. }
  282. /**
  283. * Converts a UTF-8 string to lowercase.
  284. *
  285. * @param string $text
  286. * The string to run the operation on.
  287. *
  288. * @return string
  289. * The string in lowercase.
  290. *
  291. * @deprecated in Drupal 8.6.0, will be removed before Drupal 9.0.0. Use
  292. * mb_strtolower() instead.
  293. *
  294. * @see https://www.drupal.org/node/2850048
  295. */
  296. public static function strtolower($text) {
  297. @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);
  298. return mb_strtolower($text);
  299. }
  300. /**
  301. * Capitalizes the first character of a UTF-8 string.
  302. *
  303. * @param string $text
  304. * The string to convert.
  305. *
  306. * @return string
  307. * The string with the first character as uppercase.
  308. */
  309. public static function ucfirst($text) {
  310. return mb_strtoupper(mb_substr($text, 0, 1)) . mb_substr($text, 1);
  311. }
  312. /**
  313. * Converts the first character of a UTF-8 string to lowercase.
  314. *
  315. * @param string $text
  316. * The string that will be converted.
  317. *
  318. * @return string
  319. * The string with the first character as lowercase.
  320. *
  321. * @ingroup php_wrappers
  322. */
  323. public static function lcfirst($text) {
  324. // Note: no mbstring equivalent!
  325. return mb_strtolower(mb_substr($text, 0, 1)) . mb_substr($text, 1);
  326. }
  327. /**
  328. * Capitalizes the first character of each word in a UTF-8 string.
  329. *
  330. * @param string $text
  331. * The text that will be converted.
  332. *
  333. * @return string
  334. * The input $text with each word capitalized.
  335. *
  336. * @ingroup php_wrappers
  337. */
  338. public static function ucwords($text) {
  339. $regex = '/(^|[' . static::PREG_CLASS_WORD_BOUNDARY . '])([^' . static::PREG_CLASS_WORD_BOUNDARY . '])/u';
  340. return preg_replace_callback($regex, function (array $matches) {
  341. return $matches[1] . mb_strtoupper($matches[2]);
  342. }, $text);
  343. }
  344. /**
  345. * Cuts off a piece of a string based on character indices and counts.
  346. *
  347. * Follows the same behavior as PHP's own substr() function. Note that for
  348. * cutting off a string at a known character/substring location, the usage of
  349. * PHP's normal strpos/substr is safe and much faster.
  350. *
  351. * @param string $text
  352. * The input string.
  353. * @param int $start
  354. * The position at which to start reading.
  355. * @param int $length
  356. * The number of characters to read.
  357. *
  358. * @return string
  359. * The shortened string.
  360. *
  361. * @deprecated in Drupal 8.6.0, will be removed before Drupal 9.0.0. Use
  362. * mb_substr() instead.
  363. *
  364. * @see https://www.drupal.org/node/2850048
  365. */
  366. public static function substr($text, $start, $length = NULL) {
  367. @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);
  368. return mb_substr($text, $start, $length);
  369. }
  370. /**
  371. * Truncates a UTF-8-encoded string safely to a number of characters.
  372. *
  373. * @param string $string
  374. * The string to truncate.
  375. * @param int $max_length
  376. * An upper limit on the returned string length, including trailing ellipsis
  377. * if $add_ellipsis is TRUE.
  378. * @param bool $wordsafe
  379. * If TRUE, attempt to truncate on a word boundary. Word boundaries are
  380. * spaces, punctuation, and Unicode characters used as word boundaries in
  381. * non-Latin languages; see Unicode::PREG_CLASS_WORD_BOUNDARY for more
  382. * information. If a word boundary cannot be found that would make the length
  383. * of the returned string fall within length guidelines (see parameters
  384. * $max_length and $min_wordsafe_length), word boundaries are ignored.
  385. * @param bool $add_ellipsis
  386. * If TRUE, add '...' to the end of the truncated string (defaults to
  387. * FALSE). The string length will still fall within $max_length.
  388. * @param int $min_wordsafe_length
  389. * If $wordsafe is TRUE, the minimum acceptable length for truncation (before
  390. * adding an ellipsis, if $add_ellipsis is TRUE). Has no effect if $wordsafe
  391. * is FALSE. This can be used to prevent having a very short resulting string
  392. * that will not be understandable. For instance, if you are truncating the
  393. * string "See myverylongurlexample.com for more information" to a word-safe
  394. * return length of 20, the only available word boundary within 20 characters
  395. * is after the word "See", which wouldn't leave a very informative string. If
  396. * you had set $min_wordsafe_length to 10, though, the function would realise
  397. * that "See" alone is too short, and would then just truncate ignoring word
  398. * boundaries, giving you "See myverylongurl..." (assuming you had set
  399. * $add_ellipses to TRUE).
  400. *
  401. * @return string
  402. * The truncated string.
  403. */
  404. public static function truncate($string, $max_length, $wordsafe = FALSE, $add_ellipsis = FALSE, $min_wordsafe_length = 1) {
  405. $ellipsis = '';
  406. $max_length = max($max_length, 0);
  407. $min_wordsafe_length = max($min_wordsafe_length, 0);
  408. if (mb_strlen($string) <= $max_length) {
  409. // No truncation needed, so don't add ellipsis, just return.
  410. return $string;
  411. }
  412. if ($add_ellipsis) {
  413. // Truncate ellipsis in case $max_length is small.
  414. $ellipsis = mb_substr('…', 0, $max_length);
  415. $max_length -= mb_strlen($ellipsis);
  416. $max_length = max($max_length, 0);
  417. }
  418. if ($max_length <= $min_wordsafe_length) {
  419. // Do not attempt word-safe if lengths are bad.
  420. $wordsafe = FALSE;
  421. }
  422. if ($wordsafe) {
  423. $matches = [];
  424. // Find the last word boundary, if there is one within $min_wordsafe_length
  425. // to $max_length characters. preg_match() is always greedy, so it will
  426. // find the longest string possible.
  427. $found = preg_match('/^(.{' . $min_wordsafe_length . ',' . $max_length . '})[' . Unicode::PREG_CLASS_WORD_BOUNDARY . ']/us', $string, $matches);
  428. if ($found) {
  429. $string = $matches[1];
  430. }
  431. else {
  432. $string = mb_substr($string, 0, $max_length);
  433. }
  434. }
  435. else {
  436. $string = mb_substr($string, 0, $max_length);
  437. }
  438. if ($add_ellipsis) {
  439. // If we're adding an ellipsis, remove any trailing periods.
  440. $string = rtrim($string, '.');
  441. $string .= $ellipsis;
  442. }
  443. return $string;
  444. }
  445. /**
  446. * Compares UTF-8-encoded strings in a binary safe case-insensitive manner.
  447. *
  448. * @param string $str1
  449. * The first string.
  450. * @param string $str2
  451. * The second string.
  452. *
  453. * @return int
  454. * Returns < 0 if $str1 is less than $str2; > 0 if $str1 is greater than
  455. * $str2, and 0 if they are equal.
  456. */
  457. public static function strcasecmp($str1, $str2) {
  458. return strcmp(mb_strtoupper($str1), mb_strtoupper($str2));
  459. }
  460. /**
  461. * Encodes MIME/HTTP headers that contain incorrectly encoded characters.
  462. *
  463. * For example, Unicode::mimeHeaderEncode('tést.txt') returns
  464. * "=?UTF-8?B?dMOpc3QudHh0?=".
  465. *
  466. * See http://www.rfc-editor.org/rfc/rfc2047.txt for more information.
  467. *
  468. * Notes:
  469. * - Only encode strings that contain non-ASCII characters.
  470. * - We progressively cut-off a chunk with self::truncateBytes(). This ensures
  471. * each chunk starts and ends on a character boundary.
  472. * - Using \n as the chunk separator may cause problems on some systems and
  473. * may have to be changed to \r\n or \r.
  474. *
  475. * @param string $string
  476. * The header to encode.
  477. * @param bool $shorten
  478. * If TRUE, only return the first chunk of a multi-chunk encoded string.
  479. *
  480. * @return string
  481. * The mime-encoded header.
  482. */
  483. public static function mimeHeaderEncode($string, $shorten = FALSE) {
  484. if (preg_match('/[^\x20-\x7E]/', $string)) {
  485. // floor((75 - strlen("=?UTF-8?B??=")) * 0.75);
  486. $chunk_size = 47;
  487. $len = strlen($string);
  488. $output = '';
  489. while ($len > 0) {
  490. $chunk = static::truncateBytes($string, $chunk_size);
  491. $output .= ' =?UTF-8?B?' . base64_encode($chunk) . "?=\n";
  492. if ($shorten) {
  493. break;
  494. }
  495. $c = strlen($chunk);
  496. $string = substr($string, $c);
  497. $len -= $c;
  498. }
  499. return trim($output);
  500. }
  501. return $string;
  502. }
  503. /**
  504. * Decodes MIME/HTTP encoded header values.
  505. *
  506. * @param string $header
  507. * The header to decode.
  508. *
  509. * @return string
  510. * The mime-decoded header.
  511. */
  512. public static function mimeHeaderDecode($header) {
  513. $callback = function ($matches) {
  514. $data = ($matches[2] == 'B') ? base64_decode($matches[3]) : str_replace('_', ' ', quoted_printable_decode($matches[3]));
  515. if (strtolower($matches[1]) != 'utf-8') {
  516. $data = static::convertToUtf8($data, $matches[1]);
  517. }
  518. return $data;
  519. };
  520. // First step: encoded chunks followed by other encoded chunks (need to collapse whitespace)
  521. $header = preg_replace_callback('/=\?([^?]+)\?(Q|B)\?([^?]+|\?(?!=))\?=\s+(?==\?)/', $callback, $header);
  522. // Second step: remaining chunks (do not collapse whitespace)
  523. return preg_replace_callback('/=\?([^?]+)\?(Q|B)\?([^?]+|\?(?!=))\?=/', $callback, $header);
  524. }
  525. /**
  526. * Flip U+C0-U+DE to U+E0-U+FD and back. Can be used as preg_replace callback.
  527. *
  528. * @param array $matches
  529. * An array of matches by preg_replace_callback().
  530. *
  531. * @return string
  532. * The flipped text.
  533. */
  534. public static function caseFlip($matches) {
  535. return $matches[0][0] . chr(ord($matches[0][1]) ^ 32);
  536. }
  537. /**
  538. * Checks whether a string is valid UTF-8.
  539. *
  540. * All functions designed to filter input should use drupal_validate_utf8
  541. * to ensure they operate on valid UTF-8 strings to prevent bypass of the
  542. * filter.
  543. *
  544. * When text containing an invalid UTF-8 lead byte (0xC0 - 0xFF) is presented
  545. * as UTF-8 to Internet Explorer 6, the program may misinterpret subsequent
  546. * bytes. When these subsequent bytes are HTML control characters such as
  547. * quotes or angle brackets, parts of the text that were deemed safe by filters
  548. * end up in locations that are potentially unsafe; An onerror attribute that
  549. * is outside of a tag, and thus deemed safe by a filter, can be interpreted
  550. * by the browser as if it were inside the tag.
  551. *
  552. * The function does not return FALSE for strings containing character codes
  553. * above U+10FFFF, even though these are prohibited by RFC 3629.
  554. *
  555. * @param string $text
  556. * The text to check.
  557. *
  558. * @return bool
  559. * TRUE if the text is valid UTF-8, FALSE if not.
  560. */
  561. public static function validateUtf8($text) {
  562. if (strlen($text) == 0) {
  563. return TRUE;
  564. }
  565. // With the PCRE_UTF8 modifier 'u', preg_match() fails silently on strings
  566. // containing invalid UTF-8 byte sequences. It does not reject character
  567. // codes above U+10FFFF (represented by 4 or more octets), though.
  568. return (preg_match('/^./us', $text) == 1);
  569. }
  570. /**
  571. * Finds the position of the first occurrence of a string in another string.
  572. *
  573. * @param string $haystack
  574. * The string to search in.
  575. * @param string $needle
  576. * The string to find in $haystack.
  577. * @param int $offset
  578. * If specified, start the search at this number of characters from the
  579. * beginning (default 0).
  580. *
  581. * @return int|false
  582. * The position where $needle occurs in $haystack, always relative to the
  583. * beginning (independent of $offset), or FALSE if not found. Note that
  584. * a return value of 0 is not the same as FALSE.
  585. *
  586. * @deprecated in Drupal 8.6.0, will be removed before Drupal 9.0.0. Use
  587. * mb_strpos() instead.
  588. *
  589. * @see https://www.drupal.org/node/2850048
  590. */
  591. public static function strpos($haystack, $needle, $offset = 0) {
  592. @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);
  593. return mb_strpos($haystack, $needle, $offset);
  594. }
  595. }