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.
9 Joshua Eldridge (joshuae74#hotmail.com)
12 class ADODB_ldap extends ADOConnection {
13 var $databaseType = 'ldap';
14 var $dataProvider = 'ldap';
16 # Connection information
17 var $username = false;
18 var $password = false;
20 # Used during searches
29 // returns true or false
31 function _connect( $host, $username, $password, $ldapbase )
34 if ( !function_exists( 'ldap_connect' ) ) return false;
36 $conn_info = array( $host );
38 if ( strstr( $host, ':' ) ) {
39 $conn_info = explode(':', $host);
42 $this->_connectionID = ldap_connect( $conn_info[0], $conn_info[1] )
43 or die( 'Could not connect to ' . $this->_connectionID );
44 if ($username && $password) {
45 $bind = ldap_bind( $this->_connectionID, $username, $password )
46 or die( 'Could not bind to ' . $this->_connectionID . ' with $username & $password');
48 $bind = ldap_bind( $this->_connectionID )
49 or die( 'Could not bind anonymously to ' . $this->_connectionID );
51 return $this->_connectionID;
54 /* returns _queryID or false */
55 function _query($sql,$inputarr)
57 $rs = ldap_search( $this->_connectionID, $this->database, $sql );
62 /* closes the LDAP connection */
65 @ldap_close( $this->_connectionID );
66 $this->_connectionID = false;
71 if( is_array( $this->version ) ) return $this->version;
74 Determines how aliases are handled during search.
75 LDAP_DEREF_NEVER (0x00)
76 LDAP_DEREF_SEARCHING (0x01)
77 LDAP_DEREF_FINDING (0x02)
78 LDAP_DEREF_ALWAYS (0x03)
79 The LDAP_DEREF_SEARCHING value means aliases are dereferenced during the search but
80 not when locating the base object of the search. The LDAP_DEREF_FINDING value means
81 aliases are dereferenced when locating the base object but not during the search.
82 Default: LDAP_DEREF_NEVER
84 ldap_get_option( $this->_connectionID, LDAP_OPT_DEREF, $version['LDAP_OPT_DEREF'] ) ;
85 switch ( $version['LDAP_OPT_DEREF'] ) {
87 $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_NEVER';
89 $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_SEARCHING';
91 $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_FINDING';
93 $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_ALWAYS';
97 A limit on the number of entries to return from a search.
98 LDAP_NO_LIMIT (0) means no limit.
99 Default: LDAP_NO_LIMIT
101 ldap_get_option( $this->_connectionID, LDAP_OPT_SIZELIMIT, $version['LDAP_OPT_SIZELIMIT'] );
102 if ( $version['LDAP_OPT_SIZELIMIT'] == 0 ) {
103 $version['LDAP_OPT_SIZELIMIT'] = 'LDAP_NO_LIMIT';
107 A limit on the number of seconds to spend on a search.
108 LDAP_NO_LIMIT (0) means no limit.
109 Default: LDAP_NO_LIMIT
111 ldap_get_option( $this->_connectionID, LDAP_OPT_TIMELIMIT, $version['LDAP_OPT_TIMELIMIT'] );
112 if ( $version['LDAP_OPT_TIMELIMIT'] == 0 ) {
113 $version['LDAP_OPT_TIMELIMIT'] = 'LDAP_NO_LIMIT';
117 Determines whether the LDAP library automatically follows referrals returned by LDAP servers or not.
122 ldap_get_option( $this->_connectionID, LDAP_OPT_REFERRALS, $version['LDAP_OPT_REFERRALS'] );
123 if ( $version['LDAP_OPT_REFERRALS'] == 0 ) {
124 $version['LDAP_OPT_REFERRALS'] = 'LDAP_OPT_OFF';
126 $version['LDAP_OPT_REFERRALS'] = 'LDAP_OPT_ON';
130 Determines whether LDAP I/O operations are automatically restarted if they abort prematurely.
135 ldap_get_option( $this->_connectionID, LDAP_OPT_RESTART, $version['LDAP_OPT_RESTART'] );
136 if ( $version['LDAP_OPT_RESTART'] == 0 ) {
137 $version['LDAP_OPT_RESTART'] = 'LDAP_OPT_OFF';
139 $version['LDAP_OPT_RESTART'] = 'LDAP_OPT_ON';
143 This option indicates the version of the LDAP protocol used when communicating with the primary LDAP server.
146 Default: LDAP_VERSION2 (2)
148 ldap_get_option( $this->_connectionID, LDAP_OPT_PROTOCOL_VERSION, $version['LDAP_OPT_PROTOCOL_VERSION'] );
149 if ( $version['LDAP_OPT_PROTOCOL_VERSION'] == 2 ) {
150 $version['LDAP_OPT_PROTOCOL_VERSION'] = 'LDAP_VERSION2';
152 $version['LDAP_OPT_PROTOCOL_VERSION'] = 'LDAP_VERSION3';
155 /* The host name (or list of hosts) for the primary LDAP server. */
156 ldap_get_option( $this->_connectionID, LDAP_OPT_HOST_NAME, $version['LDAP_OPT_HOST_NAME'] );
157 ldap_get_option( $this->_connectionID, OPT_ERROR_NUMBER, $version['OPT_ERROR_NUMBER'] );
158 ldap_get_option( $this->_connectionID, OPT_ERROR_STRING, $version['OPT_ERROR_STRING'] );
159 ldap_get_option( $this->_connectionID, LDAP_OPT_MATCHED_DN, $version['LDAP_OPT_MATCHED_DN'] );
161 return $this->version = $version;
166 /*--------------------------------------------------------------------------------------
167 Class Name: Recordset
168 --------------------------------------------------------------------------------------*/
170 class ADORecordSet_ldap extends ADORecordSet{
172 var $databaseType = "ldap";
173 var $canSeek = false;
174 var $_entryID; /* keeps track of the entry resource identifier */
176 function ADORecordSet_ldap($queryID,$mode=false)
178 if ($mode === false) {
179 global $ADODB_FETCH_MODE;
180 $mode = $ADODB_FETCH_MODE;
184 case ADODB_FETCH_NUM:
185 $this->fetchMode = LDAP_NUM;
187 case ADODB_FETCH_ASSOC:
188 $this->fetchMode = LDAP_ASSOC;
191 case ADODB_FETCH_DEFAULT:
192 case ADODB_FETCH_BOTH:
193 $this->fetchMode = LDAP_BOTH;
197 $this->ADORecordSet($queryID);
203 This could be teaked to respect the $COUNTRECS directive from ADODB
204 It's currently being used in the _fetch() function and the
207 $this->_numOfRows = ldap_count_entries( $this->connection->_connectionID, $this->_queryID );
212 Return whole recordset as a multi-dimensional associative array
214 function &GetAssoc($force_array = false, $first2cols = false)
216 $records = $this->_numOfRows;
218 for ( $i=0; $i < $records; $i++ ) {
219 foreach ( $this->fields as $k=>$v ) {
220 if ( is_array( $v ) ) {
221 if ( $v['count'] == 1 ) {
222 $results[$i][$k] = $v[0];
225 $results[$i][$k] = $v;
234 function &GetRowAssoc()
237 foreach ( $this->fields as $k=>$v ) {
238 if ( is_array( $v ) ) {
239 if ( $v['count'] == 1 ) {
240 $results[$k] = $v[0];
251 function GetRowNums()
254 foreach ( $this->fields as $k=>$v ) {
256 if (is_array( $v )) {
257 if ( $v['count'] == 1 ) {
258 $results[$i] = $v[0];
271 if ( $this->_currentRow >= $this->_numOfRows && $this->_numOfRows >= 0 )
274 if ( $this->_currentRow == 0 ) {
275 $this->_entryID = ldap_first_entry( $this->connection->_connectionID, $this->_queryID );
277 $this->_entryID = ldap_next_entry( $this->connection->_connectionID, $this->_entryID );
280 $this->fields = ldap_get_attributes( $this->connection->_connectionID, $this->_entryID );
281 $this->_numOfFields = $this->fields['count'];
282 switch ( $this->fetchMode ) {
285 $this->fields = $this->GetRowAssoc();
289 $this->fields = $this->GetRowNums();
296 return ( is_array( $this->fields ) );
300 @ldap_free_result( $this->_queryID );
301 $this->_queryID = false;