php ldap 创建用户,PHP LDAP获取作为组成员的成员的用户详细信息

使用

Sam J Levy创建的出色功能完成了它.

这是最终的代码.

function explode_dn($dn, $with_attributes=0)

{

$result = ldap_explode_dn($dn, $with_attributes);

foreach($result as $key => $value) $result[$key] = preg_replace("/\\\([0-9A-Fa-f]{2})/e", "''.chr(hexdec('\\1')).''", $value);

return $result;

}

function get_members($group,$user,$password) {

$ldap_host = "LDAPSERVER";

$ldap_dn = "OU=some_group,OU=some_group,DC=company,DC=com";

$base_dn = "DC=company,DC=com";

$ldap_usr_dom = "@company.com";

$ldap = ldap_connect($ldap_host);

ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION,3);

ldap_set_option($ldap, LDAP_OPT_REFERRALS,0);

ldap_bind($ldap, $user . $ldap_usr_dom, $password);

$results = ldap_search($ldap,$ldap_dn, "cn=" . $group);

$member_list = ldap_get_entries($ldap, $results);

$dirty = 0;

$group_member_details = array();

foreach($member_list[0]['member'] as $member) {

if($dirty == 0) {

$dirty = 1;

} else {

$member_dn = explode_dn($member);

$member_cn = str_replace("CN=","",$member_dn[0]);

$member_search = ldap_search($ldap, $base_dn, "(CN=" . $member_cn . ")");

$member_details = ldap_get_entries($ldap, $member_search);

$group_member_details[] = array($member_details[0]['givenname'][0],$member_details[0]['sn'][0],$member_details[0]['telephonenumber'][0],$member_details[0]['othertelephone'][0]);

}

}

ldap_close($ldap);

return $group_member_details;

}

// Specify the group from where to get members and a username and password with rights to query it

$result = get_members("groupname","username","password");

// The following will create an XML file with the details from $group_member_details

$xml = simplexml_load_string("<?xml version='1.0'?>\n");

$version = $xml->addChild('version', '1');

foreach($result as $e) {

$contact = $xml->addChild('Contact');

$contact->addChild('FirstName', $e[0]);

$contact->addChild('LastName', $e[1]);

$phone = $contact->addChild('Phone');

if ($e[3] == '') {

$phone->addChild('phonenumber', '0');

} else {

$phone->addChild('phonenumber', $e[3]);

}

$phone->addChild('accountindex', '0');

$phone = $contact->addChild('Phone');

if ($e[2] == '') {

$phone->addChild('phonenumber', '0');

} else {

$phone->addChild('phonenumber', $e[2]);

}

$phone->addChild('accountindex', '1');

$contact->addChild('Group', '0');

$contact->addChild('PhotoUrl', 'empty');

}

$xml->asXML('phonebook.xml');

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值