<?php

// TODO: use ldapClass
function getBaseDnFromUserPrincipalName($userPrincipalName)
{
  $BaseDn="";
  $domain = explode("@",$userPrincipalName)[1];
  $domainParts = explode(".",$domain);
  $BaseDn = "DC=".$domainParts[0].",DC=".$domainParts[1];
  return $BaseDn;
}

function extractNameFromDn($dn)
{
  //CN=Builtin,DC=easylinux,DC=lan
  return explode("=",explode(",",$dn)[0])[1];
}

//$User = "CN=Administrator,CN=Users,DC=easylinux,DC=lan";
$User = "administrator@easylinux.lan";
$Pass = "1duB015!";
$uri = "ldap://dc1:389";
// $uri = "ldaps://dc1:636";

if( ! $cnx=ldap_connect($uri)) 
  die("erreur: ldap_connect");

if( ! ldap_bind ( $cnx , $User , $Pass ) )
{
  echo ldap_errno($cnx).": ".ldap_error($cnx);
  die("<br />erreur: ldap_bind");
}
  

ldap_set_option($cnx, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($cnx, LDAP_OPT_REFERRALS, 0);

$sr = ldap_list($cnx, getBaseDnFromUserPrincipalName($User),"dn=*");

$info = ldap_get_entries($cnx, $sr);

for ($i=0; $i < $info["count"]; $i++) {
  //echo var_dump($info);
  
  echo "show: ".var_dump($info[$i]["showinadvancedviewonly"][0]). "- ";
  if( $info[$i]["showinadvancedviewonly"][0] == "FALSE" )
  {
    echo extractNameFromDn($info[$i]["dn"]);
  }
  
  // //echo $info[$i]["dn"];
  echo "<br/>";
}
ldap_close($cnx);


echo "<br/><br/>".getBaseDnFromUserPrincipalName($User);

echo "<br/><a href='/index.php'>Accueil</a>";

/*

objectCategory contient le type
*/
?>