Index: branches/2.2/jabberit_messenger/inc/class.contacts_im.inc.php
===================================================================
--- branches/2.2/jabberit_messenger/inc/class.contacts_im.inc.php	(revision 3102)
+++ branches/2.2/jabberit_messenger/inc/class.contacts_im.inc.php	(revision 3266)
@@ -74,11 +74,18 @@
 		$order		= array();
 		$ou_User	= substr($this->ou_User, (strpos($this->ou_User,"=")+1));
-		$return		= '<empty/>';
+		$return		= "";
 		$users		= $this->getUsersIm($param['name']);
 
-		
-		if (!is_array($users) && trim($users) === 'Many Results')
-			return "<error>Many Results</error>";
-
+		if( count($users) == 0  )
+			return "<empty></empty>";
+		
+		if( $users === "manyresults" )
+		{
+			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']);
@@ -130,152 +137,100 @@
 
 	private final function getUsersIm($pName)
-	{   
+	{
 		$array_uids		= array();
+		$countUids		= 0;
         $members		= array();
         $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 )
-        {
-        	$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 != "" )
-					{
-						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(")(uid=",$partial_uids_external);
-		    	        		$filter_uid_external	= "(uid=". $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];
+		$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."*");
+					$countUids += count($result[$key]);
 				}
 				else
 				{
-					if(isset($_SESSION['phpgw_info']['jabberit_messenger']['photo']))
-						unset($_SESSION['phpgw_info']['jabberit_messenger']['photo'][$members[$i]['ou']]);
-				}
-			}
-			
-			return $uids_org;
-        }            
-
-		return $members;        
+					if( !$this->groupsLocked() )
+					{
+						$result[$key] =  $this->ldap->getUsersLdapCatalog("cn=*".$pName."*", $key );
+						$countUids += count($result[$key]);
+					}
+				}
+			}
+
+			if( $countUids >  $this->ldap->getMaxResults() )
+			{
+				return "manyresults";
+			}
+
+			$_RESULT = $this->ldap->getMembers($result, $filters);
+			
+			foreach( $_RESULT as $key => $value )
+				$array_uids = array_merge($array_uids, $_RESULT[$key]);
+		}
+		else
+		{
+	        $result[] = $this->ldap->getUsersLdapRoot("cn=*".$pName."*");
+	       
+	       	foreach( $result as $key => $value )
+	       	{
+				$array_uids = array_merge($array_uids, $result[$key]);
+	       	}
+	       	
+	       	if( count($array_uids) >  $this->ldap->getMaxResults() )
+			{
+				return "manyresults";
+			}
+		}
+
+		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]) )
+					{
+						$_restrict[] = $array_uids[$j];
+					}	
+				}	
+			}
+			
+			return $_restrict;
+		}
+		else
+		{	
+			return $array_uids;
+		}	
 	}
 	
