91b426acccc3e6dc9afe2bfcc0dccafdc34f315a
[squirrelmail.git] / src / folders_delete.php
1 <?php
2
3 /**
4 * folders_delete.php
5 *
6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Deletes folders from the IMAP server.
10 * Called from the folders.php
11 *
12 * $Id$
13 */
14
15 /* Path for SquirrelMail required files. */
16 define('SM_PATH','../');
17
18 /* SquirrelMail required files. */
19 require_once(SM_PATH . 'include/validate.php');
20 require_once(SM_PATH . 'functions/imap.php');
21 require_once(SM_PATH . 'functions/tree.php');
22 require_once(SM_PATH . 'functions/display_messages.php');
23 require_once(SM_PATH . 'functions/html.php');
24
25 /*
26 * Incoming values:
27 * $mailbox - selected mailbox from the form
28 */
29
30 /* globals */
31 $username = $_SESSION['username'];
32 $key = $_COOKIE['key'];
33 $delimiter = $_SESSION['delimiter'];
34 $onetimepad = $_SESSION['onetimepad'];
35
36 $mailbox = $_POST['mailbox'];
37
38 /* end globals */
39
40 if ($mailbox == '') {
41 displayPageHeader($color, 'None');
42
43 plain_error_message(_("You have not selected a folder to delete. Please do so.").
44 '<BR><A HREF="../src/folders.php">'._("Click here to go back").'</A>.', $color);
45 exit;
46 }
47
48 if (isset($_POST['backingout'])) {
49 $location = get_location();
50 header ("Location: $location/folders.php");
51 exit;
52 }
53
54 if(!isset($_POST['confirmed'])) {
55 displayPageHeader($color, 'None');
56
57 echo '<br>' .
58 html_tag( 'table', '', 'center', '', 'width="95%" border="0"' ) .
59 html_tag( 'tr',
60 html_tag( 'td', '<b>' . _("Delete Folder") . '</b>', 'center', $color[0] )
61 ) .
62 html_tag( 'tr' ) .
63 html_tag( 'td', '', 'center', $color[4] ) .
64 sprintf(_("Are you sure you want to delete %s?"), imap_utf7_decode_local($mailbox)).
65 '<FORM ACTION="folders_delete.php" METHOD="POST"><p>'.
66
67 '<INPUT TYPE=HIDDEN NAME="mailbox" VALUE="'.$mailbox."\">\n" .
68 '<INPUT TYPE=SUBMIT NAME="confirmed" VALUE="'._("Yes")."\">\n".
69 '<INPUT TYPE=SUBMIT NAME="backingout" VALUE="'._("No")."\">\n".
70 '</p></FORM><BR></td></tr></table>';
71
72 exit;
73 }
74
75 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
76
77 $boxes = sqimap_mailbox_list ($imap_stream);
78 $numboxes = count($boxes);
79
80 global $delete_folder;
81
82 if (substr($mailbox, -1) == $delimiter)
83 $mailbox_no_dm = substr($mailbox, 0, strlen($mailbox) - 1);
84 else
85 $mailbox_no_dm = $mailbox;
86
87 /** lets see if we CAN move folders to the trash.. otherwise,
88 ** just delete them **/
89
90 /* Courier IMAP doesn't like subfolders of Trash
91 * If global options say we can't move it into Trash
92 * If it's already a subfolder of trash, we'll have to delete it */
93 if (strtolower($imap_server_type) == 'courier' ||
94 (isset($delete_folder) && $delete_folder) ||
95 eregi('^'.$trash_folder.'.+', $mailbox) )
96 {
97 $can_move_to_trash = FALSE;
98 }
99
100 /* Otherwise, check if trash folder exits and support sub-folders */
101 else {
102 for ($i = 0; $i < $numboxes; $i++) {
103 if ($boxes[$i]['unformatted'] == $trash_folder) {
104 $can_move_to_trash = !in_array('noinferiors', $boxes[$i]['flags']);
105 }
106 }
107 }
108
109 /** First create the top node in the tree **/
110 for ($i = 0; $i < $numboxes; $i++) {
111 if (($boxes[$i]['unformatted-dm'] == $mailbox) && (strlen($boxes[$i]['unformatted-dm']) == strlen($mailbox))) {
112 $foldersTree[0]['value'] = $mailbox;
113 $foldersTree[0]['doIHaveChildren'] = false;
114 continue;
115 }
116 }
117
118 /* Now create the nodes for subfolders of the parent folder
119 You can tell that it is a subfolder by tacking the mailbox delimiter
120 on the end of the $mailbox string, and compare to that. */
121 for ($i = 0; $i < $numboxes; $i++) {
122 if (substr($boxes[$i]['unformatted'], 0, strlen($mailbox_no_dm . $delimiter)) == ($mailbox_no_dm . $delimiter)) {
123 addChildNodeToTree($boxes[$i]["unformatted"], $boxes[$i]['unformatted-dm'], $foldersTree);
124 }
125 }
126
127 /** Lets start removing the folders and messages **/
128 if (($move_to_trash == true) && ($can_move_to_trash == true)) { /** if they wish to move messages to the trash **/
129 walkTreeInPostOrderCreatingFoldersUnderTrash(0, $imap_stream, $foldersTree, $mailbox);
130 walkTreeInPreOrderDeleteFolders(0, $imap_stream, $foldersTree);
131 } else { /** if they do NOT wish to move messages to the trash (or cannot)**/
132 walkTreeInPreOrderDeleteFolders(0, $imap_stream, $foldersTree);
133 }
134
135 /** Log out this session **/
136 sqimap_logout($imap_stream);
137
138 $location = get_location();
139 header ("Location: $location/folders.php?success=delete");
140
141 ?>