3 * SquirrelMail NewMail plugin
12 if (! defined('SM_PATH')) define('SM_PATH','../../');
15 * sqm_baseuri() function for setups that don't load it by default
17 include_once(SM_PATH
. 'functions/display_messages.php');
19 /** file type defines */
20 define('SM_NEWMAIL_FILETYPE_WAV',2);
21 define('SM_NEWMAIL_FILETYPE_MP3',3);
22 define('SM_NEWMAIL_FILETYPE_OGG',4);
23 define('SM_NEWMAIL_FILETYPE_SWF',5);
24 define('SM_NEWMAIL_FILETYPE_SVG',6);
26 /** load default config */
27 if (file_exists(SM_PATH
. 'plugins/newmail/config_default.php')) {
28 include_once(SM_PATH
. 'plugins/newmail/config_default.php');
32 if (file_exists(SM_PATH
. 'config/newmail_config.php')) {
33 include_once(SM_PATH
. 'config/newmail_config.php');
34 } elseif (file_exists(SM_PATH
. 'plugins/newmail/config.php')) {
35 include_once(SM_PATH
. 'plugins/newmail/config.php');
38 // ----- hooked functions -----
41 * Register newmail option block
43 function newmail_optpage_register_block_function() {
44 // Gets added to the user's OPTIONS page.
45 global $optpage_blocks;
47 /* Register Squirrelspell with the $optionpages array. */
48 $optpage_blocks[] = array(
49 'name' => _("NewMail Options"),
50 'url' => sqm_baseuri() . 'plugins/newmail/newmail_opt.php',
51 'desc' => _("This configures settings for playing sounds and/or showing popup windows when new mail arrives."),
57 * Save newmail plugin settings
59 function newmail_sav_function() {
60 global $data_dir, $username, $_FILES;
62 if ( sqgetGlobalVar('submit_newmail', $submit, SQ_POST
) ) {
67 $media_changetitle = '';
72 sqgetGlobalVar('media_enable', $media_enable, SQ_POST
);
73 sqgetGlobalVar('media_popup', $media_popup, SQ_POST
);
74 sqgetGlobalVar('media_allbox', $media_allbox, SQ_POST
);
75 sqgetGlobalVar('media_recent', $media_recent, SQ_POST
);
76 sqgetGlobalVar('media_changetitle', $media_changetitle, SQ_POST
);
77 sqgetGlobalVar('popup_width', $popup_width, SQ_POST
);
78 sqgetGlobalVar('popup_height', $popup_height, SQ_POST
);
80 // sanitize height and width
81 $popup_width = (int) $popup_width;
82 if ($popup_width<=0) $popup_width=200;
83 $popup_height = (int) $popup_height;
84 if ($popup_height<=0) $popup_height=130;
86 setPref($data_dir,$username,'newmail_enable',$media_enable);
87 setPref($data_dir,$username,'newmail_popup', $media_popup);
88 setPref($data_dir,$username,'newmail_allbox',$media_allbox);
89 setPref($data_dir,$username,'newmail_recent',$media_recent);
90 setPref($data_dir,$username,'newmail_changetitle',$media_changetitle);
91 setPref($data_dir,$username,'newmail_popup_width',$popup_width);
92 setPref($data_dir,$username,'newmail_popup_height',$popup_height);
94 if( sqgetGlobalVar('media_sel', $media_sel, SQ_POST
) &&
95 $media_sel == '(none)' ) {
96 removePref($data_dir,$username,'newmail_media');
98 setPref($data_dir,$username,'newmail_media',$media_sel);
101 // process uploaded file
102 if (isset($_FILES['media_file']['tmp_name']) && $_FILES['media_file']['tmp_name']!='') {
103 // set temp file and get media file name
104 $newmail_tempmedia=getHashedDir($username, $data_dir) . "/$username.tempsound";
105 $newmail_mediafile=getHashedFile($username, $data_dir, $username . '.sound');
106 if (move_uploaded_file($_FILES['media_file']['tmp_name'], $newmail_tempmedia)) {
107 // new media file is in $newmail_tempmedia
108 if (file_exists($newmail_mediafile)) unlink($newmail_mediafile);
109 if (! rename($newmail_tempmedia,$newmail_mediafile)) {
110 // remove (userfile), if file rename fails
111 removePref($data_dir,$username,'newmail_media');
114 if (isset($_FILES['media_file']['type']) && isset($_FILES['media_file']['name'])) {
115 setPref($data_dir,$username,'newmail_userfile_type',
116 newmail_get_mediatype($_FILES['media_file']['type'],$_FILES['media_file']['name']));
118 removePref($data_dir,$username,'newmail_userfile_type');
121 if (isset($_FILES['media_file']['name'])) {
122 setPref($data_dir,$username,'newmail_userfile_name',basename($_FILES['media_file']['name']));
124 setPref($data_dir,$username,'newmail_userfile_name','mediafile.unknown');
133 * Load newmail plugin settings
135 function newmail_pref_function() {
136 global $username,$data_dir;
137 global $newmail_media,$newmail_enable,$newmail_popup,$newmail_allbox;
138 global $newmail_recent, $newmail_changetitle;
139 global $newmail_userfile_type, $newmail_userfile_name;
140 global $newmail_popup_width, $newmail_popup_height;
142 $newmail_recent = getPref($data_dir,$username,'newmail_recent');
143 $newmail_enable = getPref($data_dir,$username,'newmail_enable');
144 $newmail_media = getPref($data_dir, $username, 'newmail_media', '(none)');
145 $newmail_popup = getPref($data_dir, $username, 'newmail_popup');
146 $newmail_popup_width = getPref($data_dir, $username, 'newmail_popup_width',200);
147 $newmail_popup_height = getPref($data_dir, $username, 'newmail_popup_height',130);
148 $newmail_allbox = getPref($data_dir, $username, 'newmail_allbox');
149 $newmail_changetitle = getPref($data_dir, $username, 'newmail_changetitle');
151 $newmail_userfile_type = getPref($data_dir, $username, 'newmail_userfile_type');
152 $newmail_userfile_name = getPref($data_dir,$username,'newmail_userfile_name','');
158 * Used by option page when saving settings.
160 function newmail_set_loadinfo_function() {
161 global $optpage, $optpage_name;
162 if ($optpage=='newmail') {
163 $optpage_name=_("NewMail Options");
168 * Insert needed data in left_main
170 function newmail_plugin_function() {
171 global $username, $newmail_media, $newmail_enable, $newmail_popup,
172 $newmail_recent, $newmail_changetitle, $imapConnection, $PHP_SELF;
173 global $newmail_mmedia, $newmail_allowsound;
174 global $newmail_userfile_type;
175 global $newmail_popup_width, $newmail_popup_height;
177 if ($newmail_enable == 'on' ||
178 $newmail_popup == 'on' ||
179 $newmail_changetitle) {
181 // open a connection on the imap port (143)
183 $boxes = sqimap_mailbox_list($imapConnection);
184 $delimeter = sqimap_get_delimiter($imapConnection);
189 for ($i = 0;$i < count($boxes); $i++
) {
191 $mailbox = $boxes[$i]['formatted'];
193 if (! isset($boxes[$i]['unseen'])) {
194 $boxes[$i]['unseen'] = '';
196 if ($boxes[$i]['flags']) {
198 for ($h = 0; $h < count($boxes[$i]['flags']); $h++
) {
199 if (strtolower($boxes[$i]["flags"][$h]) == 'noselect') {
204 $status +
= CheckNewMailboxSound($imapConnection,
206 $boxes[$i]['unformatted'],
208 $boxes[$i]['unseen'],
212 $status +
= CheckNewMailboxSound($imapConnection,
214 $boxes[$i]['unformatted'],
216 $boxes[$i]['unseen'],
221 // sqimap_logout($imapConnection);
223 // If we found unseen messages, then we
224 // will play the sound as follows:
226 if ($newmail_changetitle) {
227 echo "<script language=\"javascript\" type=\"text/javascript\">\n" .
228 "function ChangeTitleLoad() {\n";
229 echo 'window.parent.document.title = "' .
230 sprintf(ngettext("%s New Message","%s New Messages",$totalNew), $totalNew) .
232 echo "if (BeforeChangeTitle != null)\n".
233 "BeforeChangeTitle();\n".
235 "BeforeChangeTitle = window.onload;\n".
236 "window.onload = ChangeTitleLoad;\n".
240 // create media output if there are new email messages
241 if ($newmail_allowsound && $totalNew > 0 && $newmail_enable == 'on' && $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 language=\"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 -----
269 * Checks if mailbox contains new messages.
271 * @param stream $imapConnection
272 * @param string $mailbox FIXME: option is not used
273 * @param string $real_box unformated mailbox name
274 * @param string $delimeter FIXME: option is not used
275 * @param string $unseen FIXME: option is not used
276 * @param integer $total_new number of new messages
277 * @return bool true, if there are new messages
279 function CheckNewMailboxSound($imapConnection, $mailbox, $real_box, $delimeter, $unseen, &$total_new) {
280 global $trash_folder, $sent_folder,
281 $unseen_notify, $newmail_allbox,
284 $mailboxURL = urlencode($real_box);
286 // Skip folders for Sent and Trash
287 if ($real_box == $sent_folder ||
288 $real_box == $trash_folder) {
292 if (($unseen_notify == 2 && $real_box == 'INBOX') ||
293 ($unseen_notify == 3 && ($newmail_allbox == 'on' ||
294 $real_box == 'INBOX'))) {
295 $status = sqimap_status_messages( $imapConnection, $real_box);
296 if($newmail_recent == 'on') {
297 $total_new +
= $status['RECENT'];
299 $total_new +
= $status['UNSEEN'];
309 * Function tries to detect if file contents match declared file type
311 * Function returns default extension for detected mime type or 'false'
313 * TODO: use $contents to check if file is in specified type
314 * @param string $contents file contents
315 * @param string $type file mime type
318 function newmail_detect_filetype($contents,$type) {
319 // convert $type to lower case
320 $type=strtolower($type);
331 case 'application/ogg':
334 case 'application/x-shockwave-flash':
337 case 'image/svg+xml':
347 * Function tries to detect uploaded file type
348 * @param string $type
349 * @param string $filename
350 * @return integer One of SM_NEWMAIL_FILETYPE_* defines or false.
352 function newmail_get_mediatype($type,$filename) {
354 // fix for browser's that upload file as application/octet-stream
355 case 'application/octet-stream':
356 $ret=newmail_get_mediatype_by_ext($filename);
359 $ret=SM_NEWMAIL_FILETYPE_WAV
;
362 $ret=SM_NEWMAIL_FILETYPE_MP3
;
364 case 'application/ogg':
365 $ret=SM_NEWMAIL_FILETYPE_OGG
;
367 case 'application/x-shockwave-flash':
368 $ret=SM_NEWMAIL_FILETYPE_SWF
;
370 case 'image/svg+xml':
371 $ret=SM_NEWMAIL_FILETYPE_SVG
;
380 * Function provides filetype detection for browsers, that
381 * upload files with application/octet-stream file type.
382 * Ex. some version of Opera.
383 * @param string $filename
384 * @return integer One of SM_NEWMAIL_FILETYPE_* defines or false.
386 function newmail_get_mediatype_by_ext($filename) {
387 if (preg_match("/\.wav$/i",$filename)) return SM_NEWMAIL_FILETYPE_WAV
;
388 if (preg_match("/\.mp3$/i",$filename)) return SM_NEWMAIL_FILETYPE_MP3
;
389 if (preg_match("/\.ogg$/i",$filename)) return SM_NEWMAIL_FILETYPE_OGG
;
390 if (preg_match("/\.swf$/i",$filename)) return SM_NEWMAIL_FILETYPE_SWF
;
391 if (preg_match("/\.svg$/i",$filename)) return SM_NEWMAIL_FILETYPE_SVG
;
396 * Creates html object tags of multimedia object
398 * Main function that creates multimedia object tags
399 * @param string $object object name
400 * @param integer $type media object type
401 * @param string $path URL to media object
402 * @param array $args media object attributes
403 * @param string $extra tags that have to buried deep inside object tags
404 * @param bool $addsuffix controls addition of suffix to media object url
405 * @return string object html tags and attributes required by selected media type.
407 function newmail_media_objects($object,$types,$path,$args=array(),$extra='',$addsuffix=true) {
408 global $newmail_mediacompat_mode;
410 // first prepare single object for IE
411 $ret = newmail_media_object_ie($object,$types[0],$path,$args,$addsuffix);
413 // W3.org nested objects
414 $ret.= "<!--[if !IE]> <-->\n"; // not for IE
416 foreach ($types as $type) {
417 $ret.= newmail_media_object($object,$type,$path,$args,$addsuffix);
420 if (isset($newmail_mediacompat_mode) && $newmail_mediacompat_mode)
421 $ret.= newmail_media_embed($object,$types[0],$path,$args,$addsuffix);
422 // add $extra code inside objects
426 if (isset($newmail_mediacompat_mode) && $newmail_mediacompat_mode)
427 $ret.= newmail_media_embed_close($types[0]);
429 foreach (array_reverse($types) as $type) {
430 $ret.= newmail_media_object_close($type);
432 $ret.= "<!--> <![endif]-->\n"; // end non-IE mode
434 $ret.= newmail_media_object_ie_close($types[0]);
440 * Creates object tags of multimedia object for browsers that comply to w3.org
445 * <li>Returned string does not contain html closing tag.
446 * <li>This is internal function, use newmail_media_objects() instead
448 * @link http://www.w3.org/TR/html4/struct/objects.html#edef-OBJECT W3.org specs
449 * @param string $object object name
450 * @param integer $type media object type
451 * @param string $path URL to media object
452 * @param array $args media object attributes
453 * @param bool $addsuffix controls addition of suffix to media object url
454 * @return string object html tags and attributes required by selected media type.
456 function newmail_media_object($object,$type,$path,$args=array(),$addsuffix=true) {
459 $sArgs=newmail_media_prepare_args($args);
462 case SM_NEWMAIL_FILETYPE_SWF
:
463 if ($addsuffix) $suffix='.swf';
464 $ret_w3 = '<object data="' . $path . $object . $suffix . '" '
466 .'type="application/x-shockwave-flash">' . "\n";
468 case SM_NEWMAIL_FILETYPE_WAV
:
469 if ($addsuffix) $suffix='.wav';
470 $ret_w3 = '<object data="' . $path . $object . $suffix . '" '
472 .'type="audio/x-wav">' . "\n";
474 case SM_NEWMAIL_FILETYPE_OGG
:
475 if ($addsuffix) $suffix='.ogg';
476 $ret_w3 = '<object data="' . $path . $object . $suffix . '" '
478 .'type="application/ogg">' . "\n";
480 case SM_NEWMAIL_FILETYPE_MP3
:
481 if ($addsuffix) $suffix='.mp3';
482 $ret_w3 = '<object data="' . $path . $object . $suffix . '" '
484 .'type="audio/mpeg">' . "\n";
486 case SM_NEWMAIL_FILETYPE_SVG
:
487 if ($addsuffix) $suffix='.svg';
488 $ret_w3 = '<object data="' . $path . $object . $suffix . '" '
490 .'type="image/svg+xml">' . "\n";
499 * Creates multimedia object tags for Internet Explorer (Win32)
502 * * Returned string does not contain html closing tag, because
503 * this multimedia object can include other media objects.
504 * * This is internal function, use newmail_media_objects() instead
506 * @param string $object object name
507 * @param integer $type media object type
508 * @param string $path URL to media object
509 * @param array $args media object attributes
510 * @param bool $addsuffix controls addition of suffix to media object url
511 * @return string object html tags and attributes required by selected media type.
513 function newmail_media_object_ie($object,$type,$path,$args=array(),$addsuffix) {
516 $sArgs=newmail_media_prepare_args($args);
519 case SM_NEWMAIL_FILETYPE_SWF
:
520 if ($addsuffix) $suffix='.swf';
521 $ret_ie ='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '
522 .'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" '
523 . $sArgs . 'id="' . $object ."\">\n"
524 .'<param name="movie" value="' . $path . $object . $suffix . "\">\n"
525 .'<param name="hidden" value="true">' . "\n";
527 case SM_NEWMAIL_FILETYPE_WAV
:
528 if ($addsuffix) $suffix='.wav';
529 $ret_ie ='<object classid="clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95" '
530 .'codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,0902" '
531 . $sArgs . 'id="' . $object ."\" \n"
532 .'type="audio/x-wav">' ."\n"
533 .'<param name="FileName" value="' . $path . $object . $suffix . "\">\n";
535 case SM_NEWMAIL_FILETYPE_MP3
:
536 if ($addsuffix) $suffix='.mp3';
537 $ret_ie ='<object classid="clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95" '
538 .'codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,0902" '
539 . $sArgs . 'id="' . $object ."\" \n"
540 .'type="audio/mpeg">' ."\n"
541 .'<param name="FileName" value="' . $path . $object . $suffix . "\">\n";
543 case SM_NEWMAIL_FILETYPE_OGG
:
544 case SM_NEWMAIL_FILETYPE_SVG
:
552 * Creates embed tags of multimedia object
555 * Apple: http://www.apple.com/quicktime/authoring/embed.html
558 * * Returned string does not contain html closing tag.
559 * * embed tags will be created by newmail_media_objects() only
560 * when $newmail_mediacompat_mode option is enabled. Option is not
561 * enabled by default in order to comply to w3.org specs.
562 * * This is internal function, use newmail_media_objects() instead
563 * @link http://www.apple.com/quicktime/authoring/embed.html Info about embed tag
564 * @param string $object object name
565 * @param integer $type media object type
566 * @param string $path URL to media object
567 * @param array $args media object attributes
568 * @param bool $addsuffix controls addition of suffix to media object url
569 * @return string embed html tags and attributes required by selected media type.
571 function newmail_media_embed($object,$type,$path,$args=array(),$addsuffix=true) {
574 $sArgs=newmail_media_prepare_args($args);
577 case SM_NEWMAIL_FILETYPE_SWF
:
578 if ($addsuffix) $suffix='.swf';
579 $ret_embed='<embed src="' . $path . $object . $suffix . '" '. "\n"
580 .'hidden="true" autostart="true" '. "\n"
582 .'name="' . $object .'" ' . "\n"
583 .'type="application/x-shockwave-flash" ' . "\n"
584 .'pluginspage="http://www.macromedia.com/go/getflashplayer">' . "\n";
586 case SM_NEWMAIL_FILETYPE_WAV
:
587 if ($addsuffix) $suffix='.wav';
588 $ret_embed='<embed src="' . $path . $object . $suffix . '" '. "\n"
589 .' hidden="true" autostart="true" '. "\n"
591 .' name="' . $object .'" ' . "\n"
592 .' type="audio/x-wav">' . "\n";
594 case SM_NEWMAIL_FILETYPE_OGG
:
595 case SM_NEWMAIL_FILETYPE_MP3
:
596 case SM_NEWMAIL_FILETYPE_SVG
:
604 * Adds closing tags for ie object
606 * * This is internal function, use newmail_media_objects() instead
607 * @param integer $type media object type
608 * @return string closing tag of media object
610 function newmail_media_object_ie_close($type) {
613 case SM_NEWMAIL_FILETYPE_SWF
:
614 case SM_NEWMAIL_FILETYPE_WAV
:
615 case SM_NEWMAIL_FILETYPE_MP3
:
616 $ret_end="</object>\n";
618 case SM_NEWMAIL_FILETYPE_OGG
:
619 case SM_NEWMAIL_FILETYPE_SVG
:
627 * Adds closing tags for object
629 * * This is internal function, use newmail_media_objects() instead
630 * @param integer $type media object type
631 * @return string closing tag of media object
633 function newmail_media_object_close($type) {
636 case SM_NEWMAIL_FILETYPE_SWF
:
637 case SM_NEWMAIL_FILETYPE_WAV
:
638 case SM_NEWMAIL_FILETYPE_OGG
:
639 case SM_NEWMAIL_FILETYPE_MP3
:
640 case SM_NEWMAIL_FILETYPE_SVG
:
641 $ret_end="</object>\n";
650 * Adds closing tags for object
652 * * This is internal function, use newmail_media_objects() instead
653 * @param integer $type media object type
654 * @return string closing tag of media object
656 function newmail_media_embed_close($type) {
659 case SM_NEWMAIL_FILETYPE_SWF
:
660 case SM_NEWMAIL_FILETYPE_WAV
:
661 $ret_end="</embed>\n";
663 case SM_NEWMAIL_FILETYPE_OGG
:
664 case SM_NEWMAIL_FILETYPE_MP3
:
665 case SM_NEWMAIL_FILETYPE_SVG
:
673 * Converts media attributes to string
675 * * attribute values are automatically sanitized by htmlspecialchars()
676 * * This is internal function, use newmail_media_objects() instead
677 * @param array $args array with object attributes
678 * @return string string with object attributes
680 function newmail_media_prepare_args($args) {
682 foreach ($args as $arg => $value) {
683 $ret_args.= $arg . '="' . htmlspecialchars($value) . '" ';
689 * Detects used media type and creates all need tags
690 * @param string $newmail_media
691 * @return string html tags with media objects
693 function newmail_create_media_tags($newmail_media) {
694 global $newmail_mmedia, $newmail_userfile_type;
696 if (preg_match("/^mmedia_+/",$newmail_media)) {
697 $ret_media = "<!-- newmail mmedia option -->\n";
699 $newmail_mmedia_short=preg_replace("/^mmedia_/",'',$newmail_media);
700 // check if media option is not removed
701 if (isset($newmail_mmedia[$newmail_mmedia_short])) {
702 $ret_media.= newmail_media_objects($newmail_mmedia_short,
703 $newmail_mmedia[$newmail_mmedia_short]['types'],
704 sqm_baseuri() . 'plugins/newmail/media/',
705 $newmail_mmedia[$newmail_mmedia_short]['args']);
707 $ret_media.= "<!-- end of newmail mmedia option -->\n";
708 } elseif ($newmail_media=='(userfile)') {
709 $ret_media = "<!-- newmail usermedia option -->\n";
710 $ret_media.= newmail_media_objects('loadfile.php',
711 array($newmail_userfile_type),
712 sqm_baseuri() . 'plugins/newmail/',
713 array('width'=>0,'height'=>0),
715 $ret_media.= "<!-- end of newmail usermedia option -->\n";
717 $ret_media = "<!-- newmail sounds from sounds/*.wav -->\n";
718 $ret_media.= newmail_media_objects(basename($newmail_media),
719 array(SM_NEWMAIL_FILETYPE_WAV
),
720 sqm_baseuri() . 'plugins/newmail/sounds/',
721 array('width'=>0,'height'=>0),
723 $ret_media.= "<!-- end of newmail sounds from sounds/*.wav -->\n";