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 if ( checkForJavascript() ) {
48 /* Register Squirrelspell with the $optionpages array. */
49 $optpage_blocks[] = array(
50 'name' => _("NewMail Options"),
51 'url' => sqm_baseuri() . 'plugins/newmail/newmail_opt.php',
52 'desc' => _("This configures settings for playing sounds and/or showing popup windows when new mail arrives."),
59 * Save newmail plugin settings
61 function newmail_sav_function() {
62 global $data_dir, $username, $_FILES;
64 if ( sqgetGlobalVar('submit_newmail', $submit, SQ_POST
) ) {
69 $media_changetitle = '';
74 sqgetGlobalVar('media_enable', $media_enable, SQ_POST
);
75 sqgetGlobalVar('media_popup', $media_popup, SQ_POST
);
76 sqgetGlobalVar('media_allbox', $media_allbox, SQ_POST
);
77 sqgetGlobalVar('media_recent', $media_recent, SQ_POST
);
78 sqgetGlobalVar('media_changetitle', $media_changetitle, SQ_POST
);
79 sqgetGlobalVar('popup_width', $popup_width, SQ_POST
);
80 sqgetGlobalVar('popup_height', $popup_height, SQ_POST
);
82 // sanitize height and width
83 $popup_width = (int) $popup_width;
84 if ($popup_width<=0) $popup_width=200;
85 $popup_height = (int) $popup_height;
86 if ($popup_height<=0) $popup_height=130;
88 setPref($data_dir,$username,'newmail_enable',$media_enable);
89 setPref($data_dir,$username,'newmail_popup', $media_popup);
90 setPref($data_dir,$username,'newmail_allbox',$media_allbox);
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 (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_enable,$newmail_popup,$newmail_allbox;
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_enable = getPref($data_dir,$username,'newmail_enable');
146 $newmail_media = getPref($data_dir, $username, 'newmail_media', '(none)');
147 $newmail_popup = getPref($data_dir, $username, 'newmail_popup');
148 $newmail_popup_width = getPref($data_dir, $username, 'newmail_popup_width',200);
149 $newmail_popup_height = getPref($data_dir, $username, 'newmail_popup_height',130);
150 $newmail_allbox = getPref($data_dir, $username, 'newmail_allbox');
151 $newmail_changetitle = getPref($data_dir, $username, 'newmail_changetitle');
153 $newmail_userfile_type = getPref($data_dir, $username, 'newmail_userfile_type');
154 $newmail_userfile_name = getPref($data_dir,$username,'newmail_userfile_name','');
160 * Used by option page when saving settings.
162 function newmail_set_loadinfo_function() {
163 global $optpage, $optpage_name;
164 if ($optpage=='newmail') {
165 $optpage_name=_("NewMail Options");
170 * Insert needed data in left_main
172 function newmail_plugin_function() {
173 global $username, $newmail_media, $newmail_enable, $newmail_popup,
174 $newmail_recent, $newmail_changetitle, $imapConnection, $PHP_SELF;
175 global $newmail_mmedia, $newmail_allowsound;
176 global $newmail_userfile_type;
177 global $newmail_popup_width, $newmail_popup_height;
179 if ($newmail_enable == 'on' ||
180 $newmail_popup == 'on' ||
181 $newmail_changetitle) {
183 // open a connection on the imap port (143)
185 $boxes = sqimap_mailbox_list($imapConnection);
186 $delimeter = sqimap_get_delimiter($imapConnection);
191 for ($i = 0;$i < count($boxes); $i++
) {
193 $mailbox = $boxes[$i]['formatted'];
195 if (! isset($boxes[$i]['unseen'])) {
196 $boxes[$i]['unseen'] = '';
198 if ($boxes[$i]['flags']) {
200 for ($h = 0; $h < count($boxes[$i]['flags']); $h++
) {
201 if (strtolower($boxes[$i]["flags"][$h]) == 'noselect') {
206 $status +
= CheckNewMailboxSound($imapConnection,
208 $boxes[$i]['unformatted'],
210 $boxes[$i]['unseen'],
214 $status +
= CheckNewMailboxSound($imapConnection,
216 $boxes[$i]['unformatted'],
218 $boxes[$i]['unseen'],
223 // sqimap_logout($imapConnection);
225 // If we found unseen messages, then we
226 // will play the sound as follows:
228 if ($newmail_changetitle) {
229 echo "<script language=\"javascript\" type=\"text/javascript\">\n" .
230 "function ChangeTitleLoad() {\n";
231 echo 'window.parent.document.title = "' .
232 sprintf(ngettext("%s New Message","%s New Messages",$totalNew), $totalNew) .
234 echo "if (BeforeChangeTitle != null)\n".
235 "BeforeChangeTitle();\n".
237 "BeforeChangeTitle = window.onload;\n".
238 "window.onload = ChangeTitleLoad;\n".
242 // create media output if there are new email messages
243 if ($newmail_allowsound && $totalNew > 0 && $newmail_enable == 'on' && $newmail_media != '' ) {
244 echo newmail_create_media_tags($newmail_media);
247 if ($totalNew > 0 && $newmail_popup == 'on') {
248 // Idea by: Nic Wolfe (Nic@TimelapseProductions.com)
249 // Web URL: http://fineline.xs.mw
250 // More code from Tyler Akins
251 echo "<script language=\"JavaScript\">\n".
253 "function PopupScriptLoad() {\n".
254 'window.open("'.sqm_baseuri().'plugins/newmail/newmail.php?numnew='.$totalNew.
256 "\"width=$newmail_popup_width,height=$newmail_popup_height,scrollbars=no\");\n".
257 "if (BeforePopupScript != null)\n".
258 "BeforePopupScript();\n".
260 "BeforePopupScript = window.onload;\n".
261 "window.onload = PopupScriptLoad;\n".
268 // ----- end of hooked functions -----
271 * Checks if mailbox contains new messages.
273 * @param stream $imapConnection
274 * @param string $mailbox FIXME: option is not used
275 * @param string $real_box unformated mailbox name
276 * @param string $delimeter FIXME: option is not used
277 * @param string $unseen FIXME: option is not used
278 * @param integer $total_new number of new messages
279 * @return bool true, if there are new messages
281 function CheckNewMailboxSound($imapConnection, $mailbox, $real_box, $delimeter, $unseen, &$total_new) {
282 global $trash_folder, $sent_folder,
283 $unseen_notify, $newmail_allbox,
286 $mailboxURL = urlencode($real_box);
288 // Skip folders for Sent and Trash
289 if ($real_box == $sent_folder ||
290 $real_box == $trash_folder) {
294 if (($unseen_notify == 2 && $real_box == 'INBOX') ||
295 ($unseen_notify == 3 && ($newmail_allbox == 'on' ||
296 $real_box == 'INBOX'))) {
297 $status = sqimap_status_messages( $imapConnection, $real_box);
298 if($newmail_recent == 'on') {
299 $total_new +
= $status['RECENT'];
301 $total_new +
= $status['UNSEEN'];
311 * Function tries to detect if file contents match declared file type
313 * Function returns default extension for detected mime type or 'false'
315 * TODO: use $contents to check if file is in specified type
316 * @param string $contents file contents
317 * @param string $type file mime type
320 function newmail_detect_filetype($contents,$type) {
321 // convert $type to lower case
322 $type=strtolower($type);
333 case 'application/ogg':
336 case 'application/x-shockwave-flash':
339 case 'image/svg+xml':
349 * Function tries to detect uploaded file type
350 * @param string $type
351 * @param string $filename
352 * @return integer One of SM_NEWMAIL_FILETYPE_* defines or false.
354 function newmail_get_mediatype($type,$filename) {
356 // fix for browser's that upload file as application/octet-stream
357 case 'application/octet-stream':
358 $ret=newmail_get_mediatype_by_ext($filename);
361 $ret=SM_NEWMAIL_FILETYPE_WAV
;
364 $ret=SM_NEWMAIL_FILETYPE_MP3
;
366 case 'application/ogg':
367 $ret=SM_NEWMAIL_FILETYPE_OGG
;
369 case 'application/x-shockwave-flash':
370 $ret=SM_NEWMAIL_FILETYPE_SWF
;
372 case 'image/svg+xml':
373 $ret=SM_NEWMAIL_FILETYPE_SVG
;
382 * Function provides filetype detection for browsers, that
383 * upload files with application/octet-stream file type.
384 * Ex. some version of Opera.
385 * @param string $filename
386 * @return integer One of SM_NEWMAIL_FILETYPE_* defines or false.
388 function newmail_get_mediatype_by_ext($filename) {
389 if (preg_match("/\.wav$/i",$filename)) return SM_NEWMAIL_FILETYPE_WAV
;
390 if (preg_match("/\.mp3$/i",$filename)) return SM_NEWMAIL_FILETYPE_MP3
;
391 if (preg_match("/\.ogg$/i",$filename)) return SM_NEWMAIL_FILETYPE_OGG
;
392 if (preg_match("/\.swf$/i",$filename)) return SM_NEWMAIL_FILETYPE_SWF
;
393 if (preg_match("/\.svg$/i",$filename)) return SM_NEWMAIL_FILETYPE_SVG
;
398 * Creates html object tags of multimedia object
400 * Main function that creates multimedia object tags
401 * @param string $object object name
402 * @param integer $type media object type
403 * @param string $path URL to media object
404 * @param array $args media object attributes
405 * @param string $extra tags that have to buried deep inside object tags
406 * @param bool $addsuffix controls addition of suffix to media object url
407 * @return string object html tags and attributes required by selected media type.
409 function newmail_media_objects($object,$types,$path,$args=array(),$extra='',$addsuffix=true) {
410 global $newmail_mediacompat_mode;
412 // first prepare single object for IE
413 $ret = newmail_media_object_ie($object,$types[0],$path,$args,$addsuffix);
415 // W3.org nested objects
416 $ret.= "<!--[if !IE]> <-->\n"; // not for IE
418 foreach ($types as $type) {
419 $ret.= newmail_media_object($object,$type,$path,$args,$addsuffix);
422 if (isset($newmail_mediacompat_mode) && $newmail_mediacompat_mode)
423 $ret.= newmail_media_embed($object,$types[0],$path,$args,$addsuffix);
424 // add $extra code inside objects
428 if (isset($newmail_mediacompat_mode) && $newmail_mediacompat_mode)
429 $ret.= newmail_media_embed_close($types[0]);
431 foreach (array_reverse($types) as $type) {
432 $ret.= newmail_media_object_close($type);
434 $ret.= "<!--> <![endif]-->\n"; // end non-IE mode
436 $ret.= newmail_media_object_ie_close($types[0]);
442 * Creates object tags of multimedia object for browsers that comply to w3.org
447 * <li>Returned string does not contain html closing tag.
448 * <li>This is internal function, use newmail_media_objects() instead
450 * @link http://www.w3.org/TR/html4/struct/objects.html#edef-OBJECT W3.org specs
451 * @param string $object object name
452 * @param integer $type media object type
453 * @param string $path URL to media object
454 * @param array $args media object attributes
455 * @param bool $addsuffix controls addition of suffix to media object url
456 * @return string object html tags and attributes required by selected media type.
458 function newmail_media_object($object,$type,$path,$args=array(),$addsuffix=true) {
461 $sArgs=newmail_media_prepare_args($args);
464 case SM_NEWMAIL_FILETYPE_SWF
:
465 if ($addsuffix) $suffix='.swf';
466 $ret_w3 = '<object data="' . $path . $object . $suffix . '" '
468 .'type="application/x-shockwave-flash">' . "\n";
470 case SM_NEWMAIL_FILETYPE_WAV
:
471 if ($addsuffix) $suffix='.wav';
472 $ret_w3 = '<object data="' . $path . $object . $suffix . '" '
474 .'type="audio/x-wav">' . "\n";
476 case SM_NEWMAIL_FILETYPE_OGG
:
477 if ($addsuffix) $suffix='.ogg';
478 $ret_w3 = '<object data="' . $path . $object . $suffix . '" '
480 .'type="application/ogg">' . "\n";
482 case SM_NEWMAIL_FILETYPE_MP3
:
483 if ($addsuffix) $suffix='.mp3';
484 $ret_w3 = '<object data="' . $path . $object . $suffix . '" '
486 .'type="audio/mpeg">' . "\n";
488 case SM_NEWMAIL_FILETYPE_SVG
:
489 if ($addsuffix) $suffix='.svg';
490 $ret_w3 = '<object data="' . $path . $object . $suffix . '" '
492 .'type="image/svg+xml">' . "\n";
501 * Creates multimedia object tags for Internet Explorer (Win32)
504 * * Returned string does not contain html closing tag, because
505 * this multimedia object can include other media objects.
506 * * This is internal function, use newmail_media_objects() instead
508 * @param string $object object name
509 * @param integer $type media object type
510 * @param string $path URL to media object
511 * @param array $args media object attributes
512 * @param bool $addsuffix controls addition of suffix to media object url
513 * @return string object html tags and attributes required by selected media type.
515 function newmail_media_object_ie($object,$type,$path,$args=array(),$addsuffix) {
518 $sArgs=newmail_media_prepare_args($args);
521 case SM_NEWMAIL_FILETYPE_SWF
:
522 if ($addsuffix) $suffix='.swf';
523 $ret_ie ='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '
524 .'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" '
525 . $sArgs . 'id="' . $object ."\">\n"
526 .'<param name="movie" value="' . $path . $object . $suffix . "\">\n"
527 .'<param name="hidden" value="true">' . "\n";
529 case SM_NEWMAIL_FILETYPE_WAV
:
530 if ($addsuffix) $suffix='.wav';
531 $ret_ie ='<object classid="clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95" '
532 .'codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,0902" '
533 . $sArgs . 'id="' . $object ."\" \n"
534 .'type="audio/x-wav">' ."\n"
535 .'<param name="FileName" value="' . $path . $object . $suffix . "\">\n";
537 case SM_NEWMAIL_FILETYPE_MP3
:
538 if ($addsuffix) $suffix='.mp3';
539 $ret_ie ='<object classid="clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95" '
540 .'codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,0902" '
541 . $sArgs . 'id="' . $object ."\" \n"
542 .'type="audio/mpeg">' ."\n"
543 .'<param name="FileName" value="' . $path . $object . $suffix . "\">\n";
545 case SM_NEWMAIL_FILETYPE_OGG
:
546 case SM_NEWMAIL_FILETYPE_SVG
:
554 * Creates embed tags of multimedia object
557 * Apple: http://www.apple.com/quicktime/authoring/embed.html
560 * * Returned string does not contain html closing tag.
561 * * embed tags will be created by newmail_media_objects() only
562 * when $newmail_mediacompat_mode option is enabled. Option is not
563 * enabled by default in order to comply to w3.org specs.
564 * * This is internal function, use newmail_media_objects() instead
565 * @link http://www.apple.com/quicktime/authoring/embed.html Info about embed tag
566 * @param string $object object name
567 * @param integer $type media object type
568 * @param string $path URL to media object
569 * @param array $args media object attributes
570 * @param bool $addsuffix controls addition of suffix to media object url
571 * @return string embed html tags and attributes required by selected media type.
573 function newmail_media_embed($object,$type,$path,$args=array(),$addsuffix=true) {
576 $sArgs=newmail_media_prepare_args($args);
579 case SM_NEWMAIL_FILETYPE_SWF
:
580 if ($addsuffix) $suffix='.swf';
581 $ret_embed='<embed src="' . $path . $object . $suffix . '" '. "\n"
582 .'hidden="true" autostart="true" '. "\n"
584 .'name="' . $object .'" ' . "\n"
585 .'type="application/x-shockwave-flash" ' . "\n"
586 .'pluginspage="http://www.macromedia.com/go/getflashplayer">' . "\n";
588 case SM_NEWMAIL_FILETYPE_WAV
:
589 if ($addsuffix) $suffix='.wav';
590 $ret_embed='<embed src="' . $path . $object . $suffix . '" '. "\n"
591 .' hidden="true" autostart="true" '. "\n"
593 .' name="' . $object .'" ' . "\n"
594 .' type="audio/x-wav">' . "\n";
596 case SM_NEWMAIL_FILETYPE_OGG
:
597 case SM_NEWMAIL_FILETYPE_MP3
:
598 case SM_NEWMAIL_FILETYPE_SVG
:
606 * Adds closing tags for ie object
608 * * This is internal function, use newmail_media_objects() instead
609 * @param integer $type media object type
610 * @return string closing tag of media object
612 function newmail_media_object_ie_close($type) {
615 case SM_NEWMAIL_FILETYPE_SWF
:
616 case SM_NEWMAIL_FILETYPE_WAV
:
617 case SM_NEWMAIL_FILETYPE_MP3
:
618 $ret_end="</object>\n";
620 case SM_NEWMAIL_FILETYPE_OGG
:
621 case SM_NEWMAIL_FILETYPE_SVG
:
629 * Adds closing tags for object
631 * * This is internal function, use newmail_media_objects() instead
632 * @param integer $type media object type
633 * @return string closing tag of media object
635 function newmail_media_object_close($type) {
638 case SM_NEWMAIL_FILETYPE_SWF
:
639 case SM_NEWMAIL_FILETYPE_WAV
:
640 case SM_NEWMAIL_FILETYPE_OGG
:
641 case SM_NEWMAIL_FILETYPE_MP3
:
642 case SM_NEWMAIL_FILETYPE_SVG
:
643 $ret_end="</object>\n";
652 * Adds closing tags for object
654 * * This is internal function, use newmail_media_objects() instead
655 * @param integer $type media object type
656 * @return string closing tag of media object
658 function newmail_media_embed_close($type) {
661 case SM_NEWMAIL_FILETYPE_SWF
:
662 case SM_NEWMAIL_FILETYPE_WAV
:
663 $ret_end="</embed>\n";
665 case SM_NEWMAIL_FILETYPE_OGG
:
666 case SM_NEWMAIL_FILETYPE_MP3
:
667 case SM_NEWMAIL_FILETYPE_SVG
:
675 * Converts media attributes to string
677 * * attribute values are automatically sanitized by htmlspecialchars()
678 * * This is internal function, use newmail_media_objects() instead
679 * @param array $args array with object attributes
680 * @return string string with object attributes
682 function newmail_media_prepare_args($args) {
684 foreach ($args as $arg => $value) {
685 $ret_args.= $arg . '="' . htmlspecialchars($value) . '" ';
691 * Detects used media type and creates all need tags
692 * @param string $newmail_media
693 * @return string html tags with media objects
695 function newmail_create_media_tags($newmail_media) {
696 global $newmail_mmedia, $newmail_userfile_type;
698 if (preg_match("/^mmedia_+/",$newmail_media)) {
699 $ret_media = "<!-- newmail mmedia option -->\n";
701 $newmail_mmedia_short=preg_replace("/^mmedia_/",'',$newmail_media);
702 // check if media option is not removed
703 if (isset($newmail_mmedia[$newmail_mmedia_short])) {
704 $ret_media.= newmail_media_objects($newmail_mmedia_short,
705 $newmail_mmedia[$newmail_mmedia_short]['types'],
706 sqm_baseuri() . 'plugins/newmail/media/',
707 $newmail_mmedia[$newmail_mmedia_short]['args']);
709 $ret_media.= "<!-- end of newmail mmedia option -->\n";
710 } elseif ($newmail_media=='(userfile)') {
711 $ret_media = "<!-- newmail usermedia option -->\n";
712 $ret_media.= newmail_media_objects('loadfile.php',
713 array($newmail_userfile_type),
714 sqm_baseuri() . 'plugins/newmail/',
715 array('width'=>0,'height'=>0),
717 $ret_media.= "<!-- end of newmail usermedia option -->\n";
719 $ret_media = "<!-- newmail sounds from sounds/*.wav -->\n";
720 $ret_media.= newmail_media_objects(basename($newmail_media),
721 array(SM_NEWMAIL_FILETYPE_WAV
),
722 sqm_baseuri() . 'plugins/newmail/sounds/',
723 array('width'=>0,'height'=>0),
725 $ret_media.= "<!-- end of newmail sounds from sounds/*.wav -->\n";