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