unicode.inc
<?php
define('UNICODE_ERROR', -1);
define('UNICODE_SINGLEBYTE', 0);
define('UNICODE_MULTIBYTE', 1);
function unicode_check() {
list($GLOBALS['multibyte']) = _unicode_check();
}
function _unicode_check() {
$t = get_t();
setlocale(LC_CTYPE, 'C');
if (!function_exists('mb_strlen')) {
return array(UNICODE_SINGLEBYTE, $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.', array('@url' => 'http://www.php.net/mbstring')));
}
if (ini_get('mbstring.func_overload') != 0) {
return array(UNICODE_ERROR, $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.', array('@url' => 'http://www.php.net/mbstring')));
}
if (ini_get('mbstring.encoding_translation') != 0) {
return array(UNICODE_ERROR, $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.', array('@url' => 'http://www.php.net/mbstring')));
}
if (ini_get('mbstring.http_input') != 'pass') {
return array(UNICODE_ERROR, $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.', array('@url' => 'http://www.php.net/mbstring')));
}
if (ini_get('mbstring.http_output') != 'pass') {
return array(UNICODE_ERROR, $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.', array('@url' => 'http://www.php.net/mbstring')));
}
mb_internal_encoding('utf-8');
mb_language('uni');
return array(UNICODE_MULTIBYTE, '');
}
function unicode_requirements() {
$t = get_t();
$libraries = array(
UNICODE_SINGLEBYTE => $t('Standard PHP'),
UNICODE_MULTIBYTE => $t('PHP Mbstring Extension'),
UNICODE_ERROR => $t('Error'),
);
$severities = array(
UNICODE_SINGLEBYTE => REQUIREMENT_WARNING,
UNICODE_MULTIBYTE => REQUIREMENT_OK,
UNICODE_ERROR => REQUIREMENT_ERROR,
);
list($library, $description) = _unicode_check();
$requirements['unicode'] = array(
'title' => $t('Unicode library'),
'value' => $libraries[$library],
);
if ($description) {
$requirements['unicode']['description'] = $description;
}
$requirements['unicode']['severity'] = $severities[$library];
return $requirements;
}
function drupal_xml_parser_create(&$data) {
$encoding = 'utf-8';
$bom = FALSE;
if (!strncmp($data, "\xEF\xBB\xBF", 3)) {
$bom = TRUE;
$data = substr($data, 3);
}
if (!$bom && preg_match('/^<\?xml[^>]+encoding="(.+?)"/', $data, $match)) {
$encoding = $match[1];
}
$php_supported = array('utf-8', 'iso-8859-1', 'us-ascii');
if (!in_array(strtolower($encoding), $php_supported)) {
$out = drupal_convert_to_utf8($data, $encoding);
if ($out !== FALSE) {
$encoding = 'utf-8';
$data = preg_replace('/^(<\?xml[^>]+encoding)="(.+?)"/', '\\1="utf-8"', $out);
}
else {
watchdog('php', 'Could not convert XML encoding %s to UTF-8.', array('%s' => $encoding), WATCHDOG_WARNING);
return FALSE;
}
}
$xml_parser = xml_parser_create($encoding);
xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, 'utf-8');
return $xml_parser;
}
function drupal_convert_to_utf8($data, $encoding) {
if (function_exists('iconv')) {
$out = @iconv($encoding, 'utf-8', $data);
}
elseif (function_exists('mb_convert_encoding')) {
$out = @mb_convert_encoding($data, 'utf-8', $encoding);
}
elseif (function_exists('recode_string')) {
$out = @recode_string($encoding . '..utf-8', $data);
}
else {
watchdog('php', 'Unsupported encoding %s. Please install iconv, GNU recode or mbstring for PHP.', array('%s' => $encoding), WATCHDOG_ERROR);
return FALSE;
}
return $out;
}
function drupal_truncate_bytes($string, $len) {
if (strlen($string) <= $len) {
return $string;
}
if ((ord($string[$len]) < 0x80) || (ord($string[$len]) >= 0xC0)) {
return substr($string, 0, $len);
}
while (--$len >= 0 && ord($string[$len]) >= 0x80 && ord($string[$len]) < 0xC0);
return substr($string, 0, $len);
}
function truncate_utf8($string, $len, $wordsafe = FALSE, $dots = FALSE) {
if (drupal_strlen($string) <= $len) {
return $string;
}
if ($dots) {
$len -= 4;
}
if ($wordsafe) {
$string = drupal_substr($string, 0, $len + 1); if ($last_space = strrpos($string, ' ')) { $string = substr($string, 0, $last_space);
}
else {
$string = drupal_substr($string, 0, $len);
}
}
else {
$string = drupal_substr($string, 0, $len);
}
if ($dots) {
$string .= ' ...';
}
return $string;
}
function mime_header_encode($string) {
if (preg_match('/[^\x20-\x7E]/', $string)) {
$chunk_size = 47; $len = strlen($string);
$output = '';
while ($len > 0) {
$chunk = drupal_truncate_bytes($string, $chunk_size);
$output .= ' =?UTF-8?B?' . base64_encode($chunk) . "?=\n";
$c = strlen($chunk);
$string = substr($string, $c);
$len -= $c;
}
return trim($output);
}
return $string;
}
function mime_header_decode($header) {
$header = preg_replace_callback('/=\?([^?]+)\?(Q|B)\?([^?]+|\?(?!=))\?=\s+(?==\?)/', '_mime_header_decode', $header);
return preg_replace_callback('/=\?([^?]+)\?(Q|B)\?([^?]+|\?(?!=))\?=/', '_mime_header_decode', $header);
}
function _mime_header_decode($matches) {
$data = ($matches[2] == 'B') ? base64_decode($matches[3]) : str_replace('_', ' ', quoted_printable_decode($matches[3]));
if (strtolower($matches[1]) != 'utf-8') {
$data = drupal_convert_to_utf8($data, $matches[1]);
}
return $data;
}
function decode_entities($text, $exclude = array()) {
static $html_entities;
if (!isset($html_entities)) {
include DRUPAL_ROOT . '/includes/unicode.entities.inc';
}
$exclude = array_flip($exclude);
return preg_replace('/&(#x?)?([A-Za-z0-9]+);/e', '_decode_entities("$1", "$2", "$0", $html_entities, $exclude)', $text);
}
function _decode_entities($prefix, $codepoint, $original, &$html_entities, &$exclude) {
if (!$prefix) {
if (isset($html_entities[$original]) && !isset($exclude[$html_entities[$original]])) {
return $html_entities[$original];
}
else {
return $original;
}
}
if ($prefix == '#x') {
$codepoint = base_convert($codepoint, 16, 10);
}
else {
$codepoint = preg_replace('/^0+/', '', $codepoint);
}
if ($codepoint < 0x80) {
$str = chr($codepoint);
}
elseif ($codepoint < 0x800) {
$str = chr(0xC0 | ($codepoint >> 6))
. chr(0x80 | ($codepoint & 0x3F));
}
elseif ($codepoint < 0x10000) {
$str = chr(0xE0 | ( $codepoint >> 12))
. chr(0x80 | (($codepoint >> 6) & 0x3F))
. chr(0x80 | ( $codepoint & 0x3F));
}
elseif ($codepoint < 0x200000) {
$str = chr(0xF0 | ( $codepoint >> 18))
. chr(0x80 | (($codepoint >> 12) & 0x3F))
. chr(0x80 | (($codepoint >> 6) & 0x3F))
. chr(0x80 | ( $codepoint & 0x3F));
}
if (isset($exclude[$str])) {
return $original;
}
else {
return $str;
}
}
function drupal_strlen($text) {
global $multibyte;
if ($multibyte == UNICODE_MULTIBYTE) {
return mb_strlen($text);
}
else {
return strlen(preg_replace("/[\x80-\xBF]/", '', $text));
}
}
function drupal_strtoupper($text) {
global $multibyte;
if ($multibyte == UNICODE_MULTIBYTE) {
return mb_strtoupper($text);
}
else {
$text = strtoupper($text);
$text = preg_replace_callback('/\xC3[\xA0-\xB6\xB8-\xBE]/', '_unicode_caseflip', $text);
return $text;
}
}
function drupal_strtolower($text) {
global $multibyte;
if ($multibyte == UNICODE_MULTIBYTE) {
return mb_strtolower($text);
}
else {
$text = strtolower($text);
$text = preg_replace_callback('/\xC3[\x80-\x96\x98-\x9E]/', '_unicode_caseflip', $text);
return $text;
}
}
function _unicode_caseflip($matches) {
return $matches[0][0] . chr(ord($matches[0][1]) ^ 32);
}
function drupal_ucfirst($text) {
return drupal_strtoupper(drupal_substr($text, 0, 1)) . drupal_substr($text, 1);
}
function drupal_substr($text, $start, $length = NULL) {
global $multibyte;
if ($multibyte == UNICODE_MULTIBYTE) {
return $length === NULL ? mb_substr($text, $start) : mb_substr($text, $start, $length);
}
else {
$strlen = strlen($text);
$bytes = 0;
if ($start > 0) {
$bytes = -1; $chars = -1;
while ($bytes < $strlen - 1 && $chars < $start) {
$bytes++;
$c = ord($text[$bytes]);
if ($c < 0x80 || $c >= 0xC0) {
$chars++;
}
}
}
elseif ($start < 0) {
$start = abs($start);
$bytes = $strlen; $chars = 0;
while ($bytes > 0 && $chars < $start) {
$bytes--;
$c = ord($text[$bytes]);
if ($c < 0x80 || $c >= 0xC0) {
$chars++;
}
}
}
$istart = $bytes;
if ($length === NULL) {
$iend = $strlen;
}
elseif ($length > 0) {
$iend = $istart - 1; $chars = -1;
while ($iend < $strlen - 1 && $chars < $length) {
$iend++;
$c = ord($text[$iend]);
if ($c < 0x80 || $c >= 0xC0) {
$chars++;
}
}
if ($iend < $strlen - 1) {
$iend--;
}
}
elseif ($length < 0) {
$length = abs($length);
$iend = $strlen; $chars = 0;
while ($iend > 0 && $chars < $length) {
$iend--;
$c = ord($text[$iend]);
if ($c < 0x80 || $c >= 0xC0) {
$chars++;
}
}
if ($iend > 0) {
$iend--;
}
}
else {
$iend = $istart - 1;
}
return substr($text, $istart, max(0, $iend - $istart + 1));
}
}