Added support for image proxy
authorAlexander Ebert <ebert@woltlab.com>
Tue, 16 Aug 2016 18:06:31 +0000 (20:06 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Tue, 16 Aug 2016 18:06:31 +0000 (20:06 +0200)
wcfsetup/install/files/lib/system/html/output/node/HtmlOutputNodeImg.class.php [new file with mode: 0644]

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 (file)
index 0000000..253be2e
--- /dev/null
@@ -0,0 +1,72 @@
+<?php
+namespace wcf\system\html\output\node;
+use wcf\system\html\node\AbstractHtmlNodeProcessor;
+use wcf\system\request\LinkHandler;
+use wcf\system\WCF;
+use wcf\util\CryptoUtil;
+use wcf\util\exception\CryptoException;
+
+/**
+ * Processes images.
+ * 
+ * @author      Alexander Ebert
+ * @copyright   2001-2016 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @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;
+               }
+       }
+}