From 52d7ce1a859f7073a9122473a7631836dc4fca94 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Tue, 16 Aug 2016 20:06:31 +0200 Subject: [PATCH] Added support for image proxy --- .../output/node/HtmlOutputNodeImg.class.php | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 wcfsetup/install/files/lib/system/html/output/node/HtmlOutputNodeImg.class.php diff --git a/wcfsetup/install/files/lib/system/html/output/node/HtmlOutputNodeImg.class.php b/wcfsetup/install/files/lib/system/html/output/node/HtmlOutputNodeImg.class.php new file mode 100644 index 0000000000..253be2eb9f --- /dev/null +++ b/wcfsetup/install/files/lib/system/html/output/node/HtmlOutputNodeImg.class.php @@ -0,0 +1,72 @@ + + * @package WoltLabSuite\Core\System\Html\Output\Node + * @since 3.0 + */ +class HtmlOutputNodeImg extends AbstractHtmlOutputNode { + /** + * @inheritDoc + */ + protected $tagName = 'img'; + + /** + * @inheritDoc + */ + public function process(array $elements, AbstractHtmlNodeProcessor $htmlNodeProcessor) { + if (!MODULE_IMAGE_PROXY) { + return; + } + + /** @var \DOMElement $element */ + foreach ($elements as $element) { + $src = $element->getAttribute('src'); + if ($src) { + $element->setAttribute('src', $this->getProxyLink($src)); + } + } + } + + /** + * Returns the link to the cached image (or the link to fetch the image + * using the image proxy). + * + * @param string $link + * @return string + * @since 3.0 + */ + protected function getProxyLink($link) { + try { + $key = CryptoUtil::createSignedString($link); + // does not need to be secure, just sufficiently "random" + $fileName = sha1($key); + + $fileExtension = pathinfo($link, PATHINFO_EXTENSION); + + $path = 'images/proxy/'.substr($fileName, 0, 2).'/'.$fileName.($fileExtension ? '.'.$fileExtension : ''); + + $fileLocation = WCF_DIR.$path; + if (file_exists($fileLocation)) { + return WCF::getPath().$path; + } + + return LinkHandler::getInstance()->getLink('ImageProxy', [ + 'key' => $key + ]); + } + catch (CryptoException $e) { + return $link; + } + } +} -- 2.20.1