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