<?PHP
/* $Id: ldap.inc.php,v 1.8 2004/04/21 21:54:05 liedekef Exp $ */

function authentify_user($user, $pass, $encrypted_pwd=''){
    global $appconf, $conn;

    $ldap = @ldap_connect($appconf["auth_ldaphost"]);
    if (!$ldap) {
	print "Could not connect to LDAP server ";
	exit();
    };

    // decrypt ldap pw from config.inc.php
    include("include/crypt.inc.php");
    $myC = new Crypto();

    /* Attempt to bind to the LDAP server as administrator. */
    $bind = ldap_bind($ldap, $appconf["auth_ldapuid"], $myC->decrypt("moregroupware", $appconf["auth_ldappasswd"]));
    if ($bind == false) {
        ldap_close($ldap);
        return false;
    }

    if($appconf["auth_ldaptype"]==1) {
	// Active Directory LDAP server
	//do search
	$search = ldap_search($ldap, $appconf["auth_ldapbase"],
			      "(samaccountname=".$user.")",
			      array("usncreated","samaccountname","dn","givenname","sn","cn"));
        $result = ldap_get_entries($ldap, $search);
        if (is_array($result) && (count($result) > 1)) {
	    $dn=$user.$appconf["auth_ldapsuffixe"];
        }
	else {
	    //user not found
            return false;
        }
    }
    else {
	// OpenLDAP server
	//do search
	$search = ldap_search($ldap, $appconf["auth_ldapbase"],
			      "(uid=".$user.")",
			      array("dn"));
        $result = ldap_get_entries($ldap, $search);
        if (is_array($result) && (count($result) > 1)) {
	    $dn=$result[0]["dn"];
        }
	else {
	    //user not found
            return false;
        }
    }

    /* Attempt to bind to the LDAP server as the user. */
    if ($pass=="")
	$pass="foo_moregroupware";
    $bind = ldap_bind($ldap, $dn, $pass);

    if ($bind != false) {
	ldap_close($ldap);
	// if bind succeeds, we just return true, so create_account_on_login
	// still works.
	return true;
    }

    ldap_close($ldap);
    return false;
}
?>
