[FEATURE] Render additionalAttributes and data-* attr in media renderer
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Rendering / YouTubeRenderer.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 use TYPO3\CMS\Core\Resource\File;
17 use TYPO3\CMS\Core\Resource\FileInterface;
18 use TYPO3\CMS\Core\Resource\FileReference;
19 use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperInterface;
20 use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Core\Utility\MathUtility;
23
24 /**
25 * YouTube renderer class
26 */
27 class YouTubeRenderer 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/youtube' || $file->getExtension() === 'youtube') && $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 = null, $usedPathsRelativeToCurrentScript = false)
92 {
93 // Check for an autoplay option at the file reference itself, if not overridden yet.
94 if (!isset($options['autoplay']) && $file instanceof FileReference) {
95 $autoplay = $file->getProperty('autoplay');
96 if ($autoplay !== null) {
97 $options['autoplay'] = $autoplay;
98 }
99 }
100
101 if ($file instanceof FileReference) {
102 $orgFile = $file->getOriginalFile();
103 } else {
104 $orgFile = $file;
105 }
106
107 $videoId = $this->getOnlineMediaHelper($file)->getOnlineMediaId($orgFile);
108
109 $urlParams = ['autohide=1'];
110
111 $options['controls'] = MathUtility::canBeInterpretedAsInteger($options['controls']) ? (int)$options['controls'] : 2;
112 $options['controls'] = MathUtility::forceIntegerInRange($options['controls'], 0, 2);
113 $urlParams[] = 'controls=' . $options['controls'];
114 if (!empty($options['autoplay'])) {
115 $urlParams[] = 'autoplay=1';
116 }
117 if (!empty($options['loop'])) {
118 $urlParams[] = 'loop=1&amp;playlist=' . $videoId;
119 }
120 if (isset($options['relatedVideos'])) {
121 $urlParams[] = 'rel=' . (int)(bool)$options['relatedVideos'];
122 }
123 if (!isset($options['enablejsapi']) || !empty($options['enablejsapi'])) {
124 $urlParams[] = 'enablejsapi=1&amp;origin=' . rawurlencode(GeneralUtility::getIndpEnv('TYPO3_REQUEST_HOST'));
125 }
126 $urlParams[] = 'showinfo=' . (int)!empty($options['showinfo']);
127
128 $src = sprintf(
129 'https://www.youtube%s.com/embed/%s?%s',
130 !empty($options['no-cookie']) ? '-nocookie' : '',
131 $videoId,
132 implode('&amp;', $urlParams)
133 );
134
135 $attributes = ['allowfullscreen'];
136 if (is_array($options['additionalAttributes'])) {
137 $attributes[] = GeneralUtility::implodeAttributes($options['additionalAttributes'], true, true);
138 }
139 if (is_array($options['data'])) {
140 array_walk($options['data'], function (&$value, $key) {
141 $value = 'data-' . htmlspecialchars($key) . '="' . htmlspecialchars($value) . '"';
142 });
143 $attributes[] = implode(' ', $options['data']);
144 }
145 if ((int)$width > 0) {
146 $attributes[] = 'width="' . (int)$width . '"';
147 }
148 if ((int)$height > 0) {
149 $attributes[] = 'height="' . (int)$height . '"';
150 }
151 if (is_object($GLOBALS['TSFE']) && $GLOBALS['TSFE']->config['config']['doctype'] !== 'html5') {
152 $attributes[] = 'frameborder="0"';
153 }
154 foreach (['class', 'dir', 'id', 'lang', 'style', 'title', 'accesskey', 'tabindex', 'onclick', 'poster', 'preload'] as $key) {
155 if (!empty($options[$key])) {
156 $attributes[] = $key . '="' . htmlspecialchars($options[$key]) . '"';
157 }
158 }
159
160 return sprintf(
161 '<iframe src="%s"%s></iframe>',
162 $src,
163 empty($attributes) ? '' : ' ' . implode(' ', $attributes)
164 );
165 }
166 }