‪TYPO3CMS  10.4
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 
23 
28 {
32  protected ‪$onlineMediaHelper;
33 
43  public function ‪getPriority()
44  {
45  return 1;
46  }
47 
54  public function ‪canRender(‪FileInterface $file)
55  {
56  return ($file->‪getMimeType() === 'video/vimeo' || $file->‪getExtension() === 'vimeo') && $this->‪getOnlineMediaHelper($file) !== false;
57  }
58 
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  }
79  }
80 
91  public function ‪render(‪FileInterface $file, $width, $height, array $options = [], $usedPathsRelativeToCurrentScript = false)
92  {
93  $options = $this->‪collectOptions($options, $file);
94  $src = $this->‪createVimeoUrl($options, $file);
95  $attributes = $this->‪collectIframeAttributes($width, $height, $options);
96 
97  return sprintf(
98  '<iframe src="%s"%s></iframe>',
99  htmlspecialchars($src, ENT_QUOTES | ENT_HTML5),
100  empty($attributes) ? '' : ' ' . $this->‪implodeAttributes($attributes)
101  );
102  }
103 
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  if (!isset($options['allow'])) {
120  $options['allow'] = 'fullscreen';
121  if (!empty($options['autoplay'])) {
122  $options['allow'] = 'autoplay; fullscreen';
123  }
124  }
125 
126  return $options;
127  }
128 
134  protected function ‪createVimeoUrl(array $options, ‪FileInterface $file)
135  {
136  $videoId = $this->‪getVideoIdFromFile($file);
137  $urlParams = [];
138  if (!empty($options['autoplay'])) {
139  $urlParams[] = 'autoplay=1';
140  }
141  if (!empty($options['loop'])) {
142  $urlParams[] = 'loop=1';
143  }
144 
145  if (isset($options['api']) && (int)$options['api'] === 1) {
146  $urlParams[] = 'api=1';
147  }
148  if (!empty($options['no-cookie'])) {
149  $urlParams[] = 'dnt=1';
150  }
151  $urlParams[] = 'title=' . (int)!empty($options['showinfo']);
152  $urlParams[] = 'byline=' . (int)!empty($options['showinfo']);
153  $urlParams[] = 'portrait=0';
154 
155  return sprintf('https://player.vimeo.com/video/%s?%s', $videoId, implode('&', $urlParams));
156  }
157 
162  protected function ‪getVideoIdFromFile(‪FileInterface $file)
163  {
164  if ($file instanceof ‪FileReference) {
165  $orgFile = $file->getOriginalFile();
166  } else {
167  $orgFile = $file;
168  }
169 
170  return $this->‪getOnlineMediaHelper($file)->‪getOnlineMediaId($orgFile);
171  }
172 
179  protected function ‪collectIframeAttributes($width, $height, array $options)
180  {
181  $attributes = [];
182  $attributes['allowfullscreen'] = true;
183 
184  if (isset($options['additionalAttributes']) && is_array($options['additionalAttributes'])) {
185  $attributes = array_merge($attributes, $options['additionalAttributes']);
186  }
187  if (isset($options['data']) && is_array($options['data'])) {
188  array_walk(
189  $options['data'],
190  function (&$value, $key) use (&$attributes) {
191  $attributes['data-' . $key] = $value;
192  }
193  );
194  }
195  if ((int)$width > 0) {
196  $attributes['width'] = (int)$width;
197  }
198  if ((int)$height > 0) {
199  $attributes['height'] = (int)$height;
200  }
201  if (isset(‪$GLOBALS['TSFE']) && is_object(‪$GLOBALS['TSFE']) && (isset(‪$GLOBALS['TSFE']->config['config']['doctype']) && ‪$GLOBALS['TSFE']->config['config']['doctype'] !== 'html5')) {
202  $attributes['frameborder'] = 0;
203  }
204  foreach (['class', 'dir', 'id', 'lang', 'style', 'title', 'accesskey', 'tabindex', 'onclick', 'allow'] as $key) {
205  if (!empty($options[$key])) {
206  $attributes[$key] = $options[$key];
207  }
208  }
209  return $attributes;
210  }
211 
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 }
‪TYPO3\CMS\Core\Resource\Rendering\VimeoRenderer
Definition: VimeoRenderer.php:28
‪TYPO3\CMS\Core\Resource\FileInterface\getExtension
‪string getExtension()
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry
Definition: OnlineMediaHelperRegistry.php:27
‪TYPO3\CMS\Core\Resource\Rendering\VimeoRenderer\implodeAttributes
‪string implodeAttributes(array $attributes)
Definition: VimeoRenderer.php:216
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:22
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:33
‪TYPO3\CMS\Core\Resource\Rendering\VimeoRenderer\createVimeoUrl
‪string createVimeoUrl(array $options, FileInterface $file)
Definition: VimeoRenderer.php:133
‪TYPO3\CMS\Core\Resource\FileInterface\getProperty
‪string getProperty($key)
‪TYPO3\CMS\Core\Resource\Rendering\VimeoRenderer\getPriority
‪int getPriority()
Definition: VimeoRenderer.php:42
‪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\VimeoRenderer\getVideoIdFromFile
‪string getVideoIdFromFile(FileInterface $file)
Definition: VimeoRenderer.php:161
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Resource\Rendering\VimeoRenderer\collectOptions
‪array collectOptions(array $options, FileInterface $file)
Definition: VimeoRenderer.php:108
‪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\VimeoRenderer\getOnlineMediaHelper
‪bool OnlineMediaHelperInterface getOnlineMediaHelper(FileInterface $file)
Definition: VimeoRenderer.php:64
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Resource\Rendering\VimeoRenderer\$onlineMediaHelper
‪OnlineMediaHelperInterface $onlineMediaHelper
Definition: VimeoRenderer.php:31
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperInterface
Definition: OnlineMediaHelperInterface.php:25
‪TYPO3\CMS\Core\Resource\Rendering\VimeoRenderer\render
‪string render(FileInterface $file, $width, $height, array $options=[], $usedPathsRelativeToCurrentScript=false)
Definition: VimeoRenderer.php:90
‪TYPO3\CMS\Core\Resource\Rendering\VimeoRenderer\canRender
‪bool canRender(FileInterface $file)
Definition: VimeoRenderer.php:53
‪TYPO3\CMS\Core\Resource\FileInterface\getMimeType
‪string getMimeType()
‪TYPO3\CMS\Core\Resource\Rendering\VimeoRenderer\collectIframeAttributes
‪array collectIframeAttributes($width, $height, array $options)
Definition: VimeoRenderer.php:178
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperInterface\getOnlineMediaId
‪string getOnlineMediaId(File $file)