Application

<?php
 
include "ldap_view.inc";
include "ldap_model.inc";
 
session_start();
 
if($_SERVER['REQUEST_METHOD']=='POST') {
	if(isset($_POST['form_auth_submit'])) {
		$res=auth($_POST['login'], $_POST['passwd']);
		if($res) {
			// creation d'une session
 
			$_SESSION['login']=$_POST['login'] ;
			head();
			toolbar($_SESSION['login']);
			foot();
		}
		else {
			head();
			echo "identifiant ou mot de passe incorrect" ;
			view_auth();
			foot();
		}
	}
} else if($_GET["page"]=="disconnect") {
		session_destroy();
		header('Location:index.php');
} else if(isset($_GET["profil"])) {
		head();
		toolbar($_SESSION['login']);
		echo "visualiser le profil de ".$_SESSION["login"];
		//view_profil($_SESSION['login']);
		foot();
}
else  if($_SESSION["login"]=="") {
		head();
		view_auth();
		foot();
}
else {
	head();
	toolbar($_SESSION['login']);
	foot();
}
 
?>
<?php
 
function head() {
echo <<<EOB
	<html>
	<body>
EOB;
}
 
function foot() {
echo <<<EOB
	</body>
	</html>
 
EOB;
}
 
function toolbar($login) {
	echo <<<EOB
	<TABLE BORDER=0 width="500px">
	<TR>
		<TD>login : {$login} (<a href="index.php?page=disconnect">disconnect</a>)</TD>
		<td><a href=index.php?profil={$login}>My profile</a></td>
		<td><a href=index.php?page=user_list>La liste des utilisateurs</a></td>
	</TR>
	</TABLE>
EOB;
}
 
function view_auth() {
echo <<<EOB
	<FORM method=post name="form_auth" action="index.php">
	<center>
	<b>Authentification sur l'annuaire LDAP</b>
	<TABLE BORDER=0 width="500px">
	<TR>
		<TD>Login</TD>
		<TD>
		<INPUT type=text name="login">
		</TD>
	</TR>
 
	<TR>
		<TD>Password</TD>
		<TD>
		<INPUT type=password name="passwd">
		</TD>
	</TR>
	<TR>
		<TD COLSPAN=2>
		<INPUT type="submit" name="form_auth_submit" value="Envoyer">
		</TD>
	</TR>
	</TABLE>
	</center>
EOB;
}
 
?>
<?php
 
include "ldap_lib.inc";
 
/*
* Authentification
*/
function auth($login,$passwd) {
	$uid="uid=".$login.",ou=users,dc=univ-lehavre,dc=fr";
	return connectLdap("127.0.0.1", $uid, $passwd);
}
 
?>
<?php
 
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 list_of_people($host, $path, $userDn, $userPasswd) {
        $ds=ldap_connect($host);
        ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
        $r=@ldap_bind($ds,$userDn,$userPasswd);
        if($ds) {
                $sr=ldap_search($ds, $path, "objectclass=*");
                $info = ldap_get_entries($ds, $sr);
 
		// Affiche toutes les données
		foreach($info as $k=>$v) {
			if(is_array($v)) {
				foreach($v as $k1=>$v1) {
					if(is_array($v1)) {
						foreach($v1 as $k2=>$v2) {
							echo $k1."=".$v2."<br/>";
						}
					}
				}
			}
		}
 
		//affiche une partie des données
                for ($i=0;$i<$info["count"];$i++) {
                        $res[$i]["uid"]=$info[$i]["uid"][0];
                        $res[$i]["cn"]=$info[$i]["cn"][0];
                }
                return $res ;
        }
        else {
                return "Erreur de connexion" ;
        }
}
 
function ldapAddUser($login, $clearpass, $loginshell, $gidnumber, $uidnumber, $homedirectory) {
        $host="localhost";
        $rootdn="cn=admin,dc=univ-lehavre,dc=fr";
        $rootpasswd="azerty";
        $ds = ldap_connect($host);
        if($ds) {
		 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
                $r=ldap_bind($ds,$rootdn,$rootpasswd);
                $info["uid"] = $login ;
                $info["cn"] = $login ;
                $info["objectclass"][0] = "account" ;
                $info["objectclass"][1] = "posixAccount" ;
                $info["userpassword"] = "{crypt}".constructPass($clearpass);
                $info["uidnumber"] = $uidnumber ;
                $info["gidnumber"] = $gidnumber ;
                $info["homedirectory"] = $homedirectory ;
                $dn = "uid=".$login.",ou=users,dc=univ-lehavre,dc=fr";
                $res = ldap_add($ds, $dn, $info);
                $res = ldap_err2str( ldap_errno($ds) );
                ldap_close($ds);
                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'));
}
 
 
//Delete
function ldapDelUser($dn) {
        $host="localhost";
        $rootdn="cn=admin,dc=univ-lehavre,dc=fr";
        $rootpasswd="azerty";
        $ds = ldap_connect($host);
        if($ds) {
		ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
                $r=ldap_bind($ds,$rootdn,$rootpasswd);
		$res=ldap_delete($ds,$dn);
		ldap_close($ds);
	}
	return $res ;
}
 
function ldapModPassUser($login, $newpass) {
        $host="localhost";
        $rootdn="cn=admin,dc=univ-lehavre,dc=fr";
        $rootpasswd="azerty";
        $ds = ldap_connect($host);
	$dn = "uid=".$login.",ou=users,dc=univ-lehavre,dc=fr";
	$info["userpassword"]="{crypt}".constructPass($newpass);
        if($ds) {
		ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
                $r=ldap_bind($ds,$rootdn,$rootpasswd);
		$res=ldap_modify($ds,$dn,$info);
		ldap_close($ds);
	}
	return $res; 
}
?>