looks like opera implemented it according to apple specs and height/width
[squirrelmail.git] / plugins / newmail / setup.php
1 <?php
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 */
28
29 /**
30 */
31 include_once(SM_PATH . 'functions/display_messages.php');
32
33 /**
34 * Checks if mailbox contains new messages.
35 *
36 * @param object $imapConnection
37 * @param mixed $mailbox FIXME: option is not used
38 * @param string $real_box unformated mailbox name
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,
46 $unseen_notify, $newmail_allbox,
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) {
54 return 0;
55 }
56
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 }
69 }
70 return 0;
71 }
72
73 /**
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 }
85
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
100 );
101 }
102 }
103
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);
135 }
136 }
137 }
138
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;
146
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 }
154
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 }
166
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;
173
174 if ($newmail_enable == 'on' ||
175 $newmail_popup == 'on' ||
176 $newmail_changetitle) {
177
178 // open a connection on the imap port (143)
179
180 $boxes = sqimap_mailbox_list($imapConnection);
181 $delimeter = sqimap_get_delimiter($imapConnection);
182
183 $status = 0;
184 $totalNew = 0;
185
186 for ($i = 0;$i < count($boxes); $i++) {
187
188 $mailbox = $boxes[$i]['formatted'];
189
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;
198 }
199 }
200 if (! $noselect) {
201 $status += CheckNewMailboxSound($imapConnection,
202 $mailbox,
203 $boxes[$i]['unformatted'],
204 $delimeter,
205 $boxes[$i]['unseen'],
206 $totalNew);
207 }
208 } else {
209 $status += CheckNewMailboxSound($imapConnection,
210 $mailbox,
211 $boxes[$i]['unformatted'],
212 $delimeter,
213 $boxes[$i]['unseen'],
214 $totalNew);
215 }
216 }
217
218 // sqimap_logout($imapConnection);
219
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 = "' .
227 sprintf(ngettext("%s New Message","%s New Messages",$totalNew), $totalNew) .
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 != '' ) {
238 /**
239 * docs about embed
240 * Apple: http://www.apple.com/quicktime/authoring/embed.html
241 */
242 echo '<embed src="'.htmlspecialchars($newmail_media) .
243 "\" hidden=\"true\" autostart=\"true\" width=\"2\" height=\"2\">\n";
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";
262 }
263 }
264 }
265 ?>