1<?php 2 3/* 4 * This file is part of the league/commonmark package. 5 * 6 * (c) Colin O'Dell <colinodell@gmail.com> 7 * 8 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js) 9 * - (c) John MacFarlane 10 * 11 * For the full copyright and license information, please view the LICENSE 12 * file that was distributed with this source code. 13 */ 14 15namespace League\CommonMark\Util; 16 17use League\CommonMark\Exception\UnexpectedEncodingException; 18 19final class UrlEncoder 20{ 21 /** @var string[] */ 22 private static $encodeCache = ['%00', '%01', '%02', '%03', '%04', '%05', '%06', '%07', '%08', '%09', '%0A', '%0B', '%0C', '%0D', '%0E', '%0F', '%10', '%11', '%12', '%13', '%14', '%15', '%16', '%17', '%18', '%19', '%1A', '%1B', '%1C', '%1D', '%1E', '%1F', '%20', '!', '%22', '#', '$', '%25', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '%3C', '=', '%3E', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '%5B', '%5C', '%5D', '%5E', '_', '%60', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '%7B', '%7C', '%7D', '~', '%7F']; 23 24 public static function unescapeAndEncode(string $uri): string 25 { 26 // Optimization: if the URL only includes characters we know will be kept as-is, then just return the URL as-is. 27 if (\preg_match('/^[A-Za-z0-9~!@#$&*()\-_=+;:,.\/?]+$/', $uri)) { 28 return $uri; 29 } 30 31 $result = ''; 32 33 $chars = \preg_split('//u', $uri, -1, \PREG_SPLIT_NO_EMPTY); 34 35 if (!\is_array($chars) || !\mb_check_encoding($uri, 'UTF-8')) { 36 throw new UnexpectedEncodingException('Unexpected encoding - UTF-8 or ASCII was expected'); 37 } 38 39 $l = \count($chars); 40 for ($i = 0; $i < $l; $i++) { 41 $code = $chars[$i]; 42 if ($code === '%' && $i + 2 < $l) { 43 if (\preg_match('/^[0-9a-f]{2}$/i', $chars[$i + 1] . $chars[$i + 2]) === 1) { 44 $result .= '%' . $chars[$i + 1] . $chars[$i + 2]; 45 $i += 2; 46 continue; 47 } 48 } 49 50 if (\ord($code) < 128) { 51 $result .= self::$encodeCache[\ord($code)]; 52 continue; 53 } 54 55 $result .= \rawurlencode($code); 56 } 57 58 return $result; 59 } 60} 61