unicode.inc 4.31 KB
Newer Older
1 2
<?php

3 4 5 6 7
/**
 * @file
 * Provides Unicode-related conversions and operations.
 */

8
use Drupal\Component\Utility\Unicode;
9

10
/**
11
 * Returns Unicode library status and errors.
12
 */
13 14
function unicode_requirements() {
  $libraries = array(
15 16 17
    Unicode::STATUS_SINGLEBYTE => t('Standard PHP'),
    Unicode::STATUS_MULTIBYTE => t('PHP Mbstring Extension'),
    Unicode::STATUS_ERROR => t('Error'),
18
  );
19
  $severities = array(
20 21 22
    Unicode::STATUS_SINGLEBYTE => REQUIREMENT_WARNING,
    Unicode::STATUS_MULTIBYTE => NULL,
    Unicode::STATUS_ERROR => REQUIREMENT_ERROR,
23
  );
24 25
  $failed_check = Unicode::check();
  $library = Unicode::getStatus();
26

27
  $requirements['unicode'] = array(
28
    'title' => t('Unicode library'),
29
    'value' => $libraries[$library],
30
    'severity' => $severities[$library],
31
  );
32
  $t_args = array(':url' => 'http://www.php.net/mbstring');
33 34
  switch ($failed_check) {
    case 'mb_strlen':
35
      $requirements['unicode']['description'] = t('Operations on Unicode strings are emulated on a best-effort basis. Install the <a href=":url">PHP mbstring extension</a> for improved Unicode support.', $t_args);
36
      break;
37

38
    case 'mbstring.func_overload':
39
      $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=":url">PHP mbstring documentation</a> for more information.', $t_args);
40 41 42
      break;

    case 'mbstring.encoding_translation':
43
      $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=":url">PHP mbstring documentation</a> for more information.', $t_args);
44 45 46
      break;

    case 'mbstring.http_input':
47
      $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=":url">PHP mbstring documentation</a> for more information.', $t_args);
48 49 50
      break;

    case 'mbstring.http_output':
51
      $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=":url">PHP mbstring documentation</a> for more information.', $t_args);
52 53
      break;
  }
54

55 56
  return $requirements;
}
57

58
/**
59
 * Prepares a new XML parser.
60
 *
61 62 63 64 65 66
 * This is a wrapper around xml_parser_create() which extracts the encoding
 * from the XML data first and sets the output encoding to UTF-8. This function
 * should be used instead of xml_parser_create(), because PHP 4's XML parser
 * doesn't check the input encoding itself. "Starting from PHP 5, the input
 * encoding is automatically detected, so that the encoding parameter specifies
 * only the output encoding."
67
 *
68 69
 * This is also where unsupported encodings will be converted. Callers should
 * take this into account: $data might have been changed after the call.
70
 *
71
 * @param $data
72
 *   The XML data which will be parsed later.
73
 *
74
 * @return
75
 *   An XML parser object or FALSE on error.
76 77
 *
 * @ingroup php_wrappers
78 79 80 81
 */
function drupal_xml_parser_create(&$data) {
  // Default XML encoding is UTF-8
  $encoding = 'utf-8';
82
  $bom = FALSE;
83 84 85

  // Check for UTF-8 byte order mark (PHP5's XML parser doesn't handle it).
  if (!strncmp($data, "\xEF\xBB\xBF", 3)) {
86
    $bom = TRUE;
87 88 89 90
    $data = substr($data, 3);
  }

  // Check for an encoding declaration in the XML prolog if no BOM was found.
91
  if (!$bom && preg_match('/^<\?xml[^>]+encoding="(.+?)"/', $data, $match)) {
92 93 94 95 96 97
    $encoding = $match[1];
  }

  // Unsupported encodings are converted here into UTF-8.
  $php_supported = array('utf-8', 'iso-8859-1', 'us-ascii');
  if (!in_array(strtolower($encoding), $php_supported)) {
98
    $out = Unicode::convertToUtf8($data, $encoding);
99
    if ($out !== FALSE) {
100
      $encoding = 'utf-8';
101
      $data = preg_replace('/^(<\?xml[^>]+encoding)="(.+?)"/', '\\1="utf-8"', $out);
102 103
    }
    else {
104
      \Drupal::logger('php')->warning('Could not convert XML encoding %s to UTF-8.', array('%s' => $encoding));
105
      return FALSE;
106 107 108 109 110 111 112
    }
  }

  $xml_parser = xml_parser_create($encoding);
  xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, 'utf-8');
  return $xml_parser;
}