‪TYPO3CMS  10.4
YouTubeRenderer.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
24 
29 {
33  protected ‪$onlineMediaHelper;
34 
44  public function ‪getPriority()
45  {
46  return 1;
47  }
48 
55  public function ‪canRender(‪FileInterface $file)
56  {
57  return ($file->‪getMimeType() === 'video/youtube' || $file->‪getExtension() === 'youtube') && $this->‪getOnlineMediaHelper($file) !== false;
58  }
59 
66  protected function ‪getOnlineMediaHelper(‪FileInterface $file)
67  {
68  if ($this->onlineMediaHelper === null) {
69  $orgFile = $file;
70  if ($orgFile instanceof ‪FileReference) {
71  $orgFile = $orgFile->getOriginalFile();
72  }
73  if ($orgFile instanceof ‪File) {
74  $this->onlineMediaHelper = ‪OnlineMediaHelperRegistry::getInstance()->‪getOnlineMediaHelper($orgFile);
75  } else {
76  $this->onlineMediaHelper = false;
77  }
78  }
80  }
81 
92  public function ‪render(‪FileInterface $file, $width, $height, array $options = [], $usedPathsRelativeToCurrentScript = false)
93  {
94  $options = $this->‪collectOptions($options, $file);
95  $src = $this->‪createYouTubeUrl($options, $file);
96  $attributes = $this->‪collectIframeAttributes($width, $height, $options);
97 
98  return sprintf(
99  '<iframe src="%s"%s></iframe>',
100  htmlspecialchars($src, ENT_QUOTES | ENT_HTML5),
101  empty($attributes) ? '' : ' ' . $this->‪implodeAttributes($attributes)
102  );
103  }
104 
110  protected function ‪collectOptions(array $options, ‪FileInterface $file)
111  {
112  // Check for an autoplay option at the file reference itself, if not overridden yet.
113  if (!isset($options['autoplay']) && $file instanceof ‪FileReference) {
114  $autoplay = $file->‪getProperty('autoplay');
115  if ($autoplay !== null) {
116  $options['autoplay'] = $autoplay;
117  }
118  }
119 
120  $showPlayerControls = 1;
121  $options['controls'] = (int)!empty($options['controls'] ?? $showPlayerControls);
122 
123  if (!isset($options['allow'])) {
124  $options['allow'] = 'fullscreen';
125  if (!empty($options['autoplay'])) {
126  $options['allow'] = 'autoplay; fullscreen';
127  }
128  }
129  return $options;
130  }
131 
137  protected function ‪createYouTubeUrl(array $options, ‪FileInterface $file)
138  {
139  $videoId = $this->‪getVideoIdFromFile($file);
140 
141  $urlParams = ['autohide=1'];
142  $urlParams[] = 'controls=' . $options['controls'];
143  if (!empty($options['autoplay'])) {
144  $urlParams[] = 'autoplay=1';
145  // If autoplay is enabled, enforce mute=1, see https://developer.chrome.com/blog/autoplay/
146  $urlParams[] = 'mute=1';
147  }
148  if (!empty($options['modestbranding'])) {
149  $urlParams[] = 'modestbranding=1';
150  }
151  if (!empty($options['loop'])) {
152  $urlParams[] = 'loop=1&playlist=' . rawurlencode($videoId);
153  }
154  if (isset($options['relatedVideos'])) {
155  $urlParams[] = 'rel=' . (int)(bool)$options['relatedVideos'];
156  }
157  if (!isset($options['enablejsapi']) || !empty($options['enablejsapi'])) {
158  $urlParams[] = 'enablejsapi=1&origin=' . rawurlencode(GeneralUtility::getIndpEnv('TYPO3_REQUEST_HOST'));
159  }
160 
161  $youTubeUrl = sprintf(
162  'https://www.youtube%s.com/embed/%s?%s',
163  !isset($options['no-cookie']) || !empty($options['no-cookie']) ? '-nocookie' : '',
164  rawurlencode($videoId),
165  implode('&', $urlParams)
166  );
167 
168  return $youTubeUrl;
169  }
170 
175  protected function ‪getVideoIdFromFile(‪FileInterface $file)
176  {
177  if ($file instanceof ‪FileReference) {
178  $orgFile = $file->getOriginalFile();
179  } else {
180  $orgFile = $file;
181  }
182 
183  return $this->‪getOnlineMediaHelper($file)->‪getOnlineMediaId($orgFile);
184  }
185 
192  protected function ‪collectIframeAttributes($width, $height, array $options)
193  {
194  $attributes = [];
195  $attributes['allowfullscreen'] = true;
196 
197  if (isset($options['additionalAttributes']) && is_array($options['additionalAttributes'])) {
198  $attributes = array_merge($attributes, $options['additionalAttributes']);
199  }
200  if (isset($options['data']) && is_array($options['data'])) {
201  array_walk($options['data'], function (&$value, $key) use (&$attributes) {
202  $attributes['data-' . $key] = $value;
203  });
204  }
205  if ((int)$width > 0) {
206  $attributes['width'] = (int)$width;
207  }
208  if ((int)$height > 0) {
209  $attributes['height'] = (int)$height;
210  }
211  if (isset(‪$GLOBALS['TSFE']) && is_object(‪$GLOBALS['TSFE']) && (isset(‪$GLOBALS['TSFE']->config['config']['doctype']) && ‪$GLOBALS['TSFE']->config['config']['doctype'] !== 'html5')) {
212  $attributes['frameborder'] = 0;
213  }
214  foreach (['class', 'dir', 'id', 'lang', 'style', 'title', 'accesskey', 'tabindex', 'onclick', 'poster', 'preload', 'allow'] as $key) {
215  if (!empty($options[$key])) {
216  $attributes[$key] = $options[$key];
217  }
218  }
219 
220  return $attributes;
221  }
222 
228  protected function ‪implodeAttributes(array $attributes): string
229  {
230  $attributeList = [];
231  foreach ($attributes as $name => $value) {
232  $name = preg_replace('/[^\p{L}0-9_.-]/u', '', $name);
233  if ($value === true) {
234  $attributeList[] = $name;
235  } else {
236  $attributeList[] = $name . '="' . htmlspecialchars($value, ENT_QUOTES | ENT_HTML5) . '"';
237  }
238  }
239  return implode(' ', $attributeList);
240  }
241 }
‪TYPO3\CMS\Core\Resource\FileInterface\getExtension
‪string getExtension()
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry
Definition: OnlineMediaHelperRegistry.php:27
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\getOnlineMediaHelper
‪bool OnlineMediaHelperInterface getOnlineMediaHelper(FileInterface $file)
Definition: YouTubeRenderer.php:65
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:22
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\getVideoIdFromFile
‪string getVideoIdFromFile(FileInterface $file)
Definition: YouTubeRenderer.php:174
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:33
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\collectOptions
‪array collectOptions(array $options, FileInterface $file)
Definition: YouTubeRenderer.php:109
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\$onlineMediaHelper
‪OnlineMediaHelperInterface $onlineMediaHelper
Definition: YouTubeRenderer.php:32
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\implodeAttributes
‪string implodeAttributes(array $attributes)
Definition: YouTubeRenderer.php:227
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\getPriority
‪int getPriority()
Definition: YouTubeRenderer.php:43
‪TYPO3\CMS\Core\Resource\FileInterface\getProperty
‪string getProperty($key)
‪TYPO3\CMS\Core\Resource\Rendering
Definition: AudioTagRenderer.php:16
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry\getOnlineMediaHelper
‪bool OnlineMediaHelperInterface getOnlineMediaHelper(File $file)
Definition: OnlineMediaHelperRegistry.php:55
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\canRender
‪bool canRender(FileInterface $file)
Definition: YouTubeRenderer.php:54
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface
Definition: FileRendererInterface.php:25
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry\getInstance
‪static OnlineMediaHelperRegistry getInstance()
Definition: OnlineMediaHelperRegistry.php:33
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer
Definition: YouTubeRenderer.php:29
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\createYouTubeUrl
‪string createYouTubeUrl(array $options, FileInterface $file)
Definition: YouTubeRenderer.php:136
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperInterface
Definition: OnlineMediaHelperInterface.php:25
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\collectIframeAttributes
‪array collectIframeAttributes($width, $height, array $options)
Definition: YouTubeRenderer.php:191
‪TYPO3\CMS\Core\Resource\FileInterface\getMimeType
‪string getMimeType()
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\render
‪string render(FileInterface $file, $width, $height, array $options=[], $usedPathsRelativeToCurrentScript=false)
Definition: YouTubeRenderer.php:91
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperInterface\getOnlineMediaId
‪string getOnlineMediaId(File $file)