[TASK] Reduce logic in render methods of YouTube and Vimeo Renderer
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Rendering / YouTubeRenderer.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Rendering;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use TYPO3\CMS\Core\Resource\File;
17 use TYPO3\CMS\Core\Resource\FileInterface;
18 use TYPO3\CMS\Core\Resource\FileReference;
19 use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperInterface;
20 use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Core\Utility\MathUtility;
23
24 /**
25 * YouTube renderer class
26 */
27 class YouTubeRenderer implements FileRendererInterface
28 {
29 /**
30 * @var OnlineMediaHelperInterface
31 */
32 protected $onlineMediaHelper;
33
34 /**
35 * Returns the priority of the renderer
36 * This way it is possible to define/overrule a renderer
37 * for a specific file type/context.
38 * For example create a video renderer for a certain storage/driver type.
39 * Should be between 1 and 100, 100 is more important than 1
40 *
41 * @return int
42 */
43 public function getPriority()
44 {
45 return 1;
46 }
47
48 /**
49 * Check if given File(Reference) can be rendered
50 *
51 * @param FileInterface $file File of FileReference to render
52 * @return bool
53 */
54 public function canRender(FileInterface $file)
55 {
56 return ($file->getMimeType() === 'video/youtube' || $file->getExtension() === 'youtube') && $this->getOnlineMediaHelper($file) !== false;
57 }
58
59 /**
60 * Get online media helper
61 *
62 * @param FileInterface $file
63 * @return bool|OnlineMediaHelperInterface
64 */
65 protected function getOnlineMediaHelper(FileInterface $file)
66 {
67 if ($this->onlineMediaHelper === null) {
68 $orgFile = $file;
69 if ($orgFile instanceof FileReference) {
70 $orgFile = $orgFile->getOriginalFile();
71 }
72 if ($orgFile instanceof File) {
73 $this->onlineMediaHelper = OnlineMediaHelperRegistry::getInstance()->getOnlineMediaHelper($orgFile);
74 } else {
75 $this->onlineMediaHelper = false;
76 }
77 }
78 return $this->onlineMediaHelper;
79 }
80
81 /**
82 * Render for given File(Reference) html output
83 *
84 * @param FileInterface $file
85 * @param int|string $width TYPO3 known format; examples: 220, 200m or 200c
86 * @param int|string $height TYPO3 known format; examples: 220, 200m or 200c
87 * @param array $options
88 * @param bool $usedPathsRelativeToCurrentScript See $file->getPublicUrl()
89 * @return string
90 */
91 public function render(FileInterface $file, $width, $height, array $options = [], $usedPathsRelativeToCurrentScript = false)
92 {
93 $options = $this->collectOptions($options, $file);
94 $src = $this->createYouTubeUrl($options, $file);
95 $attributes = $this->collectIframeAttributes($width, $height, $options);
96
97 return sprintf(
98 '<iframe src="%s"%s></iframe>',
99 $src,
100 empty($attributes) ? '' : ' ' . implode(' ', $attributes)
101 );
102 }
103
104 /**
105 * @param array $options
106 * @param FileInterface $file
107 * @return array
108 */
109 protected function collectOptions(array $options, FileInterface $file)
110 {
111 // Check for an autoplay option at the file reference itself, if not overridden yet.
112 if (!isset($options['autoplay']) && $file instanceof FileReference) {
113 $autoplay = $file->getProperty('autoplay');
114 if ($autoplay !== null) {
115 $options['autoplay'] = $autoplay;
116 }
117 }
118
119 $options['controls'] = $options['controls'] ?? 2;
120 $options['controls'] = MathUtility::canBeInterpretedAsInteger($options['controls']) ? MathUtility::forceIntegerInRange($options['controls'], 0, 2) : 2;
121
122 if (!isset($options['allow'])) {
123 $options['allow'] = 'fullscreen';
124 if (!empty($options['autoplay'])) {
125 $options['allow'] = 'autoplay; fullscreen';
126 }
127 }
128 return $options;
129 }
130
131 /**
132 * @param array $options
133 * @param FileInterface $file
134 * @return string
135 */
136 protected function createYouTubeUrl(array $options, FileInterface $file)
137 {
138 $videoId = $this->getVideoIdFromFile($file);
139
140 $urlParams = ['autohide=1'];
141 $urlParams[] = 'controls=' . $options['controls'];
142 if (!empty($options['autoplay'])) {
143 $urlParams[] = 'autoplay=1';
144 }
145 if (!empty($options['modestbranding'])) {
146 $urlParams[] = 'modestbranding=1';
147 }
148 if (!empty($options['loop'])) {
149 $urlParams[] = 'loop=1&amp;playlist=' . $videoId;
150 }
151 if (isset($options['relatedVideos'])) {
152 $urlParams[] = 'rel=' . (int)(bool)$options['relatedVideos'];
153 }
154 if (!isset($options['enablejsapi']) || !empty($options['enablejsapi'])) {
155 $urlParams[] = 'enablejsapi=1&amp;origin=' . rawurlencode(GeneralUtility::getIndpEnv('TYPO3_REQUEST_HOST'));
156 }
157 $urlParams[] = 'showinfo=' . (int)!empty($options['showinfo']);
158
159 $youTubeUrl = sprintf(
160 'https://www.youtube%s.com/embed/%s?%s',
161 !isset($options['no-cookie']) || !empty($options['no-cookie']) ? '-nocookie' : '',
162 $videoId,
163 implode('&amp;', $urlParams)
164 );
165
166 return $youTubeUrl;
167 }
168
169 /**
170 * @param FileInterface $file
171 * @return string
172 */
173 protected function getVideoIdFromFile(FileInterface $file)
174 {
175 if ($file instanceof FileReference) {
176 $orgFile = $file->getOriginalFile();
177 } else {
178 $orgFile = $file;
179 }
180
181 return $this->getOnlineMediaHelper($file)->getOnlineMediaId($orgFile);
182 }
183
184 /**
185 * @param int|string $width
186 * @param int|string $height
187 * @param array $options
188 * @return array
189 */
190 protected function collectIframeAttributes($width, $height, array $options)
191 {
192 $attributes = ['allowfullscreen'];
193
194 if (isset($options['additionalAttributes']) && is_array($options['additionalAttributes'])) {
195 $attributes[] = GeneralUtility::implodeAttributes($options['additionalAttributes'], true, true);
196 }
197 if (isset($options['data']) && is_array($options['data'])) {
198 array_walk($options['data'], function (&$value, $key) {
199 $value = 'data-' . htmlspecialchars($key) . '="' . htmlspecialchars($value) . '"';
200 });
201 $attributes[] = implode(' ', $options['data']);
202 }
203 if ((int)$width > 0) {
204 $attributes[] = 'width="' . (int)$width . '"';
205 }
206 if ((int)$height > 0) {
207 $attributes[] = 'height="' . (int)$height . '"';
208 }
209 if (isset($GLOBALS['TSFE']) && is_object($GLOBALS['TSFE']) && $GLOBALS['TSFE']->config['config']['doctype'] !== 'html5') {
210 $attributes[] = 'frameborder="0"';
211 }
212 foreach (['class', 'dir', 'id', 'lang', 'style', 'title', 'accesskey', 'tabindex', 'onclick', 'poster', 'preload', 'allow'] as $key) {
213 if (!empty($options[$key])) {
214 $attributes[] = $key . '="' . htmlspecialchars($options[$key]) . '"';
215 }
216 }
217
218 return $attributes;
219 }
220 }