TYPO3 CMS  TYPO3_8-7
VimeoRenderer.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  */
16 
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/vimeo' || $file->getExtension() === 'vimeo') && $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->createVimeoUrl($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  if (!isset($options['allow'])) {
119  $options['allow'] = 'fullscreen';
120  if (!empty($options['autoplay'])) {
121  $options['allow'] = 'autoplay; fullscreen';
122  }
123  }
124 
125  return $options;
126  }
127 
133  protected function createVimeoUrl(array $options, FileInterface $file)
134  {
135  $videoId = $this->getVideoIdFromFile($file);
136 
137  $urlParams = [];
138  if (!empty($options['autoplay'])) {
139  $urlParams[] = 'autoplay=1';
140  }
141  if (!empty($options['loop'])) {
142  $urlParams[] = 'loop=1';
143  }
144  $urlParams[] = 'title=' . (int)!empty($options['showinfo']);
145  $urlParams[] = 'byline=' . (int)!empty($options['showinfo']);
146  $urlParams[] = 'portrait=0';
147 
148  return sprintf('https://player.vimeo.com/video/%s?%s', $videoId, implode('&', $urlParams));
149  }
150 
155  protected function getVideoIdFromFile(FileInterface $file)
156  {
157  if ($file instanceof FileReference) {
158  $orgFile = $file->getOriginalFile();
159  } else {
160  $orgFile = $file;
161  }
162 
163  return $this->getOnlineMediaHelper($file)->getOnlineMediaId($orgFile);
164  }
165 
172  protected function collectIframeAttributes($width, $height, array $options)
173  {
174  $attributes = [];
175  $attributes['allowfullscreen'] = true;
176 
177  if ((int)$width > 0) {
178  $attributes['width'] = (int)$width;
179  }
180  if ((int)$height > 0) {
181  $attributes['height'] = (int)$height;
182  }
183  if (isset($GLOBALS['TSFE']) &&
184  is_object($GLOBALS['TSFE']) &&
185  $GLOBALS['TSFE']->config['config']['doctype'] !== 'html5') {
186  $attributes['frameborder'] = 0;
187  }
188  foreach (['class', 'dir', 'id', 'lang', 'style', 'title', 'accesskey', 'tabindex', 'onclick', 'allow'] as $key) {
189  if (!empty($options[$key])) {
190  $attributes[$key] = $options[$key];
191  }
192  }
193  return $attributes;
194  }
195 
201  protected function implodeAttributes(array $attributes): string
202  {
203  $attributeList = [];
204  foreach ($attributes as $name => $value) {
205  $name = preg_replace('/[^\p{L}0-9_.-]/u', '', $name);
206  if ($value === true) {
207  $attributeList[] = $name;
208  } else {
209  $attributeList[] = $name . '="' . htmlspecialchars($value, ENT_QUOTES | ENT_HTML5) . '"';
210  }
211  }
212  return implode(' ', $attributeList);
213  }
214 }
collectIframeAttributes($width, $height, array $options)
collectOptions(array $options, FileInterface $file)
createVimeoUrl(array $options, FileInterface $file)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
render(FileInterface $file, $width, $height, array $options=[], $usedPathsRelativeToCurrentScript=false)