59177427 |
1 | <?php |
bccadd02 |
2 | |
35586184 |
3 | /** |
4 | * imap_mailbox.php |
5 | * |
15e6162e |
6 | * Copyright (c) 1999-2002 The SquirrelMail Project Team |
35586184 |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
8 | * |
9 | * This impliments all functions that manipulate mailboxes |
10 | * |
11 | * $Id$ |
12 | */ |
f435778e |
13 | |
1e18bf95 |
14 | function isBoxBelow( $box2, $box1 ) { |
90de1755 |
15 | |
1e18bf95 |
16 | global $delimiter, $folder_prefix, $imap_server_type; |
90de1755 |
17 | |
fdc05b69 |
18 | if ( $imap_server_type == 'uw' ) { |
1e18bf95 |
19 | $boxs = $box2; |
20 | $i = strpos( $box1, $delimiter, strlen( $folder_prefix ) ); |
fdc05b69 |
21 | if ( $i === FALSE ) { |
1e18bf95 |
22 | $i = strlen( $box2 ); |
fdc05b69 |
23 | } |
24 | } else { |
1e18bf95 |
25 | $boxs = $box2 . $delimiter; |
fdc05b69 |
26 | // Skip next second delimiter |
1e18bf95 |
27 | $i = strpos( $box1, $delimiter ); |
28 | $i = strpos( $box1, $delimiter, $i + 1 ); |
fdc05b69 |
29 | if ( $i === FALSE ) { |
1e18bf95 |
30 | $i = strlen( $box2 ); |
72b9aff9 |
31 | } else { |
fdc05b69 |
32 | $i++; |
4d6bd355 |
33 | } |
7980d569 |
34 | } |
72b9aff9 |
35 | |
1e18bf95 |
36 | return( substr( $box1, 0, $i ) == substr( $boxs, 0, $i ) ); |
37 | |
38 | } |
39 | |
40 | /* |
41 | Defines Special Mail Boxes |
42 | */ |
43 | function isSpecialMailbox( $box ) { |
44 | |
45 | global $trash_folder, $sent_folder, $draft_folder, |
46 | $move_to_trash, $move_to_sent, $save_as_draft, |
47 | $delimiter, $folder_prefix, $imap_server_type; |
48 | |
90de1755 |
49 | $ret = ( (strtolower($box) == 'inbox') || |
1e18bf95 |
50 | ( $move_to_trash && isBoxBelow( $box, $trash_folder ) ) || |
51 | ( $move_to_sent && isBoxBelow( $box, $sent_folder )) || |
90de1755 |
52 | ($box == $draft_folder && |
53 | $save_as_draft) ); |
54 | |
55 | return( $ret ); |
56 | |
57 | } |
58 | |
35586184 |
59 | /************************* |
60 | ** Expunges a mailbox ** |
61 | *************************/ |
90de1755 |
62 | function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = TRUE) |
43b698c7 |
63 | { |
1c72b151 |
64 | $read = sqimap_run_command($imap_stream, 'EXPUNGE', |
65 | $handle_errors, $response, $message); |
43b698c7 |
66 | } |
67 | |
68 | |
69 | /****************************************************************************** |
70 | ** Checks whether or not the specified mailbox exists |
71 | ******************************************************************************/ |
72 | function sqimap_mailbox_exists ($imap_stream, $mailbox) |
73 | { |
74 | if (! isset($mailbox)) { |
75 | return false; |
76 | } |
1c72b151 |
77 | $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"", |
90de1755 |
78 | TRUE, $response, $message); |
43b698c7 |
79 | return isset($mbx[0]); |
80 | } |
81 | |
82 | /****************************************************************************** |
83 | ** Selects a mailbox |
84 | ******************************************************************************/ |
85 | function sqimap_mailbox_select ($imap_stream, $mailbox, |
90de1755 |
86 | $hide=TRUE, $recent=false) |
43b698c7 |
87 | { |
88 | global $auto_expunge; |
89 | |
90 | if ( $mailbox == 'None' ) { |
91 | return; |
92 | } |
93 | |
1c72b151 |
94 | $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"", |
90de1755 |
95 | TRUE, $response, $message); |
43b698c7 |
96 | if ($recent) { |
97 | for ($i=0; $i<count($read); $i++) { |
bccadd02 |
98 | if (strpos(strtolower($read[$i]), 'recent')) { |
43b698c7 |
99 | $r = explode(' ', $read[$i]); |
04632dbc |
100 | } |
43b698c7 |
101 | } |
102 | return $r[1]; |
103 | } |
104 | if ($auto_expunge) { |
1a16af11 |
105 | $tmp = sqimap_run_command($imap_stream, 'EXPUNGE', |
1c72b151 |
106 | false, $a, $b); |
43b698c7 |
107 | } |
108 | } |
109 | |
110 | |
111 | |
112 | /****************************************************************************** |
113 | ** Creates a folder |
114 | ******************************************************************************/ |
115 | function sqimap_mailbox_create ($imap_stream, $mailbox, $type) |
116 | { |
117 | global $delimiter; |
118 | if (strtolower($type) == 'noselect') { |
119 | $mailbox = $mailbox.$delimiter; |
120 | } |
1c72b151 |
121 | $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"", |
90de1755 |
122 | TRUE, $response, $message); |
43b698c7 |
123 | |
124 | sqimap_subscribe ($imap_stream, $mailbox); |
125 | } |
126 | |
127 | |
128 | |
129 | /****************************************************************************** |
130 | ** Subscribes to an existing folder |
131 | ******************************************************************************/ |
132 | function sqimap_subscribe ($imap_stream, $mailbox) |
133 | { |
1a16af11 |
134 | $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"", |
90de1755 |
135 | TRUE, $response, $message); |
43b698c7 |
136 | } |
137 | |
138 | |
139 | |
140 | /****************************************************************************** |
141 | ** Unsubscribes to an existing folder |
142 | ******************************************************************************/ |
143 | function sqimap_unsubscribe ($imap_stream, $mailbox) |
144 | { |
145 | global $imap_server_type; |
146 | |
1a16af11 |
147 | $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"", |
90de1755 |
148 | TRUE, $response, $message); |
43b698c7 |
149 | } |
150 | |
151 | |
152 | |
153 | /****************************************************************************** |
154 | ** This function simply deletes the given folder |
155 | ******************************************************************************/ |
156 | function sqimap_mailbox_delete ($imap_stream, $mailbox) |
157 | { |
1c72b151 |
158 | $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"", |
90de1755 |
159 | TRUE, $response, $message); |
43b698c7 |
160 | sqimap_unsubscribe ($imap_stream, $mailbox); |
161 | } |
162 | |
163 | /*********************************************************************** |
164 | ** Determines if the user is subscribed to the folder or not |
165 | **********************************************************************/ |
166 | function sqimap_mailbox_is_subscribed($imap_stream, $folder) |
167 | { |
168 | $boxes = sqimap_mailbox_list ($imap_stream); |
169 | foreach ($boxes as $ref) { |
170 | if ($ref['unformatted'] == $folder) { |
90de1755 |
171 | return TRUE; |
43b698c7 |
172 | } |
173 | } |
174 | return false; |
175 | } |
176 | |
1c52ba77 |
177 | /* |
178 | Renames a mailbox |
179 | */ |
180 | function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) { |
43b698c7 |
181 | |
1c52ba77 |
182 | if ( $old_name <> $new_name ) { |
183 | |
184 | global $delimiter; |
185 | |
186 | if ( substr( $old_name, -1 ) == $delimiter ) { |
187 | $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 ); |
188 | $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 ); |
189 | $postfix = $delimiter; |
190 | $boxes = sqimap_mailbox_list($imap_stream); |
191 | } else { |
192 | $postfix = ''; |
193 | $boxes = FALSE; |
194 | } |
195 | |
1c72b151 |
196 | $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"'; |
197 | $data = sqimap_run_command($imap_stream, $cmd, |
1c52ba77 |
198 | TRUE, $response, $message); |
199 | sqimap_unsubscribe($imap_stream, $old_name.$postfix); |
200 | sqimap_subscribe($imap_stream, $new_name.$postfix); |
201 | |
202 | if ( $boxes ) { |
203 | // Sub-unsub subfolders |
204 | $l = strlen( $old_name ) + 1; |
205 | $p = 'unformatted'; |
206 | foreach ( $boxes as $box ) { |
207 | if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) { |
208 | sqimap_unsubscribe($imap_stream, $box[$p]); |
209 | sqimap_subscribe($imap_stream, |
210 | $new_name . $delimiter . substr( $box[$p], $l ) ); |
211 | } |
212 | } |
213 | } |
214 | |
215 | } |
216 | |
217 | } |
43b698c7 |
218 | |
219 | /****************************************************************************** |
220 | ** Formats a mailbox into 4 parts for the $boxes array |
221 | ** |
222 | ** The four parts are: |
223 | ** |
224 | ** raw - Raw LIST/LSUB response from the IMAP server |
225 | ** formatted - nicely formatted folder name |
226 | ** unformatted - unformatted, but with delimiter at end removed |
227 | ** unformatted-dm - folder name as it appears in raw response |
228 | ** unformatted-disp - unformatted without $folder_prefix |
229 | ** |
230 | ******************************************************************************/ |
231 | function sqimap_mailbox_parse ($line, $line_lsub) |
232 | { |
233 | global $folder_prefix, $delimiter; |
234 | |
235 | /* Process each folder line */ |
236 | for ($g=0; $g < count($line); $g++) { |
237 | |
238 | /* Store the raw IMAP reply */ |
239 | if (isset($line[$g])) { |
a7ea7540 |
240 | $boxes[$g]["raw"] = $line[$g]; |
43b698c7 |
241 | } |
242 | else { |
a7ea7540 |
243 | $boxes[$g]["raw"] = ""; |
43b698c7 |
244 | } |
245 | |
246 | |
247 | /* Count number of delimiters ($delimiter) in folder name */ |
248 | $mailbox = trim($line_lsub[$g]); |
602e1431 |
249 | $dm_count = substr_count($mailbox, $delimiter); |
43b698c7 |
250 | if (substr($mailbox, -1) == $delimiter) { |
251 | /* If name ends in delimiter - decrement count by one */ |
252 | $dm_count--; |
253 | } |
254 | |
255 | /* Format folder name, but only if it's a INBOX.* or have */ |
256 | /* a parent. */ |
257 | $boxesbyname[$mailbox] = $g; |
258 | $parentfolder = readMailboxParent($mailbox, $delimiter); |
259 | if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") || |
260 | (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) || |
261 | ( isset($boxesbyname[$parentfolder]) && |
262 | (strlen($parentfolder) > 0) ) ) { |
602e1431 |
263 | $indent = $dm_count - ( substr_count($folder_prefix, $delimiter)); |
43b698c7 |
264 | if ($indent > 0) { |
90de1755 |
265 | $boxes[$g]['formatted'] = str_repeat(" ", $indent); |
43b698c7 |
266 | } |
267 | else { |
90de1755 |
268 | $boxes[$g]['formatted'] = ''; |
43b698c7 |
269 | } |
90de1755 |
270 | $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter); |
43b698c7 |
271 | } |
272 | else { |
90de1755 |
273 | $boxes[$g]['formatted'] = $mailbox; |
43b698c7 |
274 | } |
90de1755 |
275 | |
43b698c7 |
276 | $boxes[$g]['unformatted-dm'] = $mailbox; |
277 | if (substr($mailbox, -1) == $delimiter) { |
8e9e8afa |
278 | $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); |
43b698c7 |
279 | } |
280 | $boxes[$g]['unformatted'] = $mailbox; |
281 | if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) { |
631b9da3 |
282 | $mailbox = substr($mailbox, strlen($folder_prefix)); |
43b698c7 |
283 | } |
284 | $boxes[$g]['unformatted-disp'] = $mailbox; |
285 | $boxes[$g]['id'] = $g; |
90de1755 |
286 | |
43b698c7 |
287 | $boxes[$g]['flags'] = array(); |
288 | if (isset($line[$g])) { |
36dfb0c9 |
289 | ereg("\(([^)]*)\)",$line[$g],$regs); |
1a7e1e97 |
290 | $flags = trim(strtolower(str_replace('\\', '',$regs[1]))); |
43b698c7 |
291 | if ($flags) { |
292 | $boxes[$g]['flags'] = explode(' ', $flags); |
293 | } |
294 | } |
295 | } |
90de1755 |
296 | |
43b698c7 |
297 | return $boxes; |
298 | } |
299 | |
a3439b27 |
300 | /** |
301 | * Sorting function used to sort mailbox names. |
302 | * + Original patch from dave_michmerhuizen@yahoo.com |
303 | * + Allows case insensitivity when sorting folders |
304 | * + Takes care of the delimiter being sorted to the end, causing |
305 | * subfolders to be listed in below folders that are prefixed |
306 | * with their parent folders name. |
307 | * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar |
308 | * Without special sort function: foobar between foo and foo.bar |
309 | * With special sort function: foobar AFTER foo and foo.bar :) |
43b698c7 |
310 | */ |
a3439b27 |
311 | function user_strcasecmp($a, $b) { |
312 | global $delimiter; |
313 | |
314 | /* Calculate the length of some strings. */ |
315 | $a_length = strlen($a); |
316 | $b_length = strlen($b); |
317 | $min_length = min($a_length, $b_length); |
318 | $delimiter_length = strlen($delimiter); |
319 | |
320 | /* Set the initial result value. */ |
321 | $result = 0; |
322 | |
323 | /* Check the strings... */ |
324 | for ($c = 0; $c < $min_length; ++$c) { |
325 | $a_del = substr($a, $c, $delimiter_length); |
326 | $b_del = substr($b, $c, $delimiter_length); |
327 | |
328 | if (($a_del == $delimiter) && ($b_del == $delimiter)) { |
329 | $result = 0; |
330 | } else if (($a_del == $delimiter) && ($b_del != $delimiter)) { |
a3439b27 |
331 | $result = -1; |
c64c33f4 |
332 | } else if (($a_del != $delimiter) && ($b_del == $delimiter)) { |
333 | $result = 1; |
a3439b27 |
334 | } else { |
335 | $result = strcasecmp($a{$c}, $b{$c}); |
336 | } |
337 | |
338 | if ($result != 0) { |
339 | break; |
340 | } |
341 | } |
90de1755 |
342 | |
a3439b27 |
343 | /* If one string is a prefix of the other... */ |
344 | if ($result == 0) { |
345 | if ($a_length < $b_length) { |
346 | $result = -1; |
347 | } else if ($a_length > $b_length) { |
348 | $result = 1; |
349 | } |
350 | } |
351 | |
352 | return ($result); |
43b698c7 |
353 | } |
354 | |
355 | |
356 | /****************************************************************************** |
357 | ** Returns sorted mailbox lists in several different ways. |
358 | ** See comment on sqimap_mailbox_parse() for info about the returned array. |
359 | ******************************************************************************/ |
a3439b27 |
360 | function sqimap_mailbox_list ($imap_stream) { |
90de1755 |
361 | global $data_dir, $username, $list_special_folders_first, |
362 | $folder_prefix, $trash_folder, $sent_folder, $draft_folder, |
363 | $move_to_trash, $move_to_sent, $save_as_draft, |
364 | $delimiter; |
365 | |
43b698c7 |
366 | $inbox_in_list = false; |
367 | $inbox_subscribed = false; |
90de1755 |
368 | |
43b698c7 |
369 | require_once('../src/load_prefs.php'); |
370 | require_once('../functions/array.php'); |
90de1755 |
371 | |
43b698c7 |
372 | /** LSUB array **/ |
1c72b151 |
373 | $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"", |
90de1755 |
374 | TRUE, $response, $message); |
375 | |
43b698c7 |
376 | /* Section about removing the last element was removed */ |
377 | /* We don't return "* OK" anymore from sqimap_read_data */ |
90de1755 |
378 | |
43b698c7 |
379 | $sorted_lsub_ary = array(); |
380 | for ($i=0;$i < count($lsub_ary); $i++) { |
381 | /* Workaround for EIMS */ |
382 | /* Doesn't work if the mailbox name is multiple lines */ |
383 | if (isset($lsub_ary[$i + 1]) && |
90de1755 |
384 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
43b698c7 |
385 | $lsub_ary[$i], $regs)) { |
386 | $i ++; |
387 | $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . |
388 | '"' . $regs[2]; |
389 | } |
390 | $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]); |
391 | $sorted_lsub_ary[] = $temp_mailbox_name; |
392 | if (strtoupper($temp_mailbox_name) == 'INBOX') { |
90de1755 |
393 | $inbox_subscribed = TRUE; |
43b698c7 |
394 | } |
395 | } |
396 | $new_ary = array(); |
397 | for ($i=0; $i < count($sorted_lsub_ary); $i++) { |
398 | if (!in_array($sorted_lsub_ary[$i], $new_ary)) { |
ee62ce13 |
399 | $new_ary[] = $sorted_lsub_ary[$i]; |
43b698c7 |
400 | } |
401 | } |
402 | $sorted_lsub_ary = $new_ary; |
403 | if (isset($sorted_lsub_ary)) { |
404 | usort($sorted_lsub_ary, 'user_strcasecmp'); |
90de1755 |
405 | } |
406 | |
43b698c7 |
407 | /** LIST array **/ |
408 | $sorted_list_ary = array(); |
409 | for ($i=0; $i < count($sorted_lsub_ary); $i++) { |
410 | if (substr($sorted_lsub_ary[$i], -1) == $delimiter) { |
f9b3e5d9 |
411 | $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1); |
43b698c7 |
412 | } |
413 | else { |
f9b3e5d9 |
414 | $mbx = $sorted_lsub_ary[$i]; |
43b698c7 |
415 | } |
90de1755 |
416 | |
1c72b151 |
417 | $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"", |
90de1755 |
418 | TRUE, $response, $message); |
43b698c7 |
419 | /* Another workaround for EIMS */ |
90de1755 |
420 | if (isset($read[1]) && |
421 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
43b698c7 |
422 | $read[0], $regs)) { |
423 | $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) . |
90de1755 |
424 | '"' . $regs[2]; |
43b698c7 |
425 | } |
90de1755 |
426 | |
43b698c7 |
427 | if (isset($sorted_list_ary[$i])) { |
90de1755 |
428 | $sorted_list_ary[$i] = ''; |
43b698c7 |
429 | } |
90de1755 |
430 | |
43b698c7 |
431 | if (isset($read[0])) { |
44a3ca20 |
432 | $sorted_list_ary[$i] = $read[0]; |
43b698c7 |
433 | } |
434 | else { |
90de1755 |
435 | $sorted_list_ary[$i] = ''; |
43b698c7 |
436 | } |
90de1755 |
437 | |
438 | if (isset($sorted_list_ary[$i]) && |
439 | strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') { |
440 | $inbox_in_list = TRUE; |
43b698c7 |
441 | } |
442 | } |
90de1755 |
443 | |
43b698c7 |
444 | /** |
445 | * Just in case they're not subscribed to their inbox, |
446 | * we'll get it for them anyway |
447 | */ |
448 | if ($inbox_subscribed == false || $inbox_in_list == false) { |
1c72b151 |
449 | $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"", |
90de1755 |
450 | TRUE, $response, $message); |
43b698c7 |
451 | /* Another workaround for EIMS */ |
452 | if (isset($inbox_ary[1]) && |
90de1755 |
453 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
43b698c7 |
454 | $inbox_ary[0], $regs)) { |
455 | $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) . |
456 | '"' . $regs[2]; |
457 | } |
90de1755 |
458 | |
43b698c7 |
459 | $sorted_list_ary[] = $inbox_ary[0]; |
460 | $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]); |
461 | } |
90de1755 |
462 | |
43b698c7 |
463 | $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary); |
90de1755 |
464 | |
43b698c7 |
465 | /** Now, lets sort for special folders **/ |
90de1755 |
466 | $boxesnew = $used = array(); |
467 | |
43b698c7 |
468 | /* Find INBOX */ |
90de1755 |
469 | foreach ( $boxes as $k => $box ) { |
470 | if ( strtolower($box['unformatted']) == 'inbox') { |
471 | $boxesnew[] = $box; |
472 | $used[$k] = TRUE; |
473 | } else { |
474 | $used[$k] = FALSE; |
43b698c7 |
475 | } |
476 | } |
90de1755 |
477 | |
43b698c7 |
478 | /* List special folders and their subfolders, if requested. */ |
90de1755 |
479 | if ($list_special_folders_first == TRUE) { |
480 | |
481 | foreach ( $boxes as $k => $box ) { |
482 | if ( !$used[$k] && |
483 | isSpecialMailbox( $box['unformatted'] ) ) { |
484 | $boxesnew[] = $box; |
485 | $used[$k] = TRUE; |
2752bb16 |
486 | } |
43b698c7 |
487 | } |
90de1755 |
488 | |
43b698c7 |
489 | } |
90de1755 |
490 | |
43b698c7 |
491 | /* Rest of the folders */ |
90de1755 |
492 | foreach ( $boxes as $k => $box ) { |
493 | if ( !$used[$k] ) { |
494 | $boxesnew[] = $box; |
43b698c7 |
495 | } |
496 | } |
90de1755 |
497 | |
498 | return( $boxesnew ); |
43b698c7 |
499 | } |
500 | |
90de1755 |
501 | /* |
502 | * Returns a list of all folders, subscribed or not |
503 | */ |
43b698c7 |
504 | function sqimap_mailbox_list_all ($imap_stream) |
505 | { |
506 | global $list_special_folders_first, $folder_prefix; |
507 | global $delimiter; |
90de1755 |
508 | |
509 | if (!function_exists('ary_sort')) { |
43b698c7 |
510 | include_once('../functions/array.php'); |
511 | } |
90de1755 |
512 | |
43b698c7 |
513 | $ssid = sqimap_session_id(); |
90de1755 |
514 | $lsid = strlen( $ssid ); |
43b698c7 |
515 | fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n"); |
90de1755 |
516 | $read_ary = sqimap_read_data ($imap_stream, $ssid, TRUE, $response, $message); |
43b698c7 |
517 | $g = 0; |
90de1755 |
518 | $phase = 'inbox'; |
519 | |
43b698c7 |
520 | for ($i = 0; $i < count($read_ary); $i++) { |
521 | /* Another workaround for EIMS */ |
522 | if (isset($read_ary[$i + 1]) && |
90de1755 |
523 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
43b698c7 |
524 | $read_ary[$i], $regs)) { |
525 | $i ++; |
90de1755 |
526 | $read_ary[$i] = $regs[1] . '"' . |
1c52ba77 |
527 | addslashes(trim($read_ary[$i])) . |
528 | '"' . $regs[2]; |
43b698c7 |
529 | } |
90de1755 |
530 | if (substr($read_ary[$i], 0, $lsid) != $ssid ) { |
531 | |
43b698c7 |
532 | /* Store the raw IMAP reply */ |
8e9e8afa |
533 | $boxes[$g]["raw"] = $read_ary[$i]; |
1c52ba77 |
534 | |
43b698c7 |
535 | /* Count number of delimiters ($delimiter) in folder name */ |
8e9e8afa |
536 | $mailbox = find_mailbox_name($read_ary[$i]); |
602e1431 |
537 | $dm_count = substr_count($mailbox, $delimiter); |
43b698c7 |
538 | if (substr($mailbox, -1) == $delimiter) { |
0cfd745a |
539 | /* If name ends in delimiter - decrement count by one */ |
90de1755 |
540 | $dm_count--; |
43b698c7 |
541 | } |
90de1755 |
542 | |
43b698c7 |
543 | /* Format folder name, but only if it's a INBOX.* or have */ |
544 | /* a parent. */ |
2752bb16 |
545 | $boxesbyname[$mailbox] = $g; |
525b7ae6 |
546 | $parentfolder = readMailboxParent($mailbox, $delimiter); |
90de1755 |
547 | if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) || |
146e0c45 |
548 | (ereg('^'.$folder_prefix, $mailbox)) || |
2752bb16 |
549 | ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) { |
43b698c7 |
550 | if ($dm_count) { |
90de1755 |
551 | $boxes[$g]['formatted'] = str_repeat(' ', $dm_count); |
43b698c7 |
552 | } |
553 | else { |
90de1755 |
554 | $boxes[$g]['formatted'] = ''; |
43b698c7 |
555 | } |
90de1755 |
556 | $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter); |
2752bb16 |
557 | } |
43b698c7 |
558 | else { |
90de1755 |
559 | $boxes[$g]['formatted'] = $mailbox; |
43b698c7 |
560 | } |
90de1755 |
561 | |
8e9e8afa |
562 | $boxes[$g]["unformatted-dm"] = $mailbox; |
43b698c7 |
563 | if (substr($mailbox, -1) == $delimiter) { |
564 | $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); |
565 | } |
90de1755 |
566 | $boxes[$g]['unformatted'] = $mailbox; |
567 | $boxes[$g]['unformatted-disp'] = |
0cfd745a |
568 | ereg_replace('^' . $folder_prefix, '', $mailbox); |
90de1755 |
569 | $boxes[$g]['id'] = $g; |
570 | |
8e9e8afa |
571 | /** Now lets get the flags for this mailbox **/ |
1c72b151 |
572 | $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"", |
90de1755 |
573 | TRUE, $response, $message); |
574 | |
43b698c7 |
575 | /* Another workaround for EIMS */ |
576 | if (isset($read_mlbx[1]) && |
90de1755 |
577 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
43b698c7 |
578 | $read_mlbx[0], $regs)) { |
1c52ba77 |
579 | $read_mlbx[0] = $regs[1] . '"' . |
580 | addslashes(trim($read_mlbx[1])) . |
581 | '"' . $regs[2]; |
43b698c7 |
582 | } |
90de1755 |
583 | |
584 | $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+1); |
585 | $flags = substr($flags, 0, strpos($flags, ')')); |
146e0c45 |
586 | $flags = str_replace('\\', '', $flags); |
8e9e8afa |
587 | $flags = trim(strtolower($flags)); |
588 | if ($flags) { |
1c52ba77 |
589 | $boxes[$g]['flags'] = explode(' ', $flags); |
90de1755 |
590 | } else { |
43b698c7 |
591 | $boxes[$g]['flags'] = array(); |
12d61439 |
592 | } |
43b698c7 |
593 | } |
594 | $g++; |
595 | } |
596 | if(is_array($boxes)) { |
1c52ba77 |
597 | $boxes = ary_sort ($boxes, 'unformatted', 1); |
43b698c7 |
598 | } |
90de1755 |
599 | |
43b698c7 |
600 | return $boxes; |
601 | } |
5bdd7223 |
602 | |
f7b1b3b1 |
603 | ?> |