Completely cleanup the folders management (create/subscribe etc) code.
[squirrelmail.git] / src / folders.php
1 <?php
2
3 /**
4 * folders.php
5 *
6 * Copyright (c) 1999-2005 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Handles all interaction between the user and the other folder
10 * scripts which do most of the work. Also handles the Special
11 * Folders.
12 *
13 * @version $Id$
14 * @package squirrelmail
15 */
16
17 /**
18 * Path for SquirrelMail required files.
19 * @ignore
20 */
21 define('SM_PATH','../');
22
23 /* SquirrelMail required files. */
24 require_once(SM_PATH . 'include/validate.php');
25 require_once(SM_PATH . 'functions/imap.php');
26 require_once(SM_PATH . 'functions/folder_manip.php');
27 require_once(SM_PATH . 'functions/plugin.php');
28 require_once(SM_PATH . 'functions/html.php');
29 require_once(SM_PATH . 'functions/forms.php');
30
31 displayPageHeader($color, 'None');
32
33 /* get globals we may need */
34
35 sqgetGlobalVar('username', $username, SQ_SESSION);
36 sqgetGlobalVar('key', $key, SQ_COOKIE);
37 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
38 sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
39
40 sqgetGlobalVar('smaction', $action, SQ_POST);
41
42 /* end of get globals */
43
44 echo '<br />' .
45 html_tag( 'table', '', 'center', $color[0], 'width="95%" cellpadding="1" cellspacing="0" border="0"' ) .
46 html_tag( 'tr' ) .
47 html_tag( 'td', '', 'center' ) . '<b>' . _("Folders") . '</b>' .
48 html_tag( 'table', '', 'center', '', 'width="100%" cellpadding="5" cellspacing="0" border="0"' ) .
49 html_tag( 'tr' ) .
50 html_tag( 'td', '', 'center', $color[4] );
51
52 $imapConnection = sqimap_login ($username, $key, $imapServerAddress, $imapPort, 0);
53
54 /* switch to the right function based on what the user selected */
55 if ( sqgetGlobalVar('smaction', $action, SQ_POST) ) {
56
57 switch ($action)
58 {
59 case 'create':
60 sqgetGlobalVar('folder_name', $folder_name, SQ_POST);
61 sqgetGlobalVar('subfolder', $subfolder, SQ_POST);
62 sqgetGlobalVar('contain_subs', $contain_subs, SQ_POST);
63 folders_create($imapConnection, $delimiter, $folder_name, $subfolder, $contain_subs);
64 $td_str = _("Created folder successfully.");
65 break;
66 case 'rename':
67 if ( sqgetGlobalVar('cancelbutton', $dummy, SQ_POST) ) {
68 break;
69 }
70 if ( ! sqgetGlobalVar('new_name', $new_name, SQ_POST) ) {
71 sqgetGlobalVar('old_name', $old_name, SQ_POST);
72 folders_rename_getname($imapConnection, $delimiter, $old_name);
73 } else {
74 sqgetGlobalVar('orig', $orig, SQ_POST);
75 sqgetGlobalVar('old_name', $old_name, SQ_POST);
76 folders_rename_do($imapConnection, $delimiter, $orig, $old_name, $new_name);
77 $td_str = _("Renamed successfully!");
78 }
79 break;
80 case 'delete':
81 if ( sqgetGlobalVar('cancelbutton', $dummy, SQ_POST) ) {
82 break;
83 }
84 sqgetGlobalVar('folder_name', $folder_name, SQ_POST);
85 if ( sqgetGlobalVar('confirmed', $dummy, SQ_POST) ) {
86 folders_delete_do($imapConnection, $delimiter, $folder_name);
87 $td_str = _("Deleted folder successfully.");
88 } else {
89 folders_delete_ask($imapConnection, $folder_name);
90 }
91 break;
92 case 'subscribe':
93 sqgetGlobalVar('folder_names', $folder_names, SQ_POST);
94 folders_subscribe($imapConnection, $folder_names);
95 $td_str = _("Subscribed successfully.");
96 break;
97 case 'unsubscribe':
98 sqgetGlobalVar('folder_names', $folder_names, SQ_POST);
99 folders_unsubscribe($imapConnection, $folder_names);
100 $td_str = _("Unsubscribed successfully.");
101 break;
102 }
103
104 echo html_tag( 'table',
105 html_tag( 'tr',
106 html_tag( 'td', '<b>' . $td_str . "</b><br />\n" .
107 '<a href="../src/left_main.php" target="left">' .
108 _("refresh folder list") . '</a>' ,
109 'center' )
110 ) ,
111 'center', '', 'width="100%" cellpadding="4" cellspacing="0" border="0"' );
112 }
113
114 echo "\n<br />";
115
116 $boxes = sqimap_mailbox_list($imapConnection,true);
117
118 /** CREATING FOLDERS **/
119 echo html_tag( 'table', '', 'center', '', 'width="70%" cellpadding="4" cellspacing="0" border="0"' ) .
120 html_tag( 'tr',
121 html_tag( 'td', '<b>' . _("Create Folder") . '</b>', 'center', $color[9] )
122 ) .
123 html_tag( 'tr' ) .
124 html_tag( 'td', '', 'center', $color[0] ) .
125 addForm('folders.php', 'post', 'cf').
126 addHidden('smaction','create').
127 addInput('folder_name', '', 25).
128 "<br />\n". _("as a subfolder of"). '<br />'.
129 "<tt><select name=\"subfolder\">\n";
130
131 $show_selected = array();
132 $skip_folders = array();
133 $server_type = strtolower($imap_server_type);
134 if ( $server_type == 'courier' ) {
135 array_push($skip_folders, 'inbox.trash');
136 if ( $default_folder_prefix == 'INBOX.' ) {
137 array_push($skip_folders, 'INBOX');
138 }
139 }
140
141 if ( $default_sub_of_inbox == false ) {
142 echo '<option selected="selected" value="">[ '._("None")." ]</option>\n";
143 } else {
144 echo '<option value="">[ '._("None")." ]</option>\n";
145 $show_selected = array('inbox');
146 }
147
148 // Call sqimap_mailbox_option_list, using existing connection to IMAP server,
149 // the arrays of folders to include or skip (assembled above),
150 // use 'noinferiors' as a mailbox filter to leave out folders that can not contain other folders.
151 // use the long format to show subfolders in an intelligible way if parent is missing (special folder)
152 echo sqimap_mailbox_option_list($imapConnection, $show_selected, $skip_folders, $boxes, 'noinferiors', true);
153
154 echo "</select></tt>\n";
155 if ($show_contain_subfolders_option) {
156 echo '<br />'.
157 addCheckBox('contain_subs', FALSE, '1') .' &nbsp;'
158 . _("Let this folder contain subfolders")
159 . '<br />';
160 }
161 echo "<input type=\"submit\" value=\""._("Create")."\" />\n";
162 echo "</form></td></tr>\n";
163
164 echo html_tag( 'tr',
165 html_tag( 'td', '&nbsp;', 'left', $color[4] )
166 ) ."\n";
167
168 /** count special folders **/
169
170 // FIX ME, why not check if the folders are defined IMHO move_to_sent, move_to_trash has nothing todo with it
171 $count_special_folders = 0;
172 $num_max = 1;
173 if (strtolower($imap_server_type) == "courier" || $move_to_trash) {
174 $num_max++;
175 }
176 if ($move_to_sent) {
177 $num_max++;
178 }
179 if ($save_as_draft) {
180 $num_max++;
181 }
182
183 // What if move_to_sent = false and $sent_folder is set? Should it still be skipped?
184
185 for ($p = 0, $cnt = count($boxes); $p < $cnt && $count_special_folders < $num_max; $p++) {
186 switch ($boxes[$p]['unformatted']) {
187 case (strtoupper($boxes[$p]['unformatted']) == 'INBOX'):
188 ++$count_special_folders;
189 $skip_folders[] = $boxes[$p]['unformatted'];
190 break;
191 // FIX ME inbox.trash should be set in conf.pl
192 case 'inbox.trash':
193 if (strtolower($imap_server_type) == 'courier') {
194 ++$count_special_folders;
195 }
196 break;
197 case $trash_folder:
198 ++$count_special_folders;
199 $skip_folders[] = $trash_folder;
200 break;
201 case $sent_folder:
202 ++$count_special_folders;
203 $skip_folders[] = $sent_folder;
204 break;
205 case $draft_folder:
206 ++$count_special_folders;
207 $skip_folders[] = $draft_folder;
208 break;
209 default: break;
210 }
211 }
212
213
214 /** RENAMING FOLDERS **/
215 echo html_tag( 'tr',
216 html_tag( 'td', '<b>' . _("Rename a Folder") . '</b>', 'center', $color[9] )
217 ) .
218 html_tag( 'tr' ) .
219 html_tag( 'td', '', 'center', $color[0] );
220
221 /* show only if we have folders to rename */
222 if ($count_special_folders < count($boxes)) {
223 echo addForm('folders.php')
224 . addHidden('smaction', 'rename')
225 . "<tt><select name=\"old_name\">\n"
226 . ' <option value="">[ ' . _("Select a folder") . " ]</option>\n";
227
228 // use existing IMAP connection, we have no special values to show,
229 // but we do include values to skip. Use the pre-created $boxes to save an IMAP query.
230 // send NULL for the flag - ALL folders are eligible for rename!
231 // use long format to make sure folder names make sense when parents may be missing.
232 echo sqimap_mailbox_option_list($imapConnection, 0, $skip_folders, $boxes, NULL, true);
233
234 echo "</select></tt>\n".
235 '<input type="submit" value="'.
236 _("Rename").
237 "\" />\n".
238 "</form></td></tr>\n";
239 } else {
240 echo _("No folders found") . '<br /><br /></td></tr>';
241 }
242
243 echo html_tag( 'tr',
244 html_tag( 'td', '&nbsp;', 'left', $color[4] )
245 ) ."\n";
246
247 /** DELETING FOLDERS **/
248 echo html_tag( 'tr',
249 html_tag( 'td', '<b>' . _("Delete Folder") . '</b>', 'center', $color[9] )
250 ) .
251 html_tag( 'tr' ) .
252 html_tag( 'td', '', 'center', $color[0] );
253
254 /* show only if we have folders to delete */
255 if ($count_special_folders < count($boxes)) {
256 echo addForm('folders.php')
257 . addHidden('smaction', 'delete')
258 . "<tt><select name=\"folder_name\">\n"
259 . ' <option value="">[ ' . _("Select a folder") . " ]</option>\n";
260
261 // send NULL for the flag - ALL folders are eligible for delete (except what we've got in skiplist)
262 // use long format to make sure folder names make sense when parents may be missing.
263 echo sqimap_mailbox_option_list($imapConnection, 0, $skip_folders, $boxes, NULL, true);
264
265 echo "</select></tt>\n"
266 . '<input type="submit" value="'
267 . _("Delete")
268 . "\" />\n"
269 . "</form></td></tr>\n";
270 } else {
271 echo _("No folders found") . "<br /><br /></td></tr>";
272 }
273
274 echo html_tag( 'tr',
275 html_tag( 'td', '&nbsp;', 'left', $color[4] )
276 ) ."</table>\n";
277
278
279 if ($show_only_subscribed_folders) {
280
281 /** UNSUBSCRIBE FOLDERS **/
282 echo html_tag( 'table', '', 'center', '', 'width="70%" cellpadding="4" cellspacing="0" border="0"' ) .
283 html_tag( 'tr',
284 html_tag( 'td', '<b>' . _("Unsubscribe") . '/' . _("Subscribe") . '</b>', 'center', $color[9], 'colspan="2"' )
285 ) .
286 html_tag( 'tr' ) .
287 html_tag( 'td', '', 'center', $color[0], 'width="50%"' );
288
289 if ($count_special_folders < count($boxes)) {
290 echo addForm('folders.php')
291 . addHidden('smaction', 'unsubscribe')
292 . "<tt><select name=\"folder_names[]\" multiple=\"multiple\" size=\"8\">\n";
293 foreach ( $boxes as $box ) {
294 $use_folder = true;
295 if ((strtolower($box["unformatted"]) != "inbox") &&
296 ($box['unformatted'] != $trash_folder) &&
297 ($box['unformatted'] != $sent_folder) &&
298 ($box['unformatted'] != $draft_folder)) {
299 $box_enc = htmlspecialchars($box['unformatted-dm']);
300 $box_disp = str_replace(' ', '&nbsp;',
301 htmlspecialchars(imap_utf7_decode_local($box["unformatted-disp"])));
302 echo " <option value=\"$box_enc\">$box_disp</option>\n";
303 }
304 }
305 echo "</select></tt><br /><br />\n"
306 . '<input type="submit" value="'
307 . _("Unsubscribe")
308 . "\" />\n"
309 . "</form></td>\n";
310 } else {
311 echo _("No folders were found to unsubscribe from!") . '</td>';
312 }
313
314 /** SUBSCRIBE TO FOLDERS **/
315 echo html_tag( 'td', '', 'center', $color[0], 'width="50%"' );
316 if(!$no_list_for_subscribe) {
317 $boxes_all = sqimap_mailbox_list_all ($imapConnection);
318
319 $subboxes = array();
320 // here we filter out all boxes we're already subscribed to,
321 // so we keep only the unsubscribed ones.
322 foreach ($boxes_all as $box_a) {
323
324 $use_folder = true;
325 foreach ( $boxes as $box ) {
326 if ($box_a['unformatted'] == $box['unformatted'] ||
327 $box_a['unformatted-dm'] == $folder_prefix ) {
328 $use_folder = false;
329 }
330 }
331
332 if ($use_folder == true) {
333 $box_enc = htmlspecialchars($box_a['unformatted-dm']);
334 $box_disp = htmlspecialchars(imap_utf7_decode_local($box_a['unformatted-disp']));
335 $subboxes[$box_enc] = $box_disp;
336 }
337 }
338
339 if ( count($subboxes) > 0 ) {
340 echo addForm('folders.php')
341 . addHidden('smaction', 'subscribe')
342 . '<tt><select name="folder_names[]" multiple="multiple" size="8">';
343
344 foreach($subboxes as $subbox_enc => $subbox_disp) {
345 echo ' <option value="' . $subbox_enc . '">'.$subbox_disp."</option>\n";
346 }
347
348 echo '</select></tt><br /><br />'
349 . '<input type="submit" value="'. _("Subscribe") . "\" />\n"
350 . "</form></td></tr></table><br />\n";
351 } else {
352 echo _("No folders were found to subscribe to.") . '</td></tr></table>';
353 }
354 } else {
355 /* don't perform the list action -- this is much faster */
356 echo addForm('folders.php')
357 . addHidden('smaction', 'subscribe')
358 . _("Subscribe to:") . '<br />'
359 . '<tt><input type="text" name="folder_names[]" size="35" />'
360 . '<input type="submit" value="'. _("Subscribe") . "\" />\n"
361 . "</form></td></tr></table><br />\n";
362 }
363 }
364
365 do_hook('folders_bottom');
366 sqimap_logout($imapConnection);
367 ?>
368 </td></tr>
369 </table>
370 </td></tr>
371 </table>
372 </body></html>