Index: /branches/2.2/jabberit_messenger/jmessenger/inc/class.ContactsIm.inc.php
===================================================================
--- /branches/2.2/jabberit_messenger/jmessenger/inc/class.ContactsIm.inc.php	(revision 3263)
+++ /branches/2.2/jabberit_messenger/jmessenger/inc/class.ContactsIm.inc.php	(revision 3266)
@@ -121,5 +121,4 @@
         $uidType		= "uid";
 		$serversLdap	= unserialize( trim($_SESSION['phpgw_info']['jabberit_messenger']['groups_search']) );
-		
 
 		if( $serversLdap )
@@ -168,7 +167,15 @@
 		else
 		{
-        	// Consulta Banco
-        	$array_uids = $this->db->get_accounts_acl();
-        	$uidType = "uidnumber";
+	        $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";
+			}
 		}
 
Index: /branches/2.2/jabberit_messenger/xsl/list_ldap_contacts.xsl
===================================================================
--- /branches/2.2/jabberit_messenger/xsl/list_ldap_contacts.xsl	(revision 3102)
+++ /branches/2.2/jabberit_messenger/xsl/list_ldap_contacts.xsl	(revision 3266)
@@ -3,19 +3,27 @@
 	<xsl:output method="html" omit-xml-declaration="yes"/>
 
-	<xsl:param name="many_results" />
-	<xsl:param name="empty" />
+	<xsl:param name="lang_addContact" />
+	<xsl:param name="lang_empty" />
+	<xsl:param name="lang_error" />
+	<xsl:param name="lang_many_results" />
+	
+	<xsl:template match="/">
 
-	<xsl:template match="/">
-		<xsl:choose>
-			<xsl:when test="error">
-				<label><xsl:value-of select="$many_results" /></label>
-			</xsl:when>
-			<xsl:when test="empty">
-				<label><xsl:value-of select="$empty" /></label>
-			</xsl:when>
-			<xsl:otherwise>
-				<xsl:apply-templates select="uids/*" mode="uids" />
-			</xsl:otherwise>
-		</xsl:choose>
+		<xsl:if test="error">
+			<label style="color:red;"><xsl:value-of select="$lang_error" /></label>
+		</xsl:if>
+		
+		<xsl:if test="empty">
+			<label style="color:red;"><xsl:value-of select="$lang_empty" /></label>
+		</xsl:if>
+		
+		<xsl:if test="manyresults">
+			<label style="color:red;"><xsl:value-of select="$lang_many_results" /></label>
+		</xsl:if>
+
+		<xsl:if test="uids">
+			<xsl:apply-templates select="uids/*" mode="uids" />
+		</xsl:if>
+		
 	</xsl:template>
 
@@ -29,5 +37,5 @@
 				<br/>
 				<br/>
-				<label id="__label__{mail};{uid}" style="color:blue;cursor:pointer;">Adicionar Contato</label>
+				<label id="__label__{mail};{uid}" style="color:blue;cursor:pointer;"><xsl:value-of select="$lang_addContact" /></label>
 				<br/>
 			</span>
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;
+		}	
 	}
 	
Index: /branches/2.2/jabberit_messenger/inc/class.ldap_im.inc.php
===================================================================
--- /branches/2.2/jabberit_messenger/inc/class.ldap_im.inc.php	(revision 3102)
+++ /branches/2.2/jabberit_messenger/inc/class.ldap_im.inc.php	(revision 3266)
@@ -42,5 +42,5 @@
 		
 		// Result Ldap
-		$this->max_result = 20;
+		$this->max_result = 15;
 		
 		if ( file_exists('inc/attributeLdap.php') )
@@ -49,4 +49,6 @@
 			$this->attribute = trim($attributeTypeName);
 		}
+		else
+			$this->attribute = "uid";
 	}
 
@@ -71,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();
 	}
@@ -100,4 +107,9 @@
 
 		return $conn;
+	}
+
+	private function getLdapHost()
+	{
+		return	$_SESSION['phpgw_info']['jabberit_messenger']['server_ldap_jabberit'];
 	}
 
@@ -167,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;
 	}
 
@@ -301,5 +391,5 @@
 	}
 
-	public final function getUsersLdapCatalog( $search, $uid = false, $pLdap = false )
+	public final function getUsersLdapCatalog( $search, $pLdap = false, $uid = false )
 	{
 		$confHosts	= $this->hostsJabber;
@@ -331,5 +421,5 @@
 			          	$return = array_merge($return, $result);
 					else
-						$return = $result;				
+						$return = $result;
 				}
 				
@@ -342,5 +432,5 @@
 	}
 
-	public final function getUsersLdapRoot( $search, $uidnumber, $ous = false )
+	public final function getUsersLdapRoot( $search, $uidnumber = false, $ous = false )
 	{
 		$result = array();
@@ -350,8 +440,8 @@
 		{
 			$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);
-			$entry		= ldap_get_entries( $this->ldap, $search );
+			$search		= @ldap_search( $this->ldap, $searchRoot, $filter, $justthese, 0, $this->max_result + 1);
+			$entry		= @ldap_get_entries( $this->ldap, $search );
 			$result		= $this->resultArray( $entry, $this->ldap );
 		}		
Index: /branches/2.2/jabberit_messenger/inc/class.db_im.inc.php
===================================================================
--- /branches/2.2/jabberit_messenger/inc/class.db_im.inc.php	(revision 3102)
+++ /branches/2.2/jabberit_messenger/inc/class.db_im.inc.php	(revision 3266)
@@ -111,26 +111,4 @@
 		}
 		return $apps;
-	}
-
-	public final function get_accounts_acl()
-	{
-		$query  = "SELECT acl_account FROM phpgw_acl WHERE acl_location IN (SELECT CAST(acl_account AS varchar) FROM phpgw_acl WHERE acl_appname = 'jabberit_messenger') ";
-		$query .= "UNION SELECT acl_account FROM phpgw_acl WHERE acl_appname = 'jabberit_messenger'";
-		
-		if( $this->db->query($query) )	
-		{
-			$users = array();
-			$new_users = array();
-			while($this->db->next_record())
-				$users[] = $this->db->row();
-
-			if(is_array($users))
-				foreach($users as $tmp)
-					$new_users[] = $tmp['acl_account'];
-			
-			return $new_users;
-		}
-		
-		return false;
 	}
 	
Index: /branches/2.2/jabberit_messenger/js/ldap.js
===================================================================
--- /branches/2.2/jabberit_messenger/js/ldap.js	(revision 3102)
+++ /branches/2.2/jabberit_messenger/js/ldap.js	(revision 3266)
@@ -138,6 +138,8 @@
 					{
 						var _pList = {
-										'many_results' : jabberitGetLang('More than 50 results. Please, try to refine your search.'),
-										'empty' : jabberitGetLang('None result was found.')
+										'lang_addContact'	: "Adicionar Contatos" ,
+										'lang_empty' 		: "Nenhum resultado encontrado !",
+										'lang_error'		: "Tente Novamente!",
+										'lang_many_results' : "Muitos Resultados ! Por favor tente refinar sua busca !"
 									 };	
 						
