This commit is contained in:
2025-03-26 17:02:40 +01:00
parent 6afa94c84a
commit 5b31afe52e
8 changed files with 248 additions and 0 deletions

15
db_test.php Normal file
View File

@@ -0,0 +1,15 @@
<?php
$host = 'localhost'; // ou l'adresse IP du serveur MariaDB
$dbname = 'mysql'; // nom de votre base de donn<6E>es
$username = 'root'; // nom d'utilisateur MariaDB
$password = '4321'; // mot de passe pour l'utilisateur
try {
// Cr<43>ation d'une instance PDO pour la connexion <20> la base de donn<6E>es
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
// Configuration du mode d'erreur de PDO pour les exceptions
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connexion réussie <20> MariaDB avec PDO!";
} catch (PDOException $e) {
echo "<EFBFBD>chec de la connexion : " . $e->getMessage();
}
?>

3
index.php Normal file
View File

@@ -0,0 +1,3 @@
<?php
phpinfo();
?>

106
ldap.php Normal file
View File

@@ -0,0 +1,106 @@
<?php
$ldap_domain_name = "woodywood";
$handle = ldap_connect("ldap://$ldap_domain_name.local");
ldap_set_option($handle, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($handle, LDAP_OPT_REFERRALS, 0);
class UserInfo
{
public ?string $fullName;
public array $ous;
public function __construct(?string $fullName, array $ous)
{
$this->fullName = $fullName;
$this->ous = $ous;
}
}
function LdapConnect(string $domain, string $username, string $password, ?array $controls): LDAP\Result|false
{
global $handle;
$bind = ldap_bind_ext($handle, $username . '@' . $domain, $password, $controls);
LogConnection();
return $bind;
}
function LogConnection() {}
function LdapConnectAndBind()
{
global $ldap_domain_name;
$ldap_instance = ldap_connect("ldap://$ldap_domain_name.local");
ldap_set_option($ldap_instance, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap_instance, LDAP_OPT_REFERRALS, 0);
ldap_bind($ldap_instance, "Administrateur@woodywood", "3AFISE+25");
return $ldap_instance;
}
function LdapGetUserInfo(string $user): ?UserInfo
{
global $handle;
global $ldap_domain_name;
$search_base = "DC=$ldap_domain_name,DC=local";
$search_filter = "(sAMAccountName=$user)";
$search_attributes = ["distinguishedname"];
$result = ldap_search($handle, $search_base, $search_filter, $search_attributes);
$entries = ldap_get_entries($handle, $result);
if ($entries['count'] > 0) {
$dn = $entries[0]['distinguishedname'][0];
return new UserInfo(GetValue($dn, "CN")[0], GetValue($dn, "OU"));
}
return null;
}
function GetValue(string $dnStr, string $key): array
{
preg_match_all("/$key=([^,]+)/", $dnStr, $matches);
return isset($matches[1]) ? $matches[1] : [];
}
function LdapGetObjectsInOU(string $ou, string $objectType, string $field): array
{
global $ldap_domain_name;
global $handle;
// $handle = LdapConnectAndBind();
$searchBase = "DC=$ldap_domain_name,DC=local";
$filter = "(objectClass=$objectType)";
$attributes = [];
$object_values = [];
$result = ldap_search($handle, $searchBase, $filter, $attributes);
$entries = ldap_get_entries($handle, $result);
if ($entries['count'] > 0) {
foreach ($entries as $key => $entry) {
if (!isset($entry["dn"]))
continue;
$dn = $entry["dn"];
$ous = GetValue($dn, "OU");
$res = array_search($ou, $ous);
if (!is_numeric($res))
continue;
$cn = GetValue($dn, $field);
array_push($object_values, $cn);
}
}
return $object_values;
}
function LdapGetUsersInOU(string $ou): array
{
return LdapGetObjectsInOU($ou, "user", "CN");
}
function LdapGetGroupsInOU(string $ou): array
{
return LdapGetObjectsInOU($ou, "group", "CN");
}

33
login.php Normal file
View File

@@ -0,0 +1,33 @@
<?php
if (!isset($_POST["domain"]) || !isset($_POST["user"]) || !isset($_POST["password"])) {
require_once "templates/login_form.html";
exit;
}
$domain = rtrim($_POST["domain"]);
$user = rtrim($_POST["user"]);
$password = rtrim($_POST["password"]);
require_once "ldap.php";
$result = LdapConnect($domain, $user, $password, []);
ldap_parse_result($handle, $result, $error_code, $matched_dn, $error_message, $referrals, $controls);
if ($error_code != 0) {
require_once "templates/login_failed.html";
exit;
}
require_once "view/View.php";
$info = LdapGetUserInfo($user);
$body = PrintLoginInfo($info);
require_once "templates/login_success.html.php";
// TODO: style
// TODO: Mettre les tentatives dans la db

View File

@@ -0,0 +1,10 @@
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mauvaise connexion</title>
</head>
<body>
<p>Erreur lors de la connexion !</p>
</body>
</html>

21
templates/login_form.html Normal file
View File

@@ -0,0 +1,21 @@
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Connexion au LDAP</title>
</head>
<body>
<form method="post" action="">
<label for="domain">Domaine :</label>
<input id="domain" name="domain" type="text" value="woodywood" required>
<br/>
<label for="user">Utilisateur :</label>
<input id="user" name="user" type="text" placeholder="Utilisateur" required>
<br/>
<label for="password">Mot de passe :</label>
<input id="password" name="password" type="password" placeholder="Mot de passe" required>
<br/>
<input type="submit" value="Envoyer"/>
</form>
</body>
</html>

View File

@@ -0,0 +1,12 @@
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Connexion</title>
</head>
<body>
<?php
echo $body ?? "";
?>
</body>
</html>

48
view/View.php Normal file
View File

@@ -0,0 +1,48 @@
<?php
function brr()
{
echo "<br/>";
}
function brrr()
{
return "<br/>";
}
function PrintList(string $title, array $liste): string
{
$result = "<ul><p>$title</p>";
foreach ($liste as $element) {
$result .= "<li>$element</li>";
}
$result .= "</ul>";
return $result;
}
function PrintListFirsts(string $title, array $liste): string
{
$result = "<li>" . $title . "</li>";
$result .= "<ul>";
foreach ($liste as $element) {
$result .= "<li>" . $element[0] . "</li>";
}
$result .= "</ul>";
return $result;
}
function PrintLoginInfo($info)
{
$body = "Nom complet de l'utilisateur : " . $info->fullName;
$body .= "<ul>";
foreach ($info->ous as $ou) {
$body .= "<li>" . $ou;
$body .= "<ul>";
$body .= PrintListFirsts("Utilisateurs", LdapGetUsersInOU($ou));
$body .= PrintListFirsts("Groupes", LdapGetGroupsInOU($ou));
$body .= "</ul>";
$body .= "</li>";
}
$body .= "</ul>";
return $body;
}