| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* This is a short example on how to search for entries in your
|
|
|
4 |
* directory using Net_LDAP2.
|
|
|
5 |
*/
|
|
|
6 |
|
|
|
7 |
// We use the connecting.php example to get a link to our server.
|
|
|
8 |
// This file will also include all required basic Net_LDAP2 classes.
|
|
|
9 |
include_once 'connecting.php';
|
|
|
10 |
|
|
|
11 |
// Okay, we should have a valid link now.
|
|
|
12 |
// We must now define a filter that defines, which
|
|
|
13 |
// entries we want to have returned.
|
|
|
14 |
// We use the Net_LDAP2_Filter class for this purpose,
|
|
|
15 |
// because so we don't need to worry about
|
|
|
16 |
// RFC-2254 ;)
|
|
|
17 |
// In this example, we want all users with first names
|
|
|
18 |
// starting with "bened" and the last names ending with "ger".
|
|
|
19 |
// Additionally, we want to exclude all users with names
|
|
|
20 |
// containing "smith", which will be done throug a "negation".
|
|
|
21 |
|
|
|
22 |
// Basic filter building
|
|
|
23 |
$filter_sn = Net_LDAP2_Filter::create('gn', 'begins', 'bened');
|
|
|
24 |
$filter_gn = Net_LDAP2_Filter::create('sn', 'ends', 'ger');
|
|
|
25 |
|
|
|
26 |
// Building and negating the "no smith" filter component
|
|
|
27 |
// this must be done in two steps, because
|
|
|
28 |
// you are able to negate EVERY filter, not just leave filters.
|
|
|
29 |
// the $filter_smith will not be used afterwards and is only
|
|
|
30 |
// necessary for negation here.
|
|
|
31 |
$filter_smith = Net_LDAP2_Filter::create('sn', 'contains','smith');
|
|
|
32 |
$filter_nosmith = Net_LDAP2_Filter::combine('not', $filter_smith);
|
|
|
33 |
|
|
|
34 |
// Now combine all filter components to build our search filter
|
|
|
35 |
$filter = Net_LDAP2_Filter::combine('and', array($filter_sn, $filter_gn, $filter_nosmith));
|
|
|
36 |
|
|
|
37 |
|
|
|
38 |
// The filter is ready now, so we can
|
|
|
39 |
// use this filter now to search for entries.
|
|
|
40 |
// The scope we use is "sub", meaning "all entries below the search base".
|
|
|
41 |
// The base is "null", meaning the base defined in $ldap_config. This is similar
|
|
|
42 |
// to call $ldap->search($ldap_config['base'], ...
|
|
|
43 |
$requested_attributes = array('sn','gn','telephonenumber');
|
|
|
44 |
$search = $ldap->search(null, $filter, array('attributes' => $requested_attributes));
|
|
|
45 |
if (Net_LDAP2::isError($search)) {
|
|
|
46 |
die('LDAP search failed: '.$search->getMessage());
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
// Lets see what entries we got and print the names and telephone numbers:
|
|
|
50 |
if ($search->count() > 0) {
|
|
|
51 |
echo "Found ".$search->count().' entries:<br>';
|
|
|
52 |
|
|
|
53 |
// Note, this is is only one of several ways to fetch entries!
|
|
|
54 |
// You can also retrieve all entries in an array with
|
|
|
55 |
// $entries = $search->entries()
|
|
|
56 |
// or the same thing sorted:
|
|
|
57 |
// $entries = $search->sorted()
|
|
|
58 |
// Since Net_LDAP2 you can also use a foreach loop:
|
|
|
59 |
// foreach ($search as $dn => $entry) {
|
|
|
60 |
while ($entry = $search->shiftEntry()) {
|
|
|
61 |
$surename = $entry->getValue('sn', 'single');
|
|
|
62 |
if (Net_LDAP2::isError($surename)) {
|
|
|
63 |
die('Unable to get surename: '.$surename->getMessage());
|
|
|
64 |
}
|
|
|
65 |
$givenname = $entry->getValue('gn', 'single');
|
|
|
66 |
if (Net_LDAP2::isError($givenname)) {
|
|
|
67 |
die('Unable to get givenname: '.$givenname->getMessage());
|
|
|
68 |
}
|
|
|
69 |
$phone = $entry->getValue('telephonenumber', 'single');
|
|
|
70 |
if (Net_LDAP2::isError($phone)) {
|
|
|
71 |
die('Unable to get phone number: '.$phone->getMessage());
|
|
|
72 |
}
|
|
|
73 |
echo "<br>$givenname $surename: $phone";
|
|
|
74 |
}
|
|
|
75 |
} else {
|
|
|
76 |
die('Sorry, no entries found!');
|
|
|
77 |
}
|
|
|
78 |
?>
|