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