1 | <?php |
||
18 | final class UrlAutolinkProcessor |
||
19 | { |
||
20 | // RegEx adapted from https://github.com/symfony/symfony/blob/4.2/src/Symfony/Component/Validator/Constraints/UrlValidator.php |
||
21 | const REGEX = '~ |
||
22 | (?<=^|[ \\t\\n\\x0b\\x0c\\x0d*_\\~\\(]) # Can only come at the beginning of a line, after whitespace, or certain delimiting characters |
||
23 | ( |
||
24 | # Must start with a supported scheme + auth, or "www" |
||
25 | (?: |
||
26 | (?:%s):// # protocol |
||
27 | (?:([\.\pL\pN-]+:)?([\.\pL\pN-]+)@)? # basic auth |
||
28 | |www\.) |
||
29 | (?: |
||
30 | (?:[\pL\pN\pS\-\.])+(?:\.?(?:[\pL\pN]|xn\-\-[\pL\pN-]+)+\.?) # a domain name |
||
31 | | # or |
||
32 | \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} # an IP address |
||
33 | | # or |
||
34 | \[ |
||
35 | (?:(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){6})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:::(?:(?:(?:[0-9a-f]{1,4})):){5})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){4})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|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})))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,6}(?:(?:[0-9a-f]{1,4})))?::)))) |
||
36 | \] # an IPv6 address |
||
37 | ) |
||
38 | (?::[0-9]+)? # a port (optional) |
||
39 | (?:/ (?:[\pL\pN\-._\~!$&\'()*+,;=:@]|%%[0-9A-Fa-f]{2})* )* # a path |
||
40 | (?:\? (?:[\pL\pN\-._\~!$&\'()*+,;=:@/?]|%%[0-9A-Fa-f]{2})* )? # a query (optional) |
||
41 | (?:\# (?:[\pL\pN\-._\~!$&\'()*+,;=:@/?]|%%[0-9A-Fa-f]{2})* )? # a fragment (optional) |
||
42 | )~ixu'; |
||
43 | |||
44 | private $finalRegex; |
||
45 | |||
46 | 177 | public function __construct(array $allowedProtocols = ['http', 'https', 'ftp']) |
|
50 | |||
51 | /** |
||
52 | * @param DocumentParsedEvent $e |
||
53 | * |
||
54 | * @return void |
||
55 | */ |
||
56 | 177 | public function __invoke(DocumentParsedEvent $e) |
|
67 | |||
68 | 162 | private static function processAutolinks(Text $node, $regex) |
|
116 | |||
117 | 87 | private static function addLink(Text $node, $url) |
|
128 | |||
129 | /** |
||
130 | * @param string $content |
||
131 | * |
||
132 | * @return bool |
||
133 | */ |
||
134 | 15 | private static function hasMoreCloserParensThanOpeners($content) |
|
149 | } |
||
150 |