9b71f132b74202c5c1935399c1ddaf1e6fc354cd
[squirrelmail.git] / functions / folder_manip.php
1 <?php
2
3 /**
4 * folder_manip.php
5 *
6 * Functions for IMAP folder manipulation:
7 * (un)subscribe, create, rename, delete.
8 *
9 * @author Thijs Kinkhorst <kink at squirrelmail.org>
10 * @copyright 1999-2017 The SquirrelMail Project Team
11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
12 * @version $Id$
13 * @package squirrelmail
14 * @see folders.php
15 */
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 == '')) {
27
28 global $color, $oTemplate;
29 error_box(_("Illegal folder name.") . "<br />\n" .
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>.');
35
36 sqimap_logout($imapConnection);
37 $oTemplate->display('footer.tpl');
38 exit;
39 }
40 }
41
42 /**
43 * Called from folders.php to create a new folder.
44 * @param stream $imapConnection imap connection resource
45 * @param string $delimiter delimiter
46 * @param string $folder_name new folder name
47 * @param string $subfolder folder that stores new folder
48 * @param string $contain_subs if not empty, creates folder that can store subfolders
49 * @since 1.5.1
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);
58
59 if ( ! empty($contain_subs) ) {
60 $folder_type = 'noselect';
61 } else {
62 $folder_type = '';
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) == '') {
76 sqimap_mailbox_create ($imapConnection, $folder_prefix.$folder_name, $folder_type);
77 } else {
78 sqimap_mailbox_create ($imapConnection, $subfolder.$delimiter.$folder_name, $folder_type);
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) {
89 global $color, $default_folder_prefix, $oTemplate;
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);
94 sqimap_logout($imapConnection);
95 $oTemplate->display('footer.tpl');
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);
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 }
119 } else {
120 $old_name = $old;
121 $old_parent = '';
122 }
123
124 sqimap_logout($imapConnection);
125
126 $oTemplate->assign('dialog_type', 'rename');
127 $oTemplate->assign('parent_folder', sm_encode_html_special_chars($old_parent));
128 $oTemplate->assign('current_full_name', sm_encode_html_special_chars($old));
129 $oTemplate->assign('current_folder_name', sm_encode_html_special_chars($old_name));
130 $oTemplate->assign('is_folder', $isfolder);
131
132 $oTemplate->display('folder_manip_dialog.tpl');
133 $oTemplate->display('footer.tpl');
134
135 exit;
136 }
137
138 /**
139 * Given an old and new folder name, renames the folder.
140 */
141 function folders_rename_do($imapConnection, $delimiter, $orig, $old_name, $new_name)
142 {
143 folders_checkname($imapConnection, $new_name, $delimiter);
144
145 $orig = imap_utf7_encode_local($orig);
146 $old_name = imap_utf7_encode_local($old_name);
147 $new_name = imap_utf7_encode_local($new_name);
148
149 if ($old_name != $new_name) {
150
151 if (strpos($orig, $delimiter)) {
152 $old_dir = substr($orig, 0, strrpos($orig, $delimiter));
153 } else {
154 $old_dir = '';
155 }
156
157 if ($old_dir != '') {
158 $newone = $old_dir . $delimiter . $new_name;
159 } else {
160 $newone = $new_name;
161 }
162
163 // Renaming a folder doesn't rename the folder but leaves you unsubscribed
164 // at least on Cyrus IMAP servers.
165 if (isset($isfolder)) {
166 $newone = $newone.$delimiter;
167 $orig = $orig.$delimiter;
168 }
169 sqimap_mailbox_rename( $imapConnection, $orig, $newone );
170
171 }
172
173 return;
174 }
175
176 /**
177 * Presents a confirmation dialog to the user asking whether they're
178 * sure they want to delete this folder.
179 */
180 function folders_delete_ask ($imapConnection, $folder_name)
181 {
182 global $color, $default_folder_prefix, $oTemplate;
183
184 if ($folder_name == '') {
185 plain_error_message(_("You have not selected a folder to delete. Please do so.").
186 '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
187 sqimap_logout($imapConnection);
188 $oTemplate->display('footer.tpl');
189 exit;
190 }
191
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
198 sqimap_logout($imapConnection);
199
200 $oTemplate->assign('dialog_type', 'delete');
201 $oTemplate->assign('folder_name', sm_encode_html_special_chars($folder_name));
202 $oTemplate->assign('visible_folder_name', sm_encode_html_special_chars($visible_folder_name));
203
204 $oTemplate->display('folder_manip_dialog.tpl');
205 $oTemplate->display('footer.tpl');
206
207 exit;
208 }
209
210 /**
211 * Given a folder, moves it to trash (and all subfolders of it too).
212 */
213 function folders_delete_do ($imapConnection, $delimiter, $folder_name)
214 {
215 include(SM_PATH . 'functions/tree.php');
216
217 $boxes = sqimap_mailbox_list ($imapConnection);
218
219 global $delete_folder, $imap_server_type, $trash_folder, $move_to_trash;
220
221 if (substr($folder_name, -1) == $delimiter) {
222 $folder_name_no_dm = substr($folder_name, 0, strlen($folder_name) - 1);
223 } else {
224 $folder_name_no_dm = $folder_name;
225 }
226
227 /** lets see if we CAN move folders to the trash.. otherwise,
228 ** just delete them **/
229 if ($delete_folder || preg_match('/^' . preg_quote($trash_folder, '/') . '.+/i', $folder_name) ) {
230 $can_move_to_trash = FALSE;
231 } else {
232 /* Otherwise, check if trash folder exits and support sub-folders */
233 foreach($boxes as $box) {
234 if ($box['unformatted'] == $trash_folder) {
235 $can_move_to_trash = !in_array('noinferiors', $box['flags']);
236 }
237 }
238 }
239
240 /** First create the top node in the tree **/
241 foreach($boxes as $box) {
242 if (($box['unformatted-dm'] == $folder_name) && (strlen($box['unformatted-dm']) == strlen($folder_name))) {
243 $foldersTree[0]['value'] = $folder_name;
244 $foldersTree[0]['doIHaveChildren'] = false;
245 continue;
246 }
247 }
248
249 /* Now create the nodes for subfolders of the parent folder
250 You can tell that it is a subfolder by tacking the mailbox delimiter
251 on the end of the $folder_name string, and compare to that. */
252 foreach($boxes as $box) {
253 if (substr($box['unformatted'], 0, strlen($folder_name_no_dm . $delimiter)) == ($folder_name_no_dm . $delimiter)) {
254 addChildNodeToTree($box['unformatted'], $box['unformatted-dm'], $foldersTree);
255 }
256 }
257
258 /** Lets start removing the folders and messages **/
259 if (($move_to_trash == true) && ($can_move_to_trash == true)) { /** if they wish to move messages to the trash **/
260 walkTreeInPostOrderCreatingFoldersUnderTrash(0, $imapConnection, $foldersTree, $folder_name);
261 walkTreeInPreOrderDeleteFolders(0, $imapConnection, $foldersTree);
262 } else { /** if they do NOT wish to move messages to the trash (or cannot)**/
263 walkTreeInPreOrderDeleteFolders(0, $imapConnection, $foldersTree);
264 }
265
266 return;
267 }
268
269 /**
270 * Given an array of folder_names, subscribes to each of them.
271 */
272 function folders_subscribe($imapConnection, $folder_names)
273 {
274 global $color, $oTemplate;
275
276 if (count($folder_names) == 0 || $folder_names[0] == '') {
277 plain_error_message(_("You have not selected a folder to subscribe. Please do so.").
278 '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
279 sqimap_logout($imapConnection);
280 $oTemplate->display('footer.tpl');
281 exit;
282 }
283
284 global $no_list_for_subscribe, $imap_server_type;
285
286 if($no_list_for_subscribe && $imap_server_type == 'cyrus') {
287 /* Cyrus, atleast, does not typically allow subscription to
288 * nonexistent folders (this is an optional part of IMAP),
289 * lets catch it here and report back cleanly. */
290 if(!sqimap_mailbox_exists($imapConnection, $folder_names[0])) {
291 plain_error_message(_("Subscription Unsuccessful - Folder does not exist.").
292 '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
293 sqimap_logout($imapConnection);
294 exit;
295
296 }
297 }
298 foreach ( $folder_names as $folder_name ) {
299 sqimap_subscribe ($imapConnection, $folder_name);
300 }
301
302 return;
303 }
304
305 /**
306 * Given a list of folder names, unsubscribes from each of them.
307 */
308 function folders_unsubscribe($imapConnection, $folder_names)
309 {
310 global $color, $oTemplate;
311
312 if (count($folder_names) == 0 || $folder_names[0] == '') {
313 plain_error_message(_("You have not selected a folder to unsubscribe. Please do so.").
314 '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
315 sqimap_logout($imapConnection);
316 $oTemplate->display('footer.tpl');
317 exit;
318 }
319
320 foreach ( $folder_names as $folder_name ) {
321 sqimap_unsubscribe ($imapConnection, $folder_name);
322 }
323
324 return;
325 }