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