‪TYPO3CMS  ‪main
VimeoRenderer.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/vimeo' || $file->getExtension() === 'vimeo') && $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->‪createVimeoUrl($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  if (!isset($options['allow'])) {
116  $options['allow'] = 'fullscreen';
117  if (!empty($options['autoplay'])) {
118  $options['allow'] = 'autoplay; fullscreen';
119  }
120  }
121 
122  return $options;
123  }
124 
128  protected function ‪createVimeoUrl(array $options, ‪FileInterface $file)
129  {
130  $videoIdRaw = $this->‪getVideoIdFromFile($file);
131  $videoIdRaw = ‪GeneralUtility::trimExplode('/', $videoIdRaw, true);
132 
133  $videoId = $videoIdRaw[0];
134  $hash = $videoIdRaw[1] ?? null;
135 
136  $urlParams = [];
137  if (!empty($hash)) {
138  $urlParams[] = 'h=' . $hash;
139  }
140  if (!empty($options['autoplay'])) {
141  $urlParams[] = 'autoplay=1';
142  // If autoplay is enabled, enforce muted=1, see https://developer.chrome.com/blog/autoplay/
143  $urlParams[] = 'muted=1';
144  }
145  if (!empty($options['loop'])) {
146  $urlParams[] = 'loop=1';
147  }
148 
149  if (isset($options['api']) && (int)$options['api'] === 1) {
150  $urlParams[] = 'api=1';
151  }
152  if (!isset($options['no-cookie']) || !empty($options['no-cookie'])) {
153  $urlParams[] = 'dnt=1';
154  }
155  $urlParams[] = 'title=' . (int)!empty($options['showinfo']);
156  $urlParams[] = 'byline=' . (int)!empty($options['showinfo']);
157  $urlParams[] = 'portrait=0';
158 
159  return sprintf('https://player.vimeo.com/video/%s?%s', $videoId, implode('&', $urlParams));
160  }
161 
165  protected function ‪getVideoIdFromFile(‪FileInterface $file)
166  {
167  if ($file instanceof ‪FileReference) {
168  $orgFile = $file->getOriginalFile();
169  } else {
170  $orgFile = $file;
171  }
172 
173  return $this->‪getOnlineMediaHelper($file)->‪getOnlineMediaId($orgFile);
174  }
175 
181  protected function ‪collectIframeAttributes($width, $height, array $options)
182  {
183  $attributes = [];
184  $attributes['allowfullscreen'] = true;
185 
186  if (isset($options['additionalAttributes']) && is_array($options['additionalAttributes'])) {
187  $attributes = array_merge($attributes, $options['additionalAttributes']);
188  }
189  if (isset($options['data']) && is_array($options['data'])) {
190  array_walk(
191  $options['data'],
192  static function (string $value, string|int $key) use (&$attributes): void {
193  $attributes['data-' . $key] = $value;
194  }
195  );
196  }
197  if ((int)$width > 0) {
198  $attributes['width'] = (int)$width;
199  }
200  if ((int)$height > 0) {
201  $attributes['height'] = (int)$height;
202  }
204  $attributes['frameborder'] = 0;
205  }
206  foreach (['class', 'dir', 'id', 'lang', 'style', 'title', 'accesskey', 'tabindex', 'onclick', 'allow'] as $key) {
207  if (!empty($options[$key])) {
208  $attributes[$key] = $options[$key];
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\Rendering\VimeoRenderer
Definition: VimeoRenderer.php:30
‪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\VimeoRenderer\shouldIncludeFrameBorderAttribute
‪shouldIncludeFrameBorderAttribute()
Definition: VimeoRenderer.php:233
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:37
‪TYPO3\CMS\Core\Resource\Rendering\VimeoRenderer\implodeAttributes
‪implodeAttributes(array $attributes)
Definition: VimeoRenderer.php:216
‪TYPO3\CMS\Core\Resource\Rendering\VimeoRenderer\createVimeoUrl
‪string createVimeoUrl(array $options, FileInterface $file)
Definition: VimeoRenderer.php:127
‪TYPO3\CMS\Core\Page\PageRenderer
Definition: PageRenderer.php:44
‪TYPO3\CMS\Core\Resource\Rendering\VimeoRenderer\getPriority
‪int getPriority()
Definition: VimeoRenderer.php:44
‪TYPO3\CMS\Core\Resource\Rendering
Definition: AudioTagRenderer.php:16
‪TYPO3\CMS\Core\Resource\Rendering\VimeoRenderer\render
‪string render(FileInterface $file, $width, $height, array $options=[])
Definition: VimeoRenderer.php:88
‪TYPO3\CMS\Core\Resource\Rendering\VimeoRenderer\getVideoIdFromFile
‪string getVideoIdFromFile(FileInterface $file)
Definition: VimeoRenderer.php:164
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Core\Resource\Rendering\VimeoRenderer\collectOptions
‪array collectOptions(array $options, FileInterface $file)
Definition: VimeoRenderer.php:104
‪TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface
Definition: FileRendererInterface.php:25
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperInterface
Definition: OnlineMediaHelperInterface.php:25
‪TYPO3\CMS\Core\Resource\Rendering\VimeoRenderer\canRender
‪bool canRender(FileInterface $file)
Definition: VimeoRenderer.php:55
‪TYPO3\CMS\Core\Resource\Rendering\VimeoRenderer\$onlineMediaHelper
‪OnlineMediaHelperInterface false $onlineMediaHelper
Definition: VimeoRenderer.php:33
‪TYPO3\CMS\Core\Resource\Rendering\VimeoRenderer\collectIframeAttributes
‪array collectIframeAttributes($width, $height, array $options)
Definition: VimeoRenderer.php:180
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Resource\FileInterface\getProperty
‪getProperty(string $key)
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperInterface\getOnlineMediaId
‪string getOnlineMediaId(File $file)
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:822
‪TYPO3\CMS\Core\Resource\Rendering\VimeoRenderer\getOnlineMediaHelper
‪false OnlineMediaHelperInterface getOnlineMediaHelper(FileInterface $file)
Definition: VimeoRenderer.php:65