59177427 |
1 | <?php |
bccadd02 |
2 | |
35586184 |
3 | /** |
4 | * imap_mailbox.php |
5 | * |
76911253 |
6 | * Copyright (c) 1999-2003 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 | */ |
334a77f8 |
13 | require_once(SM_PATH . 'functions/imap_utf7_local.php'); |
14 | |
3411d4ec |
15 | global $boxesnew; |
1da22cda |
16 | |
1eb028a9 |
17 | /* |
18 | FIXME. This class should be extracted and placed in a separate file that |
19 | can be included before we start the session. That makes caching of the tree |
20 | possible. On a refresh mailboxes from left_main.php the only function that |
21 | should be called is the sqimap_get_status_mbx_tree. In case of subscribe |
22 | / rename / delete / new we have to create methods for adding/changing the |
23 | mailbox in the mbx_tree without the need for a refresh. |
24 | */ |
25 | |
60b5724d |
26 | class mailboxes { |
ff245fbd |
27 | var $mailboxname_full = '', $mailboxname_sub= '', $is_noselect = false, |
28 | $is_special = false, $is_root = false, $is_inbox = false, $is_sent = false, |
29 | $is_trash = false, $is_draft = false, $mbxs = array(), |
30 | $unseen = false, $total = false; |
31 | |
32 | function addMbx($mbx, $delimiter, $start, $specialfirst) { |
33 | $ary = explode($delimiter, $mbx->mailboxname_full); |
ae7df16e |
34 | $mbx_parent =& $this; |
ff245fbd |
35 | for ($i = $start, $c = count($ary)-1; $i < $c; $i++) { |
ae7df16e |
36 | $mbx_childs =& $mbx_parent->mbxs; |
ff245fbd |
37 | $found = false; |
38 | if ($mbx_childs) { |
39 | foreach ($mbx_childs as $key => $parent) { |
40 | if ($parent->mailboxname_sub == $ary[$i]) { |
ae7df16e |
41 | $mbx_parent =& $mbx_parent->mbxs[$key]; |
ff245fbd |
42 | $found = true; |
ae7df16e |
43 | break; |
ff245fbd |
44 | } |
45 | } |
46 | } |
47 | if (!$found) { |
48 | $no_select_mbx = new mailboxes(); |
49 | if (isset($mbx_parent->mailboxname_full) && $mbx_parent->mailboxname_full != '') { |
50 | $no_select_mbx->mailboxname_full = $mbx_parent->mailboxname_full.$delimiter.$ary[$i]; |
51 | } else { |
52 | $no_select_mbx->mailboxname_full = $ary[$i]; |
53 | } |
587ec647 |
54 | $no_select_mbx->mailboxname_sub = $ary[$i]; |
ff245fbd |
55 | $no_select_mbx->is_noselect = true; |
56 | $mbx_parent->mbxs[] = $no_select_mbx; |
57 | $i--; |
58 | } |
59 | } |
60 | $mbx_parent->mbxs[] = $mbx; |
61 | if ($mbx->is_special && $specialfirst) { |
62 | usort($mbx_parent->mbxs, 'sortSpecialMbx'); |
63 | } |
64 | } |
60b5724d |
65 | } |
66 | |
67 | function sortSpecialMbx($a, $b) { |
68 | if ($a->is_inbox) { |
ff245fbd |
69 | $acmp = '0'. $a->mailboxname_full; |
60b5724d |
70 | } else if ($a->is_special) { |
ff245fbd |
71 | $acmp = '1'. $a->mailboxname_full; |
60b5724d |
72 | } else { |
ff245fbd |
73 | $acmp = '2' . $a->mailboxname_full; |
74 | } |
60b5724d |
75 | if ($b->is_inbox) { |
ff245fbd |
76 | $bcmp = '0'. $b->mailboxname_full; |
60b5724d |
77 | }else if ($b->is_special) { |
ff245fbd |
78 | $bcmp = '1' . $b->mailboxname_full; |
60b5724d |
79 | } else { |
ff245fbd |
80 | $bcmp = '2' . $b->mailboxname_full; |
60b5724d |
81 | } |
82 | if ($acmp == $bcmp) return 0; |
ff245fbd |
83 | return ($acmp > $bcmp) ? 1: -1; |
84 | } |
60b5724d |
85 | |
79e07c7e |
86 | function find_mailbox_name ($mailbox) { |
d42310bd |
87 | if (preg_match('/\*.+\"([^\r\n\"]*)\"[\s\r\n]*$/', $mailbox, $regs)) |
88 | return $regs[1]; |
79e07c7e |
89 | if (ereg(" *\"([^\r\n\"]*)\"[ \r\n]*$", $mailbox, $regs)) |
90 | return $regs[1]; |
91 | ereg(" *([^ \r\n\"]*)[ \r\n]*$",$mailbox,$regs); |
92 | return $regs[1]; |
f73348a3 |
93 | } |
94 | |
95 | function check_is_noselect ($lsub_line) { |
3698bd49 |
96 | return preg_match("/^\* (LSUB|LIST) \([^\)]*\\\\Noselect[^\)]*\)/i", $lsub_line); |
79e07c7e |
97 | } |
98 | |
97b1248c |
99 | /** |
100 | * If $haystack is a full mailbox name, and $needle is the mailbox |
101 | * separator character, returns the second last part of the full |
102 | * mailbox name (i.e. the mailbox's parent mailbox) |
103 | */ |
104 | function readMailboxParent($haystack, $needle) { |
97b1248c |
105 | if ($needle == '') { |
106 | $ret = ''; |
107 | } else { |
108 | $parts = explode($needle, $haystack); |
109 | $elem = array_pop($parts); |
110 | while ($elem == '' && count($parts)) { |
111 | $elem = array_pop($parts); |
112 | } |
113 | $ret = join($needle, $parts); |
114 | } |
115 | return( $ret ); |
116 | } |
117 | |
6a8e7cae |
118 | /** |
119 | * Check if $subbox is below the specified $parentbox |
120 | */ |
121 | function isBoxBelow( $subbox, $parentbox ) { |
cef054e4 |
122 | global $delimiter; |
6a8e7cae |
123 | /* |
124 | * Eliminate the obvious mismatch, where the |
125 | * subfolder path is shorter than that of the potential parent |
126 | */ |
127 | if ( strlen($subbox) < strlen($parentbox) ) { |
128 | return false; |
129 | } |
cef054e4 |
130 | /* check for delimiter */ |
131 | if (!substr($parentbox,-1) == $delimiter) { |
132 | $parentbox.=$delimiter; |
133 | } |
134 | if (substr($subbox,0,strlen($parentbox)) == $parentbox) { |
135 | return true; |
136 | } else { |
137 | return false; |
138 | } |
1e18bf95 |
139 | } |
140 | |
3411d4ec |
141 | /* Defines special mailboxes */ |
1e18bf95 |
142 | function isSpecialMailbox( $box ) { |
1e18bf95 |
143 | global $trash_folder, $sent_folder, $draft_folder, |
65c3ec94 |
144 | $move_to_trash, $move_to_sent, $save_as_draft; |
1e18bf95 |
145 | |
90de1755 |
146 | $ret = ( (strtolower($box) == 'inbox') || |
6a8e7cae |
147 | isTrashMailbox($box) || isSentMailbox($box) || isDraftMailbox($box) ); |
90de1755 |
148 | |
2586d588 |
149 | if ( !$ret ) { |
31524bcd |
150 | $ret = do_hook_function( 'special_mailbox', $box ); |
2586d588 |
151 | } |
3411d4ec |
152 | return $ret; |
90de1755 |
153 | } |
154 | |
6a8e7cae |
155 | function isTrashMailbox ($box) { |
156 | global $trash_folder, $move_to_trash; |
157 | return $move_to_trash && $trash_folder && |
158 | ( $box == $trash_folder || isBoxBelow($box, $trash_folder) ); |
159 | } |
160 | |
161 | function isSentMailbox($box) { |
162 | global $sent_folder, $move_to_sent; |
163 | return $move_to_sent && $sent_folder && |
164 | ( $box == $sent_folder || isBoxBelow($box, $sent_folder) ); |
165 | } |
166 | |
167 | function isDraftMailbox($box) { |
168 | global $draft_folder, $save_as_draft; |
169 | return $save_as_draft && |
170 | ( $box == $draft_folder || isBoxBelow($box, $draft_folder) ); |
171 | } |
172 | |
3411d4ec |
173 | /* Expunges a mailbox */ |
8f6505f6 |
174 | function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true, $id='') { |
63240b90 |
175 | global $uid_support; |
06b5c3ff |
176 | if ($id) { |
ff245fbd |
177 | if (is_array($id)) { |
178 | $id = sqimap_message_list_squisher($id); |
179 | } |
180 | $id = ' '.$id; |
181 | $uid = $uid_support; |
06b5c3ff |
182 | } else { |
ff245fbd |
183 | $uid = false; |
8f6505f6 |
184 | } |
06b5c3ff |
185 | $read = sqimap_run_command($imap_stream, 'EXPUNGE'.$id, $handle_errors, |
186 | $response, $message, $uid); |
63240b90 |
187 | $cnt = 0; |
ff245fbd |
188 | |
189 | if (is_array($read)) { |
63240b90 |
190 | foreach ($read as $r) { |
ff245fbd |
191 | if (preg_match('/^\*\s[0-9]+\sEXPUNGE/AUi',$r,$regs)) { |
192 | $cnt++; |
193 | } |
63240b90 |
194 | } |
8f6505f6 |
195 | } |
ff245fbd |
196 | return $cnt; |
43b698c7 |
197 | } |
198 | |
3411d4ec |
199 | /* Checks whether or not the specified mailbox exists */ |
1da22cda |
200 | function sqimap_mailbox_exists ($imap_stream, $mailbox) { |
247f700e |
201 | if (!isset($mailbox) || empty($mailbox)) { |
43b698c7 |
202 | return false; |
203 | } |
1c72b151 |
204 | $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"", |
3411d4ec |
205 | true, $response, $message); |
43b698c7 |
206 | return isset($mbx[0]); |
207 | } |
208 | |
3411d4ec |
209 | /* Selects a mailbox */ |
e4c6fe41 |
210 | function sqimap_mailbox_select ($imap_stream, $mailbox) { |
43b698c7 |
211 | global $auto_expunge; |
f69feefe |
212 | |
ff245fbd |
213 | if ($mailbox == 'None') { |
43b698c7 |
214 | return; |
215 | } |
f69feefe |
216 | |
1c72b151 |
217 | $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"", |
3411d4ec |
218 | true, $response, $message); |
e4c6fe41 |
219 | $result = array(); |
ff245fbd |
220 | for ($i = 0, $cnt = count($read); $i < $cnt; $i++) { |
e4c6fe41 |
221 | if (preg_match('/^\*\s+OK\s\[(\w+)\s(\w+)\]/',$read[$i], $regs)) { |
ff245fbd |
222 | $result[strtoupper($regs[1])] = $regs[2]; |
223 | } else if (preg_match('/^\*\s([0-9]+)\s(\w+)/',$read[$i], $regs)) { |
224 | $result[strtoupper($regs[2])] = $regs[1]; |
225 | } else { |
226 | if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) { |
227 | $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ; |
228 | $result['PERMANENTFLAGS'] = $regs[1]; |
229 | } else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) { |
230 | $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ; |
231 | $result['FLAGS'] = $regs[1]; |
232 | } |
233 | } |
e4c6fe41 |
234 | } |
235 | if (preg_match('/^\[(.+)\]/',$message, $regs)) { |
ff245fbd |
236 | $result['RIGHTS']=$regs[1]; |
e4c6fe41 |
237 | } |
f69feefe |
238 | |
e4c6fe41 |
239 | if ($auto_expunge) { |
ff245fbd |
240 | $tmp = sqimap_run_command($imap_stream, 'EXPUNGE', false, $a, $b); |
43b698c7 |
241 | } |
e4c6fe41 |
242 | return $result; |
43b698c7 |
243 | } |
244 | |
3411d4ec |
245 | /* Creates a folder */ |
246 | function sqimap_mailbox_create ($imap_stream, $mailbox, $type) { |
43b698c7 |
247 | global $delimiter; |
248 | if (strtolower($type) == 'noselect') { |
3411d4ec |
249 | $mailbox .= $delimiter; |
43b698c7 |
250 | } |
e429f014 |
251 | |
1c72b151 |
252 | $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"", |
3411d4ec |
253 | true, $response, $message); |
43b698c7 |
254 | sqimap_subscribe ($imap_stream, $mailbox); |
255 | } |
256 | |
3411d4ec |
257 | /* Subscribes to an existing folder */ |
258 | function sqimap_subscribe ($imap_stream, $mailbox) { |
1a16af11 |
259 | $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"", |
3411d4ec |
260 | true, $response, $message); |
43b698c7 |
261 | } |
262 | |
3411d4ec |
263 | /* Unsubscribes to an existing folder */ |
264 | function sqimap_unsubscribe ($imap_stream, $mailbox) { |
1a16af11 |
265 | $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"", |
3411d4ec |
266 | true, $response, $message); |
43b698c7 |
267 | } |
268 | |
3411d4ec |
269 | /* Deletes the given folder */ |
270 | function sqimap_mailbox_delete ($imap_stream, $mailbox) { |
78cc4b12 |
271 | global $data_dir, $username; |
1c72b151 |
272 | $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"", |
3411d4ec |
273 | true, $response, $message); |
43b698c7 |
274 | sqimap_unsubscribe ($imap_stream, $mailbox); |
e429f014 |
275 | do_hook_function('rename_or_delete_folder', $args = array($mailbox, 'delete', '')); |
78cc4b12 |
276 | removePref($data_dir, $username, "thread_$mailbox"); |
43b698c7 |
277 | } |
278 | |
3411d4ec |
279 | /* Determines if the user is subscribed to the folder or not */ |
1da22cda |
280 | function sqimap_mailbox_is_subscribed($imap_stream, $folder) { |
1da22cda |
281 | $boxesall = sqimap_mailbox_list ($imap_stream); |
282 | foreach ($boxesall as $ref) { |
43b698c7 |
283 | if ($ref['unformatted'] == $folder) { |
3411d4ec |
284 | return true; |
43b698c7 |
285 | } |
286 | } |
287 | return false; |
288 | } |
289 | |
3411d4ec |
290 | /* Renames a mailbox */ |
1c52ba77 |
291 | function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) { |
3411d4ec |
292 | if ( $old_name != $new_name ) { |
78cc4b12 |
293 | global $delimiter, $imap_server_type, $data_dir, $username; |
1c52ba77 |
294 | if ( substr( $old_name, -1 ) == $delimiter ) { |
295 | $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 ); |
296 | $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 ); |
297 | $postfix = $delimiter; |
1c52ba77 |
298 | } else { |
299 | $postfix = ''; |
1c52ba77 |
300 | } |
68f2ce7a |
301 | |
648713af |
302 | $boxesall = sqimap_mailbox_list($imap_stream); |
68f2ce7a |
303 | $cmd = 'RENAME "' . $old_name . '" "' . $new_name . '"'; |
3411d4ec |
304 | $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message); |
1c52ba77 |
305 | sqimap_unsubscribe($imap_stream, $old_name.$postfix); |
68f2ce7a |
306 | $oldpref = getPref($data_dir, $username, 'thread_'.$old_name.$postfix); |
307 | removePref($data_dir, $username, 'thread_'.$old_name.$postfix); |
1c52ba77 |
308 | sqimap_subscribe($imap_stream, $new_name.$postfix); |
68f2ce7a |
309 | setPref($data_dir, $username, 'thread_'.$new_name.$postfix, $oldpref); |
e429f014 |
310 | do_hook_function('rename_or_delete_folder',$args = array($old_name, 'rename', $new_name)); |
648713af |
311 | $l = strlen( $old_name ) + 1; |
312 | $p = 'unformatted'; |
68f2ce7a |
313 | |
ff245fbd |
314 | foreach ($boxesall as $box) { |
315 | if (substr($box[$p], 0, $l) == $old_name . $delimiter) { |
648713af |
316 | $new_sub = $new_name . $delimiter . substr($box[$p], $l); |
317 | if ($imap_server_type == 'cyrus') { |
68f2ce7a |
318 | $cmd = 'RENAME "' . $box[$p] . '" "' . $new_sub . '"'; |
3411d4ec |
319 | $data = sqimap_run_command($imap_stream, $cmd, true, |
648713af |
320 | $response, $message); |
1c52ba77 |
321 | } |
648713af |
322 | sqimap_unsubscribe($imap_stream, $box[$p]); |
68f2ce7a |
323 | $oldpref = getPref($data_dir, $username, 'thread_'.$box[$p]); |
324 | removePref($data_dir, $username, 'thread_'.$box[$p]); |
648713af |
325 | sqimap_subscribe($imap_stream, $new_sub); |
68f2ce7a |
326 | setPref($data_dir, $username, 'thread_'.$new_sub, $oldpref); |
327 | do_hook_function('rename_or_delete_folder', |
3411d4ec |
328 | $args = array($box[$p], 'rename', $new_sub)); |
1c52ba77 |
329 | } |
330 | } |
1c52ba77 |
331 | } |
1c52ba77 |
332 | } |
43b698c7 |
333 | |
3411d4ec |
334 | /* |
335 | * Formats a mailbox into 4 parts for the $boxesall array |
336 | * |
337 | * The four parts are: |
338 | * |
339 | * raw - Raw LIST/LSUB response from the IMAP server |
340 | * formatted - nicely formatted folder name |
341 | * unformatted - unformatted, but with delimiter at end removed |
342 | * unformatted-dm - folder name as it appears in raw response |
343 | * unformatted-disp - unformatted without $folder_prefix |
344 | */ |
345 | function sqimap_mailbox_parse ($line, $line_lsub) { |
43b698c7 |
346 | global $folder_prefix, $delimiter; |
3411d4ec |
347 | |
43b698c7 |
348 | /* Process each folder line */ |
cef054e4 |
349 | for ($g = 0, $cnt = count($line); $g < $cnt; ++$g) { |
43b698c7 |
350 | /* Store the raw IMAP reply */ |
351 | if (isset($line[$g])) { |
e429f014 |
352 | $boxesall[$g]['raw'] = $line[$g]; |
ff245fbd |
353 | } else { |
e429f014 |
354 | $boxesall[$g]['raw'] = ''; |
43b698c7 |
355 | } |
3411d4ec |
356 | |
43b698c7 |
357 | /* Count number of delimiters ($delimiter) in folder name */ |
ff245fbd |
358 | $mailbox = trim($line_lsub[$g]); |
359 | $dm_count = substr_count($mailbox, $delimiter); |
43b698c7 |
360 | if (substr($mailbox, -1) == $delimiter) { |
3411d4ec |
361 | /* If name ends in delimiter, decrement count by one */ |
362 | $dm_count--; |
43b698c7 |
363 | } |
3411d4ec |
364 | |
365 | /* Format folder name, but only if it's a INBOX.* or has a parent. */ |
1da22cda |
366 | $boxesallbyname[$mailbox] = $g; |
43b698c7 |
367 | $parentfolder = readMailboxParent($mailbox, $delimiter); |
368 | if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") || |
369 | (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) || |
ff245fbd |
370 | (isset($boxesallbyname[$parentfolder]) && |
371 | (strlen($parentfolder) > 0) ) ) { |
372 | $indent = $dm_count - (substr_count($folder_prefix, $delimiter)); |
43b698c7 |
373 | if ($indent > 0) { |
ff245fbd |
374 | $boxesall[$g]['formatted'] = str_repeat(' ', $indent); |
375 | } else { |
1da22cda |
376 | $boxesall[$g]['formatted'] = ''; |
43b698c7 |
377 | } |
447b2166 |
378 | $boxesall[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter)); |
ff245fbd |
379 | } else { |
447b2166 |
380 | $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox); |
43b698c7 |
381 | } |
90de1755 |
382 | |
1da22cda |
383 | $boxesall[$g]['unformatted-dm'] = $mailbox; |
43b698c7 |
384 | if (substr($mailbox, -1) == $delimiter) { |
8e9e8afa |
385 | $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); |
43b698c7 |
386 | } |
1da22cda |
387 | $boxesall[$g]['unformatted'] = $mailbox; |
43b698c7 |
388 | if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) { |
631b9da3 |
389 | $mailbox = substr($mailbox, strlen($folder_prefix)); |
43b698c7 |
390 | } |
1da22cda |
391 | $boxesall[$g]['unformatted-disp'] = $mailbox; |
392 | $boxesall[$g]['id'] = $g; |
90de1755 |
393 | |
1da22cda |
394 | $boxesall[$g]['flags'] = array(); |
43b698c7 |
395 | if (isset($line[$g])) { |
36dfb0c9 |
396 | ereg("\(([^)]*)\)",$line[$g],$regs); |
5c300c60 |
397 | // FIXME Flags do contain the \ character. \NoSelect \NoInferiors |
398 | // and $MDNSent <= last one doesn't have the \ |
399 | // It's better to follow RFC3501 instead of using our own naming. |
1a7e1e97 |
400 | $flags = trim(strtolower(str_replace('\\', '',$regs[1]))); |
43b698c7 |
401 | if ($flags) { |
1da22cda |
402 | $boxesall[$g]['flags'] = explode(' ', $flags); |
43b698c7 |
403 | } |
404 | } |
405 | } |
1da22cda |
406 | return $boxesall; |
43b698c7 |
407 | } |
408 | |
3411d4ec |
409 | /* |
a3439b27 |
410 | * Sorting function used to sort mailbox names. |
3411d4ec |
411 | * + Original patch from dave_michmerhuizen@yahoo.com |
412 | * + Allows case insensitivity when sorting folders |
413 | * + Takes care of the delimiter being sorted to the end, causing |
414 | * subfolders to be listed in below folders that are prefixed |
415 | * with their parent folders name. |
416 | * |
417 | * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar |
418 | * Without special sort function: foobar between foo and foo.bar |
419 | * With special sort function: foobar AFTER foo and foo.bar :) |
43b698c7 |
420 | */ |
a3439b27 |
421 | function user_strcasecmp($a, $b) { |
d42310bd |
422 | return strnatcasecmp($a, $b); |
43b698c7 |
423 | } |
424 | |
be2d5495 |
425 | /* |
426 | * Returns list of options (to be echoed into select statement |
427 | * based on available mailboxes and separators |
428 | * Caller should surround options with <SELECT..> </SELECT> and |
429 | * any formatting. |
430 | * $imap_stream - $imapConnection to query for mailboxes |
431 | * $show_selected - array containing list of mailboxes to pre-select (0 if none) |
432 | * $folder_skip - array of folders to keep out of option list (compared in lower) |
433 | * $boxes - list of already fetched boxes (for places like folder panel, where |
434 | * you know these options will be shown 3 times in a row.. (most often unset). |
59a8e3e8 |
435 | * $flag - flag to check for in mailbox flags, used to filter out mailboxes. |
436 | * 'noselect' by default to remove unselectable mailboxes. |
437 | * 'noinferiors' used to filter out folders that can not contain subfolders. |
438 | * NULL to avoid flag check entirely. |
d0928dd5 |
439 | * NOTE: noselect and noiferiors are used internally. The IMAP representation is |
440 | * \NoSelect and \NoInferiors |
59a8e3e8 |
441 | * $use_long_format - override folder display preference and always show full folder name. |
be2d5495 |
442 | */ |
59a8e3e8 |
443 | function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_skip = 0, $boxes = 0, |
444 | $flag = 'noselect', $use_long_format = false ) { |
be2d5495 |
445 | global $username, $data_dir; |
446 | $mbox_options = ''; |
45f836eb |
447 | if ( $use_long_format ) { |
448 | $shorten_box_names = 0; |
449 | } else { |
450 | $shorten_box_names = getPref($data_dir, $username, 'mailbox_select_style', SMPREF_OFF); |
451 | } |
ff245fbd |
452 | |
453 | if ($boxes == 0) { |
be2d5495 |
454 | $boxes = sqimap_mailbox_list($imap_stream); |
ff245fbd |
455 | } |
59a8e3e8 |
456 | |
be2d5495 |
457 | foreach ($boxes as $boxes_part) { |
59a8e3e8 |
458 | if ($flag == NULL || !in_array($flag, $boxes_part['flags'])) { |
be2d5495 |
459 | $box = $boxes_part['unformatted']; |
be2d5495 |
460 | |
d0928dd5 |
461 | if ($folder_skip != 0 && in_array($box, $folder_skip) ) { |
be2d5495 |
462 | continue; |
463 | } |
d0928dd5 |
464 | $lowerbox = strtolower($box); |
5c300c60 |
465 | // mailboxes are casesensitive => inbox.sent != inbox.Sent |
466 | // nevermind, to many dependencies this should be fixed! |
467 | |
d0928dd5 |
468 | if (strtolower($box) == 'inbox') { // inbox is special and not casesensitive |
be2d5495 |
469 | $box2 = _("INBOX"); |
d0928dd5 |
470 | } else { |
5c300c60 |
471 | switch ($shorten_box_names) |
472 | { |
473 | case 2: /* delimited, style = 2 */ |
d0928dd5 |
474 | $box2 = str_replace(' ', '. ', $boxes_part['formatted']); |
5c300c60 |
475 | break; |
d0928dd5 |
476 | case 1: /* indent, style = 1 */ |
477 | $box2 = $boxes_part['formatted']; |
5c300c60 |
478 | break; |
d0928dd5 |
479 | default: /* default, long names, style = 0 */ |
480 | $box2 = str_replace(' ', ' ', imap_utf7_decode_local($boxes_part['unformatted-disp'])); |
5c300c60 |
481 | break; |
482 | } |
be2d5495 |
483 | } |
484 | if ($show_selected != 0 && in_array($lowerbox, $show_selected) ) { |
485 | $mbox_options .= '<OPTION VALUE="'.$box.'" SELECTED>'.$box2.'</OPTION>' . "\n"; |
486 | } else { |
487 | $mbox_options .= '<OPTION VALUE="'.$box.'">'.$box2.'</OPTION>' . "\n"; |
488 | } |
489 | } |
490 | } |
491 | return $mbox_options; |
492 | } |
43b698c7 |
493 | |
3411d4ec |
494 | /* |
495 | * Returns sorted mailbox lists in several different ways. |
496 | * See comment on sqimap_mailbox_parse() for info about the returned array. |
497 | */ |
1da22cda |
498 | function sqimap_mailbox_list($imap_stream) { |
4b2fe13a |
499 | global $default_folder_prefix; |
7e235a1a |
500 | |
ff245fbd |
501 | if (!isset($boxesnew)) { |
3411d4ec |
502 | global $data_dir, $username, $list_special_folders_first, |
7e235a1a |
503 | $folder_prefix, $trash_folder, $sent_folder, $draft_folder, |
504 | $move_to_trash, $move_to_sent, $save_as_draft, |
ca85aabe |
505 | $delimiter, $noselect_fix_enable; |
7e235a1a |
506 | |
3411d4ec |
507 | $inbox_in_list = false; |
508 | $inbox_subscribed = false; |
7e235a1a |
509 | |
08185f2a |
510 | require_once(SM_PATH . 'include/load_prefs.php'); |
7e235a1a |
511 | |
ff245fbd |
512 | if ($noselect_fix_enable) { |
513 | $lsub_args = "LSUB \"$folder_prefix\" \"*%\""; |
514 | } else { |
515 | $lsub_args = "LSUB \"$folder_prefix\" \"*\""; |
516 | } |
3411d4ec |
517 | /* LSUB array */ |
ca85aabe |
518 | $lsub_ary = sqimap_run_command ($imap_stream, $lsub_args, |
3411d4ec |
519 | true, $response, $message); |
7e235a1a |
520 | |
7e235a1a |
521 | $sorted_lsub_ary = array(); |
ff245fbd |
522 | for ($i = 0, $cnt = count($lsub_ary);$i < $cnt; $i++) { |
3411d4ec |
523 | /* |
cef054e4 |
524 | * Workaround for mailboxes returned as literal |
525 | * Doesn't work if the mailbox name is multiple lines |
5c300c60 |
526 | * (larger then fgets buffer) |
3411d4ec |
527 | */ |
cef054e4 |
528 | if (isset($lsub_ary[$i + 1]) && substr($lsub_ary[$i],-3) == "}\r\n") { |
5c300c60 |
529 | if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
7e235a1a |
530 | $lsub_ary[$i], $regs)) { |
5c300c60 |
531 | $i++; |
532 | $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2]; |
533 | } |
7e235a1a |
534 | } |
535 | $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]); |
536 | $sorted_lsub_ary[] = $temp_mailbox_name; |
cef054e4 |
537 | if (!$inbox_subscribed && strtoupper($temp_mailbox_name) == 'INBOX') { |
3411d4ec |
538 | $inbox_subscribed = true; |
7e235a1a |
539 | } |
540 | } |
5c300c60 |
541 | /* remove duplicates */ |
542 | $sorted_lsub_ary = array_unique($sorted_lsub_ary); |
cef054e4 |
543 | |
5c300c60 |
544 | /* natural sort mailboxes */ |
7e235a1a |
545 | if (isset($sorted_lsub_ary)) { |
546 | usort($sorted_lsub_ary, 'user_strcasecmp'); |
547 | } |
5c300c60 |
548 | /* |
549 | * The LSUB response doesn't provide us information about \Noselect |
550 | * mail boxes. The LIST response does, that's why we need to do a LIST |
551 | * call to retrieve the flags for the mailbox |
cef054e4 |
552 | * Note: according RFC2060 an imap server may provide \NoSelect flags in the LSUB response. |
553 | * in other words, we cannot rely on it. |
5c300c60 |
554 | */ |
cef054e4 |
555 | $sorted_list_ary = array(); |
ff245fbd |
556 | for ($i=0; $i < count($sorted_lsub_ary); $i++) { |
7e235a1a |
557 | if (substr($sorted_lsub_ary[$i], -1) == $delimiter) { |
558 | $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1); |
559 | } |
560 | else { |
561 | $mbx = $sorted_lsub_ary[$i]; |
562 | } |
563 | |
564 | $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"", |
3411d4ec |
565 | true, $response, $message); |
7e235a1a |
566 | |
cef054e4 |
567 | /* Another workaround for literals */ |
568 | |
569 | if (isset($read[1]) && substr($read[1],-3) == "}\r\n") { |
5c300c60 |
570 | if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
cef054e4 |
571 | $read[0], $regs)) { |
5c300c60 |
572 | $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) . '"' . $regs[2]; |
573 | } |
7e235a1a |
574 | } |
575 | |
576 | if (isset($read[0])) { |
577 | $sorted_list_ary[$i] = $read[0]; |
cef054e4 |
578 | } else { |
7e235a1a |
579 | $sorted_list_ary[$i] = ''; |
580 | } |
7e235a1a |
581 | } |
cef054e4 |
582 | |
3411d4ec |
583 | /* |
7e235a1a |
584 | * Just in case they're not subscribed to their inbox, |
585 | * we'll get it for them anyway |
586 | */ |
cef054e4 |
587 | if (!$inbox_subscribed) { |
7e235a1a |
588 | $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"", |
3411d4ec |
589 | true, $response, $message); |
cef054e4 |
590 | /* Another workaround for literals */ |
322f4e37 |
591 | if (isset($inbox_ary[1]) && substr($inbox_ary[0],-3) == "}\r\n") { |
5c300c60 |
592 | if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
7e235a1a |
593 | $inbox_ary[0], $regs)) { |
5c300c60 |
594 | $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) . |
ff245fbd |
595 | '"' . $regs[2]; |
5c300c60 |
596 | } |
7e235a1a |
597 | } |
7e235a1a |
598 | $sorted_list_ary[] = $inbox_ary[0]; |
599 | $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]); |
600 | } |
601 | |
602 | $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary); |
603 | |
3411d4ec |
604 | /* Now, lets sort for special folders */ |
7e235a1a |
605 | $boxesnew = $used = array(); |
606 | |
607 | /* Find INBOX */ |
ff245fbd |
608 | $cnt = count($boxesall); |
5c300c60 |
609 | $used = array_pad($used,$cnt,false); |
cef054e4 |
610 | for($k = 0; $k < $cnt; ++$k) { |
ff245fbd |
611 | if (strtolower($boxesall[$k]['unformatted']) == 'inbox') { |
612 | $boxesnew[] = $boxesall[$k]; |
3411d4ec |
613 | $used[$k] = true; |
5c300c60 |
614 | break; |
7e235a1a |
615 | } |
616 | } |
7e235a1a |
617 | /* List special folders and their subfolders, if requested. */ |
3411d4ec |
618 | if ($list_special_folders_first) { |
cef054e4 |
619 | for($k = 0; $k < $cnt; ++$k) { |
ff245fbd |
620 | if (!$used[$k] && isSpecialMailbox($boxesall[$k]['unformatted'])) { |
621 | $boxesnew[] = $boxesall[$k]; |
622 | $used[$k] = true; |
7e235a1a |
623 | } |
5c300c60 |
624 | } |
625 | } |
7e235a1a |
626 | |
7e235a1a |
627 | /* Rest of the folders */ |
ff245fbd |
628 | for($k = 0; $k < $cnt; $k++) { |
629 | if (!$used[$k]) { |
630 | $boxesnew[] = $boxesall[$k]; |
7e235a1a |
631 | } |
632 | } |
43b698c7 |
633 | } |
3411d4ec |
634 | return $boxesnew; |
43b698c7 |
635 | } |
636 | |
90de1755 |
637 | /* |
638 | * Returns a list of all folders, subscribed or not |
639 | */ |
1da22cda |
640 | function sqimap_mailbox_list_all($imap_stream) { |
3411d4ec |
641 | global $list_special_folders_first, $folder_prefix, $delimiter; |
780dd344 |
642 | $read_ary = sqimap_run_command($imap_stream,"LIST \"$folder_prefix\" *",true,$response, $message,false); |
43b698c7 |
643 | $g = 0; |
90de1755 |
644 | $phase = 'inbox'; |
7d82bceb |
645 | $fld_pre_length = strlen($folder_prefix); |
ff245fbd |
646 | for ($i = 0, $cnt = count($read_ary); $i < $cnt; $i++) { |
43b698c7 |
647 | /* Another workaround for EIMS */ |
648 | if (isset($read_ary[$i + 1]) && |
90de1755 |
649 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
43b698c7 |
650 | $read_ary[$i], $regs)) { |
651 | $i ++; |
3411d4ec |
652 | $read_ary[$i] = $regs[1] . '"' . addslashes(trim($read_ary[$i])) . '"' . $regs[2]; |
43b698c7 |
653 | } |
780dd344 |
654 | /* Store the raw IMAP reply */ |
655 | $boxes[$g]['raw'] = $read_ary[$i]; |
90de1755 |
656 | |
780dd344 |
657 | /* Count number of delimiters ($delimiter) in folder name */ |
658 | $mailbox = find_mailbox_name($read_ary[$i]); |
659 | $dm_count = substr_count($mailbox, $delimiter); |
660 | if (substr($mailbox, -1) == $delimiter) { |
661 | /* If name ends in delimiter - decrement count by one */ |
662 | $dm_count--; |
663 | } |
90de1755 |
664 | |
780dd344 |
665 | /* Format folder name, but only if it's a INBOX.* or has a parent. */ |
666 | $boxesallbyname[$mailbox] = $g; |
667 | $parentfolder = readMailboxParent($mailbox, $delimiter); |
668 | if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) || |
669 | (ereg('^'.$folder_prefix, $mailbox)) || |
670 | ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) { |
671 | if ($dm_count) { |
672 | $boxes[$g]['formatted'] = str_repeat(' ', $dm_count); |
90de1755 |
673 | } else { |
780dd344 |
674 | $boxes[$g]['formatted'] = ''; |
12d61439 |
675 | } |
780dd344 |
676 | $boxes[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter)); |
677 | } else { |
678 | $boxes[$g]['formatted'] = imap_utf7_decode_local($mailbox); |
679 | } |
680 | |
681 | $boxes[$g]['unformatted-dm'] = $mailbox; |
682 | if (substr($mailbox, -1) == $delimiter) { |
683 | $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); |
684 | } |
685 | $boxes[$g]['unformatted'] = $mailbox; |
686 | $boxes[$g]['unformatted-disp'] = substr($mailbox,$fld_pre_length); |
687 | |
688 | $boxes[$g]['id'] = $g; |
689 | |
690 | /* Now lets get the flags for this mailbox */ |
691 | $read_mlbx = $read_ary[$i]; |
692 | $flags = substr($read_mlbx, strpos($read_mlbx, '(')+1); |
693 | $flags = substr($flags, 0, strpos($flags, ')')); |
694 | $flags = str_replace('\\', '', $flags); |
695 | $flags = trim(strtolower($flags)); |
696 | if ($flags) { |
697 | $boxes[$g]['flags'] = explode(' ', $flags); |
698 | } else { |
699 | $boxes[$g]['flags'] = array(); |
43b698c7 |
700 | } |
701 | $g++; |
702 | } |
703 | if(is_array($boxes)) { |
e429f014 |
704 | sort ($boxes); |
43b698c7 |
705 | } |
90de1755 |
706 | |
43b698c7 |
707 | return $boxes; |
708 | } |
5bdd7223 |
709 | |
60b5724d |
710 | function sqimap_mailbox_tree($imap_stream) { |
711 | global $boxesnew, $default_folder_prefix, $unseen_notify, $unseen_type; |
ff245fbd |
712 | if (!isset($boxesnew)) { |
60b5724d |
713 | |
714 | global $data_dir, $username, $list_special_folders_first, |
a2e66c6d |
715 | $folder_prefix, $delimiter, $trash_folder, $move_to_trash, |
716 | $imap_server_type; |
60b5724d |
717 | |
718 | |
719 | $inbox_in_list = false; |
720 | $inbox_subscribed = false; |
721 | |
08185f2a |
722 | require_once(SM_PATH . 'include/load_prefs.php'); |
60b5724d |
723 | |
724 | /* LSUB array */ |
2c617aa5 |
725 | $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"", |
60b5724d |
726 | true, $response, $message); |
727 | |
9871bdaa |
728 | /* Check to see if we have an INBOX */ |
78bc908d |
729 | $has_inbox = false; |
730 | |
731 | for ($i = 0, $cnt = count($lsub_ary); $i < $cnt; $i++) { |
43a31298 |
732 | if (preg_match("/^\*\s+LSUB\s+(.*)\"?INBOX\"?[^(\/\.)].*$/",$lsub_ary[$i])) { |
78bc908d |
733 | $has_inbox = true; |
734 | break; |
735 | } |
736 | } |
737 | |
738 | if ($has_inbox == false) { |
739 | $lsub_ibx = sqimap_run_command( $imap_stream, "LSUB \"\" \"INBOX\"", true, $response, $message ); |
8ff83209 |
740 | if (isset($lsub_ibx[0]) && (preg_match("/^\*\s+LSUB\s+(.*)\"?INBOX\"?[^(\/\.)].*$/",$lsub_ibx[0]))) { |
741 | $lsub_ary[] = $lsub_ibx[0]; |
742 | } else { |
743 | $lsub_ibx = sqimap_run_command( $imap_stream, "LIST \"\" \"INBOX\"", true, $response, $message ); |
744 | if (preg_match("/^\*\s+LIST\s+(.*)\"?INBOX\"?[^(\/\.)].*$/",$lsub_ibx[0])) { |
745 | sqimap_run_command( $imap_stream, "SUBSCRIBE \"INBOX\"", true, $response, $message ); |
746 | $lsub_ibx[0] = str_replace("LIST","LSUB",$lsub_ibx[0]); |
78bc908d |
747 | $lsub_ary[] = $lsub_ibx[0]; |
748 | } |
749 | } |
750 | } |
751 | |
60b5724d |
752 | /* |
753 | * Section about removing the last element was removed |
754 | * We don't return "* OK" anymore from sqimap_read_data |
755 | */ |
756 | $sorted_lsub_ary = array(); |
e4c6fe41 |
757 | $cnt = count($lsub_ary); |
43a31298 |
758 | |
ff245fbd |
759 | for ($i = 0; $i < $cnt; $i++) { |
60b5724d |
760 | /* |
761 | * Workaround for EIMS |
762 | * Doesn't work if the mailbox name is multiple lines |
763 | */ |
764 | if (isset($lsub_ary[$i + 1]) && |
765 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
766 | $lsub_ary[$i], $regs)) { |
ff245fbd |
767 | $i++; |
60b5724d |
768 | $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2]; |
769 | } |
770 | |
ff245fbd |
771 | $mbx = find_mailbox_name($lsub_ary[$i]); |
a2e66c6d |
772 | |
483f9ef9 |
773 | // only do the noselect test if !uw, is checked later. FIX ME see conf.pl setting |
774 | if ($imap_server_type != "uw") { |
775 | $noselect = check_is_noselect($lsub_ary[$i]); |
a2e66c6d |
776 | } |
ff245fbd |
777 | if (substr($mbx, -1) == $delimiter) { |
778 | $mbx = substr($mbx, 0, strlen($mbx) - 1); |
779 | } |
780 | $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect); |
60b5724d |
781 | } |
5c300c60 |
782 | // FIX ME this requires a config setting inside conf.pl instead of checking on server type |
483f9ef9 |
783 | if ($imap_server_type == "uw") { |
5c300c60 |
784 | $aQuery = array(); |
785 | $aTag = array(); |
786 | // prepare an array with queries |
787 | foreach ($sorted_lsub_ary as $aMbx) { |
788 | $mbx = $aMbx['mbx']; |
789 | $query = "LIST \"\" \"$mbx\""; |
483f9ef9 |
790 | sqimap_prepare_pipelined_query($query,$tag,$aQuery,false); |
5c300c60 |
791 | $aTag[$tag] = $mbx; |
792 | } |
793 | $sorted_lsub_ary = array(); |
794 | // execute all the queries at once |
795 | $aResponse = sqimap_run_pipelined_command ($imap_stream, $aQuery, false, $aServerResponse, $aServerMessage); |
796 | foreach($aTag as $tag => $mbx) { |
797 | if ($aServerResponse[$tag] == 'OK') { |
798 | $sResponse = implode('', $aResponse[$tag]); |
799 | $noselect = check_is_noselect($sResponse); |
800 | $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect); |
801 | } |
802 | } |
803 | $cnt = count($sorted_lsub_ary); |
483f9ef9 |
804 | } |
805 | |
806 | $sorted_lsub_ary = array_values($sorted_lsub_ary); |
807 | array_multisort($sorted_lsub_ary, SORT_ASC, SORT_REGULAR); |
808 | $boxesnew = sqimap_fill_mailbox_tree($sorted_lsub_ary,false,$imap_stream); |
809 | return $boxesnew; |
60b5724d |
810 | } |
811 | } |
812 | |
483f9ef9 |
813 | function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false,$imap_stream) { |
60b5724d |
814 | global $data_dir, $username, $list_special_folders_first, |
815 | $folder_prefix, $trash_folder, $sent_folder, $draft_folder, |
816 | $move_to_trash, $move_to_sent, $save_as_draft, |
43a31298 |
817 | $delimiter, $imap_server_type; |
60b5724d |
818 | |
819 | $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder); |
ff245fbd |
820 | |
60b5724d |
821 | /* create virtual root node */ |
822 | $mailboxes= new mailboxes(); |
823 | $mailboxes->is_root = true; |
ff245fbd |
824 | $trail_del = false; |
78bc908d |
825 | $start = 0; |
826 | |
587ec647 |
827 | |
43a31298 |
828 | if (isset($folder_prefix) && ($folder_prefix != '')) { |
ff245fbd |
829 | $start = substr_count($folder_prefix,$delimiter); |
830 | if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) { |
831 | $trail_del = true; |
832 | $mailboxes->mailboxname_full = substr($folder_prefix,0, (strlen($folder_prefix)-1)); |
833 | } else { |
834 | $mailboxes->mailboxname_full = $folder_prefix; |
835 | $start++; |
836 | } |
837 | $mailboxes->mailboxname_sub = $mailboxes->mailboxname_full; |
838 | } else { |
839 | $start = 0; |
840 | } |
78bc908d |
841 | |
e4c6fe41 |
842 | $cnt = count($mbx_ary); |
843 | for ($i=0; $i < $cnt; $i++) { |
ff245fbd |
844 | if ($mbx_ary[$i]['mbx'] !='' ) { |
845 | $mbx = new mailboxes(); |
846 | $mailbox = $mbx_ary[$i]['mbx']; |
847 | switch ($mailbox) { |
848 | case 'INBOX': |
849 | $mbx->is_inbox = true; |
850 | $mbx->is_special = true; |
851 | break; |
852 | case $trash_folder: |
853 | $mbx->is_trash = true; |
854 | $mbx->is_special = true; |
855 | break; |
856 | case $sent_folder: |
857 | $mbx->is_sent = true; |
858 | $mbx->is_special = true; |
859 | break; |
860 | case $draft_folder: |
861 | $mbx->is_draft = true; |
862 | $mbx->is_special = true; |
863 | break; |
864 | } |
865 | |
866 | if (isset($mbx_ary[$i]['unseen'])) { |
867 | $mbx->unseen = $mbx_ary[$i]['unseen']; |
868 | } |
869 | if (isset($mbx_ary[$i]['nummessages'])) { |
870 | $mbx->total = $mbx_ary[$i]['nummessages']; |
871 | } |
872 | |
873 | $mbx->is_noselect = $mbx_ary[$i]['noselect']; |
874 | |
60b5724d |
875 | $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter); |
ff245fbd |
876 | if ($r_del_pos) { |
587ec647 |
877 | $mbx->mailboxname_sub = substr($mbx_ary[$i]['mbx'],$r_del_pos+1); |
ff245fbd |
878 | } else { /* mailbox is root folder */ |
587ec647 |
879 | $mbx->mailboxname_sub = $mbx_ary[$i]['mbx']; |
ff245fbd |
880 | } |
881 | $mbx->mailboxname_full = $mbx_ary[$i]['mbx']; |
38068e69 |
882 | |
9871bdaa |
883 | $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first); |
ff245fbd |
884 | } |
60b5724d |
885 | } |
587ec647 |
886 | sqimap_utf7_decode_mbx_tree($mailboxes); |
483f9ef9 |
887 | sqimap_get_status_mbx_tree($imap_stream,$mailboxes); |
60b5724d |
888 | return $mailboxes; |
889 | } |
259faa39 |
890 | |
587ec647 |
891 | function sqimap_utf7_decode_mbx_tree(&$mbx_tree) { |
892 | $mbx_tree->mailboxname_sub=imap_utf7_decode_local($mbx_tree->mailboxname_sub); |
893 | if ($mbx_tree->mbxs) { |
894 | $iCnt = count($mbx_tree->mbxs); |
895 | for ($i=0;$i<$iCnt;++$i) { |
896 | $mbxs_tree->mbxs[$i] = sqimap_utf7_decode_mbx_tree($mbx_tree->mbxs[$i]); |
897 | } |
898 | } |
483f9ef9 |
899 | } |
900 | |
901 | |
902 | function sqimap_tree_to_ref_array(&$mbx_tree,&$aMbxs) { |
5c300c60 |
903 | if ($mbx_tree) |
483f9ef9 |
904 | $aMbxs[] =& $mbx_tree; |
905 | if ($mbx_tree->mbxs) { |
906 | $iCnt = count($mbx_tree->mbxs); |
907 | for ($i=0;$i<$iCnt;++$i) { |
5c300c60 |
908 | sqimap_tree_to_ref_array($mbx_tree->mbxs[$i],$aMbxs); |
483f9ef9 |
909 | } |
910 | } |
911 | } |
912 | |
913 | |
914 | /* Define preferences for folder settings. */ |
915 | /* FIXME, we should load constants.php |
916 | unseen_notify |
917 | define('SMPREF_UNSEEN_NONE', 1); |
918 | define('SMPREF_UNSEEN_INBOX', 2); |
919 | define('SMPREF_UNSEEN_ALL', 3); |
920 | |
921 | define('SMPREF_UNSEEN_SPECIAL', 4); // Only special folders |
922 | define('SMPREF_UNSEEN_NORMAL', 5); // Only normal folders |
923 | |
924 | unseen_type |
925 | define('SMPREF_UNSEEN_ONLY', 1); |
926 | define('SMPREF_UNSEEN_TOTAL', 2); |
927 | */ |
928 | |
929 | function sqimap_get_status_mbx_tree($imap_stream,&$mbx_tree) { |
930 | global $unseen_notify, $unseen_type, $trash_folder,$move_to_trash; |
931 | $aMbxs = $aQuery = $aTag = array(); |
932 | sqimap_tree_to_ref_array($mbx_tree,$aMbxs); |
933 | // remove the root node |
934 | array_shift($aMbxs); |
935 | |
936 | if($unseen_notify == 3) { |
937 | $cnt = count($aMbxs); |
938 | for($i=0;$i<$cnt;++$i) { |
5c300c60 |
939 | $oMbx =& $aMbxs[$i]; |
940 | if (!$oMbx->is_noselect) { |
483f9ef9 |
941 | $mbx = $oMbx->mailboxname_full; |
5c300c60 |
942 | if ($unseen_type == 2 || |
943 | ($move_to_trash && $oMbx->mailboxname_full == $trash_folder)) { |
944 | $query = "STATUS \"$mbx\" (MESSAGES UNSEEN)"; |
945 | } else { |
946 | $query = "STATUS \"$mbx\" (UNSEEN)"; |
947 | } |
483f9ef9 |
948 | sqimap_prepare_pipelined_query($query,$tag,$aQuery,false); |
5c300c60 |
949 | } else { |
950 | $oMbx->unseen = $oMbx->total = false; |
951 | $tag = false; |
952 | } |
953 | $oMbx->tag = $tag; |
954 | $aMbxs[$i] =& $oMbx; |
483f9ef9 |
955 | } |
956 | // execute all the queries at once |
957 | $aResponse = sqimap_run_pipelined_command ($imap_stream, $aQuery, false, $aServerResponse, $aServerMessage); |
958 | $cnt = count($aMbxs); |
959 | for($i=0;$i<$cnt;++$i) { |
5c300c60 |
960 | $oMbx =& $aMbxs[$i]; |
961 | $tag = $oMbx->tag; |
962 | if ($tag && $aServerResponse[$tag] == 'OK') { |
963 | $sResponse = implode('', $aResponse[$tag]); |
483f9ef9 |
964 | if (preg_match('/UNSEEN\s+([0-9]+)/i', $sResponse, $regs)) { |
965 | $oMbx->unseen = $regs[1]; |
966 | } |
967 | if (preg_match('/MESSAGES\s+([0-9]+)/i', $sResponse, $regs)) { |
968 | $oMbx->total = $regs[1]; |
5c300c60 |
969 | } |
970 | } |
971 | unset($oMbx->tag); |
972 | } |
483f9ef9 |
973 | } else if ($unseen_notify == 2) { // INBOX only |
974 | $cnt = count($aMbxs); |
975 | for($i=0;$i<$cnt;++$i) { |
5c300c60 |
976 | $oMbx =& $aMbxs[$i]; |
977 | if (strtoupper($oMbx->mailboxname_full) == 'INBOX' || |
978 | ($move_to_trash && $oMbx->mailboxname_full == $trash_folder)) { |
979 | if ($unseen_type == 2 || |
980 | ($oMbx->mailboxname_full == $trash_folder && $move_to_trash)) { |
981 | $aStatus = sqimap_status_messages($imap_stream,$oMbx->mailboxname_full); |
982 | $oMbx->unseen = $aStatus['UNSEEN']; |
983 | $oMbx->total = $aStatus['MESSAGES']; |
984 | } else { |
985 | $oMbx->unseen = sqimap_unseen_messages($imap_stream,$oMbx->mailboxname_full); |
986 | } |
987 | $aMbxs[$i] =& $oMbx; |
988 | if (!$move_to_trash && $trash_folder) { |
989 | break; |
990 | } else { |
991 | // trash comes after INBOX |
992 | if ($oMbx->mailboxname_full == $trash_folder) { |
993 | break; |
994 | } |
995 | } |
996 | } |
997 | } |
998 | } |
483f9ef9 |
999 | } |
587ec647 |
1000 | |
648713af |
1001 | ?> |