webmaster@1: PHP PCRE documentation for more information.', array('@url' => 'http://www.php.net/pcre')));
webmaster@1: }
webmaster@1:
webmaster@1: // Check for mbstring extension
webmaster@1: if (!function_exists('mb_strlen')) {
webmaster@1: return array(UNICODE_SINGLEBYTE, $t('Operations on Unicode strings are emulated on a best-effort basis. Install the PHP mbstring extension for improved Unicode support.', array('@url' => 'http://www.php.net/mbstring')));
webmaster@1: }
webmaster@1:
webmaster@1: // Check mbstring configuration
webmaster@1: if (ini_get('mbstring.func_overload') != 0) {
webmaster@1: return array(UNICODE_ERROR, $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.', array('@url' => 'http://www.php.net/mbstring')));
webmaster@1: }
webmaster@1: if (ini_get('mbstring.encoding_translation') != 0) {
webmaster@1: return array(UNICODE_ERROR, $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.', array('@url' => 'http://www.php.net/mbstring')));
webmaster@1: }
webmaster@1: if (ini_get('mbstring.http_input') != 'pass') {
webmaster@1: return array(UNICODE_ERROR, $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.', array('@url' => 'http://www.php.net/mbstring')));
webmaster@1: }
webmaster@1: if (ini_get('mbstring.http_output') != 'pass') {
webmaster@1: return array(UNICODE_ERROR, $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.', array('@url' => 'http://www.php.net/mbstring')));
webmaster@1: }
webmaster@1:
webmaster@1: // Set appropriate configuration
webmaster@1: mb_internal_encoding('utf-8');
webmaster@1: mb_language('uni');
webmaster@1: return array(UNICODE_MULTIBYTE, '');
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Return Unicode library status and errors.
webmaster@1: */
webmaster@1: function unicode_requirements() {
webmaster@1: // Ensure translations don't break at install time
webmaster@1: $t = get_t();
webmaster@1:
webmaster@1: $libraries = array(
webmaster@1: UNICODE_SINGLEBYTE => $t('Standard PHP'),
webmaster@1: UNICODE_MULTIBYTE => $t('PHP Mbstring Extension'),
webmaster@1: UNICODE_ERROR => $t('Error'),
webmaster@1: );
webmaster@1: $severities = array(
webmaster@1: UNICODE_SINGLEBYTE => REQUIREMENT_WARNING,
webmaster@1: UNICODE_MULTIBYTE => REQUIREMENT_OK,
webmaster@1: UNICODE_ERROR => REQUIREMENT_ERROR,
webmaster@1: );
webmaster@1: list($library, $description) = _unicode_check();
webmaster@1:
webmaster@1: $requirements['unicode'] = array(
webmaster@1: 'title' => $t('Unicode library'),
webmaster@1: 'value' => $libraries[$library],
webmaster@1: );
webmaster@1: if ($description) {
webmaster@1: $requirements['unicode']['description'] = $description;
webmaster@1: }
webmaster@1:
webmaster@1: $requirements['unicode']['severity'] = $severities[$library];
webmaster@1:
webmaster@1: return $requirements;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Prepare a new XML parser.
webmaster@1: *
webmaster@1: * This is a wrapper around xml_parser_create() which extracts the encoding from
webmaster@1: * the XML data first and sets the output encoding to UTF-8. This function should
webmaster@1: * be used instead of xml_parser_create(), because PHP 4's XML parser doesn't
webmaster@1: * check the input encoding itself. "Starting from PHP 5, the input encoding is
webmaster@1: * automatically detected, so that the encoding parameter specifies only the
webmaster@1: * output encoding."
webmaster@1: *
webmaster@1: * This is also where unsupported encodings will be converted. Callers should
webmaster@1: * take this into account: $data might have been changed after the call.
webmaster@1: *
webmaster@1: * @param &$data
webmaster@1: * The XML data which will be parsed later.
webmaster@1: * @return
webmaster@1: * An XML parser object.
webmaster@1: */
webmaster@1: function drupal_xml_parser_create(&$data) {
webmaster@1: // Default XML encoding is UTF-8
webmaster@1: $encoding = 'utf-8';
webmaster@1: $bom = FALSE;
webmaster@1:
webmaster@1: // Check for UTF-8 byte order mark (PHP5's XML parser doesn't handle it).
webmaster@1: if (!strncmp($data, "\xEF\xBB\xBF", 3)) {
webmaster@1: $bom = TRUE;
webmaster@1: $data = substr($data, 3);
webmaster@1: }
webmaster@1:
webmaster@1: // Check for an encoding declaration in the XML prolog if no BOM was found.
webmaster@1: if (!$bom && ereg('^<\?xml[^>]+encoding="([^"]+)"', $data, $match)) {
webmaster@1: $encoding = $match[1];
webmaster@1: }
webmaster@1:
webmaster@1: // Unsupported encodings are converted here into UTF-8.
webmaster@1: $php_supported = array('utf-8', 'iso-8859-1', 'us-ascii');
webmaster@1: if (!in_array(strtolower($encoding), $php_supported)) {
webmaster@1: $out = drupal_convert_to_utf8($data, $encoding);
webmaster@1: if ($out !== FALSE) {
webmaster@1: $encoding = 'utf-8';
webmaster@1: $data = ereg_replace('^(<\?xml[^>]+encoding)="([^"]+)"', '\\1="utf-8"', $out);
webmaster@1: }
webmaster@1: else {
webmaster@1: watchdog('php', 'Could not convert XML encoding %s to UTF-8.', array('%s' => $encoding), WATCHDOG_WARNING);
webmaster@1: return 0;
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: $xml_parser = xml_parser_create($encoding);
webmaster@1: xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, 'utf-8');
webmaster@1: return $xml_parser;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Convert data to UTF-8
webmaster@1: *
webmaster@1: * Requires the iconv, GNU recode or mbstring PHP extension.
webmaster@1: *
webmaster@1: * @param $data
webmaster@1: * The data to be converted.
webmaster@1: * @param $encoding
webmaster@1: * The encoding that the data is in
webmaster@1: * @return
webmaster@1: * Converted data or FALSE.
webmaster@1: */
webmaster@1: function drupal_convert_to_utf8($data, $encoding) {
webmaster@1: if (function_exists('iconv')) {
webmaster@1: $out = @iconv($encoding, 'utf-8', $data);
webmaster@1: }
webmaster@1: else if (function_exists('mb_convert_encoding')) {
webmaster@1: $out = @mb_convert_encoding($data, 'utf-8', $encoding);
webmaster@1: }
webmaster@1: else if (function_exists('recode_string')) {
webmaster@1: $out = @recode_string($encoding .'..utf-8', $data);
webmaster@1: }
webmaster@1: else {
webmaster@1: watchdog('php', 'Unsupported encoding %s. Please install iconv, GNU recode or mbstring for PHP.', array('%s' => $encoding), WATCHDOG_ERROR);
webmaster@1: return FALSE;
webmaster@1: }
webmaster@1:
webmaster@1: return $out;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Truncate a UTF-8-encoded string safely to a number of bytes.
webmaster@1: *
webmaster@1: * If the end position is in the middle of a UTF-8 sequence, it scans backwards
webmaster@1: * until the beginning of the byte sequence.
webmaster@1: *
webmaster@1: * Use this function whenever you want to chop off a string at an unsure
webmaster@1: * location. On the other hand, if you're sure that you're splitting on a
webmaster@1: * character boundary (e.g. after using strpos() or similar), you can safely use
webmaster@1: * substr() instead.
webmaster@1: *
webmaster@1: * @param $string
webmaster@1: * The string to truncate.
webmaster@1: * @param $len
webmaster@1: * An upper limit on the returned string length.
webmaster@1: * @return
webmaster@1: * The truncated string.
webmaster@1: */
webmaster@1: function drupal_truncate_bytes($string, $len) {
webmaster@1: if (strlen($string) <= $len) {
webmaster@1: return $string;
webmaster@1: }
webmaster@1: if ((ord($string[$len]) < 0x80) || (ord($string[$len]) >= 0xC0)) {
webmaster@1: return substr($string, 0, $len);
webmaster@1: }
webmaster@1: while (--$len >= 0 && ord($string[$len]) >= 0x80 && ord($string[$len]) < 0xC0) {};
webmaster@1: return substr($string, 0, $len);
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Truncate a UTF-8-encoded string safely to a number of characters.
webmaster@1: *
webmaster@1: * @param $string
webmaster@1: * The string to truncate.
webmaster@1: * @param $len
webmaster@1: * An upper limit on the returned string length.
webmaster@1: * @param $wordsafe
webmaster@1: * Flag to truncate at last space within the upper limit. Defaults to FALSE.
webmaster@1: * @param $dots
webmaster@1: * Flag to add trailing dots. Defaults to FALSE.
webmaster@1: * @return
webmaster@1: * The truncated string.
webmaster@1: */
webmaster@1: function truncate_utf8($string, $len, $wordsafe = FALSE, $dots = FALSE) {
webmaster@1:
webmaster@1: if (drupal_strlen($string) <= $len) {
webmaster@1: return $string;
webmaster@1: }
webmaster@1:
webmaster@1: if ($dots) {
webmaster@1: $len -= 4;
webmaster@1: }
webmaster@1:
webmaster@1: if ($wordsafe) {
webmaster@1: $string = drupal_substr($string, 0, $len + 1); // leave one more character
webmaster@1: if ($last_space = strrpos($string, ' ')) { // space exists AND is not on position 0
webmaster@1: $string = substr($string, 0, $last_space);
webmaster@1: }
webmaster@1: else {
webmaster@1: $string = drupal_substr($string, 0, $len);
webmaster@1: }
webmaster@1: }
webmaster@1: else {
webmaster@1: $string = drupal_substr($string, 0, $len);
webmaster@1: }
webmaster@1:
webmaster@1: if ($dots) {
webmaster@1: $string .= ' ...';
webmaster@1: }
webmaster@1:
webmaster@1: return $string;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Encodes MIME/HTTP header values that contain non-ASCII, UTF-8 encoded
webmaster@1: * characters.
webmaster@1: *
webmaster@1: * For example, mime_header_encode('tést.txt') returns "=?UTF-8?B?dMOpc3QudHh0?=".
webmaster@1: *
webmaster@1: * See http://www.rfc-editor.org/rfc/rfc2047.txt for more information.
webmaster@1: *
webmaster@1: * Notes:
webmaster@1: * - Only encode strings that contain non-ASCII characters.
webmaster@1: * - We progressively cut-off a chunk with truncate_utf8(). This is to ensure
webmaster@1: * each chunk starts and ends on a character boundary.
webmaster@1: * - Using \n as the chunk separator may cause problems on some systems and may
webmaster@1: * have to be changed to \r\n or \r.
webmaster@1: */
webmaster@1: function mime_header_encode($string) {
webmaster@1: if (preg_match('/[^\x20-\x7E]/', $string)) {
webmaster@1: $chunk_size = 47; // floor((75 - strlen("=?UTF-8?B??=")) * 0.75);
webmaster@1: $len = strlen($string);
webmaster@1: $output = '';
webmaster@1: while ($len > 0) {
webmaster@1: $chunk = drupal_truncate_bytes($string, $chunk_size);
webmaster@1: $output .= ' =?UTF-8?B?'. base64_encode($chunk) ."?=\n";
webmaster@1: $c = strlen($chunk);
webmaster@1: $string = substr($string, $c);
webmaster@1: $len -= $c;
webmaster@1: }
webmaster@1: return trim($output);
webmaster@1: }
webmaster@1: return $string;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Complement to mime_header_encode
webmaster@1: */
webmaster@1: function mime_header_decode($header) {
webmaster@1: // First step: encoded chunks followed by other encoded chunks (need to collapse whitespace)
webmaster@1: $header = preg_replace_callback('/=\?([^?]+)\?(Q|B)\?([^?]+|\?(?!=))\?=\s+(?==\?)/', '_mime_header_decode', $header);
webmaster@1: // Second step: remaining chunks (do not collapse whitespace)
webmaster@1: return preg_replace_callback('/=\?([^?]+)\?(Q|B)\?([^?]+|\?(?!=))\?=/', '_mime_header_decode', $header);
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Helper function to mime_header_decode
webmaster@1: */
webmaster@1: function _mime_header_decode($matches) {
webmaster@1: // Regexp groups:
webmaster@1: // 1: Character set name
webmaster@1: // 2: Escaping method (Q or B)
webmaster@1: // 3: Encoded data
webmaster@1: $data = ($matches[2] == 'B') ? base64_decode($matches[3]) : str_replace('_', ' ', quoted_printable_decode($matches[3]));
webmaster@1: if (strtolower($matches[1]) != 'utf-8') {
webmaster@1: $data = drupal_convert_to_utf8($data, $matches[1]);
webmaster@1: }
webmaster@1: return $data;
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Decode all HTML entities (including numerical ones) to regular UTF-8 bytes.
webmaster@1: * Double-escaped entities will only be decoded once ("<" becomes "<", not "<").
webmaster@1: *
webmaster@1: * @param $text
webmaster@1: * The text to decode entities in.
webmaster@1: * @param $exclude
webmaster@1: * An array of characters which should not be decoded. For example,
webmaster@1: * array('<', '&', '"'). This affects both named and numerical entities.
webmaster@1: */
webmaster@1: function decode_entities($text, $exclude = array()) {
webmaster@1: static $table;
webmaster@1: // We store named entities in a table for quick processing.
webmaster@1: if (!isset($table)) {
webmaster@1: // Get all named HTML entities.
webmaster@1: $table = array_flip(get_html_translation_table(HTML_ENTITIES));
webmaster@1: // PHP gives us ISO-8859-1 data, we need UTF-8.
webmaster@1: $table = array_map('utf8_encode', $table);
webmaster@1: // Add apostrophe (XML)
webmaster@1: $table['''] = "'";
webmaster@1: }
webmaster@1: $newtable = array_diff($table, $exclude);
webmaster@1:
webmaster@1: // Use a regexp to select all entities in one pass, to avoid decoding double-escaped entities twice.
webmaster@1: return preg_replace('/&(#x?)?([A-Za-z0-9]+);/e', '_decode_entities("$1", "$2", "$0", $newtable, $exclude)', $text);
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Helper function for decode_entities
webmaster@1: */
webmaster@1: function _decode_entities($prefix, $codepoint, $original, &$table, &$exclude) {
webmaster@1: // Named entity
webmaster@1: if (!$prefix) {
webmaster@1: if (isset($table[$original])) {
webmaster@1: return $table[$original];
webmaster@1: }
webmaster@1: else {
webmaster@1: return $original;
webmaster@1: }
webmaster@1: }
webmaster@1: // Hexadecimal numerical entity
webmaster@1: if ($prefix == '#x') {
webmaster@1: $codepoint = base_convert($codepoint, 16, 10);
webmaster@1: }
webmaster@1: // Decimal numerical entity (strip leading zeros to avoid PHP octal notation)
webmaster@1: else {
webmaster@1: $codepoint = preg_replace('/^0+/', '', $codepoint);
webmaster@1: }
webmaster@1: // Encode codepoint as UTF-8 bytes
webmaster@1: if ($codepoint < 0x80) {
webmaster@1: $str = chr($codepoint);
webmaster@1: }
webmaster@1: else if ($codepoint < 0x800) {
webmaster@1: $str = chr(0xC0 | ($codepoint >> 6))
webmaster@1: . chr(0x80 | ($codepoint & 0x3F));
webmaster@1: }
webmaster@1: else if ($codepoint < 0x10000) {
webmaster@1: $str = chr(0xE0 | ( $codepoint >> 12))
webmaster@1: . chr(0x80 | (($codepoint >> 6) & 0x3F))
webmaster@1: . chr(0x80 | ( $codepoint & 0x3F));
webmaster@1: }
webmaster@1: else if ($codepoint < 0x200000) {
webmaster@1: $str = chr(0xF0 | ( $codepoint >> 18))
webmaster@1: . chr(0x80 | (($codepoint >> 12) & 0x3F))
webmaster@1: . chr(0x80 | (($codepoint >> 6) & 0x3F))
webmaster@1: . chr(0x80 | ( $codepoint & 0x3F));
webmaster@1: }
webmaster@1: // Check for excluded characters
webmaster@1: if (in_array($str, $exclude)) {
webmaster@1: return $original;
webmaster@1: }
webmaster@1: else {
webmaster@1: return $str;
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Count the amount of characters in a UTF-8 string. This is less than or
webmaster@1: * equal to the byte count.
webmaster@1: */
webmaster@1: function drupal_strlen($text) {
webmaster@1: global $multibyte;
webmaster@1: if ($multibyte == UNICODE_MULTIBYTE) {
webmaster@1: return mb_strlen($text);
webmaster@1: }
webmaster@1: else {
webmaster@1: // Do not count UTF-8 continuation bytes.
webmaster@1: return strlen(preg_replace("/[\x80-\xBF]/", '', $text));
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Uppercase a UTF-8 string.
webmaster@1: */
webmaster@1: function drupal_strtoupper($text) {
webmaster@1: global $multibyte;
webmaster@1: if ($multibyte == UNICODE_MULTIBYTE) {
webmaster@1: return mb_strtoupper($text);
webmaster@1: }
webmaster@1: else {
webmaster@1: // Use C-locale for ASCII-only uppercase
webmaster@1: $text = strtoupper($text);
webmaster@1: // Case flip Latin-1 accented letters
webmaster@1: $text = preg_replace_callback('/\xC3[\xA0-\xB6\xB8-\xBE]/', '_unicode_caseflip', $text);
webmaster@1: return $text;
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Lowercase a UTF-8 string.
webmaster@1: */
webmaster@1: function drupal_strtolower($text) {
webmaster@1: global $multibyte;
webmaster@1: if ($multibyte == UNICODE_MULTIBYTE) {
webmaster@1: return mb_strtolower($text);
webmaster@1: }
webmaster@1: else {
webmaster@1: // Use C-locale for ASCII-only lowercase
webmaster@1: $text = strtolower($text);
webmaster@1: // Case flip Latin-1 accented letters
webmaster@1: $text = preg_replace_callback('/\xC3[\x80-\x96\x98-\x9E]/', '_unicode_caseflip', $text);
webmaster@1: return $text;
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Helper function for case conversion of Latin-1.
webmaster@1: * Used for flipping U+C0-U+DE to U+E0-U+FD and back.
webmaster@1: */
webmaster@1: function _unicode_caseflip($matches) {
webmaster@1: return $matches[0][0] . chr(ord($matches[0][1]) ^ 32);
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Capitalize the first letter of a UTF-8 string.
webmaster@1: */
webmaster@1: function drupal_ucfirst($text) {
webmaster@1: // Note: no mbstring equivalent!
webmaster@1: return drupal_strtoupper(drupal_substr($text, 0, 1)) . drupal_substr($text, 1);
webmaster@1: }
webmaster@1:
webmaster@1: /**
webmaster@1: * Cut off a piece of a string based on character indices and counts. Follows
webmaster@1: * the same behavior as PHP's own substr() function.
webmaster@1: *
webmaster@1: * Note that for cutting off a string at a known character/substring
webmaster@1: * location, the usage of PHP's normal strpos/substr is safe and
webmaster@1: * much faster.
webmaster@1: */
webmaster@1: function drupal_substr($text, $start, $length = NULL) {
webmaster@1: global $multibyte;
webmaster@1: if ($multibyte == UNICODE_MULTIBYTE) {
webmaster@1: return $length === NULL ? mb_substr($text, $start) : mb_substr($text, $start, $length);
webmaster@1: }
webmaster@1: else {
webmaster@1: $strlen = strlen($text);
webmaster@1: // Find the starting byte offset
webmaster@1: $bytes = 0;
webmaster@1: if ($start > 0) {
webmaster@1: // Count all the continuation bytes from the start until we have found
webmaster@1: // $start characters
webmaster@1: $bytes = -1; $chars = -1;
webmaster@1: while ($bytes < $strlen && $chars < $start) {
webmaster@1: $bytes++;
webmaster@1: $c = ord($text[$bytes]);
webmaster@1: if ($c < 0x80 || $c >= 0xC0) {
webmaster@1: $chars++;
webmaster@1: }
webmaster@1: }
webmaster@1: }
webmaster@1: else if ($start < 0) {
webmaster@1: // Count all the continuation bytes from the end until we have found
webmaster@1: // abs($start) characters
webmaster@1: $start = abs($start);
webmaster@1: $bytes = $strlen; $chars = 0;
webmaster@1: while ($bytes > 0 && $chars < $start) {
webmaster@1: $bytes--;
webmaster@1: $c = ord($text[$bytes]);
webmaster@1: if ($c < 0x80 || $c >= 0xC0) {
webmaster@1: $chars++;
webmaster@1: }
webmaster@1: }
webmaster@1: }
webmaster@1: $istart = $bytes;
webmaster@1:
webmaster@1: // Find the ending byte offset
webmaster@1: if ($length === NULL) {
webmaster@1: $bytes = $strlen - 1;
webmaster@1: }
webmaster@1: else if ($length > 0) {
webmaster@1: // Count all the continuation bytes from the starting index until we have
webmaster@1: // found $length + 1 characters. Then backtrack one byte.
webmaster@1: $bytes = $istart; $chars = 0;
webmaster@1: while ($bytes < $strlen && $chars < $length) {
webmaster@1: $bytes++;
webmaster@1: $c = ord($text[$bytes]);
webmaster@1: if ($c < 0x80 || $c >= 0xC0) {
webmaster@1: $chars++;
webmaster@1: }
webmaster@1: }
webmaster@1: $bytes--;
webmaster@1: }
webmaster@1: else if ($length < 0) {
webmaster@1: // Count all the continuation bytes from the end until we have found
webmaster@1: // abs($length) characters
webmaster@1: $length = abs($length);
webmaster@1: $bytes = $strlen - 1; $chars = 0;
webmaster@1: while ($bytes >= 0 && $chars < $length) {
webmaster@1: $c = ord($text[$bytes]);
webmaster@1: if ($c < 0x80 || $c >= 0xC0) {
webmaster@1: $chars++;
webmaster@1: }
webmaster@1: $bytes--;
webmaster@1: }
webmaster@1: }
webmaster@1: $iend = $bytes;
webmaster@1:
webmaster@1: return substr($text, $istart, max(0, $iend - $istart + 1));
webmaster@1: }
webmaster@1: }
webmaster@1:
webmaster@1: