View source
<?php
define('UNICODE_ERROR', -1);
define('UNICODE_SINGLEBYTE', 0);
define('UNICODE_MULTIBYTE', 1);
function unicode_check() {
$GLOBALS['multibyte'] = _unicode_check();
}
function _unicode_check($errors = false) {
setlocale(LC_CTYPE, 'C');
if (preg_match('/[à-á]/u', 'â')) {
if ($errors) {
form_set_error('unicode', t('The PCRE library in your PHP installation is outdated. This will cause problems when handling Unicode text. If you are running PHP 4.3.3 or higher, make sure you are using the PCRE library supplied by PHP. Please refer to the <a href="%url">PHP PCRE documentation</a> for more information.', array(
'%url' => 'http://www.php.net/pcre',
)));
}
return UNICODE_ERROR;
}
if (!function_exists('mb_strlen')) {
return UNICODE_SINGLEBYTE;
}
if (ini_get('mbstring.func_overload') != 0) {
if ($errors) {
form_set_error('unicode', 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',
)));
}
return UNICODE_ERROR;
}
if (ini_get('mbstring.encoding_translation') != 0) {
if ($errors) {
form_set_error('unicode', 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',
)));
}
return UNICODE_ERROR;
}
if (ini_get('mbstring.http_input') != 'pass') {
if ($errors) {
form_set_error('unicode', 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',
)));
}
return UNICODE_ERROR;
}
if (ini_get('mbstring.http_output') != 'pass') {
if ($errors) {
form_set_error('unicode', 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',
)));
}
return UNICODE_ERROR;
}
mb_internal_encoding('utf-8');
mb_language('uni');
return UNICODE_MULTIBYTE;
}
function unicode_settings() {
$status = _unicode_check(true);
$options = array(
UNICODE_SINGLEBYTE => t('Standard PHP: 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',
)),
UNICODE_MULTIBYTE => t('Multi-byte: operations on Unicode strings are supported through the <a href="%url">PHP mbstring extension</a>.', array(
'%url' => 'http://www.php.net/mbstring',
)),
UNICODE_ERROR => t('Invalid: the current configuration is incompatible with Drupal.'),
);
$form['settings'] = array(
'#type' => 'item',
'#title' => t('String handling method'),
'#value' => $options[$status],
);
return $form;
}
function drupal_xml_parser_create(&$data) {
$encoding = 'utf-8';
$bom = false;
if (!strncmp($data, "", 3)) {
$bom = true;
$data = substr($data, 3);
}
if (!$bom && ereg('^<\\?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 = ereg_replace('^(<\\?xml[^>]+encoding)="([^"]+)"', '\\1="utf-8"', $out);
}
else {
watchdog('php', t("Could not convert XML encoding '%s' to UTF-8.", array(
'%s' => theme('placeholder', $encoding),
)), WATCHDOG_WARNING);
return 0;
}
}
$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);
}
else {
if (function_exists('mb_convert_encoding')) {
$out = @mb_convert_encoding($data, 'utf-8', $encoding);
}
else {
if (function_exists('recode_string')) {
$out = @recode_string($encoding . '..utf-8', $data);
}
else {
watchdog('php', t("Unsupported encoding '%s'. Please install iconv, GNU recode or mbstring for PHP.", array(
'%s' => $encoding,
)), WATCHDOG_ERROR);
return FALSE;
}
}
}
return $out;
}
function truncate_utf8($string, $len, $wordsafe = FALSE, $dots = FALSE) {
$slen = strlen($string);
if ($slen <= $len) {
return $string;
}
if ($wordsafe) {
$end = $len;
while ($string[--$len] != ' ' && $len > 0) {
}
if ($len == 0) {
$len = $end;
}
}
if (ord($string[$len]) < 0x80 || ord($string[$len]) >= 0xc0) {
return substr($string, 0, $len) . ($dots ? ' ...' : '');
}
while (--$len >= 0 && ord($string[$len]) >= 0x80 && ord($string[$len]) < 0xc0) {
}
return substr($string, 0, $len) . ($dots ? ' ...' : '');
}
function mime_header_encode($string) {
if (preg_match('/[^\\x20-\\x7E]/', $string)) {
$chunk_size = 47;
$len = strlen($string);
$output = '';
while ($len > 0) {
$chunk = truncate_utf8($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 $table;
if (!isset($table)) {
$table = array_flip(get_html_translation_table(HTML_ENTITIES));
$table = array_map('utf8_encode', $table);
$table['''] = "'";
}
$newtable = array_diff($table, $exclude);
return preg_replace('/&(#x?)?([A-Za-z0-9]+);/e', '_decode_entities("$1", "$2", "$0", $newtable, $exclude)', $text);
}
function _decode_entities($prefix, $codepoint, $original, &$table, &$exclude) {
if (!$prefix) {
if (isset($table[$original])) {
return $table[$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);
}
else {
if ($codepoint < 0x800) {
$str = chr(0xc0 | $codepoint >> 6) . chr(0x80 | $codepoint & 0x3f);
}
else {
if ($codepoint < 0x10000) {
$str = chr(0xe0 | $codepoint >> 12) . chr(0x80 | $codepoint >> 6 & 0x3f) . chr(0x80 | $codepoint & 0x3f);
}
else {
if ($codepoint < 0x200000) {
$str = chr(0xf0 | $codepoint >> 18) . chr(0x80 | $codepoint >> 12 & 0x3f) . chr(0x80 | $codepoint >> 6 & 0x3f) . chr(0x80 | $codepoint & 0x3f);
}
}
}
}
if (in_array($str, $exclude)) {
return $original;
}
else {
return $str;
}
}
function drupal_strlen($text) {
global $multibyte;
if ($multibyte == UNICODE_MULTIBYTE) {
return mb_strlen($text);
}
else {
return strlen(preg_replace("", '', $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);
if ($start > 0) {
$bytes = -1;
$chars = -1;
while ($bytes < $strlen && $chars < $start) {
$bytes++;
$c = ord($text[$bytes]);
if ($c < 0x80 || $c >= 0xc0) {
$chars++;
}
}
}
else {
if ($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) {
$bytes = $strlen - 1;
}
else {
if ($length > 0) {
$bytes = $istart;
$chars = 0;
while ($bytes < $strlen && $chars < $length) {
$bytes++;
$c = ord($text[$bytes]);
if ($c < 0x80 || $c >= 0xc0) {
$chars++;
}
}
$bytes--;
}
else {
if ($length < 0) {
$length = abs($length);
$bytes = $strlen - 1;
$chars = 0;
while ($bytes >= 0 && $chars < $length) {
$c = ord($text[$bytes]);
if ($c < 0x80 || $c >= 0xc0) {
$chars++;
}
$bytes--;
}
}
}
}
$iend = $bytes;
return substr($text, $istart, max(0, $iend - $istart + 1));
}
}