Some cleanup.
[squirrelmail.git] / plugins / newmail / setup.php
CommitLineData
4508b1b6 1<?php
2
3 /**
4 ** newmail.php
5 ** (c)2000 by Michael Huttinger
6 **
7 ** Quite a hack -- but my first attempt at a plugin. We were
8 ** looking for a way to play a sound when there was unseen
9 ** messages to look at. Nice for users who keep the squirrel
10 ** mail window up for long periods of time and want to know
11 ** when mail arrives.
12 **
13 ** Basically, I hacked much of left_main.php into a plugin that
14 ** goes through each mail folder and increments a flag if
15 ** there are unseen messages. If the final count of unseen
16 ** folders is > 0, then we play a sound (using the HTML at the
17 ** far end of this script).
18 **
19 ** This was tested with IE5.0 - but I hear Netscape works well,
20 ** too (with a plugin).
2d4c15d6 21 **
22 ** $Id$
23 **
4508b1b6 24 **/
25
2d4c15d6 26 function CheckNewMailboxSound($imapConnection, $mailbox, $real_box, $delimeter, $unseen, &$total_unseen) {
27 global $folder_prefix, $trash_folder, $sent_folder;
6a745675 28 global $color, $move_to_sent, $move_to_trash;
2d4c15d6 29 global $unseen_notify, $unseen_type, $newmail_allbox, $newmail_recent;
30 global $newmail_changetitle;
4508b1b6 31
2d4c15d6 32 $mailboxURL = urlencode($real_box);
33 $unseen_found = 0;
4508b1b6 34
2d4c15d6 35 // Skip folders for Sent and Trash
4508b1b6 36
6a745675 37 if ($real_box == $sent_folder ||
2d4c15d6 38 $real_box == $trash_folder) {
6a745675 39 return 0;
2d4c15d6 40 }
4508b1b6 41
2d4c15d6 42 if (($unseen_notify == 2 && $real_box == 'INBOX') ||
43 ($unseen_notify == 3 && ($newmail_allbox == 'on' ||
6a745675 44 $real_box == 'INBOX'))) {
2d4c15d6 45 $unseen = sqimap_unseen_messages($imapConnection, $real_box);
6a745675 46 $total_unseen += $unseen;
47
48 if($newmail_recent == 'on') {
49 $unseen = sqimap_mailbox_select( $imapConnection, $real_box, TRUE, TRUE);
50 }
51
2d4c15d6 52 if ($unseen > 0) {
53 $unseen_found = 1;
6a745675 54 }
2d4c15d6 55 }
56 return( $unseen_found );
57 }
4508b1b6 58
3d75ef16 59 function squirrelmail_plugin_init_newmail() {
60 global $squirrelmail_plugin_hooks;
6a745675 61
3d75ef16 62 $squirrelmail_plugin_hooks['left_main_before']['newmail'] = 'newmail_plugin';
cbe5423b 63 $squirrelmail_plugin_hooks['optpage_register_block']['newmail'] = 'newmail_optpage_register_block';
3d75ef16 64 $squirrelmail_plugin_hooks['options_save']['newmail'] = 'newmail_sav';
65 $squirrelmail_plugin_hooks['loading_prefs']['newmail'] = 'newmail_pref';
66 }
4508b1b6 67
cbe5423b 68 function newmail_optpage_register_block() {
3d75ef16 69 // Gets added to the user's OPTIONS page.
cbe5423b 70 global $optpage_blocks;
4508b1b6 71
3d75ef16 72 if ( !soupNazi() ) {
4508b1b6 73
3d75ef16 74 /* Register Squirrelspell with the $optionpages array. */
cbe5423b 75 $optpage_blocks[] = array(
3d75ef16 76 'name' => _("NewMail Options"),
77 'url' => '../plugins/newmail/newmail_opt.php',
78 'desc' => _("This configures settings for playing sounds and/or showing popup windows when new mail arrives."),
79 'js' => TRUE
80 );
81 }
82 }
4508b1b6 83
e697b6cc 84 function newmail_sav() {
85
86 global $username,$data_dir;
87 global $submit_newmail,$media_file,$media_reset,$media_enable,$media_popup;
88 global $media_recent,$media_sel;
89 global $media_allbox, $media_changetitle;
90
91 if ($submit_newmail) {
92 if(isset($media_enable)) {
93 setPref($data_dir,$username,'newmail_enable',$media_enable);
94 } else {
95 setPref($data_dir,$username,'newmail_enable','');
96 }
97 if(isset($media_popup)) {
98 setPref($data_dir,$username,'newmail_popup',$media_popup);
99 } else {
100 setPref($data_dir,$username,'newmail_popup','');
101 }
102 if(isset($media_allbox)) {
103 setPref($data_dir,$username,'newmail_allbox',$media_allbox);
104 } else {
105 setPref($data_dir,$username,'newmail_allbox','');
106 }
107 if(isset($media_recent)) {
108 setPref($data_dir,$username,'newmail_recent',$media_recent);
109 } else {
110 setPref($data_dir,$username,'newmail_recent','');
111 }
112 if(isset($media_changetitle)) {
113 setPref($data_dir,$username,'newmail_changetitle',$media_changetitle);
114 } else {
115 setPref($data_dir,$username,'newmail_changetitle','');
116 }
117 if(isset($media_sel)) {
118 if($media_sel == '(local media)') {
119 setPref($data_dir,$username,'newmail_media',StripSlashes($media_file));
120 } else {
121 setPref($data_dir,$username,'newmail_media',$media_sel);
122 }
123 } else {
124 setPref($data_dir,$username,'newmail_media','');
125 }
126 echo '<center> ' . _("New Mail Notification options saved") . '</center>';
127 }
128 }
4508b1b6 129
2d4c15d6 130 function newmail_pref() {
131
132 global $username,$data_dir;
133 global $newmail_media,$newmail_enable,$newmail_popup,$newmail_allbox;
134 global $newmail_recent, $newmail_changetitle;
135
136 $newmail_recent = getPref($data_dir,$username,'newmail_recent');
137 $newmail_enable = getPref($data_dir,$username,'newmail_enable');
138 $newmail_media = getPref($data_dir, $username, 'newmail_media');
139 $newmail_popup = getPref($data_dir, $username, 'newmail_popup');
140 $newmail_allbox = getPref($data_dir, $username, 'newmail_allbox');
141 $newmail_changetitle = getPref($data_dir, $username, 'newmail_changetitle');
e697b6cc 142
2d4c15d6 143 if ($newmail_media == '') {
144 $newmail_media = '../plugins/newmail/sounds/Notify.wav';
145 }
4508b1b6 146
2d4c15d6 147 }
148
e697b6cc 149 function newmail_plugin() {
150
151 global $username,$key,$imapServerAddress,$imapPort;
152 global $newmail_media,$newmail_enable,$newmail_popup,$newmail_recent;
153 global $newmail_changetitle;
154 global $imapConnection;
155
156 if ($newmail_enable == 'on' ||
157 $newmail_popup == 'on' ||
158 $newmail_changetitle) {
159
160 // open a connection on the imap port (143)
161
162 $boxes = sqimap_mailbox_list($imapConnection);
163 $delimeter = sqimap_get_delimiter($imapConnection);
164
165 $status = 0;
166 $totalNew = 0;
167
168 for ($i = 0;$i < count($boxes); $i++) {
169
170 $line = '';
171 $mailbox = $boxes[$i]['formatted'];
172
173 if (! isset($boxes[$i]['unseen']))
174 $boxes[$i]['unseen'] = '';
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 if (! $noselect) {
182 $status = $status + CheckNewMailboxSound($imapConnection, $mailbox,
183 $boxes[$i]['unformatted'], $delimeter, $boxes[$i]['unseen'],
184 $totalNew);
185 }
186 } else {
187 $status = $status + CheckNewMailboxSound($imapConnection, $mailbox, $boxes[$i]['unformatted'],
188 $delimeter, $boxes[$i]['unseen'], $totalNew);
189 }
190
191 }
4508b1b6 192
e697b6cc 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 ) {
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 ($status > 0 && $newmail_enable == 'on') {
219 echo "<EMBED SRC=\"$newmail_media\" HIDDEN=TRUE AUTOSTART=TRUE>";
220 }
221 if ($status >0 && $newmail_popup == 'on') {
222 echo "<SCRIPT LANGUAGE=\"JavaScript\">\n".
223 "<!--\n".
224 "function PopupScriptLoad() {\n".
225 'window.open("../plugins/newmail/newmail.php", "SMPopup",'.
226 "\"width=200,height=130,scrollbars=no\");\n".
227 "if (BeforePopupScript != null)\n".
228 "BeforePopupScript();\n".
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";
237
238 }
239 }
240 }
cbe5423b 241?>