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