fixed double folder problem
[squirrelmail.git] / functions / imap_mailbox.php
index 4cf6e3a69b54d22805165f8fa8fdab9954ff5968..26d05d109c9bd9c068e3a3e5340782c3bd1ed5b1 100755 (executable)
@@ -12,6 +12,7 @@
       sqimap_mailbox_select ($imap_stream, $mailbox);
       fputs ($imap_stream, "a001 EXPUNGE\r\n");
       $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
+      sqimap_mailbox_close ($imap_stream);
    }
 
 
     **  Checks whether or not the specified mailbox exists 
     ******************************************************************************/
    function sqimap_mailbox_exists ($imap_stream, $mailbox) {
-      $boxes = sqimap_mailbox_list ($imap_stream);
-      $found = false;
-      for ($i = 0; $i < count ($boxes); $i++) {
-         if ($boxes[$i]["unformatted"] == $mailbox)
-            $found = true;
+      fputs ($imap_stream, "a001 LIST \"\" \"$mailbox\"\r\n");
+      $mbx = sqimap_read_data($imap_stream, "a001", true, $response, $message);
+      if ($mailbox) {
+         if (ereg ("$mailbox", $mbx[0])) {
+            return true;
+         } else {
+            return false;
+         }
       }
-      return $found;
    }
 
+   /******************************************************************************
+    **  Closes an open mailbox 
+    ******************************************************************************/
+   function sqimap_mailbox_close ($imap_stream) {
+      fputs ($imap_stream, "a001 CLOSE\r\n");
+      $tmp = sqimap_read_data($imap_stream, "a001", false, $response, $message);
+   }
 
-   
    /******************************************************************************
     **  Selects a mailbox
     ******************************************************************************/
-   function sqimap_mailbox_select ($imap_stream, $mailbox, $hide) {
+   function sqimap_mailbox_select ($imap_stream, $mailbox, $hide=true, $recent=false) {
       fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n");
        $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
+      if ($recent) {
+         for ($i=0; $i<count($read); $i++) {
+            if (strpos(strtolower($read[$i]), "recent")) {
+               $r = explode(" ", $read[$i]);
+            }
+         }
+         return $r[1];
+      }
    }
 
    
             $dm_count--;
             
          for ($j = 0; $j < $dm_count - (countCharInString($folder_prefix, $dm)); $j++)
-            $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . "  ";
+            $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . "&nbsp;&nbsp;";
          $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
             
          $boxes[$g]["unformatted-dm"] = $mailbox;
       $inbox_subscribed = false;
       fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n");
       $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
+   
       for ($i=0;$i < count($lsub_ary); $i++) {
          $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
          if ($sorted_lsub_ary[$i] == "INBOX")
             $inbox_subscribed = true;
       }
+      $new_ary = array();
+      for ($i=0; $i < count($sorted_lsub_ary); $i++) {
+         if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
+            $new_ary[] = $sorted_lsub_ary[$i];
+         }
+      }
+      $sorted_lsub_ary = $new_ary;
       if (isset($sorted_lsub_ary)) {
          sort($sorted_lsub_ary);
       }   
       if ($inbox_subscribed == false || $inbox_in_list == false) {
          fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
          $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
-         $sorted_list_ary[count($sorted_list_ary)] = $inbox_ary[0];
+
+         $pos = count($sorted_list_ary);
+         $sorted_list_ary[$pos] = $inbox_ary[0];
+
+         $pos = count($sorted_lsub_ary);
+         $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]);
       }
 
                $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
          }
          $g++;
       }
-      $boxes = ary_sort ($boxes, "unformatted", 1);
+      if ($boxes) {
+         $boxes = ary_sort ($boxes, "unformatted", 1);
+      }
       return $boxes;
    }