1 | <?php |
||
22 | final class RegexHelper |
||
23 | { |
||
24 | // Partial regular expressions (wrap with `/` on each side before use) |
||
25 | public const PARTIAL_ENTITY = '&(?:#x[a-f0-9]{1,6}|#[0-9]{1,7}|[a-z][a-z0-9]{1,31});'; |
||
26 | public const PARTIAL_ESCAPABLE = '[!"#$%&\'()*+,.\/:;<=>?@[\\\\\]^_`{|}~-]'; |
||
27 | public const PARTIAL_ESCAPED_CHAR = '\\\\' . self::PARTIAL_ESCAPABLE; |
||
28 | public const PARTIAL_IN_DOUBLE_QUOTES = '"(' . self::PARTIAL_ESCAPED_CHAR . '|[^"\x00])*"'; |
||
29 | public const PARTIAL_IN_SINGLE_QUOTES = '\'(' . self::PARTIAL_ESCAPED_CHAR . '|[^\'\x00])*\''; |
||
30 | public const PARTIAL_IN_PARENS = '\\((' . self::PARTIAL_ESCAPED_CHAR . '|[^)\x00])*\\)'; |
||
31 | public const PARTIAL_REG_CHAR = '[^\\\\()\x00-\x20]'; |
||
32 | public const PARTIAL_IN_PARENS_NOSP = '\((' . self::PARTIAL_REG_CHAR . '|' . self::PARTIAL_ESCAPED_CHAR . '|\\\\)*\)'; |
||
33 | public const PARTIAL_TAGNAME = '[A-Za-z][A-Za-z0-9-]*'; |
||
34 | public const PARTIAL_BLOCKTAGNAME = '(?:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h1|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)'; |
||
35 | public const PARTIAL_ATTRIBUTENAME = '[a-zA-Z_:][a-zA-Z0-9:._-]*'; |
||
36 | public const PARTIAL_UNQUOTEDVALUE = '[^"\'=<>`\x00-\x20]+'; |
||
37 | public const PARTIAL_SINGLEQUOTEDVALUE = '\'[^\']*\''; |
||
38 | public const PARTIAL_DOUBLEQUOTEDVALUE = '"[^"]*"'; |
||
39 | public const PARTIAL_ATTRIBUTEVALUE = '(?:' . self::PARTIAL_UNQUOTEDVALUE . '|' . self::PARTIAL_SINGLEQUOTEDVALUE . '|' . self::PARTIAL_DOUBLEQUOTEDVALUE . ')'; |
||
40 | public const PARTIAL_ATTRIBUTEVALUESPEC = '(?:' . '\s*=' . '\s*' . self::PARTIAL_ATTRIBUTEVALUE . ')'; |
||
41 | public const PARTIAL_ATTRIBUTE = '(?:' . '\s+' . self::PARTIAL_ATTRIBUTENAME . self::PARTIAL_ATTRIBUTEVALUESPEC . '?)'; |
||
42 | public const PARTIAL_OPENTAG = '<' . self::PARTIAL_TAGNAME . self::PARTIAL_ATTRIBUTE . '*' . '\s*\/?>'; |
||
43 | public const PARTIAL_CLOSETAG = '<\/' . self::PARTIAL_TAGNAME . '\s*[>]'; |
||
44 | public const PARTIAL_OPENBLOCKTAG = '<' . self::PARTIAL_BLOCKTAGNAME . self::PARTIAL_ATTRIBUTE . '*' . '\s*\/?>'; |
||
45 | public const PARTIAL_CLOSEBLOCKTAG = '<\/' . self::PARTIAL_BLOCKTAGNAME . '\s*[>]'; |
||
46 | public const PARTIAL_HTMLCOMMENT = '<!---->|<!--(?:-?[^>-])(?:-?[^-])*-->'; |
||
47 | public const PARTIAL_PROCESSINGINSTRUCTION = '[<][?].*?[?][>]'; |
||
48 | public const PARTIAL_DECLARATION = '<![A-Z]+' . '\s+[^>]*>'; |
||
49 | public const PARTIAL_CDATA = '<!\[CDATA\[[\s\S]*?]\]>'; |
||
50 | public const PARTIAL_HTMLTAG = '(?:' . self::PARTIAL_OPENTAG . '|' . self::PARTIAL_CLOSETAG . '|' . self::PARTIAL_HTMLCOMMENT . '|' . |
||
51 | self::PARTIAL_PROCESSINGINSTRUCTION . '|' . self::PARTIAL_DECLARATION . '|' . self::PARTIAL_CDATA . ')'; |
||
52 | public const PARTIAL_HTMLBLOCKOPEN = '<(?:' . self::PARTIAL_BLOCKTAGNAME . '(?:[\s\/>]|$)' . '|' . |
||
53 | '\/' . self::PARTIAL_BLOCKTAGNAME . '(?:[\s>]|$)' . '|' . '[?!])'; |
||
54 | public const PARTIAL_LINK_TITLE = '^(?:"(' . self::PARTIAL_ESCAPED_CHAR . '|[^"\x00])*"' . |
||
55 | '|' . '\'(' . self::PARTIAL_ESCAPED_CHAR . '|[^\'\x00])*\'' . |
||
56 | '|' . '\((' . self::PARTIAL_ESCAPED_CHAR . '|[^()\x00])*\))'; |
||
57 | |||
58 | public const REGEX_PUNCTUATION = '/^[\x{2000}-\x{206F}\x{2E00}-\x{2E7F}\p{Pc}\p{Pd}\p{Pe}\p{Pf}\p{Pi}\p{Po}\p{Ps}\\\\\'!"#\$%&\(\)\*\+,\-\.\\/:;<=>\?@\[\]\^_`\{\|\}~]/u'; |
||
59 | public const REGEX_UNSAFE_PROTOCOL = '/^javascript:|vbscript:|file:|data:/i'; |
||
60 | public const REGEX_SAFE_DATA_PROTOCOL = '/^data:image\/(?:png|gif|jpeg|webp)/i'; |
||
61 | public const REGEX_NON_SPACE = '/[^ \t\f\v\r\n]/'; |
||
62 | |||
63 | public const REGEX_WHITESPACE_CHAR = '/^[ \t\n\x0b\x0c\x0d]/'; |
||
64 | public const REGEX_WHITESPACE = '/[ \t\n\x0b\x0c\x0d]+/'; |
||
65 | public const REGEX_UNICODE_WHITESPACE_CHAR = '/^\pZ|\s/u'; |
||
66 | public const REGEX_THEMATIC_BREAK = '/^(?:(?:\*[ \t]*){3,}|(?:_[ \t]*){3,}|(?:-[ \t]*){3,})[ \t]*$/'; |
||
67 | public const REGEX_LINK_DESTINATION_BRACES = '/^(?:<(?:[^<>\\n\\\\\\x00]|\\\\.)*>)/'; |
||
68 | |||
69 | 6 | public static function isEscapable(string $character): bool |
|
73 | |||
74 | /** |
||
75 | * Attempt to match a regex in string s at offset offset |
||
76 | * |
||
77 | * @param string $regex |
||
78 | * @param string $string |
||
79 | * @param int $offset |
||
80 | * |
||
81 | * @return int|null Index of match, or null |
||
82 | */ |
||
83 | 18 | public static function matchAt(string $regex, string $string, int $offset = 0): ?int |
|
84 | { |
||
85 | 18 | $matches = []; |
|
86 | 18 | $string = \mb_substr($string, $offset, null, 'utf-8'); |
|
87 | 18 | if (!\preg_match($regex, $string, $matches, \PREG_OFFSET_CAPTURE)) { |
|
88 | return null; |
||
89 | } |
||
90 | |||
91 | // PREG_OFFSET_CAPTURE always returns the byte offset, not the char offset, which is annoying |
||
92 | 18 | $charPos = \mb_strlen(\mb_strcut($string, 0, $matches[0][1], 'utf-8'), 'utf-8'); |
|
93 | |||
94 | 18 | return $offset + $charPos; |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * Functional wrapper around preg_match_all |
||
99 | * |
||
100 | * @param string $pattern |
||
101 | * @param string $subject |
||
102 | * @param int $offset |
||
103 | * |
||
104 | * @return array<string>|null |
||
105 | */ |
||
106 | 9 | public static function matchAll(string $pattern, string $subject, int $offset = 0): ?array |
|
107 | { |
||
108 | 9 | if ($offset !== 0) { |
|
109 | $subject = \substr($subject, $offset); |
||
110 | } |
||
111 | |||
112 | 9 | \preg_match_all($pattern, $subject, $matches, \PREG_PATTERN_ORDER); |
|
113 | |||
114 | 9 | $fullMatches = \reset($matches); |
|
115 | 9 | if (empty($fullMatches)) { |
|
116 | 3 | return null; |
|
117 | } |
||
118 | |||
119 | 6 | if (\count($fullMatches) === 1) { |
|
120 | 6 | foreach ($matches as &$match) { |
|
121 | 6 | $match = \reset($match); |
|
122 | } |
||
123 | } |
||
124 | |||
125 | 6 | return $matches ?: null; |
|
126 | } |
||
127 | |||
128 | /** |
||
129 | * Replace backslash escapes with literal characters |
||
130 | * |
||
131 | * @param string $string |
||
132 | * |
||
133 | * @return string |
||
134 | */ |
||
135 | 24 | public static function unescape(string $string): string |
|
136 | { |
||
137 | 24 | $allEscapedChar = '/\\\\(' . self::PARTIAL_ESCAPABLE . ')/'; |
|
138 | |||
139 | /** @var string $escaped */ |
||
140 | 24 | $escaped = \preg_replace($allEscapedChar, '$1', $string); |
|
141 | |||
142 | /** @var string $replaced */ |
||
143 | $replaced = \preg_replace_callback('/' . self::PARTIAL_ENTITY . '/i', function ($e) { |
||
144 | return Html5EntityDecoder::decode($e[0]); |
||
145 | 24 | }, $escaped); |
|
146 | |||
147 | 24 | return $replaced; |
|
148 | } |
||
149 | |||
150 | /** |
||
151 | * @param int $type HTML block type |
||
152 | * |
||
153 | * @return string |
||
154 | * |
||
155 | * @internal |
||
156 | */ |
||
157 | 39 | public static function getHtmlBlockOpenRegex(int $type): string |
|
178 | |||
179 | /** |
||
180 | * @param int $type HTML block type |
||
181 | * |
||
182 | * @return string |
||
183 | * |
||
184 | * @internal |
||
185 | */ |
||
186 | 9 | public static function getHtmlBlockCloseRegex(int $type): string |
|
187 | { |
||
188 | 9 | switch ($type) { |
|
189 | case HtmlBlock::TYPE_1_CODE_CONTAINER: |
||
190 | return '%<\/(?:script|pre|style)>%i'; |
||
191 | case HtmlBlock::TYPE_2_COMMENT: |
||
192 | return '/-->/'; |
||
193 | case HtmlBlock::TYPE_3: |
||
194 | return '/\?>/'; |
||
195 | case HtmlBlock::TYPE_4: |
||
196 | return '/>/'; |
||
197 | case HtmlBlock::TYPE_5_CDATA: |
||
198 | return '/\]\]>/'; |
||
199 | } |
||
200 | |||
201 | 9 | throw new \InvalidArgumentException('Invalid HTML block type'); |
|
202 | } |
||
203 | |||
204 | 12 | public static function isLinkPotentiallyUnsafe(string $url): bool |
|
208 | } |
||
209 |