-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathHtmlOutputNodeImg.class.php
More file actions
255 lines (220 loc) · 9.39 KB
/
HtmlOutputNodeImg.class.php
File metadata and controls
255 lines (220 loc) · 9.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
namespace wcf\system\html\output\node;
use wcf\action\ImageProxyAction;
use wcf\data\smiley\SmileyCache;
use wcf\system\application\ApplicationHandler;
use wcf\system\bbcode\BBCodeHandler;
use wcf\system\html\node\AbstractHtmlNodeProcessor;
use wcf\system\message\embedded\object\MessageEmbeddedObjectManager;
use wcf\system\request\LinkHandler;
use wcf\system\request\RouteHandler;
use wcf\system\WCF;
use wcf\util\CryptoUtil;
use wcf\util\DOMUtil;
use wcf\util\exception\CryptoException;
use wcf\util\StringUtil;
use wcf\util\Url;
/**
* Processes images.
*
* @author Alexander Ebert
* @copyright 2001-2019 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
*/
class HtmlOutputNodeImg extends AbstractHtmlOutputNode
{
/**
* @inheritDoc
*/
protected $tagName = 'img';
#[\Override]
public function process(array $elements, AbstractHtmlNodeProcessor $htmlNodeProcessor)
{
/** @var \DOMElement $element */
foreach ($elements as $element) {
$class = $element->getAttribute('class');
if (\preg_match('~\bsmiley\b~', $class)) {
$code = $element->getAttribute('alt');
$smiley = SmileyCache::getInstance()->getSmileyByCode($code);
// Render the mapped unicode emoji instead of the legacy smiley image.
if ($smiley !== null && $smiley->emoji !== '') {
$htmlNodeProcessor->replaceElementWithText($element, $smiley->emoji, false);
continue;
}
if ($smiley === null || $this->outputType === 'text/plain') {
// output as raw code instead
$htmlNodeProcessor->replaceElementWithText($element, ' ' . $code . ' ', false);
} else {
// Ensure that the smiley's HTML is up to date.
$doc = new \DOMDocument();
$doc->loadHTML(\sprintf(
'<?xml version="1.0" encoding="UTF-8"?><html><body>%s</body></html>',
$smiley->getHtml()
));
$smileyNode = $element->ownerDocument->importNode($doc->getElementsByTagName('img')->item(0), true);
$element->parentNode->replaceChild($smileyNode, $element);
}
} else {
$src = $element->getAttribute('src');
if (!$src) {
DOMUtil::removeNode($element);
continue;
}
$element->setAttribute('data-type', 'image');
$element->setAttribute('data-fancybox', \sprintf(
'message-%s-%d',
MessageEmbeddedObjectManager::getInstance()->getActiveMessageObjectType(),
MessageEmbeddedObjectManager::getInstance()->getActiveMessageID(),
));
if (\MODULE_IMAGE_PROXY) {
if (!Url::is($src)) {
// not a valid URL, discard it
DOMUtil::removeNode($element);
continue;
}
$urlComponents = Url::parse($src);
if (empty($urlComponents['host'])) {
// relative URL, ignore it
continue;
}
if (\IMAGE_PROXY_INSECURE_ONLY && $urlComponents['scheme'] === 'https') {
// proxy is enabled for insecure connections only
if (!\IMAGE_ALLOW_EXTERNAL_SOURCE && !$this->isAllowedOrigin($src)) {
/** @var HtmlOutputNodeProcessor $htmlNodeProcessor */
$this->replaceExternalSource(
$element,
$src,
$htmlNodeProcessor->getHtmlProcessor()->enableUgc
);
}
continue;
}
if ($this->bypassProxy($urlComponents['host'])) {
// check if page was requested over a secure connection
// but the link is insecure
if (
$urlComponents['scheme'] === 'http'
&& (\MESSAGE_FORCE_SECURE_IMAGES || RouteHandler::secureConnection())
) {
// rewrite protocol to `https`
$element->setAttribute('src', \preg_replace('~^http~', 'https', $src));
}
continue;
}
$element->setAttribute('data-valid', 'true');
if (!empty($urlComponents['path']) && \preg_match('~\.svg~', \basename($urlComponents['path']))) {
// we can't proxy SVG, ignore it
continue;
}
$element->setAttribute('src', $this->getProxyLink($src));
$srcset = $element->getAttribute('srcset');
if ($srcset) {
// simplified regex to check if it appears to be a valid list of sources
if (!\preg_match('~^[^\s]+\s+[0-9\.]+[wx](,\s*[^\s]+\s+[0-9\.]+[wx])*~', $srcset)) {
$element->removeAttribute('srcset');
continue;
}
$sources = \explode(',', $srcset);
$srcset = '';
foreach ($sources as $source) {
$tmp = \preg_split('~\s+~', StringUtil::trim($source));
if (\count($tmp) === 2) {
if (!empty($srcset)) {
$srcset .= ', ';
}
$srcset .= $this->getProxyLink($tmp[0]) . ' ' . $tmp[1];
}
}
$element->setAttribute('srcset', $srcset);
}
} elseif (!\IMAGE_ALLOW_EXTERNAL_SOURCE && !$this->isAllowedOrigin($src)) {
/** @var HtmlOutputNodeProcessor $htmlNodeProcessor */
$this->replaceExternalSource($element, $src, $htmlNodeProcessor->getHtmlProcessor()->enableUgc);
} elseif (\MESSAGE_FORCE_SECURE_IMAGES && Url::parse($src)['scheme'] === 'http') {
// rewrite protocol to `https`
$element->setAttribute('src', \preg_replace('~^http~', 'https', $src));
}
}
}
}
/**
* Replaces images embedded from external sources that are not handled by the image proxy.
*
* @return void
*/
protected function replaceExternalSource(\DOMElement $element, string $src, bool $isUgc = false)
{
$element->parentNode->insertBefore(
$element->ownerDocument->createTextNode(
'[' . WCF::getLanguage()->get('wcf.bbcode.image.blocked') . ': '
),
$element
);
if (!DOMUtil::hasParent($element, 'a')) {
$link = $element->ownerDocument->createElement('a');
$link->setAttribute('href', $src);
$link->textContent = $src;
HtmlOutputNodeA::markLinkAsExternal($link, $isUgc);
} else {
$link = $element->ownerDocument->createTextNode($src);
}
$element->parentNode->insertBefore($link, $element);
$element->parentNode->insertBefore($element->ownerDocument->createTextNode(']'), $element);
$element->parentNode->removeChild($element);
}
/**
* @param string[] $hostnames
* @return callable(string):bool
* @deprecated 5.4 Use Url::getHostnameMatcher().
*/
protected function getHostMatcher(array $hostnames)
{
return Url::getHostnameMatcher($hostnames);
}
/**
* Validates the domain name against the list of own domains
* and whitelisted ones with wildcard support.
*
* @return bool
*/
protected function bypassProxy(string $hostname)
{
static $matcher = null;
if ($matcher === null) {
$whitelist = \explode("\n", StringUtil::unifyNewlines(\IMAGE_PROXY_HOST_WHITELIST));
$whitelist[] = \mb_strtolower(ApplicationHandler::getInstance()->getDomainName());
$matcher = Url::getHostnameMatcher($whitelist);
}
return $matcher($hostname);
}
/**
* Returns the link to fetch the image using the image proxy.
*
* @return string
*/
protected function getProxyLink(string $link)
{
try {
$key = CryptoUtil::createSignedString($link);
return LinkHandler::getInstance()->getControllerLink(ImageProxyAction::class, [
'key' => $key,
]);
} catch (CryptoException $e) {
return $link;
}
}
/**
* @return bool
*/
protected function isAllowedOrigin(string $src)
{
static $matcher = null;
if ($matcher === null) {
$matcher = Url::getHostnameMatcher(
BBCodeHandler::getInstance()->getImageExternalSourceWhitelist()
);
}
$host = Url::parse($src)['host'];
return !$host || $matcher($host);
}
}