[TASK] Reduce logic in render methods of YouTube and Vimeo Renderer
[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 use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24 /**
25 * Vimeo renderer class
26 */
27 class VimeoRenderer implements FileRendererInterface
28 {
29 /**
30 * @var OnlineMediaHelperInterface
31 */
32 protected $onlineMediaHelper;
33
34 /**
35 * Returns the priority of the renderer
36 * This way it is possible to define/overrule a renderer
37 * for a specific file type/context.
38 * For example create a video renderer for a certain storage/driver type.
39 * Should be between 1 and 100, 100 is more important than 1
40 *
41 * @return int
42 */
43 public function getPriority()
44 {
45 return 1;
46 }
47
48 /**
49 * Check if given File(Reference) can be rendered
50 *
51 * @param FileInterface $file File of FileReference to render
52 * @return bool
53 */
54 public function canRender(FileInterface $file)
55 {
56 return ($file->getMimeType() === 'video/vimeo' || $file->getExtension() === 'vimeo') && $this->getOnlineMediaHelper($file) !== false;
57 }
58
59 /**
60 * Get online media helper
61 *
62 * @param FileInterface $file
63 * @return bool|OnlineMediaHelperInterface
64 */
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 }
78 return $this->onlineMediaHelper;
79 }
80
81 /**
82 * Render for given File(Reference) html output
83 *
84 * @param FileInterface $file
85 * @param int|string $width TYPO3 known format; examples: 220, 200m or 200c
86 * @param int|string $height TYPO3 known format; examples: 220, 200m or 200c
87 * @param array $options
88 * @param bool $usedPathsRelativeToCurrentScript See $file->getPublicUrl()
89 * @return string
90 */
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 $src,
100 empty($attributes) ? '' : ' ' . implode(' ', $attributes)
101 );
102 }
103
104 /**
105 * @param array $options
106 * @param FileInterface $file
107 * @return array
108 */
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
129 /**
130 * @param array $options
131 * @param FileInterface $file
132 * @return string
133 */
134 protected function createVimeoUrl(array $options, FileInterface $file)
135 {
136 $videoId = $this->getVideoIdFromFile($file);
137
138 $urlParams = [];
139 if (!empty($options['autoplay'])) {
140 $urlParams[] = 'autoplay=1';
141 }
142 if (!empty($options['loop'])) {
143 $urlParams[] = 'loop=1';
144 }
145 $urlParams[] = 'title=' . (int)!empty($options['showinfo']);
146 $urlParams[] = 'byline=' . (int)!empty($options['showinfo']);
147 $urlParams[] = 'portrait=0';
148
149 return sprintf('https://player.vimeo.com/video/%s?%s', $videoId, implode('&amp;', $urlParams));
150 }
151
152 /**
153 * @param FileInterface $file
154 * @return string
155 */
156 protected function getVideoIdFromFile(FileInterface $file)
157 {
158 if ($file instanceof FileReference) {
159 $orgFile = $file->getOriginalFile();
160 } else {
161 $orgFile = $file;
162 }
163
164 return $this->getOnlineMediaHelper($file)->getOnlineMediaId($orgFile);
165 }
166
167 /**
168 * @param int|string $width
169 * @param int|string $height
170 * @param array $options
171 * @return array
172 */
173 protected function collectIframeAttributes($width, $height, array $options)
174 {
175 $attributes = ['allowfullscreen'];
176 if (isset($options['additionalAttributes']) && is_array($options['additionalAttributes'])) {
177 $attributes[] = GeneralUtility::implodeAttributes($options['additionalAttributes'], true, true);
178 }
179 if (isset($options['data']) && is_array($options['data'])) {
180 array_walk($options['data'], function (&$value, $key) {
181 $value = 'data-' . htmlspecialchars($key) . '="' . htmlspecialchars($value) . '"';
182 });
183 $attributes[] = implode(' ', $options['data']);
184 }
185 if ((int)$width > 0) {
186 $attributes[] = 'width="' . (int)$width . '"';
187 }
188 if ((int)$height > 0) {
189 $attributes[] = 'height="' . (int)$height . '"';
190 }
191 if (isset($GLOBALS['TSFE']) && is_object($GLOBALS['TSFE']) && $GLOBALS['TSFE']->config['config']['doctype'] !== 'html5') {
192 $attributes[] = 'frameborder="0"';
193 }
194 foreach (['class', 'dir', 'id', 'lang', 'style', 'title', 'accesskey', 'tabindex', 'onclick', 'allow'] as $key) {
195 if (!empty($options[$key])) {
196 $attributes[] = $key . '="' . htmlspecialchars($options[$key]) . '"';
197 }
198 }
199 return $attributes;
200 }
201 }