| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* This is a short example on how to add a new entry to 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 define the DN of the new entry. The DN is the
|
|
|
13 |
// global unique path to the data in the directory server,
|
|
|
14 |
// similar to a path name in your filesystem.
|
|
|
15 |
// Since we want to be a little flexible, we make the base
|
|
|
16 |
// dynamic, so it is enough to change the base-dn in your
|
|
|
17 |
// $ldap_config array.
|
|
|
18 |
$dn = 'cn=Foo Bar,'.$ldap_config['base'];
|
|
|
19 |
|
|
|
20 |
|
|
|
21 |
// It is a good idea to first look if the entry, that should be added,
|
|
|
22 |
// is already present:
|
|
|
23 |
if ($ldap->dnExists($dn)) {
|
|
|
24 |
die('Could not add entry! Entry already exists!');
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
// The entry does not exist so far, we can safely add him.
|
|
|
28 |
// But first, we must construct the entry.
|
|
|
29 |
// This is, because Net_LDAP2 was build to make changes only
|
|
|
30 |
// locally (in your script), not directly on the server.
|
|
|
31 |
$attributes = array(
|
|
|
32 |
'sn' => 'Foo',
|
|
|
33 |
'gn' => 'Bar',
|
|
|
34 |
'mail' => array('foo@example.org', 'bar@example2.org'),
|
|
|
35 |
'employeeNumber' => 123456
|
|
|
36 |
);
|
|
|
37 |
$new_entry = Net_LDAP2_Entry::createFresh($dn, $attributes);
|
|
|
38 |
|
|
|
39 |
// Finally add the entry in the server:
|
|
|
40 |
$result = $ldap->add($new_entry);
|
|
|
41 |
if (Net_LDAP2::isError($result)) {
|
|
|
42 |
die('Unable to add entry: '.$result->getMessage());
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
// The entry is now present in the directory server.
|
|
|
46 |
// Additionally, it is linked to the $ldap connection used for the add(),
|
|
|
47 |
// so you may call $entry->modify() (and friends) and $entry->update()
|
|
|
48 |
// without the need for passing an $ldap object.
|
|
|
49 |
// This is only the case if the entry was not linked to an Net_LDAP2 object
|
|
|
50 |
// before, so if the entry object would be fetched from a $ldap object
|
|
|
51 |
// and then added to $ldap_2, the link of the entry remains to $ldap,
|
|
|
52 |
// thus any update() will be performed on directory1 ($ldap).
|
|
|
53 |
?>
|