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