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