Benutzer aus Active Directory (LDAP) mit letztem Anmeldedatum auslesen

Mi, 30.09.2009 - 20:14 -- admin

$ldap_url = 'server';
$ldap_domain = 'domain.local';
$ldap_dn = "dc=domain,dc=local";
 
$ds = ldap_connect( $ldap_url );
  ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
  ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
 
$username = "username";
$password = "password";
 
#now try a real login
 $login = ldap_bind( $ds, "$username@$ldap_domain", $password );
#$login = ldap_bind( $ds ); anonym binding; should not work in windows ad
 $attributes = array("displayname", "mail","department","title","lastlogon","lastlogontimestamp",);
 
#only persons
 $filter = "(&(objectCategory=person))";
 
$result = ldap_search($ds, $ldap_dn, $filter, $attributes);
 
$entries_1 = ldap_get_entries($ds, $result);
 
$all=array();
foreach ($entries_1 as $entries) {
	if (strlen($entries['displayname'][0])>0) {
	         $t=array(
	           "displayName"=>$entries['displayname'][0],
	           "email"=>$entries['email'][0],
	           "department"=>$entries['department'][0],
	           "title"=>$entries['title'][0],
	           "lastlogontimestamp"=>win_filetime_to_timestamp($entries['lastlogontimestamp'][0]),
#	           "lastlogon"=>win_filetime_to_timestamp($entries['lastlogon'][0]),
	           );
 
                  if (time()-$t['lastlogontimestamp']>60*60*24*30 AND $t['lastlogontimestamp']<time()) { $t['lastlogontimestamp']='<b>'.date("d.m.y H:i",$t['lastlogontimestamp']).'</b>'; } else { $t['lastlogontimestamp']=date("d.m.y H:i",$t['lastlogontimestamp']); }
#                  if (time()-$t['lastlogon']>60*60*24*30 AND $t['lastlogon']<time()) { $t['lastlogon']='<b>'.date("d.m.y H:i",$t['lastlogon']).'</b>'; } else { $t['lastlogon']=date("d.m.y H:i",$t['lastlogon']); }
	         $all[]=$t;
	}
}
$all=array_msort($all,array('displayName'=>array(SORT_ASC)));
print_r($all);
 
ldap_unbind($ds);
 
 
/**
 * Converts LDAP (Active Directory) FILETIME Date to Unixtime 
 * 
 * If you're dealing with Active Directory and need to get values like 'lastlogon', 'pwdlastset' or similar, you'll notice that AD gives the values as Windows FILETIME timestamps. That means, the values are 100-nanosecond units passed since 1.1.1600 00:00:00.
 * To convert these to unix timestamps which PHP's date functions understand, one easy way would be the following
 * 
 * http://de.php.net/ldap_get_entries
 * reaper at sci dot fi - 18-Jun-2003 01:09
 *
 * @param string $filetime
 * @return unixtime
 */
function win_filetime_to_timestamp ($filetime) {
	$win_secs = substr($filetime,0,strlen($filetime)-7); // divide by 10 000 000 to get seconds
	$unix_timestamp = ($win_secs - 11644473600); // 1.1.1600 -> 1.1.1970 difference in seconds
	return $unix_timestamp;
}
 
/**
 * sort an multiarray on cols
 * 
 * A more inuitive way of sorting multidimensional arrays using array_msort() in just one line,
 * you don't have to divide the original array into per-column-arrays
 *
 * http://de3.php.net/manual/en/function.array-multisort.php
 * cagret at gmail dot com - 21-Jun-2009 10:38
 *  
 * @example 
 * $arr1 = array(
 *   array('id'=>1,'name'=>'aA','cat'=>'cc'),
 *   array('id'=>2,'name'=>'aa','cat'=>'dd'),
 *   array('id'=>3,'name'=>'bb','cat'=>'cc'),
 *   array('id'=>4,'name'=>'bb','cat'=>'dd')
 * );
 * $arr2 = array_msort($arr1, array('name'=>SORT_DESC, 'cat'=>SORT_ASC));
 *
 * @param array $array
 * @param sortbyarray $cols
 * @return array
 */
function array_msort($array, $cols)
{
    $colarr = array();
    foreach ($cols as $col => $order) {
        $colarr[$col] = array();
        foreach ($array as $k => $row) { $colarr[$col]['_'.$k] = strtolower($row[$col]); }
    }
    $params = array();
    foreach ($cols as $col => $order) {
        $params[] =& $colarr[$col];
        $params = array_merge($params, (array)$order);
    }
    call_user_func_array('array_multisort', $params);
    $ret = array();
    $keys = array();
    $first = true;
    foreach ($colarr as $col => $arr) {
        foreach ($arr as $k => $v) {
            if ($first) { $keys[$k] = substr($k,1); }
            $k = $keys[$k];
            if (!isset($ret[$k])) $ret[$k] = $array[$k];
            $ret[$k][$col] = $array[$k][$col];
        }
        $first = false;
    }
    return $ret;
 
}