PHP & LDAP

function show_a_person2($host, $path, $racine, $userdn, $pass) {
        $ds=ldap_connect($host);
        ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
        $fullPath=$path.",".$racine ;
        $rootdn="cn=admin,".$racine;
        $rootpw="your pass";
        $r=@ldap_bind($ds,$rootdn,$rootpw);
        if($ds && $r) {
                $sr=ldap_search($ds, $fullPath, "objectclass=*");
                $info = ldap_get_entries($ds, $sr);
                for ($i=0;$i<$info["count"];$i++) {
                        $res["cn"] = $info[$i]["cn"][0];
                        $res["gecos"] = $info[$i]["gecos"][0];
                        $res["gidnumber"] = $info[$i]["gidnumber"][0];
                        $res["homedirectory"] = $info[$i]["homedirectory"][0];
                        $res["loginshell"] = $info[$i]["loginshell"][0];
                        $res["uid"] = $info[$i]["uid"][0];
                        $res["uidnumber"] = $info[$i]["uidnumber"][0];
                        $res["userpassword"] = $info[$i]["userpassword"][0];
                }
                return $res ;
        }
        else {
                return "Erreur de connexion" ;
        }
}
/*
 * Ajouter une personne dans le LDAP
 */
function ldapAddUser($login,$clearpass, $loginshell, $gidnumber, $uidnumber, $homedirectory) {
        $ds = connectLdap(getHost(),getUserAdminDn(),getPassAdminDn);
        if($ds) {
                $r=ldap_bind($ds,getUserAdminDn(),getPassAdminDn());
                $info["uid"] = $login ;
                $info["cn"] = $login ;
                $info["objectClass"][0] = "account" ;
                $info["objectClass"][1] = "posixAccount" ;
                $info["objectClass"][2] = "top" ;
                $info["userPassword"] = "{crypt}".constructPass($clearpass);
                $info["loginShell"] = $loginshell ;
                $info["uidNumber"] = $uidnumber ;
                $info["gidNumber"] = $gidnumber ;
                $info["homeDirectory"] = $homedirectory ;
                $dn = "uid=".$login.",ou=people,ou=litis-lehavre,dc=litislab,dc=eu";
                $res = ldap_add($ds, $dn, $info);
                $res = ldap_err2str( ldap_errno($ds) );
                ldap_close($ds);
                return $res ;
        }
}
/**
 * Authentification
 */
function authentification($user, $ldappass) {
        $server="172.17.21.77";
        $ldaprdn="uid=".$user.",ou=people,ou=litis-lehavre,dc=litislab,dc=eu";
        $ds=ldap_connect($server);  // doit ĂȘtre un serveur LDAP valide !
        ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
        if ($ds) {
            $r=@ldap_bind($ds, $ldaprdn, $ldappass);     // connexion
            // Identification
            if ($r) {
                return true ;
            } else {
                return false ;
            }
            ldap_close($ds);
        } else {
            return 2 ;
        }
}
 
/**
 * connect LDAP
 */
 
function connectLdap($host,$userDn,$userPasswd) {
        $ds=ldap_connect($host);
        ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
        $r=@ldap_bind($ds,$userDn,$userPasswd);
        return $r ;
}
 
function disconnectLdap($ds) {
        return ldap_unbind($ds) ;
}
 
/**
 * list_of_people
 */
function list_of_people($host, $path, $racine, $userDn, $userPasswd) {
        $ds=ldap_connect($host);
        $fullPath=$path.",".$racine ;
        ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
        $r=@ldap_bind($ds,$userDn,$userPasswd);
        if($ds) {
                //$sr=ldap_search($ds, $fullPath, "objectclass=*");
                $sr=ldap_search($ds, $fullPath, "uid=jbaudry");
                $info = ldap_get_entries($ds, $sr);
                for ($i=0;$i<$info["count"];$i++) {
                        $res[$i]["uid"]=$info[$i]["uid"][0];
                        $res[$i]["cn"]=$info[$i]["cn"][0];
                        //echo $info[$i]["uid"][0];
                }
                return $res ;
        }
        else {
                return "Erreur de connexion" ;
        }
}
 
function getCn($uid) {
        $host="ldap.litislab.eu" ;
        $path="ou=people,ou=litis" ;
        $racine="dc=litislab,dc=eu";
        $userDn="cn=Julien BAUDRY,ou=people,ou=litis,dc=litislab,dc=eu";
        $userPasswd="password";
 
        $ds=ldap_connect($host);
                $fullPath=$path.",".$racine ;
                ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
                $r=@ldap_bind($ds,$userDn,$userPasswd);
                if($ds) {
                        $sr=ldap_search($ds, $fullPath, "uid=".$uid);
                        $info = ldap_get_entries($ds, $sr);
                        for ($i=0;$i<$info["count"];$i++) {
                                $res[$i]["uid"]=$info[$i]["uid"][0];
                                $res[$i]["cn"]=$info[$i]["cn"][0];
                        }
                        return $res[0]["cn"] ;
                }
                else {
                        return 0 ;
                }
}
 
function ldapModifyPasswd2($login,$clearpass) {
        $ds = connectLdap(getHost(),getUserAdminDn(),getPassAdminDn);
        echo "login = ".$login ;
        if($ds) {
                $r=ldap_bind($ds,getUserAdminDn(),getPassAdminDn());
                $new["userPassword"]= "{crypt}".constructPass($clearpass);
                echo $new["userPassword"]."--".$clearpass ;
                $dn = "uid=".$login.",ou=people,ou=litis-lehavre,dc=litislab,dc=eu";
                $res=ldap_modify($ds, $dn, $new);
                disconnectLdap($ds);
        }
        else {
                echo "Connexion au serveur LDAP impossible";
        }
        return $res;
}
 
 
/**
 * Genere un salt aléatoire de 8 caracteres
 */
function generateSalt($length) {
        return substr(str_shuffle("abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
}
 
function constructPass($text) {
        return crypt($text,'$1$'.generateSalt('8'));
}
 
function getUserAdminDn() {
        return "cn=admin,dc=litislab,dc=eu";
}
 
 
function create_password($length=8,$use_upper=1,$use_lower=1,$use_number=1,$use_custom=""){
    $upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $lower = "abcdefghijklmnopqrstuvwxyz";
    $number = "0123456789";
    if($use_upper){
        $seed_length += 26;
        $seed .= $upper;
    }
    if($use_lower){
        $seed_length += 26;
        $seed .= $lower;
    }
    if($use_number){
        $seed_length += 10;
        $seed .= $number;
    }
    if($use_custom){
        $seed_length +=strlen($use_custom);
        $seed .= $use_custom;
    }
    for($x=1;$x<=$length;$x++){
        $password .= $seed{rand(0,$seed_length-1)};
    }
    return($password);
}