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