acefda2c3971c979be7b18d3d682ff913e8310a8
[squirrelmail.git] / plugins / newmail / setup.php
1 <?php
2 /**
3 * newmail.php
4 *
5 * Copyright (c) 1999-2005 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 */
28
29 /**
30 * sqm_baseuri function for setups that don't load it by default
31 */
32 include_once(SM_PATH . 'functions/display_messages.php');
33
34 /** Load plugin functions */
35 include_once(SM_PATH . 'plugins/newmail/functions.php');
36
37 /**
38 * Checks if mailbox contains new messages.
39 *
40 * @param object $imapConnection
41 * @param mixed $mailbox FIXME: option is not used
42 * @param string $real_box unformated mailbox name
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,
50 $unseen_notify, $newmail_allbox,
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) {
58 return 0;
59 }
60
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 }
73 }
74 return 0;
75 }
76
77 /**
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 }
89
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
104 );
105 }
106 }
107
108 /**
109 * Save newmail plugin settings
110 */
111 function newmail_sav() {
112 global $data_dir, $username, $_FILES;
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 $popup_width = '';
122 $popup_height = '';
123
124 sqgetGlobalVar('media_enable', $media_enable, SQ_POST);
125 sqgetGlobalVar('media_popup', $media_popup, SQ_POST);
126 sqgetGlobalVar('media_allbox', $media_allbox, SQ_POST);
127 sqgetGlobalVar('media_recent', $media_recent, SQ_POST);
128 sqgetGlobalVar('media_changetitle', $media_changetitle, SQ_POST);
129 sqgetGlobalVar('popup_width', $popup_width, SQ_POST);
130 sqgetGlobalVar('popup_height', $popup_height, SQ_POST);
131
132 // sanitize height and width
133 $popup_width = (int) $popup_width;
134 if ($popup_width<=0) $popup_width=200;
135 $popup_height = (int) $popup_height;
136 if ($popup_height<=0) $popup_height=130;
137
138 setPref($data_dir,$username,'newmail_enable',$media_enable);
139 setPref($data_dir,$username,'newmail_popup', $media_popup);
140 setPref($data_dir,$username,'newmail_allbox',$media_allbox);
141 setPref($data_dir,$username,'newmail_recent',$media_recent);
142 setPref($data_dir,$username,'newmail_changetitle',$media_changetitle);
143 setPref($data_dir,$username,'newmail_popup_width',$popup_width);
144 setPref($data_dir,$username,'newmail_popup_height',$popup_height);
145
146 if( sqgetGlobalVar('media_sel', $media_sel, SQ_POST) &&
147 $media_sel == '(none)' ) {
148 removePref($data_dir,$username,'newmail_media');
149 } else {
150 setPref($data_dir,$username,'newmail_media',$media_sel);
151 }
152
153 // process uploaded file
154 if (isset($_FILES['media_file']['tmp_name']) && $_FILES['media_file']['tmp_name']!='') {
155 // set temp file and get media file name
156 $newmail_tempmedia=getHashedDir($username, $data_dir) . "/$username.tempsound";
157 $newmail_mediafile=getHashedFile($username, $data_dir, $username . '.sound');
158 if (move_uploaded_file($_FILES['media_file']['tmp_name'], $newmail_tempmedia)) {
159 // new media file is in $newmail_tempmedia
160 if (file_exists($newmail_mediafile)) unlink($newmail_mediafile);
161 if (! rename($newmail_tempmedia,$newmail_mediafile)) {
162 // remove (userfile), if file rename fails
163 removePref($data_dir,$username,'newmail_media');
164 } else {
165 // store media type
166 if (isset($_FILES['media_file']['type']) && isset($_FILES['media_file']['name'])) {
167 setPref($data_dir,$username,'newmail_userfile_type',
168 newmail_get_mediatype($_FILES['media_file']['type'],$_FILES['media_file']['name']));
169 } else {
170 removePref($data_dir,$username,'newmail_userfile_type');
171 }
172 // store file name
173 if (isset($_FILES['media_file']['name'])) {
174 setPref($data_dir,$username,'newmail_userfile_name',basename($_FILES['media_file']['name']));
175 } else {
176 setPref($data_dir,$username,'newmail_userfile_name','mediafile.unknown');
177 }
178
179 }
180 }
181 }
182 }
183 }
184
185 /**
186 * Load newmail plugin settings
187 */
188 function newmail_pref() {
189 global $username,$data_dir;
190 global $newmail_media,$newmail_enable,$newmail_popup,$newmail_allbox;
191 global $newmail_recent, $newmail_changetitle;
192 global $newmail_userfile_type, $newmail_userfile_name;
193 global $newmail_popup_width, $newmail_popup_height;
194
195 $newmail_recent = getPref($data_dir,$username,'newmail_recent');
196 $newmail_enable = getPref($data_dir,$username,'newmail_enable');
197 $newmail_media = getPref($data_dir, $username, 'newmail_media', '(none)');
198 $newmail_popup = getPref($data_dir, $username, 'newmail_popup');
199 $newmail_popup_width = getPref($data_dir, $username, 'newmail_popup_width',200);
200 $newmail_popup_height = getPref($data_dir, $username, 'newmail_popup_height',130);
201 $newmail_allbox = getPref($data_dir, $username, 'newmail_allbox');
202 $newmail_changetitle = getPref($data_dir, $username, 'newmail_changetitle');
203
204 $newmail_userfile_type = getPref($data_dir, $username, 'newmail_userfile_type');
205 $newmail_userfile_name = getPref($data_dir,$username,'newmail_userfile_name','');
206 }
207
208 /**
209 * Set loadinfo data
210 *
211 * Used by option page when saving settings.
212 */
213 function newmail_set_loadinfo() {
214 global $optpage, $optpage_name;
215 if ($optpage=='newmail') {
216 $optpage_name=_("NewMail Options");
217 }
218 }
219
220 /**
221 * Insert needed data in left_main
222 */
223 function newmail_plugin() {
224 global $username, $newmail_media, $newmail_enable, $newmail_popup,
225 $newmail_recent, $newmail_changetitle, $imapConnection, $PHP_SELF;
226 global $newmail_mmedia, $newmail_allowsound;
227 global $newmail_userfile_type;
228 global $newmail_popup_width, $newmail_popup_height;
229
230 if ($newmail_enable == 'on' ||
231 $newmail_popup == 'on' ||
232 $newmail_changetitle) {
233
234 // open a connection on the imap port (143)
235
236 $boxes = sqimap_mailbox_list($imapConnection);
237 $delimeter = sqimap_get_delimiter($imapConnection);
238
239 $status = 0;
240 $totalNew = 0;
241
242 for ($i = 0;$i < count($boxes); $i++) {
243
244 $mailbox = $boxes[$i]['formatted'];
245
246 if (! isset($boxes[$i]['unseen'])) {
247 $boxes[$i]['unseen'] = '';
248 }
249 if ($boxes[$i]['flags']) {
250 $noselect = false;
251 for ($h = 0; $h < count($boxes[$i]['flags']); $h++) {
252 if (strtolower($boxes[$i]["flags"][$h]) == 'noselect') {
253 $noselect = TRUE;
254 }
255 }
256 if (! $noselect) {
257 $status += CheckNewMailboxSound($imapConnection,
258 $mailbox,
259 $boxes[$i]['unformatted'],
260 $delimeter,
261 $boxes[$i]['unseen'],
262 $totalNew);
263 }
264 } else {
265 $status += CheckNewMailboxSound($imapConnection,
266 $mailbox,
267 $boxes[$i]['unformatted'],
268 $delimeter,
269 $boxes[$i]['unseen'],
270 $totalNew);
271 }
272 }
273
274 // sqimap_logout($imapConnection);
275
276 // If we found unseen messages, then we
277 // will play the sound as follows:
278
279 if ($newmail_changetitle) {
280 echo "<script language=\"javascript\" type=\"text/javascript\">\n" .
281 "function ChangeTitleLoad() {\n";
282 echo 'window.parent.document.title = "' .
283 sprintf(ngettext("%s New Message","%s New Messages",$totalNew), $totalNew) .
284 "\";\n";
285 echo "if (BeforeChangeTitle != null)\n".
286 "BeforeChangeTitle();\n".
287 "}\n".
288 "BeforeChangeTitle = window.onload;\n".
289 "window.onload = ChangeTitleLoad;\n".
290 "</script>\n";
291 }
292
293 // create media output if there are new email messages
294 if ($newmail_allowsound && $totalNew > 0 && $newmail_enable == 'on' && $newmail_media != '' ) {
295 echo newmail_create_media_tags($newmail_media);
296 }
297
298 if ($totalNew > 0 && $newmail_popup == 'on') {
299 // Idea by: Nic Wolfe (Nic@TimelapseProductions.com)
300 // Web URL: http://fineline.xs.mw
301 // More code from Tyler Akins
302 echo "<script language=\"JavaScript\">\n".
303 "<!--\n".
304 "function PopupScriptLoad() {\n".
305 'window.open("'.sqm_baseuri().'plugins/newmail/newmail.php?numnew='.$totalNew.
306 '", "SMPopup",'.
307 "\"width=$newmail_popup_width,height=$newmail_popup_height,scrollbars=no\");\n".
308 "if (BeforePopupScript != null)\n".
309 "BeforePopupScript();\n".
310 "}\n".
311 "BeforePopupScript = window.onload;\n".
312 "window.onload = PopupScriptLoad;\n".
313 "// End -->\n".
314 "</script>\n";
315 }
316 }
317 }
318 ?>