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