Completely cleanup the folders management (create/subscribe etc) code.
[squirrelmail.git] / functions / folder_manip.php
CommitLineData
32aa0074 1<?php
2
3/**
4* folder_manip.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* Functions for IMAP folder manipulation:
10* (un)subscribe, create, rename, delete.
11*
12* @version $Id$
13* @package squirrelmail
14* @see folders.php
15* @author Thijs Kinkhorst - kink@squirrelmail.org
16*/
17
18
19/**
20 * Helper function for the functions below; checks if the user entered
21 * folder name is valid according to the IMAP standard. If not, it
22 * bails out with an error and cleanly terminates the IMAP connection.
23 */
24function folders_checkname($imapConnection, $folder_name, $delimiter)
25{
26 if (substr_count($folder_name, '"') || substr_count($folder_name, "\\") ||
27 substr_count($folder_name, $delimiter) || ($folder_name == '')) {
28
29 global $color;
30 plain_error_message(_("Illegal folder name. Please select a different name.").
31 '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
32
33 sqimap_logout($imapConnection);
34 exit;
35 }
36}
37
38/**
39 * Called from folders.php to create a new folder.
40 */
41function folders_create ($imapConnection, $delimiter, $folder_name, $subfolder, $contain_subs)
42{
43 folders_checkname($imapConnection, $folder_name, $delimiter);
44
45 global $folder_prefix;
46
47 $folder_name = imap_utf7_encode_local($folder_name);
48
49 if ( ! empty($contain_subs) ) {
50 $folder_name = $folder_name . $delimiter;
51 }
52
53 if ($folder_prefix && (substr($folder_prefix, -1) != $delimiter)) {
54 $folder_prefix = $folder_prefix . $delimiter;
55 }
56 if ($folder_prefix && (substr($subfolder, 0, strlen($folder_prefix)) != $folder_prefix)) {
57 $subfolder_orig = $subfolder;
58 $subfolder = $folder_prefix . $subfolder;
59 } else {
60 $subfolder_orig = $subfolder;
61 }
62
63 if (trim($subfolder_orig) == '') {
64 sqimap_mailbox_create ($imapConnection, $folder_prefix.$folder_name, '');
65 } else {
66 sqimap_mailbox_create ($imapConnection, $subfolder.$delimiter.$folder_name, '');
67 }
68
69 return;
70}
71
72/**
73 * Called from folders.php, given a folder name, ask the user what this
74 * folder should be renamed to.
75 */
76function folders_rename_getname ($imapConnection, $delimiter, $old) {
77 global $color;
78
79 if ( $old == '' ) {
80 plain_error_message(_("You have not selected a folder to rename. Please do so.").
81 '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
82 sqimap_logout($imapConnection);
83 exit;
84 }
85
86 if (substr($old, strlen($old) - strlen($delimiter)) == $delimiter) {
87 $isfolder = TRUE;
88 $old = substr($old, 0, strlen($old) - 1);
89 } else {
90 $isfolder = FALSE;
91 }
92
93 $old = imap_utf7_decode_local($old);
94
95 if (strpos($old, $delimiter)) {
96 $old_name = substr($old, strrpos($old, $delimiter)+1, strlen($old));
97 $old_parent = substr($old, 0, strrpos($old, $delimiter));
98 } else {
99 $old_name = $old;
100 $old_parent = '';
101 }
102
103 echo '<br />' .
104 html_tag( 'table', '', 'center', '', 'width="95%" border="0"' ) .
105 html_tag( 'tr',
106 html_tag( 'td', '<b>' . _("Rename a folder") . '</b>', 'center', $color[0] )
107 ) .
108 html_tag( 'tr' ) .
109 html_tag( 'td', '', 'center', $color[4] ) .
110 addForm('folders.php').
111 addHidden('smaction', 'rename').
112 _("New name:").
113 '<br /><b>' . htmlspecialchars($old_parent) . ' ' . htmlspecialchars($delimiter) . '</b>' .
114 addInput('new_name', $old_name, 25) . '<br /><br />' . "\n";
115 if ( $isfolder ) {
116 echo addHidden('isfolder', 'true');
117 }
118 echo addHidden('orig', $old).
119 addHidden('old_name', $old_name).
120 '<input type="submit" value="'._("Rename")."\" />\n".
121 '<input type="submit" name="cancelbutton" value="'._("Cancel")."\" />\n".
122 '</form><br /></td></tr></table>';
123 echo "\n\n</body></html>";
124
125 sqimap_logout($imapConnection);
126 exit;
127}
128
129/**
130 * Given an old and new folder name, renames the folder.
131 */
132function folders_rename_do($imapConnection, $delimiter, $orig, $old_name, $new_name)
133{
134 folders_checkname($imapConnection, $new_name, $delimiter);
135
136 $orig = imap_utf7_encode_local($orig);
137 $old_name = imap_utf7_encode_local($old_name);
138 $new_name = imap_utf7_encode_local($new_name);
139
140 if ($old_name != $new_name) {
141
142 if (strpos($orig, $delimiter)) {
143 $old_dir = substr($orig, 0, strrpos($orig, $delimiter));
144 } else {
145 $old_dir = '';
146 }
147
148 if ($old_dir != '') {
149 $newone = $old_dir . $delimiter . $new_name;
150 } else {
151 $newone = $new_name;
152 }
153
154 // Renaming a folder doesn't rename the folder but leaves you unsubscribed
155 // at least on Cyrus IMAP servers.
156 if (isset($isfolder)) {
157 $newone = $newone.$delimiter;
158 $orig = $orig.$delimiter;
159 }
160 sqimap_mailbox_rename( $imapConnection, $orig, $newone );
161
162 }
163
164 return;
165}
166
167/**
168 * Presents a confirmation dialog to the user asking whether they're
169 * sure they want to delete this folder.
170 */
171function folders_delete_ask ($imapConnection, $folder_name)
172{
173 global $color;
174
175 if ($folder_name == '') {
176 plain_error_message(_("You have not selected a folder to delete. Please do so.").
177 '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
178 exit;
179 }
180
181 echo '<br />' .
182 html_tag( 'table', '', 'center', '', 'width="95%" border="0"' ) .
183 html_tag( 'tr',
184 html_tag( 'td', '<b>' . _("Delete Folder") . '</b>', 'center', $color[0] )
185 ) .
186 html_tag( 'tr' ) .
187 html_tag( 'td', '', 'center', $color[4] ) .
188 sprintf(_("Are you sure you want to delete %s?"),
189 str_replace(array(' ','<','>'),array('&nbsp;','&lt;','&gt;'),
190 imap_utf7_decode_local($folder_name))).
191 addForm('folders.php', 'post')."<p>\n".
192 addHidden('smaction', 'delete').
193 addHidden('folder_name', $folder_name).
194 addSubmit(_("Yes"), 'confirmed').
195 addSubmit(_("No"), 'cancelbutton').
196 '</p></form><br /></td></tr></table>';
197
198 echo "\n\n</body></html>";
199
200 sqimap_logout($imapConnection);
201 exit;
202}
203
204/**
205 * Given a folder, moves it to trash (and all subfolders of it too).
206 */
207function folders_delete_do ($imapConnection, $delimiter, $folder_name)
208{
209 require_once(SM_PATH . 'functions/tree.php');
210
211 $boxes = sqimap_mailbox_list ($imapConnection);
212
213 global $delete_folder, $imap_server_type, $trash_folder, $move_to_trash;
214
215 if (substr($folder_name, -1) == $delimiter) {
216 $folder_name_no_dm = substr($folder_name, 0, strlen($folder_name) - 1);
217 } else {
218 $folder_name_no_dm = $folder_name;
219 }
220
221 /** lets see if we CAN move folders to the trash.. otherwise,
222 ** just delete them **/
223
224 /* Courier IMAP doesn't like subfolders of Trash
225 * If global options say we can't move it into Trash
226 * If it's already a subfolder of trash, we'll have to delete it */
227 if (strtolower($imap_server_type) == 'courier' ||
228 (isset($delete_folder) && $delete_folder) ||
229 eregi('^'.$trash_folder.'.+', $folder_name) )
230 {
231 $can_move_to_trash = FALSE;
232 }
233 /* Otherwise, check if trash folder exits and support sub-folders */
234 else {
235 foreach($boxes as $box) {
236 if ($box['unformatted'] == $trash_folder) {
237 $can_move_to_trash = !in_array('noinferiors', $box['flags']);
238 }
239 }
240 }
241
242 /** First create the top node in the tree **/
243 foreach($boxes as $box) {
244 if (($box['unformatted-dm'] == $folder_name) && (strlen($box['unformatted-dm']) == strlen($folder_name))) {
245 $foldersTree[0]['value'] = $folder_name;
246 $foldersTree[0]['doIHaveChildren'] = false;
247 continue;
248 }
249 }
250
251 /* Now create the nodes for subfolders of the parent folder
252 You can tell that it is a subfolder by tacking the mailbox delimiter
253 on the end of the $folder_name string, and compare to that. */
254 foreach($boxes as $box) {
255 if (substr($box['unformatted'], 0, strlen($folder_name_no_dm . $delimiter)) == ($folder_name_no_dm . $delimiter)) {
256 addChildNodeToTree($box['unformatted'], $box['unformatted-dm'], $foldersTree);
257 }
258 }
259
260 /** Lets start removing the folders and messages **/
261 if (($move_to_trash == true) && ($can_move_to_trash == true)) { /** if they wish to move messages to the trash **/
262 walkTreeInPostOrderCreatingFoldersUnderTrash(0, $imapConnection, $foldersTree, $folder_name);
263 walkTreeInPreOrderDeleteFolders(0, $imapConnection, $foldersTree);
264 } else { /** if they do NOT wish to move messages to the trash (or cannot)**/
265 walkTreeInPreOrderDeleteFolders(0, $imapConnection, $foldersTree);
266 }
267
268 return;
269}
270
271/**
272 * Given an array of folder_names, subscribes to each of them.
273 */
274function folders_subscribe($imapConnection, $folder_names)
275{
276 global $color;
277
278 if (count($folder_names) == 0 || $folder_names[0] == '') {
279 plain_error_message(_("You have not selected a folder to subscribe. Please do so.").
280 '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
281 sqimap_logout($imapConnection);
282 exit;
283 }
284
285 global $no_list_for_subscribe, $imap_server_type;
286
287 if($no_list_for_subscribe && $imap_server_type == 'cyrus') {
288 /* Cyrus, atleast, does not typically allow subscription to
289 * nonexistent folders (this is an optional part of IMAP),
290 * lets catch it here and report back cleanly. */
291 if(!sqimap_mailbox_exists($imapConnection, $folder_names[0])) {
292 plain_error_message(_("Subscription Unsuccessful - Folder does not exist.").
293 '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
294 sqimap_logout($imapConnection);
295 exit;
296
297 }
298 }
299 foreach ( $folder_names as $folder_name ) {
300 sqimap_subscribe ($imapConnection, $folder_name);
301 }
302
303 return;
304}
305
306/**
307 * Given a list of folder names, unsubscribes from each of them.
308 */
309function folders_unsubscribe($imapConnection, $folder_names)
310{
311 global $color;
312
313 if (count($folder_names) == 0 || $folder_names[0] == '') {
314 plain_error_message(_("You have not selected a folder to unsubscribe. Please do so.").
315 '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
316 sqimap_logout($imapConnection);
317 exit;
318 }
319
320 foreach ( $folder_names as $folder_name ) {
321 sqimap_unsubscribe ($imapConnection, $folder_name);
322 }
323
324 return;
325}
326
327
328?>