R
RogerN author
Hi. Thought i share this with you.
This script searches User OU in AD using LDAP. It checks if record exists first before adding them into my Phprunner project. If they exist, it updates existing record.
You need a user account, password and server name for this to work and you need to create a table in your database withe the right names(ex. Value["employeeid"]). I'm using a cronjob to check every morning. Good luck.
<?php //this file is added to root
require_once("include/dbcommon.php");
//LDAP Bind paramters, need to be a normal AD User account.
$ldap_password = 'xxxxxxxx';
$ldap_username = 'xxxxxxxx';
$ldap_connection = ldap_connect("xxxxxxxx");
if (FALSE === $ldap_connection){
// Uh-oh, something is wrong...
echo 'Unable to connect to the ldap server';
}
// We have to set this option for the version of Active Directory we are using.
ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3) or die('Unable to set LDAP protocol version');
ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0); // We need this for doing an LDAP search.
ldap_set_option($ldap_connect, LDAP_OPT_SIZELIMIT, 0);
if (TRUE === ldap_bind($ldap_connection, $ldap_username, $ldap_password)){
//Your domains DN to query. ched AD for the correct base_dn
$ldap_base_dn = 'OU=User,OU=xxxx,DC=xxxx,DC=xxxx';
//Get standard users and contacts. Searching all users in folder
$search_filter = '(&(objectCategory=person)(CN=*))';
//Connect to LDAP
$result = ldap_search($ldap_connection, $ldap_base_dn, $search_filter);
$justthese = array('*');
$info = ldap_get_entries($ds, $result, $justthese);
if (FALSE !== $result){
$entries = ldap_get_entries($ldap_connection, $result);
// Uncomment the below if you want to write all entries to debug somethingthing
//var_dump($entries); //For each account returned by the search
for ($x=0; $x<$entries['count']; $x++){
//
//Retrieve values from Active Directory
//
//Windows Usernaame
$LDAP_samaccountname = "";
if (!empty($entries[$x]['samaccountname'][0])) {
$LDAP_samaccountname = $entries[$x]['samaccountname'][0];
if ($LDAP_samaccountname == "NULL"){
$LDAP_samaccountname= "";
}
}
//EmployeeID
$LDAP_employeeid = "";
if (!empty($entries[$x]['employeeid'][0])) {
$LDAP_employeeid = $entries[$x]['employeeid'][0];
if ($LDAP_employeeid == "NULL"){
$LDAP_employeeid= "";
}
}
//Last Name
$LDAP_LastName = "";
if (!empty($entries[$x]['sn'][0])) {
$LDAP_LastName = $entries[$x]['sn'][0];
if ($LDAP_LastName == "NULL"){
$LDAP_LastName = "";
}
}
//First Name
$LDAP_FirstName = "";
if (!empty($entries[$x]['givenname'][0])) {
$LDAP_FirstName = $entries[$x]['givenname'][0];
if ($LDAP_FirstName == "NULL"){
$LDAP_FirstName = "";
}
} //First Name
$LDAP_employeetype = "";
if (!empty($entries[$x]['employeetype'][0])) {
$LDAP_employeetype= $entries[$x]['employeetype'][0];
if ($LDAP_employeetype== "NULL"){
$LDAP_employeetype= "";
}
} //First Name
$LDAP_extensionattribute13= "";
if (!empty($entries[$x]['extensionattribute13'][0])) {
$LDAP_extensionattribute13= $entries[$x]['extensionattribute13'][0];
if ($LDAP_extensionattribute13== "NULL"){
$LDAP_extensionattribute13= "";
}
}
//First Name
$LDAP_extensionattribute9= "";
if (!empty($entries[$x]['extensionattribute9'][0])) {
$LDAP_extensionattribute9= $entries[$x]['extensionattribute9'][0];
if ($LDAP_extensionattribute9== "NULL"){
$LDAP_extensionattribute9= "";
}
} //First Name
$LDAP_displayname= "";
if (!empty($entries[$x]['displayname'][0])) {
$LDAP_displayname= $entries[$x]['displayname'][0];
if ($LDAP_displayname== "NULL"){
$LDAP_displayname= "";
}
} //Company
$LDAP_CompanyName = "";
if (!empty($entries[$x]['company'][0])) {
$LDAP_CompanyName = $entries[$x]['company'][0];
if ($LDAP_CompanyName == "NULL"){
$LDAP_CompanyName = "";
}
} //Manager
$LDAP_Manager = "";
if (!empty($entries[$x]['manager'][0])) {
$LDAP_Manager = $entries[$x]['manager'][0];
$m = explode(",",$LDAP_Manager,2);
$g = explode("=",$m[0]);
if ($LDAP_Manager == "NULL"){
$LDAP_Manager = "";
}
} //Mobile Number
$LDAP_CellPhone = "";
if (!empty($entries[$x]['mobile'][0])) {
$LDAP_CellPhone = $entries[$x]['mobile'][0];
if ($LDAP_CellPhone == "NULL"){
$LDAP_CellPhone = "";
}
}
//Email address
$LDAP_InternetAddress = "";
if (!empty($entries[$x]['mail'][0])) {
$LDAP_InternetAddress = $entries[$x]['mail'][0];
if ($LDAP_InternetAddress == "NULL"){
$LDAP_InternetAddress = "";
}
}
//Account Expires
$LDAP_accountExpires = "";
if (!empty($entries[$x]['accountexpires'][0])) {
$LDAP_accountExpires = date("Y-m-d", $entries[$x]['accountexpires'][0]/10000000-11644473600);
if ($LDAP_accountExpires == "NULL"){
$LDAP_accountExpires = "";
}
} global $conn;
$strSQLExists = "select * from mtrusers where employeeid='".$LDAP_employeeid."'";
$rsExists = db_query($strSQLExists,$conn);
$data=db_fetch_array($rsExists);
if($data)
{
global $dal;
$tblyour_table = $dal->Table("your_table");
$tblyour_table->Param["employeeid"]=$LDAP_employeeid;
$tblyour_table ->Value["samaccountname"]=$LDAP_samaccountname;
$tblyour_table ->Value["firstname"]=$LDAP_FirstName;
$tblyour_table ->Value["lastname"]=$LDAP_LastName;
$tblyour_table ->Value["employeetype"]=$LDAP_employeetype;
$tblyour_table ->Value["extensionattribute13"]=$LDAP_extensionattribute13;
$tblyour_table ->Value["extensionattribute9"]=$LDAP_extensionattribute9;
$tblyour_table ->Value["displayname"]=$LDAP_displayname;
$tblyour_table ->Value["companyname"]=$LDAP_CompanyName;
$tblyour_table ->Value["manager"]=$g[1];
$tblyour_table ->Value["cellphone"]=$LDAP_CellPhone;
$tblyour_table ->Value["internetaddress"]=$LDAP_InternetAddress;
$tblyour_table ->Value["accountexpires"]=$LDAP_accountExpires;
$tblyour_table ->Update();
}
else
{
global $dal;
$tblyour_table = $dal->Table("your_table");
$tblyour_table ->Value["employeeid"]=$LDAP_employeeid;
$tblyour_table ->Value["samaccountname"]=$LDAP_samaccountname;
$tblyour_table ->Value["firstname"]=$LDAP_FirstName;
$tblyour_table ->Value["lastname"]=$LDAP_LastName;
$tblyour_table ->Value["employeetype"]=$LDAP_employeetype;
$tblyour_table ->Value["extensionattribute13"]=$LDAP_extensionattribute13;
$tblyour_table ->Value["extensionattribute9"]=$LDAP_extensionattribute9;
$tblyour_table ->Value["displayname"]=$LDAP_displayname;
$tblyour_table ->Value["companyname"]=$LDAP_CompanyName;
$tblyour_table ->Value["manager"]=$g[1];
$tblyour_table ->Value["cellphone"]=$LDAP_CellPhone;
$tblyour_table ->Value["internetaddress"]=$LDAP_InternetAddress;
$tblyour_table ->Value["accountexpires"]=$LDAP_accountExpires;
$tblyour_table ->Add();
}
} //END for loop
} //END FALSE !== $result //I'm adding a record in my specific Cron table so i know it's ben run.
//global $dal;
//$tblcron = $dal->Table("cron");
//$tblcron ->Value["samaccountname"]="**datestamp**";
//$tblcron ->Value["file"]="filename";
//$tblcron ->Add(); ldap_unbind($ldap_connection); // Clean up after ourselves.
} //END ldap_bind
?>
|
|