Commit | Line | Data |
---|---|---|
65f833f2 | 1 | <?php |
a9229942 | 2 | |
65f833f2 | 3 | namespace wcf\system\bbcode; |
a9229942 | 4 | |
f4b12f07 | 5 | use wcf\data\media\ViewableMedia; |
65f833f2 | 6 | use wcf\system\message\embedded\object\MessageEmbeddedObjectManager; |
4411cb11 | 7 | use wcf\system\request\LinkHandler; |
65f833f2 AE |
8 | use wcf\system\WCF; |
9 | use wcf\util\StringUtil; | |
10 | ||
11 | /** | |
12 | * Parses the [wsm] bbcode tag. | |
a9229942 TD |
13 | * |
14 | * @author Alexander Ebert | |
15 | * @copyright 2001-2019 WoltLab GmbH | |
16 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
65f833f2 AE |
17 | * @since 3.0 |
18 | */ | |
a865a92b | 19 | final class WoltLabSuiteMediaBBCode extends AbstractBBCode |
a9229942 TD |
20 | { |
21 | /** | |
22 | * forces media links to be linked to the frontend | |
23 | * after it has been set to `true`, it should be set to `false` again as soon as possible | |
24 | * @var bool | |
25 | */ | |
26 | public static $forceFrontendLinks = false; | |
27 | ||
28 | /** | |
29 | * @inheritDoc | |
30 | */ | |
1457e90f | 31 | public function getParsedTag(array $openingTag, $content, array $closingTag, BBCodeParser $parser): string |
a9229942 TD |
32 | { |
33 | $mediaID = (!empty($openingTag['attributes'][0])) ? \intval($openingTag['attributes'][0]) : 0; | |
34 | if (!$mediaID) { | |
35 | return ''; | |
36 | } | |
37 | ||
38 | $removeLinks = false; | |
4bc88290 MW |
39 | /** @var \DOMElement $element */ |
40 | $element = $closingTag['__parents'][0] ?? null; | |
41 | if ($element && $element->nodeName === 'a') { | |
42 | // We do permit media elements to be nested inside a link, but we must suppress | |
43 | // the thumbnail link to be generated. Removing the link technically is meant | |
44 | // to be something else, but we use it here for backward compatibility. | |
a9229942 | 45 | $removeLinks = true; |
a9229942 | 46 | } |
a5e6ee4c | 47 | |
a9229942 TD |
48 | /** @var ViewableMedia $media */ |
49 | $media = MessageEmbeddedObjectManager::getInstance()->getObject('com.woltlab.wcf.media', $mediaID); | |
50 | if ($media !== null && $media->isAccessible()) { | |
51 | if ($removeLinks && !$media->isImage) { | |
52 | if ($parser->getOutputType() === 'text/html' || $parser->getOutputType() === 'text/simplified-html') { | |
53 | return StringUtil::encodeHTML($media->getTitle()); | |
54 | } | |
55 | ||
56 | return StringUtil::encodeHTML($this->getLink($media)); | |
57 | } | |
58 | ||
59 | if ($parser->getOutputType() == 'text/html') { | |
60 | if ($media->isImage) { | |
61 | $thumbnailSize = (!empty($openingTag['attributes'][1])) ? $openingTag['attributes'][1] : 'original'; | |
62 | $float = (!empty($openingTag['attributes'][2])) ? $openingTag['attributes'][2] : 'none'; | |
09400e13 | 63 | $width = (!empty($openingTag['attributes'][3])) ? $openingTag['attributes'][3] : 'auto'; |
a9229942 | 64 | |
a9229942 TD |
65 | return WCF::getTPL()->fetch('mediaBBCodeTag', 'wcf', [ |
66 | 'mediaLink' => $this->getLink($media), | |
67 | 'removeLinks' => $removeLinks, | |
68 | 'thumbnailLink' => $thumbnailSize !== 'original' ? $this->getThumbnailLink( | |
69 | $media, | |
70 | $thumbnailSize | |
71 | ) : '', | |
a5e6ee4c MW |
72 | 'float' => $float, |
73 | 'media' => $media->getLocalizedVersion(MessageEmbeddedObjectManager::getInstance()->getActiveMessageLanguageID()), | |
74 | 'thumbnailSize' => $thumbnailSize, | |
09400e13 | 75 | 'width' => $width, |
a5e6ee4c | 76 | ]); |
91d068c4 | 77 | } elseif ($media->isVideo() || $media->isAudio()) { |
a5e6ee4c MW |
78 | return WCF::getTPL()->fetch('mediaBBCodeTag', 'wcf', [ |
79 | 'mediaLink' => $this->getLink($media), | |
80 | 'removeLinks' => $removeLinks, | |
5ab2d6f9 | 81 | 'float' => 'none', |
a5e6ee4c | 82 | 'media' => $media->getLocalizedVersion(MessageEmbeddedObjectManager::getInstance()->getActiveMessageLanguageID()), |
a9229942 TD |
83 | ]); |
84 | } | |
85 | ||
86 | return StringUtil::getAnchorTag($this->getLink($media), $media->getTitle()); | |
87 | } elseif ($parser->getOutputType() == 'text/simplified-html') { | |
88 | return StringUtil::getAnchorTag($this->getLink($media), $media->getTitle()); | |
89 | } | |
90 | ||
91 | return StringUtil::encodeHTML($this->getLink($media)); | |
92 | } | |
93 | ||
94 | return ''; | |
95 | } | |
96 | ||
97 | /** | |
98 | * Returns the link to the given media file (while considering the value of `$forceFrontendLinks`). | |
99 | * | |
100 | * @param ViewableMedia $media linked media file | |
101 | * @return string link to media file | |
102 | */ | |
103 | protected function getLink(ViewableMedia $media) | |
104 | { | |
105 | if (self::$forceFrontendLinks) { | |
106 | return LinkHandler::getInstance()->getLink('Media', [ | |
107 | 'forceFrontend' => 'true', | |
108 | 'object' => $media, | |
109 | ]); | |
110 | } | |
111 | ||
112 | return $media->getLink(); | |
113 | } | |
114 | ||
115 | /** | |
116 | * Returns the thumbnail link to the given media file (while considering the value of `$forceFrontendLinks`). | |
117 | * | |
118 | * @param ViewableMedia $media linked media file | |
119 | * @param string $thumbnailSize thumbnail size | |
120 | * @return string link to media thumbnail | |
121 | */ | |
122 | protected function getThumbnailLink(ViewableMedia $media, $thumbnailSize) | |
123 | { | |
124 | // use `Media::getThumbnailLink()` to validate thumbnail size | |
125 | $thumbnailLink = $media->getThumbnailLink($thumbnailSize); | |
126 | ||
127 | if (self::$forceFrontendLinks) { | |
128 | if (!$this->{$thumbnailSize . 'ThumbnailType'}) { | |
129 | return $this->getLink($media); | |
130 | } | |
131 | ||
132 | return LinkHandler::getInstance()->getLink('Media', [ | |
133 | 'forceFrontend' => 'true', | |
134 | 'object' => $media, | |
135 | 'thumbnail' => $thumbnailSize, | |
136 | ]); | |
137 | } | |
138 | ||
139 | return $thumbnailLink; | |
140 | } | |
65f833f2 | 141 | } |