YouTubeRenderer.php 7.92 KB
Newer Older
1
<?php
2

3
/*
4
5
6
7
8
9
10
11
12
13
14
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */
15
16
17

namespace TYPO3\CMS\Core\Resource\Rendering;

18
use TYPO3\CMS\Core\Resource\File;
19
20
use TYPO3\CMS\Core\Resource\FileInterface;
use TYPO3\CMS\Core\Resource\FileReference;
21
22
23
24
25
26
27
use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperInterface;
use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
 * YouTube renderer class
 */
28
29
30
class YouTubeRenderer implements FileRendererInterface
{
    /**
31
     * @var OnlineMediaHelperInterface|false
32
33
     */
    protected $onlineMediaHelper;
34

35
36
37
38
39
40
41
42
43
44
45
46
47
    /**
     * Returns the priority of the renderer
     * This way it is possible to define/overrule a renderer
     * for a specific file type/context.
     * For example create a video renderer for a certain storage/driver type.
     * Should be between 1 and 100, 100 is more important than 1
     *
     * @return int
     */
    public function getPriority()
    {
        return 1;
    }
48

49
50
51
52
53
54
55
56
57
58
    /**
     * Check if given File(Reference) can be rendered
     *
     * @param FileInterface $file File of FileReference to render
     * @return bool
     */
    public function canRender(FileInterface $file)
    {
        return ($file->getMimeType() === 'video/youtube' || $file->getExtension() === 'youtube') && $this->getOnlineMediaHelper($file) !== false;
    }
59

60
61
62
63
    /**
     * Get online media helper
     *
     * @param FileInterface $file
64
     * @return false|OnlineMediaHelperInterface
65
66
67
68
69
70
71
72
73
     */
    protected function getOnlineMediaHelper(FileInterface $file)
    {
        if ($this->onlineMediaHelper === null) {
            $orgFile = $file;
            if ($orgFile instanceof FileReference) {
                $orgFile = $orgFile->getOriginalFile();
            }
            if ($orgFile instanceof File) {
74
                $this->onlineMediaHelper = GeneralUtility::makeInstance(OnlineMediaHelperRegistry::class)->getOnlineMediaHelper($orgFile);
75
76
77
78
79
80
            } else {
                $this->onlineMediaHelper = false;
            }
        }
        return $this->onlineMediaHelper;
    }
81

82
83
84
85
86
87
88
89
90
    /**
     * Render for given File(Reference) html output
     *
     * @param FileInterface $file
     * @param int|string $width TYPO3 known format; examples: 220, 200m or 200c
     * @param int|string $height TYPO3 known format; examples: 220, 200m or 200c
     * @param array $options
     * @return string
     */
91
    public function render(FileInterface $file, $width, $height, array $options = [])
92
93
94
95
96
97
98
    {
        $options = $this->collectOptions($options, $file);
        $src = $this->createYouTubeUrl($options, $file);
        $attributes = $this->collectIframeAttributes($width, $height, $options);

        return sprintf(
            '<iframe src="%s"%s></iframe>',
99
100
            htmlspecialchars($src, ENT_QUOTES | ENT_HTML5),
            empty($attributes) ? '' : ' ' . $this->implodeAttributes($attributes)
101
102
103
104
105
106
107
108
109
        );
    }

    /**
     * @param array $options
     * @param FileInterface $file
     * @return array
     */
    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
114
115
116
117
            $autoplay = $file->getProperty('autoplay');
            if ($autoplay !== null) {
                $options['autoplay'] = $autoplay;
            }
        }
118

119
120
        $showPlayerControls = 1;
        $options['controls'] = (int)!empty($options['controls'] ?? $showPlayerControls);
121
122
123
124
125
126

        if (!isset($options['allow'])) {
            $options['allow'] = 'fullscreen';
            if (!empty($options['autoplay'])) {
                $options['allow'] = 'autoplay; fullscreen';
            }
127
        }
128
129
        return $options;
    }
130

131
132
133
134
135
136
137
138
    /**
     * @param array $options
     * @param FileInterface $file
     * @return string
     */
    protected function createYouTubeUrl(array $options, FileInterface $file)
    {
        $videoId = $this->getVideoIdFromFile($file);
139

140
        $urlParams = ['autohide=1'];
141
        $urlParams[] = 'controls=' . $options['controls'];
142
143
        if (!empty($options['autoplay'])) {
            $urlParams[] = 'autoplay=1';
144
145
            // If autoplay is enabled, enforce mute=1, see https://developer.chrome.com/blog/autoplay/
            $urlParams[] = 'mute=1';
146
        }
147
148
149
        if (!empty($options['modestbranding'])) {
            $urlParams[] = 'modestbranding=1';
        }
150
        if (!empty($options['loop'])) {
151
            $urlParams[] = 'loop=1&playlist=' . rawurlencode($videoId);
152
        }
153
154
155
        if (isset($options['relatedVideos'])) {
            $urlParams[] = 'rel=' . (int)(bool)$options['relatedVideos'];
        }
156
        if (!isset($options['enablejsapi']) || !empty($options['enablejsapi'])) {
157
            $urlParams[] = 'enablejsapi=1&origin=' . rawurlencode(GeneralUtility::getIndpEnv('TYPO3_REQUEST_HOST'));
158
        }
159

160
        $youTubeUrl = sprintf(
161
            'https://www.youtube%s.com/embed/%s?%s',
162
            !isset($options['no-cookie']) || !empty($options['no-cookie']) ? '-nocookie' : '',
163
164
            rawurlencode($videoId),
            implode('&', $urlParams)
165
        );
166

167
168
169
170
171
172
173
174
175
176
177
178
179
        return $youTubeUrl;
    }

    /**
     * @param FileInterface $file
     * @return string
     */
    protected function getVideoIdFromFile(FileInterface $file)
    {
        if ($file instanceof FileReference) {
            $orgFile = $file->getOriginalFile();
        } else {
            $orgFile = $file;
180
        }
181
182
183
184
185
186
187
188

        return $this->getOnlineMediaHelper($file)->getOnlineMediaId($orgFile);
    }

    /**
     * @param int|string $width
     * @param int|string $height
     * @param array $options
189
     * @return array pairs of key/value; not yet html-escaped
190
191
192
     */
    protected function collectIframeAttributes($width, $height, array $options)
    {
193
194
        $attributes = [];
        $attributes['allowfullscreen'] = true;
195

196
        if (isset($options['additionalAttributes']) && is_array($options['additionalAttributes'])) {
197
            $attributes = array_merge($attributes, $options['additionalAttributes']);
198
        }
199
        if (isset($options['data']) && is_array($options['data'])) {
200
            array_walk($options['data'], static function (&$value, $key) use (&$attributes) {
201
                $attributes['data-' . $key] = $value;
202
203
            });
        }
204
        if ((int)$width > 0) {
205
            $attributes['width'] = (int)$width;
206
207
        }
        if ((int)$height > 0) {
208
            $attributes['height'] = (int)$height;
209
        }
210
        if (isset($GLOBALS['TSFE']) && is_object($GLOBALS['TSFE']) && (isset($GLOBALS['TSFE']->config['config']['doctype']) && $GLOBALS['TSFE']->config['config']['doctype'] !== 'html5')) {
211
            $attributes['frameborder'] = 0;
212
        }
213
        foreach (['class', 'dir', 'id', 'lang', 'style', 'title', 'accesskey', 'tabindex', 'onclick', 'poster', 'preload', 'allow'] as $key) {
214
            if (!empty($options[$key])) {
215
                $attributes[$key] = $options[$key];
216
217
            }
        }
218

219
        return $attributes;
220
    }
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239

    /**
     * @internal
     * @param array $attributes
     * @return string
     */
    protected function implodeAttributes(array $attributes): string
    {
        $attributeList = [];
        foreach ($attributes as $name => $value) {
            $name = preg_replace('/[^\p{L}0-9_.-]/u', '', $name);
            if ($value === true) {
                $attributeList[] = $name;
            } else {
                $attributeList[] = $name . '="' . htmlspecialchars($value, ENT_QUOTES | ENT_HTML5) . '"';
            }
        }
        return implode(' ', $attributeList);
    }
240
}