| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * newmail.php |
| 5 | * |
| 6 | * Copyright (c) 1999-2002 The SquirrelMail Project Team |
| 7 | * Copyright (c) 2000 by Michael Huttinger |
| 8 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
| 9 | * |
| 10 | * Quite a hack -- but my first attempt at a plugin. We were |
| 11 | * looking for a way to play a sound when there was unseen |
| 12 | * messages to look at. Nice for users who keep the squirrel |
| 13 | * mail window up for long periods of time and want to know |
| 14 | * when mail arrives. |
| 15 | * |
| 16 | * Basically, I hacked much of left_main.php into a plugin that |
| 17 | * goes through each mail folder and increments a flag if |
| 18 | * there are unseen messages. If the final count of unseen |
| 19 | * folders is > 0, then we play a sound (using the HTML at the |
| 20 | * far end of this script). |
| 21 | * |
| 22 | * This was tested with IE5.0 - but I hear Netscape works well, |
| 23 | * too (with a plugin). |
| 24 | * |
| 25 | * $Id$ |
| 26 | */ |
| 27 | include_once(SM_PATH . 'functions/display_messages.php'); |
| 28 | |
| 29 | function CheckNewMailboxSound($imapConnection, $mailbox, $real_box, $delimeter, $unseen, &$total_new) { |
| 30 | |
| 31 | global $folder_prefix, $trash_folder, $sent_folder, |
| 32 | $color, $move_to_sent, $move_to_trash, |
| 33 | $unseen_notify, $unseen_type, $newmail_allbox, |
| 34 | $newmail_recent, $newmail_changetitle; |
| 35 | |
| 36 | $mailboxURL = urlencode($real_box); |
| 37 | $unseen = $recent = 0; |
| 38 | |
| 39 | // Skip folders for Sent and Trash |
| 40 | |
| 41 | if ($real_box == $sent_folder || |
| 42 | $real_box == $trash_folder) { |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | if (($unseen_notify == 2 && $real_box == 'INBOX') || |
| 47 | ($unseen_notify == 3 && ($newmail_allbox == 'on' || |
| 48 | $real_box == 'INBOX'))) { |
| 49 | $status = sqimap_status_messages( $imapConnection, $real_box); |
| 50 | if($newmail_recent == 'on') { |
| 51 | $total_new += $status['RECENT']; |
| 52 | } else { |
| 53 | $total_new += $status['UNSEEN']; |
| 54 | } |
| 55 | if ($total_new) { |
| 56 | return 1; |
| 57 | } |
| 58 | |
| 59 | } |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | function squirrelmail_plugin_init_newmail() { |
| 64 | global $squirrelmail_plugin_hooks; |
| 65 | |
| 66 | $squirrelmail_plugin_hooks['left_main_before']['newmail'] = 'newmail_plugin'; |
| 67 | $squirrelmail_plugin_hooks['optpage_register_block']['newmail'] = 'newmail_optpage_register_block'; |
| 68 | $squirrelmail_plugin_hooks['options_save']['newmail'] = 'newmail_sav'; |
| 69 | $squirrelmail_plugin_hooks['loading_prefs']['newmail'] = 'newmail_pref'; |
| 70 | } |
| 71 | |
| 72 | function newmail_optpage_register_block() { |
| 73 | // Gets added to the user's OPTIONS page. |
| 74 | global $optpage_blocks; |
| 75 | |
| 76 | if ( !soupNazi() ) { |
| 77 | |
| 78 | /* Register Squirrelspell with the $optionpages array. */ |
| 79 | $optpage_blocks[] = array( |
| 80 | 'name' => _("NewMail Options"), |
| 81 | 'url' => SM_PATH . 'plugins/newmail/newmail_opt.php', |
| 82 | 'desc' => _("This configures settings for playing sounds and/or showing popup windows when new mail arrives."), |
| 83 | 'js' => TRUE |
| 84 | ); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | function newmail_sav() { |
| 89 | global $data_dir, $username; |
| 90 | |
| 91 | if ( sqgetGlobalVar('submit_newmail', $submit, SQ_POST) ) { |
| 92 | $media_enable = ''; |
| 93 | $media_popup = ''; |
| 94 | $media_allbox = ''; |
| 95 | $media_recent = ''; |
| 96 | $media_changetitle = ''; |
| 97 | $media_sel = ''; |
| 98 | |
| 99 | sqgetGlobalVar('media_enable', $media_enable, SQ_POST); |
| 100 | sqgetGlobalVar('media_popup', $media_popup, SQ_POST); |
| 101 | sqgetGlobalVar('media_allbox', $media_allbox, SQ_POST); |
| 102 | sqgetGlobalVar('media_recent', $media_recent, SQ_POST); |
| 103 | sqgetGlobalVar('media_changetitle', $media_changetitle, SQ_POST); |
| 104 | |
| 105 | setPref($data_dir,$username,'newmail_enable',$media_enable); |
| 106 | setPref($data_dir,$username,'newmail_popup', $media_popup); |
| 107 | setPref($data_dir,$username,'newmail_allbox',$media_allbox); |
| 108 | setPref($data_dir,$username,'newmail_recent',$media_recent); |
| 109 | setPref($data_dir,$username,'newmail_changetitle',$media_changetitle); |
| 110 | |
| 111 | if( sqgetGlobalVar('media_sel', $media_sel, SQ_POST) && |
| 112 | ($media_sel == '(none)' || $media_sel == '(local media)') ) { |
| 113 | removePref($data_dir,$username,'newmail_media'); |
| 114 | } else { |
| 115 | setPref($data_dir,$username,'newmail_media',$media_sel); |
| 116 | } |
| 117 | echo html_tag( 'p', _("New Mail Notification options saved"), 'center' ); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | function newmail_pref() { |
| 122 | |
| 123 | global $username,$data_dir; |
| 124 | global $newmail_media,$newmail_enable,$newmail_popup,$newmail_allbox; |
| 125 | global $newmail_recent, $newmail_changetitle; |
| 126 | |
| 127 | $newmail_recent = getPref($data_dir,$username,'newmail_recent'); |
| 128 | $newmail_enable = getPref($data_dir,$username,'newmail_enable'); |
| 129 | $newmail_media = getPref($data_dir, $username, 'newmail_media', '(none)'); |
| 130 | $newmail_popup = getPref($data_dir, $username, 'newmail_popup'); |
| 131 | $newmail_allbox = getPref($data_dir, $username, 'newmail_allbox'); |
| 132 | $newmail_changetitle = getPref($data_dir, $username, 'newmail_changetitle'); |
| 133 | |
| 134 | } |
| 135 | |
| 136 | function newmail_plugin() { |
| 137 | |
| 138 | global $username, $key, $imapServerAddress, $imapPort, |
| 139 | $newmail_media, $newmail_enable, $newmail_popup, |
| 140 | $newmail_recent, $newmail_changetitle, $imapConnection, $PHP_SELF; |
| 141 | |
| 142 | if ($newmail_enable == 'on' || |
| 143 | $newmail_popup == 'on' || |
| 144 | $newmail_changetitle) { |
| 145 | |
| 146 | // open a connection on the imap port (143) |
| 147 | |
| 148 | $boxes = sqimap_mailbox_list($imapConnection); |
| 149 | $delimeter = sqimap_get_delimiter($imapConnection); |
| 150 | |
| 151 | $status = 0; |
| 152 | $totalNew = 0; |
| 153 | |
| 154 | for ($i = 0;$i < count($boxes); $i++) { |
| 155 | |
| 156 | $line = ''; |
| 157 | $mailbox = $boxes[$i]['formatted']; |
| 158 | |
| 159 | if (! isset($boxes[$i]['unseen'])) { |
| 160 | $boxes[$i]['unseen'] = ''; |
| 161 | } |
| 162 | if ($boxes[$i]['flags']) { |
| 163 | $noselect = false; |
| 164 | for ($h = 0; $h < count($boxes[$i]['flags']); $h++) { |
| 165 | if (strtolower($boxes[$i]["flags"][$h]) == 'noselect') { |
| 166 | $noselect = TRUE; |
| 167 | } |
| 168 | } |
| 169 | if (! $noselect) { |
| 170 | $status += CheckNewMailboxSound($imapConnection, |
| 171 | $mailbox, |
| 172 | $boxes[$i]['unformatted'], |
| 173 | $delimeter, |
| 174 | $boxes[$i]['unseen'], |
| 175 | $totalNew); |
| 176 | } |
| 177 | } else { |
| 178 | $status += CheckNewMailboxSound($imapConnection, |
| 179 | $mailbox, |
| 180 | $boxes[$i]['unformatted'], |
| 181 | $delimeter, |
| 182 | $boxes[$i]['unseen'], |
| 183 | $totalNew); |
| 184 | } |
| 185 | |
| 186 | } |
| 187 | |
| 188 | // sqimap_logout($imapConnection); |
| 189 | |
| 190 | // If we found unseen messages, then we |
| 191 | // will play the sound as follows: |
| 192 | |
| 193 | if ($newmail_changetitle) { |
| 194 | echo "<script language=\"javascript\">\n" . |
| 195 | "function ChangeTitleLoad() {\n"; |
| 196 | if( $totalNew > 1 || $totalNew == 0 ) { |
| 197 | echo 'window.parent.document.title = "' . |
| 198 | sprintf(_("%s New Messages"), $totalNew ) . |
| 199 | "\";\n"; |
| 200 | } else { |
| 201 | echo 'window.parent.document.title = "' . |
| 202 | sprintf(_("%s New Message"), $totalNew ) . |
| 203 | "\";\n"; |
| 204 | } |
| 205 | echo "if (BeforeChangeTitle != null)\n". |
| 206 | "BeforeChangeTitle();\n". |
| 207 | "}\n". |
| 208 | "BeforeChangeTitle = window.onload;\n". |
| 209 | "window.onload = ChangeTitleLoad;\n". |
| 210 | "</script>\n"; |
| 211 | } |
| 212 | |
| 213 | if ($totalNew > 0 && $newmail_enable == 'on' && $newmail_media != '' ) { |
| 214 | echo "<EMBED SRC=\"$newmail_media\" HIDDEN=TRUE AUTOSTART=TRUE>\n"; |
| 215 | } |
| 216 | if ($totalNew > 0 && $newmail_popup == 'on') { |
| 217 | echo "<SCRIPT LANGUAGE=\"JavaScript\">\n". |
| 218 | "<!--\n". |
| 219 | "function PopupScriptLoad() {\n". |
| 220 | 'window.open("'.sqm_baseuri().'plugins/newmail/newmail.php", "SMPopup",'. |
| 221 | "\"width=200,height=130,scrollbars=no\");\n". |
| 222 | "if (BeforePopupScript != null)\n". |
| 223 | "BeforePopupScript();\n". |
| 224 | "}\n". |
| 225 | "BeforePopupScript = window.onload;\n". |
| 226 | "window.onload = PopupScriptLoad;\n". |
| 227 | // Idea by: Nic Wolfe (Nic@TimelapseProductions.com) |
| 228 | // Web URL: http://fineline.xs.mw |
| 229 | // More code from Tyler Akins |
| 230 | "// End -->\n". |
| 231 | "</script>\n"; |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | ?> |