<?php
//根据 属组名 搜索里面的成员 $full_property_name 是 DN ,是 很长的字串符 比如 "CN=AAA,OU=BBB,,DC=baidu,DC=com,DC=cn"
function _get_ad_info_from_ad_by_property_name($full_property_name, $ad_user_property = array('*'), $diqu = 'js')
{
$ad_connect_infos = _my_get_ad_connect_infos();
$ad_user = $ad_connect_infos[$diqu]['ad_user']; //域用户名 包含域名
$ad_pwd = $ad_connect_infos[$diqu]['ad_pwd'];
$host = $ad_connect_infos[$diqu]['host'];
$port = $ad_connect_infos[$diqu]['port'];//一般都是389
$conn = ldap_connect($host, $port); //不要写成ldap_connect($host.':'.$port)的形式
if ($conn) {
//设置参数
ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3); //声明使用版本3
ldap_set_option($conn, LDAP_OPT_REFERRALS, 0); // Binding to ldap server
$bd = ldap_bind($conn, $ad_user, $ad_pwd);
$basedn = $ad_connect_infos[$diqu]['basedn'];
if ($_GET['q'] == 'query_ad_info_ext' && $diqu = 'js') {//这是一种范围更大的 basedn
$basedn = 'dc=luxshare,dc=com,dc=cn';
} else if ($_GET['q'] == 'checking_ad_pwd' && $diqu = 'js') {//检查密码的时候,也检查所有的 范围更大的 basedn 吧
$basedn = 'dc=baidu,dc=com,dc=cn';
}else{
$basedn = 'dc=baidu,dc=com,dc=cn';
}
$groupDn = $full_property_name;
# 下面这一行,过滤器很重要
$filter = "(&(objectCategory=person)(objectClass=user)(memberOf:1.2.840.113556.1.4.1941:=$groupDn))";
$justthese = $ad_user_property; //选择要获取的用户属性
$pageSize = 5000;
$cookie = '';
$infos = array();
do {
ldap_control_paged_result($conn, $pageSize, true, $cookie);
$sr = ldap_search($conn, $basedn, $filter, $justthese);
$tmp_infos = ldap_get_entries($conn, $sr);
$infos = array_merge($infos, $tmp_infos);
ldap_control_paged_result_response($conn, $sr, $cookie);
} while ($cookie !== null && $cookie != '');
if ($bd) {
//drupal_set_message('LDAP 连接成功'); //相当于登录成功
} else {
$infos = false;
drupal_set_message('LDAP 连接失败', 'warning');
}
} else {
$infos = false;
drupal_set_message('无法连接到AD域服务器', 'error');
}
ldap_close($conn);
return $infos;
}