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