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