666ef82a6f57c2623e82143b5f570dde289a06c9
[squirrelmail.git] / plugins / newmail / setup.php
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
28 function CheckNewMailboxSound($imapConnection, $mailbox, $real_box, $delimeter, $unseen, &$total_unseen) {
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;
34
35 $mailboxURL = urlencode($real_box);
36 $unseen_found = 0;
37
38 // Skip folders for Sent and Trash
39
40 if ($real_box == $sent_folder ||
41 $real_box == $trash_folder) {
42 return 0;
43 }
44
45 if (($unseen_notify == 2 && $real_box == 'INBOX') ||
46 ($unseen_notify == 3 && ($newmail_allbox == 'on' ||
47 $real_box == 'INBOX'))) {
48 $unseen = sqimap_unseen_messages($imapConnection, $real_box);
49 $total_unseen += $unseen;
50
51 if($newmail_recent == 'on') {
52 $unseen = sqimap_mailbox_select( $imapConnection, $real_box, TRUE, TRUE);
53 }
54
55 if ($unseen > 0) {
56 $unseen_found = 1;
57 }
58 }
59 return( $unseen_found );
60 }
61
62 function squirrelmail_plugin_init_newmail() {
63 global $squirrelmail_plugin_hooks;
64
65 $squirrelmail_plugin_hooks['left_main_before']['newmail'] = 'newmail_plugin';
66 $squirrelmail_plugin_hooks['optpage_register_block']['newmail'] = 'newmail_optpage_register_block';
67 $squirrelmail_plugin_hooks['options_save']['newmail'] = 'newmail_sav';
68 $squirrelmail_plugin_hooks['loading_prefs']['newmail'] = 'newmail_pref';
69 }
70
71 function newmail_optpage_register_block() {
72 // Gets added to the user's OPTIONS page.
73 global $optpage_blocks;
74
75 if ( !soupNazi() ) {
76
77 /* Register Squirrelspell with the $optionpages array. */
78 $optpage_blocks[] = array(
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 }
86
87 function newmail_sav() {
88
89 global $username,$data_dir;
90 global $submit_newmail,$media_file,$media_reset,$media_enable,$media_popup;
91 global $media_recent,$media_sel;
92 global $media_allbox, $media_changetitle;
93
94 if ( isset($submit_newmail) ) {
95
96 if(isset($media_enable)) {
97 setPref($data_dir,$username,'newmail_enable',$media_enable);
98 } else {
99 setPref($data_dir,$username,'newmail_enable','');
100 }
101 if(isset($media_popup)) {
102 setPref($data_dir,$username,'newmail_popup',$media_popup);
103 } else {
104 setPref($data_dir,$username,'newmail_popup','');
105 }
106 if(isset($media_allbox)) {
107 setPref($data_dir,$username,'newmail_allbox',$media_allbox);
108 } else {
109 setPref($data_dir,$username,'newmail_allbox','');
110 }
111 if(isset($media_recent)) {
112 setPref($data_dir,$username,'newmail_recent',$media_recent);
113 } else {
114 setPref($data_dir,$username,'newmail_recent','');
115 }
116 if(isset($media_changetitle)) {
117 setPref($data_dir,$username,'newmail_changetitle',$media_changetitle);
118 } else {
119 setPref($data_dir,$username,'newmail_changetitle','');
120 }
121 if(isset($media_sel)) {
122 if($media_sel == '(local media)') {
123 setPref($data_dir,$username,'newmail_media',StripSlashes($media_file));
124 } else {
125 setPref($data_dir,$username,'newmail_media',$media_sel);
126 }
127 } else {
128 setPref($data_dir,$username,'newmail_media','');
129 }
130 echo html_tag( 'p', _("New Mail Notification options saved"), 'center' );
131 }
132 }
133
134 function newmail_pref() {
135
136 global $username,$data_dir;
137 global $newmail_media,$newmail_enable,$newmail_popup,$newmail_allbox;
138 global $newmail_recent, $newmail_changetitle;
139
140 $newmail_recent = getPref($data_dir,$username,'newmail_recent');
141 $newmail_enable = getPref($data_dir,$username,'newmail_enable');
142 $newmail_media = getPref($data_dir, $username, 'newmail_media', '../plugins/newmail/sounds/Notify.wav');
143 $newmail_popup = getPref($data_dir, $username, 'newmail_popup');
144 $newmail_allbox = getPref($data_dir, $username, 'newmail_allbox');
145 $newmail_changetitle = getPref($data_dir, $username, 'newmail_changetitle');
146
147 }
148
149 function newmail_plugin() {
150
151 global $username, $key, $imapServerAddress, $imapPort,
152 $newmail_media, $newmail_enable, $newmail_popup,
153 $newmail_recent, $newmail_changetitle, $imapConnection;
154
155 if ($newmail_enable == 'on' ||
156 $newmail_popup == 'on' ||
157 $newmail_changetitle) {
158
159 // open a connection on the imap port (143)
160
161 $boxes = sqimap_mailbox_list($imapConnection);
162 $delimeter = sqimap_get_delimiter($imapConnection);
163
164 $status = 0;
165 $totalNew = 0;
166
167 for ($i = 0;$i < count($boxes); $i++) {
168
169 $line = '';
170 $mailbox = $boxes[$i]['formatted'];
171
172 if (! isset($boxes[$i]['unseen'])) {
173 $boxes[$i]['unseen'] = '';
174 }
175 if ($boxes[$i]['flags']) {
176 $noselect = false;
177 for ($h = 0; $h < count($boxes[$i]['flags']); $h++) {
178 if (strtolower($boxes[$i]["flags"][$h]) == 'noselect') {
179 $noselect = TRUE;
180 }
181 }
182 if (! $noselect) {
183 $status += CheckNewMailboxSound($imapConnection,
184 $mailbox,
185 $boxes[$i]['unformatted'],
186 $delimeter,
187 $boxes[$i]['unseen'],
188 $totalNew);
189 }
190 } else {
191 $status += CheckNewMailboxSound($imapConnection,
192 $mailbox,
193 $boxes[$i]['unformatted'],
194 $delimeter,
195 $boxes[$i]['unseen'],
196 $totalNew);
197 }
198
199 }
200
201 // sqimap_logout($imapConnection);
202
203 // If we found unseen messages, then we
204 // will play the sound as follows:
205
206 if ($newmail_changetitle) {
207 echo "<script language=\"javascript\">\n" .
208 "function ChangeTitleLoad() {\n";
209 if( $totalNew > 1 || $totalNew == 0 ) {
210 echo 'window.parent.document.title = "' .
211 sprintf(_("%s New Messages"), $totalNew ) .
212 "\";\n";
213 } else {
214 echo 'window.parent.document.title = "' .
215 sprintf(_("%s New Message"), $totalNew ) .
216 "\";\n";
217 }
218 echo "if (BeforeChangeTitle != null)\n".
219 "BeforeChangeTitle();\n".
220 "}\n".
221 "BeforeChangeTitle = window.onload;\n".
222 "window.onload = ChangeTitleLoad;\n".
223 "</script>\n";
224 }
225
226 if ($totalNew > 0 && $newmail_enable == 'on') {
227 echo "<EMBED SRC=\"$newmail_media\" HIDDEN=TRUE AUTOSTART=TRUE>\n";
228 }
229 if ($totalNew > 0 && $newmail_popup == 'on') {
230 echo "<SCRIPT LANGUAGE=\"JavaScript\">\n".
231 "<!--\n".
232 "function PopupScriptLoad() {\n".
233 'window.open("../plugins/newmail/newmail.php", "SMPopup",'.
234 "\"width=200,height=130,scrollbars=no\");\n".
235 "if (BeforePopupScript != null)\n".
236 "BeforePopupScript();\n".
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 }
249 ?>