Browse Source

deleting folders is now recursive and should work more reliably

nehresma 25 years ago
parent
commit
a5fdbfdfcf
4 changed files with 49 additions and 38 deletions
  1. 11 0
      functions/array.php
  2. 26 24
      functions/imap.php
  3. 10 10
      src/empty_trash.php
  4. 2 4
      src/folders_delete.php

+ 11 - 0
functions/array.php

@@ -38,4 +38,15 @@
          }
          return $r;
       }
+
+   function removeElement($array, $element) {
+      $j = 0;
+      for ($i = 0;$i < count($array);$i++)
+         if ($i != $element) {
+            $newArray[$j] = $array[$i];
+            $j++;
+         }
+
+      return $newArray;
+   }
 ?>

+ 26 - 24
functions/imap.php

@@ -56,30 +56,17 @@
       return $box;
    }
 
-   /** Finds the delimeter between mailboxes **/
+   /** 
+      Finds the delimeter between mailboxes.  This should now be more compliant across
+         different server types that vary in their RFC2060 compliance.
+   **/
    function findMailboxDelimeter($imapConnection) {
-      fputs($imapConnection, ". list \"\" \"\"\n");
-      $read = fgets($imapConnection, 1024);
-
-      if (strrpos($read, "\"") == strlen($read)) {
-         $pos = strrpos($read, "\"");
-         $read = substr($read, 0, $pos);
-
-         $pos = strrpos($read, "\"");
-         $read = substr($read, 0, $pos);
-      } else {
-         $pos = strrpos($read, " ");
-         $read = substr($read, 0, $pos);
-      }
-   
-      $pos = strrpos($read, "\"");
-      $read = substr($read, 0, $pos);
+      fputs($imapConnection, ". list \"\" *\n");
+      $read = imapReadData($imapConnection, ".", true, $a, $b);
+      $quotePosition = strpos($read[0], "\"");
+      $delim = substr($read[0], $quotePosition+1, 1);
 
-      $pos = strrpos($read, "\"");
-      $read = substr($read, $pos+1, strlen($read));
-
-      $tmp = fgets($imapConnection, 1024);
-      return $read;
+      return $delim;
    }
 
    function getMailboxFlags($imapConnection, $mailbox) {
@@ -173,10 +160,25 @@
       $data = imapReadData($imapConnection, "1", true, $response, $message);
    }
 
-   function removeFolder($imapConnection, $folder) {
+   /**
+      This is a recursive function that checks to see if the folder has any subfolders, 
+         and if so, it calls removeFolder on the subfolders first, then removes the parent
+         folder.
+   **/
+   function removeFolder($imapConnection, $folder, $delimiter) {
+      global $boxes;
+      
+      // bug if there are 2 subfolders of a folder, it won't get to the second one
+      for ($i = 0; $i < count($boxes); $i++) {
+         if (strstr($boxes[$i]["UNFORMATTED"], $folder . $delimiter)) {
+            $newDelete = $boxes[$i]["UNFORMATTED"];
+            $boxes = removeElement($boxes, $i);
+            removeFolder($imapConnection, $newDelete, $boxes, $delimiter);
+         }
+      }
+      
       fputs ($imapConnection, "1 unsubscribe \"$folder\"\n");
       $data = imapReadData($imapConnection, "1", true, $response, $message);
-      echo $data[0] . "<BR>";
       fputs($imapConnection, "1 delete \"$folder\"\n");
       $data = imapReadData($imapConnection, "1", false, $response, $message);
       if ($response == "NO") {

+ 10 - 10
src/empty_trash.php

@@ -17,8 +17,8 @@
    fputs($imapConnection, "1 LIST \"$mailbox\" *\n");
    $data = imapReadData($imapConnection , "1", false, $response, $message);
       
-   echo "DEBUG - data from IMAP \"LIST\" : " . $data[0] . "<BR>\n"; 
-
+   $dm = findMailboxDelimeter($imapConnection);
+   
    // According to RFC2060, a DELETE command should NOT remove inferiors (sub folders)
    //    so lets go through the list of subfolders and remove them before removing the
    //    parent.
@@ -26,16 +26,16 @@
    //       and work up.
    
 
-   for ($i = 0; $i < count($boxes); $i++) {
+//   for ($i = 0; $i < count($boxes); $i++) {
 //      if (($boxes[$i]["UNFORMATTED"] == $mailbox) ||
 //          (substr($boxes[$i]["UNFORMATTED"], 0, strlen($mailbox . $dm)) == $mailbox . $dm)) {
-      if (($boxes[$i]["UNFORMATTED"] != $mailbox) && (substr($boxes[$i]["UNFORMATTED"], 0, strlen($mailbox . $dm)) == $mailbox . $dm)) {
-         removeFolder($imapConnection, $boxes[$i]["UNFORMATTED"]);
-         echo "removing " . $boxes[$i]["UNFORMATTED"] . "<BR>";
-      }
-   }
-   // now lets remove the top level trash folder
-   removeFolder($imapConnection, $mailbox);
+//      if (($boxes[$i]["UNFORMATTED"] != $mailbox) && (substr($boxes[$i]["UNFORMATTED"], 0, strlen($mailbox . $dm)) == $mailbox . $dm)) {
+//         removeFolder($imapConnection, $boxes[$i]["UNFORMATTED"], $dm);
+//      }
+//   }
+
+   // lets remove the trash folder
+   removeFolder($imapConnection, $mailbox, $dm);
 
    createFolder($imapConnection, "$trash_folder", "");
 

+ 2 - 4
src/folders_delete.php

@@ -55,7 +55,7 @@
                $success = true;
 
             if ($success == true)
-               removeFolder($imapConnection, $boxes[$i]["UNFORMATTED"]);
+               removeFolder($imapConnection, $boxes[$i]["UNFORMATTED"], $dm);
          }
       }
    } else { /** if they do NOT wish to move messages to the trash (or cannot)**/
@@ -65,7 +65,7 @@
          for ($i = 0; $i < count($boxes); $i++) {
             if (($boxes[$i]["UNFORMATTED"] == $mailbox) ||
                 (substr($boxes[$i]["UNFORMATTED"], 0, strlen($mailbox . $dm)) == $mailbox . $dm)) {
-               removeFolder($imapConnection, $boxes[$i]["UNFORMATTED"]);
+               removeFolder($imapConnection, $boxes[$i]["UNFORMATTED"], $dm);
             }
          }
          fputs($imapConnection, "1 LIST \"$mailbox\" *\n");
@@ -89,5 +89,3 @@
    
    echo "</BODY></HTML>";
 ?>
-
-