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