Source for file adodb-ldap.inc.php
Documentation is available at adodb-ldap.inc.php
V4.97 22 Jan 2008 (c) 2000-2008 John Lim (jlim#natsoft.com.my). All rights reserved.
Released under both BSD license and Lesser GPL library license.
Whenever there is any discrepancy between the two licenses,
the BSD license will take precedence.
Revision 1: (02/25/2005) Updated codebase to include the _inject_bind_options function. This allows
users to access the options in the ldap_set_option function appropriately. Most importantly
LDAP Version 3 is now supported. See the examples for more information. Also fixed some minor
bugs that surfaced when PHP error levels were set high.
Joshua Eldridge (joshuae74#hotmail.com)
define('LDAP_ASSOC',ADODB_FETCH_ASSOC);
define('LDAP_NUM',ADODB_FETCH_NUM);
define('LDAP_BOTH',ADODB_FETCH_BOTH);
# Options configuration information
# error on binding, eg. "Binding: invalid credentials"
function _connect( $host, $username, $password, $ldapbase)
global $LDAP_CONNECT_OPTIONS;
$conn_info = array( $host,$this->port);
$conn_info = split( ':', $host );
$this->_connectionID = @ldap_connect( $conn_info[0], $conn_info[1] );
$e = 'Could not connect to ' . $conn_info[0];
if( count( $LDAP_CONNECT_OPTIONS ) > 0 ) {
$bind = @ldap_bind( $this->_connectionID, $username, $password );
Valid Domain Values for LDAP Options:
LDAP_OPT_SIZELIMIT (integer)
LDAP_OPT_TIMELIMIT (integer)
LDAP_OPT_PROTOCOL_VERSION (integer)
LDAP_OPT_ERROR_NUMBER (integer)
LDAP_OPT_REFERRALS (boolean)
LDAP_OPT_RESTART (boolean)
LDAP_OPT_HOST_NAME (string)
LDAP_OPT_ERROR_STRING (string)
LDAP_OPT_MATCHED_DN (string)
LDAP_OPT_SERVER_CONTROLS (array)
LDAP_OPT_CLIENT_CONTROLS (array)
Make sure to set this BEFORE calling Connect()
$LDAP_CONNECT_OPTIONS = Array(
"OPTION_NAME"=>LDAP_OPT_DEREF,
"OPTION_NAME"=>LDAP_OPT_SIZELIMIT,
"OPTION_NAME"=>LDAP_OPT_TIMELIMIT,
"OPTION_NAME"=>LDAP_OPT_PROTOCOL_VERSION,
"OPTION_NAME"=>LDAP_OPT_ERROR_NUMBER,
"OPTION_NAME"=>LDAP_OPT_REFERRALS,
"OPTION_NAME"=>LDAP_OPT_RESTART,
foreach( $options as $option ) {
ldap_set_option( $this->_connectionID, $option["OPTION_NAME"], $option["OPTION_VALUE"] )
or die( "Unable to set server option: " . $option["OPTION_NAME"] );
/* returns _queryID or false */
function _query($sql,$inputarr)
/* closes the LDAP connection */
Determines how aliases are handled during search.
LDAP_DEREF_SEARCHING (0x01)
LDAP_DEREF_FINDING (0x02)
The LDAP_DEREF_SEARCHING value means aliases are dereferenced during the search but
not when locating the base object of the search. The LDAP_DEREF_FINDING value means
aliases are dereferenced when locating the base object but not during the search.
Default: LDAP_DEREF_NEVER
ldap_get_option( $this->_connectionID, LDAP_OPT_DEREF, $version['LDAP_OPT_DEREF'] ) ;
switch ( $version['LDAP_OPT_DEREF'] ) {
$version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_NEVER';
$version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_SEARCHING';
$version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_FINDING';
$version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_ALWAYS';
A limit on the number of entries to return from a search.
LDAP_NO_LIMIT (0) means no limit.
ldap_get_option( $this->_connectionID, LDAP_OPT_SIZELIMIT, $version['LDAP_OPT_SIZELIMIT'] );
if ( $version['LDAP_OPT_SIZELIMIT'] == 0 ) {
$version['LDAP_OPT_SIZELIMIT'] = 'LDAP_NO_LIMIT';
A limit on the number of seconds to spend on a search.
LDAP_NO_LIMIT (0) means no limit.
ldap_get_option( $this->_connectionID, LDAP_OPT_TIMELIMIT, $version['LDAP_OPT_TIMELIMIT'] );
if ( $version['LDAP_OPT_TIMELIMIT'] == 0 ) {
$version['LDAP_OPT_TIMELIMIT'] = 'LDAP_NO_LIMIT';
Determines whether the LDAP library automatically follows referrals returned by LDAP servers or not.
ldap_get_option( $this->_connectionID, LDAP_OPT_REFERRALS, $version['LDAP_OPT_REFERRALS'] );
if ( $version['LDAP_OPT_REFERRALS'] == 0 ) {
$version['LDAP_OPT_REFERRALS'] = 'LDAP_OPT_OFF';
$version['LDAP_OPT_REFERRALS'] = 'LDAP_OPT_ON';
Determines whether LDAP I/O operations are automatically restarted if they abort prematurely.
ldap_get_option( $this->_connectionID, LDAP_OPT_RESTART, $version['LDAP_OPT_RESTART'] );
if ( $version['LDAP_OPT_RESTART'] == 0 ) {
$version['LDAP_OPT_RESTART'] = 'LDAP_OPT_OFF';
$version['LDAP_OPT_RESTART'] = 'LDAP_OPT_ON';
This option indicates the version of the LDAP protocol used when communicating with the primary LDAP server.
Default: LDAP_VERSION2 (2)
ldap_get_option( $this->_connectionID, LDAP_OPT_PROTOCOL_VERSION, $version['LDAP_OPT_PROTOCOL_VERSION'] );
if ( $version['LDAP_OPT_PROTOCOL_VERSION'] == 2 ) {
$version['LDAP_OPT_PROTOCOL_VERSION'] = 'LDAP_VERSION2';
$version['LDAP_OPT_PROTOCOL_VERSION'] = 'LDAP_VERSION3';
/* The host name (or list of hosts) for the primary LDAP server. */
ldap_get_option( $this->_connectionID, LDAP_OPT_HOST_NAME, $version['LDAP_OPT_HOST_NAME'] );
ldap_get_option( $this->_connectionID, LDAP_OPT_ERROR_NUMBER, $version['LDAP_OPT_ERROR_NUMBER'] );
ldap_get_option( $this->_connectionID, LDAP_OPT_ERROR_STRING, $version['LDAP_OPT_ERROR_STRING'] );
ldap_get_option( $this->_connectionID, LDAP_OPT_MATCHED_DN, $version['LDAP_OPT_MATCHED_DN'] );
/*--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------*/
var $_entryID; /* keeps track of the entry resource identifier */
global $ADODB_FETCH_MODE;
$mode = $ADODB_FETCH_MODE;
This could be teaked to respect the $COUNTRECS directive from ADODB
It's currently being used in the _fetch() function and the
Return whole recordset as a multi-dimensional associative array
function &GetAssoc($force_array = false, $first2cols = false)
for ( $i= 0; $i < $records; $i++ ) {
foreach ( $this->fields as $k=> $v ) {
if ( $v['count'] == 1 ) {
$results[$i][$k] = $v[0];
foreach ( $this->fields as $k=> $v ) {
if ( $v['count'] == 1 ) {
foreach ( $this->fields as $k=> $v ) {
if ( $v['count'] == 1 ) {
|