[BUGFIX] Use only domain with scheme at youtube origin
[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
23 /**
24 * YouTube renderer class
25 */
26 class YouTubeRenderer 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/youtube' || $file->getExtension() === 'youtube') && $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 = null, $usedPathsRelativeToCurrentScript = false)
91 {
92 // Check for an autoplay option at the file reference itself, if not overriden yet.
93 if (!isset($options['autoplay']) && $file instanceof FileReference) {
94 $autoplay = $file->getProperty('autoplay');
95 if ($autoplay !== null) {
96 $options['autoplay'] = $autoplay;
97 }
98 }
99
100 $urlParams = ['autohide=1'];
101 if (!isset($options['controls']) || !empty($options['controls'])) {
102 $urlParams[] = 'controls=2';
103 }
104 if (!empty($options['autoplay'])) {
105 $urlParams[] = 'autoplay=1';
106 }
107 if (!empty($options['loop'])) {
108 $urlParams[] = 'loop=1';
109 }
110 if (!isset($options['enablejsapi']) || !empty($options['enablejsapi'])) {
111 $urlParams[] = 'enablejsapi=1&amp;origin=' . GeneralUtility::getIndpEnv('TYPO3_REQUEST_HOST');
112 }
113 $urlParams[] = 'showinfo=' . (int)!empty($options['showinfo']);
114
115 if ($file instanceof FileReference) {
116 $orgFile = $file->getOriginalFile();
117 } else {
118 $orgFile = $file;
119 }
120
121 $videoId = $this->getOnlineMediaHelper($file)->getOnlineMediaId($orgFile);
122 $src = sprintf(
123 'https://www.youtube%s.com/embed/%s?%s',
124 !empty($options['no-cookie']) ? '-nocookie' : '',
125 $videoId,
126 implode('&amp;', $urlParams)
127 );
128
129 $attributes = ['allowfullscreen'];
130 if ((int)$width > 0) {
131 $attributes[] = 'width="' . (int)$width . '"';
132 }
133 if ((int)$height > 0) {
134 $attributes[] = 'height="' . (int)$height . '"';
135 }
136 if (is_object($GLOBALS['TSFE']) && $GLOBALS['TSFE']->config['config']['doctype'] !== 'html5') {
137 $attributes[] = 'frameborder="0"';
138 }
139 foreach (['class', 'dir', 'id', 'lang', 'style', 'title', 'accesskey', 'tabindex', 'onclick', 'poster', 'preload'] as $key) {
140 if (!empty($options[$key])) {
141 $attributes[] = $key . '="' . htmlspecialchars($options[$key]) . '"';
142 }
143 }
144
145 return sprintf(
146 '<iframe src="%s"%s></iframe>',
147 $src,
148 empty($attributes) ? '' : ' ' . implode(' ', $attributes)
149 );
150 }
151 }