[BUGFIX] Set proper default value for unavailable redirect report
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Rendering / VimeoRenderer.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Rendering;
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
17 use TYPO3\CMS\Core\Resource\File;
18 use TYPO3\CMS\Core\Resource\FileInterface;
19 use TYPO3\CMS\Core\Resource\FileReference;
20 use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperInterface;
21 use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry;
22
23 /**
24 * Vimeo renderer class
25 */
26 class VimeoRenderer implements FileRendererInterface
27 {
28 /**
29 * @var OnlineMediaHelperInterface
30 */
31 protected $onlineMediaHelper;
32
33 /**
34 * Returns the priority of the renderer
35 * This way it is possible to define/overrule a renderer
36 * for a specific file type/context.
37 * For example create a video renderer for a certain storage/driver type.
38 * Should be between 1 and 100, 100 is more important than 1
39 *
40 * @return int
41 */
42 public function getPriority()
43 {
44 return 1;
45 }
46
47 /**
48 * Check if given File(Reference) can be rendered
49 *
50 * @param FileInterface $file File of FileReference to render
51 * @return bool
52 */
53 public function canRender(FileInterface $file)
54 {
55 return ($file->getMimeType() === 'video/vimeo' || $file->getExtension() === 'vimeo') && $this->getOnlineMediaHelper($file) !== false;
56 }
57
58 /**
59 * Get online media helper
60 *
61 * @param FileInterface $file
62 * @return bool|OnlineMediaHelperInterface
63 */
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 }
77 return $this->onlineMediaHelper;
78 }
79
80 /**
81 * Render for given File(Reference) html output
82 *
83 * @param FileInterface $file
84 * @param int|string $width TYPO3 known format; examples: 220, 200m or 200c
85 * @param int|string $height TYPO3 known format; examples: 220, 200m or 200c
86 * @param array $options
87 * @param bool $usedPathsRelativeToCurrentScript See $file->getPublicUrl()
88 * @return string
89 */
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
103 /**
104 * @param array $options
105 * @param FileInterface $file
106 * @return array
107 */
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
128 /**
129 * @param array $options
130 * @param FileInterface $file
131 * @return string
132 */
133 protected function createVimeoUrl(array $options, FileInterface $file)
134 {
135 $videoId = $this->getVideoIdFromFile($file);
136 $urlParams = [];
137 if (!empty($options['autoplay'])) {
138 $urlParams[] = 'autoplay=1';
139 }
140 if (!empty($options['loop'])) {
141 $urlParams[] = 'loop=1';
142 }
143
144 if (isset($options['api']) && (int)$options['api'] === 1) {
145 $urlParams[] = 'api=1';
146 }
147 $urlParams[] = 'title=' . (int)!empty($options['showinfo']);
148 $urlParams[] = 'byline=' . (int)!empty($options['showinfo']);
149 $urlParams[] = 'portrait=0';
150
151 return sprintf('https://player.vimeo.com/video/%s?%s', $videoId, implode('&', $urlParams));
152 }
153
154 /**
155 * @param FileInterface $file
156 * @return string
157 */
158 protected function getVideoIdFromFile(FileInterface $file)
159 {
160 if ($file instanceof FileReference) {
161 $orgFile = $file->getOriginalFile();
162 } else {
163 $orgFile = $file;
164 }
165
166 return $this->getOnlineMediaHelper($file)->getOnlineMediaId($orgFile);
167 }
168
169 /**
170 * @param int|string $width
171 * @param int|string $height
172 * @param array $options
173 * @return array pairs of key/value; not yet html-escaped
174 */
175 protected function collectIframeAttributes($width, $height, array $options)
176 {
177 $attributes = [];
178 $attributes['allowfullscreen'] = true;
179
180 if (isset($options['additionalAttributes']) && is_array($options['additionalAttributes'])) {
181 $attributes = array_merge($attributes, $options['additionalAttributes']);
182 }
183 if (isset($options['data']) && is_array($options['data'])) {
184 array_walk(
185 $options['data'],
186 function (&$value, $key) use (&$attributes) {
187 $attributes['data-' . $key] = $value;
188 }
189 );
190 }
191 if ((int)$width > 0) {
192 $attributes['width'] = (int)$width;
193 }
194 if ((int)$height > 0) {
195 $attributes['height'] = (int)$height;
196 }
197 if (isset($GLOBALS['TSFE']) && is_object($GLOBALS['TSFE']) && (isset($GLOBALS['TSFE']->config['config']['doctype']) && $GLOBALS['TSFE']->config['config']['doctype'] !== 'html5')) {
198 $attributes['frameborder'] = 0;
199 }
200 foreach (['class', 'dir', 'id', 'lang', 'style', 'title', 'accesskey', 'tabindex', 'onclick', 'allow'] as $key) {
201 if (!empty($options[$key])) {
202 $attributes[$key] = $options[$key];
203 }
204 }
205 return $attributes;
206 }
207
208 /**
209 * @internal
210 * @param array $attributes
211 * @return string
212 */
213 protected function implodeAttributes(array $attributes): string
214 {
215 $attributeList = [];
216 foreach ($attributes as $name => $value) {
217 $name = preg_replace('/[^\p{L}0-9_.-]/u', '', $name);
218 if ($value === true) {
219 $attributeList[] = $name;
220 } else {
221 $attributeList[] = $name . '="' . htmlspecialchars($value, ENT_QUOTES | ENT_HTML5) . '"';
222 }
223 }
224 return implode(' ', $attributeList);
225 }
226 }