4508b1b6 |
1 | <?php |
ebe02dfc |
2 | /** |
3 | * newmail.php |
4 | * |
5 | * Copyright (c) 1999-2004 The SquirrelMail Project Team |
6 | * Copyright (c) 2000 by Michael Huttinger |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
8 | * |
9 | * Quite a hack -- but my first attempt at a plugin. We were |
10 | * looking for a way to play a sound when there was unseen |
11 | * messages to look at. Nice for users who keep the squirrel |
12 | * mail window up for long periods of time and want to know |
13 | * when mail arrives. |
14 | * |
15 | * Basically, I hacked much of left_main.php into a plugin that |
16 | * goes through each mail folder and increments a flag if |
17 | * there are unseen messages. If the final count of unseen |
18 | * folders is > 0, then we play a sound (using the HTML at the |
19 | * far end of this script). |
20 | * |
21 | * This was tested with IE5.0 - but I hear Netscape works well, |
22 | * too (with a plugin). |
23 | * |
24 | * @version $Id$ |
25 | * @package plugins |
26 | * @subpackage newmail |
27 | */ |
4508b1b6 |
28 | |
ebe02dfc |
29 | /** |
30 | */ |
31 | include_once(SM_PATH . 'functions/display_messages.php'); |
4508b1b6 |
32 | |
ebe02dfc |
33 | /** |
34 | * Checks if mailbox contains new messages. |
35 | * |
36 | * @param object $imapConnection |
37 | * @param mixed $mailbox FIXME: option is not used |
91e0dccc |
38 | * @param string $real_box unformated mailbox name |
ebe02dfc |
39 | * @param mixed $delimeter FIXME: option is not used |
40 | * @param string $unseen FIXME: option is not used |
41 | * @param integer $total_new number of new messages |
42 | * @return bool true, if there are new messages |
43 | */ |
44 | function CheckNewMailboxSound($imapConnection, $mailbox, $real_box, $delimeter, $unseen, &$total_new) { |
45 | global $trash_folder, $sent_folder, |
91e0dccc |
46 | $unseen_notify, $newmail_allbox, |
ebe02dfc |
47 | $newmail_recent; |
48 | |
49 | $mailboxURL = urlencode($real_box); |
50 | |
51 | // Skip folders for Sent and Trash |
52 | if ($real_box == $sent_folder || |
53 | $real_box == $trash_folder) { |
973cfba8 |
54 | return 0; |
2d4c15d6 |
55 | } |
4508b1b6 |
56 | |
ebe02dfc |
57 | if (($unseen_notify == 2 && $real_box == 'INBOX') || |
58 | ($unseen_notify == 3 && ($newmail_allbox == 'on' || |
59 | $real_box == 'INBOX'))) { |
60 | $status = sqimap_status_messages( $imapConnection, $real_box); |
61 | if($newmail_recent == 'on') { |
62 | $total_new += $status['RECENT']; |
63 | } else { |
64 | $total_new += $status['UNSEEN']; |
65 | } |
66 | if ($total_new) { |
67 | return 1; |
68 | } |
3d75ef16 |
69 | } |
ebe02dfc |
70 | return 0; |
71 | } |
72 | |
91e0dccc |
73 | /** |
ebe02dfc |
74 | * Init newmail plugin |
75 | */ |
76 | function squirrelmail_plugin_init_newmail() { |
77 | global $squirrelmail_plugin_hooks; |
78 | |
79 | $squirrelmail_plugin_hooks['left_main_before']['newmail'] = 'newmail_plugin'; |
80 | $squirrelmail_plugin_hooks['optpage_register_block']['newmail'] = 'newmail_optpage_register_block'; |
81 | $squirrelmail_plugin_hooks['options_save']['newmail'] = 'newmail_sav'; |
82 | $squirrelmail_plugin_hooks['loading_prefs']['newmail'] = 'newmail_pref'; |
83 | $squirrelmail_plugin_hooks['optpage_set_loadinfo']['newmail'] = 'newmail_set_loadinfo'; |
84 | } |
4508b1b6 |
85 | |
ebe02dfc |
86 | /** |
87 | * Register newmail option block |
88 | */ |
89 | function newmail_optpage_register_block() { |
90 | // Gets added to the user's OPTIONS page. |
91 | global $optpage_blocks; |
92 | |
93 | if ( checkForJavascript() ) { |
94 | /* Register Squirrelspell with the $optionpages array. */ |
95 | $optpage_blocks[] = array( |
96 | 'name' => _("NewMail Options"), |
97 | 'url' => SM_PATH . 'plugins/newmail/newmail_opt.php', |
98 | 'desc' => _("This configures settings for playing sounds and/or showing popup windows when new mail arrives."), |
99 | 'js' => TRUE |
3d75ef16 |
100 | ); |
3d75ef16 |
101 | } |
ebe02dfc |
102 | } |
4508b1b6 |
103 | |
ebe02dfc |
104 | /** |
105 | * Save newmail plugin settings |
106 | */ |
107 | function newmail_sav() { |
108 | global $data_dir, $username; |
109 | |
110 | if ( sqgetGlobalVar('submit_newmail', $submit, SQ_POST) ) { |
111 | $media_enable = ''; |
112 | $media_popup = ''; |
113 | $media_allbox = ''; |
114 | $media_recent = ''; |
115 | $media_changetitle = ''; |
116 | $media_sel = ''; |
117 | |
118 | sqgetGlobalVar('media_enable', $media_enable, SQ_POST); |
119 | sqgetGlobalVar('media_popup', $media_popup, SQ_POST); |
120 | sqgetGlobalVar('media_allbox', $media_allbox, SQ_POST); |
121 | sqgetGlobalVar('media_recent', $media_recent, SQ_POST); |
122 | sqgetGlobalVar('media_changetitle', $media_changetitle, SQ_POST); |
123 | |
124 | setPref($data_dir,$username,'newmail_enable',$media_enable); |
125 | setPref($data_dir,$username,'newmail_popup', $media_popup); |
126 | setPref($data_dir,$username,'newmail_allbox',$media_allbox); |
127 | setPref($data_dir,$username,'newmail_recent',$media_recent); |
128 | setPref($data_dir,$username,'newmail_changetitle',$media_changetitle); |
129 | |
130 | if( sqgetGlobalVar('media_sel', $media_sel, SQ_POST) && |
131 | ($media_sel == '(none)' || $media_sel == '(local media)') ) { |
132 | removePref($data_dir,$username,'newmail_media'); |
133 | } else { |
134 | setPref($data_dir,$username,'newmail_media',$media_sel); |
e697b6cc |
135 | } |
136 | } |
ebe02dfc |
137 | } |
4508b1b6 |
138 | |
ebe02dfc |
139 | /** |
140 | * Load newmail plugin settings |
141 | */ |
142 | function newmail_pref() { |
143 | global $username,$data_dir; |
144 | global $newmail_media,$newmail_enable,$newmail_popup,$newmail_allbox; |
145 | global $newmail_recent, $newmail_changetitle; |
e697b6cc |
146 | |
ebe02dfc |
147 | $newmail_recent = getPref($data_dir,$username,'newmail_recent'); |
148 | $newmail_enable = getPref($data_dir,$username,'newmail_enable'); |
149 | $newmail_media = getPref($data_dir, $username, 'newmail_media', '(none)'); |
150 | $newmail_popup = getPref($data_dir, $username, 'newmail_popup'); |
151 | $newmail_allbox = getPref($data_dir, $username, 'newmail_allbox'); |
152 | $newmail_changetitle = getPref($data_dir, $username, 'newmail_changetitle'); |
153 | } |
2d4c15d6 |
154 | |
ebe02dfc |
155 | /** |
156 | * Set loadinfo data |
157 | * |
158 | * Used by option page when saving settings. |
159 | */ |
160 | function newmail_set_loadinfo() { |
161 | global $optpage, $optpage_name; |
162 | if ($optpage=='newmail') { |
163 | $optpage_name=_("NewMail Options"); |
164 | } |
165 | } |
e697b6cc |
166 | |
ebe02dfc |
167 | /** |
168 | * Insert needed data in left_main |
169 | */ |
170 | function newmail_plugin() { |
171 | global $username, $newmail_media, $newmail_enable, $newmail_popup, |
172 | $newmail_recent, $newmail_changetitle, $imapConnection, $PHP_SELF; |
973cfba8 |
173 | |
ebe02dfc |
174 | if ($newmail_enable == 'on' || |
175 | $newmail_popup == 'on' || |
176 | $newmail_changetitle) { |
e697b6cc |
177 | |
ebe02dfc |
178 | // open a connection on the imap port (143) |
e697b6cc |
179 | |
ebe02dfc |
180 | $boxes = sqimap_mailbox_list($imapConnection); |
181 | $delimeter = sqimap_get_delimiter($imapConnection); |
e697b6cc |
182 | |
ebe02dfc |
183 | $status = 0; |
184 | $totalNew = 0; |
e697b6cc |
185 | |
ebe02dfc |
186 | for ($i = 0;$i < count($boxes); $i++) { |
e697b6cc |
187 | |
ebe02dfc |
188 | $mailbox = $boxes[$i]['formatted']; |
e697b6cc |
189 | |
ebe02dfc |
190 | if (! isset($boxes[$i]['unseen'])) { |
191 | $boxes[$i]['unseen'] = ''; |
192 | } |
193 | if ($boxes[$i]['flags']) { |
194 | $noselect = false; |
195 | for ($h = 0; $h < count($boxes[$i]['flags']); $h++) { |
196 | if (strtolower($boxes[$i]["flags"][$h]) == 'noselect') { |
197 | $noselect = TRUE; |
e697b6cc |
198 | } |
ebe02dfc |
199 | } |
200 | if (! $noselect) { |
91e0dccc |
201 | $status += CheckNewMailboxSound($imapConnection, |
ebe02dfc |
202 | $mailbox, |
91e0dccc |
203 | $boxes[$i]['unformatted'], |
204 | $delimeter, |
ebe02dfc |
205 | $boxes[$i]['unseen'], |
7d931c28 |
206 | $totalNew); |
e697b6cc |
207 | } |
ebe02dfc |
208 | } else { |
91e0dccc |
209 | $status += CheckNewMailboxSound($imapConnection, |
210 | $mailbox, |
ebe02dfc |
211 | $boxes[$i]['unformatted'], |
91e0dccc |
212 | $delimeter, |
213 | $boxes[$i]['unseen'], |
ebe02dfc |
214 | $totalNew); |
e697b6cc |
215 | } |
ebe02dfc |
216 | } |
4508b1b6 |
217 | |
ebe02dfc |
218 | // sqimap_logout($imapConnection); |
e697b6cc |
219 | |
ebe02dfc |
220 | // If we found unseen messages, then we |
221 | // will play the sound as follows: |
222 | |
223 | if ($newmail_changetitle) { |
224 | echo "<script language=\"javascript\">\n" . |
225 | "function ChangeTitleLoad() {\n"; |
226 | echo 'window.parent.document.title = "' . |
91e0dccc |
227 | sprintf(ngettext("%s New Message","%s New Messages",$totalNew), $totalNew) . |
ebe02dfc |
228 | "\";\n"; |
229 | echo "if (BeforeChangeTitle != null)\n". |
230 | "BeforeChangeTitle();\n". |
231 | "}\n". |
232 | "BeforeChangeTitle = window.onload;\n". |
233 | "window.onload = ChangeTitleLoad;\n". |
234 | "</script>\n"; |
235 | } |
236 | |
237 | if ($totalNew > 0 && $newmail_enable == 'on' && $newmail_media != '' ) { |
fd968951 |
238 | /** |
239 | * docs about embed |
240 | * Apple: http://www.apple.com/quicktime/authoring/embed.html |
241 | */ |
ebe02dfc |
242 | echo '<embed src="'.htmlspecialchars($newmail_media) . |
fd968951 |
243 | "\" hidden=\"true\" autostart=\"true\" width=\"2\" height=\"2\">\n"; |
ebe02dfc |
244 | } |
245 | if ($totalNew > 0 && $newmail_popup == 'on') { |
246 | echo "<script language=\"JavaScript\">\n". |
247 | "<!--\n". |
248 | "function PopupScriptLoad() {\n". |
249 | 'window.open("'.sqm_baseuri().'plugins/newmail/newmail.php?numnew='.$totalNew. |
250 | '", "SMPopup",'. |
251 | "\"width=200,height=130,scrollbars=no\");\n". |
252 | "if (BeforePopupScript != null)\n". |
253 | "BeforePopupScript();\n". |
254 | "}\n". |
255 | "BeforePopupScript = window.onload;\n". |
256 | "window.onload = PopupScriptLoad;\n". |
257 | // Idea by: Nic Wolfe (Nic@TimelapseProductions.com) |
258 | // Web URL: http://fineline.xs.mw |
259 | // More code from Tyler Akins |
260 | "// End -->\n". |
261 | "</script>\n"; |
e697b6cc |
262 | } |
263 | } |
ebe02dfc |
264 | } |
6fd95361 |
265 | ?> |