I have written the following code in our osticket installation, to pull newly created users in ADS and insert them into db of osticket as well as lock users who are disabled in ADS.
<?php
//RE012-NEWLY CREATED FOR LISTING ADS USERS WITH THEIR STATUS
if(!defined('OSTADMININC') || !$thisuser->isadmin()) die('Access Denied');
$groups=db_query('SELECT group_id,group_name FROM '.GROUP_TABLE);
$depts= db_query('SELECT dept_id,dept_name FROM '.DEPT_TABLE);
$SearchField="samaccountname";
$LDAPHost = "ldap://something.com";
$dn = "DC=something,DC=com";
$LDAPUserDomain = "@something.com";
$LDAPUser = "administrator";
$LDAPUserPassword = "xxxx";
$LDAPFieldsToFind = array("cn", "givenname","samaccountname","homedirectory", "telephonenumber", "mail","name","userAccountControl","countryCode");
$office=1;
$cnx = ldap_connect($LDAPHost) or die("Could not connect to LDAP");
ldap_set_option($cnx, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($cnx, LDAP_OPT_REFERRALS, 0);
ldap_bind($cnx,$LDAPUser.$LDAPUserDomain,$LDAPUserPassword) or die("Could not bind to LDAP");
error_reporting (E_ALL ^ E_NOTICE);
$filter="($SearchField=*)";
//echo $filter;
$sr=ldap_search($cnx, $dn, $filter,$LDAPFieldsToFind);
$info = ldap_get_entries($cnx, $sr);
?>
User NameStatusEmail Address
<?
for ($x=0; $x
$sam=$info;
$e=$info;
$fullname = $info;
$mail=$info;
$country=$info;
if($e==514){
$status="Disabled";
$isactive=0;
$exec=db_query("UPDATE ost_staff SET isactive = 0 WHERE username = '$sam'");
}
else{
$status="Enabled";
//echo $sam;
if ((!preg_match('+/', $sam)) ){
$usrList = "SELECT username FROM ost_staff WHERE username = '$sam'";
//echo $usrList;
$res=db_query($usrList);
if(!$res || !db_num_rows($res)&& (!empty($mail))){
//echo "failure";
$insert = "INSERT INTO ost_staff (username,firstname,lastname,email,office_id)values('$sam','$fullname','$fullname','$mail',$office)";
$exec = db_query($insert);
//echo $insert;
if(!$exec)
echo "wrong sql ".$insert;
}
//RE014
?>
<?echo $sam;?><?echo $status;?><?echo $mail;?><?echo $country;?>
<?
}
}}?>