Index: trunk/expressoAdmin1_2/inc/class.ldap_functions.inc.php
===================================================================
--- trunk/expressoAdmin1_2/inc/class.ldap_functions.inc.php	(revision 63)
+++ trunk/expressoAdmin1_2/inc/class.ldap_functions.inc.php	(revision 64)
@@ -10,4 +10,5 @@
 	var $current_config;
 	var $functions;
+	var $manager_contexts;
 	
 	function ldap_functions(){
@@ -16,13 +17,44 @@
 		$common = new common();
 		$this->ldap = $common->ldapConnect();
+		
 		$this->functions = new functions;
+		$manager_acl = $this->functions->read_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid']);
+		$this->manager_contexts = $manager_acl['contexts'];
 	}
 	
 	function validate_fields($params)
 	{
+		/* ldap connection following referals and using Contac Center config*/
+		if (is_array($_SESSION['phpgw_info']['expresso']['cc_ldap_server']))
+		{
+			$ldap_connection = ldap_connect($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['host']);
+			if ($ldap_connection)
+			{
+				ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
+				ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
+				if ( ($GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['acc'] != '') && ($GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['pw'] != '') )
+					ldap_bind($ldap_connection, $GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['acc'], $GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['pw']);
+				$context = $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['dn'];
+			}
+			else
+			{
+				$result['status'] = false;
+				$result['msg'] = 'Falha na conexao com o ldap.';
+				return $result;
+			}
+		}
+		else
+		{
+			$ldap_connection = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
+			ldap_set_option($ldap_connection,LDAP_OPT_PROTOCOL_VERSION,3);
+			ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
+			ldap_bind($ldap_connection, $GLOBALS['phpgw_info']['server']['ldap_root_dn'], $GLOBALS['phpgw_info']['server']['ldap_root_pw']);
+			$context = $GLOBALS['phpgw_info']['server']['ldap_context'];
+		}
+		
 		$result['status'] = true;
+		
 		$params = unserialize($params['attributes']);
 		$type = $params['type'];
-		$context = $GLOBALS['phpgw_info']['server']['ldap_context'];
 		$uid = $params['uid'];
 		$mail = $params['mail'];
@@ -38,13 +70,25 @@
 		if (($type == 'create_user') || ($type == 'rename_user')) 
 		{
-			// UID
-			$filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(uid=$uid))";
+			//UID
+			if (($type == 'rename_user') && ($this->current_config['expressoAdmin_prefix_org'] == 'true'))
+			{
+				//Obtenho UID sem a organização. Na criação o uid já vem sem a organização
+				$tmp_uid_without_org = split("-", $params['uid']);
+				$tmp_reverse_uid_without_org = array_reverse($tmp_uid_without_org);
+				array_pop($tmp_reverse_uid_without_org);
+				$uid_without_org = implode("-", $tmp_reverse_uid_without_org);
+				$filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(|(uid=$uid)(uid=$uid_without_org)))";
+			}
+			else
+			{
+				$filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(uid=$uid))";
+			}
 			$justthese = array("uid");
-			$search = ldap_search($this->ldap, $context, $filter, $justthese);
-			$count_entries = ldap_count_entries($this->ldap,$search);
+			$search = ldap_search($ldap_connection, $context, $filter, $justthese);
+			$count_entries = ldap_count_entries($ldap_connection,$search);
 			if ($count_entries > 0)
 			{
 				$result['status'] = false;
-				$result['msg'] = 'LOGIN já esta sendo usado.';
+				$result['msg'] = 'LOGIN já está sendo utilizado.';
 				return $result;
 			}
@@ -53,6 +97,6 @@
 			$filter = "(&(phpgwAccountType=g)(cn=$uid))";
 			$justthese = array("cn");
-			$search = ldap_search($this->ldap, $context, $filter, $justthese);
-			$count_entries = ldap_count_entries($this->ldap,$search);
+			$search = ldap_search($ldap_connection, $context, $filter, $justthese);
+			$count_entries = ldap_count_entries($ldap_connection,$search);
 			if ($count_entries > 0)
 			{
@@ -61,26 +105,53 @@
 				return $result;
 			}
-		
-			// UID em outras organizações
-			//Quando tento retirar as organizações pelo expressoAdmin dá este erro.
-			$filter = "(ou=*)";
-			$justthese = array("ou");
-			$search = ldap_list($this->ldap, $context, $filter, $justthese);
-			$entries = ldap_get_entries($this->ldap,$search);
-			foreach ($entries as $index=>$org)
-			{
-				$organization = $org['ou'][0];
-				$organization = strtolower($organization);
+			
+			
+			// UID em outras organizações, pesquiso apenas na maquina local e se utilizar prefix_org
+			if ($this->current_config['expressoAdmin_prefix_org'] == 'true')
+			{
+				$ldap_connection2 = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
+				ldap_set_option($ldap_connection2,LDAP_OPT_PROTOCOL_VERSION,3);
+				ldap_set_option($ldap_connection2, LDAP_OPT_REFERRALS, false);
+				ldap_bind($ldap_connection2, $GLOBALS['phpgw_info']['server']['ldap_root_dn'], $GLOBALS['phpgw_info']['server']['ldap_root_pw']);
+				$context = $GLOBALS['phpgw_info']['server']['ldap_context'];
 				
-				$filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(uid=$organization-$uid))";
-				$justthese = array("uid");
-				$search = ldap_search($this->ldap, $context, $filter, $justthese);
-				$count_entries = ldap_count_entries($this->ldap,$search);
-				if ($count_entries > 0)
+				//Obtenho UID sem a organização
+				/*
+				$tmp_uid_without_org = split("-", $params['uid']);
+				if (count($tmp_uid_without_org) < 2)
 				{
 					$result['status'] = false;
-					$result['msg'] = 'LOGIN já esta sendo usado por outro usuário em outra organização.';
+					$result['msg'] = 'Novo login sem organização.';
 					return $result;
 				}
+				$tmp_reverse_uid_without_org = array_reverse($tmp_uid_without_org);
+				array_pop($tmp_reverse_uid_without_org);
+				$uid_without_org = implode("-", $tmp_reverse_uid_without_org);
+				*/
+				
+				$filter = "(ou=*)";
+				$justthese = array("ou");
+				$search = ldap_list($ldap_connection2, $context, $filter, $justthese);
+				$entries = ldap_get_entries($ldap_connection2	,$search);
+				
+				foreach ($entries as $index=>$org)
+				{
+					$organization = $org['ou'][0];
+					$organization = strtolower($organization);
+				
+					$filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(uid=$organization-$uid))";
+					
+					$justthese = array("uid");
+					$search = ldap_search($ldap_connection2, $context, $filter, $justthese);
+					$count_entries = ldap_count_entries($ldap_connection2,$search);
+					if ($count_entries > 0)
+					{
+						$result['status'] = false;
+						$result['msg'] = 'LOGIN já esta sendo usado por outro usuário em outra organização.';
+						ldap_close($ldap_connection2);
+						return $result;
+					}
+				}
+				ldap_close($ldap_connection2);
 			}
 		}
@@ -94,11 +165,11 @@
 		$filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(|(mail=$mail)(mailalternateaddress=$mail)))";
 		$justthese = array("mail", "uid");
-		$search = ldap_search($this->ldap, $context, $filter, $justthese);
-		$entries = ldap_get_entries($this->ldap,$search);
+		$search = ldap_search($ldap_connection, $context, $filter, $justthese);
+		$entries = ldap_get_entries($ldap_connection,$search);
 		if ($entries['count'] == 1){
 			if ($entries[0]['uid'][0] != $uid){
 				$result['status'] = false;
 				$result['msg'] = 'E-MAIL está sendo usado por 1 usuário: ' . $entries[0]['uid'][0];
-				//ldap_close($this->ldap);
+				//ldap_close($ldap_connection);
 				return $result;
 			}
@@ -106,6 +177,6 @@
 		else if ($entries['count'] > 1){
 			$result['status'] = false;
-			$result['msg'] = 'E-MAIL está sendo usado por de 2 ou mais usuários.';
-			//ldap_close($this->ldap);
+			$result['msg'] = 'E-MAIL está sendo usado por 2 ou mais usuários.';
+			//ldap_close($ldap_connection);
 			return $result;
 		}
@@ -114,11 +185,11 @@
 		$filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(|(mail=$mailalternateaddress)(mailalternateaddress=$mailalternateaddress)))";
 		$justthese = array("mail", "uid");
-		$search = ldap_search($this->ldap, $context, $filter, $justthese);
-		$entries = ldap_get_entries($this->ldap,$search);
+		$search = ldap_search($ldap_connection, $context, $filter, $justthese);
+		$entries = ldap_get_entries($ldap_connection,$search);
 		if ($entries['count'] == 1){
 			if ($entries[0]['uid'][0] != $uid){
 				$result['status'] = false;
 				$result['msg'] = "E-MAIL alternativo está sendo usado por 1 usuário: " . $entries[0]['uid'][0];
-				//ldap_close($this->ldap);
+				//ldap_close($ldap_connection);
 				return $result;
 			}
@@ -135,5 +206,32 @@
 	function validate_fields_group($params)
 	{
-		$context = $GLOBALS['phpgw_info']['server']['ldap_context'];
+		/* ldap connection following referals and using Contac Center config*/
+		if (is_array($_SESSION['phpgw_info']['expresso']['cc_ldap_server']))
+		{
+			$ldap_connection = ldap_connect($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['host']);
+			if ($ldap_connection)
+			{
+				ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
+				ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
+				if ( ($GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['acc'] != '') && ($GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['pw'] != '') )
+					ldap_bind($ldap_connection, $GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['acc'], $GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['pw']);
+				$context = $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['dn'];
+			}
+			else
+			{
+				$result['status'] = false;
+				$result['msg'] = 'Falha na conexao com o ldap.';
+				return $result;
+			}
+		}
+		else
+		{
+			$ldap_connection = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
+			ldap_set_option($ldap_connection,LDAP_OPT_PROTOCOL_VERSION,3);
+			ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
+			ldap_bind($ldap_connection, $GLOBALS['phpgw_info']['server']['ldap_root_dn'], $GLOBALS['phpgw_info']['server']['ldap_root_pw']);
+			$context = $GLOBALS['phpgw_info']['server']['ldap_context'];
+		}
+
 		$cn = $params['cn'];
 		$result['status'] = true;
@@ -149,6 +247,6 @@
 		$filter = "(&(phpgwAccountType=g)(cn=$cn))";
 		$justthese = array("cn");
-		$search = ldap_search($this->ldap, $context, $filter, $justthese);
-		$count_entries = ldap_count_entries($this->ldap,$search);
+		$search = ldap_search($ldap_connection, $context, $filter, $justthese);
+		$count_entries = ldap_count_entries($ldap_connection,$search);
 		if ($count_entries > 0)
 		{
@@ -161,6 +259,6 @@
 		$filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(uid=$cn))";
 		$justthese = array("uid");
-		$search = ldap_search($this->ldap, $context, $filter, $justthese);
-		$count_entries = ldap_count_entries($this->ldap,$search);
+		$search = ldap_search($ldap_connection, $context, $filter, $justthese);
+		$count_entries = ldap_count_entries($ldap_connection,$search);
 		if ($count_entries > 0)
 		{
@@ -175,5 +273,32 @@
 	function validate_fields_maillist($params)
 	{
-		$context = $GLOBALS['phpgw_info']['server']['ldap_context'];
+		/* ldap connection following referals and using Contac Center config*/
+		if (is_array($_SESSION['phpgw_info']['expresso']['cc_ldap_server']))
+		{
+			$ldap_connection = ldap_connect($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['host']);
+			if ($ldap_connection)
+			{
+				ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
+				ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
+				if ( ($GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['acc'] != '') && ($GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['pw'] != '') )
+					ldap_bind($ldap_connection, $GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['acc'], $GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['pw']);
+				$context = $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['dn'];
+			}
+			else
+			{
+				$result['status'] = false;
+				$result['msg'] = 'Falha na conexao com o ldap.';
+				return $result;
+			}
+		}
+		else
+		{
+			$ldap_connection = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
+			ldap_set_option($ldap_connection,LDAP_OPT_PROTOCOL_VERSION,3);
+			ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
+			ldap_bind($ldap_connection, $GLOBALS['phpgw_info']['server']['ldap_root_dn'], $GLOBALS['phpgw_info']['server']['ldap_root_pw']);
+			$context = $GLOBALS['phpgw_info']['server']['ldap_context'];
+		}
+		
 		$uid = $params['uid'];
 		$mail = $params['mail'];
@@ -190,6 +315,6 @@
 		$filter = "(&(phpgwAccountType=l)(uid=$uid))";
 		$justthese = array("uid");
-		$search = ldap_search($this->ldap, $context, $filter, $justthese);
-		$count_entries = ldap_count_entries($this->ldap,$search);
+		$search = ldap_search($ldap_connection, $context, $filter, $justthese);
+		$count_entries = ldap_count_entries($ldap_connection,$search);
 		if ($count_entries > 0)
 		{
@@ -203,6 +328,6 @@
 		$filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(|(mail=$mail)(mailalternateaddress=$mail)))";
 		$justthese = array("mail");
-		$search = ldap_search($this->ldap, $context, $filter, $justthese);
-		$count_entries = ldap_count_entries($this->ldap,$search);
+		$search = ldap_search($ldap_connection, $context, $filter, $justthese);
+		$count_entries = ldap_count_entries($ldap_connection,$search);
 		if ($count_entries > 0)
 		{
@@ -610,288 +735,337 @@
 	}
 	
-	function get_user_info($uidnumber, $context)
-	{
-		$filter="(&(phpgwAccountType=u)(uidNumber=".$uidnumber."))";
-		$search = ldap_search($this->ldap, $context, $filter);
-		$entry = ldap_get_entries($this->ldap, $search);
-
-		//Pega o dn do setor do usuario.
-		$entry[0]['dn'] = strtolower($entry[0]['dn']);
-		$sector_dn_array = explode(",", $entry[0]['dn']);
-		for($i=1; $i<count($sector_dn_array); $i++)
-			$sector_dn .= $sector_dn_array[$i] . ',';
-		//Retira ultimo pipe.
-		$sector_dn = substr($sector_dn,0,(strlen($sector_dn) - 1));
-		
-		$result['context']				= $sector_dn;
-		$result['uid']					= $entry[0]['uid'][0];
-		$result['uidnumber']			= $entry[0]['uidnumber'][0];
-		$result['gidnumber']			= $entry[0]['gidnumber'][0];
-		$result['departmentnumber']		= $entry[0]['departmentnumber'][0];
-		$result['givenname']			= $entry[0]['givenname'][0];
-		$result['sn']					= $entry[0]['sn'][0];
-		$result['telephonenumber']		= $entry[0]['telephonenumber'][0];
-		$result['phpgwaccountstatus']	= $entry[0]['phpgwaccountstatus'][0];
-		$result['phpgwaccountvisible']	= $entry[0]['phpgwaccountvisible'][0];
-		$result['accountstatus']		= $entry[0]['accountstatus'][0];
-		$result['mail']					= $entry[0]['mail'][0];
-		$result['mailalternateaddress']	= $entry[0]['mailalternateaddress'][0];
-		$result['mailforwardingaddress']= $entry[0]['mailforwardingaddress'][0];
-		$result['deliverymode']			= $entry[0]['deliverymode'][0];
-		$result['userPasswordRFC2617']	= $entry[0]['userpasswordrfc2617'][0];
-
-		//Photo
-		if ($entry[0]['jpegphoto']['count'] == 1)
-			$result['photo_exist'] = 'true';
-		
-		// Samba
-		for ($i=0; $i<$entry[0]['objectclass']['count']; $i++)
-		{
-			if ($entry[0]['objectclass'][$i] == 'sambaSamAccount')
-				$result['sambaUser'] = true;
-		}		 
-		if (($this->current_config['expressoAdmin_samba_support'] == 'true') && ($result['sambaUser']))
-		{
-			$result['sambaaccflags'] = $entry[0]['sambaacctflags'][0];
-			$result['sambalogonscript'] = $entry[0]['sambalogonscript'][0];
-			$result['homedirectory'] = $entry[0]['homedirectory'][0];
-			$a_tmp = explode("-", $entry[0]['sambasid'][0]);
-			array_pop($a_tmp);
-			$result['sambasid'] = implode("-", $a_tmp);
-		}
-
-		// Verifica o acesso do gerente aos atributos corporativos
-		if ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'manipulate_corporative_information'))
-		{
-			$result['corporative_information_employeenumber']= $entry[0]['employeenumber'][0];
-			$result['corporative_information_cpf']			= $entry[0]['cpf'][0];
-			$result['corporative_information_rg']			= $entry[0]['rg'][0];
-			$result['corporative_information_rguf']			= $entry[0]['rguf'][0];
-		}
-		
-		// MailLists
+	function get_user_info($uidnumber)
+	{
+		foreach ($this->manager_contexts as $index=>$context)
+		{
+			$filter="(&(phpgwAccountType=u)(uidNumber=".$uidnumber."))";
+			$search = ldap_search($this->ldap, $context, $filter);
+			$entry = ldap_get_entries($this->ldap, $search);
+			
+			if ($entry['count'])
+			{
+				//Pega o dn do setor do usuario.
+				$entry[0]['dn'] = strtolower($entry[0]['dn']);
+				$sector_dn_array = explode(",", $entry[0]['dn']);
+				for($i=1; $i<count($sector_dn_array); $i++)
+					$sector_dn .= $sector_dn_array[$i] . ',';
+				//Retira ultimo pipe.
+				$sector_dn = substr($sector_dn,0,(strlen($sector_dn) - 1));
+		
+				$result['context']				= $sector_dn;
+				$result['uid']					= $entry[0]['uid'][0];
+				$result['uidnumber']			= $entry[0]['uidnumber'][0];
+				$result['gidnumber']			= $entry[0]['gidnumber'][0];
+				$result['departmentnumber']		= $entry[0]['departmentnumber'][0];
+				$result['givenname']			= $entry[0]['givenname'][0];
+				$result['sn']					= $entry[0]['sn'][0];
+				$result['telephonenumber']		= $entry[0]['telephonenumber'][0];
+				$result['phpgwaccountstatus']	= $entry[0]['phpgwaccountstatus'][0];
+				$result['phpgwaccountvisible']	= $entry[0]['phpgwaccountvisible'][0];
+				$result['accountstatus']		= $entry[0]['accountstatus'][0];
+				$result['mail']					= $entry[0]['mail'][0];
+				$result['mailalternateaddress']	= $entry[0]['mailalternateaddress'][0];
+				$result['mailforwardingaddress']= $entry[0]['mailforwardingaddress'][0];
+				$result['deliverymode']			= $entry[0]['deliverymode'][0];
+				$result['userPasswordRFC2617']	= $entry[0]['userpasswordrfc2617'][0];
+
+				//Photo
+				if ($entry[0]['jpegphoto']['count'] == 1)
+					$result['photo_exist'] = 'true';
+		
+				// Samba
+				for ($i=0; $i<$entry[0]['objectclass']['count']; $i++)
+				{
+					if ($entry[0]['objectclass'][$i] == 'sambaSamAccount')
+						$result['sambaUser'] = true;
+				}
+				if (($this->current_config['expressoAdmin_samba_support'] == 'true') && ($result['sambaUser']))
+				{
+					$result['sambaaccflags'] = $entry[0]['sambaacctflags'][0];
+					$result['sambalogonscript'] = $entry[0]['sambalogonscript'][0];
+					$result['homedirectory'] = $entry[0]['homedirectory'][0];
+					$a_tmp = explode("-", $entry[0]['sambasid'][0]);
+					array_pop($a_tmp);
+					$result['sambasid'] = implode("-", $a_tmp);
+				}
+
+				// Verifica o acesso do gerente aos atributos corporativos
+				if ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'manipulate_corporative_information'))
+				{
+					$result['corporative_information_employeenumber']= $entry[0]['employeenumber'][0];
+					$result['corporative_information_cpf']			= $entry[0]['cpf'][0];
+					$result['corporative_information_rg']			= $entry[0]['rg'][0];
+					$result['corporative_information_rguf']			= $entry[0]['rguf'][0];
+				}
+				
+				// MailLists
+				$result['maillists_info'] = $this->get_user_maillists($result['mail']);
+				foreach ($result['maillists_info'] as $maillist)
+				{
+					$result['maillists'][] = $maillist['uidnumber'];
+				}
+				
+				// Groups
+				$justthese = array("gidnumber","cn");
+				$filter="(&(phpgwAccountType=g)(memberuid=".$result['uid']."))";
+				$search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
+    			ldap_sort($this->ldap, $search, "cn");
+    			$entries = ldap_get_entries($this->ldap, $search);
+    			for ($i=0; $i<$entries['count']; $i++)
+	    		{
+    				$result['groups_ldap'][ $entries[$i]['gidnumber'][0] ] = $entries[$i]['cn'][0];
+    			}
+			}
+		}
+		return $result;
+	}
+		
+	function get_user_maillists($mail)
+	{
+		$result = array();
+		
+		//Mostra somente os mailists dos contextos do gerente
 		$justthese = array("uid","mail","uidnumber");
-		$filter="(&(phpgwAccountType=l)(mailforwardingaddress=".$result['mail']."))";
-		$search = ldap_search($this->ldap, $context, $filter, $justthese);
-    	ldap_sort($this->ldap, $search, "uid");
-    	$entries = ldap_get_entries($this->ldap, $search);
-    	for ($i=0; $i<$entries['count']; $i++)
+		$filter="(&(phpgwAccountType=l)(mailforwardingaddress=$mail))";
+		
+		foreach ($this->manager_contexts as $index=>$context)
+		{
+			$search = ldap_search($this->ldap, $context, $filter, $justthese);
+			//ldap_sort($this->ldap, $search, "uid");
+    		$entries = ldap_get_entries($this->ldap, $search);
+    		
+	    	for ($i=0; $i<$entries['count']; $i++)
+    		{
+				$result[ $entries[$i]['uid'][0] ]['uidnumber']	= $entries[$i]['uidnumber'][0];
+				$result[ $entries[$i]['uid'][0] ]['uid']		= $entries[$i]['uid'][0];
+				$result[ $entries[$i]['uid'][0] ]['mail']		= $entries[$i]['mail'][0];
+				
+				$a_tmp[] = $entries[$i]['uid'][0];
+    		}
+		}
+    	
+    	natcasesort($a_tmp);
+    	
+    	foreach ($a_tmp as $uid)
     	{
-    		$result['maillists_info'][$i]['uidnumber'] = $entries[$i]['uidnumber'][0];
-    		$result['maillists_info'][$i]['uid'] = $entries[$i]['uid'][0];
-    		$result['maillists_info'][$i]['mail'] = $entries[$i]['mail'][0];
-    		$result['maillists'][] = $entries[$i]['uidnumber'][0];
+    		$return[$uid]['uidnumber']	= $result[$uid]['uidnumber'];
+			$return[$uid]['uid']		= $result[$uid]['uid'];
+			$return[$uid]['mail']		= $result[$uid]['mail'];
     	}
-    	
-    	// Groups
-		$justthese = array("gidnumber","cn");
-		$filter="(&(phpgwAccountType=g)(memberuid=".$result['uid']."))";
-		$search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
-    	ldap_sort($this->ldap, $search, "cn");
-    	$entries = ldap_get_entries($this->ldap, $search);
-    	for ($i=0; $i<$entries['count']; $i++)
-    	{
-    		$result['groups_ldap'][ $entries[$i]['gidnumber'][0] ] = $entries[$i]['cn'][0];
-    	}
-    	
-		return $result;		
-	}
-	
-	function get_group_info($gidnumber, $context)
-	{
-		$filter="(&(phpgwAccountType=g)(gidNumber=".$gidnumber."))";
-		$search = ldap_search($this->ldap, $context, $filter);
-		$entry = ldap_get_entries($this->ldap, $search);
-
-		//Pega o dn do setor do grupo.
-		$entry[0]['dn'] = strtolower($entry[0]['dn']);
-		$sector_dn_array = explode(",", $entry[0]['dn']);
-		for($i=1; $i<count($sector_dn_array); $i++)
-			$sector_dn .= $sector_dn_array[$i] . ',';
-		//Retira ultimo pipe.
-		$sector_dn = substr($sector_dn,0,(strlen($sector_dn) - 1));
-		
-		$result['context']				= $sector_dn;
-		$result['cn']					= $entry[0]['cn'][0];
-		$result['description']			= $entry[0]['description'][0];
-		$result['gidnumber']			= $entry[0]['gidnumber'][0];
-		$result['phpgwaccountvisible']	= $entry[0]['phpgwaccountvisible'][0];
-		$result['email']				= $entry[0]['mail'][0];
-		
-		//MemberUid
-		for ($i=0; $i<$entry[0]['memberuid']['count']; $i++)
-		{
-			$justthese = array("cn","uid","uidnumber");
-			
-			// Montagem dinamica do filtro
-			$filter="(&(phpgwAccountType=u)(|";
-			for ($k=0; (($k<10) && ($i<$entry[0]['memberuid']['count'])); $k++)
-			{
-				$filter .= "(uid=".$entry[0]['memberuid'][$i].")";
-				$i++;
-			}
-			$i--;
-			$filter .= "))";
-			
-			$search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
-			$user_entry = ldap_get_entries($this->ldap, $search);
-
-			for ($j=0; $j<$user_entry['count']; $j++)
-			{
-				$result['memberuid_info'][$user_entry[$j]['uid'][0]]['cn'] = $user_entry[$j]['cn'][0];
-				$result['memberuid_info'][$user_entry[$j]['uid'][0]]['uidnumber'] = $user_entry[$j]['uidnumber'][0];
-				$result['memberuid_info'][$user_entry[$j]['uid'][0]]['type'] = 'u';
-			}
-		}
-		
-		// Retira o count do array
-		array_shift($entry[0]['memberuid']);
-		
-		// Checamos e-mails que não fazem parte do expresso.
-		// Criamos um array temporario
-		$tmp_array = array();
-		foreach ($result['memberuid_info'] as $uid => $user_data)
-		{
-			$tmp_array[] = $uid;
-		}
-
-		// Vemos a diferença
-		$array_diff = array_diff($entry[0]['memberuid'], $tmp_array);
-		
-		// Incluimos no resultado
-		foreach ($array_diff as $index=>$uid)
-		{
-			$result['memberuid_info'][$uid]['cn'] = $uid;
-		}
-		
-		// Samba
-		for ($i=0; $i<$entry[0]['objectclass']['count']; $i++)
-		{
-			if ($entry[0]['objectclass'][$i] == 'sambaGroupMapping')
-				$result['sambaGroup'] = true;
-
-			$a_tmp = explode("-", $entry[0]['sambasid'][0]);
-			array_pop($a_tmp);
-			$result['sambasid'] = implode("-", $a_tmp);
-		}
-
-		return $result;
+		return $return;
+	}
+	
+	function get_group_info($gidnumber)
+	{
+		foreach ($this->manager_contexts as $index=>$context)
+		{
+			$filter="(&(phpgwAccountType=g)(gidNumber=".$gidnumber."))";
+			$search = ldap_search($this->ldap, $context, $filter);
+			$entry = ldap_get_entries($this->ldap, $search);
+			
+			if ($entry['count'])
+			{
+				//Pega o dn do setor do grupo.
+				$entry[0]['dn'] = strtolower($entry[0]['dn']);
+				$sector_dn_array = explode(",", $entry[0]['dn']);
+				for($i=1; $i<count($sector_dn_array); $i++)
+					$sector_dn .= $sector_dn_array[$i] . ',';
+				//Retira ultimo pipe.
+				$sector_dn = substr($sector_dn,0,(strlen($sector_dn) - 1));
+		
+				$result['context']				= $sector_dn;
+				$result['cn']					= $entry[0]['cn'][0];
+				$result['description']			= $entry[0]['description'][0];
+				$result['gidnumber']			= $entry[0]['gidnumber'][0];
+				$result['phpgwaccountvisible']	= $entry[0]['phpgwaccountvisible'][0];
+				$result['email']				= $entry[0]['mail'][0];
+		
+				//MemberUid
+				for ($i=0; $i<$entry[0]['memberuid']['count']; $i++)
+				{
+					$justthese = array("cn","uid","uidnumber");
+			
+					// Montagem dinamica do filtro
+					$filter="(&(phpgwAccountType=u)(|";
+					for ($k=0; (($k<10) && ($i<$entry[0]['memberuid']['count'])); $k++)
+					{
+						$filter .= "(uid=".$entry[0]['memberuid'][$i].")";
+						$i++;
+					}
+					$i--;
+					$filter .= "))";
+			
+					$search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
+					$user_entry = ldap_get_entries($this->ldap, $search);
+
+					for ($j=0; $j<$user_entry['count']; $j++)
+					{
+						$result['memberuid_info'][$user_entry[$j]['uid'][0]]['cn'] = $user_entry[$j]['cn'][0];
+						$result['memberuid_info'][$user_entry[$j]['uid'][0]]['uidnumber'] = $user_entry[$j]['uidnumber'][0];
+						$result['memberuid_info'][$user_entry[$j]['uid'][0]]['type'] = 'u';
+					}
+				}
+		
+				// Retira o count do array
+				array_shift($entry[0]['memberuid']);
+		
+				// Checamos e-mails que não fazem parte do expresso.
+				// Criamos um array temporario
+				$tmp_array = array();
+				foreach ($result['memberuid_info'] as $uid => $user_data)
+				{
+					$tmp_array[] = $uid;
+				}
+
+				// Vemos a diferença
+				$array_diff = array_diff($entry[0]['memberuid'], $tmp_array);
+		
+				// Incluimos no resultado
+				foreach ($array_diff as $index=>$uid)
+				{
+					$result['memberuid_info'][$uid]['cn'] = $uid;
+				}
+		
+				// Samba
+				for ($i=0; $i<$entry[0]['objectclass']['count']; $i++)
+				{
+					if ($entry[0]['objectclass'][$i] == 'sambaGroupMapping')
+						$result['sambaGroup'] = true;
+
+					$a_tmp = explode("-", $entry[0]['sambasid'][0]);
+					array_pop($a_tmp);
+					$result['sambasid'] = implode("-", $a_tmp);
+				}
+				return $result;
+			}
+		}
 	}	
 	
-	function get_maillist_info($uidnumber, $context)
-	{
-		$filter="(&(phpgwAccountType=l)(uidNumber=".$uidnumber."))";
-		$search = ldap_search($this->ldap, $context, $filter);
-		$entry = ldap_get_entries($this->ldap, $search);
-	
-		//Pega o dn do setor do usuario.
-		$entry[0]['dn'] = strtolower($entry[0]['dn']);
-		$sector_dn_array = explode(",", $entry[0]['dn']);
-		for($i=1; $i<count($sector_dn_array); $i++)
-			$sector_dn .= $sector_dn_array[$i] . ',';
-		//Retira ultimo pipe.
-		$sector_dn = substr($sector_dn,0,(strlen($sector_dn) - 1));
-			
-		$result['context']				= $sector_dn;
-		$result['uidnumber']			= $entry[0]['uidnumber'][0];
-		$result['uid']					= $entry[0]['uid'][0];
-		$result['cn']					= $entry[0]['cn'][0];
-		$result['mail']					= $entry[0]['mail'][0];
-		$result['accountStatus']		= $entry[0]['accountstatus'][0];
-		$result['phpgwAccountVisible']	= $entry[0]['phpgwaccountvisible'][0];
-			
-		//Members
-		for ($i=0; $i<$entry[0]['mailforwardingaddress']['count']; $i++)
-		{
-			$justthese = array("cn", "uidnumber", "uid", "phpgwaccounttype", "mail");
+	function get_maillist_info($uidnumber)
+	{
+		foreach ($this->manager_contexts as $index=>$context)
+		{
+			$filter="(&(phpgwAccountType=l)(uidNumber=".$uidnumber."))";
+			$search = ldap_search($this->ldap, $context, $filter);
+			$entry = ldap_get_entries($this->ldap, $search);
+			
+			if ($entry['count'])
+			{
+				//Pega o dn do setor do usuario.
+				$entry[0]['dn'] = strtolower($entry[0]['dn']);
+				$sector_dn_array = explode(",", $entry[0]['dn']);
+				for($i=1; $i<count($sector_dn_array); $i++)
+					$sector_dn .= $sector_dn_array[$i] . ',';
+				//Retira ultimo pipe.
+				$sector_dn = substr($sector_dn,0,(strlen($sector_dn) - 1));
+			
+				$result['context']				= $sector_dn;
+				$result['uidnumber']			= $entry[0]['uidnumber'][0];
+				$result['uid']					= $entry[0]['uid'][0];
+				$result['cn']					= $entry[0]['cn'][0];
+				$result['mail']					= $entry[0]['mail'][0];
+				$result['accountStatus']		= $entry[0]['accountstatus'][0];
+				$result['phpgwAccountVisible']	= $entry[0]['phpgwaccountvisible'][0];
+			
+				//Members
+				for ($i=0; $i<$entry[0]['mailforwardingaddress']['count']; $i++)
+				{
+					$justthese = array("cn", "uidnumber", "uid", "phpgwaccounttype", "mail");
 				
-			// Montagem dinamica do filtro
-			$filter="(&(|(phpgwAccountType=u)(phpgwAccountType=l))(|";
-			for ($k=0; (($k<10) && ($i<$entry[0]['mailforwardingaddress']['count'])); $k++)
-			{
-				$filter .= "(mail=".$entry[0]['mailforwardingaddress'][$i].")";
-				$i++;
-			}
-			$i--;
-			$filter .= "))";
+					// Montagem dinamica do filtro
+					$filter="(&(|(phpgwAccountType=u)(phpgwAccountType=l))(|";
+					for ($k=0; (($k<10) && ($i<$entry[0]['mailforwardingaddress']['count'])); $k++)
+					{
+						$filter .= "(mail=".$entry[0]['mailforwardingaddress'][$i].")";
+						$i++;
+					}
+					$i--;
+					$filter .= "))";
 				
-			$search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
-			$user_entry = ldap_get_entries($this->ldap, $search);
+					$search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
+					$user_entry = ldap_get_entries($this->ldap, $search);
 				
-			for ($j=0; $j<$user_entry['count']; $j++)
-			{
-				$result['members_info'][$user_entry[$j]['uidnumber'][0]]['uid'] = $user_entry[$j]['uid'][0];
-				$result['members_info'][$user_entry[$j]['uidnumber'][0]]['cn'] = $user_entry[$j]['cn'][0];
-				$result['members_info'][$user_entry[$j]['uidnumber'][0]]['type'] = $user_entry[$j]['phpgwaccounttype'][0];
-				$result['members_info'][$user_entry[$j]['uidnumber'][0]]['mail'] = $user_entry[$j]['mail'][0];
-				$result['members'][] = $user_entry[$j]['uidnumber'][0];
-			}
-		}
-
-		// Retira o count do array
-		array_shift($entry[0]['mailforwardingaddress']);
-
-		// Checamos e-mails que não fazem parte do expresso.
-		// Criamos um array temporario
-		$tmp_array = array();
-		foreach ($result['members_info'] as $uid => $user_data)
-		{
-			$tmp_array[] = $user_data['mail'];
-		}
-
-		// Vemos a diferença
-		$array_diff = array_diff($entry[0]['mailforwardingaddress'], $tmp_array);
-
-		// Incluimos no resultado
-		foreach ($array_diff as $index=>$mailforwardingaddress)
-		{
-			$result['members_info'][$mailforwardingaddress]['uid'] = $mailforwardingaddress;
-			$result['members_info'][$mailforwardingaddress]['cn'] = 'E-Mail nao encontrado';
-			$result['members_info'][$mailforwardingaddress]['mailforwardingaddress'] = $mailforwardingaddress;
-			$result['members'][] = $mailforwardingaddress;
-		}
-		return $result;	
+					for ($j=0; $j<$user_entry['count']; $j++)
+					{
+						$result['members_info'][$user_entry[$j]['uidnumber'][0]]['uid'] = $user_entry[$j]['uid'][0];
+						$result['members_info'][$user_entry[$j]['uidnumber'][0]]['cn'] = $user_entry[$j]['cn'][0];
+						$result['members_info'][$user_entry[$j]['uidnumber'][0]]['type'] = $user_entry[$j]['phpgwaccounttype'][0];
+						$result['members_info'][$user_entry[$j]['uidnumber'][0]]['mail'] = $user_entry[$j]['mail'][0];
+						$result['members'][] = $user_entry[$j]['uidnumber'][0];
+					}
+				}
+
+				// Retira o count do array
+				array_shift($entry[0]['mailforwardingaddress']);
+
+				// Checamos e-mails que não fazem parte do expresso.
+				// Criamos um array temporario
+				$tmp_array = array();
+				foreach ($result['members_info'] as $uid => $user_data)
+				{
+					$tmp_array[] = $user_data['mail'];
+				}
+
+				// Vemos a diferença
+				$array_diff = array_diff($entry[0]['mailforwardingaddress'], $tmp_array);
+
+				// Incluimos no resultado
+				foreach ($array_diff as $index=>$mailforwardingaddress)
+				{
+					$result['members_info'][$mailforwardingaddress]['uid'] = $mailforwardingaddress;
+					$result['members_info'][$mailforwardingaddress]['cn'] = 'E-Mail nao encontrado';
+					$result['members_info'][$mailforwardingaddress]['mailforwardingaddress'] = $mailforwardingaddress;
+					$result['members'][] = $mailforwardingaddress;
+				}
+				return $result;
+			}
+		}
 	}	
 
-	function get_maillist_scl_info($uidnumber, $context)
-	{
-		$filter="(&(phpgwAccountType=l)(uidNumber=".$uidnumber."))";
-		$search = ldap_search($this->ldap, $context, $filter);
-		$entry = ldap_get_entries($this->ldap, $search);
-
-		//Pega o dn do setor do usuario.
-		$entry[0]['dn'] = strtolower($entry[0]['dn']);
-		$sector_dn_array = explode(",", $entry[0]['dn']);
-		for($i=1; $i<count($sector_dn_array); $i++)
-			$sector_dn .= $sector_dn_array[$i] . ',';
-		//Retira ultimo pipe.
-		$sector_dn = substr($sector_dn,0,(strlen($sector_dn) - 1));
-		
-		$result['dn']						= $entry[0]['dn'];
-		$result['context']					= $sector_dn;
-		$result['uidnumber']				= $entry[0]['uidnumber'][0];
-		$result['uid']						= $entry[0]['uid'][0];
-		$result['cn']						= $entry[0]['cn'][0];
-		$result['mail']						= $entry[0]['mail'][0];
-		$result['accountStatus']			= $entry[0]['accountstatus'][0];
-		$result['phpgwAccountVisible']		= $entry[0]['phpgwaccountvisible'][0];
-		$result['accountRestrictive']		= $entry[0]['accountrestrictive'][0];
-		$result['participantCanSendMail']	= $entry[0]['participantcansendmail'][0];
-		
-		//Senders
-		for ($i=0; $i<$entry[0]['mailsenderaddress']['count']; $i++)
-		{
-			$justthese = array("cn", "uidnumber", "uid", "mail");
-			$filter="(&(phpgwAccountType=u)(mail=".$entry[0]['mailsenderaddress'][$i]."))";
-			$search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
-			$user_entry = ldap_get_entries($this->ldap, $search);
-			
-			$result['senders_info'][$user_entry[0]['uidnumber'][0]]['uid'] = $user_entry[0]['uid'][0];
-			$result['senders_info'][$user_entry[0]['uidnumber'][0]]['cn'] = $user_entry[0]['cn'][0];
-			$result['senders_info'][$user_entry[0]['uidnumber'][0]]['mail'] = $user_entry[0]['mail'][0];
-			$result['members'][] = $user_entry[0]['uidnumber'][0];
-		}
-
-		return $result;
+	function get_maillist_scl_info($uidnumber)
+	{
+		foreach ($this->manager_contexts as $index=>$context)
+		{
+			$filter="(&(phpgwAccountType=l)(uidNumber=$uidnumber))";
+			$search = ldap_search($this->ldap, $context, $filter);
+			$entry = ldap_get_entries($this->ldap, $search);
+
+			if ($entry['count'])
+			{
+				//Pega o dn do setor do usuario.
+				$entry[0]['dn'] = strtolower($entry[0]['dn']);
+				$sector_dn_array = explode(",", $entry[0]['dn']);
+				for($i=1; $i<count($sector_dn_array); $i++)
+					$sector_dn .= $sector_dn_array[$i] . ',';
+				//Retira ultimo pipe.
+				$sector_dn = substr($sector_dn,0,(strlen($sector_dn) - 1));
+		
+				$result['dn']						= $entry[0]['dn'];
+				$result['context']					= $sector_dn;
+				$result['uidnumber']				= $entry[0]['uidnumber'][0];
+				$result['uid']						= $entry[0]['uid'][0];
+				$result['cn']						= $entry[0]['cn'][0];
+				$result['mail']						= $entry[0]['mail'][0];
+				$result['accountStatus']			= $entry[0]['accountstatus'][0];
+				$result['phpgwAccountVisible']		= $entry[0]['phpgwaccountvisible'][0];
+				$result['accountRestrictive']		= $entry[0]['accountrestrictive'][0];
+				$result['participantCanSendMail']	= $entry[0]['participantcansendmail'][0];
+		
+				//Senders
+				for ($i=0; $i<$entry[0]['mailsenderaddress']['count']; $i++)
+				{
+					$justthese = array("cn", "uidnumber", "uid", "mail");
+					$filter="(&(phpgwAccountType=u)(mail=".$entry[0]['mailsenderaddress'][$i]."))";
+					$search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
+					$user_entry = ldap_get_entries($this->ldap, $search);
+			
+					$result['senders_info'][$user_entry[0]['uidnumber'][0]]['uid'] = $user_entry[0]['uid'][0];
+					$result['senders_info'][$user_entry[0]['uidnumber'][0]]['cn'] = $user_entry[0]['cn'][0];
+					$result['senders_info'][$user_entry[0]['uidnumber'][0]]['mail'] = $user_entry[0]['mail'][0];
+					$result['members'][] = $user_entry[0]['uidnumber'][0];
+				}
+				return $result;
+			}
+		}
 	}	
 
@@ -909,5 +1083,5 @@
 	}
 
-	function gidnumbers2cn($gidnumbers, $context)
+	function gidnumbers2cn($gidnumbers)
 	{
 		$result = array();
@@ -927,10 +1101,16 @@
 					$result['groups_info'][$i]['cn'] = $entry[0]['cn'][0];
 				$result['groups_info'][$i]['gidnumber'] = $gidnumber;
-				
-				if (!strpos(strtolower($entry[0]['dn']), strtolower($context)))
-					$result['groups_info'][$i]['group_disabled'] = 'true';
-				else
-					$result['groups_info'][$i]['group_disabled'] = 'false';
-				
+			
+				/* o gerente pode excluir um grupo de um usuario onde este grupo esta em outra OU ? */
+				/* é o mesmo que o manager editar um grupo de outra OU */
+				$result['groups_info'][$i]['group_disabled'] = 'true';
+				foreach ($this->manager_contexts as $index=>$context)
+				{
+					if (strpos(strtolower($entry[0]['dn']), strtolower($context)))
+					{
+						$result['groups_info'][$i]['group_disabled'] = 'false';
+					}
+				}
+
 				$i++;
 			}
@@ -1025,4 +1205,21 @@
 	function delete_user($user_info)
 	{
+		// Verifica acesso do gerente (OU) ao tentar deletar um usuário.
+		$manager_access = false;
+		foreach ($this->manager_contexts as $index=>$context)
+		{
+			if ( (strpos(strtolower($user_info['context']), strtolower($context))) || (strtolower($user_info['context']) == strtolower($context)) )
+			{
+				$manager_access = true;
+				break;
+			}
+		}
+		if (!$manager_access)
+		{
+			$return['status'] = false;
+			$return['msg'] .= 'Você não tem acesso para deletar este usuário.';
+			return $return;
+		}
+		
 		$return['status'] = true;
 		
@@ -1120,4 +1317,18 @@
 	}
 
+	function check_access_to_renamed($uid)
+	{
+		$justthese = array("dn");
+		$filter="(&(phpgwAccountType=u)(uid=$uid))";
+		
+		foreach ($this->manager_contexts as $index=>$context)
+		{
+			$search = ldap_search($this->ldap, $context, $filter, $justthese);
+			$entry = ldap_get_entries($this->ldap, $search);
+			if ($entry['count'])
+				return true;
+		}
+	    return false;
+	}
 	
 	function rename_uid($uid, $new_uid)
@@ -1191,61 +1402,19 @@
 		return $return;
 	}
-/*
-	function rename_departmentnumber($old_dp, $new_dp)
-	{
-		$return['status'] = true;
-		
-		$justthese = array("dn");
-		$filter="(&(phpgwAccountType=u)(departmentnumber=".$old_dp."))";
-		$search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
-	    $entry = ldap_get_entries($this->ldap, $search);
-		
-	    for ($i=0; $i<=$entry['count']; $i++)
-	    {
-	    	$dn = strtolower($entry[$i]['dn']);
-	    	$ldap_mod_replace = array();
-	    	$ldap_mod_replace['departmentnumber'] = $new_dp;
-	    	@ldap_mod_replace ( $this->ldap, $dn,  $ldap_mod_replace);
-			//if (!@ldap_mod_replace ( $this->ldap, $dn,  $ldap_mod_replace))
-			//{
-			//	$return['status'] = false;
-			//	$return['msg'] .= 'Erro em ldap_funcitons->rename_departmentnumber: ' . ldap_error($this->ldap);
-			//}
-	    }
-		return $return;
-	}
-*/
-/*
-	function get_sambadomains($context)
-	{
-		$return['status'] = true;
-		$return['sambaDomains'] = array();
-		
-		$justthese = array("sambaSID","sambaDomainName");
-		$filter="(objectClass=sambaDomain)";
-		$search = ldap_search($this->ldap, $context, $filter, $justthese);
-	    $entry = ldap_get_entries($this->ldap, $search);
-		
-	    for ($i=0; $i<$entry['count']; $i++)
-	    {
-			$return['sambaDomains'][$i]['samba_domain_sid'] = $entry[$i]['sambasid'][0];
-			$return['sambaDomains'][$i]['samba_domain_name'] = $entry[$i]['sambadomainname'][0];
-			$return['sambaDomains'][$i]['samba_domain_dn'] = $entry[$i]['dn'];
-	    }
-	    
-		return $return;
-	}
-*/
-	function exist_sambadomains($context, $sambaDomainName)
+	
+	function exist_sambadomains($contexts, $sambaDomainName)
 	{
 		$justthese = array("dn");
 		$filter="(&(objectClass=sambaDomain)(sambaDomainName=$sambaDomainName))";
-		$search = ldap_search($this->ldap, $context, $filter, $justthese);
-	    $entry = ldap_get_entries($this->ldap, $search);
+		
+		foreach ($contexts as $index=>$context)
+		{
+			$search = ldap_search($this->ldap, $context, $filter, $justthese);
+		    $entry = ldap_get_entries($this->ldap, $search);
 	    
-		if ($entry['count'])
-			return true;
-		else
-			return false;
+			if ($entry['count'])
+				return true;
+		}
+		return false;
 	}
 	
