unicode.inc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * @file
  4. * Provides Unicode-related conversions and operations.
  5. */
  6. use Drupal\Component\Utility\Unicode;
  7. /**
  8. * Returns Unicode library status and errors.
  9. */
  10. /**
  11. * Moves unicode_requirements() logic to system_requirements().
  12. *
  13. * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0.
  14. *
  15. * @see https://www.drupal.org/node/2884698
  16. */
  17. function unicode_requirements() {
  18. @trigger_error('unicode_requirements() is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. There is no replacement; system_requirements() now includes the logic instead. See https://www.drupal.org/node/2884698', E_USER_DEPRECATED);
  19. $libraries = [
  20. Unicode::STATUS_SINGLEBYTE => t('Standard PHP'),
  21. Unicode::STATUS_MULTIBYTE => t('PHP Mbstring Extension'),
  22. Unicode::STATUS_ERROR => t('Error'),
  23. ];
  24. $severities = [
  25. Unicode::STATUS_SINGLEBYTE => REQUIREMENT_WARNING,
  26. Unicode::STATUS_MULTIBYTE => NULL,
  27. Unicode::STATUS_ERROR => REQUIREMENT_ERROR,
  28. ];
  29. $failed_check = Unicode::check();
  30. $library = Unicode::getStatus();
  31. $requirements['unicode'] = [
  32. 'title' => t('Unicode library'),
  33. 'value' => $libraries[$library],
  34. 'severity' => $severities[$library],
  35. ];
  36. switch ($failed_check) {
  37. case 'mb_strlen':
  38. $requirements['unicode']['description'] = t('Operations on Unicode strings are emulated on a best-effort basis. Install the <a href="http://php.net/mbstring">PHP mbstring extension</a> for improved Unicode support.');
  39. break;
  40. case 'mbstring.func_overload':
  41. $requirements['unicode']['description'] = t('Multibyte string function overloading in PHP is active and must be disabled. Check the php.ini <em>mbstring.func_overload</em> setting. Please refer to the <a href="http://php.net/mbstring">PHP mbstring documentation</a> for more information.');
  42. break;
  43. case 'mbstring.encoding_translation':
  44. $requirements['unicode']['description'] = t('Multibyte string input conversion in PHP is active and must be disabled. Check the php.ini <em>mbstring.encoding_translation</em> setting. Please refer to the <a href="http://php.net/mbstring">PHP mbstring documentation</a> for more information.');
  45. break;
  46. case 'mbstring.http_input':
  47. $requirements['unicode']['description'] = t('Multibyte string input conversion in PHP is active and must be disabled. Check the php.ini <em>mbstring.http_input</em> setting. Please refer to the <a href="http://php.net/mbstring">PHP mbstring documentation</a> for more information.');
  48. break;
  49. case 'mbstring.http_output':
  50. $requirements['unicode']['description'] = t('Multibyte string output conversion in PHP is active and must be disabled. Check the php.ini <em>mbstring.http_output</em> setting. Please refer to the <a href="http://php.net/mbstring">PHP mbstring documentation</a> for more information.');
  51. break;
  52. }
  53. return $requirements;
  54. }
  55. /**
  56. * Prepares a new XML parser.
  57. *
  58. * This is a wrapper around xml_parser_create() which extracts the encoding
  59. * from the XML data first and sets the output encoding to UTF-8. This function
  60. * should be used instead of xml_parser_create(), because PHP 4's XML parser
  61. * doesn't check the input encoding itself. "Starting from PHP 5, the input
  62. * encoding is automatically detected, so that the encoding parameter specifies
  63. * only the output encoding."
  64. *
  65. * This is also where unsupported encodings will be converted. Callers should
  66. * take this into account: $data might have been changed after the call.
  67. *
  68. * @param $data
  69. * The XML data which will be parsed later.
  70. *
  71. * @return
  72. * An XML parser object or FALSE on error.
  73. *
  74. * @ingroup php_wrappers
  75. *
  76. * @deprecated in Drupal 8.3.0 and will bre removed in Drupal 9.0.0. Use
  77. * xml_parser_create() and
  78. * xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, 'utf-8')
  79. * instead.
  80. */
  81. function drupal_xml_parser_create(&$data) {
  82. // Default XML encoding is UTF-8
  83. $encoding = 'utf-8';
  84. $bom = FALSE;
  85. // Check for UTF-8 byte order mark (PHP5's XML parser doesn't handle it).
  86. if (!strncmp($data, "\xEF\xBB\xBF", 3)) {
  87. $bom = TRUE;
  88. $data = substr($data, 3);
  89. }
  90. // Check for an encoding declaration in the XML prolog if no BOM was found.
  91. if (!$bom && preg_match('/^<\?xml[^>]+encoding="(.+?)"/', $data, $match)) {
  92. $encoding = $match[1];
  93. }
  94. // Unsupported encodings are converted here into UTF-8.
  95. $php_supported = ['utf-8', 'iso-8859-1', 'us-ascii'];
  96. if (!in_array(strtolower($encoding), $php_supported)) {
  97. $out = Unicode::convertToUtf8($data, $encoding);
  98. if ($out !== FALSE) {
  99. $encoding = 'utf-8';
  100. $data = preg_replace('/^(<\?xml[^>]+encoding)="(.+?)"/', '\\1="utf-8"', $out);
  101. }
  102. else {
  103. \Drupal::logger('php')->warning('Could not convert XML encoding %s to UTF-8.', ['%s' => $encoding]);
  104. return FALSE;
  105. }
  106. }
  107. $xml_parser = xml_parser_create($encoding);
  108. xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, 'utf-8');
  109. return $xml_parser;
  110. }