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