Sfoglia il codice sorgente

Fixed how folder names are formatted in a hierarchy of folders.

pallo 24 anni fa
parent
commit
653b606b95

+ 89 - 38
functions/imap_mailbox.php

@@ -111,20 +111,42 @@
 
    /******************************************************************************
     **  Formats a mailbox into 4 parts for the $boxes array
+    **
+    **  The four parts are:
+    **
+    **    raw            - Raw LIST/LSUB response from the IMAP server
+    **    formatted      - nicely formatted folder name
+    **    unformatted    - unformatted, but with delimiter at end removed
+    **    unformatted-dm - folder name as it appears in raw response
+    **
     ******************************************************************************/
    function sqimap_mailbox_parse ($line, $line_lsub, $dm) {
-		global $folder_prefix;
+      global $folder_prefix;
+     
+      // Process each folder line
       for ($g=0; $g < count($line); $g++) {
+
+	 // Store the raw IMAP reply
          $boxes[$g]["raw"] = $line[$g];
-            
+
+	 // Count number of delimiters ($dm) in folder name
          $mailbox = trim($line_lsub[$g]);
          $dm_count = countCharInString($mailbox, $dm);
          if (substr($mailbox, -1) == $dm)
-            $dm_count--;
-            
-         for ($j = 0; $j < $dm_count - (countCharInString($folder_prefix, $dm)); $j++)
-            $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . "&nbsp;&nbsp;";
-         $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
+            $dm_count--;  // If name ends in delimiter - decrement count by one
+
+	 // Format folder name, but only if it's a INBOX.* or have
+	 // a parent.
+	 $boxesbyname[$mailbox] = $g;
+	 $parentfolder = readMailboxParent($mailbox, $dm);
+	 if((eregi("^inbox".quotemeta($dm), $mailbox)) || 
+	    ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
+	    $indent = $dm_count - (countCharInString($folder_prefix, $dm));
+	    $boxes[$g]["formatted"]  = str_repeat("&nbsp;&nbsp;", $indent);
+	    $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
+	 } else {
+	    $boxes[$g]["formatted"]  = $mailbox;
+	 }
             
          $boxes[$g]["unformatted-dm"] = $mailbox;
          if (substr($mailbox, -1) == $dm)
@@ -138,22 +160,24 @@
             $boxes[$g]["flags"] = explode(" ", $flags);
          }
       }
+
       return $boxes;
    }
 
-	/* patch from dave_michmerhuizen@yahoo.com
-	 * allows case insensativity when sorting folders
-	 */
-	function _icmp ($a, $b) {
-		return strcasecmp($a, $b);
-	}
+   /* patch from dave_michmerhuizen@yahoo.com
+    * allows case insensativity when sorting folders
+    */
+   function _icmp ($a, $b) {
+      return strcasecmp($a, $b);
+   }
    
    /******************************************************************************
     **  Returns sorted mailbox lists in several different ways.
-    **  The array returned looks like this:
+    **  See comment on sqimap_mailbox_parse() for info about the returned array.
     ******************************************************************************/
    function sqimap_mailbox_list ($imap_stream) {
-      global $load_prefs_php, $prefs_php, $config_php, $data_dir, $username, $list_special_folders_first;
+      global $load_prefs_php, $prefs_php, $config_php;
+      global $data_dir, $username, $list_special_folders_first;
       global $trash_folder, $sent_folder;
       global $move_to_trash, $move_to_sent;
 
@@ -184,7 +208,7 @@
       }
       $sorted_lsub_ary = $new_ary;
       if (isset($sorted_lsub_ary)) {
-			usort($sorted_lsub_ary, "_icmp");
+	 usort($sorted_lsub_ary, "_icmp");
          //sort($sorted_lsub_ary);
       }   
 
@@ -216,40 +240,54 @@
 
       $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
 
+
       /** Now, lets sort for special folders **/
+
+      $boxesnew = Array();
+
+      // Find INBOX
       for ($i = 0; $i < count($boxes); $i++) {
          if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
-            $boxesnew[0] = $boxes[$i];
+            $boxesnew[] = $boxes[$i];
             $boxes[$i]["used"] = true;
-				$i = count($boxes);
+	    $i = count($boxes);
          }
       }
 
       if ($list_special_folders_first == true) {
-         for ($i = count($boxes)-1; $i >= 0 ; $i--) {
-				if (($boxes[$i]["unformatted"] == $trash_folder) && ($move_to_trash)) {	
-               $pos = count($boxesnew);
-               $boxesnew[$pos] = $boxes[$i];
+
+	 // Then list special folders and their subfolders
+	 for ($i = 0 ; $i <= count($boxes) ; $i++) {
+	    if((eregi("^".$trash_folder.'$', $boxes[$i]["unformatted"]) ||
+		eregi("^".$trash_folder.quotemeta($dm), $boxes[$i]["unformatted"]) )  &&
+	       ($move_to_trash)) {	
+               $boxesnew[] = $boxes[$i];
                $boxes[$i]["used"] = true;
-					$trash_found = true;
             }
-				else if (($boxes[$i]["unformatted"] == $sent_folder) && ($move_to_sent)) {	
-               $pos = count($boxesnew);
-               $boxesnew[$pos] = $boxes[$i];
+	    else if((eregi("^".$sent_folder.'$', $boxes[$i]["unformatted"]) ||
+		     eregi("^".$sent_folder.quotemeta($dm), $boxes[$i]["unformatted"]) )  &&
+		    ($move_to_sent)) {	
+               $boxesnew[] = $boxes[$i];
                $boxes[$i]["used"] = true;
-					$sent_found = true;
             }
-
-				if (($sent_found && $trash_found) || ($sent_found && !$move_to_trash) || ($trash_found && !$move_to_sent) || (!$move_to_sent && !$move_to_trash))
-					$i = -1;
          }
+
+	 // Put INBOX.* folders ahead of the rest
+	 for ($i = 0; $i <= count($boxes); $i++) {
+	    if (eregi("^inbox\.", $boxes[$i]["unformatted"]) &&
+		($boxes[$i]["used"] == false)) {
+	       $boxesnew[] = $boxes[$i];
+	       $boxes[$i]["used"] = true;
+	    }
+	 }
+
       }
 
+      // Rest of the folders
       for ($i = 0; $i < count($boxes); $i++) {
          if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
              ($boxes[$i]["used"] == false))  {
-            $pos = count($boxesnew);
-            $boxesnew[$pos] = $boxes[$i];
+            $boxesnew[] = $boxes[$i];
             $boxes[$i]["used"] = true;
          }
       }
@@ -272,18 +310,30 @@
       $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
       $g = 0;
       $phase = "inbox"; 
+
       for ($i = 0; $i < count($read_ary); $i++) {
          if (substr ($read_ary[$i], 0, 4) != "a001") {
+
+	    // Store the raw IMAP reply
             $boxes[$g]["raw"] = $read_ary[$i];
 
+	    // Count number of delimiters ($dm) in folder name
             $mailbox = find_mailbox_name($read_ary[$i]);
             $dm_count = countCharInString($mailbox, $dm);
             if (substr($mailbox, -1) == $dm)
-               $dm_count--;
-               
-            for ($j = 0; $j < $dm_count; $j++)
-               $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . "  ";
-            $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
+               $dm_count--;  // If name ends in delimiter - decrement count by one
+	    
+	    // Format folder name, but only if it's a INBOX.* or have
+	    // a parent.
+	    $boxesbyname[$mailbox] = $g;
+	    $parentfolder = readMailboxParent($mailbox, $dm);
+	    if((eregi("^inbox".quotemeta($dm), $mailbox)) || 
+	       ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
+	       $boxes[$g]["formatted"]  = str_repeat("&nbsp;&nbsp;", $dm_count);
+	       $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
+	    } else {
+	       $boxes[$g]["formatted"]  = $mailbox;
+	    }
                
             $boxes[$g]["unformatted-dm"] = $mailbox;
             if (substr($mailbox, -1) == $dm)
@@ -305,9 +355,10 @@
          }
          $g++;
       }
-      if ($boxes) {
+      if(is_array($boxes)) {
          $boxes = ary_sort ($boxes, "unformatted", 1);
       }
+
       return $boxes;
    }
    

+ 10 - 0
functions/strings.php

@@ -20,6 +20,16 @@
       return $regs[1];
    }
 
+   //*************************************************************************
+   // Read from the back of $haystack until $needle is found, or the begining
+   //    of the $haystack is reached.  $needle is a single character
+   //*************************************************************************
+   function readMailboxParent($haystack, $needle) {
+      if ($needle == ".") $needle = "\.";
+      ereg("^(.+)$needle([^$needle]+)$needle?$", $haystack, $regs);
+      return $regs[1];
+   }
+
    // Searches for the next position in a string minus white space
    function next_pos_minus_white ($haystack, $pos) {
       while (substr($haystack, $pos, 1) == " " ||

+ 49 - 48
src/folders.php

@@ -70,7 +70,8 @@
 
    //display form option for creating Sent and Trash folder
    if ($imap_server_type == "cyrus" && ($sent_folder != "none" || $trash_folder != "none")) {
-      if ((!sqimap_mailbox_exists ($imapConnection, $sent_folder)) || (!sqimap_mailbox_exists ($imapConnection, $trash_folder))) {
+      if ((!sqimap_mailbox_exists ($imapConnection, $sent_folder)) || 
+	  (!sqimap_mailbox_exists ($imapConnection, $trash_folder))) {
          echo "<TABLE WIDTH=70% COLS=1 ALIGN=CENTER cellpadding=2 cellspacing=0 border=0>\n";
          echo "<TR><TD BGCOLOR=\"$color[9]\" ALIGN=CENTER><B>";
          echo _("Special Folder Options");
@@ -107,9 +108,9 @@
       if (strtolower($boxes[$p]["unformatted"]) == "inbox")
          $count_special_folders++;
       else if (strtolower($imap_server_type) == "courier" &&
-               strtolower($boxes[$p]["unformatted"]) == "inbox.trash")                                                               
+               strtolower($boxes[$p]["unformatted"]) == "inbox.trash")
          $count_special_folders++;
-      else if ($boxes[$p]["unformatted"] == $trash_folder && $trash_folder)                                                                  
+      else if ($boxes[$p]["unformatted"] == $trash_folder && $trash_folder)
          $count_special_folders++;
       else if ($boxes[$p]["unformatted"] == $sent_folder && $sent_folder)
          $count_special_folders++;
@@ -120,16 +121,16 @@
       echo "<TT><SELECT NAME=mailbox>\n";
       for ($i = 0; $i < count($boxes); $i++) {
          $use_folder = true;
-        if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&                                                                             
-            ($boxes[$i]["unformatted"] != $trash_folder) && 
-            ($boxes[$i]["unformatted"] != $sent_folder) &&
-            (strtolower($imap_server_type) != "courier" ||
-             strtolower($boxes[$i]["unformatted"]) != "inbox.trash"))                                                              
-          {
-            $box = $boxes[$i]["unformatted-dm"];
-            $box2 = replace_spaces($boxes[$i]["formatted"]);
-            echo "         <OPTION VALUE=\"$box\">$box2\n";
-         }
+	 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
+	     ($boxes[$i]["unformatted"] != $trash_folder) && 
+	     ($boxes[$i]["unformatted"] != $sent_folder) &&
+	     (strtolower($imap_server_type) != "courier" ||
+	      strtolower($boxes[$i]["unformatted"]) != "inbox.trash"))
+	    {
+	       $box = $boxes[$i]["unformatted-dm"];
+	       $box2 = replace_spaces($boxes[$i]["formatted"]);
+	       echo "         <OPTION VALUE=\"$box\">$box2\n";
+	    }
       }
       echo "</SELECT></TT>\n";
       echo "<INPUT TYPE=SUBMIT VALUE=\"";
@@ -217,15 +218,15 @@
       for ($i = 0; $i < count($boxes); $i++) {
          $use_folder = true;
 
-			if ((strtolower($boxes[$i]["unformatted"]) != "inbox") && 
-			    ($boxes[$i]["unformatted"] != $trash_folder)  &&
-			    ($boxes[$i]["unformatted"] != $sent_folder)) 
-			{	
-            $box = $boxes[$i]["unformatted-dm"];
-            $box2 = replace_spaces($boxes[$i]["formatted"]);
-            if (strtolower($imap_server_type) != "courier" || strtolower($box) != "inbox.trash")
-               echo "<OPTION VALUE=\"$box\">$box2\n";
-         }
+	 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") && 
+	     ($boxes[$i]["unformatted"] != $trash_folder)  &&
+	     ($boxes[$i]["unformatted"] != $sent_folder)) 
+	    {	
+	       $box = $boxes[$i]["unformatted-dm"];
+	       $box2 = replace_spaces($boxes[$i]["formatted"]);
+	       if (strtolower($imap_server_type) != "courier" || strtolower($box) != "inbox.trash")
+		  echo "<OPTION VALUE=\"$box\">$box2\n";
+	    }
       }
       echo "</SELECT></TT>\n";
       echo "<INPUT TYPE=SUBMIT VALUE=\"";
@@ -250,14 +251,14 @@
       echo "<TT><SELECT NAME=mailbox[] multiple size=8>\n";
       for ($i = 0; $i < count($boxes); $i++) {
          $use_folder = true;
-			if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
-			    ($boxes[$i]["unformatted"] != $trash_folder) &&
-			    ($boxes[$i]["unformatted"] != $sent_folder)) 
-			{	
-            $box = $boxes[$i]["unformatted-dm"];
-            $box2 = replace_spaces($boxes[$i]["formatted"]);
-            echo "         <OPTION VALUE=\"$box\">$box2\n";
-         }
+	 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
+	     ($boxes[$i]["unformatted"] != $trash_folder) &&
+	     ($boxes[$i]["unformatted"] != $sent_folder)) 
+	    {	
+	       $box = $boxes[$i]["unformatted-dm"];
+	       $box2 = replace_spaces($boxes[$i]["formatted"]);
+	       echo "         <OPTION VALUE=\"$box\">$box2\n";
+	    }
       }
       echo "</SELECT></TT><br>\n";
       echo "<INPUT TYPE=SUBMIT VALUE=\"";
@@ -276,25 +277,25 @@
    $imap_stream = sqimap_login ($username, $key, $imapServerAddress, $imapPort, 1);
    $boxes_all = sqimap_mailbox_list_all ($imap_stream);
 
-      $box = "";
-      $box2 = "";
-      for ($i = 0, $q = 0; $i < count($boxes_all); $i++) {
-         $use_folder = true;
-			for ($p = 0; $p < count ($boxes); $p++) {
-				if ($boxes_all[$i]["unformatted"] == $boxes[$p]["unformatted"]) {
-					$use_folder = false;
-					continue;
-				} else if ($boxes_all[$i]["unformatted-dm"] == $folder_prefix) {
-					$use_folder = false;
-				}
-			}
-			if ($use_folder == true) {	
-            $box[$q] = $boxes_all[$i]["unformatted-dm"];
-            $box2[$q] = $boxes_all[$i]["formatted"];
-            $q++;
-         }
+   $box = "";
+   $box2 = "";
+   for ($i = 0, $q = 0; $i < count($boxes_all); $i++) {
+      $use_folder = true;
+      for ($p = 0; $p < count ($boxes); $p++) {
+	 if ($boxes_all[$i]["unformatted"] == $boxes[$p]["unformatted"]) {
+	    $use_folder = false;
+	    continue;
+	 } else if ($boxes_all[$i]["unformatted-dm"] == $folder_prefix) {
+	    $use_folder = false;
+	 }
       }
-      sqimap_logout($imap_stream);
+      if ($use_folder == true) {	
+	 $box[$q] = $boxes_all[$i]["unformatted-dm"];
+	 $box2[$q] = $boxes_all[$i]["unformatted"];
+	 $q++;
+      }
+   }
+   sqimap_logout($imap_stream);
 
    if ($box && $box2) {
       echo "<FORM ACTION=\"folders_subscribe.php?method=sub\" METHOD=\"POST\">\n";

+ 8 - 1
src/folders_rename_do.php

@@ -22,6 +22,13 @@
 
    include("../src/load_prefs.php");
 
+
+   if($old_name == $new_name) {
+      $location = get_location();
+      header ("Location: $location/folders.php");
+      exit;
+   }
+
    $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
    $dm = sqimap_get_delimiter($imapConnection);
 
@@ -56,7 +63,7 @@
    {
       $name = find_mailbox_name($data[$i]);
 
-      if ($name != $newone) # don't try to resubscribe when renaming ab to abc
+      if ($name != $newone) // don't try to resubscribe when renaming ab to abc
       {
         sqimap_unsubscribe($imapConnection, $name);
         $name = substr($name, strlen($orig));

+ 4 - 5
src/folders_rename_getname.php

@@ -48,13 +48,12 @@
    echo "<TR><TD BGCOLOR=\"$color[4]\" ALIGN=CENTER>";
    echo "<FORM ACTION=\"folders_rename_do.php\" METHOD=\"POST\">\n";
    echo _("New name:");
-   echo " &nbsp;&nbsp;<INPUT TYPE=TEXT SIZE=25 NAME=new_name VALUE=\"$old_name\"><BR>\n";
+   echo "<br><B>$old_parent . </B><INPUT TYPE=TEXT SIZE=25 NAME=new_name VALUE=\"$old_name\"><BR>\n";
    if ($isfolder)
       echo "<INPUT TYPE=HIDDEN NAME=isfolder VALUE=\"true\">";
-   echo "<INPUT TYPE=HIDDEN NAME=orig VALUE=\"$old\">";
-   echo "<INPUT TYPE=SUBMIT VALUE=\"";
-   echo _("Submit");
-   echo "\">\n";
+   printf("<INPUT TYPE=HIDDEN NAME=orig VALUE=\"%s\">\n", $old);
+   printf("<INPUT TYPE=HIDDEN NAME=old_name VALUE=\"%s\">\n", $old_name);
+   echo "<INPUT TYPE=SUBMIT VALUE=\""._("Submit")."\">\n";
    echo "</FORM><BR></TD></TR>";
    echo "</TABLE>";
 

+ 15 - 15
src/left_main.php

@@ -45,14 +45,14 @@
    displayHtmlHeader();
 
    function formatMailboxName($imapConnection, $mailbox, $real_box, $delimeter, $unseen) {
-		global $folder_prefix, $trash_folder, $sent_folder;
-		global $color, $move_to_sent, $move_to_trash;
+      global $folder_prefix, $trash_folder, $sent_folder;
+      global $color, $move_to_sent, $move_to_trash;
       global $unseen_notify, $unseen_type;
-
+      
       $mailboxURL = urlencode($real_box);
-
+      
       if ($unseen_notify == 2 && $real_box == "INBOX") {
-	      $unseen = sqimap_unseen_messages($imapConnection, $numUnseen, $real_box);
+	 $unseen = sqimap_unseen_messages($imapConnection, $numUnseen, $real_box);
          if ($unseen_type == 1 && $unseen > 0) {
             $unseen_string = "($unseen)";
             $unseen_found = true;
@@ -62,7 +62,7 @@
             $unseen_found = true;
          }
       } else if ($unseen_notify == 3) {
-	      $unseen = sqimap_unseen_messages($imapConnection, $numUnseen, $real_box);
+	 $unseen = sqimap_unseen_messages($imapConnection, $numUnseen, $real_box);
          if ($unseen_type == 1 && $unseen > 0) {
             $unseen_string = "($unseen)";
             $unseen_found = true;
@@ -72,17 +72,17 @@
             $unseen_found = true;
          }
       }
-
+      
       $line .= "<NOBR>";
       if ($unseen > 0)
          $line .= "<B>";
 
       $special_color = false;
-		if ((strtolower($real_box) == "inbox") ||
-		    (($real_box == $trash_folder) && ($move_to_trash)) ||
-			 (($real_box == $sent_folder) && ($move_to_sent)))
-			$special_color = true;
-
+      if ((strtolower($real_box) == "inbox") ||
+	  (($real_box == $trash_folder) && ($move_to_trash)) ||
+	  (($real_box == $sent_folder) && ($move_to_sent)))
+	 $special_color = true;
+      
       if ($special_color == true) {
          $line .= "<a href=\"right_main.php?sort=0&startMessage=1&mailbox=$mailboxURL\" target=\"right\" style=\"text-decoration:none\"><FONT COLOR=\"$color[11]\">";
          $line .= replace_spaces($mailbox);
@@ -95,7 +95,7 @@
 
       if ($unseen > 0)
          $line .= "</B>";
-
+      
       if ($unseen_found) {
          $line .= "&nbsp;<small>$unseen_string</small>";
       }
@@ -133,7 +133,7 @@
    for ($i = 0;$i < count($boxes); $i++) {
       $line = "";
       $mailbox = $boxes[$i]["formatted"];
-
+      
       if ($boxes[$i]["flags"]) {
          $noselect = false;
          for ($h = 0; $h < count($boxes[$i]["flags"]); $h++) {
@@ -142,7 +142,7 @@
          }
          if ($noselect == true) {
             $line .= "<FONT COLOR=\"$color[10]\">";
-            $line .= replace_spaces(readShortMailboxName($mailbox, $delimeter));
+            $line .= replace_spaces($mailbox);
             $line .= "</FONT>";
          } else {
             $line .= formatMailboxName($imapConnection, $mailbox, $boxes[$i]["unformatted"], $delimeter, $boxes[$i]["unseen"]);