Allow media links generated in the ACP to point to the frontend
authorMatthias Schmidt <gravatronics@live.com>
Sun, 27 Jan 2019 12:06:35 +0000 (13:06 +0100)
committerMatthias Schmidt <gravatronics@live.com>
Sun, 27 Jan 2019 12:06:35 +0000 (13:06 +0100)
com.woltlab.wcf/templates/mediaBBCodeTag.tpl
wcfsetup/install/files/acp/templates/mediaBBCodeTag.tpl [new file with mode: 0644]
wcfsetup/install/files/lib/system/bbcode/WoltLabSuiteMediaBBCode.class.php

index e80ee2c1ed336c4cd524b29e88c840d40cb3294f..9d75f479d5aea1ac4ec349788dd4b203e9fb9d03 100644 (file)
@@ -1,8 +1,8 @@
 <span class="mediaBBCode{if $float != 'none'} messageFloatObject{$float|ucfirst}{/if}">
        {if $thumbnailSize != 'original'}
-               <a href="{$media->getLink()}" class="embeddedAttachmentLink jsImageViewer"><img src="{$media->getThumbnailLink($thumbnailSize)}" alt="{$media->altText}" title="{$media->title}" data-width="{@$media->getThumbnailWidth($thumbnailSize)}" data-height="{@$media->getThumbnailHeight($thumbnailSize)}"></a>
+               <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>
        {else}
-               <img src="{$media->getLink()}" alt="{$media->altText}" title="{$media->title}" data-width="{@$media->width}" data-height="{@$media->height}">
+               <img src="{$mediaLink}" alt="{$media->altText}" title="{$media->title}" data-width="{@$media->width}" data-height="{@$media->height}">
        {/if}
        
        {if $media->caption}
diff --git a/wcfsetup/install/files/acp/templates/mediaBBCodeTag.tpl b/wcfsetup/install/files/acp/templates/mediaBBCodeTag.tpl
new file mode 100644 (file)
index 0000000..9d75f47
--- /dev/null
@@ -0,0 +1,11 @@
+<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>
+       {else}
+               <img src="{$mediaLink}" alt="{$media->altText}" title="{$media->title}" data-width="{@$media->width}" data-height="{@$media->height}">
+       {/if}
+       
+       {if $media->caption}
+               <span class="mediaBBCodeCaption">{$media->caption}</span>
+       {/if}
+</span>
index 67eb80f9b83d7f51322bdde880ebec3875474c60..20d059ee24714549bc134b46dbff7c2e13f3305e 100644 (file)
@@ -2,6 +2,7 @@
 namespace wcf\system\bbcode;
 use wcf\data\media\ViewableMedia;
 use wcf\system\message\embedded\object\MessageEmbeddedObjectManager;
+use wcf\system\request\LinkHandler;
 use wcf\system\WCF;
 use wcf\util\StringUtil;
 
@@ -15,6 +16,13 @@ use wcf\util\StringUtil;
  * @since       3.0
  */
 class WoltLabSuiteMediaBBCode extends AbstractBBCode {
+       /**
+        * forces media links to be linked to the frontend
+        * after it has been set to `true`, it should be set to `false` again as soon as possible
+        * @var boolean
+        */
+       public static $forceFrontendLinks = false;
+       
        /**
         * @inheritDoc
         */
@@ -27,22 +35,75 @@ class WoltLabSuiteMediaBBCode extends AbstractBBCode {
                /** @var ViewableMedia $media */
                $media = MessageEmbeddedObjectManager::getInstance()->getObject('com.woltlab.wcf.media', $mediaID);
                if ($media !== null && $media->isAccessible()) {
-                       if ($media->isImage) {
-                               $thumbnailSize = (!empty($openingTag['attributes'][1])) ? $openingTag['attributes'][1] : 'original';
-                               $float = (!empty($openingTag['attributes'][2])) ? $openingTag['attributes'][2] : 'none';
-                               
-                               WCF::getTPL()->assign([
-                                       'float' => $float,
-                                       'media' => $media->getLocalizedVersion(MessageEmbeddedObjectManager::getInstance()->getActiveMessageLanguageID()),
-                                       'thumbnailSize' => $thumbnailSize
-                               ]);
+                       if ($parser->getOutputType() == 'text/html') {
+                               if ($media->isImage) {
+                                       $thumbnailSize = (!empty($openingTag['attributes'][1])) ? $openingTag['attributes'][1] : 'original';
+                                       $float = (!empty($openingTag['attributes'][2])) ? $openingTag['attributes'][2] : 'none';
+                                       
+                                       WCF::getTPL()->assign([
+                                               'float' => $float,
+                                               'media' => $media->getLocalizedVersion(MessageEmbeddedObjectManager::getInstance()->getActiveMessageLanguageID()),
+                                               'thumbnailSize' => $thumbnailSize
+                                       ]);
+                                       
+                                       return WCF::getTPL()->fetch('mediaBBCodeTag', 'wcf', [
+                                               'mediaLink' => $this->getLink($media),
+                                               'thumbnailLink' => $thumbnailSize !== 'original' ? $this->getThumbnailLink($media, $thumbnailSize) : ''
+                                       ]);
+                               }
                                
-                               return WCF::getTPL()->fetch('mediaBBCodeTag', 'wcf');
+                               return StringUtil::getAnchorTag($this->getLink($media), $media->getTitle());
+                       }
+                       else if ($parser->getOutputType() == 'text/simplified-html') {
+                               return StringUtil::getAnchorTag($this->getLink($media), $media->getTitle());
                        }
                        
-                       return StringUtil::getAnchorTag($media->getLink(), $media->getTitle());
+                       return StringUtil::encodeHTML($this->getLink($media));
                }
                
                return '';
        }
+       
+       /**
+        * Returns the link to the given media file (while considering the value of `$forceFrontendLinks`).
+        * 
+        * @param       ViewableMedia   $media          linked media file
+        * @return      string                          link to media file
+        */
+       protected function getLink(ViewableMedia $media) {
+               if (self::$forceFrontendLinks) {
+                       return LinkHandler::getInstance()->getLink('Media', [
+                               'forceFrontend' => 'true',
+                               'object' => $media
+                       ]);
+               }
+               
+               return $media->getLink();
+       }
+       
+       /**
+        * Returns the thumbnail link to the given media file (while considering the value of `$forceFrontendLinks`).
+        * 
+        * @param       ViewableMedia   $media          linked media file
+        * @param       string  $thumbnailSize          thumbnail size
+        * @return      string                          link to media thumbnail
+        */
+       protected function getThumbnailLink(ViewableMedia $media, $thumbnailSize) {
+               // use `Media::getThumbnailLink()` to validate thumbnail size
+               $thumbnailLink = $media->getThumbnailLink($thumbnailSize);
+               
+               if (self::$forceFrontendLinks) {
+                       if (!$this->{$thumbnailSize.'ThumbnailType'}) {
+                               return $this->getLink($media);
+                       }
+                       
+                       return LinkHandler::getInstance()->getLink('Media', [
+                               'forceFrontend' => 'true',
+                               'object' => $media,
+                               'thumbnail' => $thumbnailSize
+                       ]);
+               }
+               
+               return $thumbnailLink;
+       }
 }