Prevent the generation of HTML anchors if $removeLinks is set
authorAlexander Ebert <ebert@woltlab.com>
Sun, 3 Nov 2019 18:42:29 +0000 (19:42 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Sun, 3 Nov 2019 18:42:29 +0000 (19:42 +0100)
com.woltlab.wcf/templates/mediaBBCodeTag.tpl
wcfsetup/install/files/lib/system/bbcode/HtmlBBCodeParser.class.php
wcfsetup/install/files/lib/system/bbcode/WoltLabSuiteMediaBBCode.class.php
wcfsetup/install/files/lib/system/html/output/node/AbstractHtmlOutputNode.class.php
wcfsetup/install/files/lib/system/html/output/node/HtmlOutputNodeProcessor.class.php
wcfsetup/install/files/lib/system/html/output/node/HtmlOutputNodeWoltlabMetacode.class.php
wcfsetup/install/files/lib/system/html/output/node/IHtmlOutputNode.class.php

index f18d734ab3a4add85910c8a450304a9f633d225e..01a0c795410ca43da94f55210bce72cd3f0c668d 100644 (file)
@@ -1,6 +1,7 @@
+{if !$removeLinks|isset}{assign var='removeLinks' value=false}{/if}
 <span class="mediaBBCode{if $float != 'none'} messageFloatObject{$float|ucfirst}{/if}">
        {if $thumbnailSize != 'original'}
-               <a href="{$mediaLink}" class="embeddedAttachmentLink jsImageViewer"><img src="{$thumbnailLink}" alt="{$media->altText}" title="{$media->title}" data-width="{@$media->getThumbnailWidth($thumbnailSize)}" data-height="{@$media->getThumbnailHeight($thumbnailSize)}"></a>
+               {if !$removeLinks}<a href="{$mediaLink}" class="embeddedAttachmentLink jsImageViewer">{/if}<img src="{$thumbnailLink}" alt="{$media->altText}" title="{$media->title}" data-width="{@$media->getThumbnailWidth($thumbnailSize)}" data-height="{@$media->getThumbnailHeight($thumbnailSize)}">{if !$removeLinks}</a>{/if}
        {else}
                <img src="{$mediaLink}" alt="{$media->altText}" title="{$media->title}" data-width="{@$media->width}" data-height="{@$media->height}">
        {/if}
index 730aa992ea49423f72e97a7257d73cc6b5700920..75683fdd2c203ec51c03940da71b9eb9f3cfe581 100644 (file)
@@ -52,6 +52,12 @@ class HtmlBBCodeParser extends BBCodeParser {
         */
        protected $validBBCodePattern = '~^[a-z](?:[a-z0-9\-_]+)?$~';
        
+       /**
+        * @var bool
+        * @since 5.2
+        */
+       protected $removeLinks;
+       
        /**
         * @inheritDoc
         */
@@ -414,6 +420,22 @@ class HtmlBBCodeParser extends BBCodeParser {
                return $bbcodes;
        }
        
+       /**
+        * @param bool $removeLinks
+        * @since 5.2
+        */
+       public function setRemoveLinks($removeLinks) {
+               $this->removeLinks = $removeLinks;
+       }
+       
+       /**
+        * @return bool
+        * @since 5.2
+        */
+       public function getRemoveLinks() {
+               return $this->removeLinks;
+       }
+       
        /**
         * Compiles tag fragments into the custom HTML element.
         * 
index 444b3fbaf0dfd9d2db8f1c9e09a229eedb16026e..332c2a9adb2450900b86e1d6016bb9dbeb5109b3 100644 (file)
@@ -32,9 +32,22 @@ class WoltLabSuiteMediaBBCode extends AbstractBBCode {
                        return '';
                }
                
+               $removeLinks = false;
+               if ($parser instanceof HtmlBBCodeParser && $parser->getRemoveLinks()) {
+                       $removeLinks = true;
+               }
+               
                /** @var ViewableMedia $media */
                $media = MessageEmbeddedObjectManager::getInstance()->getObject('com.woltlab.wcf.media', $mediaID);
                if ($media !== null && $media->isAccessible()) {
+                       if ($removeLinks && !$media->isImage) {
+                               if ($parser->getOutputType() === 'text/html' || $parser->getOutputType() === 'text/simplified-html') {
+                                       return StringUtil::encodeHTML($media->getTitle());
+                               }
+                               
+                               return StringUtil::encodeHTML($this->getLink($media));
+                       }
+                       
                        if ($parser->getOutputType() == 'text/html') {
                                if ($media->isImage) {
                                        $thumbnailSize = (!empty($openingTag['attributes'][1])) ? $openingTag['attributes'][1] : 'original';
@@ -48,6 +61,7 @@ class WoltLabSuiteMediaBBCode extends AbstractBBCode {
                                        
                                        return WCF::getTPL()->fetch('mediaBBCodeTag', 'wcf', [
                                                'mediaLink' => $this->getLink($media),
+                                               'removeLinks' => $removeLinks,
                                                'thumbnailLink' => $thumbnailSize !== 'original' ? $this->getThumbnailLink($media, $thumbnailSize) : ''
                                        ]);
                                }
index bdc03ffc474637267032b8613c0ebdf2ff305a61..b136aed869fcb1100f5875e95e98ebc69def529b 100644 (file)
@@ -19,6 +19,11 @@ abstract class AbstractHtmlOutputNode extends AbstractHtmlNode implements IHtmlO
         */
        protected $outputType = 'text/html';
        
+       /**
+        * @var bool
+        */
+       protected $removeLinks = false;
+       
        /**
         * @inheritDoc
         */
@@ -33,4 +38,12 @@ abstract class AbstractHtmlOutputNode extends AbstractHtmlNode implements IHtmlO
        public function setOutputType($outputType) {
                $this->outputType = $outputType;
        }
+       
+       /**
+        * @param bool $removeLinks
+        * @since 5.2
+        */
+       public function setRemoveLinks($removeLinks) {
+               $this->removeLinks = $removeLinks;
+       }
 }
index 65eca5bca4cead619c2ce290b6db17155d60ecc0..86160cb32e6aa1146bfa3ebaa446445943b9f166 100644 (file)
@@ -287,6 +287,7 @@ class HtmlOutputNodeProcessor extends AbstractHtmlNodeProcessor {
        protected function invokeHtmlNode(IHtmlNode $htmlNode) {
                /** @var IHtmlOutputNode $htmlNode */
                $htmlNode->setOutputType($this->outputType);
+               $htmlNode->setRemoveLinks($this->getHtmlProcessor()->removeLinks);
                
                parent::invokeHtmlNode($htmlNode);
        }
index 9523d8355b651489f79d6fca9db1f40013ee1a70..b4707290178273cac313d68d95351f79f246ebd4 100644 (file)
@@ -46,6 +46,16 @@ class HtmlOutputNodeWoltlabMetacode extends AbstractHtmlOutputNode {
        public function replaceTag(array $data) {
                HtmlBBCodeParser::getInstance()->setOutputType($this->outputType);
                
-               return HtmlBBCodeParser::getInstance()->getHtmlOutput($data['name'], $data['attributes'], $data['element']);
+               if ($this->removeLinks) {
+                       HtmlBBCodeParser::getInstance()->setRemoveLinks($this->removeLinks);
+               }
+               
+               $output = HtmlBBCodeParser::getInstance()->getHtmlOutput($data['name'], $data['attributes'], $data['element']);
+               
+               if ($this->removeLinks) {
+                       HtmlBBCodeParser::getInstance()->setRemoveLinks(false);
+               }
+               
+               return $output;
        }
 }
index 9fd3e8c5a117ca054be76f86e406586ab4569bec..e8f5797c0e605daf84af188260fc36f88395b2ca 100644 (file)
@@ -18,4 +18,12 @@ interface IHtmlOutputNode extends IHtmlNode {
         * @param       string          $outputType     desired output type
         */
        public function setOutputType($outputType);
+       
+       /**
+        * Requests the HTML output to omit any HTML anchor.
+        * 
+        * @param bool $removeLinks
+        * @since 5.2
+        */
+       public function setRemoveLinks($removeLinks);
 }