Index: branches/2.2/jabberit_messenger/jmessenger/inc/class.ContactsIm.inc.php
===================================================================
--- branches/2.2/jabberit_messenger/jmessenger/inc/class.ContactsIm.inc.php	(revision 3102)
+++ branches/2.2/jabberit_messenger/jmessenger/inc/class.ContactsIm.inc.php	(revision 3260)
@@ -25,5 +25,5 @@
 		// (OU) User
 		$this->ou_User = $this->dn_User;
-		$this->ou_User = substr($this->ou_User,strpos($this->ou_User, "ou="));
+		$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=")));
 		
@@ -49,14 +49,23 @@
 	public final function getListContacts($param)
 	{
-		
 		$order		= array();
 		$ou_User	= substr($this->ou_User, (strpos($this->ou_User,"=")+1));
-		$return		= '<empty/>';
+		$return		= "<empty></empty>";
 		$users		= $this->getUsersIm($param['name']);
-
-		
-		if (!is_array($users) && trim($users) === 'Many Results')
-			return "<error>Many Results</error>";
-
+		
+		if ( !is_array($users) )
+			return "<error></error>";
+		
+		if( count($users) == 0  )
+			return "<empty></empty>";
+		
+		if( count($users) > $this->ldap->getMaxResults() )
+		{
+			if( isset($_SESSION['phpgw_info']['jabberit_messenger']['photo']) )
+				unset($_SESSION['phpgw_info']['jabberit_messenger']['photo']);
+			
+			return "<manyresults></manyresults>";
+		}
+				
 		// Hosts Jabber
 		$hostsJabber = unserialize($_SESSION['phpgw_info']['jabberit_messenger']['map_org_realm_jabberit']);
@@ -113,147 +122,78 @@
         $result			= array();
         $uidType		= "uid";
-		$serversLdap	= unserialize(trim($_SESSION['phpgw_info']['jabberit_messenger']['groups_search']));
-
-        if( $serversLdap )
-        {
-	        if( array_key_exists($this->serverLdap, $serversLdap) )
-	        {
-	        	$groups = unserialize($serversLdap[$this->serverLdap]);
-	        }
-
-	        if( count($groups) > 0 )
-	        {
-				foreach($groups as $tmp)
-				{
-					$group = explode(":",$tmp);
-					$array_result = $this->ldap->getGroupsMemberUid($group[0], $this->serverLdap );
-					@array_shift($array_result[0]['memberuid']);
-					$array_uids = @array_merge($array_uids, $array_result[0]['memberuid']);
-					unset($array_result);
-				}
-	        }
-        }        			
-		
-        if( count($array_uids) == 0 )
-        {
+		$serversLdap	= unserialize( trim($_SESSION['phpgw_info']['jabberit_messenger']['groups_search']) );
+
+		if( $serversLdap )
+		{
+			// Usa Grupos Ldap
+			
+			$filters = array( );
+			
+			foreach( $serversLdap as $servers => $groups )
+			{
+				$filter = '';
+				foreach( unserialize($groups) as $group )
+					$filter .= '(gidnumber' . strstr( $group, ':' ) . ')';
+
+				$filters[ $servers ] = str_replace( ':', '=', $filter );
+			}
+			
+			foreach( $serversLdap as $key => $tmp )
+			{
+				if( $key === $this->serverLdap )
+				{
+					$result[$key] = $this->ldap->getUsersLdapRoot("cn=*".$pName."*");
+				}
+				else
+				{
+					if( !$this->groupsLocked() )
+						$result[$key] =  $this->ldap->getUsersLdapCatalog("cn=*".$pName."*", $key );
+				}
+			}
+				
+			$_RESULT = $this->ldap->getMembers($result, $filters);
+			
+			foreach( $_RESULT as $key => $value )
+				$array_uids = array_merge($array_uids, $_RESULT[$key]); 
+		}
+		else
+		{
+        	// Consulta Banco
         	$array_uids = $this->db->get_accounts_acl();
-        	$uidType = "uidnumber";	
-        }        
-
-        for( $i = 0; $i < count($array_uids) ; $i+=50 )
-        {   
-            $partial_uids = array_slice($array_uids,$i,50);
-            $filter_uid = implode(")(".$uidType."=",$partial_uids);
-            $filter_uid = "(".$uidType."=". $filter_uid. ")";
-
-			if( $this->groupsLocked() )
-			{
-	            $orgs[] = $this->ou_User;
-	            $orgsGroupsLocked = explode(",", $_SESSION['phpgw_info']['jabberit_messenger']['organizationsGroupsLocked']); 
-	            
-				foreach( $orgsGroupsLocked as $tmp )
-				{
-					if( $tmp != "" )
+        	$uidType = "uidnumber";
+		}
+
+		if( $this->groupsLocked() )
+		{
+            $orgs[] 			= substr($this->ou_User, ( strpos($this->ou_User, "ou=") + 3 ) );
+            $orgsGroupsLocked	= explode(",", $_SESSION['phpgw_info']['jabberit_messenger']['organizationsGroupsLocked']); 
+            
+			foreach( $orgsGroupsLocked as $tmp )
+			{
+				if( $tmp != "")
+					$orgs[] = $tmp;
+			}
+
+            $orgs = array_unique($orgs);
+
+			$_restrict = array();
+			
+			for( $i = 0 ; $i < count($orgs) ; $i++ )
+			{
+				for( $j = 0 ; $j < count($array_uids) ; $j++ )
+				{
+					if( trim($array_uids[$j]['ou']) === trim($orgs[$i]) )
 					{
-						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 );
-
-			}
-			else
-				$result[] = $this->ldap->getUsersLdapRoot("cn=*".$pName."*", $filter_uid);
-        }
-	
-        if( !$this->groupsLocked() )
-        {
-			unset($serversLdap[$this->serverLdap]);
-
-			if( $serversLdap  && count($serversLdap))
-			{
-				foreach( $serversLdap as $key => $tmp )
-				{
-					$array_uids_external = array( );
-					$groupsExternal = unserialize($tmp);
-
-					if( count($groupsExternal) )
-					{
-						foreach( $groupsExternal as $tmpExt )
-						{
-							$group = explode(":",$tmpExt);
-							$array_result = $this->ldap->getGroupsMemberUid( $group[0], $key );
-							
-							if( $array_result[0]['memberuid']['count'] )
-								unset($array_result[0]['memberuid']['count']);
-
-							$array_uids_external = @array_merge($array_uids_external, $array_result[0]['memberuid']);
-						}
-						
-						
-						if( is_array($array_uids_external) )
-						{
-							for( $i = 0; $i < count( $array_uids_external ) ; $i+=50 )
-		        			{   
-			            		$partial_uids_external	= array_slice($array_uids_external,$i,50);
-			            		$filter_uid_external	= implode(")(".$this->attribute."=",$partial_uids_external);
-		    	        		$filter_uid_external	= "(".$this->attribute."=". $filter_uid_external. ")";
-		    	        		$result[] = $this->ldap->getUsersLdapCatalog("cn=*".$pName."*", $filter_uid_external, $key );
-		        			}
-		        		}
-					}
-				}
-			}
-
-			if( count($this->hostsJabberLdap) )
-			{
-				for($i = 0 ; $i < count($this->hostsJabberLdap); $i++)
-				{
-					if( !@array_key_exists($this->hostsJabberLdap[$i]['serverLdap'], $serversLdap) )
-					{
-						$result[] = $this->ldap->getUsersLdapCatalog("cn=*".$pName."*", false, $this->hostsJabberLdap[$i]['serverLdap'] );
-					}
-				}
-			}
-        }
-        
-        if ( is_array($result) )
-        {
-          	for( $i = 0; $i < count($result); $i++ )
-           		if( is_array($result[$i]) )
-           			$members = @array_merge($members,$result[$i]);
-        }
-	     
-        if( count($members) > 150)
-        {
-	    	$ou = substr( $this->ou_User, strpos($this->ou_User, "=") + 1 );
-	    	
-			for($i = 0 ; $i < count($members); $i++ )
-			{
-				if( $ou == $members[$i]['ou'] )
-				{
-					$uids_org[] = $members[$i];
-				}
-				else
-				{
-					if(isset($_SESSION['phpgw_info']['jabberit_messenger']['photo']))
-						unset($_SESSION['phpgw_info']['jabberit_messenger']['photo'][$members[$i]['ou']]);
-				}
-			}
-			
-			return $uids_org;
-        }            
-
-		return $members;        
+						$_restrict[] = $array_uids[$j];
+					}	
+				}	
+			}
+			
+			return $_restrict;
+		}
+		else
+		{	
+			return $array_uids;
+		}	
 	}
 	
@@ -355,5 +295,5 @@
 			if( count($search) > 0 )
 			{
-				// Verifica permissÃµes do grupo
+				// Verifica permissões do grupo
 				foreach($groupsLocked as $value)
 				{							
@@ -392,5 +332,5 @@
 				if( array_search($this->ou_User, $search) === false )
 				{
-					// Verifica permissÃµes do grupo
+					// Verifica permissões do grupo
 					foreach($groupsLocked as $value)
 					{							
Index: branches/2.2/jabberit_messenger/jmessenger/inc/class.LdapIM.inc.php
===================================================================
--- branches/2.2/jabberit_messenger/jmessenger/inc/class.LdapIM.inc.php	(revision 3102)
+++ branches/2.2/jabberit_messenger/jmessenger/inc/class.LdapIM.inc.php	(revision 3260)
@@ -42,5 +42,5 @@
 		
 		// Result Ldap
-		$this->max_result = 20;
+		$this->max_result = 15;
 		
 		if ( file_exists('inc/attributeLdap.php') )
@@ -73,4 +73,9 @@
 	private final function ldapRoot()
 	{
+		$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();
 	}
@@ -102,4 +107,9 @@
 
 		return $conn;
+	}
+	
+	private function getLdapHost()
+	{
+		return	$_SESSION['phpgw_info']['jabberit_messenger']['server_ldap_jabberit'];
 	}
 
@@ -169,4 +179,82 @@
 
 		return $result_groups;
+	}
+
+	public final function getMaxResults()
+	{
+		return $this->max_result;
+	}
+
+	public final function getMembers( $pMembers, $pServers )
+	{
+		$members = $pMembers;
+		
+		foreach( $pServers as $servers => $groups )
+		{
+			if( $servers == $this->getLdapHost() || $servers == 'localhost')
+			{
+				$this->ldapRoot();
+				
+				$count = count($members[$servers]);
+				
+				for( $i = 0; $i < $count; $i++ )
+				{
+					if ( ! $this->getMemberUid($groups, $members[$servers][$i]['uid'] ) )
+						unset( $members[$servers][$i] );
+				}
+
+				if( $this->ldap )
+					@ldap_close($this->ldap);
+			}
+			else
+			{
+				$confHosts	= $this->hostsJabber;
+			
+				for($i = 0; $i < count($confHosts); $i++ )
+				{
+					if( $this->ldap )
+						@ldap_close($this->ldap);
+						
+					if( trim($servers) === trim($confHosts[$i]['serverLdap']) )
+					{
+						$this->ldap_host 	= $confHosts[$i]['serverLdap'];
+						$this->ldap_context = $confHosts[$i]['contextLdap'];
+						$this->ldap_user	= $confHosts[$i]['user'];
+						$this->ldap_org		= $confHosts[$i]['org'];
+						$this->ldap_pass	= $confHosts[$i]['password'];
+						$this->ldap 		= $this->ldapCatalog();
+
+						$count = count($members[$servers]);
+						
+						for( $i = 0; $i < $count; $i++ )
+						{
+							if ( ! $this->getMemberUid($groups, $members[$servers][$i]['uid'] ) )
+								unset( $members[$servers][$i] );
+						}
+		
+						if( $this->ldap )
+							@ldap_close($this->ldap);
+					}
+				}
+			}
+		}
+		
+		return $members;
+	}
+
+	private function getMemberUid( $pGidNumber, $pMemberUid )
+	{
+		$filter		= "(&(phpgwAccountType=g)(|{$pGidNumber})(memberuid={$pMemberUid}))";
+		$justthese	= array("memberuid");
+	
+		if( $this->ldap )
+		{
+			$search = ldap_search($this->ldap, $this->ldap_context, $filter, $justthese );
+			$result = ldap_get_entries($this->ldap,$search);
+			if( $result["count"] )
+				return true;
+		}
+
+		return false;
 	}
 
@@ -360,5 +448,5 @@
 	}
 
-	public final function getUsersLdapCatalog( $search, $uid = false, $pLdap = false )
+	public final function getUsersLdapCatalog( $search, $pLdap = false, $uid = false )
 	{
 		$confHosts	= $this->hostsJabber;
@@ -390,5 +478,5 @@
 			          	$return = array_merge($return, $result);
 					else
-						$return = $result;				
+						$return = $result;
 				}
 				
@@ -401,6 +489,7 @@
 	}
 
-	public final function getUsersLdapRoot( $search, $uidnumber, $ous = false )
-	{
+	public final function getUsersLdapRoot( $search, $uidnumber = false, $ous = false )
+	{
+		
 		$result = array();
 		$this->ldapRoot();
@@ -409,5 +498,5 @@
 		{
 			$searchRoot	= ( $ous ) ? $ous.",".$this->ldap_context : $this->ldap_context ;
-			$filter		= "(&(phpgwaccounttype=u)(|".$uidnumber.")(".$search ."))";
+			$filter		= ($uidnumber) ? "(&(phpgwaccounttype=u)(|".$uidnumber.")(".$search ."))" : "(&(phpgwaccounttype=u)(".$search ."))";
 			$justthese	= array( $this->attribute, "uidNumber", "cn", "mail", "phpgwAccountVisible", "dn", "jpegPhoto" );								
 			$search		= @ldap_search( $this->ldap, $searchRoot, $filter, $justthese, 0, $this->max_result + 1);
