\n
Active Directory >> Zentrack Import\n\n";
echo "Active Directory >> Zentrack Users Database Import Tool
\n";
echo "Run on: " . date(DATE_RFC822) . "
\n";
// Query AD for user accounts
$ad = ldap_connect($dc) or die("Couldn't connect to AD!");
echo "Connected to AD via LDAP
\n";
// Required for AD/Server 2003?
ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
$bd = ldap_bind($ad,$user,$pw) or die("Couldn't bind to AD!");
echo "Bound to AD via LDAP
\n";
// Recursive Query:
$ldapResults = ldap_search($ad, $dn, $filter, $attributes);
// Same-Level Query:
// Uncomment the following line and comment out the line above to search only the same OU level
// $ldapResults = ldap_list($ad, $dn, $filter, $attributes);
$entries = ldap_get_entries($ad, $ldapResults);
echo "Found ".$entries["count"]." entries in AD via LDAP Search
\n";
// Match AD users accounts to MySQL user table
$db = mysql_connect($mysql_server,$mysql_user,$mysql_password);
if (!$db) {
die('Could not connect: ' . mysql_error());
}
echo "Connected to MySQL server ".$mysql_server."
\n";
mysql_select_db($mysql_database, $db);
// begin loop through the DB usernames and if they aren't in AD delete them
$sql = "SELECT login,notes FROM zentrack_users";
$result = mysql_query($sql, $db);
if (!$result)
{
$message = 'Invalid query: ' . mysql_error($db) . "
";
$message .= 'Whole query: ' . $sql . "
";
die($message);
}
$deletedcount = 0;
$totalrows = mysql_num_rows($result);
echo "Found " . $totalrows . " entries in DB via SQL Search
\n";
echo "
";
while ($row = mysql_fetch_array($result))
{
$namearray[] = strtolower($row['login']);
$otherarray[] = $row['notes'];
}
for ($j=0; $j<($totalrows); $j++)
{
$inAD = false;
$exemptuser = false;
if ($namearray[$j] == $adminexempt)
{
echo "
Admin User : " . $namearray[$j] . "
\n";
$exemptuser = true;
}
if (($otherarray[$j] == $otherexempt) && (!$exemptuser))
{
echo "Exempt User: : " . $namearray[$j] . "
\n";
$exemptuser = true;
}
if (!$exemptuser)
{
for ($i=0; $i<$entries["count"]; $i++)
{
if ($namearray[$j] == strtolower($entries[$i]["samaccountname"][0]))
{
$i=$entries["count"];
$inAD=true;
}
}
if (!($inAD))
{
DeleteUser( $j, $db, $namearray[$j]);
++$deletedcount;
}
}
}
// end loop through the DB usernames and if they aren't in AD delete them
$usersadded=0;
$usersupdated=0;
echo "
\n";
for ($i=0; $i<$entries["count"]; $i++)
{
// look up AD user account in MySQL database
// add to database if user does not exist in database already
// update all user DB fields if they are already in the database so the DB matches AD
// PLEASE NOTE: All "index" values must be in lower-case! This is a PHP array handling quirk?
$username = $entries[$i]["samaccountname"][0];
if (DatabaseLookup($username, $db))
{
// echo $i." : AD user: ".$username." found in database
\n";
if (UpdateUser( $i, $db,
$entries[$i]["givenname"][0],
$entries[$i]["sn"][0],
$entries[$i]["samaccountname"][0],
$entries[$i]["mail"][0]))
{
++$usersupdated;
}
}
else
{
// echo $i." : AD user: ".$username." not found in database
\n";
AddUser( $i, $db,
$entries[$i]["givenname"][0],
$entries[$i]["sn"][0],
$entries[$i]["samaccountname"][0],
$entries[$i]["mail"][0]);
++$usersadded;
}
}
echo "
";
if (($deletedcount>0)||($usersadded>0)||($usersupdated>0)) { echo "
\n"; }
echo "Number of users deleted : " . $deletedcount . "
\n";
echo "Number of users added : " . $usersadded . "
\n";
echo "Number of users updated : " . $usersupdated . "
\n";
echo "