‪TYPO3CMS  9.5
YouTubeRenderer.php
Go to the documentation of this file.
1 <?php
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  */
22 
27 {
31  protected ‪$onlineMediaHelper;
32 
42  public function ‪getPriority()
43  {
44  return 1;
45  }
46 
53  public function ‪canRender(‪FileInterface $file)
54  {
55  return ($file->‪getMimeType() === 'video/youtube' || $file->‪getExtension() === 'youtube') && $this->‪getOnlineMediaHelper($file) !== false;
56  }
57 
64  protected function ‪getOnlineMediaHelper(‪FileInterface $file)
65  {
66  if ($this->onlineMediaHelper === null) {
67  $orgFile = $file;
68  if ($orgFile instanceof ‪FileReference) {
69  $orgFile = $orgFile->getOriginalFile();
70  }
71  if ($orgFile instanceof ‪File) {
72  $this->onlineMediaHelper = ‪OnlineMediaHelperRegistry::getInstance()->‪getOnlineMediaHelper($orgFile);
73  } else {
74  $this->onlineMediaHelper = false;
75  }
76  }
78  }
79 
90  public function ‪render(‪FileInterface $file, $width, $height, array $options = [], $usedPathsRelativeToCurrentScript = false)
91  {
92  $options = $this->‪collectOptions($options, $file);
93  $src = $this->‪createYouTubeUrl($options, $file);
94  $attributes = $this->‪collectIframeAttributes($width, $height, $options);
95 
96  return sprintf(
97  '<iframe src="%s"%s></iframe>',
98  htmlspecialchars($src, ENT_QUOTES | ENT_HTML5),
99  empty($attributes) ? '' : ' ' . $this->‪implodeAttributes($attributes)
100  );
101  }
102 
108  protected function ‪collectOptions(array $options, ‪FileInterface $file)
109  {
110  // Check for an autoplay option at the file reference itself, if not overridden yet.
111  if (!isset($options['autoplay']) && $file instanceof ‪FileReference) {
112  $autoplay = $file->‪getProperty('autoplay');
113  if ($autoplay !== null) {
114  $options['autoplay'] = $autoplay;
115  }
116  }
117 
118  $showPlayerControls = 1;
119  $options['controls'] = (int)!empty($options['controls'] ?? $showPlayerControls);
120 
121  if (!isset($options['allow'])) {
122  $options['allow'] = 'fullscreen';
123  if (!empty($options['autoplay'])) {
124  $options['allow'] = 'autoplay; fullscreen';
125  }
126  }
127  return $options;
128  }
129 
135  protected function ‪createYouTubeUrl(array $options, ‪FileInterface $file)
136  {
137  $videoId = $this->‪getVideoIdFromFile($file);
138 
139  $urlParams = ['autohide=1'];
140  $urlParams[] = 'controls=' . $options['controls'];
141  if (!empty($options['autoplay'])) {
142  $urlParams[] = 'autoplay=1';
143  }
144  if (!empty($options['modestbranding'])) {
145  $urlParams[] = 'modestbranding=1';
146  }
147  if (!empty($options['loop'])) {
148  $urlParams[] = 'loop=1&playlist=' . rawurlencode($videoId);
149  }
150  if (isset($options['relatedVideos'])) {
151  $urlParams[] = 'rel=' . (int)(bool)$options['relatedVideos'];
152  }
153  if (!isset($options['enablejsapi']) || !empty($options['enablejsapi'])) {
154  $urlParams[] = 'enablejsapi=1&origin=' . rawurlencode(GeneralUtility::getIndpEnv('TYPO3_REQUEST_HOST'));
155  }
156 
157  $youTubeUrl = sprintf(
158  'https://www.youtube%s.com/embed/%s?%s',
159  !isset($options['no-cookie']) || !empty($options['no-cookie']) ? '-nocookie' : '',
160  rawurlencode($videoId),
161  implode('&', $urlParams)
162  );
163 
164  return $youTubeUrl;
165  }
166 
171  protected function ‪getVideoIdFromFile(‪FileInterface $file)
172  {
173  if ($file instanceof ‪FileReference) {
174  $orgFile = $file->getOriginalFile();
175  } else {
176  $orgFile = $file;
177  }
178 
179  return $this->‪getOnlineMediaHelper($file)->‪getOnlineMediaId($orgFile);
180  }
181 
188  protected function ‪collectIframeAttributes($width, $height, array $options)
189  {
190  $attributes = [];
191  $attributes['allowfullscreen'] = true;
192 
193  if (isset($options['additionalAttributes']) && is_array($options['additionalAttributes'])) {
194  $attributes = array_merge($attributes, $options['additionalAttributes']);
195  }
196  if (isset($options['data']) && is_array($options['data'])) {
197  array_walk($options['data'], function (&$value, $key) use (&$attributes) {
198  $attributes['data-' . $key] = $value;
199  });
200  }
201  if ((int)$width > 0) {
202  $attributes['width'] = (int)$width;
203  }
204  if ((int)$height > 0) {
205  $attributes['height'] = (int)$height;
206  }
207  if (isset(‪$GLOBALS['TSFE']) && is_object(‪$GLOBALS['TSFE']) && (isset(‪$GLOBALS['TSFE']->config['config']['doctype']) && ‪$GLOBALS['TSFE']->config['config']['doctype'] !== 'html5')) {
208  $attributes['frameborder'] = 0;
209  }
210  foreach (['class', 'dir', 'id', 'lang', 'style', 'title', 'accesskey', 'tabindex', 'onclick', 'poster', 'preload', 'allow'] as $key) {
211  if (!empty($options[$key])) {
212  $attributes[$key] = $options[$key];
213  }
214  }
215 
216  return $attributes;
217  }
218 
224  protected function ‪implodeAttributes(array $attributes): string
225  {
226  $attributeList = [];
227  foreach ($attributes as $name => $value) {
228  $name = preg_replace('/[^\p{L}0-9_.-]/u', '', $name);
229  if ($value === true) {
230  $attributeList[] = $name;
231  } else {
232  $attributeList[] = $name . '="' . htmlspecialchars($value, ENT_QUOTES | ENT_HTML5) . '"';
233  }
234  }
235  return implode(' ', $attributeList);
236  }
237 }
‪TYPO3\CMS\Core\Resource\FileInterface\getExtension
‪string getExtension()
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry
Definition: OnlineMediaHelperRegistry.php:25
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\getOnlineMediaHelper
‪bool OnlineMediaHelperInterface getOnlineMediaHelper(FileInterface $file)
Definition: YouTubeRenderer.php:63
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:21
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\getVideoIdFromFile
‪string getVideoIdFromFile(FileInterface $file)
Definition: YouTubeRenderer.php:170
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:31
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\collectOptions
‪array collectOptions(array $options, FileInterface $file)
Definition: YouTubeRenderer.php:107
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\$onlineMediaHelper
‪OnlineMediaHelperInterface $onlineMediaHelper
Definition: YouTubeRenderer.php:30
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\implodeAttributes
‪string implodeAttributes(array $attributes)
Definition: YouTubeRenderer.php:223
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\getPriority
‪int getPriority()
Definition: YouTubeRenderer.php:41
‪TYPO3\CMS\Core\Resource\FileInterface\getProperty
‪string getProperty($key)
‪TYPO3\CMS\Core\Resource\Rendering
Definition: AudioTagRenderer.php:2
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry\getOnlineMediaHelper
‪bool OnlineMediaHelperInterface getOnlineMediaHelper(File $file)
Definition: OnlineMediaHelperRegistry.php:42
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\canRender
‪bool canRender(FileInterface $file)
Definition: YouTubeRenderer.php:52
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:23
‪TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface
Definition: FileRendererInterface.php:23
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry\getInstance
‪static OnlineMediaHelperRegistry getInstance()
Definition: OnlineMediaHelperRegistry.php:31
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer
Definition: YouTubeRenderer.php:27
‪$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:134
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperInterface
Definition: OnlineMediaHelperInterface.php:24
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\collectIframeAttributes
‪array collectIframeAttributes($width, $height, array $options)
Definition: YouTubeRenderer.php:187
‪TYPO3\CMS\Core\Resource\FileInterface\getMimeType
‪string getMimeType()
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Resource\Rendering\YouTubeRenderer\render
‪string render(FileInterface $file, $width, $height, array $options=[], $usedPathsRelativeToCurrentScript=false)
Definition: YouTubeRenderer.php:89
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperInterface\getOnlineMediaId
‪string getOnlineMediaId(File $file)