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