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