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