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_link_and_description']['newmail'] = 'newmail_options'; |
65 | $squirrelmail_plugin_hooks['options_save']['newmail'] = 'newmail_sav'; |
66 | $squirrelmail_plugin_hooks['loading_prefs']['newmail'] = 'newmail_pref'; |
67 | } |
4508b1b6 |
68 | |
cbe5423b |
69 | function newmail_optpage_register_block() { |
3d75ef16 |
70 | // Gets added to the user's OPTIONS page. |
cbe5423b |
71 | global $optpage_blocks; |
4508b1b6 |
72 | |
3d75ef16 |
73 | if ( !soupNazi() ) { |
4508b1b6 |
74 | |
3d75ef16 |
75 | /* Register Squirrelspell with the $optionpages array. */ |
cbe5423b |
76 | $optpage_blocks[] = array( |
3d75ef16 |
77 | 'name' => _("NewMail Options"), |
78 | 'url' => '../plugins/newmail/newmail_opt.php', |
79 | 'desc' => _("This configures settings for playing sounds and/or showing popup windows when new mail arrives."), |
80 | 'js' => TRUE |
81 | ); |
82 | } |
83 | } |
4508b1b6 |
84 | |
85 | function newmail_sav() { |
86 | |
87 | global $username,$data_dir; |
88 | global $submit_newmail,$media_file,$media_reset,$media_enable,$media_popup; |
89 | global $media_recent,$media_sel; |
90 | global $media_allbox, $media_changetitle; |
91 | |
92 | if ($submit_newmail) { |
93 | if(isset($media_enable)) { |
94 | setPref($data_dir,$username,"newmail_enable",$media_enable); |
95 | } else { |
96 | setPref($data_dir,$username,"newmail_enable",""); |
97 | } |
98 | if(isset($media_popup)) { |
99 | setPref($data_dir,$username,"newmail_popup",$media_popup); |
100 | } else { |
101 | setPref($data_dir,$username,"newmail_popup",""); |
102 | } |
103 | if(isset($media_allbox)) { |
104 | setPref($data_dir,$username,"newmail_allbox",$media_allbox); |
105 | } else { |
106 | setPref($data_dir,$username,"newmail_allbox",""); |
107 | } |
108 | if(isset($media_recent)) { |
109 | setPref($data_dir,$username,"newmail_recent",$media_recent); |
110 | } else { |
111 | setPref($data_dir,$username,"newmail_recent",""); |
112 | } |
113 | if(isset($media_changetitle)) { |
114 | setPref($data_dir,$username,"newmail_changetitle",$media_changetitle); |
115 | } else { |
116 | setPref($data_dir,$username,"newmail_changetitle",""); |
117 | } |
118 | if(isset($media_sel)) { |
119 | if($media_sel == "(local media)") { |
120 | setPref($data_dir,$username,"newmail_media",StripSlashes($media_file)); |
121 | } else { |
122 | setPref($data_dir,$username,"newmail_media",$media_sel); |
123 | } |
124 | } else { |
125 | setPref($data_dir,$username,"newmail_media",""); |
126 | } |
2d4c15d6 |
127 | echo '<center> ' . _("New Mail Notification options saved") . '</center>'; |
4508b1b6 |
128 | } |
129 | } |
130 | |
2d4c15d6 |
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'); |
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 | if ($newmail_media == '') { |
145 | $newmail_media = '../plugins/newmail/sounds/Notify.wav'; |
146 | } |
147 | |
148 | } |
4508b1b6 |
149 | |
150 | function newmail_plugin() { |
151 | |
152 | global $username,$key,$imapServerAddress,$imapPort; |
153 | global $newmail_media,$newmail_enable,$newmail_popup,$newmail_recent; |
154 | global $newmail_changetitle; |
2d4c15d6 |
155 | global $imapConnection; |
4508b1b6 |
156 | |
2d4c15d6 |
157 | if ($newmail_enable == 'on' || $newmail_popup == 'on' || $newmail_changetitle) { |
4508b1b6 |
158 | |
159 | // open a connection on the imap port (143) |
160 | |
2d4c15d6 |
161 | // $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10); // the 10 is to hide the output |
4508b1b6 |
162 | |
163 | $boxes = sqimap_mailbox_list($imapConnection); |
164 | $delimeter = sqimap_get_delimiter($imapConnection); |
165 | |
166 | $status = 0; |
167 | $totalNew = 0; |
168 | |
169 | for ($i = 0;$i < count($boxes); $i++) { |
2d4c15d6 |
170 | |
171 | $line = ''; |
172 | $mailbox = $boxes[$i]['formatted']; |
4508b1b6 |
173 | |
174 | if (! isset($boxes[$i]['unseen'])) |
175 | $boxes[$i]['unseen'] = ''; |
2d4c15d6 |
176 | if ($boxes[$i]['flags']) { |
4508b1b6 |
177 | $noselect = false; |
2d4c15d6 |
178 | for ($h = 0; $h < count($boxes[$i]['flags']); $h++) { |
179 | if (strtolower($boxes[$i]["flags"][$h]) == 'noselect') |
4508b1b6 |
180 | $noselect = true; |
181 | } |
182 | if (! $noselect) { |
183 | $status = $status + CheckNewMailboxSound($imapConnection, $mailbox, |
2d4c15d6 |
184 | $boxes[$i]['unformatted'], $delimeter, $boxes[$i]['unseen'], |
4508b1b6 |
185 | $totalNew); |
186 | } |
187 | } else { |
2d4c15d6 |
188 | $status = $status + CheckNewMailboxSound($imapConnection, $mailbox, $boxes[$i]['unformatted'], |
189 | $delimeter, $boxes[$i]['unseen'], $totalNew); |
4508b1b6 |
190 | } |
191 | |
192 | } |
2d4c15d6 |
193 | |
194 | // sqimap_logout($imapConnection); |
4508b1b6 |
195 | |
196 | // If we found unseen messages, then we |
197 | // will play the sound as follows: |
198 | |
2d4c15d6 |
199 | if ($newmail_changetitle) { |
200 | echo "<script language=\"javascript\">\n" . |
201 | "function ChangeTitleLoad() {\n"; |
202 | if( $totalNew > 1 ) { |
203 | echo 'window.parent.document.title = "' . sprintf(_("%s New Messages"), $totalNew ) . "\";\n"; |
204 | }else { |
205 | echo 'window.parent.document.title = "' . sprintf(_("%s New Message"), $totalNew ) . "\";\n"; |
206 | } |
207 | echo "if (BeforeChangeTitle != null)\n". |
208 | "BeforeChangeTitle();\n". |
209 | "}\n". |
210 | "BeforeChangeTitle = window.onload;\n". |
211 | "window.onload = ChangeTitleLoad;\n". |
212 | "</script>\n"; |
213 | } |
214 | |
215 | if ($status > 0 && $newmail_enable == 'on') { |
216 | echo "<EMBED SRC=\"$newmail_media\" HIDDEN=TRUE AUTOSTART=TRUE>"; |
217 | } |
218 | if ($status >0 && $newmail_popup == 'on') { |
4508b1b6 |
219 | ?> |
220 | <SCRIPT LANGUAGE="JavaScript"> |
221 | <!-- |
222 | function PopupScriptLoad() { |
223 | window.open("../plugins/newmail/newmail.php", "SMPopup", |
224 | "width=200,height=130,scrollbars=no"); |
225 | if (BeforePopupScript != null) |
226 | BeforePopupScript(); |
227 | } |
228 | BeforePopupScript = window.onload; |
229 | window.onload = PopupScriptLoad; |
230 | |
231 | // Idea by: Nic Wolfe (Nic@TimelapseProductions.com) |
232 | // Web URL: http://fineline.xs.mw |
233 | // More code from Tyler Akins |
234 | // End --> |
235 | </script> |
236 | <?php |
237 | |
238 | } |
239 | } |
240 | } |
cbe5423b |
241 | ?> |