Location: PHPKode > projects > BugIn and Ticketing System > bugin-0.65/lib/ldap.inc.php
<?php
/*function ldapGetUser($user, $pass) {{{
	gets a user from an LDAP server using the supplied name and password */
function ldapGetUser($user, $pass) {
	global $options;

	$ldapDn = "cn={$user},cn={$options['ldapDomain']}";
	$filter="(uid={$user})";
	if($options['debug']) {
		print "<li>searching for $filter</li>\n";
		print "<li>ldapReadServer: {$options['ldapReadServer']}</li>\n";
		print "<li>ldapDomain: {$options['ldapDomain']}</li>\n";
		print "<li>ldapDn: $ldapDn</li>\n";
	}

	$ds=ldap_connect($options['ldapReadServer']);

	if ($ds) {
		if($options['debug']) {
			buginError("connected to {$options['ldapReadServer']}", MESSAGE_DEBUG);
		}
		if((@$r=ldap_bind($ds, $ldapDn, $pass))&&($options['debug'])) {
			buginError("bound to {$options['ldapReadServer']}", MESSAGE_DEBUG);
		}
		elseif ($r < 1) {
			$_SESSION['loginError'] = "Could not auth to server";
			buginError($_SESSION['loginError'], MESSAGE_ERROR);
			return 0;
		}
		if ($sr=ldap_search($ds, "", $filter)) {
			$ui = ldap_get_entries($ds, $sr);
		}

		if(($options['debug'])&&($ui)) {
			buginError("data was returned by the ldap search", MESSAGE_DEBUG);
		}
		elseif (!$ui) {
			buginError($_SESSION['loginError'], MESSAGE_DEBUG);
			ldap_close($ds);
			return 0;
		}
		ldap_close($ds);
	}
	else {
		if($options['debug']) {
			buginError("failed connecting to {$options['ldapReadServer']}", MESSAGE_DEBUG);
		}
		return 0;
	}

	$info["username"] = $ui[0]["uid"][0];
	$info["email"] = $ui[0]["mail"][0];
	$info["pass"] = md5($pass);
	$info["phone"] = $ui[0]["telephoneNumber"][0];
	if ($ui[0]["givenname"][0] || $ui[0]["sn"][0])
		$info["name"] = $ui[0]["givenname"][0]." ".$ui[0]["sn"][0];
	return $info;
}

/* }}} */

/* function ldapGetUserByEmail($emailAddy, $search = 0) {{{
	searches an ldap database for a user based upon the supplied
	e-mail address. */
function ldapGetUserByEmail($emailAddy, $search = 0) {
	global $options;
	$searchField = "mail";

	$ds=ldap_connect($options['ldapReadServer']);  // must be a valid LDAP server!
	if ($ds) { 
		$searchIndex = 0;

		$r=ldap_bind($ds);	 // this is an "anonymous" bind, typically

		$searches[] = "mail=";
		$searches[] = "otherMailbox=smtp$";
	
		// Search surname entry
		while (!$ui['count'] && $searches[$searchIndex]) {
			$sr=ldap_search($ds,"", $searches[$searchIndex].$emailAddy);  
	
			$ui = ldap_get_entries($ds, $sr);

			$searchIndex ++;
		}
	
		ldap_close($ds);

		if (!$ui[count]) 
			return 0;

		if ($search) {
			return $ui;
		}
		else {
			$info["username"] = $ui[0]["uid"][0];
			$info["email"] = $ui[0]["mail"][0];
			$info["phone"] = $ui[0]["telephoneNumber"][0];
			if ($ui[0]["givenname"][0] || $ui[0]["sn"][0])
				$info["name"] = $ui[0]["givenname"][0]." ".$ui[0]["sn"][0];

			return $info;
		}

	}
}
/* }}} */
?>
Return current item: BugIn and Ticketing System