Index: trunk/contactcenter/inc/class.bo_global_ldap_catalog.inc.php
===================================================================
--- trunk/contactcenter/inc/class.bo_global_ldap_catalog.inc.php	(revision 284)
+++ trunk/contactcenter/inc/class.bo_global_ldap_catalog.inc.php	(revision 285)
@@ -16,16 +16,14 @@
 	*/
 	include_once('class.abo_catalog.inc.php');
-
+	
 	class bo_global_ldap_catalog extends abo_catalog
 	{
 		var $ldap;
-
+	
 		var $src_info;
-
+		
 		var $trans_table;
-
 		// used to determine if a catalog is external
 		var $external;
-
 		var $fields = array(
 			'id_contact'    => true,
@@ -43,8 +41,5 @@
 			'pgp_key'       => true,
 			'notes'         => true,
-			'department'    => true,
-
-			'empNumber'     => true,
-
+			
 			/* Array fields */
 			'companies'     => true,
@@ -53,7 +48,7 @@
 			'connections'   => true
 		);
-
+		
 		/*
-
+		
 			@function global_ldap_catalog
 			@abstract Constructor
@@ -64,18 +59,15 @@
 			@param string $context Ldap bind DN
 			@param integer $external 0 = internal catalog, 1 = external catalog
-
 		*/
 		function bo_global_ldap_catalog ( $id_source, $context, $external = 0 )
 		{
-
 			$this->external = $external;
-
 			if (!function_exists('ldap_search'))
 			{
 				exit('PHP LDAP support Unavailable!');
 			}
-
+			
 			$this->ldap = CreateObject('contactcenter.bo_ldap_manager');
-
+			
 			if ($this->external)
 			{
@@ -100,11 +92,11 @@
 			}
 			else
-			{
+			{	
 				$this->trans_table = $this->ldap->get_ldap_fields_association($id_source);
 			}
 		}
-
+		
 		/*
-
+		
 			@function find
 			@abstract Searches the LDAP directory for the specified fields with
@@ -112,5 +104,5 @@
 				that matches the rules.
 			@author Raphael Derosso Pereira
-
+			
 			@param array $what The fields to be taken
 			@param array $rules The rules to be match. See class.abo_catalog.inc.php
@@ -121,12 +113,12 @@
 					'offset' => (integer) [NOT IMPLEMENTED]
 				)
-
+		
 		*/
 		function find($what, $rules=false, $other=false)
 		{
 			$restric_fields = $this->get_restrictions_without_branch($rules);
-
+			
 			$trans_f = $this->translate_fields($what, $restric_fields);
-
+			
 			foreach($trans_f as $orig => $field_a)
 			{
@@ -136,30 +128,30 @@
 				}
 			}
-
+			
 			$fields = array_unique($fields);
-
+			
 			$filter = $this->process_restrictions($rules, $trans_f);
 
 			if(strstr($this->src_info['context'], "ldap://")) {
 				$refer_context 	= str_replace("??base","",preg_replace('!^(ldap://[^/]+)/(.*$)!', '\\2', $this->src_info['context']));
-				$host	= preg_replace('!^(ldap://[^/]+)/.*$!', '\\1', $this->src_info['context']);
+				$host	= preg_replace('!^(ldap://[^/]+)/.*$!', '\\1', $this->src_info['context']);				
 				$this->src_info['host'] = $host;
-				$this->src_info['acc'] = '';
-				$this->src_info['pw']  = '';
-			}
-
-			$ldap = $GLOBALS['phpgw']->common->ldapConnect($this->src_info['host'], $this->src_info['acc'], $this->src_info['pw'], true);
-
-			if (!$ldap)
-			{
+			}			
+
+			if($refer_context || "ou" === strtolower(substr($this->src_info['context'], 0, 2)))
+			{		
+				$followReferral = false;			
+			}
+			elseif(trim($rules[4]['value']) != "inetOrgPerson")	
+			{			
+				$followReferral = true;
+			}
+			else
+			{				
 				return false;
 			}
-
-			if (!$this->external)
-			{
-				$fields[]  ='phpgwAccountVisible';
-			}
-
-			$result_r = @ldap_search($ldap ,($refer_context  ? $refer_context  : $this->src_info['context']), $filter, $fields, 0);
+			
+			$ldap = $GLOBALS['phpgw']->common->ldapConnect($this->src_info['host'], $this->src_info['acc'], $this->src_info['pw'], $followReferral);
+			$result_r = ldap_search($ldap ,($refer_context  ? $refer_context  : $this->src_info['context']), $filter, $fields, 0);
 
 			if (!$result_r)
@@ -167,5 +159,5 @@
 				return false;
 			}
-
+			
 			if ($other['order'])
 			{
@@ -181,7 +173,7 @@
 				}
 			}
-
+			
 			$result_u = ldap_get_entries($ldap, $result_r);
-
+			
 			$i = 0;
 			foreach ($result_u as $index => $result_p)
@@ -191,5 +183,5 @@
 					continue;
 				}
-
+				
 				foreach ($trans_f as $orig => $trans)
 				{
@@ -209,23 +201,23 @@
 				$i++;
 			}
-
+			
 			return $return;
 		}
-
+		
 		/*
-
+		
 			@function translate_fields
 			@abstract Return the LDAP objectClass fields that corresponds to the
 				specified parameter fields
 			@author Raphael Derosso Pereira
-
+			
 			@param array $fields The fields in the standard ContactCenter format
 			@param array $rules The rules
-
+		
 		*/
 		function translate_fields ( $fields, &$restric_fields )
 		{
 			$return = array();
-
+			
 			$i = 0;
 			foreach ($fields as $field)
@@ -235,13 +227,12 @@
 					continue;
 				}
-
+				
 				if (!is_array($this->trans_table[$field]))
 				{
 					$reference = $this->trans_table[$field];
-
+					
 					reset($restric_fields);
 					while(list(,$field_r) = each($restric_fields))
 					{
-
 						if ($field_r['field'] === $reference and array_key_exists($field_r['value'], $this->trans_table[$reference]))
 						{
@@ -252,5 +243,4 @@
 				else
 				{
-
 					if (!is_array($return[$field]))
 					{
@@ -263,22 +253,22 @@
 				}
 			}
-
+			
 			if (count($return))
 			{
 				return $return;
 			}
-
+			
 			return false;
 		}
-
+		
 		/*
-
+		
 			@function process_restrictions
 			@abstract Returns a LDAP filter string that corresponds to the
 				specified restriction rules
 			@author Raphael Derosso Pereira
-
+			
 			@param string $rules The restriction rules
-
+		
 		*/
 		function process_restrictions( $rules, &$trans_table, $join_type='&' )
@@ -288,5 +278,5 @@
 				return null;
 			}
-
+			
 			foreach($rules as $rule_i => $rule)
 			{
@@ -300,13 +290,13 @@
 								$join = '|';
 								break;
-
+								
 							case 'AND':
 								$join = '&';
 								break;
-
+								
 							case 'NOT':
 								$join = '!';
 								break;
-
+								
 							default:
 								$join = $join_type;
@@ -314,5 +304,5 @@
 						$return_t[] = $this->process_restrictions($rule['sub_branch'], $trans_table, $join);
 						break;
-
+						
 					case '=':
 						if (array_key_exists($rule['field'], $trans_table))
@@ -325,6 +315,6 @@
 						}
 						break;
-
-					case '!=':
+					
+					case '!=':	
 						if (array_key_exists($rule['field'], $trans_table))
 						{
@@ -336,5 +326,5 @@
 						}
 						break;
-
+					
 					case '<=':
 					case '<':
@@ -348,5 +338,5 @@
 						}
 						break;
-
+					
 					case '>':
 					case '>=':
@@ -360,5 +350,5 @@
 						}
 						break;
-
+						
 					case 'NULL':
 						if (array_key_exists($rule['field'], $trans_table))
@@ -371,5 +361,5 @@
 						}
 						break;
-
+					
 					case 'IN':
 						if (array_key_exists($rule['field'], $trans_table))
@@ -391,5 +381,5 @@
 							$value_1 = strtoupper(str_replace('%', '*', $rule['value']));
 							$value_2 = strtolower($value_1);
-
+							
 							foreach($trans_table[$rule['field']] as $field)
 							{
@@ -400,10 +390,10 @@
 						}
 						break;
-
+						
 */					case 'LIKE':
 						if (array_key_exists($rule['field'], $trans_table))
 						{
 							$value = str_replace('%', '*', $rule['value']);
-
+							
 							foreach($trans_table[$rule['field']] as $field)
 							{
@@ -413,5 +403,5 @@
 						}
 						break;
-
+						
 					case 'NOT NULL':
 						if (array_key_exists($rule['field'], $trans_table))
@@ -424,5 +414,5 @@
 						}
 						break;
-
+					
 					case 'NOT IN':
 						if (array_key_exists($rule['field'], $trans_table))
@@ -444,5 +434,5 @@
 							$value_1 = strtoupper(str_replace('%', '*', $rule['value']));
 							$value_2 = strtolower($value_1);
-
+							
 							foreach($trans_table[$rule['field']] as $field)
 							{
@@ -458,5 +448,5 @@
 						{
 							$value = str_replace('%', '*', $rule['value']);
-
+							
 							foreach($trans_table[$rule['field']] as $field)
 							{
@@ -468,5 +458,5 @@
 				}
 			}
-
+			
 			if (count($return_t))
 			{
@@ -486,12 +476,12 @@
 
 		/*!
-
+		
 			@function get_restrictions_without_branch
 			@abstract Returns an array containing the restrictions ignoring the
 				branches
 			@author Raphael Derosso Pereira
-
+			
 			@param array $restrictions The restrictions
-
+		
 		*/
 		function get_restrictions_without_branch(&$restrictions)
@@ -501,7 +491,7 @@
 				return null;
 			}
-
+			
 			$fields = array();
-
+			
 			foreach ($restrictions as $restrict_data)
 			{
@@ -511,7 +501,7 @@
 						$fields = array_merge($fields, $this->get_restrictions_without_branch($restrict_data['sub_branch']));
 						break;
-
+						
 					case '=':
-					case '!=':
+					case '!=':	
 					case '<=':
 					case '<':
@@ -528,35 +518,35 @@
 						array_push($fields, $restrict_data);
 						break;
-
+						
 					default:
-						exit('Error in '.__FILE__.' on '.__LINE__.'<br>The restriction type passed was: '.$restrict_data['type']);
-				}
-			}
-
+						exit('Error in '.__FILE__.' on '.__LINE__.'<br>The restriction type passed was: '.$restrict_data['type']);					
+				}
+			}
+			
 			return $fields;
 		}
-
-
+		
+		
 		/*********************************************************************\
 		 *                        Methods to Get Data                        *
 		\*********************************************************************/
-
-
+		
+	
 		/*!
-
+		
 		 @function get_single_entry
 		 @abstract Returns all information requested about one contact
 		 @author Raphael Derosso Pereira
-
+		     
 		 @param integer $id_contact The contact ID
 		 @param array $fields The array returned by get_fields with true
 		 	on the fields to be taken.
-
+		 	
 		*/
 		function get_single_entry ( $id_contact, $fields )
 		{
-			if (!is_array($fields))
-			{
-				if (is_object($GLOBALS['phpgw']->log))
+			if (!is_array($fields)) 
+			{
+				if (is_object($GLOBALS['phpgw']->log)) 
 				{
 					$GLOBALS['phpgw']->log->message(array(
@@ -564,20 +554,20 @@
 						'line' => __LINE__,
 						'file' => __FILE__));
-
+					
 					$GLOBALS['phpgw']->log->commit();
 				}
-				else
+				else 
 				{
 					exit('Argument Error on: <br>File:'.__FILE__.'<br>Line:'.__LINE__.'<br>');
 				}
 			}
-
+			
 			$ldap = $GLOBALS['phpgw']->common->ldapConnect($this->src_info['host'],$this->src_info['acc'],$this->src_info['pw'],true);
-
+			
 			if (!$ldap)
 			{
 				return false;
 			}
-
+			
 			$resource = ldap_read($ldap, $id_contact, 'objectClass='.$this->src_info['obj']);
 			$n_entries = ldap_count_entries($ldap, $resource);
@@ -586,15 +576,15 @@
 				return false;
 			}
-
+			
 			$first_entry = ldap_first_entry($ldap, $resource);
 			$contact = ldap_get_attributes($ldap,$first_entry);
-			if($contact['jpegPhoto']){
-				$contact['jpegPhoto'] = ldap_get_values_len ($ldap, $first_entry, "jpegPhoto");
+			if($contact['jpegPhoto']){	
+				$contact['jpegPhoto'] = ldap_get_values_len ($ldap, $first_entry, "jpegPhoto"); 
 			}
 
 //			print_r($contact);
-
+			
 		//	$contact_data = $this->fields;
-
+			
 			foreach($fields as $field => $trueness)
 			{
@@ -604,5 +594,5 @@
 					continue;
 				}
-
+				
 				switch ($field)
 				{
@@ -613,5 +603,5 @@
 						$l_fields['department']    = $this->trans_table['contact.business_info.department'];
 						$l_fields['company_notes'] = $this->trans_table['contact.company.company_notes'];
-
+						
 						$contact_data['companies'] = array();
 						foreach($l_fields as $l_field => $l_value)
@@ -621,8 +611,8 @@
 								continue;
 							}
-
+							
 							$contact_data['companies']['company1'][$l_field] = utf8_decode($contact[$l_value[0]][0]);
 						}
-
+						
 						if (!(count($contact_data['companies'])))
 						{
@@ -630,5 +620,5 @@
 						}
 						break;
-
+					
 					case 'relations':
 						unset($l_fields);
@@ -637,5 +627,5 @@
 							unset($contact_data['relations']);
 						}
-
+						
 						$contact_data['relations'] = array();
 						if (!is_array($this->trans_table['contact.contact_related.names_ordered']))
@@ -645,5 +635,5 @@
 								continue;
 							}
-
+							
 							$i = 1;
 							foreach($trans as $l_type => $l_type_fields)
@@ -653,5 +643,5 @@
 									continue;
 								}
-
+								
 								$contact_data['relations']['relation'.$i]['type'] = $l_type;
 								$contact_data['relations']['relation'.$i]['names_ordered'] = utf8_decode($contact[$l_type_fields[0]][0]);
@@ -659,5 +649,5 @@
 							}
 						}
-
+						
 						if (!(count($contact_data['relations'])))
 						{
@@ -665,5 +655,5 @@
 						}
 						break;
-
+					
 					case 'addresses':
 						unset($l_fields);
@@ -694,5 +684,5 @@
 									continue;
 								}
-
+								
 								$i = 1;
 								foreach($trans as $l_type => $l_type_fields)
@@ -702,5 +692,5 @@
 										continue;
 									}
-
+									
 									$contact_data['addresses']['address'.$i]['type'] = $l_type;
 									$contact_data['addresses']['address'.$i][$l_field] = utf8_decode($contact[$l_type_fields[0]][0]);
@@ -713,5 +703,5 @@
 							}
 						}
-
+						
 						if (!(count($contact_data['addresses'])))
 						{
@@ -719,5 +709,5 @@
 						}
 						break;
-
+					
 					case 'connections':
 	                    $preferences = ExecMethod('contactcenter.ui_preferences.get_preferences');
@@ -740,5 +730,5 @@
 									continue;
 								}
-
+								
 								$i = 1;
 								foreach($trans as $l_type => $l_type_fields)
@@ -748,5 +738,5 @@
 										continue;
 									}
-
+									
 									switch ($l_type)
 									{
@@ -768,5 +758,5 @@
 							}
 						}
-
+						
 						if (!(count($contact_data['connections'])))
 						{
@@ -774,5 +764,5 @@
 						}
 						break;
-
+					
 					case 'prefix':
 						unset($l_fields);
@@ -783,8 +773,8 @@
 							continue;
 						}
-
+						
 						$contact_data['prefix'] = utf8_decode($contact[$l_fields[0]][0]);
 						break;
-
+						
 					case 'suffix':
 						unset($l_fields);
@@ -795,8 +785,8 @@
 							continue;
 						}
-
+						
 						$contact_data['suffix'] = utf8_decode($contact[$l_fields[0]][0]);
 						break;
-
+						
 					case 'status':
 						unset($l_fields);
@@ -807,8 +797,8 @@
 							continue;
 						}
-
+						
 						$contact_data['status'] = utf8_decode($contact[$l_fields[0]][0]);
 						break;
-
+						
 						case 'photo':
 						unset($l_fields);
@@ -819,7 +809,7 @@
 							continue;
 						}
-
+						
 						$contact_data['photo'] = $contact[$l_fields[0]][0];
-						break;
+						break;											
 
 					default:
@@ -831,5 +821,5 @@
 							continue;
 						}
-
+						
 						if(count($contact[$l_fields[0]]) > 1)
 						{
@@ -848,13 +838,13 @@
 				}
 			}
-
+			
 			if (!is_array($contact_data))
 			{
 				return false;
 			}
-
+			
 			return $contact_data;
 		}
-
+		//SERPRO
 		/*!
 
@@ -932,7 +922,4 @@
 							$l_fields['department']    = $this->trans_table['contact.business_info.department'];
 							$l_fields['company_notes'] = $this->trans_table['contact.company.company_notes'];
-
-							///Relaciona o array com o atributo 'employeeNumber' do RHDS
-							$l_fields['empNumber']     = $this->trans_table['contact.business_info.empNumber'];
 
 							$contact_data['companies'] = array();
@@ -1181,10 +1168,10 @@
 			return $contacts;
 		}
-
+		
 		function get_multiple_entries ( $id_contacts, $fields, $other_data = false )
 		{
 			if (!is_array($id_contacts) or !is_array($fields) or ($other_data != false and !is_array($other_data)))
 			{
-				if (is_object($GLOBALS['phpgw']->log))
+				if (is_object($GLOBALS['phpgw']->log)) 
 				{
 					$GLOBALS['phpgw']->log->message(array(
@@ -1192,5 +1179,5 @@
 						'line' => __LINE__,
 						'file' => __FILE__));
-
+					
 					$GLOBALS['phpgw']->log->commit();
 				}
@@ -1199,7 +1186,16 @@
 				}
 			}
-
-			$contacts = array();
-
+			 
+			foreach ($id_contacts as $id)
+			{
+				$contacts[$id] = $this->get_single_entry($id,$fields);
+			}
+			
+			return $contacts;
+
+			// SERPRO
+						
+			/*$contacts = array();
+	
 			if ($other_data)
 			{
@@ -1211,7 +1207,21 @@
 			//$teste = $this->get_all_entries($filter, $fields);
 
-			return $this->get_all_entries($filter, $fields);
-		}
-
+			return $this->get_all_entries($filter, $fields);*/
+			
+		}
+
+        // CELEPAR
+		function generate_filter($id_contacts)
+		{
+			if (($size = count($id_contacts)))
+			{
+				$contacts[$id] = $this->get_single_entry($id,$fields);
+			}
+			
+			return $contacts;
+		}
+		
+		// SERPRO
+		/*
 		function generate_filter($id_contacts)
 		{
@@ -1222,21 +1232,22 @@
 				{
 
-					/*
-					 * Não utiliza mais a função ldap_explode, usa a expressão regular a seguir para pegar o primeiro
-					 * componente da dn
-					 */
+					// 
+					//  Não utiliza mais a função ldap_explode, usa a expressão regular a seguir para pegar o primeiro
+					//  componente da dn
+					// 
 					preg_match('/^(\w*=[^,]*),.*$/', $id_contacts[$i], $cn);
 
-    				/*
-    				 * Adicionados os str_replace para adicionar caracteres de escape em frente aos caracteres '(' e ')',
-    				 * posteriormente poderá ser necessário substituir por uma expressão regular mais genérica.
-    				 */
+    				//
+    				// Adicionados os str_replace para adicionar caracteres de escape em frente aos caracteres '(' e ')',
+    				// posteriormente poderá ser necessário substituir por uma expressão regular mais genérica.
+    				//
+
 					if ($cn[1])
 					{
-						/*
-						 * Esta operação resolve o problema causado pela conversão de caracteres acentuados realizada
-						 * pela função ldap_explode_dn(). Talvez seja necessário utilizar esta tradução em outros lugares,
-						 * neste caso é mais apropriado colocar dentro de uma função.
-						 */
+						//
+						// Esta operação resolve o problema causado pela conversão de caracteres acentuados realizada
+						// pela função ldap_explode_dn(). Talvez seja necessário utilizar esta tradução em outros lugares,
+						// neste caso é mais apropriado colocar dentro de uma função.
+						//
 						//foreach($cn as $key=>$value){
 	          			//	$cn[$key]=preg_replace("/\\\([0-9A-Fa-f]{2})/e", "''.chr(hexdec('\\1')).''", $value);
@@ -1252,4 +1263,5 @@
 
 		}
+		*/
 
 		function get_all_entries_ids ()
@@ -1280,60 +1292,60 @@
 			return null;
 		}
-
+		
 		function get_relations ($id_contact,$extra=false)
 		{
 		}
-
+		
 		function get_addresses ( $id_contact,$extra=false )
 		{
 		}
-
+		
 		function get_connections ( $id_contact,$extra=false )
 		{
 		}
-
+		
 		function get_companies ( $id_contact, $extra=false )
 		{
 		}
-
+		
 		function get_all_prefixes (  )
 		{
 		}
-
+		
 		function get_all_suffixes (  )
 		{
 		}
-
+		
 		function get_all_status (  )
 		{
 		}
-
+		
 		function get_all_relations_types (  )
 		{
 		}
-
+		
 		function get_all_addresses_types (  )
 		{
 		}
-
+		
 		function get_all_connections_types (  )
 		{
 		}
-
+		
 		function get_vcard ( $id_contact )
 		{
 		}
-
-
-
-
+		
+		
+		
+		
 		function get_global_tree ( $root )
 		{
 		}
-
+	
 		function get_actual_brach (  )
 		{
 		}
-
+	
 		function set_actual_branch ( $branch )
 		{
