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