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
14 var $databaseType = 'ldap';
15 var $dataProvider = 'ldap';
17 # Connection information
18 var $username = false;
19 var $password = false;
21 # Used during searches
30 // returns true or false
32 function _connect($host, $username, $password, $ldapbase)
35 if (!function_exists('ldap_connect')) return false;
37 $conn_info = array($host);
39 if (strstr($host, ':')) {
40 $conn_info = explode(':', $host);
43 $this->_connectionID = ldap_connect($conn_info[0], $conn_info[1])
44 or die('Could not connect to ' . $this->_connectionID);
45 if ($username && $password) {
46 $bind = ldap_bind($this->_connectionID, $username, $password)
47 or die('Could not bind to ' . $this->_connectionID . ' with $username & $password');
49 $bind = ldap_bind($this->_connectionID)
50 or die('Could not bind anonymously to ' . $this->_connectionID);
52 return $this->_connectionID;
55 /* returns _queryID or false */
56 function _query($sql, $inputarr)
58 $rs = ldap_search($this->_connectionID, $this->database, $sql);
63 /* closes the LDAP connection */
66 @ldap_close($this->_connectionID);
67 $this->_connectionID = false;
72 if (is_array($this->version)) return $this->version;
75 Determines how aliases are handled during search.
76 LDAP_DEREF_NEVER (0x00)
77 LDAP_DEREF_SEARCHING (0x01)
78 LDAP_DEREF_FINDING (0x02)
79 LDAP_DEREF_ALWAYS (0x03)
80 The LDAP_DEREF_SEARCHING value means aliases are dereferenced during the search but
81 not when locating the base object of the search. The LDAP_DEREF_FINDING value means
82 aliases are dereferenced when locating the base object but not during the search.
83 Default: LDAP_DEREF_NEVER
85 ldap_get_option($this->_connectionID, LDAP_OPT_DEREF, $version['LDAP_OPT_DEREF']);
86 switch ($version['LDAP_OPT_DEREF']) {
88 $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_NEVER';
90 $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_SEARCHING';
92 $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_FINDING';
94 $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_ALWAYS';
98 A limit on the number of entries to return from a search.
99 LDAP_NO_LIMIT (0) means no limit.
100 Default: LDAP_NO_LIMIT
102 ldap_get_option($this->_connectionID, LDAP_OPT_SIZELIMIT, $version['LDAP_OPT_SIZELIMIT']);
103 if ($version['LDAP_OPT_SIZELIMIT'] == 0) {
104 $version['LDAP_OPT_SIZELIMIT'] = 'LDAP_NO_LIMIT';
108 A limit on the number of seconds to spend on a search.
109 LDAP_NO_LIMIT (0) means no limit.
110 Default: LDAP_NO_LIMIT
112 ldap_get_option($this->_connectionID, LDAP_OPT_TIMELIMIT, $version['LDAP_OPT_TIMELIMIT']);
113 if ($version['LDAP_OPT_TIMELIMIT'] == 0) {
114 $version['LDAP_OPT_TIMELIMIT'] = 'LDAP_NO_LIMIT';
118 Determines whether the LDAP library automatically follows referrals returned by LDAP servers or not.
123 ldap_get_option($this->_connectionID, LDAP_OPT_REFERRALS, $version['LDAP_OPT_REFERRALS']);
124 if ($version['LDAP_OPT_REFERRALS'] == 0) {
125 $version['LDAP_OPT_REFERRALS'] = 'LDAP_OPT_OFF';
127 $version['LDAP_OPT_REFERRALS'] = 'LDAP_OPT_ON';
131 Determines whether LDAP I/O operations are automatically restarted if they abort prematurely.
136 ldap_get_option($this->_connectionID, LDAP_OPT_RESTART, $version['LDAP_OPT_RESTART']);
137 if ($version['LDAP_OPT_RESTART'] == 0) {
138 $version['LDAP_OPT_RESTART'] = 'LDAP_OPT_OFF';
140 $version['LDAP_OPT_RESTART'] = 'LDAP_OPT_ON';
144 This option indicates the version of the LDAP protocol used when communicating with the primary LDAP server.
147 Default: LDAP_VERSION2 (2)
149 ldap_get_option($this->_connectionID, LDAP_OPT_PROTOCOL_VERSION, $version['LDAP_OPT_PROTOCOL_VERSION']);
150 if ($version['LDAP_OPT_PROTOCOL_VERSION'] == 2) {
151 $version['LDAP_OPT_PROTOCOL_VERSION'] = 'LDAP_VERSION2';
153 $version['LDAP_OPT_PROTOCOL_VERSION'] = 'LDAP_VERSION3';
156 /* The host name (or list of hosts) for the primary LDAP server. */
157 ldap_get_option($this->_connectionID, LDAP_OPT_HOST_NAME, $version['LDAP_OPT_HOST_NAME']);
158 ldap_get_option($this->_connectionID, OPT_ERROR_NUMBER, $version['OPT_ERROR_NUMBER']);
159 ldap_get_option($this->_connectionID, OPT_ERROR_STRING, $version['OPT_ERROR_STRING']);
160 ldap_get_option($this->_connectionID, LDAP_OPT_MATCHED_DN, $version['LDAP_OPT_MATCHED_DN']);
162 return $this->version = $version;
167 /*--------------------------------------------------------------------------------------
168 Class Name: Recordset
169 --------------------------------------------------------------------------------------*/
171 class ADORecordSet_ldap extends ADORecordSet
174 var $databaseType = "ldap";
175 var $canSeek = false;
176 var $_entryID; /* keeps track of the entry resource identifier */
178 function ADORecordSet_ldap($queryID, $mode = false)
180 if ($mode === false) {
181 global $ADODB_FETCH_MODE;
182 $mode = $ADODB_FETCH_MODE;
185 case ADODB_FETCH_NUM:
186 $this->fetchMode = LDAP_NUM;
188 case ADODB_FETCH_ASSOC:
189 $this->fetchMode = LDAP_ASSOC;
192 case ADODB_FETCH_DEFAULT:
193 case ADODB_FETCH_BOTH:
194 $this->fetchMode = LDAP_BOTH;
198 $this->ADORecordSet($queryID);
204 This could be teaked to respect the $COUNTRECS directive from ADODB
205 It's currently being used in the _fetch() function and the
208 $this->_numOfRows = ldap_count_entries($this->connection->_connectionID, $this->_queryID);
213 Return whole recordset as a multi-dimensional associative array
215 function &GetAssoc($force_array = false, $first2cols = false)
217 $records = $this->_numOfRows;
219 for ($i = 0; $i < $records; $i++) {
220 foreach ($this->fields as $k => $v) {
222 if ($v['count'] == 1) {
223 $results[$i][$k] = $v[0];
226 $results[$i][$k] = $v;
235 function &GetRowAssoc()
238 foreach ($this->fields as $k => $v) {
240 if ($v['count'] == 1) {
241 $results[$k] = $v[0];
252 function GetRowNums()
255 foreach ($this->fields as $k => $v) {
258 if ($v['count'] == 1) {
259 $results[$i] = $v[0];
272 if ($this->_currentRow >= $this->_numOfRows && $this->_numOfRows >= 0)
275 if ($this->_currentRow == 0) {
276 $this->_entryID = ldap_first_entry($this->connection->_connectionID, $this->_queryID);
278 $this->_entryID = ldap_next_entry($this->connection->_connectionID, $this->_entryID);
281 $this->fields = ldap_get_attributes($this->connection->_connectionID, $this->_entryID);
282 $this->_numOfFields = $this->fields['count'];
283 switch ($this->fetchMode) {
286 $this->fields = $this->GetRowAssoc();
290 $this->fields = $this->GetRowNums();
297 return (is_array($this->fields));
302 @ldap_free_result($this->_queryID);
303 $this->_queryID = false;