deprecated - considered obsolescent
[squirrelmail.git] / functions / folder_manip.php
CommitLineData
32aa0074 1<?php
2
3/**
aa00e648 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 */
32aa0074 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);
aa00e648 32
32aa0074 33 sqimap_logout($imapConnection);
aa00e648 34 exit;
32aa0074 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) {
aa00e648 77 global $color,$default_folder_prefix;
32aa0074 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));
aa00e648 97 // hide default prefix (INBOX., mail/ or other)
98 $quoted_prefix=preg_quote($default_folder_prefix,'/');
99 $prefix_length=(preg_match("/^$quoted_prefix/",$old) ? strlen($default_folder_prefix) : 0);
100 if ($prefix_length>strrpos($old, $delimiter)) {
101 $old_parent = '';
102 } else {
103 $old_parent = substr($old, $prefix_length, (strrpos($old, $delimiter)-$prefix_length))
104 . ' ' . $delimiter;
105 }
32aa0074 106 } else {
107 $old_name = $old;
108 $old_parent = '';
109 }
110
111 echo '<br />' .
112 html_tag( 'table', '', 'center', '', 'width="95%" border="0"' ) .
113 html_tag( 'tr',
114 html_tag( 'td', '<b>' . _("Rename a folder") . '</b>', 'center', $color[0] )
115 ) .
116 html_tag( 'tr' ) .
117 html_tag( 'td', '', 'center', $color[4] ) .
118 addForm('folders.php').
119 addHidden('smaction', 'rename').
120 _("New name:").
aa00e648 121 '<br /><b>' . htmlspecialchars($old_parent) . '</b>' .
32aa0074 122 addInput('new_name', $old_name, 25) . '<br /><br />' . "\n";
123 if ( $isfolder ) {
124 echo addHidden('isfolder', 'true');
125 }
126 echo addHidden('orig', $old).
127 addHidden('old_name', $old_name).
128 '<input type="submit" value="'._("Rename")."\" />\n".
129 '<input type="submit" name="cancelbutton" value="'._("Cancel")."\" />\n".
130 '</form><br /></td></tr></table>';
131 echo "\n\n</body></html>";
132
133 sqimap_logout($imapConnection);
134 exit;
135}
136
137/**
138 * Given an old and new folder name, renames the folder.
139 */
140function folders_rename_do($imapConnection, $delimiter, $orig, $old_name, $new_name)
141{
142 folders_checkname($imapConnection, $new_name, $delimiter);
143
144 $orig = imap_utf7_encode_local($orig);
145 $old_name = imap_utf7_encode_local($old_name);
146 $new_name = imap_utf7_encode_local($new_name);
147
148 if ($old_name != $new_name) {
149
150 if (strpos($orig, $delimiter)) {
151 $old_dir = substr($orig, 0, strrpos($orig, $delimiter));
152 } else {
153 $old_dir = '';
154 }
155
156 if ($old_dir != '') {
157 $newone = $old_dir . $delimiter . $new_name;
158 } else {
159 $newone = $new_name;
160 }
161
162 // Renaming a folder doesn't rename the folder but leaves you unsubscribed
163 // at least on Cyrus IMAP servers.
164 if (isset($isfolder)) {
165 $newone = $newone.$delimiter;
166 $orig = $orig.$delimiter;
167 }
168 sqimap_mailbox_rename( $imapConnection, $orig, $newone );
169
170 }
171
172 return;
173}
174
175/**
176 * Presents a confirmation dialog to the user asking whether they're
177 * sure they want to delete this folder.
178 */
179function folders_delete_ask ($imapConnection, $folder_name)
180{
aa00e648 181 global $color,$default_folder_prefix;
32aa0074 182
183 if ($folder_name == '') {
184 plain_error_message(_("You have not selected a folder to delete. Please do so.").
185 '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
186 exit;
187 }
188
aa00e648 189 // hide default folder prefix (INBOX., mail/ or other)
190 $visible_folder_name = imap_utf7_decode_local($folder_name);
191 $quoted_prefix = preg_quote($default_folder_prefix,'/');
192 $prefix_length = (preg_match("/^$quoted_prefix/",$visible_folder_name) ? strlen($default_folder_prefix) : 0);
193 $visible_folder_name = substr($visible_folder_name,$prefix_length);
194
32aa0074 195 echo '<br />' .
196 html_tag( 'table', '', 'center', '', 'width="95%" border="0"' ) .
197 html_tag( 'tr',
198 html_tag( 'td', '<b>' . _("Delete Folder") . '</b>', 'center', $color[0] )
199 ) .
200 html_tag( 'tr' ) .
201 html_tag( 'td', '', 'center', $color[4] ) .
202 sprintf(_("Are you sure you want to delete %s?"),
aa00e648 203 str_replace(array(' ','<','>'),array('&nbsp;','&lt;','&gt;'),$visible_folder_name)).
32aa0074 204 addForm('folders.php', 'post')."<p>\n".
205 addHidden('smaction', 'delete').
206 addHidden('folder_name', $folder_name).
207 addSubmit(_("Yes"), 'confirmed').
208 addSubmit(_("No"), 'cancelbutton').
209 '</p></form><br /></td></tr></table>';
210
211 echo "\n\n</body></html>";
212
213 sqimap_logout($imapConnection);
214 exit;
215}
216
217/**
218 * Given a folder, moves it to trash (and all subfolders of it too).
219 */
220function folders_delete_do ($imapConnection, $delimiter, $folder_name)
221{
222 require_once(SM_PATH . 'functions/tree.php');
223
224 $boxes = sqimap_mailbox_list ($imapConnection);
225
226 global $delete_folder, $imap_server_type, $trash_folder, $move_to_trash;
227
228 if (substr($folder_name, -1) == $delimiter) {
229 $folder_name_no_dm = substr($folder_name, 0, strlen($folder_name) - 1);
230 } else {
231 $folder_name_no_dm = $folder_name;
232 }
233
234 /** lets see if we CAN move folders to the trash.. otherwise,
235 ** just delete them **/
236
237 /* Courier IMAP doesn't like subfolders of Trash
238 * If global options say we can't move it into Trash
239 * If it's already a subfolder of trash, we'll have to delete it */
240 if (strtolower($imap_server_type) == 'courier' ||
241 (isset($delete_folder) && $delete_folder) ||
242 eregi('^'.$trash_folder.'.+', $folder_name) )
243 {
244 $can_move_to_trash = FALSE;
245 }
246 /* Otherwise, check if trash folder exits and support sub-folders */
247 else {
248 foreach($boxes as $box) {
249 if ($box['unformatted'] == $trash_folder) {
250 $can_move_to_trash = !in_array('noinferiors', $box['flags']);
251 }
252 }
253 }
254
255 /** First create the top node in the tree **/
256 foreach($boxes as $box) {
257 if (($box['unformatted-dm'] == $folder_name) && (strlen($box['unformatted-dm']) == strlen($folder_name))) {
258 $foldersTree[0]['value'] = $folder_name;
259 $foldersTree[0]['doIHaveChildren'] = false;
260 continue;
261 }
262 }
263
264 /* Now create the nodes for subfolders of the parent folder
265 You can tell that it is a subfolder by tacking the mailbox delimiter
266 on the end of the $folder_name string, and compare to that. */
267 foreach($boxes as $box) {
268 if (substr($box['unformatted'], 0, strlen($folder_name_no_dm . $delimiter)) == ($folder_name_no_dm . $delimiter)) {
269 addChildNodeToTree($box['unformatted'], $box['unformatted-dm'], $foldersTree);
270 }
271 }
272
273 /** Lets start removing the folders and messages **/
274 if (($move_to_trash == true) && ($can_move_to_trash == true)) { /** if they wish to move messages to the trash **/
275 walkTreeInPostOrderCreatingFoldersUnderTrash(0, $imapConnection, $foldersTree, $folder_name);
276 walkTreeInPreOrderDeleteFolders(0, $imapConnection, $foldersTree);
277 } else { /** if they do NOT wish to move messages to the trash (or cannot)**/
278 walkTreeInPreOrderDeleteFolders(0, $imapConnection, $foldersTree);
279 }
280
281 return;
282}
283
284/**
285 * Given an array of folder_names, subscribes to each of them.
286 */
287function folders_subscribe($imapConnection, $folder_names)
288{
289 global $color;
290
291 if (count($folder_names) == 0 || $folder_names[0] == '') {
292 plain_error_message(_("You have not selected a folder to subscribe. Please do so.").
293 '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
294 sqimap_logout($imapConnection);
295 exit;
296 }
297
298 global $no_list_for_subscribe, $imap_server_type;
299
300 if($no_list_for_subscribe && $imap_server_type == 'cyrus') {
301 /* Cyrus, atleast, does not typically allow subscription to
302 * nonexistent folders (this is an optional part of IMAP),
303 * lets catch it here and report back cleanly. */
304 if(!sqimap_mailbox_exists($imapConnection, $folder_names[0])) {
305 plain_error_message(_("Subscription Unsuccessful - Folder does not exist.").
306 '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
307 sqimap_logout($imapConnection);
308 exit;
309
310 }
311 }
312 foreach ( $folder_names as $folder_name ) {
313 sqimap_subscribe ($imapConnection, $folder_name);
314 }
315
316 return;
317}
318
319/**
320 * Given a list of folder names, unsubscribes from each of them.
321 */
322function folders_unsubscribe($imapConnection, $folder_names)
323{
324 global $color;
325
326 if (count($folder_names) == 0 || $folder_names[0] == '') {
327 plain_error_message(_("You have not selected a folder to unsubscribe. Please do so.").
328 '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
329 sqimap_logout($imapConnection);
330 exit;
331 }
332
333 foreach ( $folder_names as $folder_name ) {
334 sqimap_unsubscribe ($imapConnection, $folder_name);
335 }
336
337 return;
338}
339
340
341?>