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