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