‪TYPO3CMS  ‪main
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 
25 
30 {
34  protected ‪$onlineMediaHelper;
35 
45  public function ‪getPriority()
46  {
47  return 1;
48  }
49 
56  public function ‪canRender(‪FileInterface $file)
57  {
58  return ($file->getMimeType() === 'video/youtube' || $file->getExtension() === 'youtube') && $this->‪getOnlineMediaHelper($file) !== false;
59  }
60 
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 = GeneralUtility::makeInstance(OnlineMediaHelperRegistry::class)->getOnlineMediaHelper($orgFile);
75  } else {
76  $this->onlineMediaHelper = false;
77  }
78  }
80  }
81 
89  public function ‪render(‪FileInterface $file, $width, $height, array $options = [])
90  {
91  $options = $this->‪collectOptions($options, $file);
92  $src = $this->‪createYouTubeUrl($options, $file);
93  $attributes = $this->‪collectIframeAttributes($width, $height, $options);
94 
95  return sprintf(
96  '<iframe src="%s"%s></iframe>',
97  htmlspecialchars($src, ENT_QUOTES | ENT_HTML5),
98  empty($attributes) ? '' : ' ' . $this->‪implodeAttributes($attributes)
99  );
100  }
101 
105  protected function ‪collectOptions(array $options, ‪FileInterface $file)
106  {
107  // Check for an autoplay option at the file reference itself, if not overridden yet.
108  if (!isset($options['autoplay']) && $file instanceof ‪FileReference) {
109  $autoplay = $file->‪getProperty('autoplay');
110  if ($autoplay !== null) {
111  $options['autoplay'] = $autoplay;
112  }
113  }
114 
115  $showPlayerControls = 1;
116  $options['controls'] = (int)!empty($options['controls'] ?? $showPlayerControls);
117 
118  if (!isset($options['allow'])) {
119  $options['allow'] = 'fullscreen';
120  if (!empty($options['autoplay'])) {
121  $options['allow'] = 'autoplay; fullscreen';
122  }
123  }
124  return $options;
125  }
126 
130  protected function ‪createYouTubeUrl(array $options, ‪FileInterface $file)
131  {
132  $videoId = $this->‪getVideoIdFromFile($file);
133 
134  $urlParams = ['autohide=1'];
135  $urlParams[] = 'controls=' . $options['controls'];
136  if (!empty($options['autoplay'])) {
137  $urlParams[] = 'autoplay=1';
138  // If autoplay is enabled, enforce mute=1, see https://developer.chrome.com/blog/autoplay/
139  $urlParams[] = 'mute=1';
140  }
141  if (!empty($options['modestbranding'])) {
142  $urlParams[] = 'modestbranding=1';
143  }
144  if (!empty($options['loop'])) {
145  $urlParams[] = 'loop=1&playlist=' . rawurlencode($videoId);
146  }
147  if (isset($options['relatedVideos'])) {
148  $urlParams[] = 'rel=' . (int)(bool)$options['relatedVideos'];
149  }
150  if (!isset($options['enablejsapi']) || !empty($options['enablejsapi'])) {
151  $urlParams[] = 'enablejsapi=1&origin=' . rawurlencode(GeneralUtility::getIndpEnv('TYPO3_REQUEST_HOST'));
152  }
153 
154  $youTubeUrl = sprintf(
155  'https://www.youtube%s.com/embed/%s?%s',
156  !isset($options['no-cookie']) || !empty($options['no-cookie']) ? '-nocookie' : '',
157  rawurlencode($videoId),
158  implode('&', $urlParams)
159  );
160 
161  return $youTubeUrl;
162  }
163 
167  protected function ‪getVideoIdFromFile(‪FileInterface $file)
168  {
169  if ($file instanceof ‪FileReference) {
170  $orgFile = $file->getOriginalFile();
171  } else {
172  $orgFile = $file;
173  }
174 
175  return $this->‪getOnlineMediaHelper($file)->‪getOnlineMediaId($orgFile);
176  }
177 
183  protected function ‪collectIframeAttributes($width, $height, array $options)
184  {
185  $attributes = [];
186  $attributes['allowfullscreen'] = true;
187 
188  if (isset($options['additionalAttributes']) && is_array($options['additionalAttributes'])) {
189  $attributes = array_merge($attributes, $options['additionalAttributes']);
190  }
191  if (isset($options['data']) && is_array($options['data'])) {
192  array_walk($options['data'], static function (string|int $value, string $key) use (&$attributes): void {
193  $attributes['data-' . $key] = $value;
194  });
195  }
196  if ((int)$width > 0) {
197  $attributes['width'] = (int)$width;
198  }
199  if ((int)$height > 0) {
200  $attributes['height'] = (int)$height;
201  }
203  $attributes['frameborder'] = 0;
204  }
205  foreach (['class', 'dir', 'id', 'lang', 'style', 'title', 'accesskey', 'tabindex', 'onclick', 'poster', 'preload', 'allow'] as $key) {
206  if (!empty($options[$key])) {
207  $attributes[$key] = $options[$key];
208  }
209  }
210 
211  return $attributes;
212  }
213 
217  protected function ‪implodeAttributes(array $attributes): string
218  {
219  $attributeList = [];
220  foreach ($attributes as $name => $value) {
221  $name = preg_replace('/[^\p{L}0-9_.-]/u', '', $name);
222  if ($value === true) {
223  $attributeList[] = $name;
224  } else {
225  $attributeList[] = $name . '="' . htmlspecialchars($value, ENT_QUOTES | ENT_HTML5) . '"';
226  }
227  }
228  return implode(' ', $attributeList);
229  }
230 
234  protected function ‪shouldIncludeFrameBorderAttribute(): bool
235  {
236  return GeneralUtility::makeInstance(PageRenderer::class)->getDocType()->shouldIncludeFrameBorderAttribute();
237  }
238 }
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry
Definition: OnlineMediaHelperRegistry.php:27
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:26
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\$onlineMediaHelper
‪OnlineMediaHelperInterface false $onlineMediaHelper
Definition: YouTubeRenderer.php:33
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\getVideoIdFromFile
‪string getVideoIdFromFile(FileInterface $file)
Definition: YouTubeRenderer.php:166
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:37
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\collectOptions
‪array collectOptions(array $options, FileInterface $file)
Definition: YouTubeRenderer.php:104
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\getPriority
‪int getPriority()
Definition: YouTubeRenderer.php:44
‪TYPO3\CMS\Core\Page\PageRenderer
Definition: PageRenderer.php:44
‪TYPO3\CMS\Core\Resource\Rendering
Definition: AudioTagRenderer.php:16
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\canRender
‪bool canRender(FileInterface $file)
Definition: YouTubeRenderer.php:55
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\implodeAttributes
‪implodeAttributes(array $attributes)
Definition: YouTubeRenderer.php:216
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface
Definition: FileRendererInterface.php:25
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\shouldIncludeFrameBorderAttribute
‪shouldIncludeFrameBorderAttribute()
Definition: YouTubeRenderer.php:233
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer
Definition: YouTubeRenderer.php:30
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\createYouTubeUrl
‪string createYouTubeUrl(array $options, FileInterface $file)
Definition: YouTubeRenderer.php:129
‪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:182
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\render
‪string render(FileInterface $file, $width, $height, array $options=[])
Definition: YouTubeRenderer.php:88
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Resource\FileInterface\getProperty
‪getProperty(string $key)
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\getOnlineMediaHelper
‪false OnlineMediaHelperInterface getOnlineMediaHelper(FileInterface $file)
Definition: YouTubeRenderer.php:65
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperInterface\getOnlineMediaId
‪string getOnlineMediaId(File $file)