TYPO3  7.6
YouTubeRenderer.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Rendering;
3 
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  if ($file instanceof FileReference) {
94  $autoplay = $file->getProperty('autoplay');
95  if ($autoplay !== null) {
96  $options['autoplay'] = $autoplay;
97  }
98  }
99 
100  $urlParams = array('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('HTTP_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  '//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 }