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