Ldap :: Object encapsulation of php's ldap functions
This library provides a class encapsulation of php's ldap functions. This might be very useful for mocking during unit testing or if you simply prefer the beauty of OOP.
This library is no longer maintained and its usage is discouraged. As a replacement, please take a look at Dreamscapes/Ldap-Core which offers similar functionality.
- Class Ldap\Ldap provides function encapsulation of all important php ldap_* functions
- Class Ldap\Option provides you with a known ldap options as class constants
- Class Ldap\Response provides a nice way to handle server responses
- Class Ldap\ResponseCode defines most of the known server response codes for you to use in your implementations
- PHP 5.4.0 and newer with LDAP support ( setup instructions )
- OpenSSL module for SSL / TLS connections ( setup instructions )
composer require alaneor/ldap:dev-master
( visit Packagist for list of all available versions )
You use the
Ldap\Ldap class to connect to an ldap server. Simply construct the instance with the proper server hostname/IP address and optional port ( default is 389 ) and then use any of the below described functions to work with the connection.
// Include Composer's autoloader include 'vendor/autoload.php'; // Open the ldap connection $link = new Ldap\Ldap( 'example.com', 389 ); // Authenticate the connection with the Admin account $link->bind( 'CN=Admin,DC=example,DC=com', 'MySecretPwd!' ); // List the items that are in the baseDN $response = $link->ldap_list( 'DC=example,DC=com', 'objectclass=*', ['name', 'objectclass'] ); // Take a look at the structure of the Ldap\Response instance print_r( $response );
There are a few rules that generally apply to the method names and their parameters.
- A method's name is the function's name, stripped of the leading ldap_ prefix. Where a syntax error would occur ( e.g. ldap_list -> list or ldap_8859_to_t61 -> 8859_to_t61 ) the prefix is kept.
resource $link_identifierparameter is omitted in all situations ( the link identifier is stored in the instance of
- Where a
resource $result_identifieris expected, you pass an instance of
Ldap\Responseclass ( e.g. in the
Ldap\Ldap::sort()method ) that is returned for all ldap method calls.
- For all other function parameters and its default values, standard php documentation applies.
There are two exceptions to the above naming rules:
The pagination control request is even shorter, for your convenience:
list cannot be used as method name, all lookup functions are defined with their prefixes to keep them consistent:
Here's a list of methods you can use.
Class methods do not return an instance of Ldap\Response but directly the output of the mapped function.
Ldap\Ldap::resource()-> get the ldap resource identifier
Ldap\Ldap::rootDSE()-> read the rootDSE entry of the ldap server
Each method call returns new instance of the
The structure of the response is as follows:
Ldap\Response::result- Whatever the ldap function returned, either a boolean, a resource or anything else
Ldap\Response::data- If a function returned a resource, the actual ldap data will be already extracted here
Ldap\Response::code- The ldap response code of the operation performed
Ldap\Response::message- The ldap response message that corresponds to the response code
Ldap\Response::referrals- If the server responds with referrals, you will find them here
Ldap\Response::cookie- For paged result responses, a cookie will be here, if returned from server
Ldap\Response::estimated- The estimated number of objects remaining to return from server when doing paged searches ( not all ldap implementations return this value )
Ldap\Response::matchedDN- Not much is known here; read php's documentation about ldap_parse_result()
Not all properties have values in all situations - some of them are only present when doing specific actions, like the cookie - it will only be present when pagination is enabled, a lookup operation has been executed and the server returned a cookie.
This software is licensed under the BSD (3-Clause) License. See the LICENSE file for more information.