[BUGFIX] Unused TDParams in ColumnsContentObject()
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / ContentObject / MediaContentObject.php
1 <?php
2 namespace TYPO3\CMS\Frontend\ContentObject;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2013 Xavier Perseguers <typo3@perseguers.ch>
8 * (c) 2010-2013 Steffen Kamper <steffen@typo3.org>
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the text file GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 /**
31 * Contains MEDIA class object.
32 *
33 * @author Xavier Perseguers <typo3@perseguers.ch>
34 * @author Steffen Kamper <steffen@typo3.org>
35 */
36 class MediaContentObject extends \TYPO3\CMS\Frontend\ContentObject\AbstractContentObject {
37
38 /**
39 * Rendering the cObject, MEDIA
40 *
41 * @param array $conf Array of TypoScript properties
42 * @return string Output
43 */
44 public function render($conf = array()) {
45 $content = '';
46 // Add flex parameters to configuration
47 $flexParams = isset($conf['flexParams.']) ? $this->cObj->stdWrap($conf['flexParams'], $conf['flexParams.']) : $conf['flexParams'];
48 if (substr($flexParams, 0, 1) === '<') {
49 // It is a content element rather a TS object
50 $flexParams = \TYPO3\CMS\Core\Utility\GeneralUtility::xml2array($flexParams, 'T3');
51 foreach ($flexParams['data'] as $sheetData) {
52 $this->cObj->readFlexformIntoConf($sheetData['lDEF'], $conf['parameter.'], TRUE);
53 }
54 }
55 // Type is video or audio
56 $conf['type'] = $this->doFlexFormOverlay($conf, 'type');
57 // Video sources
58 $sources = $this->doFlexFormOverlay($conf, 'sources', 'mmSourcesContainer');
59 if (is_array($sources) && count($sources)) {
60 $conf['sources'] = array();
61 foreach ($sources as $key => $source) {
62 if (isset($source['mmSource'])) {
63 $source = $source['mmSource'];
64 $conf['sources'][$key] = $this->retrieveMediaUrl($source);
65 }
66 }
67 } else {
68 unset($conf['sources']);
69 }
70 // Video fallback and backward compatibility file
71 $videoFallback = $this->doFlexFormOverlay($conf, 'file');
72
73 // Backward compatibility file
74 if ($videoFallback !== NULL) {
75 $conf['file'] = $this->retrieveMediaUrl($videoFallback);
76 } else {
77 unset($conf['file']);
78 }
79 // Audio sources
80 $audioSources = $this->doFlexFormOverlay($conf, 'audioSources', 'mmAudioSourcesContainer');
81 if (is_array($audioSources) && count($audioSources)) {
82 $conf['audioSources'] = array();
83 foreach ($audioSources as $key => $source) {
84 if (isset($source['mmAudioSource'])) {
85 $source = $source['mmAudioSource'];
86 $conf['audioSources'][$key] = $this->retrieveMediaUrl($source);
87 }
88 }
89 } else {
90 unset($conf['audioSources']);
91 }
92 // Audio fallback
93 $audioFallback = $this->doFlexFormOverlay($conf, 'audioFallback');
94 if ($audioFallback) {
95 $conf['audioFallback'] = $this->retrieveMediaUrl($audioFallback);
96 } else {
97 unset($conf['audioFallback']);
98 }
99 // Caption file
100 $caption = $this->doFlexFormOverlay($conf, 'caption');
101 if ($caption) {
102 $conf['caption'] = $this->retrieveMediaUrl($caption);
103 } else {
104 unset($conf['caption']);
105 }
106 // Establish render type
107 $renderType = $this->doFlexFormOverlay($conf, 'renderType');
108 $conf['preferFlashOverHtml5'] = 0;
109 if ($renderType === 'preferFlashOverHtml5') {
110 $renderType = 'auto';
111 }
112 if ($renderType === 'auto') {
113 // Default renderType is swf
114 $renderType = 'swf';
115 $handler = array_keys($conf['fileExtHandler.']);
116 if ($conf['type'] === 'video') {
117 $fileinfo = \TYPO3\CMS\Core\Utility\GeneralUtility::split_fileref($conf['file']);
118 } else {
119 $fileinfo = \TYPO3\CMS\Core\Utility\GeneralUtility::split_fileref($conf['audioFallback']);
120 }
121 if (in_array($fileinfo['fileext'], $handler)) {
122 $renderType = strtolower($conf['fileExtHandler.'][$fileinfo['fileext']]);
123 }
124 }
125 $mime = $renderType . 'object';
126 $typeConf = $conf['mimeConf.'][$mime . '.'][$conf['type'] . '.'] ?: array();
127 $conf['predefined'] = array();
128 // Width and height
129 $conf['width'] = (int)$this->doFlexFormOverlay($conf, 'width');
130 $conf['height'] = (int)$this->doFlexFormOverlay($conf, 'height');
131 if (is_array($conf['parameter.']['mmMediaOptions'])) {
132 foreach ($conf['parameter.']['mmMediaOptions'] as $key => $value) {
133 if ($key == 'mmMediaCustomParameterContainer') {
134 foreach ($value as $val) {
135 // Custom parameter entry
136 $rawTS = $val['mmParamCustomEntry'];
137 // Read and merge
138 $tmp = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(LF, $rawTS);
139 if (count($tmp)) {
140 foreach ($tmp as $tsLine) {
141 if (substr($tsLine, 0, 1) != '#' && ($pos = strpos($tsLine, '.'))) {
142 $parts[0] = substr($tsLine, 0, $pos);
143 $parts[1] = substr($tsLine, $pos + 1);
144 $valueParts = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode('=', $parts[1], TRUE);
145 switch (strtolower($parts[0])) {
146 case 'flashvars':
147 $conf['flashvars.'][$valueParts[0]] = $valueParts[1];
148 break;
149 case 'params':
150 $conf['params.'][$valueParts[0]] = $valueParts[1];
151 break;
152 case 'attributes':
153 $conf['attributes.'][$valueParts[0]] = $valueParts[1];
154 break;
155 }
156 }
157 }
158 }
159 }
160 } elseif ($key == 'mmMediaOptionsContainer') {
161 foreach ($value as $val) {
162 if (isset($val['mmParamSet'])) {
163 $pName = $val['mmParamName'];
164 $pSet = $val['mmParamSet'];
165 $pValue = $pSet == 2 ? $val['mmParamValue'] : ($pSet == 0 ? 'false' : 'true');
166 $conf['predefined'][$pName] = $pValue;
167 }
168 }
169 }
170 }
171 }
172 if ($renderType === 'swf' && $this->doFlexFormOverlay($conf, 'useHTML5')) {
173 $renderType = 'flowplayer';
174 }
175 if ($conf['type'] === 'audio' && !isset($conf['audioSources'])) {
176 $renderType = 'swf';
177 }
178 if ($renderType !== 'qt' && $renderType !== 'embed' && $conf['type'] == 'video') {
179 if (isset($conf['file']) && (strpos($conf['file'], '.swf') !== FALSE || strpos($conf['file'], '://') !== FALSE && strpos(\TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($conf['file'], 2), 'application/x-shockwave-flash') !== FALSE)) {
180 $conf = array_merge((array) $conf['mimeConf.']['swfobject.'], $conf);
181 $conf[$conf['type'] . '.']['player'] = strpos($conf['file'], '://') === FALSE ? 'http://' . $conf['file'] : $conf['file'];
182 $conf['installUrl'] = 'null';
183 $conf['forcePlayer'] = 0;
184 $renderType = 'swf';
185 } elseif (isset($conf['file']) && strpos($conf['file'], '://') !== FALSE) {
186 $mediaWizard = \TYPO3\CMS\Frontend\MediaWizard\MediaWizardProviderManager::getValidMediaWizardProvider($conf['file']);
187 if ($mediaWizard !== NULL) {
188 $conf['installUrl'] = 'null';
189 $conf['forcePlayer'] = 0;
190 $renderType = 'swf';
191 }
192 } elseif (isset($conf['file']) && !isset($conf['caption']) && !isset($conf['sources'])) {
193 $renderType = 'swf';
194 $conf['forcePlayer'] = 1;
195 }
196 }
197 switch ($renderType) {
198 case 'flowplayer':
199 $conf[$conf['type'] . '.'] = array_merge((array) $conf['mimeConf.']['flowplayer.'][($conf['type'] . '.')], $typeConf);
200 $conf = array_merge((array) $conf['mimeConf.']['flowplayer.'], $conf);
201 unset($conf['mimeConf.']);
202 $conf['attributes.'] = array_merge((array) $conf['attributes.'], $conf['predefined']);
203 $conf['params.'] = array_merge((array) $conf['params.'], $conf['predefined']);
204 $conf['flashvars.'] = array_merge((array) $conf['flashvars.'], $conf['predefined']);
205 $content = $this->cObj->FLOWPLAYER($conf);
206 break;
207 case 'swf':
208 $conf[$conf['type'] . '.'] = array_merge((array) $conf['mimeConf.']['swfobject.'][($conf['type'] . '.')], $typeConf);
209 $conf = array_merge((array) $conf['mimeConf.']['swfobject.'], $conf);
210 unset($conf['mimeConf.']);
211 $conf['flashvars.'] = array_merge((array) $conf['flashvars.'], $conf['predefined']);
212 $content = $this->cObj->SWFOBJECT($conf);
213 break;
214 case 'qt':
215 $conf[$conf['type'] . '.'] = array_merge($conf['mimeConf.']['swfobject.'][$conf['type'] . '.'], $typeConf);
216 $conf = array_merge($conf['mimeConf.']['qtobject.'], $conf);
217 unset($conf['mimeConf.']);
218 $conf['params.'] = array_merge((array) $conf['params.'], $conf['predefined']);
219 $content = $this->cObj->QTOBJECT($conf);
220 break;
221 case 'embed':
222 $paramsArray = array_merge((array) $typeConf['default.']['params.'], (array) $conf['params.'], $conf['predefined']);
223 $conf['params'] = '';
224 foreach ($paramsArray as $key => $value) {
225 $conf['params'] .= $key . '=' . $value . LF;
226 }
227 $content = $this->cObj->MULTIMEDIA($conf);
228 break;
229 default:
230 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/hooks/class.tx_cms_mediaitems.php']['customMediaRender'])) {
231 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/hooks/class.tx_cms_mediaitems.php']['customMediaRender'] as $classRef) {
232 $hookObj = \TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj($classRef);
233 $conf['file'] = $videoFallback;
234 $conf['mode'] = is_file(PATH_site . $videoFallback) ? 'file' : 'url';
235 if (method_exists($hookObj, 'customMediaRender')) {
236 $content = $hookObj->customMediaRender($renderType, $conf, $this);
237 }
238 }
239 }
240 if (isset($conf['stdWrap.'])) {
241 $content = $this->cObj->stdWrap($content, $conf['stdWrap.']);
242 }
243 }
244 return $content;
245 }
246
247 /**
248 * Resolves the URL of an file
249 *
250 * @param string $file
251 * @return string|NULL
252 */
253 protected function retrieveMediaUrl($file) {
254 $returnValue = NULL;
255
256 // because the file value can possibly have link parameters, use explode to split all values
257 $fileParts = explode(' ', $file);
258
259 /** @var $mediaWizard \TYPO3\CMS\Frontend\MediaWizard\MediaWizardProviderInterface */
260 $mediaWizard = \TYPO3\CMS\Frontend\MediaWizard\MediaWizardProviderManager::getValidMediaWizardProvider($fileParts[0]);
261 // Get the path relative to the page currently outputted
262 if (substr($fileParts[0], 0, 5) === "file:") {
263 $fileUid = substr($fileParts[0], 5);
264
265 if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($fileUid)) {
266 $fileObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->getFileObject($fileUid);
267
268 if ($fileObject instanceof \TYPO3\CMS\Core\Resource\FileInterface) {
269 $returnValue = $fileObject->getPublicUrl();
270 }
271 }
272 } elseif (is_file(PATH_site . $fileParts[0])) {
273 $returnValue = $GLOBALS['TSFE']->tmpl->getFileName($fileParts[0]);
274 } elseif ($mediaWizard !== NULL) {
275 $returnValue = $this->cObj->typoLink_URL(array(
276 'parameter' => $mediaWizard->rewriteUrl($fileParts[0])
277 ));
278 } elseif (\TYPO3\CMS\Core\Utility\GeneralUtility::isValidUrl($fileParts[0])) {
279 $returnValue = $fileParts[0];
280 }
281
282 return $returnValue;
283 }
284
285 /**
286 * Looks up if the key is set via flexform and returns the actual value.
287 * If not present in flexform, it processes the value which might be given in TS
288 * with stdWrap (if needed) and returns that value.
289 *
290 * @param array &$confArray
291 * @param string $key
292 * @param string $sectionKey
293 * @return mixed
294 */
295 protected function doFlexFormOverlay(array &$confArray, $key, $sectionKey = NULL) {
296 $flexValue = NULL;
297 $flexKey = 'mm' . ucfirst($key);
298 if ($sectionKey === NULL) {
299 $flexValue = $confArray['parameter.'][$flexKey];
300 } else {
301 $flexValue = $confArray['parameter.'][$flexKey][$sectionKey];
302 }
303 if ($flexValue === NULL) {
304 $flexValue = isset($confArray[$key . '.']) ? $this->cObj->stdWrap($confArray[$key], $confArray[$key . '.']) : $confArray[$key];
305 }
306 return $flexValue;
307 }
308
309 }