| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
define('UNICODE_ERROR', -1);
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
define('UNICODE_SINGLEBYTE', 0);
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
define('UNICODE_MULTIBYTE', 1);
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
function unicode_check() {
|
| 24 |
list($GLOBALS['multibyte']) = _unicode_check();
|
| 25 |
}
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
function _unicode_check() {
|
| 39 |
|
| 40 |
$t = get_t();
|
| 41 |
|
| 42 |
|
| 43 |
setlocale(LC_CTYPE, 'C');
|
| 44 |
|
| 45 |
|
| 46 |
if (!function_exists('mb_strlen')) {
|
| 47 |
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')));
|
| 48 |
}
|
| 49 |
|
| 50 |
|
| 51 |
if (ini_get('mbstring.func_overload') != 0) {
|
| 52 |
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')));
|
| 53 |
}
|
| 54 |
if (ini_get('mbstring.encoding_translation') != 0) {
|
| 55 |
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')));
|
| 56 |
}
|
| 57 |
if (ini_get('mbstring.http_input') != 'pass') {
|
| 58 |
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')));
|
| 59 |
}
|
| 60 |
if (ini_get('mbstring.http_output') != 'pass') {
|
| 61 |
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')));
|
| 62 |
}
|
| 63 |
|
| 64 |
|
| 65 |
mb_internal_encoding('utf-8');
|
| 66 |
mb_language('uni');
|
| 67 |
return array(UNICODE_MULTIBYTE, '');
|
| 68 |
}
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
function unicode_requirements() {
|
| 74 |
|
| 75 |
$t = get_t();
|
| 76 |
|
| 77 |
|
| 78 |
UNICODE_SINGLEBYTE => $t('Standard PHP'),
|
| 79 |
UNICODE_MULTIBYTE => $t('PHP Mbstring Extension'),
|
| 80 |
UNICODE_ERROR => $t('Error'),
|
| 81 |
);
|
| 82 |
|
| 83 |
UNICODE_SINGLEBYTE => REQUIREMENT_WARNING,
|
| 84 |
UNICODE_MULTIBYTE => REQUIREMENT_OK,
|
| 85 |
UNICODE_ERROR => REQUIREMENT_ERROR,
|
| 86 |
);
|
| 87 |
list($library, $description) = _unicode_check();
|
| 88 |
|
| 89 |
$requirements['unicode'] = array(
|
| 90 |
'title' => $t('Unicode library'),
|
| 91 |
'value' => $libraries[$library],
|
| 92 |
|
| 93 |
if ($description) {
|
| 94 |
$requirements['unicode']['description'] = $description;
|
| 95 |
}
|
| 96 |
|
| 97 |
$requirements['unicode']['severity'] = $severities[$library];
|
| 98 |
|
| 99 |
return $requirements;
|
| 100 |
}
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
function drupal_xml_parser_create(&$data) {
|
| 121 |
|
| 122 |
$encoding = 'utf-8';
|
| 123 |
$bom = FALSE;
|
| 124 |
|
| 125 |
|
| 126 |
if (!strncmp($data, "\xEF\xBB\xBF", 3)) {
|
| 127 |
$bom = TRUE;
|
| 128 |
$data = substr($data, 3);
|
| 129 |
}
|
| 130 |
|
| 131 |
|
| 132 |
if (!$bom && preg_match('/^<\?xml[^>]+encoding="(.+?)"/', $data, $match)) {
|
| 133 |
$encoding = $match[1];
|
| 134 |
}
|
| 135 |
|
| 136 |
|
| 137 |
$php_supported = array('utf-8', 'iso-8859-1', 'us-ascii');
|
| 138 |
if (!in_array(strtolower($encoding), $php_supported)) {
|
| 139 |
$out = drupal_convert_to_utf8($data, $encoding);
|
| 140 |
if ($out !== FALSE) {
|
| 141 |
$encoding = 'utf-8';
|
| 142 |
$data = preg_replace('/^(<\?xml[^>]+encoding)="(.+?)"/', '\\1="utf-8"', $out);
|
| 143 |
}
|
| 144 |
|
| 145 |
watchdog('php', 'Could not convert XML encoding %s to UTF-8.', array('%s' => $encoding), WATCHDOG_WARNING);
|
| 146 |
return FALSE;
|
| 147 |
|
| 148 |
}
|
| 149 |
|
| 150 |
$xml_parser = xml_parser_create($encoding);
|
| 151 |
xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, 'utf-8');
|
| 152 |
return $xml_parser;
|
| 153 |
}
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
function drupal_convert_to_utf8($data, $encoding) {
|
| 168 |
if (function_exists('iconv')) {
|
| 169 |
$out = @iconv($encoding, 'utf-8', $data);
|
| 170 |
}
|
| 171 |
elseif (function_exists('mb_convert_encoding')) {
|
| 172 |
$out = @mb_convert_encoding($data, 'utf-8', $encoding);
|
| 173 |
}
|
| 174 |
elseif (function_exists('recode_string')) {
|
| 175 |
$out = @recode_string($encoding . '..utf-8', $data);
|
| 176 |
}
|
| 177 |
|
| 178 |
watchdog('php', 'Unsupported encoding %s. Please install iconv, GNU recode or mbstring for PHP.', array('%s' => $encoding), WATCHDOG_ERROR);
|
| 179 |
return FALSE;
|
| 180 |
|
| 181 |
|
| 182 |
return $out;
|
| 183 |
}
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
function drupal_truncate_bytes($string, $len) {
|
| 204 |
if (strlen($string) <= $len) {
|
| 205 |
return $string;
|
| 206 |
}
|
| 207 |
if ((ord($string[$len]) < 0x80) || (ord($string[$len]) >= 0xC0)) {
|
| 208 |
return substr($string, 0, $len);
|
| 209 |
}
|
| 210 |
|
| 211 |
while (--$len >= 0 && ord($string[$len]) >= 0x80 && ord($string[$len]) < 0xC0);
|
| 212 |
|
| 213 |
return substr($string, 0, $len);
|
| 214 |
}
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
function truncate_utf8($string, $len, $wordsafe = FALSE, $dots = FALSE) {
|
| 231 |
|
| 232 |
if (drupal_strlen($string) <= $len) {
|
| 233 |
return $string;
|
| 234 |
}
|
| 235 |
|
| 236 |
if ($dots) {
|
| 237 |
$len -= 4;
|
| 238 |
}
|
| 239 |
|
| 240 |
if ($wordsafe) {
|
| 241 |
$string = drupal_substr($string, 0, $len + 1); // leave one more character
|
| 242 |
if ($last_space = strrpos($string, ' ')) { // space exists AND is not on position 0
|
| 243 |
$string = substr($string, 0, $last_space);
|
| 244 |
}
|
| 245 |
|
| 246 |
$string = drupal_substr($string, 0, $len);
|
| 247 |
|
| 248 |
}
|
| 249 |
|
| 250 |
$string = drupal_substr($string, 0, $len);
|
| 251 |
|
| 252 |
|
| 253 |
if ($dots) {
|
| 254 |
$string .= ' ...';
|
| 255 |
}
|
| 256 |
|
| 257 |
return $string;
|
| 258 |
}
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
function mime_header_encode($string) {
|
| 276 |
if (preg_match('/[^\x20-\x7E]/', $string)) {
|
| 277 |
$chunk_size = 47; // floor((75 - strlen("=?UTF-8?B??=")) * 0.75);
|
| 278 |
$len = strlen($string);
|
| 279 |
$output = '';
|
| 280 |
while ($len > 0) {
|
| 281 |
$chunk = drupal_truncate_bytes($string, $chunk_size);
|
| 282 |
$output .= ' =?UTF-8?B?' . base64_encode($chunk) . "?=\n";
|
| 283 |
$c = strlen($chunk);
|
| 284 |
$string = substr($string, $c);
|
| 285 |
$len -= $c;
|
| 286 |
}
|
| 287 |
return trim($output);
|
| 288 |
}
|
| 289 |
return $string;
|
| 290 |
}
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
function mime_header_decode($header) {
|
| 296 |
|
| 297 |
$header = preg_replace_callback('/=\?([^?]+)\?(Q|B)\?([^?]+|\?(?!=))\?=\s+(?==\?)/', '_mime_header_decode', $header);
|
| 298 |
|
| 299 |
return preg_replace_callback('/=\?([^?]+)\?(Q|B)\?([^?]+|\?(?!=))\?=/', '_mime_header_decode', $header);
|
| 300 |
}
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
function _mime_header_decode($matches) {
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
$data = ($matches[2] == 'B') ? base64_decode($matches[3]) : str_replace('_', ' ', quoted_printable_decode($matches[3]));
|
| 311 |
if (strtolower($matches[1]) != 'utf-8') {
|
| 312 |
$data = drupal_convert_to_utf8($data, $matches[1]);
|
| 313 |
}
|
| 314 |
return $data;
|
| 315 |
}
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
function decode_entities($text, $exclude = array()) {
|
| 328 |
static $html_entities;
|
| 329 |
if (!isset($html_entities)) {
|
| 330 |
include DRUPAL_ROOT . '/includes/unicode.entities.inc';
|
| 331 |
}
|
| 332 |
|
| 333 |
|
| 334 |
$exclude = array_flip($exclude);
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
return preg_replace('/&(#x?)?([A-Za-z0-9]+);/e', '_decode_entities("$1", "$2", "$0", $html_entities, $exclude)', $text);
|
| 341 |
}
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
function _decode_entities($prefix, $codepoint, $original, &$html_entities, &$exclude) {
|
| 347 |
|
| 348 |
if (!$prefix) {
|
| 349 |
|
| 350 |
if (isset($html_entities[$original]) && !isset($exclude[$html_entities[$original]])) {
|
| 351 |
return $html_entities[$original];
|
| 352 |
}
|
| 353 |
|
| 354 |
return $original;
|
| 355 |
|
| 356 |
}
|
| 357 |
|
| 358 |
if ($prefix == '#x') {
|
| 359 |
$codepoint = base_convert($codepoint, 16, 10);
|
| 360 |
}
|
| 361 |
|
| 362 |
|
| 363 |
$codepoint = preg_replace('/^0+/', '', $codepoint);
|
| 364 |
|
| 365 |
|
| 366 |
if ($codepoint < 0x80) {
|
| 367 |
$str = chr($codepoint);
|
| 368 |
}
|
| 369 |
elseif ($codepoint < 0x800) {
|
| 370 |
$str = chr(0xC0 | ($codepoint >> 6))
|
| 371 |
. chr(0x80 | ($codepoint & 0x3F));
|
| 372 |
}
|
| 373 |
elseif ($codepoint < 0x10000) {
|
| 374 |
$str = chr(0xE0 | ( $codepoint >> 12))
|
| 375 |
. chr(0x80 | (($codepoint >> 6) & 0x3F))
|
| 376 |
. chr(0x80 | ( $codepoint & 0x3F));
|
| 377 |
}
|
| 378 |
elseif ($codepoint < 0x200000) {
|
| 379 |
$str = chr(0xF0 | ( $codepoint >> 18))
|
| 380 |
. chr(0x80 | (($codepoint >> 12) & 0x3F))
|
| 381 |
. chr(0x80 | (($codepoint >> 6) & 0x3F))
|
| 382 |
. chr(0x80 | ( $codepoint & 0x3F));
|
| 383 |
}
|
| 384 |
|
| 385 |
if (isset($exclude[$str])) {
|
| 386 |
return $original;
|
| 387 |
}
|
| 388 |
|
| 389 |
return $str;
|
| 390 |
|
| 391 |
}
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
function drupal_strlen($text) {
|
| 398 |
global $multibyte;
|
| 399 |
if ($multibyte == UNICODE_MULTIBYTE) {
|
| 400 |
return mb_strlen($text);
|
| 401 |
}
|
| 402 |
|
| 403 |
|
| 404 |
return strlen(preg_replace("/[\x80-\xBF]/", '', $text));
|
| 405 |
|
| 406 |
}
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
function drupal_strtoupper($text) {
|
| 412 |
global $multibyte;
|
| 413 |
if ($multibyte == UNICODE_MULTIBYTE) {
|
| 414 |
return mb_strtoupper($text);
|
| 415 |
}
|
| 416 |
|
| 417 |
|
| 418 |
$text = strtoupper($text);
|
| 419 |
|
| 420 |
$text = preg_replace_callback('/\xC3[\xA0-\xB6\xB8-\xBE]/', '_unicode_caseflip', $text);
|
| 421 |
return $text;
|
| 422 |
|
| 423 |
}
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
function drupal_strtolower($text) {
|
| 429 |
global $multibyte;
|
| 430 |
if ($multibyte == UNICODE_MULTIBYTE) {
|
| 431 |
return mb_strtolower($text);
|
| 432 |
}
|
| 433 |
|
| 434 |
|
| 435 |
$text = strtolower($text);
|
| 436 |
|
| 437 |
$text = preg_replace_callback('/\xC3[\x80-\x96\x98-\x9E]/', '_unicode_caseflip', $text);
|
| 438 |
return $text;
|
| 439 |
|
| 440 |
}
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
function _unicode_caseflip($matches) {
|
| 447 |
return $matches[0][0] . chr(ord($matches[0][1]) ^ 32);
|
| 448 |
}
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
function drupal_ucfirst($text) {
|
| 454 |
|
| 455 |
return drupal_strtoupper(drupal_substr($text, 0, 1)) . drupal_substr($text, 1);
|
| 456 |
}
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
function drupal_substr($text, $start, $length = NULL) {
|
| 467 |
global $multibyte;
|
| 468 |
if ($multibyte == UNICODE_MULTIBYTE) {
|
| 469 |
return $length === NULL ? mb_substr($text, $start) : mb_substr($text, $start, $length);
|
| 470 |
}
|
| 471 |
|
| 472 |
$strlen = strlen($text);
|
| 473 |
|
| 474 |
$bytes = 0;
|
| 475 |
if ($start > 0) {
|
| 476 |
|
| 477 |
|
| 478 |
$bytes = -1; $chars = -1;
|
| 479 |
while ($bytes < $strlen - 1 && $chars < $start) {
|
| 480 |
$bytes++;
|
| 481 |
$c = ord($text[$bytes]);
|
| 482 |
if ($c < 0x80 || $c >= 0xC0) {
|
| 483 |
$chars++;
|
| 484 |
}
|
| 485 |
}
|
| 486 |
}
|
| 487 |
elseif ($start < 0) {
|
| 488 |
|
| 489 |
|
| 490 |
$start = abs($start);
|
| 491 |
$bytes = $strlen; $chars = 0;
|
| 492 |
while ($bytes > 0 && $chars < $start) {
|
| 493 |
$bytes--;
|
| 494 |
$c = ord($text[$bytes]);
|
| 495 |
if ($c < 0x80 || $c >= 0xC0) {
|
| 496 |
$chars++;
|
| 497 |
}
|
| 498 |
}
|
| 499 |
}
|
| 500 |
$istart = $bytes;
|
| 501 |
|
| 502 |
|
| 503 |
if ($length === NULL) {
|
| 504 |
$iend = $strlen;
|
| 505 |
}
|
| 506 |
elseif ($length > 0) {
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
$iend = $istart - 1; $chars = -1;
|
| 511 |
while ($iend < $strlen - 1 && $chars < $length) {
|
| 512 |
$iend++;
|
| 513 |
$c = ord($text[$iend]);
|
| 514 |
if ($c < 0x80 || $c >= 0xC0) {
|
| 515 |
$chars++;
|
| 516 |
}
|
| 517 |
}
|
| 518 |
|
| 519 |
if ($iend < $strlen - 1) {
|
| 520 |
$iend--;
|
| 521 |
}
|
| 522 |
}
|
| 523 |
elseif ($length < 0) {
|
| 524 |
|
| 525 |
|
| 526 |
$length = abs($length);
|
| 527 |
$iend = $strlen; $chars = 0;
|
| 528 |
while ($iend > 0 && $chars < $length) {
|
| 529 |
$iend--;
|
| 530 |
$c = ord($text[$iend]);
|
| 531 |
if ($c < 0x80 || $c >= 0xC0) {
|
| 532 |
$chars++;
|
| 533 |
}
|
| 534 |
}
|
| 535 |
|
| 536 |
if ($iend > 0) {
|
| 537 |
$iend--;
|
| 538 |
}
|
| 539 |
}
|
| 540 |
|
| 541 |
|
| 542 |
$iend = $istart - 1;
|
| 543 |
|
| 544 |
|
| 545 |
return substr($text, $istart, max(0, $iend - $istart + 1));
|
| 546 |
|
| 547 |
}
|
| 548 |
|
| 549 |
|
| 550 |
|