debut
This commit is contained in:
106
ldap.php
Normal file
106
ldap.php
Normal 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");
|
||||
}
|
||||
Reference in New Issue
Block a user