From 4411cb1181a482e537e6a3309cf6ecbafa3668be Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Sun, 27 Jan 2019 13:06:35 +0100 Subject: [PATCH] Allow media links generated in the ACP to point to the frontend --- com.woltlab.wcf/templates/mediaBBCodeTag.tpl | 4 +- .../files/acp/templates/mediaBBCodeTag.tpl | 11 +++ .../bbcode/WoltLabSuiteMediaBBCode.class.php | 83 ++++++++++++++++--- 3 files changed, 85 insertions(+), 13 deletions(-) create mode 100644 wcfsetup/install/files/acp/templates/mediaBBCodeTag.tpl diff --git a/com.woltlab.wcf/templates/mediaBBCodeTag.tpl b/com.woltlab.wcf/templates/mediaBBCodeTag.tpl index e80ee2c1ed..9d75f479d5 100644 --- a/com.woltlab.wcf/templates/mediaBBCodeTag.tpl +++ b/com.woltlab.wcf/templates/mediaBBCodeTag.tpl @@ -1,8 +1,8 @@ {if $thumbnailSize != 'original'} - {$media->altText} + {$media->altText} {else} - {$media->altText} + {$media->altText} {/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 index 0000000000..9d75f479d5 --- /dev/null +++ b/wcfsetup/install/files/acp/templates/mediaBBCodeTag.tpl @@ -0,0 +1,11 @@ + + {if $thumbnailSize != 'original'} + {$media->altText} + {else} + {$media->altText} + {/if} + + {if $media->caption} + {$media->caption} + {/if} + diff --git a/wcfsetup/install/files/lib/system/bbcode/WoltLabSuiteMediaBBCode.class.php b/wcfsetup/install/files/lib/system/bbcode/WoltLabSuiteMediaBBCode.class.php index 67eb80f9b8..20d059ee24 100644 --- a/wcfsetup/install/files/lib/system/bbcode/WoltLabSuiteMediaBBCode.class.php +++ b/wcfsetup/install/files/lib/system/bbcode/WoltLabSuiteMediaBBCode.class.php @@ -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; + } } -- 2.20.1