Index: trunk/expressoMail1_2/inc/class.ldap_functions.inc.php
===================================================================
--- trunk/expressoMail1_2/inc/class.ldap_functions.inc.php	(revision 381)
+++ trunk/expressoMail1_2/inc/class.ldap_functions.inc.php	(revision 387)
@@ -1,3 +1,3 @@
-<?php			
+<?php
 include_once("class.imap_functions.inc.php");
 include_once("class.functions.inc.php");
@@ -7,5 +7,5 @@
 	@ldap_bind($ldap_connection, $_SESSION['phpgw_info']['expressomail']['ldap_server']['acc'],$_SESSION['phpgw_info']['expressomail']['ldap_server']['pw']);
 }
-			
+
 class ldap_functions
 {
@@ -21,7 +21,7 @@
 		@include("../contactcenter/setup/external_catalogs.inc.php");
 		$this->external_srcs= $external_srcs;
-		$this->max_result 	= 200;		
-		$this->functions = new functions();		
-	}	
+		$this->max_result 	= 200;
+		$this->functions = new functions();
+	}
 	// Using ContactCenter configuration.
 	function ldapConnect($refer = false,$catalog = 0){
@@ -39,7 +39,7 @@
 			$this->bind_dn = $_SESSION['phpgw_info']['expressomail']['ldap_server']['acc'];
 			$this->bind_dn_pw = $_SESSION['phpgw_info']['expressomail']['ldap_server']['pw'];
-			$this->branch = 'ou';				
-		}
-		
+			$this->branch = 'ou';
+		}
+
 		$this->ds = ldap_connect($this->ldap_host);
 		ldap_set_option($this->ds, LDAP_OPT_PROTOCOL_VERSION, 3);
@@ -48,5 +48,5 @@
 			ldap_set_rebind_proc($this->ds, ldapRebind);
 		}
-		@ldap_bind($this->ds,$this->bind_dn,$this->bind_dn_pw );		
+		@ldap_bind($this->ds,$this->bind_dn,$this->bind_dn_pw );
 	}
 
@@ -54,8 +54,8 @@
 	function ldapConnect2($refer = false){
 		$ds = ldap_connect($_SESSION['phpgw_info']['expressomail']['ldap_server']['host']);
-		
+
 		if (!$ds)
 			return false;
-		
+
 		ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
 		ldap_set_option($ds, LDAP_OPT_REFERRALS, $refer);
@@ -63,6 +63,6 @@
 			ldap_set_rebind_proc($ds, ldapRebind);
 		@ldap_bind($ds, $_SESSION['phpgw_info']['expressomail']['ldap_server']['acc'],$_SESSION['phpgw_info']['expressomail']['ldap_server']['pw']);
-		
-		return $ds;				
+
+		return $ds;
 	}
 
@@ -75,17 +75,17 @@
 		ldap_set_option($this->ds, LDAP_OPT_PROTOCOL_VERSION, 3);
 		ldap_set_option($this->ds, LDAP_OPT_REFERRALS, $refer);
-		ldap_bind($this->ds, $_SESSION['phpgw_info']['expressomail']['server']['ldap_root_dn'],$_SESSION['phpgw_info']['expressomail']['server']['ldap_root_pw']);				
+		ldap_bind($this->ds, $_SESSION['phpgw_info']['expressomail']['server']['ldap_root_dn'],$_SESSION['phpgw_info']['expressomail']['server']['ldap_root_pw']);
 
 	}
 
 	function quicksearch($params)
-	{	
+	{
 		include_once("class.functions.inc.php");
-		$functions = new functions;						
+		$functions = new functions;
 
 		$search_for	= utf8_encode($params['search_for']);
 		$field		= $params['field'];
 		$ID			= $params['ID'];
-		
+
 		$search_for	= explode(" ",$search_for);
 		$aux="";
@@ -105,8 +105,8 @@
 		$contacts_result['field'] = $field;
 		$contacts_result['ID'] = $ID;
-		
+
 		// follow the referral
 		$this->ldapConnect(true);
-				
+
 		if ($this->ds)
 		{
@@ -126,5 +126,5 @@
 			$count_entries = ldap_count_entries($this->ds,$sr);
 
-			// Get user org dn.			
+			// Get user org dn.
 			$user_dn = $_SESSION['phpgw_info']['expressomail']['user']['account_dn'];
 			$user_sector_dn = ldap_explode_dn ( $user_dn, false );
@@ -132,5 +132,5 @@
 			array_shift($user_sector_dn);
 			$user_sector_dn = implode(",", $user_sector_dn);
-			
+
 			// New search only on user sector
 			if ($count_entries > $this->max_result)
@@ -138,17 +138,17 @@
 				// Close old ldap conection
 				ldap_close($this->ds);
-				
+
 				// Reopen a local ldap connection, following referral
 				$this->ldapRootConnect(true);
-				
+
 				$sr= ldap_search($this->ds, $user_sector_dn, $filter, $justthese);
 				if(!$sr)
 					return null;
 				$count_entries = ldap_count_entries($this->ds,$sr);
-				
+
 				if ($count_entries > $this->max_result){
 					$return = array();
 					$return['status'] = false;
-					$return['error'] = "many results";					
+					$return['error'] = "many results";
 					return $return;
 				}
@@ -158,10 +158,10 @@
 				}
 			}
-			
+
 			$info = ldap_get_entries($this->ds, $sr);
-			
+
 			$tmp = array();
 			$tmp_users_from_user_org = array();
-			
+
 			if (!$quickSearch_only_in_userSector) {
 				$catalogsNum=count($this->external_srcs);
@@ -176,6 +176,6 @@
 						$count_entries = ldap_count_entries($this->ds,$sr);
 						$search = ldap_get_entries($this->ds, $sr);
-						for ($i=0; $i<$search["count"]; $i++) {
-							$info[] = $search[$i];
+						for ($j=0; $j<$search["count"]; $j++) {
+							$info[] = $search[$j];
 						}
 						$info["count"] = count($info)-1;
@@ -204,12 +204,12 @@
 			natcasesort($tmp_users_from_user_org);
 			natcasesort($tmp);
-			
+
 			if (($field != 'null') && ($ID != 'null'))
 			{
 				$i = 0;
-				
+
 				$tmp = array_merge($tmp, $tmp_users_from_user_org);
 				natcasesort($tmp);
-				
+
 				foreach ($tmp as $info => $cn)
 				{
@@ -242,5 +242,5 @@
 					$i++;
 				}
-				
+
 				if ($quickSearch_only_in_userSector)
 				{
@@ -293,17 +293,17 @@
 	{
 		include_once("class.functions.inc.php");
-		$functions = new functions;						
-		
+		$functions = new functions;
+
 		$contacts_result = array();
 		$contacts_result["cn"] = $cn;
 		list ($contacts_result["mail"], $contacts_result["phone"], $contacts_result["mobile"], $contacts_result["uid"], $contacts_result["jpegphoto"]) = split ('%', $info);
-					
+
 		if ($contacts_result['jpegphoto'])
 			$photo_link = '<img src="./inc/show_user_photo.php?mail='.$contacts_result['mail'].'">';
 		else
 			$photo_link = '<img src="./templates/default/images/photo.png">';
-					
+
 		$phoneUser = $contacts_result['phone'];
-					
+
 		if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['voip_enabled'] && $phoneUser) {
 			$phoneUser = '<a title="'.$this->functions->getLang("Call to Comercial Number").'" href="#" onclick="InfoContact.connectVoip(\''.$phoneUser.'\',\'com\')">'.$phoneUser.'</a>';
@@ -332,8 +332,8 @@
 	}
 
-	function get_catalogs(){		
+	function get_catalogs(){
 		$catalogs = array();
-		$catalogs[0] = $this->functions->getLang("Global Catalog");		
-		if($this->external_srcs) 
+		$catalogs[0] = $this->functions->getLang("Global Catalog");
+		if($this->external_srcs)
 			foreach ($this->external_srcs as $key => $valor ){
 			$catalogs[$key] = $valor['name'];
@@ -353,10 +353,10 @@
 			$sr = ldap_list($this->ds, $this->ldap_context, $filter, $justthese);
 			$info = ldap_get_entries($this->ds, $sr);
-			
+
 			for ($i=0; $i<$info["count"]; $i++)
 				$organizations[$i] = $info[$i]["ou"][0];
 
 			ldap_close($this->ds);
-			sort($organizations);					
+			sort($organizations);
 		return $organizations;
 			}else{
@@ -369,9 +369,9 @@
 		$this->ldapRootConnect($referral);
 		if ($this->ds) {
-			$filter="ou=*";		
+			$filter="ou=*";
 			$justthese = array("ou");
 			$sr = ldap_list($this->ds, $this->ldap_context, $filter, $justthese);
 			$info = ldap_get_entries($this->ds, $sr);
-			
+
 			for ($i=0; $i<$info["count"]; $i++)
 			{
@@ -379,7 +379,7 @@
 				$organizations[$i]['dn'] = $info[$i]["dn"];
 			}
-			
+
 			ldap_close($this->ds);
-			sort($organizations);					
+			sort($organizations);
 		}
 		return $organizations;
@@ -387,15 +387,15 @@
 
 	function catalogsearch($params)
-	{	
+	{
 		$cn	= $params['search_for'] ? "*".utf8_encode($params['search_for'])."*" : "*";
-		$max_result	  = $params['max_result'] ? $params['max_result'] : $this->max_result;		
+		$max_result	  = $params['max_result'] ? $params['max_result'] : $this->max_result;
 		$catalog = $params['catalog'];
 		$error = False;
-		
+
 		$this->ldapConnect(true,$catalog);
-		
-		$params['organization'] == 'all' ? $user_context = $this->ldap_context :$user_context = $this->branch."=".$params['organization'].",".$this->ldap_context;		
-		
-		if ($this->ds) {											
+
+		$params['organization'] == 'all' ? $user_context = $this->ldap_context :$user_context = $this->branch."=".$params['organization'].",".$this->ldap_context;
+
+		if ($this->ds) {
 			if ($catalog == 0){
 				//os atributos "employeeNumber" e "ou" foram adicionado ao vetor de busca;
@@ -409,5 +409,5 @@
 				//$user_context = $this->branch."=".$params['organization'].",".$external_srcs[$catalog]['dn'];
 			}
-			
+
 			$sr=@ldap_search($this->ds, $user_context, $filter, $justthese, 0, $max_result+1);
 			if(!$sr)
@@ -416,29 +416,29 @@
 			if ($count_entries > $max_result){
 				$info = null;
-				$error = True;					
+				$error = True;
 			}
 			else
-				$info = ldap_get_entries($this->ds, $sr);		
-			
-			ldap_close($this->ds);			
-			
+				$info = ldap_get_entries($this->ds, $sr);
+
+			ldap_close($this->ds);
+
 			$u_tmp = array();
 			$g_tmp = array();
-			
+
 			for ($i=0; $i<$info["count"]; $i++){
 				if((!$catalog==0)||(strtoupper($info[$i]["phpgwaccounttype"][0]) == 'U') && ($info[$i]["phpgwaccountvisible"][0] != '-1'))
 					//aqui eh feita a concatenacao do departamento ao cn;
 					$u_tmp[$info[$i]["mail"][0]] = utf8_decode($info[$i]["cn"][0]). '%' . $info[$i]["ou"][0];
-				if((!$catalog==0)||(strtoupper($info[$i]["phpgwaccounttype"][0]) == 'L') && ($info[$i]["phpgwaccountvisible"][0] != '-1'))							
+				if((!$catalog==0)||(strtoupper($info[$i]["phpgwaccounttype"][0]) == 'L') && ($info[$i]["phpgwaccountvisible"][0] != '-1'))
 					$g_tmp[$info[$i]["mail"][0]] = utf8_decode($info[$i]["cn"][0]);
-			}										
-			
+			}
+
 			natcasesort($u_tmp);
 			natcasesort($g_tmp);
-			
+
 			$i = 0;
-			$users = array();			
-			
-			foreach ($u_tmp as $mail => $cn){				
+			$users = array();
+
+			foreach ($u_tmp as $mail => $cn){
 
 				$tmp = explode("%", $cn); //explode o cn pelo caracter "%" e joga em $tmp;
@@ -449,39 +449,39 @@
 			}
 			unset($u_tmp);
-			
-			$i = 0;			
+
+			$i = 0;
 			$groups = array();
-			
-			foreach ($g_tmp as $mail => $cn){				
+
+			foreach ($g_tmp as $mail => $cn){
 				$groups[$i++] = array("name" => $cn, "email" => $mail);
 			}
 			unset($g_tmp);
-			
+
 			return  array('users' => $users, 'groups' => $groups, 'error' => $error);
-		}else				
+		}else
 		return null;
 	}
-	
+
 	function get_emails_ldap(){
 
 		$result['mail']= array();
-		$result['mailalter']= array();		
+		$result['mailalter']= array();
 		$user = $_SESSION['phpgw_info']['expressomail']['user']['account_lid'];
 		$this->ldapRootConnect(false);
 		if ($this->ds) {
-			$filter="uid=".$user;		
+			$filter="uid=".$user;
 			$justthese = array("mail","mailAlternateAddress");
 			$sr = ldap_search($this->ds,$this->ldap_context, $filter, $justthese);
 			$ent = ldap_get_entries($this->ds, $sr);
 			ldap_close($this->ds);
-			
+
 			for ($i=0; $i<$ent["count"]; $i++){
 				$result['mail'][] = $ent[$i]["mail"][0];
-				$result['mailalter'][] = $ent[$i]["mailalternateaddress"][0];				
+				$result['mailalter'][] = $ent[$i]["mailalternateaddress"][0];
 			}
 		}
 		return $result;
 	}
-	
+
 	//Busca usuarios de um contexto e ja retorna as options do select;
 	function get_available_users($params)
@@ -503,5 +503,5 @@
             $info = ldap_get_entries($this->ds, $sr);
             for ($i=0; $i<$info["count"]; $i++)
-                $groups[$uids=$info[$i]["gidnumber"][0]] = Array('name'    =>    $uids=$info[$i]["cn"][0], 'type'    =>    g);            
+                $groups[$uids=$info[$i]["gidnumber"][0]] = Array('name'    =>    $uids=$info[$i]["cn"][0], 'type'    =>    g);
             $justthese = array("phpgwaccountvisible","uidNumber","cn");
             if ($params['type'] == 'search')
@@ -519,7 +519,7 @@
         }
         ldap_close($this->ds);
-            
+
         @asort($users);
-        @reset($users);    
+        @reset($users);
         @asort($groups);
         @reset($groups);
@@ -544,14 +544,14 @@
 	{
 		$this->ldapRootConnect();
-		
+
 		$context= $params['context'];
 		$justthese = array("cn", "uid", "cn");
 		$filter = "(&(phpgwaccounttype=u)(!(phpgwaccountvisible=-1)))";
 
-	    if ($this->ds) 
+	    if ($this->ds)
 	    {
 			$sr=ldap_search($this->ds, $context, $filter, $justthese);
 			$entries = ldap_get_entries($this->ds, $sr);
-			
+
 			for ($i=0; $i<$entries["count"]; $i++){
 				if($_SESSION['phpgw_info']['expressomail']['user']['account_lid'] != $entries[$i]["uid"][0]){
@@ -559,10 +559,10 @@
 				}
 			}
-			
+
 			natcasesort($u_tmp);
 
 			$i = 0;
 			$users = array();
-			
+
 			if (count($u_tmp))
 			{
@@ -578,12 +578,12 @@
 		}
 	}
-	
+
 	function uid2cn($uid)
-	{	
+	{
 		// do not follow the referral
 		$this->ldapRootConnect(false);
 		if ($this->ds)
 		{
-			$filter="(&(phpgwAccountType=u)(uid=$uid))";		
+			$filter="(&(phpgwAccountType=u)(uid=$uid))";
 			$justthese = array("cn");
 			$sr=@ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
@@ -595,12 +595,12 @@
 		return false;
 	}
-	function getSharedUsersFrom($params){						
+	function getSharedUsersFrom($params){
 		$filter = '';
 		$i = 0;
 		if($params['uids']) {
-			$uids = explode(";",$params['uids']);		
-			$this->imap = new imap_functions();		
+			$uids = explode(";",$params['uids']);
+			$this->imap = new imap_functions();
 			foreach($uids as $index => $uid){
-				$params = array();		
+				$params = array();
 				$acl = $this->imap->getacltouser($uid);
 				if(preg_match("/a/",$acl))
@@ -608,6 +608,6 @@
 			}
 		}
-		
-		$this->ldapRootConnect(false);		
+
+		$this->ldapRootConnect(false);
 		if ($this->ds) {
 			$justthese = array("cn","mail");
@@ -615,14 +615,14 @@
 				$filter="(&(phpgwAccountType=u)(|$filter))";
 				$sr		=	ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
-				ldap_sort($this->ds,$sr,"cn");			
-				$info 	= 	ldap_get_entries($this->ds, $sr);										
+				ldap_sort($this->ds,$sr,"cn");
+				$info 	= 	ldap_get_entries($this->ds, $sr);
 				for ($i = 0;$i < $info["count"]; $i++)
 					$info[$i]['cn'][0] = utf8_decode($info[$i]['cn'][0]);
 			}
 			$info['myname'] = $_SESSION['phpgw_info']['expressomail']['user']['fullname'];
-			
+
 			//Find institucional_account.
 			$filter="(&(phpgwAccountType=i)(mailForwardingAddress=".$_SESSION['phpgw_info']['expressomail']['user']['email']."))";
-			$sr	= ldap_search($this->ds, $this->ldap_context, $filter, $justthese);			
+			$sr	= ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
 			if(!ldap_count_entries($this->ds,$sr))
 				return $info;
@@ -646,13 +646,13 @@
 		$ds = $this->ldapConnect2(true);
 		if ($ds)
-		{			
+		{
 			$sr=@ldap_search($ds, $ldap_context, $filter, $justthese);
-			
+
 			if (!$sr)
 				return null;
-			
+
 			$entry = ldap_first_entry($ds, $sr);
-			
-			if($entry) {						
+
+			if($entry) {
 				$obj =  array("cn" => utf8_decode(current(ldap_get_values($ds, $entry, "cn"))),
 						  "email" => $params['email'],
