Fixed how folder names are formatted in a hierarchy of folders.
[squirrelmail.git] / functions / imap_mailbox.php
1 <?php
2 /**
3 ** imap_mailbox.php
4 **
5 ** This impliments all functions that manipulate mailboxes
6 **/
7
8 /******************************************************************************
9 ** Expunges a mailbox
10 ******************************************************************************/
11 function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = true) {
12 sqimap_mailbox_select ($imap_stream, $mailbox);
13 fputs ($imap_stream, "a001 EXPUNGE\r\n");
14 $read = sqimap_read_data($imap_stream, "a001", $handle_errors, $response, $message);
15 sqimap_mailbox_close ($imap_stream);
16 }
17
18
19 /******************************************************************************
20 ** Checks whether or not the specified mailbox exists
21 ******************************************************************************/
22 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
23 fputs ($imap_stream, "a001 LIST \"\" \"$mailbox\"\r\n");
24 $mbx = sqimap_read_data($imap_stream, "a001", true, $response, $message);
25 if ($mailbox) {
26 return !!(ereg ("$mailbox", $mbx[0])); // To force into true/false
27 }
28 }
29
30 /******************************************************************************
31 ** Closes an open mailbox
32 ******************************************************************************/
33 function sqimap_mailbox_close ($imap_stream) {
34 fputs ($imap_stream, "a001 CLOSE\r\n");
35 $tmp = sqimap_read_data($imap_stream, "a001", false, $response, $message);
36 }
37
38 /******************************************************************************
39 ** Selects a mailbox
40 ******************************************************************************/
41 function sqimap_mailbox_select ($imap_stream, $mailbox, $hide=true, $recent=false) {
42 global $auto_expunge;
43
44 fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n");
45 $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
46 if ($recent) {
47 for ($i=0; $i<count($read); $i++) {
48 if (strpos(strtolower($read[$i]), "recent")) {
49 $r = explode(" ", $read[$i]);
50 }
51 }
52 return $r[1];
53 }
54 if ($auto_expunge) {
55 fputs ($imap_stream, "a001 EXPUNGE\r\n");
56 $tmp = sqimap_read_data($imap_stream, "a001", false, $a, $b);
57 }
58 }
59
60
61
62 /******************************************************************************
63 ** Creates a folder
64 ******************************************************************************/
65 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
66 if (strtolower($type) == "noselect") {
67 $dm = sqimap_get_delimiter($imap_stream);
68 $mailbox = $mailbox.$dm;
69 }
70 fputs ($imap_stream, "a001 CREATE \"$mailbox\"\r\n");
71 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
72
73 sqimap_subscribe ($imap_stream, $mailbox);
74 }
75
76
77
78 /******************************************************************************
79 ** Subscribes to an existing folder
80 ******************************************************************************/
81 function sqimap_subscribe ($imap_stream, $mailbox) {
82 fputs ($imap_stream, "a001 SUBSCRIBE \"$mailbox\"\r\n");
83 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
84 }
85
86
87
88
89 /******************************************************************************
90 ** Unsubscribes to an existing folder
91 ******************************************************************************/
92 function sqimap_unsubscribe ($imap_stream, $mailbox) {
93 global $imap_server_type;
94
95 fputs ($imap_stream, "a001 UNSUBSCRIBE \"$mailbox\"\r\n");
96 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
97 }
98
99
100
101
102 /******************************************************************************
103 ** This function simply deletes the given folder
104 ******************************************************************************/
105 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
106 fputs ($imap_stream, "a001 DELETE \"$mailbox\"\r\n");
107 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
108 sqimap_unsubscribe ($imap_stream, $mailbox);
109 }
110
111
112 /******************************************************************************
113 ** Formats a mailbox into 4 parts for the $boxes array
114 **
115 ** The four parts are:
116 **
117 ** raw - Raw LIST/LSUB response from the IMAP server
118 ** formatted - nicely formatted folder name
119 ** unformatted - unformatted, but with delimiter at end removed
120 ** unformatted-dm - folder name as it appears in raw response
121 **
122 ******************************************************************************/
123 function sqimap_mailbox_parse ($line, $line_lsub, $dm) {
124 global $folder_prefix;
125
126 // Process each folder line
127 for ($g=0; $g < count($line); $g++) {
128
129 // Store the raw IMAP reply
130 $boxes[$g]["raw"] = $line[$g];
131
132 // Count number of delimiters ($dm) in folder name
133 $mailbox = trim($line_lsub[$g]);
134 $dm_count = countCharInString($mailbox, $dm);
135 if (substr($mailbox, -1) == $dm)
136 $dm_count--; // If name ends in delimiter - decrement count by one
137
138 // Format folder name, but only if it's a INBOX.* or have
139 // a parent.
140 $boxesbyname[$mailbox] = $g;
141 $parentfolder = readMailboxParent($mailbox, $dm);
142 if((eregi("^inbox".quotemeta($dm), $mailbox)) ||
143 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
144 $indent = $dm_count - (countCharInString($folder_prefix, $dm));
145 $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $indent);
146 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
147 } else {
148 $boxes[$g]["formatted"] = $mailbox;
149 }
150
151 $boxes[$g]["unformatted-dm"] = $mailbox;
152 if (substr($mailbox, -1) == $dm)
153 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
154 $boxes[$g]["unformatted"] = $mailbox;
155 $boxes[$g]["id"] = $g;
156
157 ereg("\(([^)]*)\)",$line[$g],$regs);
158 $flags = trim(strtolower(str_replace("\\", "",$regs[1])));
159 if ($flags) {
160 $boxes[$g]["flags"] = explode(" ", $flags);
161 }
162 }
163
164 return $boxes;
165 }
166
167 /* patch from dave_michmerhuizen@yahoo.com
168 * allows case insensativity when sorting folders
169 */
170 function _icmp ($a, $b) {
171 return strcasecmp($a, $b);
172 }
173
174 /******************************************************************************
175 ** Returns sorted mailbox lists in several different ways.
176 ** See comment on sqimap_mailbox_parse() for info about the returned array.
177 ******************************************************************************/
178 function sqimap_mailbox_list ($imap_stream) {
179 global $load_prefs_php, $prefs_php, $config_php;
180 global $data_dir, $username, $list_special_folders_first;
181 global $trash_folder, $sent_folder;
182 global $move_to_trash, $move_to_sent;
183
184 $inbox_in_list = false;
185 $inbox_subscribed = false;
186
187 if (!isset($load_prefs_php)) include "../src/load_prefs.php";
188 else global $folder_prefix;
189 if (!function_exists ("ary_sort")) include "../functions/array.php";
190
191 $dm = sqimap_get_delimiter ($imap_stream);
192
193 /** LSUB array **/
194 $inbox_subscribed = false;
195 fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n");
196 $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
197
198 for ($i=0;$i < count($lsub_ary); $i++) {
199 $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
200 if ($sorted_lsub_ary[$i] == "INBOX")
201 $inbox_subscribed = true;
202 }
203 $new_ary = array();
204 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
205 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
206 $new_ary[] = $sorted_lsub_ary[$i];
207 }
208 }
209 $sorted_lsub_ary = $new_ary;
210 if (isset($sorted_lsub_ary)) {
211 usort($sorted_lsub_ary, "_icmp");
212 //sort($sorted_lsub_ary);
213 }
214
215 /** LIST array **/
216 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
217 if (substr($sorted_lsub_ary[$i], -1) == $dm)
218 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
219 else
220 $mbx = $sorted_lsub_ary[$i];
221
222 fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n");
223 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
224 $sorted_list_ary[$i] = $read[0];
225 if (find_mailbox_name($sorted_list_ary[$i]) == "INBOX")
226 $inbox_in_list = true;
227 }
228
229 /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/
230 if ($inbox_subscribed == false || $inbox_in_list == false) {
231 fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
232 $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
233
234 $pos = count($sorted_list_ary);
235 $sorted_list_ary[$pos] = $inbox_ary[0];
236
237 $pos = count($sorted_lsub_ary);
238 $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]);
239 }
240
241 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
242
243
244 /** Now, lets sort for special folders **/
245
246 $boxesnew = Array();
247
248 // Find INBOX
249 for ($i = 0; $i < count($boxes); $i++) {
250 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
251 $boxesnew[] = $boxes[$i];
252 $boxes[$i]["used"] = true;
253 $i = count($boxes);
254 }
255 }
256
257 if ($list_special_folders_first == true) {
258
259 // Then list special folders and their subfolders
260 for ($i = 0 ; $i <= count($boxes) ; $i++) {
261 if((eregi("^".$trash_folder.'$', $boxes[$i]["unformatted"]) ||
262 eregi("^".$trash_folder.quotemeta($dm), $boxes[$i]["unformatted"]) ) &&
263 ($move_to_trash)) {
264 $boxesnew[] = $boxes[$i];
265 $boxes[$i]["used"] = true;
266 }
267 else if((eregi("^".$sent_folder.'$', $boxes[$i]["unformatted"]) ||
268 eregi("^".$sent_folder.quotemeta($dm), $boxes[$i]["unformatted"]) ) &&
269 ($move_to_sent)) {
270 $boxesnew[] = $boxes[$i];
271 $boxes[$i]["used"] = true;
272 }
273 }
274
275 // Put INBOX.* folders ahead of the rest
276 for ($i = 0; $i <= count($boxes); $i++) {
277 if (eregi("^inbox\.", $boxes[$i]["unformatted"]) &&
278 ($boxes[$i]["used"] == false)) {
279 $boxesnew[] = $boxes[$i];
280 $boxes[$i]["used"] = true;
281 }
282 }
283
284 }
285
286 // Rest of the folders
287 for ($i = 0; $i < count($boxes); $i++) {
288 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
289 ($boxes[$i]["used"] == false)) {
290 $boxesnew[] = $boxes[$i];
291 $boxes[$i]["used"] = true;
292 }
293 }
294
295 return $boxesnew;
296 }
297
298 /******************************************************************************
299 ** Returns a list of all folders, subscribed or not
300 ******************************************************************************/
301 function sqimap_mailbox_list_all ($imap_stream) {
302 global $list_special_folders_first, $folder_prefix;
303
304 if (!function_exists ("ary_sort"))
305 include ("../functions/array.php");
306
307 $dm = sqimap_get_delimiter ($imap_stream);
308
309 fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
310 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
311 $g = 0;
312 $phase = "inbox";
313
314 for ($i = 0; $i < count($read_ary); $i++) {
315 if (substr ($read_ary[$i], 0, 4) != "a001") {
316
317 // Store the raw IMAP reply
318 $boxes[$g]["raw"] = $read_ary[$i];
319
320 // Count number of delimiters ($dm) in folder name
321 $mailbox = find_mailbox_name($read_ary[$i]);
322 $dm_count = countCharInString($mailbox, $dm);
323 if (substr($mailbox, -1) == $dm)
324 $dm_count--; // If name ends in delimiter - decrement count by one
325
326 // Format folder name, but only if it's a INBOX.* or have
327 // a parent.
328 $boxesbyname[$mailbox] = $g;
329 $parentfolder = readMailboxParent($mailbox, $dm);
330 if((eregi("^inbox".quotemeta($dm), $mailbox)) ||
331 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
332 $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $dm_count);
333 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
334 } else {
335 $boxes[$g]["formatted"] = $mailbox;
336 }
337
338 $boxes[$g]["unformatted-dm"] = $mailbox;
339 if (substr($mailbox, -1) == $dm)
340 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
341 $boxes[$g]["unformatted"] = $mailbox;
342 $boxes[$g]["id"] = $g;
343
344 /** Now lets get the flags for this mailbox **/
345 fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
346 $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
347
348 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
349 $flags = substr($flags, 0, strpos($flags, ")"));
350 $flags = str_replace("\\", "", $flags);
351 $flags = trim(strtolower($flags));
352 if ($flags) {
353 $boxes[$g]["flags"] = explode(" ", $flags);
354 }
355 }
356 $g++;
357 }
358 if(is_array($boxes)) {
359 $boxes = ary_sort ($boxes, "unformatted", 1);
360 }
361
362 return $boxes;
363 }
364
365 ?>