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