+
+function sqimap_tree_to_ref_array(&$mbx_tree,&$aMbxs) {
+ if ($mbx_tree)
+ $aMbxs[] =& $mbx_tree;
+ if ($mbx_tree->mbxs) {
+ $iCnt = count($mbx_tree->mbxs);
+ for ($i=0;$i<$iCnt;++$i) {
+ sqimap_tree_to_ref_array($mbx_tree->mbxs[$i],$aMbxs);
+ }
+ }
+}
+
+
+/* Define preferences for folder settings. */
+/* FIXME, we should load constants.php
+unseen_notify
+define('SMPREF_UNSEEN_NONE', 1);
+define('SMPREF_UNSEEN_INBOX', 2);
+define('SMPREF_UNSEEN_ALL', 3);
+
+define('SMPREF_UNSEEN_SPECIAL', 4); // Only special folders
+define('SMPREF_UNSEEN_NORMAL', 5); // Only normal folders
+
+unseen_type
+define('SMPREF_UNSEEN_ONLY', 1);
+define('SMPREF_UNSEEN_TOTAL', 2);
+*/
+
+function sqimap_get_status_mbx_tree($imap_stream,&$mbx_tree) {
+ global $unseen_notify, $unseen_type, $trash_folder,$move_to_trash;
+ $aMbxs = $aQuery = $aTag = array();
+ sqimap_tree_to_ref_array($mbx_tree,$aMbxs);
+ // remove the root node
+ array_shift($aMbxs);
+
+ if($unseen_notify == 3) {
+ $cnt = count($aMbxs);
+ for($i=0;$i<$cnt;++$i) {
+ $oMbx =& $aMbxs[$i];
+ if (!$oMbx->is_noselect) {
+ $mbx = $oMbx->mailboxname_full;
+ if ($unseen_type == 2 ||
+ ($move_to_trash && $oMbx->mailboxname_full == $trash_folder)) {
+ $query = "STATUS \"$mbx\" (MESSAGES UNSEEN)";
+ } else {
+ $query = "STATUS \"$mbx\" (UNSEEN)";
+ }
+ sqimap_prepare_pipelined_query($query,$tag,$aQuery,false);
+ } else {
+ $oMbx->unseen = $oMbx->total = false;
+ $tag = false;
+ }
+ $oMbx->tag = $tag;
+ $aMbxs[$i] =& $oMbx;
+ }
+ // execute all the queries at once
+ $aResponse = sqimap_run_pipelined_command ($imap_stream, $aQuery, false, $aServerResponse, $aServerMessage);
+ $cnt = count($aMbxs);
+ for($i=0;$i<$cnt;++$i) {
+ $oMbx =& $aMbxs[$i];
+ $tag = $oMbx->tag;
+ if ($tag && $aServerResponse[$tag] == 'OK') {
+ $sResponse = implode('', $aResponse[$tag]);
+ if (preg_match('/UNSEEN\s+([0-9]+)/i', $sResponse, $regs)) {
+ $oMbx->unseen = $regs[1];
+ }
+ if (preg_match('/MESSAGES\s+([0-9]+)/i', $sResponse, $regs)) {
+ $oMbx->total = $regs[1];
+ }
+ }
+ unset($oMbx->tag);
+ }
+ } else if ($unseen_notify == 2) { // INBOX only
+ $cnt = count($aMbxs);
+ for($i=0;$i<$cnt;++$i) {
+ $oMbx =& $aMbxs[$i];
+ if (strtoupper($oMbx->mailboxname_full) == 'INBOX' ||
+ ($move_to_trash && $oMbx->mailboxname_full == $trash_folder)) {
+ if ($unseen_type == 2 ||
+ ($oMbx->mailboxname_full == $trash_folder && $move_to_trash)) {
+ $aStatus = sqimap_status_messages($imap_stream,$oMbx->mailboxname_full);
+ $oMbx->unseen = $aStatus['UNSEEN'];
+ $oMbx->total = $aStatus['MESSAGES'];
+ } else {
+ $oMbx->unseen = sqimap_unseen_messages($imap_stream,$oMbx->mailboxname_full);
+ }
+ $aMbxs[$i] =& $oMbx;
+ if (!$move_to_trash && $trash_folder) {
+ break;
+ } else {
+ // trash comes after INBOX
+ if ($oMbx->mailboxname_full == $trash_folder) {
+ break;
+ }
+ }
+ }
+ }
+ }
+}
+