PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/patchwork/utf8/src/Patchwork/PHP/Shim/Mbstring.php

https://gitlab.com/xolotsoft/pumasruiz
PHP | 562 lines | 415 code | 94 blank | 53 comment | 73 complexity | 1634cc7cf661ee8a025d73853f3986a6 MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright (C) 2013 Nicolas Grekas - p@tchwork.com
  4. *
  5. * This library is free software; you can redistribute it and/or modify it
  6. * under the terms of the (at your option):
  7. * Apache License v2.0 (http://apache.org/licenses/LICENSE-2.0.txt), or
  8. * GNU General Public License v2.0 (http://gnu.org/licenses/gpl-2.0.txt).
  9. */
  10. namespace Patchwork\PHP\Shim;
  11. /**
  12. * Partial mbstring implementation in PHP, iconv based, UTF-8 centric.
  13. *
  14. * Implemented:
  15. * - mb_convert_encoding - Convert character encoding
  16. * - mb_decode_mimeheader - Decode string in MIME header field
  17. * - mb_encode_mimeheader - Encode string for MIME header XXX NATIVE IMPLEMENTATION IS REALLY BUGGED
  18. * - mb_convert_case - Perform case folding on a string
  19. * - mb_get_info - Get internal settings of mbstring
  20. * - mb_http_input - Detect HTTP input character encoding
  21. * - mb_http_output - Set/Get HTTP output character encoding
  22. * - mb_internal_encoding - Set/Get internal character encoding
  23. * - mb_list_encodings - Returns an array of all supported encodings
  24. * - mb_output_handler - Callback function converts character encoding in output buffer
  25. * - mb_strlen - Get string length
  26. * - mb_strpos - Find position of first occurrence of string in a string
  27. * - mb_strrpos - Find position of last occurrence of a string in a string
  28. * - mb_strtolower - Make a string lowercase
  29. * - mb_strtoupper - Make a string uppercase
  30. * - mb_substitute_character - Set/Get substitution character
  31. * - mb_substr - Get part of string
  32. * - mb_stripos - Finds position of first occurrence of a string within another, case insensitive
  33. * - mb_stristr - Finds first occurrence of a string within another, case insensitive
  34. * - mb_strrchr - Finds the last occurrence of a character in a string within another
  35. * - mb_strrichr - Finds the last occurrence of a character in a string within another, case insensitive
  36. * - mb_strripos - Finds position of last occurrence of a string within another, case insensitive
  37. * - mb_strstr - Finds first occurrence of a string within anothers
  38. * - mb_strwidth - Return width of string
  39. * - mb_substr_count - Count the number of substring occurrences
  40. *
  41. * Not implemented:
  42. * - mb_convert_kana - Convert "kana" one from another ("zen-kaku", "han-kaku" and more)
  43. * - mb_convert_variables - Convert character code in variable(s)
  44. * - mb_decode_numericentity - Decode HTML numeric string reference to character
  45. * - mb_encode_numericentity - Encode character to HTML numeric string reference
  46. * - mb_ereg_* - Regular expression with multibyte support
  47. * - mb_parse_str - Parse GET/POST/COOKIE data and set global variable
  48. * - mb_preferred_mime_name - Get MIME charset string
  49. * - mb_regex_encoding - Returns current encoding for multibyte regex as string
  50. * - mb_regex_set_options - Set/Get the default options for mbregex functions
  51. * - mb_send_mail - Send encoded mail
  52. * - mb_split - Split multibyte string using regular expression
  53. * - mb_strcut - Get part of string
  54. * - mb_strimwidth - Get truncated string with specified width
  55. */
  56. class Mbstring
  57. {
  58. const MB_CASE_FOLD = PHP_INT_MAX;
  59. protected static $encoding_list = array('ASCII', 'UTF-8');
  60. protected static $language = 'neutral';
  61. protected static $internal_encoding = 'UTF-8';
  62. protected static $caseFold = array(
  63. array('µ','ſ',"\xCD\x85",'ς',"\xCF\x90","\xCF\x91","\xCF\x95","\xCF\x96","\xCF\xB0","\xCF\xB1","\xCF\xB5","\xE1\xBA\x9B","\xE1\xBE\xBE"),
  64. array('μ','s','ι', 'σ','β', 'θ', 'φ', 'π', 'κ', 'ρ', 'ε', "\xE1\xB9\xA1",'ι'),
  65. );
  66. public static function mb_convert_encoding($s, $to_encoding, $from_encoding = INF)
  67. {
  68. INF === $from_encoding and $from_encoding = self::$internal_encoding;
  69. $from_encoding = strtolower($from_encoding);
  70. $to_encoding = strtolower($to_encoding);
  71. if ('base64' === $from_encoding) {
  72. $s = base64_decode($s);
  73. $from_encoding = $to_encoding;
  74. }
  75. if ('base64' === $to_encoding) {
  76. return base64_encode($s);
  77. }
  78. if ('html-entities' === $to_encoding) {
  79. 'html-entities' === $from_encoding and $from_encoding = 'Windows-1252';
  80. if ('utf-8' !== $from_encoding && 'utf8' !== $from_encoding) {
  81. $s = iconv($from_encoding, 'UTF-8//IGNORE', $s);
  82. }
  83. return preg_replace_callback('/[\x80-\xFF]+/', array(__CLASS__, 'html_encoding_callback'), $s);
  84. }
  85. if ('html-entities' === $from_encoding) {
  86. $s = html_entity_decode($s, ENT_COMPAT, 'UTF-8');
  87. $from_encoding = 'UTF-8';
  88. }
  89. return iconv($from_encoding, $to_encoding.'//IGNORE', $s);
  90. }
  91. public static function mb_decode_mimeheader($s)
  92. {
  93. return iconv_mime_decode($s, 2, self::$internal_encoding.'//IGNORE');
  94. }
  95. public static function mb_encode_mimeheader($s, $charset = INF, $transfer_encoding = INF, $linefeed = INF, $indent = INF)
  96. {
  97. user_error('mb_encode_mimeheader() is bugged. Please use iconv_mime_encode() instead', E_USER_WARNING);
  98. }
  99. public static function mb_convert_case($s, $mode, $encoding = INF)
  100. {
  101. if ('' === $s .= '') {
  102. return '';
  103. }
  104. if (INF === $encoding) {
  105. $encoding = self::$internal_encoding;
  106. } else {
  107. $encoding = strtoupper($encoding);
  108. }
  109. if ('UTF-8' === $encoding || 'UTF8' === $encoding) {
  110. $encoding = INF;
  111. } else {
  112. $s = iconv($encoding, 'UTF-8//IGNORE', $s);
  113. }
  114. if (MB_CASE_TITLE == $mode) {
  115. $s = preg_replace_callback('/\b\p{Ll}/u', array(__CLASS__, 'title_case_upper'), $s);
  116. $s = preg_replace_callback('/\B[\p{Lu}\p{Lt}]+/u', array(__CLASS__, 'title_case_lower'), $s);
  117. } else {
  118. if (MB_CASE_UPPER == $mode) {
  119. static $upper;
  120. isset($upper) or $upper = static::getData('upperCase');
  121. $map = $upper;
  122. } else {
  123. if (self::MB_CASE_FOLD === $mode) {
  124. $s = str_replace(self::$caseFold[0], self::$caseFold[1], $s);
  125. }
  126. static $lower;
  127. isset($lower) or $lower = static::getData('lowerCase');
  128. $map = $lower;
  129. }
  130. static $ulen_mask = array("\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4);
  131. $i = 0;
  132. $len = strlen($s);
  133. while ($i < $len) {
  134. $ulen = $s[$i] < "\x80" ? 1 : $ulen_mask[$s[$i] & "\xF0"];
  135. $uchr = substr($s, $i, $ulen);
  136. $i += $ulen;
  137. if (isset($map[$uchr])) {
  138. $uchr = $map[$uchr];
  139. $nlen = strlen($uchr);
  140. if ($nlen == $ulen) {
  141. $nlen = $i;
  142. do {
  143. $s[--$nlen] = $uchr[--$ulen];
  144. } while ($ulen);
  145. } else {
  146. $s = substr_replace($s, $uchr, $i - $ulen, $ulen);
  147. $len += $nlen - $ulen;
  148. $i += $nlen - $ulen;
  149. }
  150. }
  151. }
  152. }
  153. if (INF === $encoding) {
  154. return $s;
  155. } else {
  156. return iconv('UTF-8', $encoding, $s);
  157. }
  158. }
  159. public static function mb_internal_encoding($encoding = INF)
  160. {
  161. if (INF === $encoding) {
  162. return self::$internal_encoding;
  163. } else {
  164. $encoding = strtoupper($encoding);
  165. }
  166. if ('UTF-8' === $encoding || 'UTF8' === $encoding || false !== @iconv($encoding, $encoding, ' ')) {
  167. self::$internal_encoding = 'UTF8' === $encoding ? 'UTF-8' : $encoding;
  168. return true;
  169. }
  170. return false;
  171. }
  172. public static function mb_language($lang = INF)
  173. {
  174. if (INF === $lang) {
  175. return self::$language;
  176. }
  177. switch ($lang = strtolower($lang)) {
  178. case 'uni':
  179. case 'neutral':
  180. self::$language = $lang;
  181. return true;
  182. }
  183. return false;
  184. }
  185. public static function mb_list_encodings()
  186. {
  187. return array('UTF-8');
  188. }
  189. public static function mb_encoding_aliases($encoding)
  190. {
  191. switch (strtolower($encoding)) {
  192. case 'utf8':
  193. case 'utf-8':
  194. return array('utf8');
  195. }
  196. return false;
  197. }
  198. public static function mb_check_encoding($var = INF, $encoding = INF)
  199. {
  200. if (INF === $encoding) {
  201. if (INF === $var) {
  202. return false;
  203. }
  204. $encoding = self::$internal_encoding;
  205. }
  206. return false !== mb_detect_encoding($var, array($encoding), true);
  207. }
  208. public static function mb_detect_encoding($str, $encoding_list = INF, $strict = false)
  209. {
  210. if (INF === $encoding_list) {
  211. $encoding_list = self::$encoding_list;
  212. } else {
  213. if (!is_array($encoding_list)) {
  214. $encoding_list = array_map('trim', explode(',', $encoding_list));
  215. }
  216. $encoding_list = array_map('strtoupper', $encoding_list);
  217. }
  218. foreach ($encoding_list as $enc) {
  219. switch ($enc) {
  220. case 'ASCII':
  221. if (!preg_match('/[\x80-\xFF]/', $str)) {
  222. return $enc;
  223. }
  224. break;
  225. case 'UTF8':
  226. case 'UTF-8':
  227. if (preg_match('//u', $str)) {
  228. return $enc;
  229. }
  230. break;
  231. default:
  232. return strncmp($enc, 'ISO-8859-', 9) ? false : $enc;
  233. }
  234. }
  235. return false;
  236. }
  237. public static function mb_detect_order($encoding_list = INF)
  238. {
  239. if (INF === $encoding_list) {
  240. return self::$encoding_list;
  241. }
  242. if (!is_array($encoding_list)) {
  243. $encoding_list = array_map('trim', explode(',', $encoding_list));
  244. }
  245. $encoding_list = array_map('strtoupper', $encoding_list);
  246. foreach ($encoding_list as $enc) {
  247. switch ($enc) {
  248. default:
  249. if (strncmp($enc, 'ISO-8859-', 9)) {
  250. return false;
  251. }
  252. case 'ASCII':
  253. case 'UTF8':
  254. case 'UTF-8':
  255. }
  256. }
  257. self::$encoding_list = $encoding_list;
  258. return true;
  259. }
  260. public static function mb_strlen($s, $encoding = INF)
  261. {
  262. INF === $encoding and $encoding = self::$internal_encoding;
  263. return iconv_strlen($s, $encoding.'//IGNORE');
  264. }
  265. public static function mb_strpos($haystack, $needle, $offset = 0, $encoding = INF)
  266. {
  267. INF === $encoding and $encoding = self::$internal_encoding;
  268. if ('' === $needle .= '') {
  269. user_error(__METHOD__.': Empty delimiter', E_USER_WARNING);
  270. return false;
  271. } else {
  272. return iconv_strpos($haystack, $needle, $offset, $encoding.'//IGNORE');
  273. }
  274. }
  275. public static function mb_strrpos($haystack, $needle, $offset = 0, $encoding = INF)
  276. {
  277. INF === $encoding and $encoding = self::$internal_encoding;
  278. if ($offset != (int) $offset) {
  279. $offset = 0;
  280. } elseif ($offset = (int) $offset) {
  281. if ($offset < 0) {
  282. $haystack = self::mb_substr($haystack, 0, $offset, $encoding);
  283. $offset = 0;
  284. } else {
  285. $haystack = self::mb_substr($haystack, $offset, 2147483647, $encoding);
  286. }
  287. }
  288. $pos = iconv_strrpos($haystack, $needle, $encoding.'//IGNORE');
  289. return false !== $pos ? $offset + $pos : false;
  290. }
  291. public static function mb_strtolower($s, $encoding = INF)
  292. {
  293. return self::mb_convert_case($s, MB_CASE_LOWER, $encoding);
  294. }
  295. public static function mb_strtoupper($s, $encoding = INF)
  296. {
  297. return self::mb_convert_case($s, MB_CASE_UPPER, $encoding);
  298. }
  299. public static function mb_substitute_character($c = INF)
  300. {
  301. return INF !== $c ? false : 'none';
  302. }
  303. public static function mb_substr($s, $start, $length = null, $encoding = INF)
  304. {
  305. INF === $encoding and $encoding = self::$internal_encoding;
  306. if ($start < 0) {
  307. $start = iconv_strlen($s, $encoding.'//IGNORE') + $start;
  308. if ($start < 0) {
  309. $start = 0;
  310. }
  311. }
  312. if (null === $length) {
  313. $length = 2147483647;
  314. } elseif ($length < 0) {
  315. $length = iconv_strlen($s, $encoding.'//IGNORE') + $length - $start;
  316. if ($length < 0) {
  317. return '';
  318. }
  319. }
  320. return iconv_substr($s, $start, $length, $encoding.'//IGNORE').'';
  321. }
  322. public static function mb_stripos($haystack, $needle, $offset = 0, $encoding = INF)
  323. {
  324. INF === $encoding and $encoding = self::$internal_encoding;
  325. $haystack = self::mb_convert_case($haystack, self::MB_CASE_FOLD, $encoding);
  326. $needle = self::mb_convert_case($needle, self::MB_CASE_FOLD, $encoding);
  327. return self::mb_strpos($haystack, $needle, $offset, $encoding);
  328. }
  329. public static function mb_stristr($haystack, $needle, $part = false, $encoding = INF)
  330. {
  331. $pos = self::mb_stripos($haystack, $needle, 0, $encoding);
  332. return self::getSubpart($pos, $part, $haystack, $encoding);
  333. }
  334. public static function mb_strrchr($haystack, $needle, $part = false, $encoding = INF)
  335. {
  336. INF === $encoding and $encoding = self::$internal_encoding;
  337. $needle = self::mb_substr($needle, 0, 1, $encoding);
  338. $pos = iconv_strrpos($haystack, $needle, $encoding);
  339. return self::getSubpart($pos, $part, $haystack, $encoding);
  340. }
  341. public static function mb_strrichr($haystack, $needle, $part = false, $encoding = INF)
  342. {
  343. $needle = self::mb_substr($needle, 0, 1, $encoding);
  344. $pos = self::mb_strripos($haystack, $needle, $encoding);
  345. return self::getSubpart($pos, $part, $haystack, $encoding);
  346. }
  347. public static function mb_strripos($haystack, $needle, $offset = 0, $encoding = INF)
  348. {
  349. INF === $encoding and $encoding = self::$internal_encoding;
  350. $haystack = self::mb_convert_case($haystack, self::MB_CASE_FOLD, $encoding);
  351. $needle = self::mb_convert_case($needle, self::MB_CASE_FOLD, $encoding);
  352. return self::mb_strrpos($haystack, $needle, $offset, $encoding);
  353. }
  354. public static function mb_strstr($haystack, $needle, $part = false, $encoding = INF)
  355. {
  356. $pos = strpos($haystack, $needle);
  357. if (false === $pos) {
  358. return false;
  359. }
  360. if ($part) {
  361. return substr($haystack, 0, $pos);
  362. } else {
  363. return substr($haystack, $pos);
  364. }
  365. }
  366. public static function mb_get_info($type = 'all')
  367. {
  368. $info = array(
  369. 'internal_encoding' => self::$internal_encoding,
  370. 'http_output' => 'pass',
  371. 'http_output_conv_mimetypes' => '^(text/|application/xhtml\+xml)',
  372. 'func_overload' => 0,
  373. 'func_overload_list' => 'no overload',
  374. 'mail_charset' => 'UTF-8',
  375. 'mail_header_encoding' => 'BASE64',
  376. 'mail_body_encoding' => 'BASE64',
  377. 'illegal_chars' => 0,
  378. 'encoding_translation' => 'Off',
  379. 'language' => self::$language,
  380. 'detect_order' => self::$encoding_list,
  381. 'substitute_character' => 'none',
  382. 'strict_detection' => 'Off',
  383. );
  384. if ('all' === $type) {
  385. return $info;
  386. } elseif (isset($info[$type])) {
  387. return $info[$type];
  388. } else {
  389. return false;
  390. }
  391. }
  392. public static function mb_http_input($type = '')
  393. {
  394. return false;
  395. }
  396. public static function mb_http_output($encoding = INF)
  397. {
  398. return INF !== $encoding ? 'pass' === $encoding : 'pass';
  399. }
  400. public static function mb_strwidth($s, $encoding = INF)
  401. {
  402. $encoding = INF === $encoding ? self::$internal_encoding : strtoupper($encoding);
  403. if ('UTF-8' !== $encoding && 'UTF8' !== $encoding) {
  404. $s = iconv($encoding, 'UTF-8//IGNORE', $s);
  405. }
  406. $s = preg_replace('/[\x00-\x19]/', '', $s);
  407. preg_replace('/[\x{0020}-\x{1FFF}\x{FF61}-\x{FF9F}]/u', '', $s, -1, $narrow);
  408. return (iconv_strlen($s, 'UTF-8') << 1) - $narrow;
  409. }
  410. public static function mb_substr_count($haystack, $needle, $encoding = INF)
  411. {
  412. return substr_count($haystack, $needle);
  413. }
  414. public static function mb_output_handler($contents, $status)
  415. {
  416. return $contents;
  417. }
  418. protected static function getSubpart($pos, $part, $haystack, $encoding)
  419. {
  420. INF === $encoding and $encoding = self::$internal_encoding;
  421. if (false === $pos) {
  422. return false;
  423. }
  424. if ($part) {
  425. return self::mb_substr($haystack, 0, $pos, $encoding);
  426. } else {
  427. return self::mb_substr($haystack, $pos, null, $encoding);
  428. }
  429. }
  430. protected static function html_encoding_callback($m)
  431. {
  432. $i = 1;
  433. $entities = '';
  434. $m = unpack('C*', htmlentities($m[0], ENT_COMPAT, 'UTF-8'));
  435. while (isset($m[$i])) {
  436. if (0x80 > $m[$i]) {
  437. $entities .= chr($m[$i++]);
  438. continue;
  439. }
  440. if (0xF0 <= $m[$i]) {
  441. $c = (($m[$i++] - 0xF0) << 18) + (($m[$i++] - 0x80) << 12) + (($m[$i++] - 0x80) << 6) + $m[$i++] - 0x80;
  442. } elseif (0xE0 <= $m[$i]) {
  443. $c = (($m[$i++] - 0xE0) << 12) + (($m[$i++] - 0x80) << 6) + $m[$i++] - 0x80;
  444. } else {
  445. $c = (($m[$i++] - 0xC0) << 6) + $m[$i++] - 0x80;
  446. }
  447. $entities .= '&#'.$c.';';
  448. }
  449. return $entities;
  450. }
  451. protected static function title_case_lower($s)
  452. {
  453. return self::mb_convert_case($s[0], MB_CASE_LOWER, 'UTF-8');
  454. }
  455. protected static function title_case_upper($s)
  456. {
  457. return self::mb_convert_case($s[0], MB_CASE_UPPER, 'UTF-8');
  458. }
  459. protected static function getData($file)
  460. {
  461. $file = __DIR__.'/unidata/'.$file.'.ser';
  462. if (file_exists($file)) {
  463. return unserialize(file_get_contents($file));
  464. } else {
  465. return false;
  466. }
  467. }
  468. }