PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/polyfill-mbstring/Mbstring.php

https://gitlab.com/Pasantias/pasantiasASLG
PHP | 604 lines | 439 code | 108 blank | 57 comment | 77 complexity | a06f18a7549692d0eba51f5800a89e38 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Polyfill\Mbstring;
  11. /**
  12. * Partial mbstring implementation in PHP, iconv based, UTF-8 centric.
  13. *
  14. * Implemented:
  15. * - mb_convert_encoding - Convert character encoding
  16. * - mb_convert_variables - Convert character code in variable(s)
  17. * - mb_decode_mimeheader - Decode string in MIME header field
  18. * - mb_encode_mimeheader - Encode string for MIME header XXX NATIVE IMPLEMENTATION IS REALLY BUGGED
  19. * - mb_convert_case - Perform case folding on a string
  20. * - mb_get_info - Get internal settings of mbstring
  21. * - mb_http_input - Detect HTTP input character encoding
  22. * - mb_http_output - Set/Get HTTP output character encoding
  23. * - mb_internal_encoding - Set/Get internal character encoding
  24. * - mb_list_encodings - Returns an array of all supported encodings
  25. * - mb_output_handler - Callback function converts character encoding in output buffer
  26. * - mb_strlen - Get string length
  27. * - mb_strpos - Find position of first occurrence of string in a string
  28. * - mb_strrpos - Find position of last occurrence of a string in a string
  29. * - mb_strtolower - Make a string lowercase
  30. * - mb_strtoupper - Make a string uppercase
  31. * - mb_substitute_character - Set/Get substitution character
  32. * - mb_substr - Get part of string
  33. * - mb_stripos - Finds position of first occurrence of a string within another, case insensitive
  34. * - mb_stristr - Finds first occurrence of a string within another, case insensitive
  35. * - mb_strrchr - Finds the last occurrence of a character in a string within another
  36. * - mb_strrichr - Finds the last occurrence of a character in a string within another, case insensitive
  37. * - mb_strripos - Finds position of last occurrence of a string within another, case insensitive
  38. * - mb_strstr - Finds first occurrence of a string within anothers
  39. * - mb_strwidth - Return width of string
  40. * - mb_substr_count - Count the number of substring occurrences
  41. *
  42. * Not implemented:
  43. * - mb_convert_kana - Convert "kana" one from another ("zen-kaku", "han-kaku" and more)
  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. * @author Nicolas Grekas <p@tchwork.com>
  57. *
  58. * @internal
  59. */
  60. final class Mbstring
  61. {
  62. const MB_CASE_FOLD = PHP_INT_MAX;
  63. private static $encodingList = array('ASCII', 'UTF-8');
  64. private static $language = 'neutral';
  65. private static $internalEncoding = 'UTF-8';
  66. private static $caseFold = array(
  67. array('µ','ſ',"\xCD\x85",'ς',"\xCF\x90","\xCF\x91","\xCF\x95","\xCF\x96","\xCF\xB0","\xCF\xB1","\xCF\xB5","\xE1\xBA\x9B","\xE1\xBE\xBE"),
  68. array('μ','s','ι', 'σ','β', 'θ', 'φ', 'π', 'κ', 'ρ', 'ε', "\xE1\xB9\xA1",'ι'),
  69. );
  70. public static function mb_convert_encoding($s, $toEncoding, $fromEncoding = null)
  71. {
  72. if (is_array($fromEncoding) || false !== strpos($fromEncoding, ',')) {
  73. $fromEncoding = self::mb_detect_encoding($s, $fromEncoding);
  74. } else {
  75. $fromEncoding = self::getEncoding($fromEncoding);
  76. }
  77. $toEncoding = self::getEncoding($toEncoding);
  78. if ('BASE64' === $fromEncoding) {
  79. $s = base64_decode($s);
  80. $fromEncoding = $toEncoding;
  81. }
  82. if ('BASE64' === $toEncoding) {
  83. return base64_encode($s);
  84. }
  85. if ('HTML-ENTITIES' === $toEncoding || 'HTML' === $toEncoding) {
  86. if ('HTML-ENTITIES' === $fromEncoding || 'HTML' === $fromEncoding) {
  87. $fromEncoding = 'Windows-1252';
  88. }
  89. if ('UTF-8' !== $fromEncoding) {
  90. $s = iconv($fromEncoding, 'UTF-8', $s);
  91. }
  92. return preg_replace_callback('/[\x80-\xFF]+/', array(__CLASS__, 'html_encoding_callback'), $s);
  93. }
  94. if ('HTML-ENTITIES' === $fromEncoding) {
  95. $s = html_entity_decode($s, ENT_COMPAT, 'UTF-8');
  96. $fromEncoding = 'UTF-8';
  97. }
  98. return iconv($fromEncoding, $toEncoding, $s);
  99. }
  100. public static function mb_convert_variables($toEncoding, $fromEncoding, &$a = null, &$b = null, &$c = null, &$d = null, &$e = null, &$f = null)
  101. {
  102. $vars = array(&$a, &$b, &$c, &$d, &$e, &$f);
  103. $ok = true;
  104. array_walk_recursive($vars, function (&$v) use (&$ok, $toEncoding, $fromEncoding) {
  105. if (false === $v = Mbstring::mb_convert_encoding($v, $toEncoding, $fromEncoding)) {
  106. $ok = false;
  107. }
  108. });
  109. return $ok ? $fromEncoding : false;
  110. }
  111. public static function mb_decode_mimeheader($s)
  112. {
  113. return iconv_mime_decode($s, 2, self::$internalEncoding);
  114. }
  115. public static function mb_encode_mimeheader($s, $charset = null, $transferEncoding = null, $linefeed = null, $indent = null)
  116. {
  117. trigger_error('mb_encode_mimeheader() is bugged. Please use iconv_mime_encode() instead', E_USER_WARNING);
  118. }
  119. public static function mb_convert_case($s, $mode, $encoding = null)
  120. {
  121. if ('' === $s .= '') {
  122. return '';
  123. }
  124. $encoding = self::getEncoding($encoding);
  125. if ('UTF-8' === $encoding) {
  126. $encoding = null;
  127. } else {
  128. $s = iconv($encoding, 'UTF-8', $s);
  129. }
  130. if (MB_CASE_TITLE == $mode) {
  131. $s = preg_replace_callback('/\b\p{Ll}/u', array(__CLASS__, 'title_case_upper'), $s);
  132. $s = preg_replace_callback('/\B[\p{Lu}\p{Lt}]+/u', array(__CLASS__, 'title_case_lower'), $s);
  133. } else {
  134. if (MB_CASE_UPPER == $mode) {
  135. static $upper = null;
  136. if (null === $upper) {
  137. $upper = self::getData('upperCase');
  138. }
  139. $map = $upper;
  140. } else {
  141. if (self::MB_CASE_FOLD === $mode) {
  142. $s = str_replace(self::$caseFold[0], self::$caseFold[1], $s);
  143. }
  144. static $lower = null;
  145. if (null === $lower) {
  146. $lower = self::getData('lowerCase');
  147. }
  148. $map = $lower;
  149. }
  150. static $ulenMask = array("\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4);
  151. $i = 0;
  152. $len = strlen($s);
  153. while ($i < $len) {
  154. $ulen = $s[$i] < "\x80" ? 1 : $ulenMask[$s[$i] & "\xF0"];
  155. $uchr = substr($s, $i, $ulen);
  156. $i += $ulen;
  157. if (isset($map[$uchr])) {
  158. $uchr = $map[$uchr];
  159. $nlen = strlen($uchr);
  160. if ($nlen == $ulen) {
  161. $nlen = $i;
  162. do {
  163. $s[--$nlen] = $uchr[--$ulen];
  164. } while ($ulen);
  165. } else {
  166. $s = substr_replace($s, $uchr, $i - $ulen, $ulen);
  167. $len += $nlen - $ulen;
  168. $i += $nlen - $ulen;
  169. }
  170. }
  171. }
  172. }
  173. if (null === $encoding) {
  174. return $s;
  175. }
  176. return iconv('UTF-8', $encoding, $s);
  177. }
  178. public static function mb_internal_encoding($encoding = null)
  179. {
  180. if (null === $encoding) {
  181. return self::$internalEncoding;
  182. }
  183. $encoding = self::getEncoding($encoding);
  184. if ('UTF-8' === $encoding || false !== @iconv($encoding, $encoding, ' ')) {
  185. self::$internalEncoding = $encoding;
  186. return true;
  187. }
  188. return false;
  189. }
  190. public static function mb_language($lang = null)
  191. {
  192. if (null === $lang) {
  193. return self::$language;
  194. }
  195. switch ($lang = strtolower($lang)) {
  196. case 'uni':
  197. case 'neutral':
  198. self::$language = $lang;
  199. return true;
  200. }
  201. return false;
  202. }
  203. public static function mb_list_encodings()
  204. {
  205. return array('UTF-8');
  206. }
  207. public static function mb_encoding_aliases($encoding)
  208. {
  209. switch (strtoupper($encoding)) {
  210. case 'UTF8':
  211. case 'UTF-8':
  212. return array('utf8');
  213. }
  214. return false;
  215. }
  216. public static function mb_check_encoding($var = null, $encoding = null)
  217. {
  218. if (null === $encoding) {
  219. if (null === $var) {
  220. return false;
  221. }
  222. $encoding = self::$internalEncoding;
  223. }
  224. return self::mb_detect_encoding($var, array($encoding)) || false !== @iconv($encoding, $encoding, $var);
  225. }
  226. public static function mb_detect_encoding($str, $encodingList = null, $strict = false)
  227. {
  228. if (null === $encodingList) {
  229. $encodingList = self::$encodingList;
  230. } else {
  231. if (!is_array($encodingList)) {
  232. $encodingList = array_map('trim', explode(',', $encodingList));
  233. }
  234. $encodingList = array_map('strtoupper', $encodingList);
  235. }
  236. foreach ($encodingList as $enc) {
  237. switch ($enc) {
  238. case 'ASCII':
  239. if (!preg_match('/[\x80-\xFF]/', $str)) {
  240. return $enc;
  241. }
  242. break;
  243. case 'UTF8':
  244. case 'UTF-8':
  245. if (preg_match('//u', $str)) {
  246. return 'UTF-8';
  247. }
  248. break;
  249. default:
  250. if (0 === strncmp($enc, 'ISO-8859-', 9)) {
  251. return $enc;
  252. }
  253. }
  254. }
  255. return false;
  256. }
  257. public static function mb_detect_order($encodingList = null)
  258. {
  259. if (null === $encodingList) {
  260. return self::$encodingList;
  261. }
  262. if (!is_array($encodingList)) {
  263. $encodingList = array_map('trim', explode(',', $encodingList));
  264. }
  265. $encodingList = array_map('strtoupper', $encodingList);
  266. foreach ($encodingList as $enc) {
  267. switch ($enc) {
  268. default:
  269. if (strncmp($enc, 'ISO-8859-', 9)) {
  270. return false;
  271. }
  272. case 'ASCII':
  273. case 'UTF8':
  274. case 'UTF-8':
  275. }
  276. }
  277. self::$encodingList = $encodingList;
  278. return true;
  279. }
  280. public static function mb_strlen($s, $encoding = null)
  281. {
  282. $encoding = self::getEncoding($encoding);
  283. return iconv_strlen($s, $encoding);
  284. }
  285. public static function mb_strpos($haystack, $needle, $offset = 0, $encoding = null)
  286. {
  287. $encoding = self::getEncoding($encoding);
  288. if ('' === $needle .= '') {
  289. trigger_error(__METHOD__.': Empty delimiter', E_USER_WARNING);
  290. return false;
  291. }
  292. return iconv_strpos($haystack, $needle, $offset, $encoding);
  293. }
  294. public static function mb_strrpos($haystack, $needle, $offset = 0, $encoding = null)
  295. {
  296. $encoding = self::getEncoding($encoding);
  297. if ($offset != (int) $offset) {
  298. $offset = 0;
  299. } elseif ($offset = (int) $offset) {
  300. if ($offset < 0) {
  301. $haystack = self::mb_substr($haystack, 0, $offset, $encoding);
  302. $offset = 0;
  303. } else {
  304. $haystack = self::mb_substr($haystack, $offset, 2147483647, $encoding);
  305. }
  306. }
  307. $pos = iconv_strrpos($haystack, $needle, $encoding);
  308. return false !== $pos ? $offset + $pos : false;
  309. }
  310. public static function mb_strtolower($s, $encoding = null)
  311. {
  312. return self::mb_convert_case($s, MB_CASE_LOWER, $encoding);
  313. }
  314. public static function mb_strtoupper($s, $encoding = null)
  315. {
  316. return self::mb_convert_case($s, MB_CASE_UPPER, $encoding);
  317. }
  318. public static function mb_substitute_character($c = null)
  319. {
  320. if (0 === strcasecmp($c, 'none')) {
  321. return true;
  322. }
  323. return null !== $c ? false : 'none';
  324. }
  325. public static function mb_substr($s, $start, $length = null, $encoding = null)
  326. {
  327. $encoding = self::getEncoding($encoding);
  328. if ($start < 0) {
  329. $start = iconv_strlen($s, $encoding) + $start;
  330. if ($start < 0) {
  331. $start = 0;
  332. }
  333. }
  334. if (null === $length) {
  335. $length = 2147483647;
  336. } elseif ($length < 0) {
  337. $length = iconv_strlen($s, $encoding) + $length - $start;
  338. if ($length < 0) {
  339. return '';
  340. }
  341. }
  342. return iconv_substr($s, $start, $length, $encoding).'';
  343. }
  344. public static function mb_stripos($haystack, $needle, $offset = 0, $encoding = null)
  345. {
  346. $haystack = self::mb_convert_case($haystack, self::MB_CASE_FOLD, $encoding);
  347. $needle = self::mb_convert_case($needle, self::MB_CASE_FOLD, $encoding);
  348. return self::mb_strpos($haystack, $needle, $offset, $encoding);
  349. }
  350. public static function mb_stristr($haystack, $needle, $part = false, $encoding = null)
  351. {
  352. $pos = self::mb_stripos($haystack, $needle, 0, $encoding);
  353. return self::getSubpart($pos, $part, $haystack, $encoding);
  354. }
  355. public static function mb_strrchr($haystack, $needle, $part = false, $encoding = null)
  356. {
  357. $encoding = self::getEncoding($encoding);
  358. $needle = self::mb_substr($needle, 0, 1, $encoding);
  359. $pos = iconv_strrpos($haystack, $needle, $encoding);
  360. return self::getSubpart($pos, $part, $haystack, $encoding);
  361. }
  362. public static function mb_strrichr($haystack, $needle, $part = false, $encoding = null)
  363. {
  364. $needle = self::mb_substr($needle, 0, 1, $encoding);
  365. $pos = self::mb_strripos($haystack, $needle, $encoding);
  366. return self::getSubpart($pos, $part, $haystack, $encoding);
  367. }
  368. public static function mb_strripos($haystack, $needle, $offset = 0, $encoding = null)
  369. {
  370. $haystack = self::mb_convert_case($haystack, self::MB_CASE_FOLD, $encoding);
  371. $needle = self::mb_convert_case($needle, self::MB_CASE_FOLD, $encoding);
  372. return self::mb_strrpos($haystack, $needle, $offset, $encoding);
  373. }
  374. public static function mb_strstr($haystack, $needle, $part = false, $encoding = null)
  375. {
  376. $pos = strpos($haystack, $needle);
  377. if (false === $pos) {
  378. return false;
  379. }
  380. if ($part) {
  381. return substr($haystack, 0, $pos);
  382. }
  383. return substr($haystack, $pos);
  384. }
  385. public static function mb_get_info($type = 'all')
  386. {
  387. $info = array(
  388. 'internal_encoding' => self::$internalEncoding,
  389. 'http_output' => 'pass',
  390. 'http_output_conv_mimetypes' => '^(text/|application/xhtml\+xml)',
  391. 'func_overload' => 0,
  392. 'func_overload_list' => 'no overload',
  393. 'mail_charset' => 'UTF-8',
  394. 'mail_header_encoding' => 'BASE64',
  395. 'mail_body_encoding' => 'BASE64',
  396. 'illegal_chars' => 0,
  397. 'encoding_translation' => 'Off',
  398. 'language' => self::$language,
  399. 'detect_order' => self::$encodingList,
  400. 'substitute_character' => 'none',
  401. 'strict_detection' => 'Off',
  402. );
  403. if ('all' === $type) {
  404. return $info;
  405. }
  406. if (isset($info[$type])) {
  407. return $info[$type];
  408. }
  409. return false;
  410. }
  411. public static function mb_http_input($type = '')
  412. {
  413. return false;
  414. }
  415. public static function mb_http_output($encoding = null)
  416. {
  417. return null !== $encoding ? 'pass' === $encoding : 'pass';
  418. }
  419. public static function mb_strwidth($s, $encoding = null)
  420. {
  421. $encoding = self::getEncoding($encoding);
  422. if ('UTF-8' !== $encoding) {
  423. $s = iconv($encoding, 'UTF-8', $s);
  424. }
  425. $s = preg_replace('/[\x{1100}-\x{115F}\x{2329}\x{232A}\x{2E80}-\x{303E}\x{3040}-\x{A4CF}\x{AC00}-\x{D7A3}\x{F900}-\x{FAFF}\x{FE10}-\x{FE19}\x{FE30}-\x{FE6F}\x{FF00}-\x{FF60}\x{FFE0}-\x{FFE6}\x{20000}-\x{2FFFD}\x{30000}-\x{3FFFD}]/u', '', $s, -1, $wide);
  426. return ($wide << 1) + iconv_strlen($s, 'UTF-8');
  427. }
  428. public static function mb_substr_count($haystack, $needle, $encoding = null)
  429. {
  430. return substr_count($haystack, $needle);
  431. }
  432. public static function mb_output_handler($contents, $status)
  433. {
  434. return $contents;
  435. }
  436. private static function getSubpart($pos, $part, $haystack, $encoding)
  437. {
  438. if (false === $pos) {
  439. return false;
  440. }
  441. if ($part) {
  442. return self::mb_substr($haystack, 0, $pos, $encoding);
  443. }
  444. return self::mb_substr($haystack, $pos, null, $encoding);
  445. }
  446. private static function html_encoding_callback($m)
  447. {
  448. $i = 1;
  449. $entities = '';
  450. $m = unpack('C*', htmlentities($m[0], ENT_COMPAT, 'UTF-8'));
  451. while (isset($m[$i])) {
  452. if (0x80 > $m[$i]) {
  453. $entities .= chr($m[$i++]);
  454. continue;
  455. }
  456. if (0xF0 <= $m[$i]) {
  457. $c = (($m[$i++] - 0xF0) << 18) + (($m[$i++] - 0x80) << 12) + (($m[$i++] - 0x80) << 6) + $m[$i++] - 0x80;
  458. } elseif (0xE0 <= $m[$i]) {
  459. $c = (($m[$i++] - 0xE0) << 12) + (($m[$i++] - 0x80) << 6) + $m[$i++] - 0x80;
  460. } else {
  461. $c = (($m[$i++] - 0xC0) << 6) + $m[$i++] - 0x80;
  462. }
  463. $entities .= '&#'.$c.';';
  464. }
  465. return $entities;
  466. }
  467. private static function title_case_lower($s)
  468. {
  469. return self::mb_convert_case($s[0], MB_CASE_LOWER, 'UTF-8');
  470. }
  471. private static function title_case_upper($s)
  472. {
  473. return self::mb_convert_case($s[0], MB_CASE_UPPER, 'UTF-8');
  474. }
  475. private static function getData($file)
  476. {
  477. if (file_exists($file = __DIR__.'/Resources/unidata/'.$file.'.ser')) {
  478. return unserialize(file_get_contents($file));
  479. }
  480. return false;
  481. }
  482. private static function getEncoding($encoding)
  483. {
  484. if (null === $encoding) {
  485. return self::$internalEncoding;
  486. }
  487. $encoding = strtoupper($encoding);
  488. if ('8BIT' === $encoding || 'BINARY' === $encoding) {
  489. return 'CP850';
  490. }
  491. if ('UTF8' === $encoding) {
  492. return 'UTF-8';
  493. }
  494. return $encoding;
  495. }
  496. }