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