Index: /trunk/expressoMail1_2/inc/class.db_functions.inc.php
===================================================================
--- /trunk/expressoMail1_2/inc/class.db_functions.inc.php	(revision 673)
+++ /trunk/expressoMail1_2/inc/class.db_functions.inc.php	(revision 757)
@@ -10,4 +10,5 @@
 	var $db;
 	var $user_id;
+	var $related_ids; 
 	
 	function db_functions(){
@@ -30,19 +31,6 @@
 		$result = array();
 		$stringDropDownContacts = '';		
-		// Traz primeiro os usuarios que compartilharam com o usuario logado:
-		$query = 'select id_related from phpgw_cc_contact_rels where id_contact='.$this -> user_id.' and id_typeof_contact_relation=1';
-		// Somente do usuario logado
-		$query_related = 'A.id_owner ='.$this -> user_id;
-		if (!$this->db->query($query))
-        	return null;			
-		while($this->db->next_record()){
-			$row = $this->db->row();
-			$result[] = $row['id_related'];
-		}
-		$ids_related = implode(",",$result);
-
-		// Se houver contatos compartilhados então altera a query.
-		if($ids_related)		
-			$query_related = '(A.id_owner ='.$this -> user_id.' or A.id_owner in ('.$ids_related.'))';
+		
+		$query_related = $this->get_query_related('A.id_owner'); // field name for owner
 			
 		// Traz os contatos pessoais e compartilhados
@@ -50,7 +38,7 @@
 			'phpgw_cc_contact_conns B, phpgw_cc_connections C where '.
 			'A.id_contact = B.id_contact and B.id_connection = C.id_connection '.
-			'and B.id_typeof_contact_connection = 1 and '.$query_related.' group by '. 
-			'A.names_ordered,C.connection_value	order by A.names_ordered';
-
+			'and B.id_typeof_contact_connection = 1 and ('.$query_related.') group by '. 
+			'A.names_ordered,C.connection_value	order by lower(A.names_ordered)';
+		
         if (!$this->db->query($query))
         	return null;
@@ -71,5 +59,26 @@
 		return $stringDropDownContacts;
 	}
-	
+	// Get Related Ids for sharing contacts or groups.
+	function get_query_related($field_name){		
+		$query_related = $field_name .'='.$this -> user_id;
+		// Only at first time, it gets all related ids...
+		if(!$this->related_ids) {
+			$query = 'select id_related from phpgw_cc_contact_rels where id_contact='.$this -> user_id.' and id_typeof_contact_relation=1';		
+			if (!$this->db->query($query)){
+    	    	return $query_related;
+			}
+			
+			while($this->db->next_record()){
+				$row = $this->db->row();
+				$result[] = $row['id_related'];
+			}
+			if($result)
+				$this->related_ids = implode(",",$result);
+		}
+		if($this->related_ids)
+			$query_related .= ' or '.$field_name.' in ('.$this->related_ids.')';
+		
+		return $query_related;
+	}
 	function get_cc_groups() 
 	{
@@ -77,5 +86,7 @@
 		$stringDropDownContacts = '';			
 		$result = array();
-		$query = 'select title, short_name from phpgw_cc_groups where owner ='.$this -> user_id.' order by title';
+		$query_related = $this->get_query_related('owner'); // field name for 'owner'		
+		$query = 'select title, short_name, owner from phpgw_cc_groups where '.$query_related.' order by lower(title)';
+
 		// Executa a query 
 		if (!$this->db->query($query))
@@ -84,15 +95,30 @@
 		while($this->db->next_record())
 			$result[] = $this->db->row();
-		// Se houver grupos ....
+
+		// Se houver grupos ....				
 		if (count($result) != 0) 
-		{	
-			foreach($result as $group)
-				$stringDropDownContacts .=  $group['title']. ';' . $group['short_name'] . ',';
+		{
+			// Create Ldap Object, if exists related Ids for sharing groups.
+			if($this->related_ids){
+				$_SESSION['phpgw_info']['expressomail']['user']['cc_related_ids']= array();
+				include_once("class.ldap_functions.inc.php");
+				$ldap = new ldap_functions();
+			}
+			foreach($result as $group){
+				// Searching uid (LDAP), if exists related Ids for sharing groups.
+				// Save into user session. It will used before send mail (verify permission).
+				if(!$_SESSION['phpgw_info']['expressomail']['user']['cc_related_ids'][$group['owner']] && $ldap){					
+					$_SESSION['phpgw_info']['expressomail']['user']['cc_related_ids'][$group['owner']] = $ldap -> uidnumber2uid($group['owner']);
+				}
+				if($this->user_id != $group['owner'])
+					$owneruid = "::".$_SESSION['phpgw_info']['expressomail']['user']['cc_related_ids'][$group['owner']];
+
+				$stringDropDownContacts .=  $group['title']. ';' . ($group['short_name'].$owneruid) . ',';
+			}
 			//Retira ultima virgula.
 			$stringDropDownContacts = substr($stringDropDownContacts,0,(strlen($stringDropDownContacts) - 1));
 		}
 		else
-			return null;
-			
+			return null;		
 		return $stringDropDownContacts;
 	}
@@ -100,4 +126,10 @@
 	function getContactsByGroupAlias($alias)
 	{
+		list($alias,$uid) = explode("::",$alias);		
+		$cc_related_ids = $_SESSION['phpgw_info']['expressomail']['user']['cc_related_ids'];		
+		// Explode personal group, If exists related ids (the user has permission to send email).
+		if(is_array($cc_related_ids) && $uid){
+			$owner =  array_search($uid,$cc_related_ids);			
+		}
 		
 		$query = "select C.id_connection, A.names_ordered, C.connection_value from phpgw_cc_contact A, ".
@@ -105,8 +137,7 @@
 		"A.id_contact = B.id_contact and B.id_connection = C.id_connection ".
 		"and B.id_typeof_contact_connection = 1 and ".
-		"A.id_owner =".$this -> user_id." and ".			
+		"A.id_owner =".($owner ? $owner : $this->user_id)." and ".			
 		"D.id_group = E.id_group and ".
-		"D.id_connection = C.id_connection and E.short_name = '".$alias.
-		"' order by A.names_ordered";
+		"D.id_connection = C.id_connection and E.short_name = '".$alias."'";
 
 		if (!$this->db->query($query))
@@ -136,5 +167,6 @@
 
 				$alias = substr($array_addrs[$i], strpos($array_addrs[$i],'<'), strpos($array_addrs[$i],'>'));				
-		 		$alias = str_replace('<','', str_replace('>','',$alias));		 		        	           		
+		 		$alias = str_replace('<','', str_replace('>','',$alias));
+		 		 		        	           		
 		 		$arrayContacts = $this -> getContactsByGroupAlias($alias);
 
Index: /trunk/expressoMail1_2/inc/class.ldap_functions.inc.php
===================================================================
--- /trunk/expressoMail1_2/inc/class.ldap_functions.inc.php	(revision 657)
+++ /trunk/expressoMail1_2/inc/class.ldap_functions.inc.php	(revision 757)
@@ -612,4 +612,20 @@
 		return false;
 	}
+	function uidnumber2uid($uidnumber)
+	{
+		// do not follow the referral
+		$this->ldapRootConnect(false);
+		if ($this->ds)
+		{
+			$filter="(&(phpgwAccountType=u)(uidnumber=$uidnumber))";
+			$justthese = array("uid");
+			$sr=@ldap_search($this->ds, $this->ldap_context, $filter, $justthese);
+			if(!$sr)
+				return false;
+			$info = ldap_get_entries($this->ds, $sr);
+			return $info[0]["uid"][0];
+		}
+		return false;
+	}
 	function getSharedUsersFrom($params){
 		$filter = '';
