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