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