4508b1b6 |
1 | <?php |
ebe02dfc |
2 | /** |
3 | * newmail.php |
4 | * |
6c84ba1e |
5 | * Copyright (c) 1999-2005 The SquirrelMail Project Team |
ebe02dfc |
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 | /** |
dbc7cd0a |
30 | * sqm_baseuri function for setups that don't load it by default |
ebe02dfc |
31 | */ |
32 | include_once(SM_PATH . 'functions/display_messages.php'); |
4508b1b6 |
33 | |
dbc7cd0a |
34 | /** Load plugin functions */ |
35 | include_once(SM_PATH . 'plugins/newmail/functions.php'); |
36 | |
ebe02dfc |
37 | /** |
38 | * Checks if mailbox contains new messages. |
39 | * |
40 | * @param object $imapConnection |
41 | * @param mixed $mailbox FIXME: option is not used |
91e0dccc |
42 | * @param string $real_box unformated mailbox name |
ebe02dfc |
43 | * @param mixed $delimeter FIXME: option is not used |
44 | * @param string $unseen FIXME: option is not used |
45 | * @param integer $total_new number of new messages |
46 | * @return bool true, if there are new messages |
47 | */ |
48 | function CheckNewMailboxSound($imapConnection, $mailbox, $real_box, $delimeter, $unseen, &$total_new) { |
49 | global $trash_folder, $sent_folder, |
91e0dccc |
50 | $unseen_notify, $newmail_allbox, |
ebe02dfc |
51 | $newmail_recent; |
52 | |
53 | $mailboxURL = urlencode($real_box); |
54 | |
55 | // Skip folders for Sent and Trash |
56 | if ($real_box == $sent_folder || |
57 | $real_box == $trash_folder) { |
973cfba8 |
58 | return 0; |
2d4c15d6 |
59 | } |
4508b1b6 |
60 | |
ebe02dfc |
61 | if (($unseen_notify == 2 && $real_box == 'INBOX') || |
62 | ($unseen_notify == 3 && ($newmail_allbox == 'on' || |
63 | $real_box == 'INBOX'))) { |
64 | $status = sqimap_status_messages( $imapConnection, $real_box); |
65 | if($newmail_recent == 'on') { |
66 | $total_new += $status['RECENT']; |
67 | } else { |
68 | $total_new += $status['UNSEEN']; |
69 | } |
70 | if ($total_new) { |
71 | return 1; |
72 | } |
3d75ef16 |
73 | } |
ebe02dfc |
74 | return 0; |
75 | } |
76 | |
91e0dccc |
77 | /** |
ebe02dfc |
78 | * Init newmail plugin |
79 | */ |
80 | function squirrelmail_plugin_init_newmail() { |
81 | global $squirrelmail_plugin_hooks; |
82 | |
83 | $squirrelmail_plugin_hooks['left_main_before']['newmail'] = 'newmail_plugin'; |
84 | $squirrelmail_plugin_hooks['optpage_register_block']['newmail'] = 'newmail_optpage_register_block'; |
85 | $squirrelmail_plugin_hooks['options_save']['newmail'] = 'newmail_sav'; |
86 | $squirrelmail_plugin_hooks['loading_prefs']['newmail'] = 'newmail_pref'; |
87 | $squirrelmail_plugin_hooks['optpage_set_loadinfo']['newmail'] = 'newmail_set_loadinfo'; |
88 | } |
4508b1b6 |
89 | |
ebe02dfc |
90 | /** |
91 | * Register newmail option block |
92 | */ |
93 | function newmail_optpage_register_block() { |
94 | // Gets added to the user's OPTIONS page. |
95 | global $optpage_blocks; |
96 | |
97 | if ( checkForJavascript() ) { |
98 | /* Register Squirrelspell with the $optionpages array. */ |
99 | $optpage_blocks[] = array( |
100 | 'name' => _("NewMail Options"), |
101 | 'url' => SM_PATH . 'plugins/newmail/newmail_opt.php', |
102 | 'desc' => _("This configures settings for playing sounds and/or showing popup windows when new mail arrives."), |
103 | 'js' => TRUE |
3d75ef16 |
104 | ); |
3d75ef16 |
105 | } |
ebe02dfc |
106 | } |
4508b1b6 |
107 | |
ebe02dfc |
108 | /** |
109 | * Save newmail plugin settings |
110 | */ |
111 | function newmail_sav() { |
dbc7cd0a |
112 | global $data_dir, $username, $_FILES; |
ebe02dfc |
113 | |
114 | if ( sqgetGlobalVar('submit_newmail', $submit, SQ_POST) ) { |
115 | $media_enable = ''; |
116 | $media_popup = ''; |
117 | $media_allbox = ''; |
118 | $media_recent = ''; |
119 | $media_changetitle = ''; |
120 | $media_sel = ''; |
121 | |
122 | sqgetGlobalVar('media_enable', $media_enable, SQ_POST); |
123 | sqgetGlobalVar('media_popup', $media_popup, SQ_POST); |
124 | sqgetGlobalVar('media_allbox', $media_allbox, SQ_POST); |
125 | sqgetGlobalVar('media_recent', $media_recent, SQ_POST); |
126 | sqgetGlobalVar('media_changetitle', $media_changetitle, SQ_POST); |
127 | |
128 | setPref($data_dir,$username,'newmail_enable',$media_enable); |
129 | setPref($data_dir,$username,'newmail_popup', $media_popup); |
130 | setPref($data_dir,$username,'newmail_allbox',$media_allbox); |
131 | setPref($data_dir,$username,'newmail_recent',$media_recent); |
132 | setPref($data_dir,$username,'newmail_changetitle',$media_changetitle); |
133 | |
134 | if( sqgetGlobalVar('media_sel', $media_sel, SQ_POST) && |
dbc7cd0a |
135 | $media_sel == '(none)' ) { |
ebe02dfc |
136 | removePref($data_dir,$username,'newmail_media'); |
137 | } else { |
138 | setPref($data_dir,$username,'newmail_media',$media_sel); |
e697b6cc |
139 | } |
dbc7cd0a |
140 | |
141 | // process uploaded file |
142 | if (isset($_FILES['media_file']['tmp_name']) && $_FILES['media_file']['tmp_name']!='') { |
143 | // set temp file and get media file name |
144 | $newmail_tempmedia=getHashedDir($username, $data_dir) . "/$username.tempsound"; |
145 | $newmail_mediafile=getHashedFile($username, $data_dir, $username . '.sound'); |
146 | if (move_uploaded_file($_FILES['media_file']['tmp_name'], $newmail_tempmedia)) { |
147 | // new media file is in $newmail_tempmedia |
148 | if (file_exists($newmail_mediafile)) unlink($newmail_mediafile); |
149 | if (! rename($newmail_tempmedia,$newmail_mediafile)) { |
150 | // remove (userfile), if file rename fails |
151 | removePref($data_dir,$username,'newmail_media'); |
152 | } else { |
153 | // store media type |
154 | if (isset($_FILES['media_file']['type']) && isset($_FILES['media_file']['name'])) { |
155 | setPref($data_dir,$username,'newmail_userfile_type', |
156 | newmail_get_mediatype($_FILES['media_file']['type'],$_FILES['media_file']['name'])); |
157 | } else { |
158 | removePref($data_dir,$username,'newmail_userfile_type'); |
159 | } |
160 | // store file name |
161 | if (isset($_FILES['media_file']['name'])) { |
162 | setPref($data_dir,$username,'newmail_userfile_name',basename($_FILES['media_file']['name'])); |
163 | } else { |
164 | setPref($data_dir,$username,'newmail_userfile_name','mediafile.unknown'); |
165 | } |
166 | |
167 | } |
168 | } |
169 | } |
e697b6cc |
170 | } |
ebe02dfc |
171 | } |
4508b1b6 |
172 | |
ebe02dfc |
173 | /** |
174 | * Load newmail plugin settings |
175 | */ |
176 | function newmail_pref() { |
177 | global $username,$data_dir; |
178 | global $newmail_media,$newmail_enable,$newmail_popup,$newmail_allbox; |
179 | global $newmail_recent, $newmail_changetitle; |
dbc7cd0a |
180 | global $newmail_userfile_type; |
e697b6cc |
181 | |
ebe02dfc |
182 | $newmail_recent = getPref($data_dir,$username,'newmail_recent'); |
183 | $newmail_enable = getPref($data_dir,$username,'newmail_enable'); |
184 | $newmail_media = getPref($data_dir, $username, 'newmail_media', '(none)'); |
185 | $newmail_popup = getPref($data_dir, $username, 'newmail_popup'); |
186 | $newmail_allbox = getPref($data_dir, $username, 'newmail_allbox'); |
187 | $newmail_changetitle = getPref($data_dir, $username, 'newmail_changetitle'); |
dbc7cd0a |
188 | |
189 | $newmail_userfile_type = getPref($data_dir, $username, 'newmail_userfile_type'); |
ebe02dfc |
190 | } |
2d4c15d6 |
191 | |
ebe02dfc |
192 | /** |
193 | * Set loadinfo data |
194 | * |
195 | * Used by option page when saving settings. |
196 | */ |
197 | function newmail_set_loadinfo() { |
198 | global $optpage, $optpage_name; |
199 | if ($optpage=='newmail') { |
200 | $optpage_name=_("NewMail Options"); |
201 | } |
202 | } |
e697b6cc |
203 | |
ebe02dfc |
204 | /** |
205 | * Insert needed data in left_main |
206 | */ |
207 | function newmail_plugin() { |
208 | global $username, $newmail_media, $newmail_enable, $newmail_popup, |
209 | $newmail_recent, $newmail_changetitle, $imapConnection, $PHP_SELF; |
70e628ef |
210 | global $newmail_mmedia, $newmail_allowsound; |
dbc7cd0a |
211 | global $newmail_userfile_type; |
973cfba8 |
212 | |
ebe02dfc |
213 | if ($newmail_enable == 'on' || |
214 | $newmail_popup == 'on' || |
215 | $newmail_changetitle) { |
e697b6cc |
216 | |
ebe02dfc |
217 | // open a connection on the imap port (143) |
e697b6cc |
218 | |
ebe02dfc |
219 | $boxes = sqimap_mailbox_list($imapConnection); |
220 | $delimeter = sqimap_get_delimiter($imapConnection); |
e697b6cc |
221 | |
ebe02dfc |
222 | $status = 0; |
223 | $totalNew = 0; |
e697b6cc |
224 | |
ebe02dfc |
225 | for ($i = 0;$i < count($boxes); $i++) { |
e697b6cc |
226 | |
ebe02dfc |
227 | $mailbox = $boxes[$i]['formatted']; |
e697b6cc |
228 | |
ebe02dfc |
229 | if (! isset($boxes[$i]['unseen'])) { |
230 | $boxes[$i]['unseen'] = ''; |
231 | } |
232 | if ($boxes[$i]['flags']) { |
233 | $noselect = false; |
234 | for ($h = 0; $h < count($boxes[$i]['flags']); $h++) { |
235 | if (strtolower($boxes[$i]["flags"][$h]) == 'noselect') { |
236 | $noselect = TRUE; |
e697b6cc |
237 | } |
ebe02dfc |
238 | } |
239 | if (! $noselect) { |
91e0dccc |
240 | $status += CheckNewMailboxSound($imapConnection, |
ebe02dfc |
241 | $mailbox, |
91e0dccc |
242 | $boxes[$i]['unformatted'], |
243 | $delimeter, |
ebe02dfc |
244 | $boxes[$i]['unseen'], |
7d931c28 |
245 | $totalNew); |
e697b6cc |
246 | } |
ebe02dfc |
247 | } else { |
91e0dccc |
248 | $status += CheckNewMailboxSound($imapConnection, |
249 | $mailbox, |
ebe02dfc |
250 | $boxes[$i]['unformatted'], |
91e0dccc |
251 | $delimeter, |
252 | $boxes[$i]['unseen'], |
ebe02dfc |
253 | $totalNew); |
e697b6cc |
254 | } |
ebe02dfc |
255 | } |
4508b1b6 |
256 | |
ebe02dfc |
257 | // sqimap_logout($imapConnection); |
e697b6cc |
258 | |
ebe02dfc |
259 | // If we found unseen messages, then we |
260 | // will play the sound as follows: |
261 | |
262 | if ($newmail_changetitle) { |
263 | echo "<script language=\"javascript\">\n" . |
264 | "function ChangeTitleLoad() {\n"; |
265 | echo 'window.parent.document.title = "' . |
91e0dccc |
266 | sprintf(ngettext("%s New Message","%s New Messages",$totalNew), $totalNew) . |
ebe02dfc |
267 | "\";\n"; |
268 | echo "if (BeforeChangeTitle != null)\n". |
269 | "BeforeChangeTitle();\n". |
270 | "}\n". |
271 | "BeforeChangeTitle = window.onload;\n". |
272 | "window.onload = ChangeTitleLoad;\n". |
273 | "</script>\n"; |
274 | } |
275 | |
dbc7cd0a |
276 | // create media output if there are new email messages |
70e628ef |
277 | if ($newmail_allowsound && $totalNew > 0 && $newmail_enable == 'on' && $newmail_media != '' ) { |
dbc7cd0a |
278 | echo newmail_create_media_tags($newmail_media); |
ebe02dfc |
279 | } |
dbc7cd0a |
280 | |
ebe02dfc |
281 | if ($totalNew > 0 && $newmail_popup == 'on') { |
282 | echo "<script language=\"JavaScript\">\n". |
283 | "<!--\n". |
284 | "function PopupScriptLoad() {\n". |
285 | 'window.open("'.sqm_baseuri().'plugins/newmail/newmail.php?numnew='.$totalNew. |
286 | '", "SMPopup",'. |
287 | "\"width=200,height=130,scrollbars=no\");\n". |
288 | "if (BeforePopupScript != null)\n". |
289 | "BeforePopupScript();\n". |
290 | "}\n". |
291 | "BeforePopupScript = window.onload;\n". |
292 | "window.onload = PopupScriptLoad;\n". |
293 | // Idea by: Nic Wolfe (Nic@TimelapseProductions.com) |
294 | // Web URL: http://fineline.xs.mw |
295 | // More code from Tyler Akins |
296 | "// End -->\n". |
297 | "</script>\n"; |
e697b6cc |
298 | } |
299 | } |
ebe02dfc |
300 | } |
6fd95361 |
301 | ?> |