1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the league/commonmark package. |
5
|
|
|
* |
6
|
|
|
* (c) Colin O'Dell <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace League\CommonMark\Extension\ExternalLink; |
13
|
|
|
|
14
|
|
|
use League\CommonMark\EnvironmentInterface; |
15
|
|
|
use League\CommonMark\Event\DocumentParsedEvent; |
16
|
|
|
use League\CommonMark\Inline\Element\Link; |
17
|
|
|
|
18
|
|
|
final class ExternalLinkProcessor |
19
|
|
|
{ |
20
|
|
|
/** @var EnvironmentInterface */ |
21
|
|
|
private $environment; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param EnvironmentInterface $environment |
25
|
|
|
*/ |
26
|
9 |
|
public function __construct(EnvironmentInterface $environment) |
27
|
|
|
{ |
28
|
9 |
|
$this->environment = $environment; |
29
|
9 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param DocumentParsedEvent $e |
33
|
|
|
*/ |
34
|
9 |
|
public function __invoke(DocumentParsedEvent $e) |
35
|
|
|
{ |
36
|
9 |
|
$internalHosts = $this->environment->getConfig('external_link/internal_hosts', []); |
37
|
9 |
|
$openInNewWindow = $this->environment->getConfig('external_link/open_in_new_window', false); |
38
|
9 |
|
$classes = $this->environment->getConfig('external_link/html_class', ''); |
39
|
|
|
|
40
|
9 |
|
$walker = $e->getDocument()->walker(); |
41
|
9 |
|
while ($event = $walker->next()) { |
42
|
9 |
|
if ($event->isEntering() && $event->getNode() instanceof Link) { |
43
|
|
|
/** @var Link $link */ |
44
|
9 |
|
$link = $event->getNode(); |
45
|
|
|
|
46
|
9 |
|
$host = parse_url($link->getUrl(), PHP_URL_HOST); |
47
|
9 |
|
if (empty($host)) { |
48
|
|
|
// Something is terribly wrong with this URL |
49
|
3 |
|
continue; |
50
|
|
|
} |
51
|
|
|
|
52
|
6 |
|
if (self::hostMatches($host, $internalHosts)) { |
53
|
3 |
|
$link->data['external'] = false; |
54
|
3 |
|
continue; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// Host does not match our list |
58
|
6 |
|
$this->markLinkAsExternal($link, $openInNewWindow, $classes); |
59
|
|
|
} |
60
|
|
|
} |
61
|
9 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Link $link |
65
|
|
|
* @param bool $openInNewWindow |
66
|
|
|
* @param string $classes |
67
|
|
|
*/ |
68
|
6 |
|
private function markLinkAsExternal(Link $link, bool $openInNewWindow, string $classes): void |
69
|
|
|
{ |
70
|
6 |
|
$link->data['external'] = true; |
71
|
6 |
|
$link->data['attributes'] = $link->getData('attributes', []); |
72
|
6 |
|
$link->data['attributes']['rel'] = 'noopener noreferrer'; |
73
|
|
|
|
74
|
6 |
|
if ($openInNewWindow) { |
75
|
3 |
|
$link->data['attributes']['target'] = '_blank'; |
76
|
|
|
} |
77
|
|
|
|
78
|
6 |
|
if (!empty($classes)) { |
79
|
3 |
|
$link->data['attributes']['class'] = trim(($link->data['attributes']['class'] ?? '') . ' ' . $classes); |
80
|
|
|
} |
81
|
6 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $host |
85
|
|
|
* @param mixed $compareTo |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
* |
89
|
|
|
* @internal This method is only public so we can easily test it. DO NOT USE THIS OUTSIDE OF THIS EXTENSION! |
90
|
|
|
*/ |
91
|
27 |
|
public static function hostMatches(string $host, $compareTo) |
92
|
|
|
{ |
93
|
27 |
|
foreach ((array) $compareTo as $c) { |
94
|
24 |
|
if (strpos($c, '/') === 0) { |
95
|
6 |
|
if (preg_match($c, $host)) { |
96
|
6 |
|
return true; |
97
|
|
|
} |
98
|
18 |
|
} elseif ($c === $host) { |
99
|
9 |
|
return true; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
15 |
|
return false; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|