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