PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/unicode.inc

https://bitbucket.org/socialmediaclassroom/socialmediaclassroomd5
Pascal | 508 lines | 237 code | 34 blank | 237 comment | 56 complexity | ec24e9c7eeb75666e329642f3929b9da MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. // $Id: unicode.inc,v 1.23.2.2 2007/05/21 01:09:21 drumm Exp $
  3. /**
  4. * Indicates an error during check for PHP unicode support.
  5. */
  6. define('UNICODE_ERROR', -1);
  7. /**
  8. * Indicates that standard PHP (emulated) unicode support is being used.
  9. */
  10. define('UNICODE_SINGLEBYTE', 0);
  11. /**
  12. * Indicates that full unicode support with the PHP mbstring extension is being
  13. * used.
  14. */
  15. define('UNICODE_MULTIBYTE', 1);
  16. /**
  17. * Wrapper around _unicode_check().
  18. */
  19. function unicode_check() {
  20. list($GLOBALS['multibyte']) = _unicode_check();
  21. }
  22. /**
  23. * Perform checks about Unicode support in PHP, and set the right settings if
  24. * needed.
  25. *
  26. * Because Drupal needs to be able to handle text in various encodings, we do
  27. * not support mbstring function overloading. HTTP input/output conversion must
  28. * be disabled for similar reasons.
  29. *
  30. * @param $errors
  31. * Whether to report any fatal errors with form_set_error().
  32. */
  33. function _unicode_check() {
  34. // Ensure translations don't break at install time
  35. $t = get_t();
  36. // Set the standard C locale to ensure consistent, ASCII-only string handling.
  37. setlocale(LC_CTYPE, 'C');
  38. // Check for outdated PCRE library
  39. // Note: we check if U+E2 is in the range U+E0 - U+E1. This test returns TRUE on old PCRE versions.
  40. if (preg_match('/[à-á]/u', 'â')) {
  41. return array(UNICODE_ERROR, $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')));
  42. }
  43. // Check for mbstring extension
  44. if (!function_exists('mb_strlen')) {
  45. 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')));
  46. }
  47. // Check mbstring configuration
  48. if (ini_get('mbstring.func_overload') != 0) {
  49. 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')));
  50. }
  51. if (ini_get('mbstring.encoding_translation') != 0) {
  52. 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')));
  53. }
  54. if (ini_get('mbstring.http_input') != 'pass') {
  55. 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')));
  56. }
  57. if (ini_get('mbstring.http_output') != 'pass') {
  58. 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')));
  59. }
  60. // Set appropriate configuration
  61. mb_internal_encoding('utf-8');
  62. mb_language('uni');
  63. return array(UNICODE_MULTIBYTE, '');
  64. }
  65. /**
  66. * Return Unicode library status and errors.
  67. */
  68. function unicode_requirements() {
  69. // Ensure translations don't break at install time
  70. $t = get_t();
  71. $libraries = array(
  72. UNICODE_SINGLEBYTE => $t('Standard PHP'),
  73. UNICODE_MULTIBYTE => $t('PHP Mbstring Extension'),
  74. UNICODE_ERROR => $t('Error'),
  75. );
  76. $severities = array(
  77. UNICODE_SINGLEBYTE => REQUIREMENT_WARNING,
  78. UNICODE_MULTIBYTE => REQUIREMENT_OK,
  79. UNICODE_ERROR => REQUIREMENT_ERROR,
  80. );
  81. list($library, $description) = _unicode_check();
  82. $requirements['unicode'] = array(
  83. 'title' => $t('Unicode library'),
  84. 'value' => $libraries[$library],
  85. );
  86. if ($description) {
  87. $requirements['unicode']['description'] = $description;
  88. }
  89. $requirements['unicode']['severity'] = $severities[$library];
  90. return $requirements;
  91. }
  92. /**
  93. * Prepare a new XML parser.
  94. *
  95. * This is a wrapper around xml_parser_create() which extracts the encoding from
  96. * the XML data first and sets the output encoding to UTF-8. This function should
  97. * be used instead of xml_parser_create(), because PHP 4's XML parser doesn't
  98. * check the input encoding itself. "Starting from PHP 5, the input encoding is
  99. * automatically detected, so that the encoding parameter specifies only the
  100. * output encoding."
  101. *
  102. * This is also where unsupported encodings will be converted. Callers should
  103. * take this into account: $data might have been changed after the call.
  104. *
  105. * @param &$data
  106. * The XML data which will be parsed later.
  107. * @return
  108. * An XML parser object.
  109. */
  110. function drupal_xml_parser_create(&$data) {
  111. // Default XML encoding is UTF-8
  112. $encoding = 'utf-8';
  113. $bom = FALSE;
  114. // Check for UTF-8 byte order mark (PHP5's XML parser doesn't handle it).
  115. if (!strncmp($data, "\xEF\xBB\xBF", 3)) {
  116. $bom = TRUE;
  117. $data = substr($data, 3);
  118. }
  119. // Check for an encoding declaration in the XML prolog if no BOM was found.
  120. if (!$bom && ereg('^<\?xml[^>]+encoding="([^"]+)"', $data, $match)) {
  121. $encoding = $match[1];
  122. }
  123. // Unsupported encodings are converted here into UTF-8.
  124. $php_supported = array('utf-8', 'iso-8859-1', 'us-ascii');
  125. if (!in_array(strtolower($encoding), $php_supported)) {
  126. $out = drupal_convert_to_utf8($data, $encoding);
  127. if ($out !== FALSE) {
  128. $encoding = 'utf-8';
  129. $data = ereg_replace('^(<\?xml[^>]+encoding)="([^"]+)"', '\\1="utf-8"', $out);
  130. }
  131. else {
  132. watchdog('php', t("Could not convert XML encoding %s to UTF-8.", array('%s' => $encoding)), WATCHDOG_WARNING);
  133. return 0;
  134. }
  135. }
  136. $xml_parser = xml_parser_create($encoding);
  137. xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, 'utf-8');
  138. return $xml_parser;
  139. }
  140. /**
  141. * Convert data to UTF-8
  142. *
  143. * Requires the iconv, GNU recode or mbstring PHP extension.
  144. *
  145. * @param $data
  146. * The data to be converted.
  147. * @param $encoding
  148. * The encoding that the data is in
  149. * @return
  150. * Converted data or FALSE.
  151. */
  152. function drupal_convert_to_utf8($data, $encoding) {
  153. if (function_exists('iconv')) {
  154. $out = @iconv($encoding, 'utf-8', $data);
  155. }
  156. else if (function_exists('mb_convert_encoding')) {
  157. $out = @mb_convert_encoding($data, 'utf-8', $encoding);
  158. }
  159. else if (function_exists('recode_string')) {
  160. $out = @recode_string($encoding .'..utf-8', $data);
  161. }
  162. else {
  163. watchdog('php', t("Unsupported encoding %s. Please install iconv, GNU recode or mbstring for PHP.", array('%s' => $encoding)), WATCHDOG_ERROR);
  164. return FALSE;
  165. }
  166. return $out;
  167. }
  168. /**
  169. * Truncate a UTF-8-encoded string safely to a number of bytes.
  170. *
  171. * If the end position is in the middle of a UTF-8 sequence, it scans backwards
  172. * until the beginning of the byte sequence.
  173. *
  174. * Use this function whenever you want to chop off a string at an unsure
  175. * location. On the other hand, if you're sure that you're splitting on a
  176. * character boundary (e.g. after using strpos() or similar), you can safely use
  177. * substr() instead.
  178. *
  179. * @param $string
  180. * The string to truncate.
  181. * @param $len
  182. * An upper limit on the returned string length.
  183. * @param $wordsafe
  184. * Flag to truncate at nearest space. Defaults to FALSE.
  185. * @return
  186. * The truncated string.
  187. */
  188. function truncate_utf8($string, $len, $wordsafe = FALSE, $dots = FALSE) {
  189. $slen = strlen($string);
  190. if ($slen <= $len) {
  191. return $string;
  192. }
  193. if ($wordsafe) {
  194. $end = $len;
  195. while (($string[--$len] != ' ') && ($len > 0)) {};
  196. if ($len == 0) {
  197. $len = $end;
  198. }
  199. }
  200. if ((ord($string[$len]) < 0x80) || (ord($string[$len]) >= 0xC0)) {
  201. return substr($string, 0, $len) . ($dots ? ' ...' : '');
  202. }
  203. while (--$len >= 0 && ord($string[$len]) >= 0x80 && ord($string[$len]) < 0xC0) {};
  204. return substr($string, 0, $len) . ($dots ? ' ...' : '');
  205. }
  206. /**
  207. * Encodes MIME/HTTP header values that contain non-ASCII, UTF-8 encoded
  208. * characters.
  209. *
  210. * For example, mime_header_encode('tést.txt') returns "=?UTF-8?B?dMOpc3QudHh0?=".
  211. *
  212. * See http://www.rfc-editor.org/rfc/rfc2047.txt for more information.
  213. *
  214. * Notes:
  215. * - Only encode strings that contain non-ASCII characters.
  216. * - We progressively cut-off a chunk with truncate_utf8(). This is to ensure
  217. * each chunk starts and ends on a character boundary.
  218. * - Using \n as the chunk separator may cause problems on some systems and may
  219. * have to be changed to \r\n or \r.
  220. */
  221. function mime_header_encode($string) {
  222. if (preg_match('/[^\x20-\x7E]/', $string)) {
  223. $chunk_size = 47; // floor((75 - strlen("=?UTF-8?B??=")) * 0.75);
  224. $len = strlen($string);
  225. $output = '';
  226. while ($len > 0) {
  227. $chunk = truncate_utf8($string, $chunk_size);
  228. $output .= ' =?UTF-8?B?'. base64_encode($chunk) ."?=\n";
  229. $c = strlen($chunk);
  230. $string = substr($string, $c);
  231. $len -= $c;
  232. }
  233. return trim($output);
  234. }
  235. return $string;
  236. }
  237. /**
  238. * Complement to mime_header_encode
  239. */
  240. function mime_header_decode($header) {
  241. // First step: encoded chunks followed by other encoded chunks (need to collapse whitespace)
  242. $header = preg_replace_callback('/=\?([^?]+)\?(Q|B)\?([^?]+|\?(?!=))\?=\s+(?==\?)/', '_mime_header_decode', $header);
  243. // Second step: remaining chunks (do not collapse whitespace)
  244. return preg_replace_callback('/=\?([^?]+)\?(Q|B)\?([^?]+|\?(?!=))\?=/', '_mime_header_decode', $header);
  245. }
  246. /**
  247. * Helper function to mime_header_decode
  248. */
  249. function _mime_header_decode($matches) {
  250. // Regexp groups:
  251. // 1: Character set name
  252. // 2: Escaping method (Q or B)
  253. // 3: Encoded data
  254. $data = ($matches[2] == 'B') ? base64_decode($matches[3]) : str_replace('_', ' ', quoted_printable_decode($matches[3]));
  255. if (strtolower($matches[1]) != 'utf-8') {
  256. $data = drupal_convert_to_utf8($data, $matches[1]);
  257. }
  258. return $data;
  259. }
  260. /**
  261. * Decode all HTML entities (including numerical ones) to regular UTF-8 bytes.
  262. * Double-escaped entities will only be decoded once ("&amp;lt;" becomes "&lt;", not "<").
  263. *
  264. * @param $text
  265. * The text to decode entities in.
  266. * @param $exclude
  267. * An array of characters which should not be decoded. For example,
  268. * array('<', '&', '"'). This affects both named and numerical entities.
  269. */
  270. function decode_entities($text, $exclude = array()) {
  271. static $table;
  272. // We store named entities in a table for quick processing.
  273. if (!isset($table)) {
  274. // Get all named HTML entities.
  275. $table = array_flip(get_html_translation_table(HTML_ENTITIES));
  276. // PHP gives us ISO-8859-1 data, we need UTF-8.
  277. $table = array_map('utf8_encode', $table);
  278. // Add apostrophe (XML)
  279. $table['&apos;'] = "'";
  280. }
  281. $newtable = array_diff($table, $exclude);
  282. // Use a regexp to select all entities in one pass, to avoid decoding double-escaped entities twice.
  283. return preg_replace('/&(#x?)?([A-Za-z0-9]+);/e', '_decode_entities("$1", "$2", "$0", $newtable, $exclude)', $text);
  284. }
  285. /**
  286. * Helper function for decode_entities
  287. */
  288. function _decode_entities($prefix, $codepoint, $original, &$table, &$exclude) {
  289. // Named entity
  290. if (!$prefix) {
  291. if (isset($table[$original])) {
  292. return $table[$original];
  293. }
  294. else {
  295. return $original;
  296. }
  297. }
  298. // Hexadecimal numerical entity
  299. if ($prefix == '#x') {
  300. $codepoint = base_convert($codepoint, 16, 10);
  301. }
  302. // Decimal numerical entity (strip leading zeros to avoid PHP octal notation)
  303. else {
  304. $codepoint = preg_replace('/^0+/', '', $codepoint);
  305. }
  306. // Encode codepoint as UTF-8 bytes
  307. if ($codepoint < 0x80) {
  308. $str = chr($codepoint);
  309. }
  310. else if ($codepoint < 0x800) {
  311. $str = chr(0xC0 | ($codepoint >> 6))
  312. . chr(0x80 | ($codepoint & 0x3F));
  313. }
  314. else if ($codepoint < 0x10000) {
  315. $str = chr(0xE0 | ( $codepoint >> 12))
  316. . chr(0x80 | (($codepoint >> 6) & 0x3F))
  317. . chr(0x80 | ( $codepoint & 0x3F));
  318. }
  319. else if ($codepoint < 0x200000) {
  320. $str = chr(0xF0 | ( $codepoint >> 18))
  321. . chr(0x80 | (($codepoint >> 12) & 0x3F))
  322. . chr(0x80 | (($codepoint >> 6) & 0x3F))
  323. . chr(0x80 | ( $codepoint & 0x3F));
  324. }
  325. // Check for excluded characters
  326. if (in_array($str, $exclude)) {
  327. return $original;
  328. }
  329. else {
  330. return $str;
  331. }
  332. }
  333. /**
  334. * Count the amount of characters in a UTF-8 string. This is less than or
  335. * equal to the byte count.
  336. */
  337. function drupal_strlen($text) {
  338. global $multibyte;
  339. if ($multibyte == UNICODE_MULTIBYTE) {
  340. return mb_strlen($text);
  341. }
  342. else {
  343. // Do not count UTF-8 continuation bytes.
  344. return strlen(preg_replace("/[\x80-\xBF]/", '', $text));
  345. }
  346. }
  347. /**
  348. * Uppercase a UTF-8 string.
  349. */
  350. function drupal_strtoupper($text) {
  351. global $multibyte;
  352. if ($multibyte == UNICODE_MULTIBYTE) {
  353. return mb_strtoupper($text);
  354. }
  355. else {
  356. // Use C-locale for ASCII-only uppercase
  357. $text = strtoupper($text);
  358. // Case flip Latin-1 accented letters
  359. $text = preg_replace_callback('/\xC3[\xA0-\xB6\xB8-\xBE]/', '_unicode_caseflip', $text);
  360. return $text;
  361. }
  362. }
  363. /**
  364. * Lowercase a UTF-8 string.
  365. */
  366. function drupal_strtolower($text) {
  367. global $multibyte;
  368. if ($multibyte == UNICODE_MULTIBYTE) {
  369. return mb_strtolower($text);
  370. }
  371. else {
  372. // Use C-locale for ASCII-only lowercase
  373. $text = strtolower($text);
  374. // Case flip Latin-1 accented letters
  375. $text = preg_replace_callback('/\xC3[\x80-\x96\x98-\x9E]/', '_unicode_caseflip', $text);
  376. return $text;
  377. }
  378. }
  379. /**
  380. * Helper function for case conversion of Latin-1.
  381. * Used for flipping U+C0-U+DE to U+E0-U+FD and back.
  382. */
  383. function _unicode_caseflip($matches) {
  384. return $matches[0][0] . chr(ord($matches[0][1]) ^ 32);
  385. }
  386. /**
  387. * Capitalize the first letter of a UTF-8 string.
  388. */
  389. function drupal_ucfirst($text) {
  390. // Note: no mbstring equivalent!
  391. return drupal_strtoupper(drupal_substr($text, 0, 1)) . drupal_substr($text, 1);
  392. }
  393. /**
  394. * Cut off a piece of a string based on character indices and counts. Follows
  395. * the same behaviour as PHP's own substr() function.
  396. *
  397. * Note that for cutting off a string at a known character/substring
  398. * location, the usage of PHP's normal strpos/substr is safe and
  399. * much faster.
  400. */
  401. function drupal_substr($text, $start, $length = NULL) {
  402. global $multibyte;
  403. if ($multibyte == UNICODE_MULTIBYTE) {
  404. return $length === NULL ? mb_substr($text, $start) : mb_substr($text, $start, $length);
  405. }
  406. else {
  407. $strlen = strlen($text);
  408. // Find the starting byte offset
  409. $bytes = 0;
  410. if ($start > 0) {
  411. // Count all the continuation bytes from the start until we have found
  412. // $start characters
  413. $bytes = -1; $chars = -1;
  414. while ($bytes < $strlen && $chars < $start) {
  415. $bytes++;
  416. $c = ord($text[$bytes]);
  417. if ($c < 0x80 || $c >= 0xC0) {
  418. $chars++;
  419. }
  420. }
  421. }
  422. else if ($start < 0) {
  423. // Count all the continuation bytes from the end until we have found
  424. // abs($start) characters
  425. $start = abs($start);
  426. $bytes = $strlen; $chars = 0;
  427. while ($bytes > 0 && $chars < $start) {
  428. $bytes--;
  429. $c = ord($text[$bytes]);
  430. if ($c < 0x80 || $c >= 0xC0) {
  431. $chars++;
  432. }
  433. }
  434. }
  435. $istart = $bytes;
  436. // Find the ending byte offset
  437. if ($length === NULL) {
  438. $bytes = $strlen - 1;
  439. }
  440. else if ($length > 0) {
  441. // Count all the continuation bytes from the starting index until we have
  442. // found $length + 1 characters. Then backtrack one byte.
  443. $bytes = $istart; $chars = 0;
  444. while ($bytes < $strlen && $chars < $length) {
  445. $bytes++;
  446. $c = ord($text[$bytes]);
  447. if ($c < 0x80 || $c >= 0xC0) {
  448. $chars++;
  449. }
  450. }
  451. $bytes--;
  452. }
  453. else if ($length < 0) {
  454. // Count all the continuation bytes from the end until we have found
  455. // abs($length) characters
  456. $length = abs($length);
  457. $bytes = $strlen - 1; $chars = 0;
  458. while ($bytes >= 0 && $chars < $length) {
  459. $c = ord($text[$bytes]);
  460. if ($c < 0x80 || $c >= 0xC0) {
  461. $chars++;
  462. }
  463. $bytes--;
  464. }
  465. }
  466. $iend = $bytes;
  467. return substr($text, $istart, max(0, $iend - $istart + 1));
  468. }
  469. }