<?PHP
/* $Id: ldap.inc.php,v 1.6 2003/12/10 16:42:48 k-fish Exp $ */

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

    $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) {
	//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 {
	//do search
	$search = ldap_search($ldap, $appconf["auth_ldapbase"],
			      "(uid=".$user.")",
			      array("usncreated","samaccountname","dn","givenname","sn","cn"));
        $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);
	return true;
    }

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