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