3 V4.22 15 Apr 2004 (c) 2000-2004 John Lim (jlim#natsoft.com.my). 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.
10 Joshua Eldridge (joshuae74#hotmail.com)
14 class ADODB_ldap extends ADOConnection {
15 var $databaseType = 'ldap';
16 var $dataProvider = 'ldap';
18 # Connection information
19 var $username = false;
20 var $password = false;
22 # Used during searches
32 // returns true or false
34 function _connect( $host, $username, $password, $ldapbase )
37 if ( !function_exists( 'ldap_connect' ) ) return false;
39 $conn_info = array( $host );
41 if ( strstr( $host, ':' ) ) {
42 $conn_info = explode(':', $host);
45 $this->_connectionID = ldap_connect( $conn_info[0], $conn_info[1] )
46 or die( 'Could not connect to ' . $this->_connectionID );
47 if ($username && $password) {
48 $bind = ldap_bind( $this->_connectionID, $username, $password )
49 or die( 'Could not bind to ' . $this->_connectionID . ' with $username & $password');
51 $bind = ldap_bind( $this->_connectionID )
52 or die( 'Could not bind anonymously to ' . $this->_connectionID );
54 return $this->_connectionID;
58 /* returns _queryID or false */
59 function _query($sql,$inputarr)
61 $rs = ldap_search( $this->_connectionID, $this->database, $sql );
66 /* closes the LDAP connection */
69 @ldap_close( $this->_connectionID );
70 $this->_connectionID = false;
75 if( is_array( $this->version ) ) return $this->version;
78 Determines how aliases are handled during search.
79 LDAP_DEREF_NEVER (0x00)
80 LDAP_DEREF_SEARCHING (0x01)
81 LDAP_DEREF_FINDING (0x02)
82 LDAP_DEREF_ALWAYS (0x03)
83 The LDAP_DEREF_SEARCHING value means aliases are dereferenced during the search but
84 not when locating the base object of the search. The LDAP_DEREF_FINDING value means
85 aliases are dereferenced when locating the base object but not during the search.
86 Default: LDAP_DEREF_NEVER
88 ldap_get_option( $this->_connectionID, LDAP_OPT_DEREF, $version['LDAP_OPT_DEREF'] ) ;
89 switch ( $version['LDAP_OPT_DEREF'] ) {
91 $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_NEVER';
93 $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_SEARCHING';
95 $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_FINDING';
97 $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_ALWAYS';
101 A limit on the number of entries to return from a search.
102 LDAP_NO_LIMIT (0) means no limit.
103 Default: LDAP_NO_LIMIT
105 ldap_get_option( $this->_connectionID, LDAP_OPT_SIZELIMIT, $version['LDAP_OPT_SIZELIMIT'] );
106 if ( $version['LDAP_OPT_SIZELIMIT'] == 0 ) {
107 $version['LDAP_OPT_SIZELIMIT'] = 'LDAP_NO_LIMIT';
111 A limit on the number of seconds to spend on a search.
112 LDAP_NO_LIMIT (0) means no limit.
113 Default: LDAP_NO_LIMIT
115 ldap_get_option( $this->_connectionID, LDAP_OPT_TIMELIMIT, $version['LDAP_OPT_TIMELIMIT'] );
116 if ( $version['LDAP_OPT_TIMELIMIT'] == 0 ) {
117 $version['LDAP_OPT_TIMELIMIT'] = 'LDAP_NO_LIMIT';
121 Determines whether the LDAP library automatically follows referrals returned by LDAP servers or not.
126 ldap_get_option( $this->_connectionID, LDAP_OPT_REFERRALS, $version['LDAP_OPT_REFERRALS'] );
127 if ( $version['LDAP_OPT_REFERRALS'] == 0 ) {
128 $version['LDAP_OPT_REFERRALS'] = 'LDAP_OPT_OFF';
130 $version['LDAP_OPT_REFERRALS'] = 'LDAP_OPT_ON';
134 Determines whether LDAP I/O operations are automatically restarted if they abort prematurely.
139 ldap_get_option( $this->_connectionID, LDAP_OPT_RESTART, $version['LDAP_OPT_RESTART'] );
140 if ( $version['LDAP_OPT_RESTART'] == 0 ) {
141 $version['LDAP_OPT_RESTART'] = 'LDAP_OPT_OFF';
143 $version['LDAP_OPT_RESTART'] = 'LDAP_OPT_ON';
147 This option indicates the version of the LDAP protocol used when communicating with the primary LDAP server.
150 Default: LDAP_VERSION2 (2)
152 ldap_get_option( $this->_connectionID, LDAP_OPT_PROTOCOL_VERSION, $version['LDAP_OPT_PROTOCOL_VERSION'] );
153 if ( $version['LDAP_OPT_PROTOCOL_VERSION'] == 2 ) {
154 $version['LDAP_OPT_PROTOCOL_VERSION'] = 'LDAP_VERSION2';
156 $version['LDAP_OPT_PROTOCOL_VERSION'] = 'LDAP_VERSION3';
159 /* The host name (or list of hosts) for the primary LDAP server. */
160 ldap_get_option( $this->_connectionID, LDAP_OPT_HOST_NAME, $version['LDAP_OPT_HOST_NAME'] );
161 ldap_get_option( $this->_connectionID, OPT_ERROR_NUMBER, $version['OPT_ERROR_NUMBER'] );
162 ldap_get_option( $this->_connectionID, OPT_ERROR_STRING, $version['OPT_ERROR_STRING'] );
163 ldap_get_option( $this->_connectionID, LDAP_OPT_MATCHED_DN, $version['LDAP_OPT_MATCHED_DN'] );
165 return $this->version = $version;
170 /*--------------------------------------------------------------------------------------
171 Class Name: Recordset
172 --------------------------------------------------------------------------------------*/
174 class ADORecordSet_ldap extends ADORecordSet{
176 var $databaseType = "ldap";
177 var $canSeek = false;
178 var $_entryID; /* keeps track of the entry resource identifier */
180 function ADORecordSet_ldap($queryID,$mode=false)
182 if ($mode === false) {
183 global $ADODB_FETCH_MODE;
184 $mode = $ADODB_FETCH_MODE;
188 case ADODB_FETCH_NUM:
189 $this->fetchMode = LDAP_NUM;
191 case ADODB_FETCH_ASSOC:
192 $this->fetchMode = LDAP_ASSOC;
195 case ADODB_FETCH_DEFAULT:
196 case ADODB_FETCH_BOTH:
197 $this->fetchMode = LDAP_BOTH;
201 $this->ADORecordSet($queryID);
207 This could be teaked to respect the $COUNTRECS directive from ADODB
208 It's currently being used in the _fetch() function and the
211 $this->_numOfRows = ldap_count_entries( $this->connection->_connectionID, $this->_queryID );
216 Return whole recordset as a multi-dimensional associative array
218 function &GetAssoc($force_array = false, $first2cols = false)
220 $records = $this->_numOfRows;
222 for ( $i=0; $i < $records; $i++ ) {
223 foreach ( $this->fields as $k=>$v ) {
224 if ( is_array( $v ) ) {
225 if ( $v['count'] == 1 ) {
226 $results[$i][$k] = $v[0];
229 $results[$i][$k] = $v;
238 function &GetRowAssoc()
241 foreach ( $this->fields as $k=>$v ) {
242 if ( is_array( $v ) ) {
243 if ( $v['count'] == 1 ) {
244 $results[$k] = $v[0];
255 function GetRowNums()
258 foreach ( $this->fields as $k=>$v ) {
260 if (is_array( $v )) {
261 if ( $v['count'] == 1 ) {
262 $results[$i] = $v[0];
275 if ( $this->_currentRow >= $this->_numOfRows && $this->_numOfRows >= 0 )
278 if ( $this->_currentRow == 0 ) {
279 $this->_entryID = ldap_first_entry( $this->connection->_connectionID, $this->_queryID );
281 $this->_entryID = ldap_next_entry( $this->connection->_connectionID, $this->_entryID );
284 $this->fields = ldap_get_attributes( $this->connection->_connectionID, $this->_entryID );
285 $this->_numOfFields = $this->fields['count'];
286 switch ( $this->fetchMode ) {
289 $this->fields = $this->GetRowAssoc();
293 $this->fields = $this->GetRowNums();
300 return ( is_array( $this->fields ) );
304 @ldap_free_result( $this->_queryID );
305 $this->_queryID = false;