In this repo i store all my websites, each in a different branch
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

664 rader
22 KiB

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