Merge remote-tracking branch 'origin/6.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / bbcode / WoltLabSuiteMediaBBCode.class.php
CommitLineData
65f833f2 1<?php
a9229942 2
65f833f2 3namespace wcf\system\bbcode;
a9229942 4
f4b12f07 5use wcf\data\media\ViewableMedia;
65f833f2 6use wcf\system\message\embedded\object\MessageEmbeddedObjectManager;
4411cb11 7use wcf\system\request\LinkHandler;
65f833f2
AE
8use wcf\system\WCF;
9use 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 19final 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);
27f4a03c 50 if ($media === null) {
fa0a7f6f 51 return WCF::getTPL()->fetch('shared_contentNotVisible');
27f4a03c
C
52 }
53
54 if ($media->isAccessible()) {
a9229942
TD
55 if ($removeLinks && !$media->isImage) {
56 if ($parser->getOutputType() === 'text/html' || $parser->getOutputType() === 'text/simplified-html') {
57 return StringUtil::encodeHTML($media->getTitle());
58 }
59
60 return StringUtil::encodeHTML($this->getLink($media));
61 }
62
63 if ($parser->getOutputType() == 'text/html') {
c64a8726
C
64 $float = (!empty($openingTag['attributes'][2])) ? $openingTag['attributes'][2] : 'none';
65
a9229942
TD
66 if ($media->isImage) {
67 $thumbnailSize = (!empty($openingTag['attributes'][1])) ? $openingTag['attributes'][1] : 'original';
09400e13 68 $width = (!empty($openingTag['attributes'][3])) ? $openingTag['attributes'][3] : 'auto';
a9229942 69
dbdea958 70 return WCF::getTPL()->fetch('shared_bbcode_wsm', 'wcf', [
a9229942
TD
71 'mediaLink' => $this->getLink($media),
72 'removeLinks' => $removeLinks,
73 'thumbnailLink' => $thumbnailSize !== 'original' ? $this->getThumbnailLink(
74 $media,
75 $thumbnailSize
76 ) : '',
a5e6ee4c
MW
77 'float' => $float,
78 'media' => $media->getLocalizedVersion(MessageEmbeddedObjectManager::getInstance()->getActiveMessageLanguageID()),
79 'thumbnailSize' => $thumbnailSize,
09400e13 80 'width' => $width,
a5e6ee4c 81 ]);
91d068c4 82 } elseif ($media->isVideo() || $media->isAudio()) {
dbdea958 83 return WCF::getTPL()->fetch('shared_bbcode_wsm', 'wcf', [
a5e6ee4c
MW
84 'mediaLink' => $this->getLink($media),
85 'removeLinks' => $removeLinks,
c64a8726 86 'float' => $float,
a5e6ee4c 87 'media' => $media->getLocalizedVersion(MessageEmbeddedObjectManager::getInstance()->getActiveMessageLanguageID()),
4bd38935 88 'width' => 'auto',
a9229942
TD
89 ]);
90 }
91
92 return StringUtil::getAnchorTag($this->getLink($media), $media->getTitle());
93 } elseif ($parser->getOutputType() == 'text/simplified-html') {
94 return StringUtil::getAnchorTag($this->getLink($media), $media->getTitle());
95 }
96
97 return StringUtil::encodeHTML($this->getLink($media));
27f4a03c 98 } else {
fa0a7f6f 99 return WCF::getTPL()->fetch('shared_contentNotVisible', 'wcf', [
b25f6f0e 100 'message' => WCF::getLanguage()->getDynamicVariable('wcf.message.content.no.permission.title')
27f4a03c 101 ], true);
a9229942 102 }
a9229942
TD
103 }
104
105 /**
106 * Returns the link to the given media file (while considering the value of `$forceFrontendLinks`).
107 *
108 * @param ViewableMedia $media linked media file
109 * @return string link to media file
110 */
111 protected function getLink(ViewableMedia $media)
112 {
113 if (self::$forceFrontendLinks) {
114 return LinkHandler::getInstance()->getLink('Media', [
115 'forceFrontend' => 'true',
116 'object' => $media,
117 ]);
118 }
119
120 return $media->getLink();
121 }
122
123 /**
124 * Returns the thumbnail link to the given media file (while considering the value of `$forceFrontendLinks`).
125 *
126 * @param ViewableMedia $media linked media file
127 * @param string $thumbnailSize thumbnail size
128 * @return string link to media thumbnail
129 */
130 protected function getThumbnailLink(ViewableMedia $media, $thumbnailSize)
131 {
132 // use `Media::getThumbnailLink()` to validate thumbnail size
133 $thumbnailLink = $media->getThumbnailLink($thumbnailSize);
134
135 if (self::$forceFrontendLinks) {
136 if (!$this->{$thumbnailSize . 'ThumbnailType'}) {
137 return $this->getLink($media);
138 }
139
140 return LinkHandler::getInstance()->getLink('Media', [
141 'forceFrontend' => 'true',
142 'object' => $media,
143 'thumbnail' => $thumbnailSize,
144 ]);
145 }
146
147 return $thumbnailLink;
148 }
65f833f2 149}