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