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