To do this, use the Net_LDAP::connect function like this:
require_once('Net_LDAP/LDAP.php'); $config = array ( 'binddn' => 'uid=tarjei,dc=php,dc=net', 'bindpw' => 'secret', 'basedn' => dc=php,dc=net ); $ldap = Net_LDAP::connect($config);
But what are valid values in the config array?
Now you should have the base ldapobject stored in the variable "$ldap". But, what if it is an error? Net_LDAP returns a Net_LDAP_error object (basicly a pear_error object) when an error occurs. So wherever you need to check an error, do like this:
$ldap = Net_LDAP::connect($config); // copied from above! if (Net_LDAP::isError($ldap)) { print $ldap->getMessage(); // this will tell you what went wrong! }
Two things to note:
1) The function is_a() might be faster:
if (is_a($ldap,'net_ldap_error')) { // do the same as above }In PHP5 you must use the instanceof operator instead of is_a().
Most of the work you do on an ldapserver is in searching,
for example, you search for your boss's password or his wife's phonenumber.
Searching an ldapserver is a bit like doing SQL and a lot not like it at all.
Think of the directory as some sort of "telephone book".
Basically, searches are performed by applying a "filter" to objects under a
specific "base" in the directory. Additionally, there is a "scope" applied to the search,
so you can specify the recursion level in the directory tree.
$filter = '(&(objectclass=person)(sn=Ha*))'; $searchbase = 'ou=dev,ou=People,dc=php,dc=net'; $options = array( 'scope' => 'sub', // all entries below the searchbase (recursive all subtrees from there) 'attributes' => array('sn','gn','telephonenumber') // what attributes to select ); $search = $ldap->search($searchbase, $filter, $options);$search should now be an Net_LDAP_Search object.
This describes how to get an entry and modifying it. If we just want one single entry, it may be useful to directly fetch that entry instead of searching it manually. To do this you can use Net_LDAPs "getEntry()" method:
$dn = 'cn=Foo Bar,ou=dev,ou=People,dc=php,dc=net'; $entry =& $ldap->getEntry($dn, array('sn','gn','telephonenumber'));With this entry object you now can perform some actions like fetching the contents of attributes:
$telephonenumber = $entry->getValue('telephonenumber','single');Or you can modify a attribute:
$entry->replace("telephonenumber" => "0123456789"); // replace the attributes values with the new number $entry->update(); // update temporarily modified entry on the serverOf course there are much more other possibilitys. Please note that adding and deleting whole entrys is performed through the Net_LDAP class and not with the Net_LDAP_Entry class.
$schema = $ldap->schema();Now you got a schemaobject. To read from this schemaobject, you have several methods defined in the class Net_LDAP_Schema.
$required = $schema->must( 'inetOrgUser' ); print_r($required); /* The output of this will be: Array ( [0] => sn [1] => cn ) */Ok, but what kind of attribute is sn? Let's check:
$att = $schema->get('attribute','sn'); print_r($att); /* The output of this will be: Array ( [aliases] => Array ( [0] => surname ) [oid] => 2.5.4.4 [name] => sn [desc] => RFC2256: last (family) name(s) for which the entity is known by [sup] => Array ( [0] => name ) [type] => attribute ) */Hmm, ok, the sup part is important. It means that surname derives it's syntax from another attribute, the name attribute. So , we need to check that as well.
$att_dep = $schema->get('attribute',$att['sup'][0]); print_r($att_dep); /* The output of this will be: Array ( [aliases] => Array ( ) [oid] => 2.5.4.41 [name] => name [desc] => RFC2256: common supertype of name attributes [equality] => caseIgnoreMatch [substr] => caseIgnoreSubstringsMatch [syntax] => 1.3.6.1.4.1.1466.115.121.1.15{32768} [max_length] => 32768 [type] => attribute ) */From this we find out that the attribute has a maxlength of 32768 characters and has the syntax 1.3.6.1.4.1.1466.115.121.1.15{32768}.