Chris@0: t('Standard PHP'), Chris@0: Unicode::STATUS_MULTIBYTE => t('PHP Mbstring Extension'), Chris@0: Unicode::STATUS_ERROR => t('Error'), Chris@0: ]; Chris@0: $severities = [ Chris@0: Unicode::STATUS_SINGLEBYTE => REQUIREMENT_WARNING, Chris@0: Unicode::STATUS_MULTIBYTE => NULL, Chris@0: Unicode::STATUS_ERROR => REQUIREMENT_ERROR, Chris@0: ]; Chris@0: $failed_check = Unicode::check(); Chris@0: $library = Unicode::getStatus(); Chris@0: Chris@0: $requirements['unicode'] = [ Chris@0: 'title' => t('Unicode library'), Chris@0: 'value' => $libraries[$library], Chris@0: 'severity' => $severities[$library], Chris@0: ]; Chris@0: switch ($failed_check) { Chris@0: case 'mb_strlen': Chris@0: $requirements['unicode']['description'] = t('Operations on Unicode strings are emulated on a best-effort basis. Install the PHP mbstring extension for improved Unicode support.'); Chris@0: break; Chris@0: Chris@0: case 'mbstring.func_overload': Chris@0: $requirements['unicode']['description'] = t('Multibyte string function overloading in PHP is active and must be disabled. Check the php.ini mbstring.func_overload setting. Please refer to the PHP mbstring documentation for more information.'); Chris@0: break; Chris@0: Chris@0: case 'mbstring.encoding_translation': Chris@0: $requirements['unicode']['description'] = t('Multibyte string input conversion in PHP is active and must be disabled. Check the php.ini mbstring.encoding_translation setting. Please refer to the PHP mbstring documentation for more information.'); Chris@0: break; Chris@0: Chris@0: case 'mbstring.http_input': Chris@0: $requirements['unicode']['description'] = t('Multibyte string input conversion in PHP is active and must be disabled. Check the php.ini mbstring.http_input setting. Please refer to the PHP mbstring documentation for more information.'); Chris@0: break; Chris@0: Chris@0: case 'mbstring.http_output': Chris@0: $requirements['unicode']['description'] = t('Multibyte string output conversion in PHP is active and must be disabled. Check the php.ini mbstring.http_output setting. Please refer to the PHP mbstring documentation for more information.'); Chris@0: break; Chris@0: } Chris@0: Chris@0: return $requirements; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prepares a new XML parser. Chris@0: * Chris@0: * This is a wrapper around xml_parser_create() which extracts the encoding Chris@0: * from the XML data first and sets the output encoding to UTF-8. This function Chris@0: * should be used instead of xml_parser_create(), because PHP 4's XML parser Chris@0: * doesn't check the input encoding itself. "Starting from PHP 5, the input Chris@0: * encoding is automatically detected, so that the encoding parameter specifies Chris@0: * only the output encoding." Chris@0: * Chris@0: * This is also where unsupported encodings will be converted. Callers should Chris@0: * take this into account: $data might have been changed after the call. Chris@0: * Chris@0: * @param $data Chris@0: * The XML data which will be parsed later. Chris@0: * Chris@0: * @return Chris@0: * An XML parser object or FALSE on error. Chris@0: * Chris@0: * @ingroup php_wrappers Chris@0: * Chris@0: * @deprecated in Drupal 8.3.0 and will bre removed in Drupal 9.0.0. Use Chris@0: * xml_parser_create() and Chris@0: * xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, 'utf-8') Chris@0: * instead. Chris@0: */ Chris@0: function drupal_xml_parser_create(&$data) { Chris@0: // Default XML encoding is UTF-8 Chris@0: $encoding = 'utf-8'; Chris@0: $bom = FALSE; Chris@0: Chris@0: // Check for UTF-8 byte order mark (PHP5's XML parser doesn't handle it). Chris@0: if (!strncmp($data, "\xEF\xBB\xBF", 3)) { Chris@0: $bom = TRUE; Chris@0: $data = substr($data, 3); Chris@0: } Chris@0: Chris@0: // Check for an encoding declaration in the XML prolog if no BOM was found. Chris@0: if (!$bom && preg_match('/^<\?xml[^>]+encoding="(.+?)"/', $data, $match)) { Chris@0: $encoding = $match[1]; Chris@0: } Chris@0: Chris@0: // Unsupported encodings are converted here into UTF-8. Chris@0: $php_supported = ['utf-8', 'iso-8859-1', 'us-ascii']; Chris@0: if (!in_array(strtolower($encoding), $php_supported)) { Chris@0: $out = Unicode::convertToUtf8($data, $encoding); Chris@0: if ($out !== FALSE) { Chris@0: $encoding = 'utf-8'; Chris@0: $data = preg_replace('/^(<\?xml[^>]+encoding)="(.+?)"/', '\\1="utf-8"', $out); Chris@0: } Chris@0: else { Chris@0: \Drupal::logger('php')->warning('Could not convert XML encoding %s to UTF-8.', ['%s' => $encoding]); Chris@0: return FALSE; Chris@0: } Chris@0: } Chris@0: Chris@0: $xml_parser = xml_parser_create($encoding); Chris@0: xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, 'utf-8'); Chris@0: return $xml_parser; Chris@0: }