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 $sent_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'];
184 if ($newmail_media_enable == 'on' ||
185 $newmail_popup == 'on' ||
186 $newmail_changetitle == 'on') {
188 // Skip folders for Sent and Trash
189 if ($statusarr['MAILBOX'] == $sent_folder ||
$statusarr['MAILBOX'] == $trash_folder) {
193 if ((($mailbox == 'INBOX') && ($unseen_notify == 2)) ||
($unseen_notify == 3)) {
194 if (($newmail_recent == 'on') && (!empty($statusarr['RECENT']))) {
195 $totalNewArr[$mailbox] = $statusarr['RECENT'];
196 } elseif ($newmail_recent != 'on' && !empty($statusarr['UNSEEN'])) {
197 $totalNewArr[$mailbox] = $statusarr['UNSEEN'];
204 * Insert needed data in left_main
206 function newmail_plugin_function() {
207 global $username, $newmail_media, $newmail_media_enable, $newmail_popup,
208 $newmail_recent, $newmail_changetitle, $imapConnection, $PHP_SELF;
209 global $newmail_mmedia, $newmail_allowsound;
210 global $newmail_userfile_type;
211 global $newmail_popup_width, $newmail_popup_height;
214 if ($newmail_media_enable == 'on' ||
215 $newmail_popup == 'on' ||
216 $newmail_changetitle) {
218 if (!empty($totalNewArr)) { $totalNew=array_sum($totalNewArr); }
219 else { $totalNew=0; }
221 // If we found unseen messages, then we
222 // will play the sound as follows:
224 if ($newmail_changetitle) {
225 echo "<script type=\"text/javascript\">\n" .
226 "function ChangeTitleLoad() {\n";
227 echo 'window.parent.document.title = "' .
228 sprintf(ngettext("%s New Message","%s New Messages",$totalNew), $totalNew) .
230 echo "if (BeforeChangeTitle != null)\n".
231 "BeforeChangeTitle();\n".
233 "BeforeChangeTitle = window.onload;\n".
234 "window.onload = ChangeTitleLoad;\n".
238 // create media output if there are new email messages
239 if ($newmail_allowsound && $totalNew > 0
240 && $newmail_media_enable == 'on'
241 && $newmail_media != '' ) {
242 echo newmail_create_media_tags($newmail_media);
245 if ($totalNew > 0 && $newmail_popup == 'on') {
246 // Idea by: Nic Wolfe (Nic@TimelapseProductions.com)
247 // Web URL: http://fineline.xs.mw
248 // More code from Tyler Akins
249 echo "<script type=\"text/javascript\">\n".
251 "function PopupScriptLoad() {\n".
252 'window.open("'.sqm_baseuri().'plugins/newmail/newmail.php?numnew='.$totalNew.
254 "\"width=$newmail_popup_width,height=$newmail_popup_height,scrollbars=no\");\n".
255 "if (BeforePopupScript != null)\n".
256 "BeforePopupScript();\n".
258 "BeforePopupScript = window.onload;\n".
259 "window.onload = PopupScriptLoad;\n".
266 // ----- end of hooked functions -----
271 * Function tries to detect if file contents match declared file type
273 * Function returns default extension for detected mime type or 'false'
275 * TODO: use $contents to check if file is in specified type
276 * @param string $contents file contents
277 * @param string $type file mime type
280 function newmail_detect_filetype($contents,$type) {
281 // convert $type to lower case
282 $type=strtolower($type);
293 case 'application/ogg':
296 case 'application/x-shockwave-flash':
299 case 'image/svg+xml':
309 * Function tries to detect uploaded file type
310 * @param string $type
311 * @param string $filename
312 * @return integer One of SM_NEWMAIL_FILETYPE_* defines or false.
314 function newmail_get_mediatype($type,$filename) {
316 // fix for browser's that upload file as application/octet-stream
317 case 'application/octet-stream':
318 $ret=newmail_get_mediatype_by_ext($filename);
321 $ret=SM_NEWMAIL_FILETYPE_WAV
;
324 $ret=SM_NEWMAIL_FILETYPE_MP3
;
326 case 'application/ogg':
327 $ret=SM_NEWMAIL_FILETYPE_OGG
;
329 case 'application/x-shockwave-flash':
330 $ret=SM_NEWMAIL_FILETYPE_SWF
;
332 case 'image/svg+xml':
333 $ret=SM_NEWMAIL_FILETYPE_SVG
;
342 * Function provides filetype detection for browsers, that
343 * upload files with application/octet-stream file type.
344 * Ex. some version of Opera.
345 * @param string $filename
346 * @return integer One of SM_NEWMAIL_FILETYPE_* defines or false.
348 function newmail_get_mediatype_by_ext($filename) {
349 if (preg_match("/\.wav$/i",$filename)) return SM_NEWMAIL_FILETYPE_WAV
;
350 if (preg_match("/\.mp3$/i",$filename)) return SM_NEWMAIL_FILETYPE_MP3
;
351 if (preg_match("/\.ogg$/i",$filename)) return SM_NEWMAIL_FILETYPE_OGG
;
352 if (preg_match("/\.swf$/i",$filename)) return SM_NEWMAIL_FILETYPE_SWF
;
353 if (preg_match("/\.svg$/i",$filename)) return SM_NEWMAIL_FILETYPE_SVG
;
358 * Creates html object tags of multimedia object
360 * Main function that creates multimedia object tags
361 * @param string $object object name
362 * @param integer $type media object type
363 * @param string $path URL to media object
364 * @param array $args media object attributes
365 * @param string $extra tags that have to buried deep inside object tags
366 * @param bool $addsuffix controls addition of suffix to media object url
367 * @return string object html tags and attributes required by selected media type.
369 function newmail_media_objects($object,$types,$path,$args=array(),$extra='',$addsuffix=true) {
370 global $newmail_mediacompat_mode;
372 // first prepare single object for IE
373 $ret = newmail_media_object_ie($object,$types[0],$path,$args,$addsuffix);
375 // W3.org nested objects
376 $ret.= "<!--[if !IE]> <-->\n"; // not for IE
378 foreach ($types as $type) {
379 $ret.= newmail_media_object($object,$type,$path,$args,$addsuffix);
382 if (isset($newmail_mediacompat_mode) && $newmail_mediacompat_mode)
383 $ret.= newmail_media_embed($object,$types[0],$path,$args,$addsuffix);
384 // add $extra code inside objects
389 if (isset($newmail_mediacompat_mode) && $newmail_mediacompat_mode)
390 $ret.= newmail_media_embed_close($types[0]);
392 // close w3.org nested objects
393 foreach (array_reverse($types) as $type) {
394 $ret.= newmail_media_object_close($type);
396 $ret.= "<!--> <![endif]-->\n"; // end non-IE mode
398 $ret.= newmail_media_object_ie_close($types[0]);
404 * Creates object tags of multimedia object for browsers that comply to w3.org
409 * <li>Returned string does not contain html closing tag.
410 * <li>This is internal function, use newmail_media_objects() instead
412 * @link http://www.w3.org/TR/html4/struct/objects.html#edef-OBJECT W3.org specs
413 * @param string $object object name
414 * @param integer $type media object type
415 * @param string $path URL to media object
416 * @param array $args media object attributes
417 * @param bool $addsuffix controls addition of suffix to media object url
418 * @return string object html tags and attributes required by selected media type.
420 function newmail_media_object($object,$type,$path,$args=array(),$addsuffix=true) {
423 $sArgs=newmail_media_prepare_args($args);
426 case SM_NEWMAIL_FILETYPE_SWF
:
427 if ($addsuffix) $suffix='.swf';
428 $ret_w3 = '<object data="' . $path . $object . $suffix . '" '
430 .'type="application/x-shockwave-flash">' . "\n";
432 case SM_NEWMAIL_FILETYPE_WAV
:
433 if ($addsuffix) $suffix='.wav';
434 $ret_w3 = '<object data="' . $path . $object . $suffix . '" '
436 .'type="audio/x-wav">' . "\n";
438 case SM_NEWMAIL_FILETYPE_OGG
:
439 if ($addsuffix) $suffix='.ogg';
440 $ret_w3 = '<object data="' . $path . $object . $suffix . '" '
442 .'type="application/ogg">' . "\n";
444 case SM_NEWMAIL_FILETYPE_MP3
:
445 if ($addsuffix) $suffix='.mp3';
446 $ret_w3 = '<object data="' . $path . $object . $suffix . '" '
448 .'type="audio/mpeg">' . "\n";
450 case SM_NEWMAIL_FILETYPE_SVG
:
451 if ($addsuffix) $suffix='.svg';
452 $ret_w3 = '<object data="' . $path . $object . $suffix . '" '
454 .'type="image/svg+xml">' . "\n";
463 * Creates multimedia object tags for Internet Explorer (Win32)
466 * * Returned string does not contain html closing tag, because
467 * this multimedia object can include other media objects.
468 * * This is internal function, use newmail_media_objects() instead
470 * @param string $object object name
471 * @param integer $type media object type
472 * @param string $path URL to media object
473 * @param array $args media object attributes
474 * @param bool $addsuffix controls addition of suffix to media object url
475 * @return string object html tags and attributes required by selected media type.
476 * @todo add ogg and svg support
478 function newmail_media_object_ie($object,$type,$path,$args=array(),$addsuffix) {
481 $sArgs=newmail_media_prepare_args($args);
484 case SM_NEWMAIL_FILETYPE_SWF
:
485 if ($addsuffix) $suffix='.swf';
486 $ret_ie ='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '
487 .'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" '
488 . $sArgs . 'id="' . $object ."\">\n"
489 .'<param name="movie" value="' . $path . $object . $suffix . "\">\n"
490 .'<param name="hidden" value="true">' . "\n";
492 case SM_NEWMAIL_FILETYPE_WAV
:
493 if ($addsuffix) $suffix='.wav';
494 $ret_ie ='<object classid="clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95" '
495 .'codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,0902" '
496 . $sArgs . 'id="' . $object ."\" \n"
497 .'type="audio/x-wav">' ."\n"
498 .'<param name="FileName" value="' . $path . $object . $suffix . "\">\n";
500 case SM_NEWMAIL_FILETYPE_MP3
:
501 if ($addsuffix) $suffix='.mp3';
502 $ret_ie ='<object classid="clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95" '
503 .'codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,0902" '
504 . $sArgs . 'id="' . $object ."\" \n"
505 .'type="audio/mpeg">' ."\n"
506 .'<param name="FileName" value="' . $path . $object . $suffix . "\">\n";
508 case SM_NEWMAIL_FILETYPE_OGG
:
509 case SM_NEWMAIL_FILETYPE_SVG
:
517 * Creates embed tags of multimedia object
520 * Apple: http://www.apple.com/quicktime/authoring/embed.html
523 * * Returned string does not contain html closing tag.
524 * * embed tags will be created by newmail_media_objects() only
525 * when $newmail_mediacompat_mode option is enabled. Option is not
526 * enabled by default in order to comply to w3.org specs.
527 * * This is internal function, use newmail_media_objects() instead
528 * @link http://www.apple.com/quicktime/authoring/embed.html Info about embed tag
529 * @param string $object object name
530 * @param integer $type media object type
531 * @param string $path URL to media object
532 * @param array $args media object attributes
533 * @param bool $addsuffix controls addition of suffix to media object url
534 * @return string embed html tags and attributes required by selected media type.
536 function newmail_media_embed($object,$type,$path,$args=array(),$addsuffix=true) {
539 $sArgs=newmail_media_prepare_args($args);
542 case SM_NEWMAIL_FILETYPE_SWF
:
543 if ($addsuffix) $suffix='.swf';
544 $ret_embed='<embed src="' . $path . $object . $suffix . '" '. "\n"
545 .'hidden="true" autostart="true" '. "\n"
547 .'name="' . $object .'" ' . "\n"
548 .'type="application/x-shockwave-flash" ' . "\n"
549 .'pluginspage="http://www.macromedia.com/go/getflashplayer">' . "\n";
551 case SM_NEWMAIL_FILETYPE_WAV
:
552 if ($addsuffix) $suffix='.wav';
553 $ret_embed='<embed src="' . $path . $object . $suffix . '" '. "\n"
554 .' hidden="true" autostart="true" '. "\n"
556 .' name="' . $object .'" ' . "\n"
557 .' type="audio/x-wav">' . "\n";
559 case SM_NEWMAIL_FILETYPE_SVG
:
560 if ($addsuffix) $suffix='.svg';
561 $ret_embed='<embed src="' . $path . $object . $suffix . '" '. "\n"
562 .'hidden="true" autostart="true" '. "\n"
564 .'name="' . $object .'" ' . "\n"
565 .'type="image/svg-xml" ' . "\n"
566 .'pluginspage="http://www.adobe.com/svg/viewer/install/">' . "\n";
568 case SM_NEWMAIL_FILETYPE_OGG
:
569 if ($addsuffix) $suffix='.ogg';
570 $ret_embed='<embed src="' . $path . $object . $suffix . '" '. "\n"
571 .' hidden="true" autostart="true" '. "\n"
573 .' name="' . $object .'" ' . "\n"
574 .' type="application/ogg">' . "\n";
576 case SM_NEWMAIL_FILETYPE_MP3
:
577 if ($addsuffix) $suffix='.mp3';
578 $ret_embed='<embed src="' . $path . $object . $suffix . '" '. "\n"
579 .' hidden="true" autostart="true" '. "\n"
581 .' name="' . $object .'" ' . "\n"
582 .' type="audio/mpeg">' . "\n";
591 * Adds closing tags for ie object
593 * * This is internal function, use newmail_media_objects() instead
594 * @param integer $type media object type
595 * @return string closing tag of media object
597 function newmail_media_object_ie_close($type) {
600 case SM_NEWMAIL_FILETYPE_SWF
:
601 case SM_NEWMAIL_FILETYPE_WAV
:
602 case SM_NEWMAIL_FILETYPE_MP3
:
603 $ret_end="</object>\n";
605 case SM_NEWMAIL_FILETYPE_OGG
:
606 case SM_NEWMAIL_FILETYPE_SVG
:
614 * Adds closing tags for object
616 * * This is internal function, use newmail_media_objects() instead
617 * @param integer $type media object type
618 * @return string closing tag of media object
620 function newmail_media_object_close($type) {
623 case SM_NEWMAIL_FILETYPE_SWF
:
624 case SM_NEWMAIL_FILETYPE_WAV
:
625 case SM_NEWMAIL_FILETYPE_OGG
:
626 case SM_NEWMAIL_FILETYPE_MP3
:
627 case SM_NEWMAIL_FILETYPE_SVG
:
628 $ret_end="</object>\n";
637 * Adds closing tags for object
639 * * This is internal function, use newmail_media_objects() instead
640 * @param integer $type media object type
641 * @return string closing tag of media object
643 function newmail_media_embed_close($type) {
646 case SM_NEWMAIL_FILETYPE_SWF
:
647 case SM_NEWMAIL_FILETYPE_WAV
:
648 case SM_NEWMAIL_FILETYPE_OGG
:
649 case SM_NEWMAIL_FILETYPE_MP3
:
650 case SM_NEWMAIL_FILETYPE_SVG
:
651 $ret_end="</embed>\n";
660 * Converts media attributes to string
662 * * attribute values are automatically sanitized by htmlspecialchars()
663 * * This is internal function, use newmail_media_objects() instead
664 * @param array $args array with object attributes
665 * @return string string with object attributes
667 function newmail_media_prepare_args($args) {
669 foreach ($args as $arg => $value) {
670 $ret_args.= $arg . '="' . htmlspecialchars($value) . '" ';
676 * Detects used media type and creates all need tags
677 * @param string $newmail_media
678 * @return string html tags with media objects
680 function newmail_create_media_tags($newmail_media) {
681 global $newmail_mmedia, $newmail_userfile_type;
683 if (preg_match("/^mmedia_+/",$newmail_media)) {
684 $ret_media = "<!-- newmail mmedia option -->\n";
686 $newmail_mmedia_short=preg_replace("/^mmedia_/",'',$newmail_media);
687 // check if media option is not removed
688 if (isset($newmail_mmedia[$newmail_mmedia_short])) {
689 $ret_media.= newmail_media_objects($newmail_mmedia_short,
690 $newmail_mmedia[$newmail_mmedia_short]['types'],
691 sqm_baseuri() . 'plugins/newmail/media/',
692 $newmail_mmedia[$newmail_mmedia_short]['args']);
694 $ret_media.= "<!-- end of newmail mmedia option -->\n";
695 } elseif ($newmail_media=='(userfile)') {
696 $ret_media = "<!-- newmail usermedia option -->\n";
697 $ret_media.= newmail_media_objects('loadfile.php',
698 array($newmail_userfile_type),
699 sqm_baseuri() . 'plugins/newmail/',
700 array('width'=>0,'height'=>0),
702 $ret_media.= "<!-- end of newmail usermedia option -->\n";
704 $ret_media = "<!-- newmail sounds from sounds/*.wav -->\n";
705 $ret_media.= newmail_media_objects(basename($newmail_media),
706 array(SM_NEWMAIL_FILETYPE_WAV
),
707 sqm_baseuri() . 'plugins/newmail/sounds/',
708 array('width'=>0,'height'=>0),
710 $ret_media.= "<!-- end of newmail sounds from sounds/*.wav -->\n";