|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the league/commonmark package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Colin O'Dell <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js) |
|
11
|
|
|
* - (c) John MacFarlane |
|
12
|
|
|
* |
|
13
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
14
|
|
|
* file that was distributed with this source code. |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace UnicornFail\Emoji\Extension\Emoji\Renderer; |
|
18
|
|
|
|
|
19
|
|
|
use League\Configuration\ConfigurationAwareInterface; |
|
20
|
|
|
use League\Configuration\ConfigurationInterface; |
|
21
|
|
|
use UnicornFail\Emoji\Node\Inline\Image; |
|
22
|
|
|
use UnicornFail\Emoji\Node\Node; |
|
23
|
|
|
use UnicornFail\Emoji\Renderer\ChildNodeRendererInterface; |
|
24
|
|
|
use UnicornFail\Emoji\Renderer\NodeRendererInterface; |
|
25
|
|
|
use UnicornFail\Emoji\Util\HtmlElement; |
|
26
|
|
|
|
|
27
|
|
|
final class ImageRenderer implements NodeRendererInterface, ConfigurationAwareInterface |
|
28
|
|
|
{ |
|
29
|
|
|
public const REGEX_UNSAFE_PROTOCOL = '/^javascript:|vbscript:|file:|data:/i'; |
|
30
|
|
|
public const REGEX_SAFE_DATA_PROTOCOL = '/^data:image\/(?:png|gif|jpeg|webp)/i'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var ConfigurationInterface |
|
34
|
|
|
* |
|
35
|
|
|
* @psalm-readonly-allow-private-mutation |
|
36
|
|
|
*/ |
|
37
|
|
|
private $config; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param Node $node {@inheritdoc} |
|
41
|
|
|
* |
|
42
|
|
|
* @psalm-suppress MoreSpecificImplementedParamType |
|
43
|
|
|
*/ |
|
44
|
|
|
public function render(Node $node, ChildNodeRendererInterface $childRenderer) |
|
45
|
|
|
{ |
|
46
|
|
|
if (! ($node instanceof Image)) { |
|
47
|
|
|
throw new \InvalidArgumentException('Incompatible node type: ' . \get_class($node)); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** @var array<string, mixed> $attrs */ |
|
51
|
|
|
$attrs = $node->data->get('attributes'); |
|
52
|
|
|
|
|
53
|
|
|
$forbidUnsafeLinks = ! $this->config->get('allow_unsafe_links'); |
|
54
|
|
|
if ($forbidUnsafeLinks && self::isLinkPotentiallyUnsafe($node->getUrl())) { |
|
|
|
|
|
|
55
|
|
|
$attrs['src'] = ''; |
|
56
|
|
|
} else { |
|
57
|
|
|
$attrs['src'] = $node->getUrl(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$alt = $childRenderer->renderNodes($node->children()); |
|
61
|
|
|
$alt = \preg_replace('/\<[^>]*alt="([^"]*)"[^>]*\>/', '$1', $alt); |
|
62
|
|
|
$attrs['alt'] = \preg_replace('/\<[^>]*\>/', '', $alt ?? ''); |
|
63
|
|
|
|
|
64
|
|
|
if ($node->data->has('title')) { |
|
65
|
|
|
$attrs['title'] = $node->data->get('title'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return new HtmlElement('img', $attrs, '', true); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function setConfiguration(ConfigurationInterface $configuration): void |
|
72
|
|
|
{ |
|
73
|
|
|
$this->config = $configuration; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @psalm-pure |
|
78
|
|
|
*/ |
|
79
|
|
|
public static function isLinkPotentiallyUnsafe(string $url): bool |
|
80
|
|
|
{ |
|
81
|
|
|
return \preg_match(self::REGEX_UNSAFE_PROTOCOL, $url) !== 0 && \preg_match(self::REGEX_SAFE_DATA_PROTOCOL, $url) === 0; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.