TYPO3 CMS  TYPO3_7-6
YouTubeRenderer.php
Go to the documentation of this file.
1 <?php
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  */
23 
28 {
32  protected $onlineMediaHelper;
33 
43  public function getPriority()
44  {
45  return 1;
46  }
47 
54  public function canRender(FileInterface $file)
55  {
56  return ($file->getMimeType() === 'video/youtube' || $file->getExtension() === 'youtube') && $this->getOnlineMediaHelper($file) !== false;
57  }
58 
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  }
79  }
80 
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 overriden 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&playlist=' . rawurlencode($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&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  !isset($options['no-cookie']) || !empty($options['no-cookie']) ? '-nocookie' : '',
131  rawurlencode($videoId),
132  implode('&', $urlParams)
133  );
134 
135  $attributes = [];
136  $attributes['allowfullscreen'] = true;
137  if ((int)$width > 0) {
138  $attributes['width'] = (int)$width;
139  }
140  if ((int)$height > 0) {
141  $attributes['height'] = (int)$height;
142  }
143  if (isset($GLOBALS['TSFE']) && is_object($GLOBALS['TSFE']) && $GLOBALS['TSFE']->config['config']['doctype'] !== 'html5') {
144  $attributes['frameborder'] = 0;
145  }
146  foreach (['class', 'dir', 'id', 'lang', 'style', 'title', 'accesskey', 'tabindex', 'onclick', 'poster', 'preload'] as $key) {
147  if (!empty($options[$key])) {
148  $attributes[$key] = $options[$key];
149  }
150  }
151 
152  return sprintf(
153  '<iframe src="%s"%s></iframe>',
154  htmlspecialchars($src, ENT_QUOTES | ENT_HTML5),
155  empty($attributes) ? '' : ' ' . $this->implodeAttributes($attributes)
156  );
157  }
158 
164  protected function implodeAttributes(array $attributes)
165  {
166  $attributeList = [];
167  foreach ($attributes as $name => $value) {
168  $name = preg_replace('/[^\p{L}0-9_.-]/u', '', $name);
169  if ($value === true) {
170  $attributeList[] = $name;
171  } else {
172  $attributeList[] = $name . '="' . htmlspecialchars($value, ENT_QUOTES | ENT_HTML5) . '"';
173  }
174  }
175  return implode(' ', $attributeList);
176  }
177 }
render(FileInterface $file, $width, $height, array $options=null, $usedPathsRelativeToCurrentScript=false)
static forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:31
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']