From 5b31afe52eb506f09b7913d6100c71f0af93ab2a Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Wed, 26 Mar 2025 17:02:40 +0100 Subject: [PATCH] debut --- db_test.php | 15 +++++ index.php | 3 + ldap.php | 106 +++++++++++++++++++++++++++++++ login.php | 33 ++++++++++ templates/login_failed.html | 10 +++ templates/login_form.html | 21 ++++++ templates/login_success.html.php | 12 ++++ view/View.php | 48 ++++++++++++++ 8 files changed, 248 insertions(+) create mode 100644 db_test.php create mode 100644 index.php create mode 100644 ldap.php create mode 100644 login.php create mode 100644 templates/login_failed.html create mode 100644 templates/login_form.html create mode 100644 templates/login_success.html.php create mode 100644 view/View.php diff --git a/db_test.php b/db_test.php new file mode 100644 index 0000000..3d7651f --- /dev/null +++ b/db_test.php @@ -0,0 +1,15 @@ +setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + echo "Connexion réussie � MariaDB avec PDO!"; +} catch (PDOException $e) { + echo "�chec de la connexion : " . $e->getMessage(); +} +?> diff --git a/index.php b/index.php new file mode 100644 index 0000000..d2b25e4 --- /dev/null +++ b/index.php @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/ldap.php b/ldap.php new file mode 100644 index 0000000..7624c76 --- /dev/null +++ b/ldap.php @@ -0,0 +1,106 @@ +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"); +} diff --git a/login.php b/login.php new file mode 100644 index 0000000..1519ab0 --- /dev/null +++ b/login.php @@ -0,0 +1,33 @@ + + + + + Mauvaise connexion + + +

Erreur lors de la connexion !

+ + \ No newline at end of file diff --git a/templates/login_form.html b/templates/login_form.html new file mode 100644 index 0000000..d1fef7c --- /dev/null +++ b/templates/login_form.html @@ -0,0 +1,21 @@ + + + + + Connexion au LDAP + + +
+ + +
+ + +
+ + +
+ +
+ + \ No newline at end of file diff --git a/templates/login_success.html.php b/templates/login_success.html.php new file mode 100644 index 0000000..a9136ba --- /dev/null +++ b/templates/login_success.html.php @@ -0,0 +1,12 @@ + + + + + Connexion + + + + + \ No newline at end of file diff --git a/view/View.php b/view/View.php new file mode 100644 index 0000000..447438c --- /dev/null +++ b/view/View.php @@ -0,0 +1,48 @@ +"; +} + +function brrr() +{ + return "
"; +} + +function PrintList(string $title, array $liste): string +{ + $result = ""; + return $result; +} + +function PrintListFirsts(string $title, array $liste): string +{ + $result = "
  • " . $title . "
  • "; + $result .= ""; + return $result; +} + +function PrintLoginInfo($info) +{ + $body = "Nom complet de l'utilisateur : " . $info->fullName; + $body .= ""; + return $body; +}