Some plugins to rg=0: bug_report message_details newmail sent_subfolders spamcop...
[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 $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']);
95 } else {
96 setPref($data_dir,$username,'newmail_enable','');
97 }
98 if(isset($_POST['media_popup'])) {
99 setPref($data_dir,$username,'newmail_popup',$_POST['media_popup']);
100 } else {
101 setPref($data_dir,$username,'newmail_popup','');
102 }
103 if(isset($_POST['media_allbox'])) {
104 setPref($data_dir,$username,'newmail_allbox',$_POST['media_allbox']);
105 } else {
106 setPref($data_dir,$username,'newmail_allbox','');
107 }
108 if(isset($_POST['media_recent'])) {
109 setPref($data_dir,$username,'newmail_recent',$_POST['media_recent']);
110 } else {
111 setPref($data_dir,$username,'newmail_recent','');
112 }
113 if(isset($_POST['media_changetitle'])) {
114 setPref($data_dir,$username,'newmail_changetitle',$_POST['media_changetitle']);
115 } else {
116 setPref($data_dir,$username,'newmail_changetitle','');
117 }
118 if(isset($_POST['media_sel'])) {
119 if($_POST['media_sel'] == '(local media)') {
120 setPref($data_dir,$username,'newmail_media',StripSlashes($_POST['media_file']));
121 } else {
122 setPref($data_dir,$username,'newmail_media',$_POST['media_sel']);
123 }
124 } else {
125 setPref($data_dir,$username,'newmail_media','');
126 }
127 echo html_tag( 'p', _("New Mail Notification options saved"), 'center' );
128 }
129 }
130
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');
139 $newmail_media = getPref($data_dir, $username, 'newmail_media', '../plugins/newmail/sounds/Notify.wav');
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');
143
144 }
145
146 function newmail_plugin() {
147
148 global $username, $key, $imapServerAddress, $imapPort,
149 $newmail_media, $newmail_enable, $newmail_popup,
150 $newmail_recent, $newmail_changetitle, $imapConnection;
151
152 if ($newmail_enable == 'on' ||
153 $newmail_popup == 'on' ||
154 $newmail_changetitle) {
155
156 // open a connection on the imap port (143)
157
158 $boxes = sqimap_mailbox_list($imapConnection);
159 $delimeter = sqimap_get_delimiter($imapConnection);
160
161 $status = 0;
162 $totalNew = 0;
163
164 for ($i = 0;$i < count($boxes); $i++) {
165
166 $line = '';
167 $mailbox = $boxes[$i]['formatted'];
168
169 if (! isset($boxes[$i]['unseen'])) {
170 $boxes[$i]['unseen'] = '';
171 }
172 if ($boxes[$i]['flags']) {
173 $noselect = false;
174 for ($h = 0; $h < count($boxes[$i]['flags']); $h++) {
175 if (strtolower($boxes[$i]["flags"][$h]) == 'noselect') {
176 $noselect = TRUE;
177 }
178 }
179 if (! $noselect) {
180 $status += CheckNewMailboxSound($imapConnection,
181 $mailbox,
182 $boxes[$i]['unformatted'],
183 $delimeter,
184 $boxes[$i]['unseen'],
185 $totalNew);
186 }
187 } else {
188 $status += CheckNewMailboxSound($imapConnection,
189 $mailbox,
190 $boxes[$i]['unformatted'],
191 $delimeter,
192 $boxes[$i]['unseen'],
193 $totalNew);
194 }
195
196 }
197
198 // sqimap_logout($imapConnection);
199
200 // If we found unseen messages, then we
201 // will play the sound as follows:
202
203 if ($newmail_changetitle) {
204 echo "<script language=\"javascript\">\n" .
205 "function ChangeTitleLoad() {\n";
206 if( $totalNew > 1 || $totalNew == 0 ) {
207 echo 'window.parent.document.title = "' .
208 sprintf(_("%s New Messages"), $totalNew ) .
209 "\";\n";
210 } else {
211 echo 'window.parent.document.title = "' .
212 sprintf(_("%s New Message"), $totalNew ) .
213 "\";\n";
214 }
215 echo "if (BeforeChangeTitle != null)\n".
216 "BeforeChangeTitle();\n".
217 "}\n".
218 "BeforeChangeTitle = window.onload;\n".
219 "window.onload = ChangeTitleLoad;\n".
220 "</script>\n";
221 }
222
223 if ($totalNew > 0 && $newmail_enable == 'on') {
224 echo "<EMBED SRC=\"$newmail_media\" HIDDEN=TRUE AUTOSTART=TRUE>\n";
225 }
226 if ($totalNew > 0 && $newmail_popup == 'on') {
227 echo "<SCRIPT LANGUAGE=\"JavaScript\">\n".
228 "<!--\n".
229 "function PopupScriptLoad() {\n".
230 'window.open("../plugins/newmail/newmail.php", "SMPopup",'.
231 "\"width=200,height=130,scrollbars=no\");\n".
232 "if (BeforePopupScript != null)\n".
233 "BeforePopupScript();\n".
234 "}\n".
235 "BeforePopupScript = window.onload;\n".
236 "window.onload = PopupScriptLoad;\n".
237 // Idea by: Nic Wolfe (Nic@TimelapseProductions.com)
238 // Web URL: http://fineline.xs.mw
239 // More code from Tyler Akins
240 "// End -->\n".
241 "</script>\n";
242
243 }
244 }
245 }
246 ?>