Location: PHPKode > projects > LDAP Navigator > ldapnavigator-0.9.6/modules/search/index.php
<?php


include_once ( './config.php' );

//$DEBUG = 1;

/**
 * init of some referentials
 *
 **/


// !! dont change that unless you know what ur doing
$searchOperators = array(
	"=" => " is ",
	"match" => " match ",
	"~=" => " looks like ",
	"start" => " starts with ",
	"end" => " ends with ",
	"<=" => " superior or equals ",	
	">=" => " inferior or equals ",
	"differs" => " different of ",
);

$searchScopes = array(
	'sub' => "Sub tree",
	'one' => "Only direct child nodes"
);

isset($fields) or $fields = $config->searchDefaultAttributes;
$searchAttrs = split( "[,;] ?", $fields );


/**
 * display search form
 *
 **/
 
if( $A == 'search.advanced' )
	include_once( "./modules/search/form.advanced.php" );
else
	include_once( "./modules/search/form.simple.php" );

flush();

/**
 * build LDAP query
 *
 **/
if( $query ) 
{

	// a plugins library to customize attribute display
	include_once( "attribute.plugins.php" );

	$ldapoperator = "=";
	$preoperator = "";
	$postoperator = "";
	$utfquery = utf8_encode($query);
	
	switch( $operator ) {
	
	case "=": case "<=": case ">=": case "~=":
		$ldapoperator = $operator; break;
		
	case "match":
		$utfquery = "*$utfquery*"; break;
		
	case "start":
		$utfquery = "$utfquery*"; break;
		
	case "end":
		$utfquery = "*$utfquery"; break;
		
	case "differs":
		$preoperator = "!("; $postoperator= ")"; break;
		
	default:
		ErrorPage( "Search Error !", "unknown operator : $operator" );
	}
	
	// champ customise "autre"
	if( $customfield != "" )
		$filter = "($preoperator$customfield$ldapoperator$utfquery$postoperator)"; // field=query search

	// champ de recherche par defaut
	elseif( $field == "" )
		$filter = "(|(" . $preoperator . "ou" . "$ldapoperator$utfquery$postoperator)($preoperator". "cn" . "$ldapoperator$utfquery$postoperator))"; // default search

	// champ du popup
	else
		$filter = "($preoperator$field$ldapoperator$utfquery$postoperator)"; // field=query search

	// filter on objectclass
	if( $oc ) 
		$filter = "(&(objectclass=$oc)$filter)";
		
	html::hidden( "savedQ", $filter );
	echo "</form>";
	
	// nombre d'items affiche dans une page
	$rowCount = $config->maxlistitems ? $config->maxlistitems : 20;

	if( ! intval($offset) || $offset < 0 )
		$offset = 0;

	debug("offset=$offset rowCount=$rowCount" );
	debug( "ldap_search($ds, $searchdn, $filter )" );
	
	// @todo use an attribute set, and a time_limit
	if( $scope == 'one' ) {
		$sr = @ldap_list($ds, $searchdn, $filter, $searchAttrs );
	} else {
		$sr = @ldap_search($ds, $searchdn, $filter, $searchAttrs );
	}

	debug( "ldap_search($ds, $searchdn, $filter )" );
	
	$timing = sprintf( "%.2f", $Utils_Timer_chronos["ldap_search"] );
	
	// something bad happened
	if( ! $sr ) {
	
		$errorNo = ldap_errno($ds);
		if( $errorNo == 32 )
			$errorMsg = "Error - Please check the 'From DN' - LDAP message is " . ldap_error($ds);
		else
			$errorMsg = "Error " . $errorNo . " : " . ldap_error($ds);

	} else {
	
		$entries_count = ldap_count_entries( $ds, $sr );
		if( $entries_count < $offset + $rowCount )
			$rowCount = $entries_count - $offset;
			
		// vies all results
		if( $fulllist ) {
			$offset=0;
			$rowCount = $entries_count;
		}
		
		// retrieve entries and store them in the $items array
		$eid = ldap_first_entry( $ds, $sr );
		$idx = 0;
		$items = array();
		while( $eid && $idx < $offset + $rowCount ) {
			// we only store the entries to be displayed
			if( $offset <= $idx ) {
				$item_dn = ldap_get_dn( $ds, $eid );
				$items[$idx] = ldap_get_attributes( $ds, $eid );
				$items[$idx]['dn'] = $item_dn;
			}
			$eid = ldap_next_entry( $ds, $eid );
			$idx++;
		}
	}
	
	if( $errorMsg ) {

		echo "<b><font color=red>$errorMsg</font></b>";
		
	// pas de resultats
	} elseif( $entries_count == 0 ) {

		echo "<b>No result found</b>";

	} else {

		$urlNextPage = $urlPrevPage = $urlFullResults = "";

		// calcul des offset pour pages next/prev
		$nextoffset = $offset + $rowCount;
		$prevoffset = $offset - $rowCount;
		$tmpvars = $HTTP_GET_VARS;

		// il reste des entrees a afficher apres cette page
		if( $nextoffset < $entries_count ) {
			// construit la query_string pour la page suivante
			$tmpvars['offset'] = $nextoffset;
			$queryConstant =  makeQueryString( $tmpvars );
			$urlNextPage = "<a href=\"$PHP_SELF?$queryConstant\"><b>[ next page ]</b></a>";
		}

		// il y a des entrees avant cete page
		if( $prevoffset >= 0 ) {
			$tmpvars['offset'] = $prevoffset;
			$queryConstant =  makeQueryString( $tmpvars );
			$urlPrevPage = "<a href=\"$PHP_SELF?$queryConstant\"><b>[ prev. page ]</b></a>";
		}

		if( $prevoffset >= 0 || $nextoffset < $entries_count ) {
			$urlFullResults = "<a href=\"$PHP_SELF?$queryConstant&fulllist=1\"><b>[ tous ]</b></a>";
		}

		
		$pos = $offset;
					
		$minbound = $pos+1; $maxbound = $pos+$rowCount;
		
		echo '<div align=center width=100%>';
		
		echo '<table cellspacing=0 style="padding: 4px; width:90%">',
			"<tr>",
			"<td align=left width=100>$urlPrevPage</td>",
			"<th align=center>", "$entries_count results in $timing seconds - Displaying $minbound - $maxbound ", "</th>",
			"<td align=right width=100>$urlFullResults</td>",
			"<td align=right width=100>$urlNextPage</td>",
			"</tr>",
			"</table>\n";

		
		// advanced search = custom fields and extra stuff
		if( $A == 'search.advanced' ) {
			
			include_once( './modules/search/results.advanced.php' );
		
		// simple search = no custom fields
		} else {

			include_once( './modules/search/results.simple.php' );
			
		}
		echo "</div>\n";
		
	}

	ldap_unbind ($ds);

}

echo "<br>\n";

include_once ("./page.footer.php");


function get_ldap_object( $item )
{
	$formattedItem = array();
	$formattedItem['dn'] = $item['dn'];
	for( $i=0; $i < $item['count'] ; $i ++ ) {
		$attrname = $item[$i];
		// lowercase attribute name because of stupid servers
		$lcattrname = strtolower($attrname);
		$formattedItem[$lcattrname] = array();
		for( $j=0; $j < $item[$attrname]['count']; $j++ ) {
			$formattedItem[$lcattrname][] = utf8_decode($item[$attrname][$j]);
		}
	}
	return $formattedItem;
}

function &attributeFormat( $attribute, &$value )
{
	global $attributesPluginsMap;
	
	$attributePlugin = $attributesPluginsMap[$attribute];
	if( function_exists( $attributePlugin) )
		return $attributePlugin( $value, 0 );
	else
		return $value;
}



?>
Return current item: LDAP Navigator