unicode.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. function unicode_requirements() {
  11. $libraries = [
  12. Unicode::STATUS_SINGLEBYTE => t('Standard PHP'),
  13. Unicode::STATUS_MULTIBYTE => t('PHP Mbstring Extension'),
  14. Unicode::STATUS_ERROR => t('Error'),
  15. ];
  16. $severities = [
  17. Unicode::STATUS_SINGLEBYTE => REQUIREMENT_WARNING,
  18. Unicode::STATUS_MULTIBYTE => NULL,
  19. Unicode::STATUS_ERROR => REQUIREMENT_ERROR,
  20. ];
  21. $failed_check = Unicode::check();
  22. $library = Unicode::getStatus();
  23. $requirements['unicode'] = [
  24. 'title' => t('Unicode library'),
  25. 'value' => $libraries[$library],
  26. 'severity' => $severities[$library],
  27. ];
  28. switch ($failed_check) {
  29. case 'mb_strlen':
  30. $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.');
  31. break;
  32. case 'mbstring.func_overload':
  33. $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.');
  34. break;
  35. case 'mbstring.encoding_translation':
  36. $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.');
  37. break;
  38. case 'mbstring.http_input':
  39. $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.');
  40. break;
  41. case 'mbstring.http_output':
  42. $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.');
  43. break;
  44. }
  45. return $requirements;
  46. }
  47. /**
  48. * Prepares a new XML parser.
  49. *
  50. * This is a wrapper around xml_parser_create() which extracts the encoding
  51. * from the XML data first and sets the output encoding to UTF-8. This function
  52. * should be used instead of xml_parser_create(), because PHP 4's XML parser
  53. * doesn't check the input encoding itself. "Starting from PHP 5, the input
  54. * encoding is automatically detected, so that the encoding parameter specifies
  55. * only the output encoding."
  56. *
  57. * This is also where unsupported encodings will be converted. Callers should
  58. * take this into account: $data might have been changed after the call.
  59. *
  60. * @param $data
  61. * The XML data which will be parsed later.
  62. *
  63. * @return
  64. * An XML parser object or FALSE on error.
  65. *
  66. * @ingroup php_wrappers
  67. *
  68. * @deprecated in Drupal 8.3.0 and will bre removed in Drupal 9.0.0. Use
  69. * xml_parser_create() and
  70. * xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, 'utf-8')
  71. * instead.
  72. */
  73. function drupal_xml_parser_create(&$data) {
  74. // Default XML encoding is UTF-8
  75. $encoding = 'utf-8';
  76. $bom = FALSE;
  77. // Check for UTF-8 byte order mark (PHP5's XML parser doesn't handle it).
  78. if (!strncmp($data, "\xEF\xBB\xBF", 3)) {
  79. $bom = TRUE;
  80. $data = substr($data, 3);
  81. }
  82. // Check for an encoding declaration in the XML prolog if no BOM was found.
  83. if (!$bom && preg_match('/^<\?xml[^>]+encoding="(.+?)"/', $data, $match)) {
  84. $encoding = $match[1];
  85. }
  86. // Unsupported encodings are converted here into UTF-8.
  87. $php_supported = ['utf-8', 'iso-8859-1', 'us-ascii'];
  88. if (!in_array(strtolower($encoding), $php_supported)) {
  89. $out = Unicode::convertToUtf8($data, $encoding);
  90. if ($out !== FALSE) {
  91. $encoding = 'utf-8';
  92. $data = preg_replace('/^(<\?xml[^>]+encoding)="(.+?)"/', '\\1="utf-8"', $out);
  93. }
  94. else {
  95. \Drupal::logger('php')->warning('Could not convert XML encoding %s to UTF-8.', ['%s' => $encoding]);
  96. return FALSE;
  97. }
  98. }
  99. $xml_parser = xml_parser_create($encoding);
  100. xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, 'utf-8');
  101. return $xml_parser;
  102. }