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