add swf and mp3 support
[squirrelmail.git] / plugins / newmail / functions.php
CommitLineData
dbc7cd0a 1<?php
2/**
3 * SquirrelMail NewMail plugin
4 *
5 * Functions
6 * @version $Id$
7 * @package plugins
8 * @subpackage new_mail
9 */
10
11/** file type defines */
12define('SM_NEWMAIL_FILETYPE_WAV',2);
13define('SM_NEWMAIL_FILETYPE_MP3',3);
14define('SM_NEWMAIL_FILETYPE_OGG',4);
15define('SM_NEWMAIL_FILETYPE_SWF',5);
16define('SM_NEWMAIL_FILETYPE_SVG',6);
17
18/** load default config */
19if (file_exists(SM_PATH . 'plugins/newmail/config_default.php')) {
20 include_once(SM_PATH . 'plugins/newmail/config_default.php');
21}
22
23/** load config */
24if (file_exists(SM_PATH . 'config/newmail_config.php')) {
25 include_once(SM_PATH . 'config/newmail_config.php');
26} elseif (file_exists(SM_PATH . 'plugins/newmail/config.php')) {
27 include_once(SM_PATH . 'plugins/newmail/config.php');
28}
29
30/**
31 * Function tries to detect if file contents match declared file type
32 *
33 * Function returns default extension for detected mime type or 'false'
34 *
35 * TODO: use $contents to check if file is in specified type
36 * @param string $contents file contents
37 * @param string $type file mime type
38 * @return string
39 */
40function newmail_detect_filetype($contents,$type) {
41 // convert $type to lower case
42 $type=strtolower($type);
43
44 $ret=false;
45
46 switch ($type) {
47 case 'audio/x-wav':
48 $ret='wav';
49 break;
50 case 'audio/mpeg':
51 $ret='mp3';
52 break;
53 case 'application/ogg':
54 $ret='ogg';
55 break;
56 case 'application/x-shockwave-flash':
57 $ret='swf';
58 break;
59 case 'image/svg+xml':
60 $ret='svg';
61 break;
62 default:
63 $ret=false;
64 }
65 return $ret;
66}
67
68/**
69 * @param string $type
70 * @param string $filename
71 * @return integer
72 */
73function newmail_get_mediatype($type,$filename) {
74 switch ($type) {
75 // fix for browser's that upload file as application/octet-stream
76 case 'application/octet-stream':
77 $ret=newmail_get_mediatype_by_ext($filename);
78 break;
79 case 'audio/x-wav':
80 $ret=SM_NEWMAIL_FILETYPE_WAV;
81 break;
82 case 'audio/mpeg':
83 $ret=SM_NEWMAIL_FILETYPE_MP3;
84 break;
85 case 'application/ogg':
86 $ret=SM_NEWMAIL_FILETYPE_OGG;
87 break;
88 case 'application/x-shockwave-flash':
89 $ret=SM_NEWMAIL_FILETYPE_SWF;
90 break;
91 case 'image/svg+xml':
92 $ret=SM_NEWMAIL_FILETYPE_SVG;
93 break;
94 default:
95 $ret=false;
96 }
97 return $ret;
98}
99
100/**
101 * Function provides filetype detection for browsers, that
102 * upload files with application/octet-stream file type.
103 * Ex. Opera.
104 * @param string $filename
105 * @return string
106 */
107function newmail_get_mediatype_by_ext($filename) {
108 if (preg_match("/\.wav$/i",$filename)) return SM_NEWMAIL_FILETYPE_WAV;
109 if (preg_match("/\.mp3$/i",$filename)) return SM_NEWMAIL_FILETYPE_MP3;
110 if (preg_match("/\.ogg$/i",$filename)) return SM_NEWMAIL_FILETYPE_OGG;
111 if (preg_match("/\.swf$/i",$filename)) return SM_NEWMAIL_FILETYPE_SWF;
112 if (preg_match("/\.svg$/i",$filename)) return SM_NEWMAIL_FILETYPE_SVG;
113 return false;
114}
115
116/**
117 * Creates html object tags of multimedia object
118 *
119 * Main function that creates multimedia object tags
120 * @param string $object object name
121 * @param integer $type media object type
122 * @param string $path URL to media object
123 * @param array $args media object attributes
124 * @param string $extra tags that have to buried deep inside object tags
125 * @param bool $addsuffix controls addition of suffix to media object url
126 * @return string object html tags and attributes required by selected media type.
127 */
128function newmail_media_objects($object,$types,$path,$args=array(),$extra='',$addsuffix=true) {
129 global $newmail_mediacompat_mode;
130
131 // first prepare single object for IE
132 $ret = newmail_media_object_ie($object,$types[0],$path,$args,$addsuffix);
133
134 // W3.org nested objects
135 $ret.= "<!--[if !IE]> <-->\n"; // not for IE
136
137 foreach ($types as $type) {
138 $ret.= newmail_media_object($object,$type,$path,$args,$addsuffix);
139 }
140
141 if (isset($newmail_mediacompat_mode) && $newmail_mediacompat_mode)
142 $ret.= newmail_media_embed($object,$types[0],$path,$args,$addsuffix);
143 // add $extra code inside objects
144 if ($extra!='')
145 $ret.=$extra . "\n";
146
147 if (isset($newmail_mediacompat_mode) && $newmail_mediacompat_mode)
148 $ret.= newmail_media_embed_close($types[0]);
149
150 foreach (array_reverse($types) as $type) {
151 $ret.= newmail_media_object_close($type);
152 }
153 $ret.= "<!--> <![endif]-->\n"; // end non-IE mode
154 // close IE object
155 $ret.= newmail_media_object_ie_close($types[0]);
156
157 return $ret;
158}
159
160/**
161 * Creates object tags of multimedia object for browsers that comply to w3.org
162 * specifications.
163 *
164 * Warnings:
165 * * Returned string does not contain html closing tag.
166 * * This is internal function, use newmail_media_objects() instead
167 * @param string $object object name
168 * @param integer $type media object type
169 * @param string $path URL to media object
170 * @param array $args media object attributes
171 * @param bool $addsuffix controls addition of suffix to media object url
172 * @return string object html tags and attributes required by selected media type.
173 */
174function newmail_media_object($object,$type,$path,$args=array(),$addsuffix=true) {
175 $ret_w3='';
176 $suffix='';
177 $sArgs=newmail_media_prepare_args($args);
178
179 switch ($type) {
180 case SM_NEWMAIL_FILETYPE_SWF:
181 if ($addsuffix) $suffix='.swf';
182 $ret_w3 = '<object data="' . $path . $object . $suffix . '" '
183 .$sArgs
184 .'type="application/x-shockwave-flash">' . "\n";
185 break;
186 case SM_NEWMAIL_FILETYPE_WAV:
187 if ($addsuffix) $suffix='.wav';
188 $ret_w3 = '<object data="' . $path . $object . $suffix . '" '
189 .$sArgs
190 .'type="audio/x-wav">' . "\n";
191 break;
192 case SM_NEWMAIL_FILETYPE_OGG:
193 if ($addsuffix) $suffix='.ogg';
194 $ret_w3 = '<object data="' . $path . $object . $suffix . '" '
195 .$sArgs
196 .'type="application/ogg">' . "\n";
197 break;
198 case SM_NEWMAIL_FILETYPE_MP3:
199 if ($addsuffix) $suffix='.mp3';
200 $ret_w3 = '<object data="' . $path . $object . $suffix . '" '
201 .$sArgs
202 .'type="audio/mpeg">' . "\n";
203 break;
204 case SM_NEWMAIL_FILETYPE_SVG:
205 if ($addsuffix) $suffix='.svg';
206 $ret_w3 = '<object data="' . $path . $object . $suffix . '" '
207 .$sArgs
208 .'type="image/svg+xml">' . "\n";
209 break;
210 default:
211 $ret_w3='';
212 }
213 return $ret_w3;
214}
215
216/**
217 * Creates multimedia object tags for Internet Explorer (Win32)
218 *
219 * Warning:
220 * * Returned string does not contain html closing tag, because
221 * this multimedia object can include other media objects.
222 * * This is internal function, use newmail_media_objects() instead
223 *
224 * @param string $object object name
225 * @param integer $type media object type
226 * @param string $path URL to media object
227 * @param array $args media object attributes
228 * @param bool $addsuffix controls addition of suffix to media object url
229 * @return string object html tags and attributes required by selected media type.
230 */
231function newmail_media_object_ie($object,$type,$path,$args=array(),$addsuffix) {
232 $ret_ie='';
233 $suffix='';
234 $sArgs=newmail_media_prepare_args($args);
235
236 switch ($type) {
237 case SM_NEWMAIL_FILETYPE_SWF:
238 if ($addsuffix) $suffix='.swf';
239 $ret_ie ='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '
240 .'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" '
241 . $sArgs . 'id="' . $object ."\">\n"
242 .'<param name="movie" value="' . $path . $object . $suffix . "\">\n"
243 .'<param name="hidden" value="true">' . "\n";
244 break;
245 case SM_NEWMAIL_FILETYPE_WAV:
246 if ($addsuffix) $suffix='.wav';
247 $ret_ie ='<object classid="clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95" '
248 .'codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,0902" '
249 . $sArgs . 'id="' . $object ."\" \n"
250 .'type="audio/x-wav">' ."\n"
251 .'<param name="FileName" value="' . $path . $object . $suffix . "\">\n";
252 break;
253 case SM_NEWMAIL_FILETYPE_MP3:
254 if ($addsuffix) $suffix='.mp3';
255 $ret_ie ='<object classid="clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95" '
256 .'codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,0902" '
257 . $sArgs . 'id="' . $object ."\" \n"
258 .'type="audio/mpeg">' ."\n"
259 .'<param name="FileName" value="' . $path . $object . $suffix . "\">\n";
260 break;
261 case SM_NEWMAIL_FILETYPE_OGG:
262 case SM_NEWMAIL_FILETYPE_SVG:
263 default:
264 $ret_ie='';
265 }
266 return $ret_ie;
267}
268
269/**
270 * Creates embed tags of multimedia object
271 *
272 * docs about embed
273 * Apple: http://www.apple.com/quicktime/authoring/embed.html
274 *
275 * Warnings:
276 * * Returned string does not contain html closing tag.
277 * * embed tags will be created by newmail_media_objects() only
278 * when $newmail_mediacompat_mode option is enabled. Option is not
279 * enabled by default in order to comply to w3.org specs.
280 * * This is internal function, use newmail_media_objects() instead
281 * @link http://www.apple.com/quicktime/authoring/embed.html Info about embed tag
282 * @param string $object object name
283 * @param integer $type media object type
284 * @param string $path URL to media object
285 * @param array $args media object attributes
286 * @param bool $addsuffix controls addition of suffix to media object url
287 * @return string embed html tags and attributes required by selected media type.
288 */
289function newmail_media_embed($object,$type,$path,$args=array(),$addsuffix=true) {
290 $ret_embed='';
291 $suffix='';
292 $sArgs=newmail_media_prepare_args($args);
293
294 switch ($type) {
295 case SM_NEWMAIL_FILETYPE_SWF:
296 if ($addsuffix) $suffix='.swf';
297 $ret_embed='<embed src="' . $path . $object . $suffix . '" '. "\n"
298 .'hidden="true" autostart="true" '. "\n"
299 .$sArgs . "\n"
300 .'name="' . $object .'" ' . "\n"
301 .'type="application/x-shockwave-flash" ' . "\n"
302 .'pluginspage="http://www.macromedia.com/go/getflashplayer">' . "\n";
303 break;
304 case SM_NEWMAIL_FILETYPE_WAV:
305 if ($addsuffix) $suffix='.wav';
306 $ret_embed='<embed src="' . $path . $object . $suffix . '" '. "\n"
307 .' hidden="true" autostart="true" '. "\n"
308 .' ' .$sArgs . "\n"
309 .' name="' . $object .'" ' . "\n"
310 .' type="audio/x-wav">' . "\n";
311 break;
312 case SM_NEWMAIL_FILETYPE_OGG:
313 case SM_NEWMAIL_FILETYPE_MP3:
314 case SM_NEWMAIL_FILETYPE_SVG:
315 default:
316 $ret_embed='';
317 }
318 return $ret_embed;
319}
320
321/**
322 * Adds closing tags for ie object
323 * Warning:
324 * * This is internal function, use newmail_media_objects() instead
325 * @param integer $type media object type
326 * @return string closing tag of media object
327 */
328function newmail_media_object_ie_close($type) {
329 $ret_end='';
330 switch ($type) {
331 case SM_NEWMAIL_FILETYPE_SWF:
332 case SM_NEWMAIL_FILETYPE_WAV:
333 case SM_NEWMAIL_FILETYPE_MP3:
334 $ret_end="</object>\n";
335 break;
336 case SM_NEWMAIL_FILETYPE_OGG:
337 case SM_NEWMAIL_FILETYPE_SVG:
338 default:
339 $ret_end='';
340 }
341 return $ret_end;
342}
343
344/**
345 * Adds closing tags for object
346 * Warning:
347 * * This is internal function, use newmail_media_objects() instead
348 * @param integer $type media object type
349 * @return string closing tag of media object
350 */
351function newmail_media_object_close($type) {
352 $ret_end='';
353 switch ($type) {
354 case SM_NEWMAIL_FILETYPE_SWF:
355 case SM_NEWMAIL_FILETYPE_WAV:
356 case SM_NEWMAIL_FILETYPE_OGG:
357 case SM_NEWMAIL_FILETYPE_MP3:
358 case SM_NEWMAIL_FILETYPE_SVG:
359 $ret_end="</object>\n";
360 break;
361 default:
362 $ret_end='';
363 }
364 return $ret_end;
365}
366
367/**
368 * Adds closing tags for object
369 * Warning:
370 * * This is internal function, use newmail_media_objects() instead
371 * @param integer $type media object type
372 * @return string closing tag of media object
373 */
374function newmail_media_embed_close($type) {
375 $ret_end='';
376 switch ($type) {
377 case SM_NEWMAIL_FILETYPE_SWF:
378 case SM_NEWMAIL_FILETYPE_WAV:
379 $ret_end="</embed>\n";
380 break;
381 case SM_NEWMAIL_FILETYPE_OGG:
382 case SM_NEWMAIL_FILETYPE_MP3:
383 case SM_NEWMAIL_FILETYPE_SVG:
384 default:
385 $ret_end='';
386 }
387 return $ret_end;
388}
389
390/**
391 * Converts media attributes to string
392 * Warning:
393 * * attribute values are automatically sanitized by htmlspecialchars()
394 * * This is internal function, use newmail_media_objects() instead
395 * @param array $args array with object attributes
396 * @return string string with object attributes
397 */
398function newmail_media_prepare_args($args) {
399 $ret_args='';
400 foreach ($args as $arg => $value) {
401 $ret_args.= $arg . '="' . htmlspecialchars($value) . '" ';
402 }
403 return $ret_args;
404}
405
406/**
407 * Detects used media type and creates all need tags
408 * @param string $newmail_media
409 * @return string html tags with media objects
410 */
411function newmail_create_media_tags($newmail_media) {
412 global $newmail_mmedia, $newmail_userfile_type;
413
414 if (preg_match("/^mmedia_+/",$newmail_media)) {
415 $ret_media = "<!-- newmail mmedia option -->\n";
416 // remove mmedia key
417 $newmail_mmedia_short=preg_replace("/^mmedia_/",'',$newmail_media);
418 // check if media option is not removed
419 if (isset($newmail_mmedia[$newmail_mmedia_short])) {
420 $ret_media.= newmail_media_objects($newmail_mmedia_short,
421 $newmail_mmedia[$newmail_mmedia_short]['types'],
422 sqm_baseuri() . 'plugins/newmail/media/',
423 $newmail_mmedia[$newmail_mmedia_short]['args']);
424 }
425 $ret_media.= "<!-- end of newmail mmedia option -->\n";
426 } elseif ($newmail_media=='(userfile)') {
427 $ret_media = "<!-- newmail usermedia option -->\n";
428 $ret_media.= newmail_media_objects('loadfile.php',
429 array($newmail_userfile_type),
430 sqm_baseuri() . 'plugins/newmail/',
431 array('width'=>0,'height'=>0),
432 '',false);
433 $ret_media.= "<!-- end of newmail usermedia option -->\n";
434 } else {
435 $ret_media = "<!-- newmail sounds from sounds/*.wav -->\n";
436 $ret_media.= newmail_media_objects(basename($newmail_media),
437 array(SM_NEWMAIL_FILETYPE_WAV),
438 sqm_baseuri() . 'plugins/newmail/sounds/',
439 array('width'=>0,'height'=>0),
440 '',false);
441 $ret_media.= "<!-- end of newmail sounds from sounds/*.wav -->\n";
442 }
443 return $ret_media;
444}
445?>