3 V5.18 3 Sep 2012 (c) 2000-2012 John Lim (jlim#natsoft.com). All rights reserved.
4 Released under both BSD license and Lesser GPL library license.
5 Whenever there is any discrepancy between the two licenses,
6 the BSD license will take precedence.
9 Revision 1: (02/25/2005) Updated codebase to include the _inject_bind_options function. This allows
10 users to access the options in the ldap_set_option function appropriately. Most importantly
11 LDAP Version 3 is now supported. See the examples for more information. Also fixed some minor
12 bugs that surfaced when PHP error levels were set high.
14 Joshua Eldridge (joshuae74#hotmail.com)
17 // security - hide paths
18 if (!defined('ADODB_DIR')) die();
20 if (!defined('LDAP_ASSOC')) {
21 define('LDAP_ASSOC',ADODB_FETCH_ASSOC);
22 define('LDAP_NUM',ADODB_FETCH_NUM);
23 define('LDAP_BOTH',ADODB_FETCH_BOTH);
26 class ADODB_ldap extends ADOConnection {
27 var $databaseType = 'ldap';
28 var $dataProvider = 'ldap';
30 # Connection information
31 var $username = false;
32 var $password = false;
34 # Used during searches
40 # Options configuration information
41 var $LDAP_CONNECT_OPTIONS;
43 # error on binding, eg. "Binding: invalid credentials"
44 var $_bind_errmsg = "Binding: %s";
50 // returns true or false
52 function _connect( $host, $username, $password, $ldapbase)
54 global $LDAP_CONNECT_OPTIONS;
56 if ( !function_exists( 'ldap_connect' ) ) return null;
58 if (strpos($host,'ldap://') === 0 || strpos($host,'ldaps://') === 0) {
59 $this->_connectionID = @ldap_connect($host);
61 $conn_info = array( $host,$this->port);
63 if ( strstr( $host, ':' ) ) {
64 $conn_info = explode( ':', $host );
67 $this->_connectionID = @ldap_connect( $conn_info[0], $conn_info[1] );
69 if (!$this->_connectionID) {
70 $e = 'Could not connect to ' . $conn_info[0];
71 $this->_errorMsg = $e;
72 if ($this->debug) ADOConnection::outp($e);
75 if( count( $LDAP_CONNECT_OPTIONS ) > 0 ) {
76 $this->_inject_bind_options( $LDAP_CONNECT_OPTIONS );
80 $bind = @ldap_bind( $this->_connectionID, $username, $password );
82 $username = 'anonymous';
83 $bind = @ldap_bind( $this->_connectionID );
87 $e = sprintf($this->_bind_errmsg,ldap_error($this->_connectionID));
88 $this->_errorMsg = $e;
89 if ($this->debug) ADOConnection::outp($e);
92 $this->_errorMsg = '';
93 $this->database = $ldapbase;
94 return $this->_connectionID;
98 Valid Domain Values for LDAP Options:
100 LDAP_OPT_DEREF (integer)
101 LDAP_OPT_SIZELIMIT (integer)
102 LDAP_OPT_TIMELIMIT (integer)
103 LDAP_OPT_PROTOCOL_VERSION (integer)
104 LDAP_OPT_ERROR_NUMBER (integer)
105 LDAP_OPT_REFERRALS (boolean)
106 LDAP_OPT_RESTART (boolean)
107 LDAP_OPT_HOST_NAME (string)
108 LDAP_OPT_ERROR_STRING (string)
109 LDAP_OPT_MATCHED_DN (string)
110 LDAP_OPT_SERVER_CONTROLS (array)
111 LDAP_OPT_CLIENT_CONTROLS (array)
113 Make sure to set this BEFORE calling Connect()
117 $LDAP_CONNECT_OPTIONS = Array(
119 "OPTION_NAME"=>LDAP_OPT_DEREF,
123 "OPTION_NAME"=>LDAP_OPT_SIZELIMIT,
127 "OPTION_NAME"=>LDAP_OPT_TIMELIMIT,
131 "OPTION_NAME"=>LDAP_OPT_PROTOCOL_VERSION,
135 "OPTION_NAME"=>LDAP_OPT_ERROR_NUMBER,
139 "OPTION_NAME"=>LDAP_OPT_REFERRALS,
140 "OPTION_VALUE"=>FALSE
143 "OPTION_NAME"=>LDAP_OPT_RESTART,
144 "OPTION_VALUE"=>FALSE
149 function _inject_bind_options( $options ) {
150 foreach( $options as $option ) {
151 ldap_set_option( $this->_connectionID, $option["OPTION_NAME"], $option["OPTION_VALUE"] )
152 or die( "Unable to set server option: " . $option["OPTION_NAME"] );
156 /* returns _queryID or false */
157 function _query($sql,$inputarr=false)
159 $rs = @ldap_search( $this->_connectionID, $this->database, $sql );
160 $this->_errorMsg = ($rs) ? '' : 'Search error on '.$sql.': '.ldap_error($this->_connectionID);
166 return $this->_errorMsg;
171 return @ldap_errno($this->_connectionID);
174 /* closes the LDAP connection */
177 @ldap_close( $this->_connectionID );
178 $this->_connectionID = false;
181 function SelectDB($db) {
182 $this->database = $db;
186 function ServerInfo()
188 if( !empty( $this->version ) ) return $this->version;
191 Determines how aliases are handled during search.
192 LDAP_DEREF_NEVER (0x00)
193 LDAP_DEREF_SEARCHING (0x01)
194 LDAP_DEREF_FINDING (0x02)
195 LDAP_DEREF_ALWAYS (0x03)
196 The LDAP_DEREF_SEARCHING value means aliases are dereferenced during the search but
197 not when locating the base object of the search. The LDAP_DEREF_FINDING value means
198 aliases are dereferenced when locating the base object but not during the search.
199 Default: LDAP_DEREF_NEVER
201 ldap_get_option( $this->_connectionID, LDAP_OPT_DEREF, $version['LDAP_OPT_DEREF'] ) ;
202 switch ( $version['LDAP_OPT_DEREF'] ) {
204 $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_NEVER';
206 $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_SEARCHING';
208 $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_FINDING';
210 $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_ALWAYS';
214 A limit on the number of entries to return from a search.
215 LDAP_NO_LIMIT (0) means no limit.
216 Default: LDAP_NO_LIMIT
218 ldap_get_option( $this->_connectionID, LDAP_OPT_SIZELIMIT, $version['LDAP_OPT_SIZELIMIT'] );
219 if ( $version['LDAP_OPT_SIZELIMIT'] == 0 ) {
220 $version['LDAP_OPT_SIZELIMIT'] = 'LDAP_NO_LIMIT';
224 A limit on the number of seconds to spend on a search.
225 LDAP_NO_LIMIT (0) means no limit.
226 Default: LDAP_NO_LIMIT
228 ldap_get_option( $this->_connectionID, LDAP_OPT_TIMELIMIT, $version['LDAP_OPT_TIMELIMIT'] );
229 if ( $version['LDAP_OPT_TIMELIMIT'] == 0 ) {
230 $version['LDAP_OPT_TIMELIMIT'] = 'LDAP_NO_LIMIT';
234 Determines whether the LDAP library automatically follows referrals returned by LDAP servers or not.
239 ldap_get_option( $this->_connectionID, LDAP_OPT_REFERRALS, $version['LDAP_OPT_REFERRALS'] );
240 if ( $version['LDAP_OPT_REFERRALS'] == 0 ) {
241 $version['LDAP_OPT_REFERRALS'] = 'LDAP_OPT_OFF';
243 $version['LDAP_OPT_REFERRALS'] = 'LDAP_OPT_ON';
247 Determines whether LDAP I/O operations are automatically restarted if they abort prematurely.
252 ldap_get_option( $this->_connectionID, LDAP_OPT_RESTART, $version['LDAP_OPT_RESTART'] );
253 if ( $version['LDAP_OPT_RESTART'] == 0 ) {
254 $version['LDAP_OPT_RESTART'] = 'LDAP_OPT_OFF';
256 $version['LDAP_OPT_RESTART'] = 'LDAP_OPT_ON';
260 This option indicates the version of the LDAP protocol used when communicating with the primary LDAP server.
263 Default: LDAP_VERSION2 (2)
265 ldap_get_option( $this->_connectionID, LDAP_OPT_PROTOCOL_VERSION, $version['LDAP_OPT_PROTOCOL_VERSION'] );
266 if ( $version['LDAP_OPT_PROTOCOL_VERSION'] == 2 ) {
267 $version['LDAP_OPT_PROTOCOL_VERSION'] = 'LDAP_VERSION2';
269 $version['LDAP_OPT_PROTOCOL_VERSION'] = 'LDAP_VERSION3';
272 /* The host name (or list of hosts) for the primary LDAP server. */
273 ldap_get_option( $this->_connectionID, LDAP_OPT_HOST_NAME, $version['LDAP_OPT_HOST_NAME'] );
274 ldap_get_option( $this->_connectionID, LDAP_OPT_ERROR_NUMBER, $version['LDAP_OPT_ERROR_NUMBER'] );
275 ldap_get_option( $this->_connectionID, LDAP_OPT_ERROR_STRING, $version['LDAP_OPT_ERROR_STRING'] );
276 ldap_get_option( $this->_connectionID, LDAP_OPT_MATCHED_DN, $version['LDAP_OPT_MATCHED_DN'] );
278 return $this->version = $version;
283 /*--------------------------------------------------------------------------------------
284 Class Name: Recordset
285 --------------------------------------------------------------------------------------*/
287 class ADORecordSet_ldap extends ADORecordSet{
289 var $databaseType = "ldap";
290 var $canSeek = false;
291 var $_entryID; /* keeps track of the entry resource identifier */
293 function ADORecordSet_ldap($queryID,$mode=false)
295 if ($mode === false) {
296 global $ADODB_FETCH_MODE;
297 $mode = $ADODB_FETCH_MODE;
301 case ADODB_FETCH_NUM:
302 $this->fetchMode = LDAP_NUM;
304 case ADODB_FETCH_ASSOC:
305 $this->fetchMode = LDAP_ASSOC;
307 case ADODB_FETCH_DEFAULT:
308 case ADODB_FETCH_BOTH:
310 $this->fetchMode = LDAP_BOTH;
314 $this->ADORecordSet($queryID);
320 This could be teaked to respect the $COUNTRECS directive from ADODB
321 It's currently being used in the _fetch() function and the
324 $this->_numOfRows = ldap_count_entries( $this->connection->_connectionID, $this->_queryID );
329 Return whole recordset as a multi-dimensional associative array
331 function GetAssoc($force_array = false, $first2cols = false)
333 $records = $this->_numOfRows;
335 for ( $i=0; $i < $records; $i++ ) {
336 foreach ( $this->fields as $k=>$v ) {
337 if ( is_array( $v ) ) {
338 if ( $v['count'] == 1 ) {
339 $results[$i][$k] = $v[0];
342 $results[$i][$k] = $v;
351 function GetRowAssoc()
354 foreach ( $this->fields as $k=>$v ) {
355 if ( is_array( $v ) ) {
356 if ( $v['count'] == 1 ) {
357 $results[$k] = $v[0];
368 function GetRowNums()
371 foreach ( $this->fields as $k=>$v ) {
373 if (is_array( $v )) {
374 if ( $v['count'] == 1 ) {
375 $results[$i] = $v[0];
388 if ( $this->_currentRow >= $this->_numOfRows && $this->_numOfRows >= 0 )
391 if ( $this->_currentRow == 0 ) {
392 $this->_entryID = ldap_first_entry( $this->connection->_connectionID, $this->_queryID );
394 $this->_entryID = ldap_next_entry( $this->connection->_connectionID, $this->_entryID );
397 $this->fields = ldap_get_attributes( $this->connection->_connectionID, $this->_entryID );
398 $this->_numOfFields = $this->fields['count'];
399 switch ( $this->fetchMode ) {
402 $this->fields = $this->GetRowAssoc();
406 $this->fields = array_merge($this->GetRowNums(),$this->GetRowAssoc());
411 $this->fields = $this->GetRowNums();
414 return ( is_array( $this->fields ) );
418 @ldap_free_result( $this->_queryID );
419 $this->_queryID = false;