Index: /trunk/jabberit_messenger/inc/class.uienabledgroups.inc.php
===================================================================
--- /trunk/jabberit_messenger/inc/class.uienabledgroups.inc.php	(revision 946)
+++ /trunk/jabberit_messenger/inc/class.uienabledgroups.inc.php	(revision 1438)
@@ -58,6 +58,6 @@
 			if( ($LdapOus = $ldap->getOrganizationsLdap()) )
 			{
-				foreach($LdapOus as $tmp )
-					$ous .= "<option value='".$tmp."'>".$tmp."</option>";
+				foreach($LdapOus as $key => $val )
+					$ous .= "<option value='".$key."'>".$val."</option>";
 			}
 
Index: /trunk/jabberit_messenger/inc/class.uigroupslocked.inc.php
===================================================================
--- /trunk/jabberit_messenger/inc/class.uigroupslocked.inc.php	(revision 946)
+++ /trunk/jabberit_messenger/inc/class.uigroupslocked.inc.php	(revision 1438)
@@ -70,10 +70,11 @@
 		
 		$ous = "<option value='-1'>-- ".lang('Select Organization')." --</option>";	
+		
 		if( ($LdapOus = $ldap->getOrganizationsLdap()) )
 		{
-			foreach($LdapOus as $tmp )
-				$ous .= "<option value='".$tmp."'>".$tmp."</option>";
+			foreach($LdapOus as $key=>$val )
+				$ous .= "<option value='".$val."'>".$val."</option>";
 		}
-		
+
 		$groupsRestricts = "";
 
@@ -82,5 +83,5 @@
 			$glocked = explode(';',$GLOBALS['phpgw_info']['server']['groups_locked_jabberit']);
 			natcasesort($glocked);
-			
+	
 			foreach( $glocked as $tmp ){
 				$option = explode(":",$tmp);
Index: /trunk/jabberit_messenger/inc/class.contacts_im.inc.php
===================================================================
--- /trunk/jabberit_messenger/inc/class.contacts_im.inc.php	(revision 1160)
+++ /trunk/jabberit_messenger/inc/class.contacts_im.inc.php	(revision 1438)
@@ -19,4 +19,5 @@
 	private $ldap;
 	private $db;
+	private $dn_User;
 	private $ou_User;
 	private $serverJabber;
@@ -27,6 +28,9 @@
 		$this->db = new db_im();
 
+		// (DN) User
+		$this->dn_User = $_SESSION['phpgw_info']['jabberit_messenger']['account_dn'];
+
 		// (OU) User
-		$this->ou_User = $_SESSION['phpgw_info']['jabberit_messenger']['account_dn'];
+		$this->ou_User = $this->dn_User;
 		$this->ou_User = substr($this->ou_User,strpos($this->ou_User, "ou="));
 		$this->ou_User = strtoupper(substr($this->ou_User, 0, strpos($this->ou_User, ",dc=")));
@@ -131,9 +135,18 @@
 				{
 					if( $tmp != "" )
-						$orgs[] = "OU=". $tmp;            
-				}
-				
+					{
+						if( strpos($tmp, "/") !== false )
+						{
+							$tt = explode("/", $tmp);
+							$newOU = implode(",OU=",array_reverse($tt));
+							$orgs[] = "OU=". $newOU ;
+						}
+						else 
+							$orgs[] = "OU=". $tmp;
+					}            
+				}
+
 	            $orgs = array_unique($orgs);
-
+	
 				foreach( $orgs as $orgB )				
 					$result[] = $this->ldap->getUsersLdapRoot("cn=*".$pName."*", $filter_uid, $orgB );
@@ -170,4 +183,21 @@
 	}
 	
+	private final function strallpos($haystack, $needle, $offset = 0)
+	{
+	    $result = array();
+	    for($i = $offset; $i< strlen($haystack); $i++ )
+	    {
+	        $pos = strpos($haystack,$needle,$i);
+	        if($pos !== FALSE)
+	        {
+	            $offset =  $pos;
+	            if($offset >= $i)
+	                $result[] = $i = $offset;
+	        }
+	    }
+    	
+    	return $result;
+	} 
+
 	public final function verifyAddNewContact($pUid)
 	{
@@ -175,4 +205,6 @@
 		$gidNumbers = array();
 		$uid = $pUid['uid'];
+		$uid_User = substr($this->dn_User, 0, strpos($this->dn_User, ","));
+		$uid_User = substr($uid_User, 4);
 
 		foreach($groupsLocked as $tmp)
@@ -186,5 +218,5 @@
 		
 		$result = $this->ldap->getGroupsMemberUid($filter_gid);
-
+		
 		if( $result && is_array($result) )
 		{
@@ -205,5 +237,5 @@
 			$search = array();
 			$search_Gid = array();
-			
+
 			// Verifica Uid em Grupo Bloqueado
 			foreach($Groups as $value)
@@ -212,10 +244,14 @@
 				{
 					$ou = substr($value['dn'],strpos($value['dn'], "ou="));
-					$search[] = strtoupper(substr($ou, 0, strpos($ou, ",dc=")));
-					$search_Gid[] = $value['gidnumber'];
-				}	
-			}
-		}
-
+					if( array_search($uid_User, $value['memberuid']) === false )
+					{
+						$search[] = strtoupper(substr($ou, 0, strpos($ou, ",dc=")));
+						$search_Gid[] = $value['gidnumber'];
+					}
+				}
+			}
+		}
+		
+	
 		if( $this->groupsLocked() )
 		{
@@ -229,6 +265,18 @@
 					{
 						$ousTp = explode(",",$tpGroups[2]);
-						$ou_User = substr($this->ou_User,3);
+						$ou_User = strtoupper(trim($this->dn_User));
 						
+						$posAll = $this->strallpos($ou_User, "OU=" );
+						$orgs = array();
+				
+						for( $i = 0 ; $i < count($posAll); $i++ )
+						{
+							$pos = strpos($ou_User, ",");
+							$tmpString = substr($ou_User, $posAll[$i] + 3);
+							$orgs[] = substr($tmpString, 0, strpos($tmpString, ","));
+						}
+				
+						$ou_User = implode("/", array_reverse($orgs));
+
 						if( array_search( $ou_User, $ousTp) !== false )
 							return "true";
@@ -251,9 +299,22 @@
 					{							
 						$tpGroups = explode(":",$value);
+						
 						if( $tpGroups[1] == $search_Gid[0] )
 						{
 							$ousTp = explode(",",$tpGroups[2]);
-							$ou_User = substr($this->ou_User,3);
-								
+							$ou_User = strtoupper(trim($this->dn_User));
+					
+							$posAll = $this->strallpos($ou_User, "OU=" );
+							$orgs = array();
+					
+							for( $i = 0 ; $i < count($posAll); $i++ )
+							{
+								$pos = strpos($ou_User, ",");
+								$tmpString = substr($ou_User, $posAll[$i] + 3);
+								$orgs[] = substr($tmpString, 0, strpos($tmpString, ","));
+							}
+					
+							$ou_User = implode("/", array_reverse($orgs));
+							
 							if( array_search( $ou_User, $ousTp) !== false )
 								return "true";
Index: /trunk/jabberit_messenger/inc/class.ldap_im.inc.php
===================================================================
--- /trunk/jabberit_messenger/inc/class.ldap_im.inc.php	(revision 1160)
+++ /trunk/jabberit_messenger/inc/class.ldap_im.inc.php	(revision 1438)
@@ -51,14 +51,17 @@
 		
 		$GLOBALS['phpgw_info']['server']['ldap_version3'] = true;
-
-		$this->ldap = $this->common->ldapConnect( $this->ldap_host, $this->ldap_user . "," . $this->ldap_context , $this->ldap_pass, false );
+		
+		if( $this->ldap_user && $this->ldap_pass )
+			$this->ldap = $this->common->ldapConnect( $this->ldap_host, $this->ldap_user . "," . $this->ldap_context , $this->ldap_pass, false );
+		else
+			$this->ldap = $this->common->ldapConnect( $this->ldap_host, $this->ldap_context , "", false );
 	}
 	
 	private final function ldapRoot()
 	{
-		$this->ldap_host 	= (isset($_SESSION['phpgw_info']['jabberit_messenger']['server_ldap_jabberit'])) ? $_SESSION['phpgw_info']['jabberit_messenger']['server_ldap_jabberit'] : $GLOBALS['phpgw_info']['server']['ldap_host'];
-		$this->ldap_context	= (isset($_SESSION['phpgw_info']['jabberit_messenger']['context_ldap_jabberit'])) ? $_SESSION['phpgw_info']['jabberit_messenger']['context_ldap_jabberit'] : $GLOBALS['phpgw_info']['server']['ldap_context'];
-		$this->ldap_user	= (isset($_SESSION['phpgw_info']['jabberit_messenger']['user_ldap_jabberit'])) ? $_SESSION['phpgw_info']['jabberit_messenger']['user_ldap_jabberit'] : $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
-		$this->ldap_pass	= (isset($_SESSION['phpgw_info']['jabberit_messenger']['password_ldap_jabberit'])) ? $_SESSION['phpgw_info']['jabberit_messenger']['password_ldap_jabberit'] : $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
+		$this->ldap_host 	= $_SESSION['phpgw_info']['jabberit_messenger']['server_ldap_jabberit'];
+		$this->ldap_context	= $_SESSION['phpgw_info']['jabberit_messenger']['context_ldap_jabberit'];
+		$this->ldap_user	= $_SESSION['phpgw_info']['jabberit_messenger']['user_ldap_jabberit'];
+		$this->ldap_pass	= $_SESSION['phpgw_info']['jabberit_messenger']['password_ldap_jabberit'];
 
 		$this->ldapConn();
@@ -146,17 +149,52 @@
 		if( $this->ldap )
 		{
-			$filter="ou=*";		
-			$justthese = array("ou");
-			$search = ldap_search($this->ldap,$this->ldap_context,$filter,$justthese);			
-			$entry = ldap_get_entries($this->ldap, $search);
-		}
-
-		foreach($entry as $tmp)
-			if($tmp['ou'][0] != "")
-				$result_org[] = $tmp['ou'][0];  
-
-		return $result_org;
-	}
-
+			$filter = "(ou=*)";
+			$justthese = array("dn");
+			$search = ldap_search($this->ldap, $this->ldap_context, $filter, $justthese);
+			$info = ldap_get_entries($this->ldap, $search);
+		
+			for ($i=0; $i<$info["count"]; $i++)
+				$a_sectors[] = $info[$i]['dn'];	
+		}
+
+		// Retiro o count do array info e inverto o array para ordenação.
+		foreach ($a_sectors as $context)
+		{
+			$array_dn = ldap_explode_dn ( $context, 1 );
+			$array_dn_reverse  = array_reverse ( $array_dn, true );
+			array_pop ( $array_dn_reverse );
+			$inverted_dn[$context] = implode ( "#", $array_dn_reverse );
+		}
+		
+		// Ordenação
+		natcasesort($inverted_dn);
+
+		foreach ( $inverted_dn as $dn=>$invert_ufn )
+		{
+            $display = '';
+
+            $array_dn_reverse = explode ( "#", $invert_ufn );
+            $array_dn  = array_reverse ( $array_dn_reverse, true );
+
+            $level = count( $array_dn ) - (int)(count(explode(",", $this->ldap_context)) + 1);
+
+            if ($level == 0)
+                    $display .= '+';
+            else 
+            {
+				for( $i = 0; $i < $level; $i++)
+					$display .= '---';
+            }
+
+            reset ( $array_dn );
+            $display .= ' ' . (current ( $array_dn ) );
+			
+			$dn = trim(strtolower($dn));
+			$options[$dn] = $display;
+    	}
+
+	    return $options;
+
+	}
 
 	public final function getUsersLdapCatalog( $search )
Index: /trunk/jabberit_messenger/inc/class.db_im.inc.php
===================================================================
--- /trunk/jabberit_messenger/inc/class.db_im.inc.php	(revision 988)
+++ /trunk/jabberit_messenger/inc/class.db_im.inc.php	(revision 1438)
@@ -426,7 +426,37 @@
 	public final function setOuGroupsLocked($pGroup)
 	{
+		
+		function strallpos($haystack, $needle, $offset = 0)
+		{
+		    $result = array();
+		    for($i = $offset; $i< strlen($haystack); $i++ )
+		    {
+		        $pos = strpos($haystack,$needle,$i);
+		        if($pos !== FALSE)
+		        {
+		            $offset =  $pos;
+		            if($offset >= $i)
+		                $result[] = $i = $offset;
+		        }
+		    }
+	    	
+	    	return $result;
+		} 
+
 		$group = $pGroup['group'];
 		$gidnumber = $pGroup['gidnumber'];
-		$organization = strtoupper($pGroup['ou']);
+		$organization = strtoupper(trim($pGroup['ou']));
+
+		$posAll = strallpos($organization, "OU=" );
+		$orgs = array();
+
+		for( $i = 0 ; $i < count($posAll); $i++ )
+		{
+			$pos = strpos($organization, ",");
+			$tmpString = substr($organization, $posAll[$i] + 3);
+			$orgs[] = substr($tmpString, 0, strpos($tmpString, ","));
+		}
+
+		$organization = implode("/", array_reverse($orgs));
 
 		if( $this->db )
