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. |
940ac569 |
44 | * @param stream $imapConnection imap connection resource |
45 | * @param string $delimiter delimiter |
46 | * @param string $folder_name create folder name |
47 | * @param string $subfolder folder that stores new folder |
48 | * @param boolean $contain_subs |
49 | * @since 1.5.1 |
32aa0074 |
50 | */ |
51 | function folders_create ($imapConnection, $delimiter, $folder_name, $subfolder, $contain_subs) |
52 | { |
53 | folders_checkname($imapConnection, $folder_name, $delimiter); |
54 | |
55 | global $folder_prefix; |
56 | |
57 | $folder_name = imap_utf7_encode_local($folder_name); |
f8a1ed5a |
58 | |
32aa0074 |
59 | if ( ! empty($contain_subs) ) { |
940ac569 |
60 | $folder_type = 'noselect'; |
61 | } else { |
62 | $folder_type = ''; |
32aa0074 |
63 | } |
64 | |
65 | if ($folder_prefix && (substr($folder_prefix, -1) != $delimiter)) { |
66 | $folder_prefix = $folder_prefix . $delimiter; |
67 | } |
68 | if ($folder_prefix && (substr($subfolder, 0, strlen($folder_prefix)) != $folder_prefix)) { |
69 | $subfolder_orig = $subfolder; |
70 | $subfolder = $folder_prefix . $subfolder; |
71 | } else { |
72 | $subfolder_orig = $subfolder; |
73 | } |
74 | |
75 | if (trim($subfolder_orig) == '') { |
940ac569 |
76 | sqimap_mailbox_create ($imapConnection, $folder_prefix.$folder_name, $folder_type); |
32aa0074 |
77 | } else { |
940ac569 |
78 | sqimap_mailbox_create ($imapConnection, $subfolder.$delimiter.$folder_name, $folder_type); |
32aa0074 |
79 | } |
80 | |
81 | return; |
82 | } |
83 | |
84 | /** |
85 | * Called from folders.php, given a folder name, ask the user what this |
86 | * folder should be renamed to. |
87 | */ |
88 | function folders_rename_getname ($imapConnection, $delimiter, $old) { |
9829e1d8 |
89 | global $color, $default_folder_prefix, $oTemplate; |
32aa0074 |
90 | |
91 | if ( $old == '' ) { |
92 | plain_error_message(_("You have not selected a folder to rename. Please do so."). |
93 | '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color); |
f8a1ed5a |
94 | sqimap_logout($imapConnection); |
9829e1d8 |
95 | $oTemplate->display('footer.tpl'); |
32aa0074 |
96 | exit; |
97 | } |
98 | |
99 | if (substr($old, strlen($old) - strlen($delimiter)) == $delimiter) { |
100 | $isfolder = TRUE; |
101 | $old = substr($old, 0, strlen($old) - 1); |
102 | } else { |
103 | $isfolder = FALSE; |
104 | } |
105 | |
106 | $old = imap_utf7_decode_local($old); |
107 | |
108 | if (strpos($old, $delimiter)) { |
109 | $old_name = substr($old, strrpos($old, $delimiter)+1, strlen($old)); |
aa00e648 |
110 | // hide default prefix (INBOX., mail/ or other) |
111 | $quoted_prefix=preg_quote($default_folder_prefix,'/'); |
112 | $prefix_length=(preg_match("/^$quoted_prefix/",$old) ? strlen($default_folder_prefix) : 0); |
113 | if ($prefix_length>strrpos($old, $delimiter)) { |
114 | $old_parent = ''; |
115 | } else { |
116 | $old_parent = substr($old, $prefix_length, (strrpos($old, $delimiter)-$prefix_length)) |
117 | . ' ' . $delimiter; |
118 | } |
32aa0074 |
119 | } else { |
120 | $old_name = $old; |
121 | $old_parent = ''; |
122 | } |
9829e1d8 |
123 | |
124 | sqimap_logout($imapConnection); |
32aa0074 |
125 | |
9829e1d8 |
126 | $oTemplate->assign('dialog_type', 'rename'); |
127 | $oTemplate->assign('color', $color); |
128 | $oTemplate->assign('old_parent', htmlspecialchars($old_parent)); |
129 | $oTemplate->assign('old', htmlspecialchars($old)); |
130 | $oTemplate->assign('old_name', htmlspecialchars($old_name)); |
131 | $oTemplate->assign('isfolder', $isfolder); |
132 | |
133 | $oTemplate->display('folder_manip_dialog.tpl'); |
134 | $oTemplate->display('footer.tpl'); |
f8a1ed5a |
135 | |
32aa0074 |
136 | exit; |
137 | } |
138 | |
139 | /** |
140 | * Given an old and new folder name, renames the folder. |
141 | */ |
142 | function folders_rename_do($imapConnection, $delimiter, $orig, $old_name, $new_name) |
143 | { |
144 | folders_checkname($imapConnection, $new_name, $delimiter); |
145 | |
146 | $orig = imap_utf7_encode_local($orig); |
147 | $old_name = imap_utf7_encode_local($old_name); |
148 | $new_name = imap_utf7_encode_local($new_name); |
f8a1ed5a |
149 | |
32aa0074 |
150 | if ($old_name != $new_name) { |
151 | |
152 | if (strpos($orig, $delimiter)) { |
153 | $old_dir = substr($orig, 0, strrpos($orig, $delimiter)); |
154 | } else { |
155 | $old_dir = ''; |
156 | } |
157 | |
158 | if ($old_dir != '') { |
159 | $newone = $old_dir . $delimiter . $new_name; |
160 | } else { |
161 | $newone = $new_name; |
162 | } |
163 | |
164 | // Renaming a folder doesn't rename the folder but leaves you unsubscribed |
165 | // at least on Cyrus IMAP servers. |
166 | if (isset($isfolder)) { |
167 | $newone = $newone.$delimiter; |
168 | $orig = $orig.$delimiter; |
169 | } |
170 | sqimap_mailbox_rename( $imapConnection, $orig, $newone ); |
171 | |
172 | } |
173 | |
174 | return; |
175 | } |
176 | |
177 | /** |
178 | * Presents a confirmation dialog to the user asking whether they're |
179 | * sure they want to delete this folder. |
180 | */ |
181 | function folders_delete_ask ($imapConnection, $folder_name) |
182 | { |
9829e1d8 |
183 | global $color, $default_folder_prefix, $oTemplate; |
32aa0074 |
184 | |
185 | if ($folder_name == '') { |
186 | plain_error_message(_("You have not selected a folder to delete. Please do so."). |
187 | '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color); |
9829e1d8 |
188 | sqimap_logout($imapConnection); |
189 | $oTemplate->display('footer.tpl'); |
32aa0074 |
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 | |
f8a1ed5a |
199 | sqimap_logout($imapConnection); |
9829e1d8 |
200 | |
201 | $oTemplate->assign('dialog_type', 'delete'); |
202 | $oTemplate->assign('color', $color); |
203 | $oTemplate->assign('folder_name', htmlspecialchars($folder_name)); |
204 | $oTemplate->assign('visible_folder_name', htmlspecialchars($visible_folder_name)); |
205 | |
206 | $oTemplate->display('folder_manip_dialog.tpl'); |
207 | $oTemplate->display('footer.tpl'); |
208 | |
32aa0074 |
209 | exit; |
210 | } |
211 | |
212 | /** |
213 | * Given a folder, moves it to trash (and all subfolders of it too). |
214 | */ |
215 | function folders_delete_do ($imapConnection, $delimiter, $folder_name) |
216 | { |
202bcbcc |
217 | include(SM_PATH . 'functions/tree.php'); |
f8a1ed5a |
218 | |
32aa0074 |
219 | $boxes = sqimap_mailbox_list ($imapConnection); |
220 | |
221 | global $delete_folder, $imap_server_type, $trash_folder, $move_to_trash; |
222 | |
223 | if (substr($folder_name, -1) == $delimiter) { |
224 | $folder_name_no_dm = substr($folder_name, 0, strlen($folder_name) - 1); |
225 | } else { |
226 | $folder_name_no_dm = $folder_name; |
227 | } |
228 | |
229 | /** lets see if we CAN move folders to the trash.. otherwise, |
230 | ** just delete them **/ |
ceb7ad3c |
231 | if ($delete_folder || eregi('^'.$trash_folder.'.+', $folder_name) ) { |
32aa0074 |
232 | $can_move_to_trash = FALSE; |
ceb7ad3c |
233 | } else { |
32aa0074 |
234 | /* Otherwise, check if trash folder exits and support sub-folders */ |
32aa0074 |
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 | */ |
274 | function folders_subscribe($imapConnection, $folder_names) |
275 | { |
9829e1d8 |
276 | global $color, $oTemplate; |
32aa0074 |
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); |
9829e1d8 |
282 | $oTemplate->display('footer.tpl'); |
32aa0074 |
283 | exit; |
284 | } |
285 | |
286 | global $no_list_for_subscribe, $imap_server_type; |
287 | |
288 | if($no_list_for_subscribe && $imap_server_type == 'cyrus') { |
289 | /* Cyrus, atleast, does not typically allow subscription to |
290 | * nonexistent folders (this is an optional part of IMAP), |
291 | * lets catch it here and report back cleanly. */ |
292 | if(!sqimap_mailbox_exists($imapConnection, $folder_names[0])) { |
293 | plain_error_message(_("Subscription Unsuccessful - Folder does not exist."). |
294 | '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color); |
295 | sqimap_logout($imapConnection); |
296 | exit; |
f8a1ed5a |
297 | |
32aa0074 |
298 | } |
299 | } |
300 | foreach ( $folder_names as $folder_name ) { |
301 | sqimap_subscribe ($imapConnection, $folder_name); |
302 | } |
303 | |
304 | return; |
305 | } |
306 | |
307 | /** |
308 | * Given a list of folder names, unsubscribes from each of them. |
309 | */ |
310 | function folders_unsubscribe($imapConnection, $folder_names) |
311 | { |
9829e1d8 |
312 | global $color, $oTemplate; |
32aa0074 |
313 | |
314 | if (count($folder_names) == 0 || $folder_names[0] == '') { |
315 | plain_error_message(_("You have not selected a folder to unsubscribe. Please do so."). |
316 | '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color); |
317 | sqimap_logout($imapConnection); |
9829e1d8 |
318 | $oTemplate->display('footer.tpl'); |
32aa0074 |
319 | exit; |
320 | } |
321 | |
322 | foreach ( $folder_names as $folder_name ) { |
323 | sqimap_unsubscribe ($imapConnection, $folder_name); |
324 | } |
325 | |
326 | return; |
327 | } |