adding mailbox caching code by Michael Long
[squirrelmail.git] / functions / imap_mailbox.php
1 <?php
2
3 /**
4 * imap_mailbox.php
5 *
6 * This implements all functions that manipulate mailboxes
7 *
8 * @copyright &copy; 1999-2005 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
10 * @version $Id$
11 * @package squirrelmail
12 * @subpackage imap
13 */
14
15 /** @ignore */
16 if (! defined('SM_PATH')) define('SM_PATH','../');
17
18 /** UTF7 support */
19 require_once(SM_PATH . 'functions/imap_utf7_local.php');
20
21 global $boxesnew;
22
23 /**
24 * Mailboxes class
25 *
26 * FIXME. This class should be extracted and placed in a separate file that
27 * can be included before we start the session. That makes caching of the tree
28 * possible. On a refresh mailboxes from left_main.php the only function that
29 * should be called is the sqimap_get_status_mbx_tree. In case of subscribe
30 * / rename / delete / new we have to create methods for adding/changing the
31 * mailbox in the mbx_tree without the need for a refresh.
32 *
33 * Some code fragments are present in 1.3.0 - 1.4.4.
34 * @package squirrelmail
35 * @subpackage imap
36 * @since 1.5.0
37 */
38 class mailboxes {
39 var $mailboxname_full = '', $mailboxname_sub= '', $is_noselect = false, $is_noinferiors = false,
40 $is_special = false, $is_root = false, $is_inbox = false, $is_sent = false,
41 $is_trash = false, $is_draft = false, $mbxs = array(),
42 $unseen = false, $total = false;
43
44 function addMbx($mbx, $delimiter, $start, $specialfirst) {
45 $ary = explode($delimiter, $mbx->mailboxname_full);
46 $mbx_parent =& $this;
47 for ($i = $start, $c = count($ary)-1; $i < $c; $i++) {
48 $mbx_childs =& $mbx_parent->mbxs;
49 $found = false;
50 if ($mbx_childs) {
51 foreach ($mbx_childs as $key => $parent) {
52 if ($parent->mailboxname_sub == $ary[$i]) {
53 $mbx_parent =& $mbx_parent->mbxs[$key];
54 $found = true;
55 break;
56 }
57 }
58 }
59 if (!$found) {
60 $no_select_mbx = new mailboxes();
61 if (isset($mbx_parent->mailboxname_full) && $mbx_parent->mailboxname_full != '') {
62 $no_select_mbx->mailboxname_full = $mbx_parent->mailboxname_full.$delimiter.$ary[$i];
63 } else {
64 $no_select_mbx->mailboxname_full = $ary[$i];
65 }
66 $no_select_mbx->mailboxname_sub = $ary[$i];
67 $no_select_mbx->is_noselect = true;
68 $mbx_parent->mbxs[] = $no_select_mbx;
69 $i--;
70 }
71 }
72 $mbx_parent->mbxs[] = $mbx;
73 if ($mbx->is_special && $specialfirst) {
74 usort($mbx_parent->mbxs, 'sortSpecialMbx');
75 }
76 }
77 }
78
79 /**
80 * array callback used for sorting in mailboxes class
81 * @param object $a
82 * @param object $b
83 * @return integer see php strnatcasecmp()
84 * @since 1.3.0
85 */
86 function sortSpecialMbx($a, $b) {
87 if ($a->is_inbox) {
88 $acmp = '0'. $a->mailboxname_full;
89 } else if ($a->is_special) {
90 $acmp = '1'. $a->mailboxname_full;
91 } else {
92 $acmp = '2' . $a->mailboxname_full;
93 }
94 if ($b->is_inbox) {
95 $bcmp = '0'. $b->mailboxname_full;
96 }else if ($b->is_special) {
97 $bcmp = '1' . $b->mailboxname_full;
98 } else {
99 $bcmp = '2' . $b->mailboxname_full;
100 }
101 return strnatcasecmp($acmp, $bcmp);
102 }
103
104 /**
105 * @param array $ary
106 * @return array
107 * @since 1.5.0
108 */
109 function compact_mailboxes_response($ary) {
110 /*
111 * Workaround for mailboxes returned as literal
112 * FIXME : Doesn't work if the mailbox name is multiple lines
113 * (larger then fgets buffer)
114 */
115 for ($i = 0, $iCnt=count($ary); $i < $iCnt; $i++) {
116 if (isset($ary[$i + 1]) && substr($ary[$i], -3) == "}\r\n") {
117 if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
118 $ary[$i], $regs)) {
119 $ary[$i] = $regs[1] . '"' . addslashes(trim($ary[$i+1])) . '"' . $regs[2];
120 array_splice($ary, $i+1, 2);
121 }
122 }
123 }
124 /* remove duplicates and ensure array is contiguous */
125 return array_values(array_unique($ary));
126 }
127
128 /**
129 * Extract the mailbox name from an untagged LIST (7.2.2) or LSUB (7.2.3) answer
130 * (LIST|LSUB) (<Flags list>) (NIL|"<separator atom>") <mailbox name string>\r\n
131 * mailbox name in quoted string MUST be unquoted and stripslashed (sm API)
132 *
133 * Originally stored in functions/strings.php. Since 1.2.6 stored in
134 * functions/imap_mailbox.php
135 * @param string $line imap LIST/LSUB response line
136 * @return string mailbox name
137 */
138 function find_mailbox_name($line) {
139 if (preg_match('/^\* (?:LIST|LSUB) \([^\)]*\) (?:NIL|\"[^\"]*\") ([^\r\n]*)[\r\n]*$/i', $line, $regs)) {
140 if (substr($regs[1], 0, 1) == '"')
141 return stripslashes(substr($regs[1], 1, -1));
142 return $regs[1];
143 }
144 return '';
145 }
146
147 /**
148 * Detects if mailbox has noselect flag (can't store messages)
149 * In versions older than 1.4.5 function checks only LSUB responses
150 * and can produce pcre warnings.
151 * @param string $lsub_line mailbox line from untagged LIST or LSUB response
152 * @return bool whether this is a Noselect mailbox.
153 * @since 1.3.2
154 */
155 function check_is_noselect ($lsub_line) {
156 return preg_match("/^\* (LSUB|LIST) \([^\)]*\\\\Noselect[^\)]*\)/i", $lsub_line);
157 }
158
159 /**
160 * Detects if mailbox has noinferiors flag (can't store subfolders)
161 * @param string $lsub_line mailbox line from untagged LIST or LSUB response
162 * @return bool whether this is a Noinferiors mailbox.
163 * @since 1.5.0
164 */
165 function check_is_noinferiors ($lsub_line) {
166 return preg_match("/^\* (LSUB|LIST) \([^\)]*\\\\Noinferiors[^\)]*\)/i", $lsub_line);
167 }
168
169 /**
170 * Detects mailbox's parent folder
171 *
172 * If $haystack is a full mailbox name, and $needle is the mailbox
173 * separator character, returns the second last part of the full
174 * mailbox name (i.e. the mailbox's parent mailbox)
175 *
176 * Originally stored in functions/strings.php. Since 1.2.6 stored in
177 * functions/imap_mailbox.php
178 * @param string $haystack full mailbox name
179 * @param string $needle delimiter
180 * @return string parent mailbox
181 */
182 function readMailboxParent($haystack, $needle) {
183 if ($needle == '') {
184 $ret = '';
185 } else {
186 $parts = explode($needle, $haystack);
187 $elem = array_pop($parts);
188 while ($elem == '' && count($parts)) {
189 $elem = array_pop($parts);
190 }
191 $ret = join($needle, $parts);
192 }
193 return( $ret );
194 }
195
196 /**
197 * Check if $subbox is below the specified $parentbox
198 * @param string $subbox potential sub folder
199 * @param string $parentbox potential parent
200 * @return boolean
201 * @since 1.2.3
202 */
203 function isBoxBelow( $subbox, $parentbox ) {
204 global $delimiter;
205 /*
206 * Eliminate the obvious mismatch, where the
207 * subfolder path is shorter than that of the potential parent
208 */
209 if ( strlen($subbox) < strlen($parentbox) ) {
210 return false;
211 }
212 /* check for delimiter */
213 if (substr($parentbox,-1) != $delimiter) {
214 $parentbox .= $delimiter;
215 }
216
217 return (substr($subbox,0,strlen($parentbox)) == $parentbox);
218 }
219
220 /**
221 * Defines special mailboxes: given a mailbox name, it checks if this is a
222 * "special" one: INBOX, Trash, Sent or Draft.
223 *
224 * Since 1.2.5 function includes special_mailbox hook.<br>
225 * Since 1.4.3 hook supports more than one plugin.
226 * @param string $box mailbox name
227 * @return boolean
228 * @since 1.2.3
229 */
230 function isSpecialMailbox( $box ) {
231 $ret = ( (strtolower($box) == 'inbox') ||
232 isTrashMailbox($box) || isSentMailbox($box) || isDraftMailbox($box) );
233
234 if ( !$ret ) {
235 $ret = boolean_hook_function('special_mailbox',$box,1);
236 }
237 return $ret;
238 }
239
240 /**
241 * Detects if mailbox is a Trash folder or subfolder of Trash
242 * @param string $box mailbox name
243 * @return bool whether this is a Trash folder
244 * @since 1.4.0
245 */
246 function isTrashMailbox ($box) {
247 global $trash_folder, $move_to_trash;
248 return $move_to_trash && $trash_folder &&
249 ( $box == $trash_folder || isBoxBelow($box, $trash_folder) );
250 }
251
252 /**
253 * Detects if mailbox is a Sent folder or subfolder of Sent
254 * @param string $box mailbox name
255 * @return bool whether this is a Sent folder
256 * @since 1.4.0
257 */
258 function isSentMailbox($box) {
259 global $sent_folder, $move_to_sent;
260 return $move_to_sent && $sent_folder &&
261 ( $box == $sent_folder || isBoxBelow($box, $sent_folder) );
262 }
263
264 /**
265 * Detects if mailbox is a Drafts folder or subfolder of Drafts
266 * @param string $box mailbox name
267 * @return bool whether this is a Draft folder
268 * @since 1.4.0
269 */
270 function isDraftMailbox($box) {
271 global $draft_folder, $save_as_draft;
272 return $save_as_draft &&
273 ( $box == $draft_folder || isBoxBelow($box, $draft_folder) );
274 }
275
276 /**
277 * Expunges a mailbox
278 *
279 * WARNING: Select mailbox before calling this function.
280 *
281 * permanently removes all messages that have the \Deleted flag
282 * set from the selected mailbox. See EXPUNGE command chapter in
283 * IMAP RFC.
284 * @param stream $imap_stream imap connection resource
285 * @param string $mailbox mailbox name (unused since 1.1.3).
286 * @param boolean $handle_errors error handling control (displays error_box on error).
287 * @param mixed $id (since 1.3.0) integer message id or array with integer ids
288 * @return integer number of expunged messages
289 * @since 1.0 or older
290 */
291 function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true, $id='') {
292 if ($id) {
293 if (is_array($id)) {
294 $id = sqimap_message_list_squisher($id);
295 }
296 $id = ' '.$id;
297 $uid = TRUE;
298 } else {
299 $uid = false;
300 }
301 $read = sqimap_run_command($imap_stream, 'EXPUNGE'.$id, $handle_errors,
302 $response, $message, $uid);
303 $cnt = 0;
304
305 if (is_array($read)) {
306 foreach ($read as $r) {
307 if (preg_match('/^\*\s[0-9]+\sEXPUNGE/AUi',$r,$regs)) {
308 $cnt++;
309 }
310 }
311 }
312 return $cnt;
313 }
314
315 /**
316 * Checks whether or not the specified mailbox exists
317 *
318 * @param stream $imap_stream imap connection resource
319 * @param string $mailbox mailbox name
320 * @param array $mailboxlist (since 1.5.1) optional array of mailboxes from
321 * sqimap_get_mailboxes() (to avoid having to talk to imap server)
322 * @return boolean
323 * @since 1.0 or older
324 */
325 function sqimap_mailbox_exists ($imap_stream, $mailbox, $mailboxlist=null) {
326 if (!isset($mailbox) || empty($mailbox)) {
327 return false;
328 }
329
330 if (is_array($mailboxlist)) {
331 // use previously retrieved mailbox list
332 foreach ($mailboxlist as $mbox) {
333 if ($mbox['unformatted-dm'] == $mailbox) { return true; }
334 }
335 return false;
336 } else {
337 // go to imap server
338 $mbx = sqimap_run_command($imap_stream, 'LIST "" ' . sqimap_encode_mailbox_name($mailbox),
339 true, $response, $message);
340 return isset($mbx[0]);
341 }
342 }
343
344 /**
345 * Selects a mailbox
346 * Before 1.3.0 used more arguments and returned data depended on those argumements.
347 * @param stream $imap_stream imap connection resource
348 * @param string $mailbox mailbox name
349 * @return array results of select command (on success - permanentflags, flags and rights)
350 * @since 1.0 or older
351 */
352 function sqimap_mailbox_select ($imap_stream, $mailbox) {
353 if ($mailbox == 'None') {
354 return;
355 }
356
357 $read = sqimap_run_command($imap_stream, 'SELECT ' . sqimap_encode_mailbox_name($mailbox),
358 true, $response, $message);
359 $result = array();
360 for ($i = 0, $cnt = count($read); $i < $cnt; $i++) {
361 if (preg_match('/^\*\s+OK\s\[(\w+)\s(\w+)\]/',$read[$i], $regs)) {
362 $result[strtoupper($regs[1])] = $regs[2];
363 } else if (preg_match('/^\*\s([0-9]+)\s(\w+)/',$read[$i], $regs)) {
364 $result[strtoupper($regs[2])] = $regs[1];
365 } else {
366 if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) {
367 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ;
368 $result['PERMANENTFLAGS'] = explode(' ',strtolower($regs[1]));
369 } else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
370 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ;
371 $result['FLAGS'] = explode(' ',strtolower($regs[1]));
372 }
373 }
374 }
375 if (!isset($result['PERMANENTFLAGS'])) {
376 $result['PERMANENTFLAGS'] = $result['FLAGS'];
377 }
378 if (preg_match('/^\[(.+)\]/',$message, $regs)) {
379 $result['RIGHTS']=strtoupper($regs[1]);
380 }
381
382 return $result;
383 }
384
385 /**
386 * Creates a folder.
387 *
388 * Mailbox is automatically subscribed.
389 *
390 * Set $type to string that does not match 'noselect' (case insensitive),
391 * if you don't want to prepend delimiter to mailbox name. Please note
392 * that 'noinferiors' might be used someday as keyword for folders
393 * that store only messages.
394 * @param stream $imap_steam imap connection resource
395 * @param string $mailbox mailbox name
396 * @param string $type folder type.
397 * @since 1.0 or older
398 */
399 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
400 global $delimiter;
401 if (strtolower($type) == 'noselect') {
402 $mailbox .= $delimiter;
403 }
404
405 $read_ary = sqimap_run_command($imap_stream, 'CREATE ' .
406 sqimap_encode_mailbox_name($mailbox),
407 true, $response, $message);
408 sqimap_subscribe ($imap_stream, $mailbox);
409 }
410
411 /**
412 * Subscribes to an existing folder.
413 * @param stream $imap_stream imap connection resource
414 * @param string $mailbox mailbox name
415 * @param boolean $debug (since 1.5.1)
416 * @since 1.0 or older
417 */
418 function sqimap_subscribe ($imap_stream, $mailbox,$debug=true) {
419 $read_ary = sqimap_run_command($imap_stream, 'SUBSCRIBE ' .
420 sqimap_encode_mailbox_name($mailbox),
421 $debug, $response, $message);
422 }
423
424 /**
425 * Unsubscribes from an existing folder
426 * @param stream $imap_stream imap connection resource
427 * @param string $mailbox mailbox name
428 * @since 1.0 or older
429 */
430 function sqimap_unsubscribe ($imap_stream, $mailbox) {
431 $read_ary = sqimap_run_command($imap_stream, 'UNSUBSCRIBE ' .
432 sqimap_encode_mailbox_name($mailbox),
433 false, $response, $message);
434 }
435
436 /**
437 * Deletes the given folder
438 * Since 1.2.6 and 1.3.0 contains rename_or_delete_folder hook
439 * @param stream $imap_stream imap connection resource
440 * @param string $mailbox mailbox name
441 * @since 1.0 or older
442 */
443 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
444 global $data_dir, $username;
445 sqimap_unsubscribe ($imap_stream, $mailbox);
446
447 if (sqimap_mailbox_exists($imap_stream, $mailbox)) {
448
449 $read_ary = sqimap_run_command($imap_stream, 'DELETE ' .
450 sqimap_encode_mailbox_name($mailbox),
451 true, $response, $message);
452 if ($response !== 'OK') {
453 // subscribe again
454 sqimap_subscribe ($imap_stream, $mailbox);
455 } else {
456 do_hook_function('rename_or_delete_folder', $args = array($mailbox, 'delete', ''));
457 removePref($data_dir, $username, "thread_$mailbox");
458 removePref($data_dir, $username, "collapse_folder_$mailbox");
459 }
460 }
461 }
462
463 /**
464 * Determines if the user is subscribed to the folder or not
465 * @param stream $imap_stream imap connection resource
466 * @param string $mailbox mailbox name
467 * @return boolean
468 * @since 1.2.0
469 */
470 function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
471 $boxesall = sqimap_mailbox_list ($imap_stream);
472 foreach ($boxesall as $ref) {
473 if ($ref['unformatted'] == $folder) {
474 return true;
475 }
476 }
477 return false;
478 }
479
480 /**
481 * Renames a mailbox.
482 * Since 1.2.6 and 1.3.0 contains rename_or_delete_folder hook
483 * @param stream $imap_stream imap connection resource
484 * @param string $old_name mailbox name
485 * @param string $new_name new mailbox name
486 * @since 1.2.3
487 */
488 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
489 if ( $old_name != $new_name ) {
490 global $delimiter, $imap_server_type, $data_dir, $username;
491 if ( substr( $old_name, -1 ) == $delimiter ) {
492 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
493 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
494 $postfix = $delimiter;
495 } else {
496 $postfix = '';
497 }
498
499 $boxesall = sqimap_mailbox_list_all($imap_stream);
500 $cmd = 'RENAME ' . sqimap_encode_mailbox_name($old_name) .
501 ' ' . sqimap_encode_mailbox_name($new_name);
502 $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message);
503 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
504 $oldpref_thread = getPref($data_dir, $username, 'thread_'.$old_name.$postfix);
505 $oldpref_collapse = getPref($data_dir, $username, 'collapse_folder_'.$old_name.$postfix);
506 removePref($data_dir, $username, 'thread_'.$old_name.$postfix);
507 removePref($data_dir, $username, 'collapse_folder_'.$old_name.$postfix);
508 sqimap_subscribe($imap_stream, $new_name.$postfix);
509 setPref($data_dir, $username, 'thread_'.$new_name.$postfix, $oldpref_thread);
510 setPref($data_dir, $username, 'collapse_folder_'.$new_name.$postfix, $oldpref_collapse);
511 do_hook_function('rename_or_delete_folder',$args = array($old_name, 'rename', $new_name));
512 $l = strlen( $old_name ) + 1;
513 $p = 'unformatted';
514
515 foreach ($boxesall as $box) {
516 if (substr($box[$p], 0, $l) == $old_name . $delimiter) {
517 $new_sub = $new_name . $delimiter . substr($box[$p], $l);
518 /* With Cyrus IMAPd >= 2.0 rename is recursive, so don't check for errors here */
519 if ($imap_server_type == 'cyrus') {
520 $cmd = 'RENAME "' . $box[$p] . '" "' . $new_sub . '"';
521 $data = sqimap_run_command($imap_stream, $cmd, false,
522 $response, $message);
523 }
524 $was_subscribed = sqimap_mailbox_is_subscribed($imap_stream, $box[$p]);
525 if ( $was_subscribed ) {
526 sqimap_unsubscribe($imap_stream, $box[$p]);
527 }
528 $oldpref_thread = getPref($data_dir, $username, 'thread_'.$box[$p]);
529 $oldpref_collapse = getPref($data_dir, $username, 'collapse_folder_'.$box[$p]);
530 removePref($data_dir, $username, 'thread_'.$box[$p]);
531 removePref($data_dir, $username, 'collapse_folder_'.$box[$p]);
532 if ( $was_subscribed ) {
533 sqimap_subscribe($imap_stream, $new_sub);
534 }
535 setPref($data_dir, $username, 'thread_'.$new_sub, $oldpref_thread);
536 setPref($data_dir, $username, 'collapse_folder_'.$new_sub, $oldpref_collapse);
537 do_hook_function('rename_or_delete_folder',
538 $args = array($box[$p], 'rename', $new_sub));
539 }
540 }
541 }
542 }
543
544 /**
545 * Formats a mailbox into parts for the $boxesall array
546 *
547 * The parts are:
548 * <ul>
549 * <li>raw - Raw LIST/LSUB response from the IMAP server
550 * <li>formatted - nicely formatted folder name
551 * <li>unformatted - unformatted, but with delimiter at end removed
552 * <li>unformatted-dm - folder name as it appears in raw response
553 * <li>unformatted-disp - unformatted without $folder_prefix
554 * <li>id - TODO: document me
555 * <li>flags - TODO: document me
556 * </ul>
557 * Before 1.2.0 used third argument for delimiter.
558 *
559 * Before 1.5.1 used second argument for lsub line. Argument was removed in order to use
560 * find_mailbox_name() on the raw input. Since 1.5.1 includes RFC3501 names in flags
561 * array (for example, "\NoSelect" in addition to "noselect")
562 * @param array $line
563 * @return array
564 * @since 1.0 or older
565 * @todo document id and flags keys in boxes array and function arguments.
566 */
567 function sqimap_mailbox_parse ($line) {
568 global $folder_prefix, $delimiter;
569
570 /* Process each folder line */
571 for ($g = 0, $cnt = count($line); $g < $cnt; ++$g) {
572 /* Store the raw IMAP reply */
573 if (isset($line[$g])) {
574 $boxesall[$g]['raw'] = $line[$g];
575 } else {
576 $boxesall[$g]['raw'] = '';
577 }
578
579 /* Count number of delimiters ($delimiter) in folder name */
580 $mailbox = find_mailbox_name($line[$g]);
581 $dm_count = substr_count($mailbox, $delimiter);
582 if (substr($mailbox, -1) == $delimiter) {
583 /* If name ends in delimiter, decrement count by one */
584 $dm_count--;
585 }
586
587 /* Format folder name, but only if it's a INBOX.* or has a parent. */
588 $boxesallbyname[$mailbox] = $g;
589 $parentfolder = readMailboxParent($mailbox, $delimiter);
590 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
591 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
592 (isset($boxesallbyname[$parentfolder]) &&
593 (strlen($parentfolder) > 0) ) ) {
594 $indent = $dm_count - (substr_count($folder_prefix, $delimiter));
595 if ($indent > 0) {
596 $boxesall[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $indent);
597 } else {
598 $boxesall[$g]['formatted'] = '';
599 }
600 $boxesall[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
601 } else {
602 $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox);
603 }
604
605 $boxesall[$g]['unformatted-dm'] = $mailbox;
606 if (substr($mailbox, -1) == $delimiter) {
607 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
608 }
609 $boxesall[$g]['unformatted'] = $mailbox;
610 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
611 $mailbox = substr($mailbox, strlen($folder_prefix));
612 }
613 $boxesall[$g]['unformatted-disp'] = $mailbox;
614 $boxesall[$g]['id'] = $g;
615
616 $boxesall[$g]['flags'] = array();
617 if (isset($line[$g])) {
618 ereg("\(([^)]*)\)",$line[$g],$regs);
619 /**
620 * Since 1.5.1 flags are stored with RFC3501 naming
621 * and also the old way for backwards compatibility
622 * so for example "\NoSelect" and "noselect"
623 */
624 $flags = trim($regs[1]);
625 if ($flags) {
626 $flagsarr = explode(' ',$flags);
627 $flagsarrnew=$flagsarr;
628 // add old type
629 foreach ($flagsarr as $flag) {
630 $flagsarrnew[]=strtolower(str_replace('\\', '',$flag));
631 }
632 $boxesall[$g]['flags']=$flagsarrnew;
633 }
634 }
635 }
636 return $boxesall;
637 }
638
639 /**
640 * Returns list of options (to be echoed into select statement
641 * based on available mailboxes and separators
642 * Caller should surround options with <select ...> </select> and
643 * any formatting.
644 * @param stream $imap_stream imap connection resource to query for mailboxes
645 * @param array $show_selected array containing list of mailboxes to pre-select (0 if none)
646 * @param array $folder_skip array of folders to keep out of option list (compared in lower)
647 * @param $boxes list of already fetched boxes (for places like folder panel, where
648 * you know these options will be shown 3 times in a row.. (most often unset).
649 * @param string $flag (since 1.4.1) flag to check for in mailbox flags, used to filter out mailboxes.
650 * 'noselect' by default to remove unselectable mailboxes.
651 * 'noinferiors' used to filter out folders that can not contain subfolders.
652 * NULL to avoid flag check entirely.
653 * NOTE: noselect and noiferiors are used internally. The IMAP representation is
654 * \NoSelect and \NoInferiors
655 * @param boolean $use_long_format (since 1.4.1) override folder display preference and always show full folder name.
656 * @return string html formated mailbox selection options
657 * @since 1.3.2
658 */
659 function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_skip = 0, $boxes = 0,
660 $flag = 'noselect', $use_long_format = false ) {
661 global $username, $data_dir;
662 $mbox_options = '';
663 if ( $use_long_format ) {
664 $shorten_box_names = 0;
665 } else {
666 $shorten_box_names = getPref($data_dir, $username, 'mailbox_select_style', SMPREF_OFF);
667 }
668
669 if ($boxes == 0) {
670 $boxes = sqimap_mailbox_list($imap_stream);
671 }
672
673 foreach ($boxes as $boxes_part) {
674 if ($flag == NULL || (is_array($boxes_part['flags'])
675 && !in_array($flag, $boxes_part['flags']))) {
676 $box = $boxes_part['unformatted'];
677
678 if ($folder_skip != 0 && in_array($box, $folder_skip) ) {
679 continue;
680 }
681 $lowerbox = strtolower($box);
682 // mailboxes are casesensitive => inbox.sent != inbox.Sent
683 // nevermind, to many dependencies this should be fixed!
684
685 if (strtolower($box) == 'inbox') { // inbox is special and not casesensitive
686 $box2 = _("INBOX");
687 } else {
688 switch ($shorten_box_names)
689 {
690 case 2: /* delimited, style = 2 */
691 $box2 = str_replace('&amp;nbsp;&amp;nbsp;', '.&nbsp;', htmlspecialchars($boxes_part['formatted']));
692 break;
693 case 1: /* indent, style = 1 */
694 $box2 = str_replace('&amp;nbsp;&amp;nbsp;', '&nbsp;&nbsp;', htmlspecialchars($boxes_part['formatted']));
695 break;
696 default: /* default, long names, style = 0 */
697 $box2 = str_replace(' ', '&nbsp;', htmlspecialchars(imap_utf7_decode_local($boxes_part['unformatted-disp'])));
698 break;
699 }
700 }
701 if ($show_selected != 0 && in_array($lowerbox, $show_selected) ) {
702 $mbox_options .= '<option value="' . htmlspecialchars($box) .'" selected="selected">'.$box2.'</option>' . "\n";
703 } else {
704 $mbox_options .= '<option value="' . htmlspecialchars($box) .'">'.$box2.'</option>' . "\n";
705 }
706 }
707 }
708 return $mbox_options;
709 }
710
711 /**
712 * Returns sorted mailbox lists in several different ways.
713 *
714 * Since 1.5.1 most of the functionality has been moved to new function sqimap_get_mailboxes
715 *
716 * See comment on sqimap_mailbox_parse() for info about the returned array.
717 * @param resource $imap_stream imap connection resource
718 * @param boolean $force force update of mailbox listing. available since 1.4.2 and 1.5.0
719 * @return array list of mailboxes
720 * @since 1.0 or older
721 */
722 function sqimap_mailbox_list($imap_stream, $force=false) {
723 global $boxesnew,$show_only_subscribed_folders;
724 if (!sqgetGlobalVar('boxesnew',$boxesnew,SQ_SESSION) || $force) {
725 $boxesnew=sqimap_get_mailboxes($imap_stream,$force,$show_only_subscribed_folders);
726 }
727 return $boxesnew;
728 }
729
730 /**
731 * Returns a list of all folders, subscribed or not
732 *
733 * Since 1.5.1 code moved to sqimap_get_mailboxes()
734 *
735 * @param stream $imap_stream imap connection resource
736 * @return array see sqimap_mailbox_parse()
737 * @since 1.0 or older
738 */
739 function sqimap_mailbox_list_all($imap_stream) {
740 global $show_only_subscribed_folders;
741 // fourth argument prevents registration of retrieved list of mailboxes in session
742 $boxes=sqimap_get_mailboxes($imap_stream,true,false,false);
743 return $boxes;
744 }
745
746
747 /**
748 * Gets the list of mailboxes for sqimap_maolbox_tree and sqimap_mailbox_list
749 *
750 * This is because both of those functions had duplicated logic, but with slightly different
751 * implementations. This will make both use the same implementation, which should make it
752 * easier to maintain and easier to modify in the future
753 * @param stream $imap_stream imap connection resource
754 * @param bool $force force a reload and ignore cache
755 * @param bool $show_only_subscribed controls listing of visible or all folders
756 * @param bool $session_register controls registration of retrieved data in session.
757 * @return object boxesnew - array of mailboxes and their attributes
758 * @since 1.5.1
759 */
760 function sqimap_get_mailboxes($imap_stream,$force=false,$show_only_subscribed=true,$session_register=true) {
761 global $show_only_subscribed_folders,$noselect_fix_enable,$folder_prefix,
762 $list_special_folders_first,$imap_server_type;
763 $inbox_subscribed = false;
764 $listsubscribed = sqimap_capability($imap_stream,'LIST-SUBSCRIBED');
765
766 if ($show_only_subscribed) { $show_only_subscribed=$show_only_subscribed_folders; }
767
768 require_once(SM_PATH . 'include/load_prefs.php');
769
770 /**
771 * There are three main listing commands we can use in IMAP:
772 * LSUB shows just the list of subscribed folders
773 * may include flags, but these are not necessarily accurate or authoratative
774 * \NoSelect has special meaning: the folder does not exist -OR- it means this
775 * folder is not subscribed but children may be
776 * [RFC-2060]
777 * LIST this shows every mailbox on the system
778 * flags are always included and are accurate and authoratative
779 * \NoSelect means folder should not be selected
780 * [RFC-2060]
781 * LIST (SUBSCRIBED) implemented with LIST-SUBSCRIBED extension
782 * this is like list but returns only subscribed folders
783 * flag meanings are like LIST, not LSUB
784 * \NonExistent means mailbox doesn't exist
785 * \PlaceHolder means parent is not valid (selectable), but one or more children are
786 * \NoSelect indeed means that the folder should not be selected
787 * IMAPEXT-LIST-EXTENSIONS-04 August 2003 B. Leiba
788 */
789 if (!$show_only_subscribed) {
790 $lsub = 'LIST';
791 $sub_cache_name='list_cache';
792 } elseif ($listsubscribed) {
793 $lsub = 'LIST (SUBSCRIBED)';
794 $sub_cache_name='listsub_cache';
795 } else {
796 $lsub = 'LSUB';
797 $sub_cache_name='lsub_cache';
798 }
799
800 // Some IMAP servers allow subfolders to exist even if the parent folders do not
801 // This fixes some problems with the folder list when this is the case, causing the
802 // NoSelect folders to be displayed
803 if ($noselect_fix_enable) {
804 $lsub_args = "$lsub \"$folder_prefix\" \"*%\"";
805 $list_args = "LIST \"$folder_prefix\" \"*%\"";
806 } else {
807 $lsub_args = "$lsub \"$folder_prefix\" \"*\"";
808 $list_args = "LIST \"$folder_prefix\" \"*\"";
809 }
810
811 // get subscribed mailbox list from cache (session)
812 // if not there, then get it from the imap server and store in cache
813 sqsession_is_active();
814
815 if (!$force) {
816 sqgetGlobalVar($sub_cache_name,$lsub_cache,SQ_SESSION);
817 }
818
819 if (!empty($lsub_cache)) {
820 $lsub_assoc_ary=$lsub_cache;
821 } else {
822 $lsub_ary = sqimap_run_command ($imap_stream, $lsub_args, true, $response, $message);
823 $lsub_ary = compact_mailboxes_response($lsub_ary);
824 if (!empty($lsub_ary)) {
825 foreach ($lsub_ary as $rawline) {
826 $temp_mailbox_name=find_mailbox_name($rawline);
827 $lsub_assoc_ary[$temp_mailbox_name]=$rawline;
828 }
829 unset($lsub_ary);
830 sqsession_register($lsub_assoc_ary,$sub_cache_name);
831 }
832 }
833
834 // Now to get the mailbox flags
835 // The LSUB response may return \NoSelect flags, etc. but it is optional
836 // according to RFC3501, and even when returned it may not be accurate
837 // or authoratative. LIST will always return accurate results.
838 if (($lsub == 'LIST') || ($lsub == 'LIST (SUBSCRIBED)')) {
839 // we've already done a LIST or LIST (SUBSCRIBED)
840 // and NOT a LSUB, so no need to do it again
841 $list_assoc_ary = $lsub_assoc_ary;
842 } else {
843 // we did a LSUB so now we need to do a LIST
844 // first see if it is in cache
845 $list_cache_name='list_cache';
846 if (!$force) {
847 sqgetGlobalVar($list_cache_name,$list_cache,SQ_SESSION);
848 }
849
850 if (!empty($list_cache)) {
851 $list_assoc_ary=$list_cache;
852 // we could store this in list_cache_name but not necessary
853 } else {
854 // not in cache so we need to go get it from the imap server
855 $list_assoc_ary = array();
856 $list_ary = sqimap_run_command($imap_stream, $list_args,
857 true, $response, $message);
858 $list_ary = compact_mailboxes_response($list_ary);
859 if (!empty($list_ary)) {
860 foreach ($list_ary as $rawline) {
861 $temp_mailbox_name=find_mailbox_name($rawline);
862 $list_assoc_ary[$temp_mailbox_name]=$rawline;
863 }
864 unset($list_ary);
865 sqsession_register($list_assoc_ary,$list_cache_name);
866 }
867 }
868 }
869
870 // If they aren't subscribed to the inbox, then add it anyway (if its in LIST)
871 $inbox_subscribed=false;
872 if (!empty($lsub_assoc_ary)) {
873 foreach ($lsub_assoc_ary as $temp_mailbox_name=>$rawline) {
874 if (strtoupper($temp_mailbox_name) == 'INBOX') {
875 $inbox_subscribed=true;
876 }
877 }
878 }
879 if (!$inbox_subscribed) {
880 if (!empty($list_assoc_ary)) {
881 foreach ($list_assoc_ary as $temp_mailbox_name=>$rawline) {
882 if (strtoupper($temp_mailbox_name) == 'INBOX') {
883 $lsub_assoc_ary[$temp_mailbox_name]=$rawline;
884 }
885 }
886 }
887 }
888
889 // Now we have the raw output, we need to create an array of mailbox names we will return
890 if (!$show_only_subscribed) {
891 $final_folders_assoc_ary=$list_assoc_ary;
892 } else {
893 /**
894 * only show subscribed folders
895 * we need to merge the folders here... we can't trust the flags, etc. from the lsub_assoc_array
896 * so we use the lsub_assoc_array as the list of folders and the values come from list_assoc_array
897 */
898 if (!empty($lsub_assoc_ary)) {
899 foreach ($lsub_assoc_ary as $temp_mailbox_name=>$rawline) {
900 if (!empty($list_assoc_ary[$temp_mailbox_name])) {
901 $final_folders_assoc_ary[$temp_mailbox_name]=$list_assoc_ary[$temp_mailbox_name];
902 }
903 }
904 }
905 }
906
907
908 // Now produce a flat, sorted list
909 if (!empty($final_folders_assoc_ary)) {
910 uksort($final_folders_assoc_ary,'strnatcasecmp');
911 foreach ($final_folders_assoc_ary as $temp_mailbox_name=>$rawline) {
912 $final_folders_ary[]=$rawline;
913 }
914 }
915
916 // this will put it into an array we can use later
917 // containing:
918 // raw - Raw LIST/LSUB response from the IMAP server
919 // formatted - formatted folder name
920 // unformatted - unformatted, but with the delimiter at the end removed
921 // unformated-dm - folder name as it appears in raw response
922 // unformatted-disp - unformatted without $folder_prefix
923 // id - the array element number (0, 1, 2, etc.)
924 // flags - mailbox flags
925 if (!empty($final_folders_ary)) {
926 $boxesall = sqimap_mailbox_parse($final_folders_ary);
927 } else {
928 // they have no mailboxes
929 $boxesall=array();
930 }
931
932 /* Now, lets sort for special folders */
933 $boxesnew = $used = array();
934
935 /* Find INBOX */
936 $cnt = count($boxesall);
937 $used = array_pad($used,$cnt,false);
938 $has_inbox = false;
939 for($k = 0; $k < $cnt; ++$k) {
940 if (strtoupper($boxesall[$k]['unformatted']) == 'INBOX') {
941 $boxesnew[] = $boxesall[$k];
942 $used[$k] = true;
943 $has_inbox = true;
944 break;
945 }
946 }
947
948 if ($has_inbox == false) {
949 // do a list request for inbox because we should always show
950 // inbox even if the user isn't subscribed to it.
951 $inbox_ary = sqimap_run_command($imap_stream, 'LIST "" "INBOX"',
952 true, $response, $message);
953 $inbox_ary = compact_mailboxes_response($inbox_ary);
954 if (count($inbox_ary)) {
955 $inbox_entry = sqimap_mailbox_parse($inbox_ary);
956 // add it on top of the list
957 if (!empty($boxesnew)) {
958 array_unshift($boxesnew,$inbox_entry[0]);
959 } else {
960 $boxesnew[]=$inbox_entry[0];
961 }
962 /* array_unshift($used,true); */
963 }
964 }
965
966 /* List special folders and their subfolders, if requested. */
967 if ($list_special_folders_first) {
968 for($k = 0; $k < $cnt; ++$k) {
969 if (!$used[$k] && isSpecialMailbox($boxesall[$k]['unformatted'])) {
970 $boxesnew[] = $boxesall[$k];
971 $used[$k] = true;
972 }
973 }
974 }
975
976 /* Find INBOX's children */
977 for($k = 0; $k < $cnt; ++$k) {
978 $isboxbelow=isBoxBelow(strtoupper($boxesall[$k]['unformatted']),'INBOX');
979 if (strtoupper($boxesall[$k]['unformatted']) == 'INBOX') {
980 $is_inbox=1;
981 } else {
982 $is_inbox=0;
983 }
984
985 if (!$used[$k] && $isboxbelow && $is_inbox) {
986 $boxesnew[] = $boxesall[$k];
987 $used[$k] = true;
988 }
989 }
990
991 /* Rest of the folders */
992 for($k = 0; $k < $cnt; $k++) {
993 if (!$used[$k]) {
994 $boxesnew[] = $boxesall[$k];
995 }
996 }
997 /**
998 * Don't register boxes in session, if $session_register is set to false
999 * Prevents registration of sqimap_mailbox_list_all() results.
1000 */
1001 if ($session_register) sqsession_register($boxesnew,'boxesnew');
1002 return $boxesnew;
1003 }
1004
1005 /**
1006 * Fills mailbox object
1007 *
1008 * this is passed the mailbox array by left_main.php
1009 * who has previously obtained it from sqimap_get_mailboxes
1010 * that way, the raw mailbox list is available in left_main to other
1011 * things besides just sqimap_mailbox_tree
1012 * imap_stream is just used now to get status info
1013 *
1014 * most of the functionality is moved to sqimap_get_mailboxes
1015 * also takes care of TODO items:
1016 * caching mailbox tree
1017 * config setting for UW imap section (not needed now)
1018 *
1019 * Some code fragments are present in 1.3.0 - 1.4.4.
1020 * @param stream $imap_stream imap connection resource
1021 * @param array $lsub_ary output array from sqimap_get_mailboxes (contains mailboxes and flags)
1022 * @return object see mailboxes class.
1023 * @since 1.5.0
1024 */
1025 function sqimap_mailbox_tree($imap_stream,$lsub_ary) {
1026
1027 $sorted_lsub_ary = array();
1028 $cnt = count($lsub_ary);
1029 for ($i = 0; $i < $cnt; $i++) {
1030 $mbx=$lsub_ary[$i]['unformatted'];
1031 $flags=$lsub_ary[$i]['flags'];
1032
1033 $noinferiors=0;
1034 if (in_array('\Noinferiors',$flags)) { $noinferiors=1; }
1035 if (in_array('\NoInferiors',$flags)) { $noinferiors=1; }
1036 if (in_array('\HasNoChildren',$flags)) { $noinferiors=1; }
1037
1038 $noselect=0;
1039 if (in_array('\NoSelect',$flags)) { $noselect=1; }
1040 /**
1041 * LIST (SUBSCRIBED) has two new flags, \NonExistent which means the mailbox is subscribed to
1042 * but doesn't exist, and \PlaceHolder which is similar (but not the same) as \NoSelect
1043 * For right now, we'll treat these the same as \NoSelect and this behavior can be changed
1044 * later if needed
1045 */
1046 if (in_array('\NonExistent',$flags)) { $noselect=1; }
1047 if (in_array('\PlaceHolder',$flags)) { $noselect=1; }
1048 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect, 'noinferiors' => $noinferiors);
1049 }
1050
1051 $sorted_lsub_ary = array_values($sorted_lsub_ary);
1052 usort($sorted_lsub_ary, 'mbxSort');
1053 $boxestree = sqimap_fill_mailbox_tree($sorted_lsub_ary,false,$imap_stream);
1054 return $boxestree;
1055 }
1056
1057 /**
1058 * Callback function used for sorting mailboxes in sqimap_mailbox_tree
1059 * @param string $a
1060 * @param string $b
1061 * @return integer see php strnatcasecmp()
1062 * @since 1.5.1
1063 */
1064 function mbxSort($a, $b) {
1065 return strnatcasecmp($a['mbx'], $b['mbx']);
1066 }
1067
1068 /**
1069 * Fills mailbox object
1070 *
1071 * Some code fragments are present in 1.3.0 - 1.4.4.
1072 * @param array $mbx_ary
1073 * @param $mbxs
1074 * @param stream $imap_stream imap connection resource
1075 * @return object see mailboxes class
1076 * @since 1.5.0
1077 */
1078 function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false,$imap_stream) {
1079 global $data_dir, $username, $list_special_folders_first,
1080 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
1081 $move_to_trash, $move_to_sent, $save_as_draft,
1082 $delimiter, $imap_server_type;
1083
1084 // $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
1085
1086 /* create virtual root node */
1087 $mailboxes= new mailboxes();
1088 $mailboxes->is_root = true;
1089 $trail_del = false;
1090 $start = 0;
1091
1092
1093 if (isset($folder_prefix) && ($folder_prefix != '')) {
1094 $start = substr_count($folder_prefix,$delimiter);
1095 if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
1096 $mailboxes->mailboxname_full = substr($folder_prefix,0, (strlen($folder_prefix)-1));
1097 } else {
1098 $mailboxes->mailboxname_full = $folder_prefix;
1099 $start++;
1100 }
1101 $mailboxes->mailboxname_sub = $mailboxes->mailboxname_full;
1102 } else {
1103 $start = 0;
1104 }
1105
1106 $cnt = count($mbx_ary);
1107 for ($i=0; $i < $cnt; $i++) {
1108 if ($mbx_ary[$i]['mbx'] !='' ) {
1109 $mbx = new mailboxes();
1110 $mailbox = $mbx_ary[$i]['mbx'];
1111
1112 /*
1113 * Set the is_special flag if it concerned a special mailbox.
1114 * Used for displaying the special folders on top in the mailbox
1115 * tree displaying code.
1116 */
1117 $mbx->is_special |= ($mbx->is_inbox = (strtoupper($mailbox) == 'INBOX'));
1118 $mbx->is_special |= ($mbx->is_trash = isTrashMailbox($mailbox));
1119 $mbx->is_special |= ($mbx->is_sent = isSentMailbox($mailbox));
1120 $mbx->is_special |= ($mbx->is_draft = isDraftMailbox($mailbox));
1121
1122 if (!$mbx->is_special)
1123 $mbx->is_special = boolean_hook_function('special_mailbox', $mailbox, 1);
1124
1125 if (isset($mbx_ary[$i]['unseen'])) {
1126 $mbx->unseen = $mbx_ary[$i]['unseen'];
1127 }
1128 if (isset($mbx_ary[$i]['nummessages'])) {
1129 $mbx->total = $mbx_ary[$i]['nummessages'];
1130 }
1131
1132 $mbx->is_noselect = $mbx_ary[$i]['noselect'];
1133 $mbx->is_noinferiors = $mbx_ary[$i]['noinferiors'];
1134
1135 $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
1136 if ($r_del_pos) {
1137 $mbx->mailboxname_sub = substr($mbx_ary[$i]['mbx'],$r_del_pos+1);
1138 } else { /* mailbox is root folder */
1139 $mbx->mailboxname_sub = $mbx_ary[$i]['mbx'];
1140 }
1141 $mbx->mailboxname_full = $mbx_ary[$i]['mbx'];
1142
1143 $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);
1144 }
1145 }
1146 sqimap_utf7_decode_mbx_tree($mailboxes);
1147 sqimap_get_status_mbx_tree($imap_stream,$mailboxes);
1148 return $mailboxes;
1149 }
1150
1151 /**
1152 * @param object $mbx_tree
1153 * @since 1.5.0
1154 */
1155 function sqimap_utf7_decode_mbx_tree(&$mbx_tree) {
1156
1157 if (strtoupper($mbx_tree->mailboxname_full) == 'INBOX')
1158 $mbx_tree->mailboxname_sub = _("INBOX");
1159 else
1160 $mbx_tree->mailboxname_sub = imap_utf7_decode_local($mbx_tree->mailboxname_sub);
1161 if ($mbx_tree->mbxs) {
1162 $iCnt = count($mbx_tree->mbxs);
1163 for ($i=0;$i<$iCnt;++$i) {
1164 sqimap_utf7_decode_mbx_tree($mbx_tree->mbxs[$i]);
1165 }
1166 }
1167 }
1168
1169 /**
1170 * @param object $mbx_tree
1171 * @param array $aMbxs
1172 * @since 1.5.0
1173 */
1174 function sqimap_tree_to_ref_array(&$mbx_tree,&$aMbxs) {
1175 if ($mbx_tree)
1176 $aMbxs[] =& $mbx_tree;
1177 if ($mbx_tree->mbxs) {
1178 $iCnt = count($mbx_tree->mbxs);
1179 for ($i=0;$i<$iCnt;++$i) {
1180 sqimap_tree_to_ref_array($mbx_tree->mbxs[$i],$aMbxs);
1181 }
1182 }
1183 }
1184
1185 /**
1186 * @param stream $imap_stream imap connection resource
1187 * @param object $mbx_tree
1188 * @since since 1.5.0
1189 */
1190 function sqimap_get_status_mbx_tree($imap_stream,&$mbx_tree) {
1191 global $unseen_notify, $unseen_type, $trash_folder,$move_to_trash;
1192 $aMbxs = $aQuery = array();
1193 sqimap_tree_to_ref_array($mbx_tree,$aMbxs);
1194 // remove the root node
1195 array_shift($aMbxs);
1196
1197 if($unseen_notify == 3) {
1198 $cnt = count($aMbxs);
1199 for($i=0;$i<$cnt;++$i) {
1200 $oMbx =& $aMbxs[$i];
1201 if (!$oMbx->is_noselect) {
1202 $mbx = $oMbx->mailboxname_full;
1203 if ($unseen_type == 2 ||
1204 ($move_to_trash && $oMbx->mailboxname_full == $trash_folder)) {
1205 $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (MESSAGES UNSEEN)';
1206 } else {
1207 $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (UNSEEN)';
1208 }
1209 sqimap_prepare_pipelined_query($query,$tag,$aQuery,false);
1210 } else {
1211 $oMbx->unseen = $oMbx->total = false;
1212 $tag = false;
1213 }
1214 $oMbx->tag = $tag;
1215 $aMbxs[$i] =& $oMbx;
1216 }
1217 // execute all the queries at once
1218 $aResponse = sqimap_run_pipelined_command ($imap_stream, $aQuery, false, $aServerResponse, $aServerMessage);
1219 $cnt = count($aMbxs);
1220 for($i=0;$i<$cnt;++$i) {
1221 $oMbx =& $aMbxs[$i];
1222 $tag = $oMbx->tag;
1223 if ($tag && $aServerResponse[$tag] == 'OK') {
1224 $sResponse = implode('', $aResponse[$tag]);
1225 if (preg_match('/UNSEEN\s+([0-9]+)/i', $sResponse, $regs)) {
1226 $oMbx->unseen = $regs[1];
1227 }
1228 if (preg_match('/MESSAGES\s+([0-9]+)/i', $sResponse, $regs)) {
1229 $oMbx->total = $regs[1];
1230 }
1231 }
1232 unset($oMbx->tag);
1233 }
1234 } else if ($unseen_notify == 2) { // INBOX only
1235 $cnt = count($aMbxs);
1236 for($i=0;$i<$cnt;++$i) {
1237 $oMbx =& $aMbxs[$i];
1238 if (strtoupper($oMbx->mailboxname_full) == 'INBOX' ||
1239 ($move_to_trash && $oMbx->mailboxname_full == $trash_folder)) {
1240 if ($unseen_type == 2 ||
1241 ($oMbx->mailboxname_full == $trash_folder && $move_to_trash)) {
1242 $aStatus = sqimap_status_messages($imap_stream,$oMbx->mailboxname_full);
1243 $oMbx->unseen = $aStatus['UNSEEN'];
1244 $oMbx->total = $aStatus['MESSAGES'];
1245 } else {
1246 $oMbx->unseen = sqimap_unseen_messages($imap_stream,$oMbx->mailboxname_full);
1247 }
1248 $aMbxs[$i] =& $oMbx;
1249 if (!$move_to_trash && $trash_folder) {
1250 break;
1251 } else {
1252 // trash comes after INBOX
1253 if ($oMbx->mailboxname_full == $trash_folder) {
1254 break;
1255 }
1256 }
1257 }
1258 }
1259 }
1260 }
1261
1262 /**
1263 * Checks if folder is noselect (can't store messages)
1264 *
1265 * Function does not check if folder subscribed.
1266 * @param stream $oImapStream imap connection resource
1267 * @param string $sImapFolder imap folder name
1268 * @param object $oBoxes mailboxes class object.
1269 * @return boolean true, when folder has noselect flag. false in any other case.
1270 * @since 1.5.1
1271 */
1272 function sqimap_mailbox_is_noselect($oImapStream,$sImapFolder,&$oBoxes) {
1273 // build mailbox object if it is not available
1274 if (! is_object($oBoxes)) $oBoxes=sqimap_mailbox_list($oImapStream);
1275 foreach($oBoxes as $box) {
1276 if ($box['unformatted']==$sImapFolder) {
1277 return (bool) check_is_noselect($box['raw']);
1278 }
1279 }
1280 return false;
1281 }
1282
1283 /**
1284 * Checks if folder is noinferiors (can't store other folders)
1285 *
1286 * Function does not check if folder subscribed.
1287 * @param stream $oImapStream imap connection resource
1288 * @param string $sImapFolder imap folder name
1289 * @param object $oBoxes mailboxes class object.
1290 * @return boolean true, when folder has noinferiors flag. false in any other case.
1291 * @since 1.5.1
1292 */
1293 function sqimap_mailbox_is_noinferiors($oImapStream,$sImapFolder,&$oBoxes) {
1294 // build mailbox object if it is not available
1295 if (! is_object($oBoxes)) $oBoxes=sqimap_mailbox_list($oImapStream);
1296 foreach($oBoxes as $box) {
1297 if ($box['unformatted']==$sImapFolder) {
1298 return (bool) check_is_noinferiors($box['raw']);
1299 }
1300 }
1301 return false;
1302 }
1303
1304 ?>