a9536a0a9e54c1cdb7498b79cf79cc49c221a537
4 * SquirrelMail NewMail plugin
8 * @copyright © 2001-2006 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
13 * @todo add midi support
17 if (! defined('SM_PATH')) define('SM_PATH','../../');
20 * sqm_baseuri() function for setups that don't load it by default
22 include_once(SM_PATH
. 'functions/display_messages.php');
24 /** file type defines */
25 define('SM_NEWMAIL_FILETYPE_WAV',2);
26 define('SM_NEWMAIL_FILETYPE_MP3',3);
27 define('SM_NEWMAIL_FILETYPE_OGG',4);
28 define('SM_NEWMAIL_FILETYPE_SWF',5);
29 define('SM_NEWMAIL_FILETYPE_SVG',6);
31 /** load default config */
32 if (file_exists(SM_PATH
. 'plugins/newmail/config_default.php')) {
33 include_once(SM_PATH
. 'plugins/newmail/config_default.php');
37 if (file_exists(SM_PATH
. 'config/newmail_config.php')) {
38 include_once(SM_PATH
. 'config/newmail_config.php');
39 } elseif (file_exists(SM_PATH
. 'plugins/newmail/config.php')) {
40 include_once(SM_PATH
. 'plugins/newmail/config.php');
43 // ----- hooked functions -----
46 * Register newmail option block
48 function newmail_optpage_register_block_function() {
49 // Gets added to the user's OPTIONS page.
50 global $optpage_blocks;
52 /* Register Squirrelspell with the $optionpages array. */
53 $optpage_blocks[] = array(
54 'name' => _("NewMail Options"),
55 'url' => sqm_baseuri() . 'plugins/newmail/newmail_opt.php',
56 'desc' => _("This configures settings for playing sounds and/or showing popup windows when new mail arrives."),
62 * Save newmail plugin settings
64 function newmail_sav_function() {
65 global $data_dir, $username, $_FILES, $newmail_uploadsounds;
67 if ( sqgetGlobalVar('submit_newmail', $submit, SQ_POST
) ) {
71 $media_changetitle = '';
76 sqgetGlobalVar('media_enable', $media_enable, SQ_POST
);
77 sqgetGlobalVar('media_popup', $media_popup, SQ_POST
);
78 sqgetGlobalVar('media_recent', $media_recent, SQ_POST
);
79 sqgetGlobalVar('media_changetitle', $media_changetitle, SQ_POST
);
80 sqgetGlobalVar('popup_width', $popup_width, SQ_POST
);
81 sqgetGlobalVar('popup_height', $popup_height, SQ_POST
);
83 // sanitize height and width
84 $popup_width = (int) $popup_width;
85 if ($popup_width<=0) $popup_width=200;
86 $popup_height = (int) $popup_height;
87 if ($popup_height<=0) $popup_height=130;
89 setPref($data_dir,$username,'newmail_enable',$media_enable);
90 setPref($data_dir,$username,'newmail_popup', $media_popup);
91 setPref($data_dir,$username,'newmail_recent',$media_recent);
92 setPref($data_dir,$username,'newmail_changetitle',$media_changetitle);
93 setPref($data_dir,$username,'newmail_popup_width',$popup_width);
94 setPref($data_dir,$username,'newmail_popup_height',$popup_height);
96 if( sqgetGlobalVar('media_sel', $media_sel, SQ_POST
) &&
97 $media_sel == '(none)' ) {
98 removePref($data_dir,$username,'newmail_media');
100 setPref($data_dir,$username,'newmail_media',$media_sel);
103 // process uploaded file
104 if ($newmail_uploadsounds && isset($_FILES['media_file']['tmp_name']) && $_FILES['media_file']['tmp_name']!='') {
105 // set temp file and get media file name
106 $newmail_tempmedia=getHashedDir($username, $data_dir) . "/$username.tempsound";
107 $newmail_mediafile=getHashedFile($username, $data_dir, $username . '.sound');
108 if (move_uploaded_file($_FILES['media_file']['tmp_name'], $newmail_tempmedia)) {
109 // new media file is in $newmail_tempmedia
110 if (file_exists($newmail_mediafile)) unlink($newmail_mediafile);
111 if (! rename($newmail_tempmedia,$newmail_mediafile)) {
112 // remove (userfile), if file rename fails
113 removePref($data_dir,$username,'newmail_media');
116 if (isset($_FILES['media_file']['type']) && isset($_FILES['media_file']['name'])) {
117 setPref($data_dir,$username,'newmail_userfile_type',
118 newmail_get_mediatype($_FILES['media_file']['type'],$_FILES['media_file']['name']));
120 removePref($data_dir,$username,'newmail_userfile_type');
123 if (isset($_FILES['media_file']['name'])) {
124 setPref($data_dir,$username,'newmail_userfile_name',basename($_FILES['media_file']['name']));
126 setPref($data_dir,$username,'newmail_userfile_name','mediafile.unknown');
135 * Load newmail plugin settings
137 function newmail_pref_function() {
138 global $username,$data_dir;
139 global $newmail_media,$newmail_media_enable,$newmail_popup;
140 global $newmail_recent, $newmail_changetitle;
141 global $newmail_userfile_type, $newmail_userfile_name;
142 global $newmail_popup_width, $newmail_popup_height;
144 $newmail_recent = getPref($data_dir,$username,'newmail_recent');
145 $newmail_media_enable = getPref($data_dir,$username,'newmail_enable');
146 $newmail_media = getPref($data_dir, $username, 'newmail_media', '(none)');
147 // remove full location from setting (since SM 1.5.1 plugin uses only filename).
148 if ($newmail_media!='(none)')
149 $newmail_media = basename($newmail_media);
151 $newmail_popup = getPref($data_dir, $username, 'newmail_popup');
152 $newmail_popup_width = getPref($data_dir, $username, 'newmail_popup_width',200);
153 $newmail_popup_height = getPref($data_dir, $username, 'newmail_popup_height',130);
154 $newmail_changetitle = getPref($data_dir, $username, 'newmail_changetitle');
156 $newmail_userfile_type = getPref($data_dir, $username, 'newmail_userfile_type');
157 $newmail_userfile_name = getPref($data_dir,$username,'newmail_userfile_name','');
163 * Used by option page when saving settings.
165 function newmail_set_loadinfo_function() {
166 global $optpage, $optpage_name;
167 if ($optpage=='newmail') {
168 $optpage_name=_("NewMail Options");
173 /* Receive the status of the folder and do something with it */
174 function newmail_folder_status($statusarr) {
175 global $newmail_media_enable,$newmail_popup,$newmail_changetitle,$trash_folder,
176 $send_folder,$totalNewArr,$unseen_notify, $newmail_recent;
178 //echo "GOT HOOK<br><pre>";
179 //var_dump($statusarr);
180 //echo "</pre><br>\n";
182 $mailbox=$statusarr['MAILBOX'];
183 if ($newmail_media_enable == 'on' ||
184 $newmail_popup == 'on' ||
185 $newmail_changetitle == 'on') {
187 // Skip folders for Sent and Trash
188 if ($statusarr['MAILBOX'] == $send_folder ||
$statusarr['MAILBOX'] == $trash_folder) {
192 if ((($mailbox == 'INBOX') && ($unseen_notify == 2)) ||
($unseen_notify == 3)) {
193 if (($newmail_recent == 'on') && (!empty($statusarr['RECENT']))) {
194 $totalNewArr[$mailbox] = $statusarr['RECENT'];
195 } elseif ($newmail_recent != 'on' && !empty($statusarr['UNSEEN'])) {
196 $totalNewArr[$mailbox] = $statusarr['UNSEEN'];
203 * Insert needed data in left_main
205 function newmail_plugin_function() {
206 global $username, $newmail_media, $newmail_media_enable, $newmail_popup,
207 $newmail_recent, $newmail_changetitle, $imapConnection, $PHP_SELF;
208 global $newmail_mmedia, $newmail_allowsound;
209 global $newmail_userfile_type;
210 global $newmail_popup_width, $newmail_popup_height;
213 if ($newmail_media_enable == 'on' ||
214 $newmail_popup == 'on' ||
215 $newmail_changetitle) {
217 if (!empty($totalNewArr)) { $totalNew=array_sum($totalNewArr); }
218 else { $totalNew=0; }
220 // If we found unseen messages, then we
221 // will play the sound as follows:
223 if ($newmail_changetitle) {
224 echo "<script type=\"text/javascript\">\n" .
225 "function ChangeTitleLoad() {\n";
226 echo 'window.parent.document.title = "' .
227 sprintf(ngettext("%s New Message","%s New Messages",$totalNew), $totalNew) .
229 echo "if (BeforeChangeTitle != null)\n".
230 "BeforeChangeTitle();\n".
232 "BeforeChangeTitle = window.onload;\n".
233 "window.onload = ChangeTitleLoad;\n".
237 // create media output if there are new email messages
238 if ($newmail_allowsound && $totalNew > 0
239 && $newmail_media_enable == 'on'
240 && $newmail_media != '' ) {
241 echo newmail_create_media_tags($newmail_media);
244 if ($totalNew > 0 && $newmail_popup == 'on') {
245 // Idea by: Nic Wolfe (Nic@TimelapseProductions.com)
246 // Web URL: http://fineline.xs.mw
247 // More code from Tyler Akins
248 echo "<script type=\"text/javascript\">\n".
250 "function PopupScriptLoad() {\n".
251 'window.open("'.sqm_baseuri().'plugins/newmail/newmail.php?numnew='.$totalNew.
253 "\"width=$newmail_popup_width,height=$newmail_popup_height,scrollbars=no\");\n".
254 "if (BeforePopupScript != null)\n".
255 "BeforePopupScript();\n".
257 "BeforePopupScript = window.onload;\n".
258 "window.onload = PopupScriptLoad;\n".
265 // ----- end of hooked functions -----
270 * Function tries to detect if file contents match declared file type
272 * Function returns default extension for detected mime type or 'false'
274 * TODO: use $contents to check if file is in specified type
275 * @param string $contents file contents
276 * @param string $type file mime type
279 function newmail_detect_filetype($contents,$type) {
280 // convert $type to lower case
281 $type=strtolower($type);
292 case 'application/ogg':
295 case 'application/x-shockwave-flash':
298 case 'image/svg+xml':
308 * Function tries to detect uploaded file type
309 * @param string $type
310 * @param string $filename
311 * @return integer One of SM_NEWMAIL_FILETYPE_* defines or false.
313 function newmail_get_mediatype($type,$filename) {
315 // fix for browser's that upload file as application/octet-stream
316 case 'application/octet-stream':
317 $ret=newmail_get_mediatype_by_ext($filename);
320 $ret=SM_NEWMAIL_FILETYPE_WAV
;
323 $ret=SM_NEWMAIL_FILETYPE_MP3
;
325 case 'application/ogg':
326 $ret=SM_NEWMAIL_FILETYPE_OGG
;
328 case 'application/x-shockwave-flash':
329 $ret=SM_NEWMAIL_FILETYPE_SWF
;
331 case 'image/svg+xml':
332 $ret=SM_NEWMAIL_FILETYPE_SVG
;
341 * Function provides filetype detection for browsers, that
342 * upload files with application/octet-stream file type.
343 * Ex. some version of Opera.
344 * @param string $filename
345 * @return integer One of SM_NEWMAIL_FILETYPE_* defines or false.
347 function newmail_get_mediatype_by_ext($filename) {
348 if (preg_match("/\.wav$/i",$filename)) return SM_NEWMAIL_FILETYPE_WAV
;
349 if (preg_match("/\.mp3$/i",$filename)) return SM_NEWMAIL_FILETYPE_MP3
;
350 if (preg_match("/\.ogg$/i",$filename)) return SM_NEWMAIL_FILETYPE_OGG
;
351 if (preg_match("/\.swf$/i",$filename)) return SM_NEWMAIL_FILETYPE_SWF
;
352 if (preg_match("/\.svg$/i",$filename)) return SM_NEWMAIL_FILETYPE_SVG
;
357 * Creates html object tags of multimedia object
359 * Main function that creates multimedia object tags
360 * @param string $object object name
361 * @param integer $type media object type
362 * @param string $path URL to media object
363 * @param array $args media object attributes
364 * @param string $extra tags that have to buried deep inside object tags
365 * @param bool $addsuffix controls addition of suffix to media object url
366 * @return string object html tags and attributes required by selected media type.
368 function newmail_media_objects($object,$types,$path,$args=array(),$extra='',$addsuffix=true) {
369 global $newmail_mediacompat_mode;
371 // first prepare single object for IE
372 $ret = newmail_media_object_ie($object,$types[0],$path,$args,$addsuffix);
374 // W3.org nested objects
375 $ret.= "<!--[if !IE]> <-->\n"; // not for IE
377 foreach ($types as $type) {
378 $ret.= newmail_media_object($object,$type,$path,$args,$addsuffix);
381 if (isset($newmail_mediacompat_mode) && $newmail_mediacompat_mode)
382 $ret.= newmail_media_embed($object,$types[0],$path,$args,$addsuffix);
383 // add $extra code inside objects
388 if (isset($newmail_mediacompat_mode) && $newmail_mediacompat_mode)
389 $ret.= newmail_media_embed_close($types[0]);
391 // close w3.org nested objects
392 foreach (array_reverse($types) as $type) {
393 $ret.= newmail_media_object_close($type);
395 $ret.= "<!--> <![endif]-->\n"; // end non-IE mode
397 $ret.= newmail_media_object_ie_close($types[0]);
403 * Creates object tags of multimedia object for browsers that comply to w3.org
408 * <li>Returned string does not contain html closing tag.
409 * <li>This is internal function, use newmail_media_objects() instead
411 * @link http://www.w3.org/TR/html4/struct/objects.html#edef-OBJECT W3.org specs
412 * @param string $object object name
413 * @param integer $type media object type
414 * @param string $path URL to media object
415 * @param array $args media object attributes
416 * @param bool $addsuffix controls addition of suffix to media object url
417 * @return string object html tags and attributes required by selected media type.
419 function newmail_media_object($object,$type,$path,$args=array(),$addsuffix=true) {
422 $sArgs=newmail_media_prepare_args($args);
425 case SM_NEWMAIL_FILETYPE_SWF
:
426 if ($addsuffix) $suffix='.swf';
427 $ret_w3 = '<object data="' . $path . $object . $suffix . '" '
429 .'type="application/x-shockwave-flash">' . "\n";
431 case SM_NEWMAIL_FILETYPE_WAV
:
432 if ($addsuffix) $suffix='.wav';
433 $ret_w3 = '<object data="' . $path . $object . $suffix . '" '
435 .'type="audio/x-wav">' . "\n";
437 case SM_NEWMAIL_FILETYPE_OGG
:
438 if ($addsuffix) $suffix='.ogg';
439 $ret_w3 = '<object data="' . $path . $object . $suffix . '" '
441 .'type="application/ogg">' . "\n";
443 case SM_NEWMAIL_FILETYPE_MP3
:
444 if ($addsuffix) $suffix='.mp3';
445 $ret_w3 = '<object data="' . $path . $object . $suffix . '" '
447 .'type="audio/mpeg">' . "\n";
449 case SM_NEWMAIL_FILETYPE_SVG
:
450 if ($addsuffix) $suffix='.svg';
451 $ret_w3 = '<object data="' . $path . $object . $suffix . '" '
453 .'type="image/svg+xml">' . "\n";
462 * Creates multimedia object tags for Internet Explorer (Win32)
465 * * Returned string does not contain html closing tag, because
466 * this multimedia object can include other media objects.
467 * * This is internal function, use newmail_media_objects() instead
469 * @param string $object object name
470 * @param integer $type media object type
471 * @param string $path URL to media object
472 * @param array $args media object attributes
473 * @param bool $addsuffix controls addition of suffix to media object url
474 * @return string object html tags and attributes required by selected media type.
475 * @todo add ogg and svg support
477 function newmail_media_object_ie($object,$type,$path,$args=array(),$addsuffix) {
480 $sArgs=newmail_media_prepare_args($args);
483 case SM_NEWMAIL_FILETYPE_SWF
:
484 if ($addsuffix) $suffix='.swf';
485 $ret_ie ='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '
486 .'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" '
487 . $sArgs . 'id="' . $object ."\">\n"
488 .'<param name="movie" value="' . $path . $object . $suffix . "\">\n"
489 .'<param name="hidden" value="true">' . "\n";
491 case SM_NEWMAIL_FILETYPE_WAV
:
492 if ($addsuffix) $suffix='.wav';
493 $ret_ie ='<object classid="clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95" '
494 .'codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,0902" '
495 . $sArgs . 'id="' . $object ."\" \n"
496 .'type="audio/x-wav">' ."\n"
497 .'<param name="FileName" value="' . $path . $object . $suffix . "\">\n";
499 case SM_NEWMAIL_FILETYPE_MP3
:
500 if ($addsuffix) $suffix='.mp3';
501 $ret_ie ='<object classid="clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95" '
502 .'codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,0902" '
503 . $sArgs . 'id="' . $object ."\" \n"
504 .'type="audio/mpeg">' ."\n"
505 .'<param name="FileName" value="' . $path . $object . $suffix . "\">\n";
507 case SM_NEWMAIL_FILETYPE_OGG
:
508 case SM_NEWMAIL_FILETYPE_SVG
:
516 * Creates embed tags of multimedia object
519 * Apple: http://www.apple.com/quicktime/authoring/embed.html
522 * * Returned string does not contain html closing tag.
523 * * embed tags will be created by newmail_media_objects() only
524 * when $newmail_mediacompat_mode option is enabled. Option is not
525 * enabled by default in order to comply to w3.org specs.
526 * * This is internal function, use newmail_media_objects() instead
527 * @link http://www.apple.com/quicktime/authoring/embed.html Info about embed tag
528 * @param string $object object name
529 * @param integer $type media object type
530 * @param string $path URL to media object
531 * @param array $args media object attributes
532 * @param bool $addsuffix controls addition of suffix to media object url
533 * @return string embed html tags and attributes required by selected media type.
535 function newmail_media_embed($object,$type,$path,$args=array(),$addsuffix=true) {
538 $sArgs=newmail_media_prepare_args($args);
541 case SM_NEWMAIL_FILETYPE_SWF
:
542 if ($addsuffix) $suffix='.swf';
543 $ret_embed='<embed src="' . $path . $object . $suffix . '" '. "\n"
544 .'hidden="true" autostart="true" '. "\n"
546 .'name="' . $object .'" ' . "\n"
547 .'type="application/x-shockwave-flash" ' . "\n"
548 .'pluginspage="http://www.macromedia.com/go/getflashplayer">' . "\n";
550 case SM_NEWMAIL_FILETYPE_WAV
:
551 if ($addsuffix) $suffix='.wav';
552 $ret_embed='<embed src="' . $path . $object . $suffix . '" '. "\n"
553 .' hidden="true" autostart="true" '. "\n"
555 .' name="' . $object .'" ' . "\n"
556 .' type="audio/x-wav">' . "\n";
558 case SM_NEWMAIL_FILETYPE_SVG
:
559 if ($addsuffix) $suffix='.svg';
560 $ret_embed='<embed src="' . $path . $object . $suffix . '" '. "\n"
561 .'hidden="true" autostart="true" '. "\n"
563 .'name="' . $object .'" ' . "\n"
564 .'type="image/svg-xml" ' . "\n"
565 .'pluginspage="http://www.adobe.com/svg/viewer/install/">' . "\n";
567 case SM_NEWMAIL_FILETYPE_OGG
:
568 if ($addsuffix) $suffix='.ogg';
569 $ret_embed='<embed src="' . $path . $object . $suffix . '" '. "\n"
570 .' hidden="true" autostart="true" '. "\n"
572 .' name="' . $object .'" ' . "\n"
573 .' type="application/ogg">' . "\n";
575 case SM_NEWMAIL_FILETYPE_MP3
:
576 if ($addsuffix) $suffix='.mp3';
577 $ret_embed='<embed src="' . $path . $object . $suffix . '" '. "\n"
578 .' hidden="true" autostart="true" '. "\n"
580 .' name="' . $object .'" ' . "\n"
581 .' type="audio/mpeg">' . "\n";
590 * Adds closing tags for ie object
592 * * This is internal function, use newmail_media_objects() instead
593 * @param integer $type media object type
594 * @return string closing tag of media object
596 function newmail_media_object_ie_close($type) {
599 case SM_NEWMAIL_FILETYPE_SWF
:
600 case SM_NEWMAIL_FILETYPE_WAV
:
601 case SM_NEWMAIL_FILETYPE_MP3
:
602 $ret_end="</object>\n";
604 case SM_NEWMAIL_FILETYPE_OGG
:
605 case SM_NEWMAIL_FILETYPE_SVG
:
613 * Adds closing tags for object
615 * * This is internal function, use newmail_media_objects() instead
616 * @param integer $type media object type
617 * @return string closing tag of media object
619 function newmail_media_object_close($type) {
622 case SM_NEWMAIL_FILETYPE_SWF
:
623 case SM_NEWMAIL_FILETYPE_WAV
:
624 case SM_NEWMAIL_FILETYPE_OGG
:
625 case SM_NEWMAIL_FILETYPE_MP3
:
626 case SM_NEWMAIL_FILETYPE_SVG
:
627 $ret_end="</object>\n";
636 * Adds closing tags for object
638 * * This is internal function, use newmail_media_objects() instead
639 * @param integer $type media object type
640 * @return string closing tag of media object
642 function newmail_media_embed_close($type) {
645 case SM_NEWMAIL_FILETYPE_SWF
:
646 case SM_NEWMAIL_FILETYPE_WAV
:
647 case SM_NEWMAIL_FILETYPE_OGG
:
648 case SM_NEWMAIL_FILETYPE_MP3
:
649 case SM_NEWMAIL_FILETYPE_SVG
:
650 $ret_end="</embed>\n";
659 * Converts media attributes to string
661 * * attribute values are automatically sanitized by htmlspecialchars()
662 * * This is internal function, use newmail_media_objects() instead
663 * @param array $args array with object attributes
664 * @return string string with object attributes
666 function newmail_media_prepare_args($args) {
668 foreach ($args as $arg => $value) {
669 $ret_args.= $arg . '="' . htmlspecialchars($value) . '" ';
675 * Detects used media type and creates all need tags
676 * @param string $newmail_media
677 * @return string html tags with media objects
679 function newmail_create_media_tags($newmail_media) {
680 global $newmail_mmedia, $newmail_userfile_type;
682 if (preg_match("/^mmedia_+/",$newmail_media)) {
683 $ret_media = "<!-- newmail mmedia option -->\n";
685 $newmail_mmedia_short=preg_replace("/^mmedia_/",'',$newmail_media);
686 // check if media option is not removed
687 if (isset($newmail_mmedia[$newmail_mmedia_short])) {
688 $ret_media.= newmail_media_objects($newmail_mmedia_short,
689 $newmail_mmedia[$newmail_mmedia_short]['types'],
690 sqm_baseuri() . 'plugins/newmail/media/',
691 $newmail_mmedia[$newmail_mmedia_short]['args']);
693 $ret_media.= "<!-- end of newmail mmedia option -->\n";
694 } elseif ($newmail_media=='(userfile)') {
695 $ret_media = "<!-- newmail usermedia option -->\n";
696 $ret_media.= newmail_media_objects('loadfile.php',
697 array($newmail_userfile_type),
698 sqm_baseuri() . 'plugins/newmail/',
699 array('width'=>0,'height'=>0),
701 $ret_media.= "<!-- end of newmail usermedia option -->\n";
703 $ret_media = "<!-- newmail sounds from sounds/*.wav -->\n";
704 $ret_media.= newmail_media_objects(basename($newmail_media),
705 array(SM_NEWMAIL_FILETYPE_WAV
),
706 sqm_baseuri() . 'plugins/newmail/sounds/',
707 array('width'=>0,'height'=>0),
709 $ret_media.= "<!-- end of newmail sounds from sounds/*.wav -->\n";