View difference between Paste ID: xH67tE6p and
SHOW: | | - or go back to the newest paste.
1-
1+
<?php
2
3
function explode_dn($dn, $with_attributes=0)
4
{
5
    $result = ldap_explode_dn($dn, $with_attributes);
6
    foreach($result as $key => $value) $result[$key] = preg_replace("/\\\([0-9A-Fa-f]{2})/e", "''.chr(hexdec('\\1')).''", $value);
7
    return $result;
8
}
9
10
function get_members($group,$user,$password) {
11
	$ldap_host = "LDAPSERVER";
12
	$ldap_dn = "OU=some_group,OU=some_group,DC=company,DC=com";
13
	$base_dn = "DC=company,DC=com";
14
	$ldap_usr_dom = "@company.com";
15
	$ldap = ldap_connect($ldap_host);
16
17
	ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION,3);
18
	ldap_set_option($ldap, LDAP_OPT_REFERRALS,0);
19
20
	ldap_bind($ldap, $user . $ldap_usr_dom, $password);
21
	$results = ldap_search($ldap,$ldap_dn, "cn=" . $group);
22
	$member_list = ldap_get_entries($ldap, $results);
23
24
	$dirty = 0;
25
	$group_member_details = array();
26
27
	foreach($member_list[0]['member'] as $member) {
28
		if($dirty == 0) {
29
			$dirty = 1;
30
		} else {
31
			$member_dn = explode_dn($member);
32
			$member_cn = str_replace("CN=","",$member_dn[0]);
33
			$member_search = ldap_search($ldap, $base_dn, "(CN=" . $member_cn . ")");
34
			$member_details = ldap_get_entries($ldap, $member_search);
35
			$group_member_details[] = array($member_details[0]['givenname'][0],$member_details[0]['sn'][0],$member_details[0]['telephonenumber'][0],$member_details[0]['othertelephone'][0]);
36
		}
37
	}
38
	ldap_close($ldap);
39
	return $group_member_details;
40
}
41
42
// Specify the group from where to get members and a username and password with rights to query it
43
$result = get_members("groupname","username","password");
44
45
$xml = simplexml_load_string("<?xml version='1.0'?>\n<AddressBook></AddressBook>");
46
$version = $xml->addChild('version', '1');
47
48
foreach($result as $e) {
49
	$contact = $xml->addChild('Contact');
50
	$contact->addChild('FirstName', $e[0]);
51
	$contact->addChild('LastName', $e[1]);
52
	$phone = $contact->addChild('Phone');
53
	if ($e[3] == '') {
54
                $phone->addChild('phonenumber', '0');
55
        } else {
56
                $phone->addChild('phonenumber', $e[3]);
57
        }
58
	$phone->addChild('accountindex', '0');
59
	$phone = $contact->addChild('Phone');
60
	if ($e[2] == '') {
61
		$phone->addChild('phonenumber', '0');
62
	} else {
63
		$phone->addChild('phonenumber', $e[2]);
64
	}
65
	$phone->addChild('accountindex', '1');
66
	$contact->addChild('Group', '0');
67
	$contact->addChild('PhotoUrl', 'empty');
68
}
69
70
$xml->asXML('phonebook.xml');
71
72
?>