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