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 | |
60b5724d |
16 | class mailboxes { |
17 | var $mailboxname_full = '', $mailboxname_sub= '', $is_noselect = false, |
18 | $is_special = false, $is_root = false, $is_inbox = false, $is_sent = false, |
19 | $is_trash = false, $is_draft = false, $mbxs = array(), |
20 | $unseen = false, $total = false; |
21 | |
22 | function addMbx($mbx, $delimiter, $start, $specialfirst) { |
23 | $ary = explode($delimiter, $mbx->mailboxname_full); |
24 | $mbx_parent = &$this; |
25 | for ($i=$start; $i < (count($ary) -1); $i++) { |
26 | $mbx_childs = &$mbx_parent->mbxs; |
27 | $found = false; |
28 | foreach ($mbx_childs as $key => $parent) { |
29 | if ($parent->mailboxname_sub == $ary[$i]) { |
30 | $mbx_parent = &$mbx_parent->mbxs[$key]; |
31 | $found = true; |
32 | } |
33 | } |
34 | if (!$found) { |
35 | $no_select_mbx = new mailboxes(); |
36 | if (isset($mbx_parent->mailboxname_full) && $mbx_parent->mailboxname_full != '') { |
37 | $no_select_mbx->mailboxname_full = $mbx_parent->mailboxname_full.$delimiter.$ary[$i]; |
38 | } else { |
39 | $no_select_mbx->mailboxname_full = $ary[$i]; |
40 | } |
41 | $no_select_mbx->mailboxname_sub = $ary[$i]; |
42 | $no_select_mbx->is_noselect = true; |
43 | $mbx_parent->mbxs[] = $no_select_mbx; |
44 | $i--; |
45 | } |
46 | |
47 | } |
48 | $mbx_parent->mbxs[] = $mbx; |
49 | if ($mbx->is_special && $specialfirst) { |
50 | usort($mbx_parent->mbxs, 'sortSpecialMbx'); |
51 | } |
52 | |
53 | } |
54 | } |
55 | |
56 | function sortSpecialMbx($a, $b) { |
57 | if ($a->is_inbox) { |
58 | $acmp = '0'. $a->mailboxname_full; |
59 | } else if ($a->is_special) { |
60 | $acmp = '1'. $a->mailboxname_full; |
61 | } else { |
62 | $acmp = '2' . $a->mailboxname_full; |
63 | } |
64 | if ($b->is_inbox) { |
65 | $bcmp = '0'. $b->mailboxname_full; |
66 | }else if ($b->is_special) { |
67 | $bcmp = '1' . $b->mailboxname_full; |
68 | } else { |
69 | $bcmp = '2' . $b->mailboxname_full; |
70 | } |
71 | if ($acmp == $bcmp) return 0; |
72 | return ($acmp>$bcmp) ? 1: -1; |
73 | } |
74 | |
75 | |
79e07c7e |
76 | function find_mailbox_name ($mailbox) { |
77 | if (ereg(" *\"([^\r\n\"]*)\"[ \r\n]*$", $mailbox, $regs)) |
78 | return $regs[1]; |
79 | ereg(" *([^ \r\n\"]*)[ \r\n]*$",$mailbox,$regs); |
80 | return $regs[1]; |
81 | |
82 | } |
83 | |
97b1248c |
84 | /** |
85 | * If $haystack is a full mailbox name, and $needle is the mailbox |
86 | * separator character, returns the second last part of the full |
87 | * mailbox name (i.e. the mailbox's parent mailbox) |
88 | */ |
89 | function readMailboxParent($haystack, $needle) { |
90 | |
91 | if ($needle == '') { |
92 | $ret = ''; |
93 | } else { |
94 | $parts = explode($needle, $haystack); |
95 | $elem = array_pop($parts); |
96 | while ($elem == '' && count($parts)) { |
97 | $elem = array_pop($parts); |
98 | } |
99 | $ret = join($needle, $parts); |
100 | } |
101 | return( $ret ); |
102 | } |
103 | |
104 | |
1e18bf95 |
105 | function isBoxBelow( $box2, $box1 ) { |
1e18bf95 |
106 | global $delimiter, $folder_prefix, $imap_server_type; |
90de1755 |
107 | |
fdc05b69 |
108 | if ( $imap_server_type == 'uw' ) { |
1e18bf95 |
109 | $boxs = $box2; |
110 | $i = strpos( $box1, $delimiter, strlen( $folder_prefix ) ); |
3411d4ec |
111 | if ( $i === false ) { |
1e18bf95 |
112 | $i = strlen( $box2 ); |
fdc05b69 |
113 | } |
114 | } else { |
1e18bf95 |
115 | $boxs = $box2 . $delimiter; |
3411d4ec |
116 | /* Skip next second delimiter */ |
1e18bf95 |
117 | $i = strpos( $box1, $delimiter ); |
118 | $i = strpos( $box1, $delimiter, $i + 1 ); |
3411d4ec |
119 | if ( $i === false ) { |
1e18bf95 |
120 | $i = strlen( $box2 ); |
72b9aff9 |
121 | } else { |
fdc05b69 |
122 | $i++; |
4d6bd355 |
123 | } |
7980d569 |
124 | } |
72b9aff9 |
125 | |
3411d4ec |
126 | return ( substr( $box1, 0, $i ) == substr( $boxs, 0, $i ) ); |
1e18bf95 |
127 | } |
128 | |
3411d4ec |
129 | /* Defines special mailboxes */ |
1e18bf95 |
130 | function isSpecialMailbox( $box ) { |
1e18bf95 |
131 | global $trash_folder, $sent_folder, $draft_folder, |
65c3ec94 |
132 | $move_to_trash, $move_to_sent, $save_as_draft; |
1e18bf95 |
133 | |
90de1755 |
134 | $ret = ( (strtolower($box) == 'inbox') || |
1e18bf95 |
135 | ( $move_to_trash && isBoxBelow( $box, $trash_folder ) ) || |
136 | ( $move_to_sent && isBoxBelow( $box, $sent_folder )) || |
ce628f00 |
137 | ($save_as_draft && $box == $draft_folder ) ); |
90de1755 |
138 | |
2586d588 |
139 | if ( !$ret ) { |
31524bcd |
140 | $ret = do_hook_function( 'special_mailbox', $box ); |
2586d588 |
141 | } |
142 | |
3411d4ec |
143 | return $ret; |
90de1755 |
144 | } |
145 | |
3411d4ec |
146 | /* Expunges a mailbox */ |
147 | function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true) { |
148 | $read = sqimap_run_command($imap_stream, 'EXPUNGE', $handle_errors, |
149 | $response, $message); |
43b698c7 |
150 | } |
151 | |
3411d4ec |
152 | /* Checks whether or not the specified mailbox exists */ |
1da22cda |
153 | function sqimap_mailbox_exists ($imap_stream, $mailbox) { |
43b698c7 |
154 | if (! isset($mailbox)) { |
155 | return false; |
156 | } |
1c72b151 |
157 | $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"", |
3411d4ec |
158 | true, $response, $message); |
43b698c7 |
159 | return isset($mbx[0]); |
160 | } |
161 | |
3411d4ec |
162 | /* Selects a mailbox */ |
43b698c7 |
163 | function sqimap_mailbox_select ($imap_stream, $mailbox, |
3411d4ec |
164 | $hide = true, $recent = false, $extrainfo = false) { |
43b698c7 |
165 | global $auto_expunge; |
f69feefe |
166 | |
43b698c7 |
167 | if ( $mailbox == 'None' ) { |
168 | return; |
169 | } |
f69feefe |
170 | |
1c72b151 |
171 | $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"", |
3411d4ec |
172 | true, $response, $message); |
43b698c7 |
173 | if ($recent) { |
174 | for ($i=0; $i<count($read); $i++) { |
bccadd02 |
175 | if (strpos(strtolower($read[$i]), 'recent')) { |
43b698c7 |
176 | $r = explode(' ', $read[$i]); |
04632dbc |
177 | } |
43b698c7 |
178 | } |
179 | return $r[1]; |
77b88425 |
180 | } else { |
181 | if ($auto_expunge) { |
3411d4ec |
182 | $tmp = sqimap_run_command($imap_stream, 'EXPUNGE', false, $a, $b); |
77b88425 |
183 | } |
732ec15d |
184 | if (isset( $extrainfo ) && $extrainfo) { |
f69feefe |
185 | $result = array(); |
186 | for ($i=0; $i<count($read); $i++) { |
187 | if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) { |
188 | $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ; |
189 | $result['PERMANENTFLAGS'] = $regs[1]; |
190 | } |
191 | else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) { |
192 | $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ; |
1da22cda |
193 | $result['FLAGS'] = $regs[1]; |
f69feefe |
194 | } |
195 | else if (preg_match("/(.*)EXISTS/i",$read[$i], $regs)) { |
196 | $result['EXISTS']=trim($regs[1]); |
197 | } |
198 | else if (preg_match("/(.*)RECENT/i",$read[$i], $regs)) { |
199 | $result['RECENT']=trim($regs[1]); |
200 | } |
201 | else if (preg_match("/\[UNSEEN(.*)\]/i",$read[$i], $regs)) { |
202 | $result['UNSEEN']=trim($regs[1]); |
203 | } |
204 | |
205 | } |
206 | return( $result ); |
207 | } |
43b698c7 |
208 | } |
209 | } |
210 | |
3411d4ec |
211 | /* Creates a folder */ |
212 | function sqimap_mailbox_create ($imap_stream, $mailbox, $type) { |
43b698c7 |
213 | global $delimiter; |
214 | if (strtolower($type) == 'noselect') { |
3411d4ec |
215 | $mailbox .= $delimiter; |
43b698c7 |
216 | } |
1c72b151 |
217 | $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"", |
3411d4ec |
218 | true, $response, $message); |
43b698c7 |
219 | sqimap_subscribe ($imap_stream, $mailbox); |
220 | } |
221 | |
3411d4ec |
222 | /* Subscribes to an existing folder */ |
223 | function sqimap_subscribe ($imap_stream, $mailbox) { |
1a16af11 |
224 | $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"", |
3411d4ec |
225 | true, $response, $message); |
43b698c7 |
226 | } |
227 | |
3411d4ec |
228 | /* Unsubscribes to an existing folder */ |
229 | function sqimap_unsubscribe ($imap_stream, $mailbox) { |
43b698c7 |
230 | global $imap_server_type; |
1a16af11 |
231 | $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"", |
3411d4ec |
232 | true, $response, $message); |
43b698c7 |
233 | } |
234 | |
3411d4ec |
235 | /* Deletes the given folder */ |
236 | function sqimap_mailbox_delete ($imap_stream, $mailbox) { |
78cc4b12 |
237 | global $data_dir, $username; |
1c72b151 |
238 | $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"", |
3411d4ec |
239 | true, $response, $message); |
43b698c7 |
240 | sqimap_unsubscribe ($imap_stream, $mailbox); |
3411d4ec |
241 | do_hook_function("rename_or_delete_folder", $args = array($mailbox, 'delete', '')); |
78cc4b12 |
242 | removePref($data_dir, $username, "thread_$mailbox"); |
43b698c7 |
243 | } |
244 | |
3411d4ec |
245 | /* Determines if the user is subscribed to the folder or not */ |
1da22cda |
246 | function sqimap_mailbox_is_subscribed($imap_stream, $folder) { |
1da22cda |
247 | $boxesall = sqimap_mailbox_list ($imap_stream); |
248 | foreach ($boxesall as $ref) { |
43b698c7 |
249 | if ($ref['unformatted'] == $folder) { |
3411d4ec |
250 | return true; |
43b698c7 |
251 | } |
252 | } |
253 | return false; |
254 | } |
255 | |
3411d4ec |
256 | /* Renames a mailbox */ |
1c52ba77 |
257 | function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) { |
3411d4ec |
258 | if ( $old_name != $new_name ) { |
78cc4b12 |
259 | global $delimiter, $imap_server_type, $data_dir, $username; |
1c52ba77 |
260 | if ( substr( $old_name, -1 ) == $delimiter ) { |
261 | $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 ); |
262 | $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 ); |
263 | $postfix = $delimiter; |
1c52ba77 |
264 | } else { |
265 | $postfix = ''; |
1c52ba77 |
266 | } |
648713af |
267 | $boxesall = sqimap_mailbox_list($imap_stream); |
1c72b151 |
268 | $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"'; |
3411d4ec |
269 | $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message); |
1c52ba77 |
270 | sqimap_unsubscribe($imap_stream, $old_name.$postfix); |
78cc4b12 |
271 | $oldpref = getPref($data_dir, $username, "thread_".$old_name.$postfix); |
272 | removePref($data_dir, $username, "thread_".$old_name.$postfix); |
1c52ba77 |
273 | sqimap_subscribe($imap_stream, $new_name.$postfix); |
78cc4b12 |
274 | setPref($data_dir, $username, "thread_".$new_name.$postfix, $oldpref); |
f5ab1fb9 |
275 | do_hook_function("rename_or_delete_folder",$args = array($old_name, 'rename', $new_name)); |
648713af |
276 | $l = strlen( $old_name ) + 1; |
277 | $p = 'unformatted'; |
278 | foreach ( $boxesall as $box ) { |
279 | if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) { |
280 | $new_sub = $new_name . $delimiter . substr($box[$p], $l); |
281 | if ($imap_server_type == 'cyrus') { |
282 | $cmd = 'RENAME "' . quoteIMAP($box[$p]) . '" "' . quoteIMAP($new_sub) . '"'; |
3411d4ec |
283 | $data = sqimap_run_command($imap_stream, $cmd, true, |
648713af |
284 | $response, $message); |
1c52ba77 |
285 | } |
648713af |
286 | sqimap_unsubscribe($imap_stream, $box[$p]); |
78cc4b12 |
287 | $oldpref = getPref($data_dir, $username, "thread_".$box[$p]); |
288 | removePref($data_dir, $username, "thread_".$box[$p]); |
648713af |
289 | sqimap_subscribe($imap_stream, $new_sub); |
78cc4b12 |
290 | setPref($data_dir, $username, "thread_".$new_sub, $oldpref); |
3411d4ec |
291 | do_hook_function("rename_or_delete_folder", |
292 | $args = array($box[$p], 'rename', $new_sub)); |
1c52ba77 |
293 | } |
294 | } |
1c52ba77 |
295 | } |
1c52ba77 |
296 | } |
43b698c7 |
297 | |
3411d4ec |
298 | /* |
299 | * Formats a mailbox into 4 parts for the $boxesall array |
300 | * |
301 | * The four parts are: |
302 | * |
303 | * raw - Raw LIST/LSUB response from the IMAP server |
304 | * formatted - nicely formatted folder name |
305 | * unformatted - unformatted, but with delimiter at end removed |
306 | * unformatted-dm - folder name as it appears in raw response |
307 | * unformatted-disp - unformatted without $folder_prefix |
308 | */ |
309 | function sqimap_mailbox_parse ($line, $line_lsub) { |
43b698c7 |
310 | global $folder_prefix, $delimiter; |
3411d4ec |
311 | |
43b698c7 |
312 | /* Process each folder line */ |
313 | for ($g=0; $g < count($line); $g++) { |
3411d4ec |
314 | |
43b698c7 |
315 | /* Store the raw IMAP reply */ |
316 | if (isset($line[$g])) { |
1da22cda |
317 | $boxesall[$g]["raw"] = $line[$g]; |
43b698c7 |
318 | } |
319 | else { |
3411d4ec |
320 | $boxesall[$g]["raw"] = ''; |
43b698c7 |
321 | } |
3411d4ec |
322 | |
323 | |
43b698c7 |
324 | /* Count number of delimiters ($delimiter) in folder name */ |
325 | $mailbox = trim($line_lsub[$g]); |
602e1431 |
326 | $dm_count = substr_count($mailbox, $delimiter); |
43b698c7 |
327 | if (substr($mailbox, -1) == $delimiter) { |
3411d4ec |
328 | /* If name ends in delimiter, decrement count by one */ |
329 | $dm_count--; |
43b698c7 |
330 | } |
3411d4ec |
331 | |
332 | /* Format folder name, but only if it's a INBOX.* or has a parent. */ |
1da22cda |
333 | $boxesallbyname[$mailbox] = $g; |
43b698c7 |
334 | $parentfolder = readMailboxParent($mailbox, $delimiter); |
335 | if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") || |
336 | (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) || |
1da22cda |
337 | ( isset($boxesallbyname[$parentfolder]) && |
43b698c7 |
338 | (strlen($parentfolder) > 0) ) ) { |
602e1431 |
339 | $indent = $dm_count - ( substr_count($folder_prefix, $delimiter)); |
43b698c7 |
340 | if ($indent > 0) { |
3411d4ec |
341 | $boxesall[$g]['formatted'] = str_repeat(' ', $indent); |
43b698c7 |
342 | } |
343 | else { |
1da22cda |
344 | $boxesall[$g]['formatted'] = ''; |
43b698c7 |
345 | } |
1da22cda |
346 | $boxesall[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter); |
43b698c7 |
347 | } |
348 | else { |
1da22cda |
349 | $boxesall[$g]['formatted'] = $mailbox; |
43b698c7 |
350 | } |
90de1755 |
351 | |
1da22cda |
352 | $boxesall[$g]['unformatted-dm'] = $mailbox; |
43b698c7 |
353 | if (substr($mailbox, -1) == $delimiter) { |
8e9e8afa |
354 | $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); |
43b698c7 |
355 | } |
1da22cda |
356 | $boxesall[$g]['unformatted'] = $mailbox; |
43b698c7 |
357 | if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) { |
631b9da3 |
358 | $mailbox = substr($mailbox, strlen($folder_prefix)); |
43b698c7 |
359 | } |
1da22cda |
360 | $boxesall[$g]['unformatted-disp'] = $mailbox; |
361 | $boxesall[$g]['id'] = $g; |
90de1755 |
362 | |
1da22cda |
363 | $boxesall[$g]['flags'] = array(); |
43b698c7 |
364 | if (isset($line[$g])) { |
36dfb0c9 |
365 | ereg("\(([^)]*)\)",$line[$g],$regs); |
1a7e1e97 |
366 | $flags = trim(strtolower(str_replace('\\', '',$regs[1]))); |
43b698c7 |
367 | if ($flags) { |
1da22cda |
368 | $boxesall[$g]['flags'] = explode(' ', $flags); |
43b698c7 |
369 | } |
370 | } |
371 | } |
90de1755 |
372 | |
1da22cda |
373 | return $boxesall; |
43b698c7 |
374 | } |
375 | |
3411d4ec |
376 | /* |
a3439b27 |
377 | * Sorting function used to sort mailbox names. |
3411d4ec |
378 | * + Original patch from dave_michmerhuizen@yahoo.com |
379 | * + Allows case insensitivity when sorting folders |
380 | * + Takes care of the delimiter being sorted to the end, causing |
381 | * subfolders to be listed in below folders that are prefixed |
382 | * with their parent folders name. |
383 | * |
384 | * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar |
385 | * Without special sort function: foobar between foo and foo.bar |
386 | * With special sort function: foobar AFTER foo and foo.bar :) |
43b698c7 |
387 | */ |
a3439b27 |
388 | function user_strcasecmp($a, $b) { |
389 | global $delimiter; |
390 | |
391 | /* Calculate the length of some strings. */ |
392 | $a_length = strlen($a); |
393 | $b_length = strlen($b); |
394 | $min_length = min($a_length, $b_length); |
395 | $delimiter_length = strlen($delimiter); |
396 | |
397 | /* Set the initial result value. */ |
398 | $result = 0; |
399 | |
400 | /* Check the strings... */ |
401 | for ($c = 0; $c < $min_length; ++$c) { |
402 | $a_del = substr($a, $c, $delimiter_length); |
403 | $b_del = substr($b, $c, $delimiter_length); |
404 | |
405 | if (($a_del == $delimiter) && ($b_del == $delimiter)) { |
406 | $result = 0; |
407 | } else if (($a_del == $delimiter) && ($b_del != $delimiter)) { |
a3439b27 |
408 | $result = -1; |
c64c33f4 |
409 | } else if (($a_del != $delimiter) && ($b_del == $delimiter)) { |
410 | $result = 1; |
a3439b27 |
411 | } else { |
412 | $result = strcasecmp($a{$c}, $b{$c}); |
413 | } |
414 | |
415 | if ($result != 0) { |
416 | break; |
417 | } |
418 | } |
90de1755 |
419 | |
a3439b27 |
420 | /* If one string is a prefix of the other... */ |
421 | if ($result == 0) { |
422 | if ($a_length < $b_length) { |
423 | $result = -1; |
424 | } else if ($a_length > $b_length) { |
425 | $result = 1; |
426 | } |
427 | } |
428 | |
3411d4ec |
429 | return $result; |
43b698c7 |
430 | } |
431 | |
432 | |
3411d4ec |
433 | /* |
434 | * Returns sorted mailbox lists in several different ways. |
435 | * See comment on sqimap_mailbox_parse() for info about the returned array. |
436 | */ |
1da22cda |
437 | function sqimap_mailbox_list($imap_stream) { |
6f369100 |
438 | global $boxesnew, $default_folder_prefix; |
7e235a1a |
439 | |
440 | if ( !isset( $boxesnew ) ) { |
441 | |
3411d4ec |
442 | global $data_dir, $username, $list_special_folders_first, |
7e235a1a |
443 | $folder_prefix, $trash_folder, $sent_folder, $draft_folder, |
444 | $move_to_trash, $move_to_sent, $save_as_draft, |
445 | $delimiter; |
446 | |
3411d4ec |
447 | $inbox_in_list = false; |
448 | $inbox_subscribed = false; |
7e235a1a |
449 | |
450 | require_once('../src/load_prefs.php'); |
451 | require_once('../functions/array.php'); |
452 | |
3411d4ec |
453 | /* LSUB array */ |
11f6f685 |
454 | $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*%\"", |
3411d4ec |
455 | true, $response, $message); |
7e235a1a |
456 | |
3411d4ec |
457 | /* |
458 | * Section about removing the last element was removed |
459 | * We don't return "* OK" anymore from sqimap_read_data |
460 | */ |
7e235a1a |
461 | |
462 | $sorted_lsub_ary = array(); |
463 | for ($i=0;$i < count($lsub_ary); $i++) { |
3411d4ec |
464 | /* |
465 | * Workaround for EIMS |
466 | * Doesn't work if the mailbox name is multiple lines |
467 | */ |
7e235a1a |
468 | if (isset($lsub_ary[$i + 1]) && |
469 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
470 | $lsub_ary[$i], $regs)) { |
471 | $i ++; |
3411d4ec |
472 | $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2]; |
7e235a1a |
473 | } |
474 | $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]); |
475 | $sorted_lsub_ary[] = $temp_mailbox_name; |
476 | if (strtoupper($temp_mailbox_name) == 'INBOX') { |
3411d4ec |
477 | $inbox_subscribed = true; |
7e235a1a |
478 | } |
479 | } |
480 | $new_ary = array(); |
481 | for ($i=0; $i < count($sorted_lsub_ary); $i++) { |
482 | if (!in_array($sorted_lsub_ary[$i], $new_ary)) { |
483 | $new_ary[] = $sorted_lsub_ary[$i]; |
484 | } |
485 | } |
486 | $sorted_lsub_ary = $new_ary; |
487 | if (isset($sorted_lsub_ary)) { |
488 | usort($sorted_lsub_ary, 'user_strcasecmp'); |
489 | } |
490 | |
3411d4ec |
491 | /* LIST array */ |
7e235a1a |
492 | $sorted_list_ary = array(); |
493 | for ($i=0; $i < count($sorted_lsub_ary); $i++) { |
494 | if (substr($sorted_lsub_ary[$i], -1) == $delimiter) { |
495 | $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1); |
496 | } |
497 | else { |
498 | $mbx = $sorted_lsub_ary[$i]; |
499 | } |
500 | |
501 | $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"", |
3411d4ec |
502 | true, $response, $message); |
7e235a1a |
503 | /* Another workaround for EIMS */ |
504 | if (isset($read[1]) && |
505 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
506 | $read[0], $regs)) { |
3411d4ec |
507 | $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) . '"' . $regs[2]; |
7e235a1a |
508 | } |
509 | |
510 | if (isset($sorted_list_ary[$i])) { |
511 | $sorted_list_ary[$i] = ''; |
512 | } |
513 | |
514 | if (isset($read[0])) { |
515 | $sorted_list_ary[$i] = $read[0]; |
516 | } |
517 | else { |
518 | $sorted_list_ary[$i] = ''; |
519 | } |
520 | |
521 | if (isset($sorted_list_ary[$i]) && |
522 | strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') { |
3411d4ec |
523 | $inbox_in_list = true; |
7e235a1a |
524 | } |
525 | } |
526 | |
3411d4ec |
527 | /* |
7e235a1a |
528 | * Just in case they're not subscribed to their inbox, |
529 | * we'll get it for them anyway |
530 | */ |
3411d4ec |
531 | if (!$inbox_subscribed || !$inbox_in_list) { |
7e235a1a |
532 | $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"", |
3411d4ec |
533 | true, $response, $message); |
7e235a1a |
534 | /* Another workaround for EIMS */ |
535 | if (isset($inbox_ary[1]) && |
536 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
537 | $inbox_ary[0], $regs)) { |
538 | $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) . |
539 | '"' . $regs[2]; |
540 | } |
541 | |
542 | $sorted_list_ary[] = $inbox_ary[0]; |
543 | $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]); |
544 | } |
545 | |
546 | $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary); |
547 | |
3411d4ec |
548 | /* Now, lets sort for special folders */ |
7e235a1a |
549 | $boxesnew = $used = array(); |
550 | |
551 | /* Find INBOX */ |
552 | foreach ( $boxesall as $k => $box ) { |
553 | if ( strtolower($box['unformatted']) == 'inbox') { |
554 | $boxesnew[] = $box; |
3411d4ec |
555 | $used[$k] = true; |
7e235a1a |
556 | } else { |
3411d4ec |
557 | $used[$k] = false; |
7e235a1a |
558 | } |
559 | } |
7e235a1a |
560 | /* List special folders and their subfolders, if requested. */ |
3411d4ec |
561 | if ($list_special_folders_first) { |
7e235a1a |
562 | foreach ( $boxesall as $k => $box ) { |
3411d4ec |
563 | if ( !$used[$k] && isSpecialMailbox( $box['unformatted'] ) ) { |
7e235a1a |
564 | $boxesnew[] = $box; |
3411d4ec |
565 | $used[$k] = true; |
7e235a1a |
566 | } |
6f369100 |
567 | $spec_sub = str_replace(' ', '', $box['formatted']); |
3d1560e7 |
568 | |
569 | /* In case of problems with preg |
570 | here is a ereg version |
571 | if (!$used[$k] && ereg("^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$", $box['unformatted']) ) { */ |
572 | |
573 | if (!$used[$k] && preg_match("?^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$?", $box['unformatted']) ) { |
7ce96393 |
574 | $boxesnew[] = $box; |
575 | $used[$k] = true; |
576 | } |
7e235a1a |
577 | } |
578 | |
579 | } |
7e235a1a |
580 | /* Rest of the folders */ |
581 | foreach ( $boxesall as $k => $box ) { |
582 | if ( !$used[$k] ) { |
583 | $boxesnew[] = $box; |
584 | } |
585 | } |
43b698c7 |
586 | } |
3411d4ec |
587 | return $boxesnew; |
43b698c7 |
588 | } |
589 | |
90de1755 |
590 | /* |
591 | * Returns a list of all folders, subscribed or not |
592 | */ |
1da22cda |
593 | function sqimap_mailbox_list_all($imap_stream) { |
3411d4ec |
594 | global $list_special_folders_first, $folder_prefix, $delimiter; |
90de1755 |
595 | |
65c3ec94 |
596 | require_once('../functions/array.php'); |
90de1755 |
597 | |
43b698c7 |
598 | $ssid = sqimap_session_id(); |
90de1755 |
599 | $lsid = strlen( $ssid ); |
43b698c7 |
600 | fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n"); |
3411d4ec |
601 | $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message); |
43b698c7 |
602 | $g = 0; |
90de1755 |
603 | $phase = 'inbox'; |
604 | |
43b698c7 |
605 | for ($i = 0; $i < count($read_ary); $i++) { |
606 | /* Another workaround for EIMS */ |
607 | if (isset($read_ary[$i + 1]) && |
90de1755 |
608 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
43b698c7 |
609 | $read_ary[$i], $regs)) { |
610 | $i ++; |
3411d4ec |
611 | $read_ary[$i] = $regs[1] . '"' . addslashes(trim($read_ary[$i])) . '"' . $regs[2]; |
43b698c7 |
612 | } |
90de1755 |
613 | if (substr($read_ary[$i], 0, $lsid) != $ssid ) { |
614 | |
43b698c7 |
615 | /* Store the raw IMAP reply */ |
65c3ec94 |
616 | $boxes[$g]['raw'] = $read_ary[$i]; |
1c52ba77 |
617 | |
43b698c7 |
618 | /* Count number of delimiters ($delimiter) in folder name */ |
8e9e8afa |
619 | $mailbox = find_mailbox_name($read_ary[$i]); |
602e1431 |
620 | $dm_count = substr_count($mailbox, $delimiter); |
43b698c7 |
621 | if (substr($mailbox, -1) == $delimiter) { |
0cfd745a |
622 | /* If name ends in delimiter - decrement count by one */ |
90de1755 |
623 | $dm_count--; |
43b698c7 |
624 | } |
90de1755 |
625 | |
3411d4ec |
626 | /* Format folder name, but only if it's a INBOX.* or has a parent. */ |
1da22cda |
627 | $boxesallbyname[$mailbox] = $g; |
525b7ae6 |
628 | $parentfolder = readMailboxParent($mailbox, $delimiter); |
90de1755 |
629 | if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) || |
146e0c45 |
630 | (ereg('^'.$folder_prefix, $mailbox)) || |
1da22cda |
631 | ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) { |
43b698c7 |
632 | if ($dm_count) { |
90de1755 |
633 | $boxes[$g]['formatted'] = str_repeat(' ', $dm_count); |
43b698c7 |
634 | } |
635 | else { |
90de1755 |
636 | $boxes[$g]['formatted'] = ''; |
43b698c7 |
637 | } |
90de1755 |
638 | $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter); |
2752bb16 |
639 | } |
43b698c7 |
640 | else { |
90de1755 |
641 | $boxes[$g]['formatted'] = $mailbox; |
43b698c7 |
642 | } |
90de1755 |
643 | |
1da22cda |
644 | $boxes[$g]['unformatted-dm'] = $mailbox; |
43b698c7 |
645 | if (substr($mailbox, -1) == $delimiter) { |
646 | $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); |
647 | } |
90de1755 |
648 | $boxes[$g]['unformatted'] = $mailbox; |
3411d4ec |
649 | $boxes[$g]['unformatted-disp'] = ereg_replace('^' . $folder_prefix, '', $mailbox); |
90de1755 |
650 | $boxes[$g]['id'] = $g; |
651 | |
3411d4ec |
652 | /* Now lets get the flags for this mailbox */ |
1c72b151 |
653 | $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"", |
3411d4ec |
654 | true, $response, $message); |
90de1755 |
655 | |
43b698c7 |
656 | /* Another workaround for EIMS */ |
657 | if (isset($read_mlbx[1]) && |
3411d4ec |
658 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", $read_mlbx[0], $regs)) { |
659 | $read_mlbx[0] = $regs[1] . '"' . addslashes(trim($read_mlbx[1])) . '"' . $regs[2]; |
43b698c7 |
660 | } |
90de1755 |
661 | |
662 | $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+1); |
663 | $flags = substr($flags, 0, strpos($flags, ')')); |
146e0c45 |
664 | $flags = str_replace('\\', '', $flags); |
8e9e8afa |
665 | $flags = trim(strtolower($flags)); |
666 | if ($flags) { |
1c52ba77 |
667 | $boxes[$g]['flags'] = explode(' ', $flags); |
90de1755 |
668 | } else { |
43b698c7 |
669 | $boxes[$g]['flags'] = array(); |
12d61439 |
670 | } |
43b698c7 |
671 | } |
672 | $g++; |
673 | } |
674 | if(is_array($boxes)) { |
1c52ba77 |
675 | $boxes = ary_sort ($boxes, 'unformatted', 1); |
43b698c7 |
676 | } |
90de1755 |
677 | |
43b698c7 |
678 | return $boxes; |
679 | } |
5bdd7223 |
680 | |
60b5724d |
681 | function sqimap_mailbox_tree($imap_stream) { |
682 | global $boxesnew, $default_folder_prefix, $unseen_notify, $unseen_type; |
683 | if ( !isset( $boxesnew ) ) { |
684 | |
685 | global $data_dir, $username, $list_special_folders_first, |
686 | $folder_prefix, $trash_folder, $sent_folder, $draft_folder, |
687 | $move_to_trash, $move_to_sent, $save_as_draft, |
688 | $delimiter; |
689 | |
690 | |
691 | $inbox_in_list = false; |
692 | $inbox_subscribed = false; |
693 | |
694 | require_once('../src/load_prefs.php'); |
695 | require_once('../functions/array.php'); |
696 | |
697 | /* LSUB array */ |
698 | $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"\" \"*\"", |
699 | true, $response, $message); |
700 | |
701 | /* |
702 | * Section about removing the last element was removed |
703 | * We don't return "* OK" anymore from sqimap_read_data |
704 | */ |
705 | $sorted_lsub_ary = array(); |
706 | |
707 | for ($i=0;$i < count($lsub_ary); $i++) { |
708 | /* |
709 | * Workaround for EIMS |
710 | * Doesn't work if the mailbox name is multiple lines |
711 | */ |
712 | if (isset($lsub_ary[$i + 1]) && |
713 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
714 | $lsub_ary[$i], $regs)) { |
715 | $i ++; |
716 | $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2]; |
717 | } |
718 | |
719 | if (preg_match("/^\*\s+LSUB\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$lsub_ary[$i],$regs)) { |
720 | $flag = $regs[1]; |
721 | $mbx = trim($regs[3]); |
722 | $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag); |
723 | } |
724 | } |
725 | array_multisort($sorted_lsub_ary, SORT_ASC, SORT_STRING); |
726 | |
727 | foreach ($sorted_lsub_ary as $mbx) { |
728 | if ($mbx['mbx'] == 'INBOX') { |
729 | $inbox_in_list = true; |
730 | break; |
731 | } |
732 | } |
733 | |
734 | /* |
735 | * Just in case they're not subscribed to their inbox, |
736 | * we'll get it for them anyway |
737 | */ |
738 | if (!$inbox_in_list) { |
739 | $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"", |
740 | true, $response, $message); |
741 | /* Another workaround for EIMS */ |
742 | if (isset($inbox_ary[1]) && |
743 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
744 | $inbox_ary[0], $regs)) { |
745 | $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) . |
746 | '"' . $regs[2]; |
747 | } |
748 | if (preg_match("/^\*\s+LIST\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$inbox_ary[0],$regs)) { |
749 | $flag = $regs[1]; |
750 | $mbx = trim($regs[3]); |
751 | $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag); |
752 | } |
753 | } |
754 | |
755 | for ($i=0 ; $i < count($sorted_lsub_ary); $i++) { |
756 | if (($unseen_notify == 2 && $sorted_lsub_ary[$i]['mbx'] == 'INBOX') |
757 | || $unseen_notify == 3 |
758 | || ($move_to_trash && ($sorted_lsub_ary[$i]['mbx'] == $trash_folder))) { |
759 | $sorted_lsub_ary[$i]['unseen'] = sqimap_unseen_messages($imap_stream, $mbx['mbx']); |
760 | if ($unseen_type == 2 || ($move_to_trash |
761 | && ($sorted_lsub_ary[$i]['mbx'] == $trash_folder) )) { |
762 | $sorted_lsub_ary[$i]['nummessages'] = sqimap_get_num_messages($imap_stream, $mbx['mbx']); |
763 | } |
764 | if ($unseen_notify != 3 && !($move_to_trash |
765 | && ($sorted_lsub_ary[$i]['mbx'] == $trash_folder) )) break; |
766 | } |
767 | } |
768 | |
769 | $boxesnew = sqimap_fill_mailbox_tree($sorted_lsub_ary); |
770 | return $boxesnew; |
771 | } |
772 | } |
773 | |
774 | |
775 | function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false) { |
776 | global $data_dir, $username, $list_special_folders_first, |
777 | $folder_prefix, $trash_folder, $sent_folder, $draft_folder, |
778 | $move_to_trash, $move_to_sent, $save_as_draft, |
779 | $delimiter; |
780 | |
781 | $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder); |
782 | |
783 | /* create virtual root node */ |
784 | $mailboxes= new mailboxes(); |
785 | $mailboxes->is_root = true; |
786 | $trail_del = false; |
787 | if (isset($folder_prefix) && $folder_prefix != '') { |
788 | $start = substr_count($folder_prefix,$delimiter); |
789 | if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) { |
790 | $trail_del = true; |
791 | $mailboxes->mailboxname_full = substr($folder_prefix,0, (strlen($folder_prefix)-1)); |
792 | } else { |
793 | $mailboxes->mailboxname_full = $folder_prefix; |
794 | $start++; |
795 | } |
796 | $mailboxes->mailboxname_sub = $mailboxes->mailboxname_full; |
797 | } else $start = 0; |
798 | |
799 | for ($i=0; $i < count($mbx_ary); $i++) { |
800 | if ($mbx_ary[$i]['mbx'] !='' ) { |
801 | $mbx = new mailboxes(); |
802 | $mailbox = $mbx_ary[$i]['mbx']; |
803 | switch ($mailbox) { |
804 | case 'INBOX': |
805 | $mbx->is_inbox = true; |
806 | $mbx->is_special = true; |
807 | break; |
808 | case $trash_folder: |
809 | $mbx->is_trash = true; |
810 | $mbx->is_special = true; |
811 | break; |
812 | case $sent_folder: |
813 | $mbx->is_sent = true; |
814 | $mbx->is_special = true; |
815 | break; |
816 | case $draft_folder: |
817 | $mbx->is_draft = true; |
818 | $mbx->is_special = true; |
819 | break; |
820 | } |
821 | |
822 | if (isset($mbx_ary[$i]['unseen'])) { |
823 | $mbx->unseen = $mbx_ary[$i]['unseen']; |
824 | } |
825 | if (isset($mbx_ary[$i]['nummessages'])) { |
826 | $mbx->total = $mbx_ary[$i]['nummessages']; |
827 | } |
828 | |
829 | $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter); |
830 | if ($r_del_pos) { |
831 | $mbx->mailboxname_sub = substr($mbx_ary[$i]['mbx'],$r_del_pos+1); |
832 | } else { /* mailbox is root folder */ |
833 | $mbx->mailboxname_sub = $mbx_ary[$i]['mbx']; |
834 | } |
835 | $mbx->mailboxname_full = $mbx_ary[$i]['mbx']; |
836 | $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first); |
837 | } |
838 | } |
839 | |
840 | return $mailboxes; |
841 | } |
842 | |
843 | |
648713af |
844 | ?> |