Skip to content

Instantly share code, notes, and snippets.

@MathieuDomingo
Last active July 5, 2021 10:31
Show Gist options
  • Save MathieuDomingo/c4810fab4b2264e403c906d4bab83414 to your computer and use it in GitHub Desktop.
Save MathieuDomingo/c4810fab4b2264e403c906d4bab83414 to your computer and use it in GitHub Desktop.
<?php
$timestart;
define('LDAP_HOST', "ldap.univ-XXXX.fr");
define('LDAP_PORT', "389");
define('LDAP_PASS', "XXXX");
define('LDAP_BIND', "cn=XXXX,ou=admin,dc=univ-XXXX,dc=fr");
define('LDAP_DN', "ou=people,dc=univ-XXXX,dc=fr");
define('MYSQL_HOST', "localhost");
define('MYSQL_USER', "XXXX");
define('MYSQL_PASS', "XXXX");
define('MYSQL_DBNAME', "XXXX");
//On lance un timer au début du main
function startTimer()
{
$GLOBALS["timestart"] = microtime(true);
echo "\n[INFO] Date du jour : ".date(DATE_RFC850)."\n";
echo "[INFO] Début du script à : ".date("H:i:s", $GLOBALS["timestart"])."\n";
}
//On arrete le timer
function endTimer()
{
$timeend = microtime(true);
$time = $timeend - $GLOBALS["timestart"];
$page_load_time = number_format($time, 3);
echo "[INFO] Fin du script à : ".date("H:i:s", $timeend)."\n";
echo "[INFO] Temps d'exécution : ".$page_load_time."s"."\n";
}
function main() {
startTimer();
// Create connection
$mysqli = new mysqli(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DBNAME);
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
echo "Mysql Connected successfully \n";
// connexion au LDAD
$ldap_conn = ldap_connect(LDAP_HOST, LDAP_PORT) or die("Impossible de se connecter au serveur LDAP.");;
$ldapbind = ldap_bind($ldap_conn, LDAP_BIND, LDAP_PASS);
//vérification de la connexion ldap
if (!$ldapbind) {
die("Connexion LDAP échouée...");
}
//On récupere les logins de ceux qui ont un prénom identique au login (et un id > à 3 pour ne pas modifier les 3 comptes présent par défaut)
$query = "SELECT login FROM credential where login = display_firstname and userid > 3";
$result = $mysqli->query($query);
//Pour chaque login obtenu on récupere le nom le prenom et le mail depuis ldap et on met à jour la ligne dans la bdd
while ($row = $result->fetch_row()) {
$login = $row[0];
printf("%s \n", $login);
$filter ="(uid=".$login.")";
$ldap_result = ldap_list($ldap_conn, LDAP_DN, $filter, array('uid', 'sn' , 'givenName', 'mail'));
$info = ldap_get_entries($ldap_conn, $ldap_result);
for ($i=0; $i < $info["count"]; $i++) {
//var_dump($info[$i]);
$prenom=$info[$i]["givenname"][0];
$nom=$info[$i]["sn"][0];
$mail=$info[$i]["mail"][0];
$query2 = "UPDATE credential set display_firstname='".$prenom."' , display_lastname='".$nom."', email='".$mail."' where login = '".$login."'";
$result2 = $mysqli->query($query2);
}
}
endTimer();
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment