1 | <?php |
||
28 | final class RegexHelper |
||
29 | { |
||
30 | // Partial regular expressions (wrap with `/` on each side before use) |
||
31 | public const PARTIAL_ENTITY = '&(?:#x[a-f0-9]{1,6}|#[0-9]{1,7}|[a-z][a-z0-9]{1,31});'; |
||
32 | public const PARTIAL_ESCAPABLE = '[!"#$%&\'()*+,.\/:;<=>?@[\\\\\]^_`{|}~-]'; |
||
33 | public const PARTIAL_ESCAPED_CHAR = '\\\\' . self::PARTIAL_ESCAPABLE; |
||
34 | public const PARTIAL_IN_DOUBLE_QUOTES = '"(' . self::PARTIAL_ESCAPED_CHAR . '|[^"\x00])*"'; |
||
35 | public const PARTIAL_IN_SINGLE_QUOTES = '\'(' . self::PARTIAL_ESCAPED_CHAR . '|[^\'\x00])*\''; |
||
36 | public const PARTIAL_IN_PARENS = '\\((' . self::PARTIAL_ESCAPED_CHAR . '|[^)\x00])*\\)'; |
||
37 | public const PARTIAL_REG_CHAR = '[^\\\\()\x00-\x20]'; |
||
38 | public const PARTIAL_IN_PARENS_NOSP = '\((' . self::PARTIAL_REG_CHAR . '|' . self::PARTIAL_ESCAPED_CHAR . '|\\\\)*\)'; |
||
39 | public const PARTIAL_TAGNAME = '[A-Za-z][A-Za-z0-9-]*'; |
||
40 | 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)'; |
||
41 | public const PARTIAL_ATTRIBUTENAME = '[a-zA-Z_:][a-zA-Z0-9:._-]*'; |
||
42 | public const PARTIAL_UNQUOTEDVALUE = '[^"\'=<>`\x00-\x20]+'; |
||
43 | public const PARTIAL_SINGLEQUOTEDVALUE = '\'[^\']*\''; |
||
44 | public const PARTIAL_DOUBLEQUOTEDVALUE = '"[^"]*"'; |
||
45 | public const PARTIAL_ATTRIBUTEVALUE = '(?:' . self::PARTIAL_UNQUOTEDVALUE . '|' . self::PARTIAL_SINGLEQUOTEDVALUE . '|' . self::PARTIAL_DOUBLEQUOTEDVALUE . ')'; |
||
46 | public const PARTIAL_ATTRIBUTEVALUESPEC = '(?:' . '\s*=' . '\s*' . self::PARTIAL_ATTRIBUTEVALUE . ')'; |
||
47 | public const PARTIAL_ATTRIBUTE = '(?:' . '\s+' . self::PARTIAL_ATTRIBUTENAME . self::PARTIAL_ATTRIBUTEVALUESPEC . '?)'; |
||
48 | public const PARTIAL_OPENTAG = '<' . self::PARTIAL_TAGNAME . self::PARTIAL_ATTRIBUTE . '*' . '\s*\/?>'; |
||
49 | public const PARTIAL_CLOSETAG = '<\/' . self::PARTIAL_TAGNAME . '\s*[>]'; |
||
50 | public const PARTIAL_OPENBLOCKTAG = '<' . self::PARTIAL_BLOCKTAGNAME . self::PARTIAL_ATTRIBUTE . '*' . '\s*\/?>'; |
||
51 | public const PARTIAL_CLOSEBLOCKTAG = '<\/' . self::PARTIAL_BLOCKTAGNAME . '\s*[>]'; |
||
52 | public const PARTIAL_HTMLCOMMENT = '<!---->|<!--(?:-?[^>-])(?:-?[^-])*-->'; |
||
53 | public const PARTIAL_PROCESSINGINSTRUCTION = '[<][?].*?[?][>]'; |
||
54 | public const PARTIAL_DECLARATION = '<![A-Z]+' . '\s+[^>]*>'; |
||
55 | public const PARTIAL_CDATA = '<!\[CDATA\[[\s\S]*?]\]>'; |
||
56 | public const PARTIAL_HTMLTAG = '(?:' . self::PARTIAL_OPENTAG . '|' . self::PARTIAL_CLOSETAG . '|' . self::PARTIAL_HTMLCOMMENT . '|' . |
||
57 | self::PARTIAL_PROCESSINGINSTRUCTION . '|' . self::PARTIAL_DECLARATION . '|' . self::PARTIAL_CDATA . ')'; |
||
58 | public const PARTIAL_HTMLBLOCKOPEN = '<(?:' . self::PARTIAL_BLOCKTAGNAME . '(?:[\s\/>]|$)' . '|' . |
||
59 | '\/' . self::PARTIAL_BLOCKTAGNAME . '(?:[\s>]|$)' . '|' . '[?!])'; |
||
60 | public const PARTIAL_LINK_TITLE = '^(?:"(' . self::PARTIAL_ESCAPED_CHAR . '|[^"\x00])*"' . |
||
61 | '|' . '\'(' . self::PARTIAL_ESCAPED_CHAR . '|[^\'\x00])*\'' . |
||
62 | '|' . '\((' . self::PARTIAL_ESCAPED_CHAR . '|[^()\x00])*\))'; |
||
63 | |||
64 | 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'; |
||
65 | public const REGEX_UNSAFE_PROTOCOL = '/^javascript:|vbscript:|file:|data:/i'; |
||
66 | public const REGEX_SAFE_DATA_PROTOCOL = '/^data:image\/(?:png|gif|jpeg|webp)/i'; |
||
67 | public const REGEX_NON_SPACE = '/[^ \t\f\v\r\n]/'; |
||
68 | |||
69 | public const REGEX_WHITESPACE_CHAR = '/^[ \t\n\x0b\x0c\x0d]/'; |
||
70 | public const REGEX_WHITESPACE = '/[ \t\n\x0b\x0c\x0d]+/'; |
||
71 | public const REGEX_UNICODE_WHITESPACE_CHAR = '/^\pZ|\s/u'; |
||
72 | public const REGEX_THEMATIC_BREAK = '/^(?:(?:\*[ \t]*){3,}|(?:_[ \t]*){3,}|(?:-[ \t]*){3,})[ \t]*$/'; |
||
73 | public const REGEX_LINK_DESTINATION_BRACES = '/^(?:<(?:[^<>\\n\\\\\\x00]|\\\\.)*>)/'; |
||
74 | |||
75 | /** |
||
76 | * @psalm-pure |
||
77 | */ |
||
78 | 123 | public static function isEscapable(string $character): bool |
|
82 | |||
83 | /** |
||
84 | * @psalm-pure |
||
85 | */ |
||
86 | 2442 | public static function isLetter(?string $character): bool |
|
94 | |||
95 | /** |
||
96 | * Attempt to match a regex in string s at offset offset |
||
97 | * |
||
98 | * @return int|null Index of match, or null |
||
99 | * |
||
100 | * @psalm-pure |
||
101 | */ |
||
102 | 1734 | public static function matchAt(string $regex, string $string, int $offset = 0): ?int |
|
115 | |||
116 | /** |
||
117 | * Functional wrapper around preg_match_all |
||
118 | * |
||
119 | * @return array<string>|null |
||
120 | * |
||
121 | * @psalm-pure |
||
122 | */ |
||
123 | 1971 | public static function matchAll(string $pattern, string $subject, int $offset = 0): ?array |
|
144 | |||
145 | /** |
||
146 | * Replace backslash escapes with literal characters |
||
147 | * |
||
148 | * @psalm-pure |
||
149 | */ |
||
150 | 543 | public static function unescape(string $string): string |
|
151 | { |
||
152 | 543 | $allEscapedChar = '/\\\\(' . self::PARTIAL_ESCAPABLE . ')/'; |
|
153 | |||
154 | 543 | $escaped = \preg_replace($allEscapedChar, '$1', $string); |
|
155 | \assert(\is_string($escaped)); |
||
156 | |||
157 | return \preg_replace_callback('/' . self::PARTIAL_ENTITY . '/i', static function ($e) { |
||
158 | 24 | return Html5EntityDecoder::decode($e[0]); |
|
159 | 543 | }, $escaped); |
|
160 | } |
||
161 | |||
162 | /** |
||
163 | * @internal |
||
164 | * |
||
165 | * @param int $type HTML block type |
||
166 | * |
||
167 | * @psalm-pure |
||
168 | */ |
||
169 | 339 | public static function getHtmlBlockOpenRegex(int $type): string |
|
190 | |||
191 | /** |
||
192 | * @internal |
||
193 | * |
||
194 | * @param int $type HTML block type |
||
195 | * |
||
196 | * @psalm-pure |
||
197 | */ |
||
198 | 66 | public static function getHtmlBlockCloseRegex(int $type): string |
|
215 | |||
216 | /** |
||
217 | * @psalm-pure |
||
218 | */ |
||
219 | 27 | public static function isLinkPotentiallyUnsafe(string $url): bool |
|
223 | } |
||
224 |