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