1 | <?php |
---|
2 | /* |
---|
3 | V4.51 29 July 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. |
---|
7 | Set tabs to 8. |
---|
8 | |
---|
9 | |
---|
10 | Joshua Eldridge (joshuae74#hotmail.com) |
---|
11 | */ |
---|
12 | |
---|
13 | // security - hide paths |
---|
14 | if (!defined('ADODB_DIR')) die(); |
---|
15 | |
---|
16 | class ADODB_ldap extends ADOConnection { |
---|
17 | var $databaseType = 'ldap'; |
---|
18 | var $dataProvider = 'ldap'; |
---|
19 | |
---|
20 | # Connection information |
---|
21 | var $username = false; |
---|
22 | var $password = false; |
---|
23 | |
---|
24 | # Used during searches |
---|
25 | var $filter; |
---|
26 | var $dn; |
---|
27 | |
---|
28 | |
---|
29 | function ADODB_ldap() |
---|
30 | { |
---|
31 | |
---|
32 | } |
---|
33 | |
---|
34 | // returns true or false |
---|
35 | |
---|
36 | function _connect( $host, $username, $password, $ldapbase ) |
---|
37 | { |
---|
38 | |
---|
39 | if ( !function_exists( 'ldap_connect' ) ) return null; |
---|
40 | |
---|
41 | $conn_info = array( $host ); |
---|
42 | |
---|
43 | if ( strstr( $host, ':' ) ) { |
---|
44 | $conn_info = split( ':', $host ); |
---|
45 | } |
---|
46 | |
---|
47 | $this->_connectionID = ldap_connect( $conn_info[0], $conn_info[1] ) |
---|
48 | or die( 'Could not connect to ' . $this->_connectionID ); |
---|
49 | if ($username && $password) { |
---|
50 | $bind = ldap_bind( $this->_connectionID, $username, $password ) |
---|
51 | or die( 'Could not bind to ' . $this->_connectionID . ' with $username & $password'); |
---|
52 | } else { |
---|
53 | $bind = ldap_bind( $this->_connectionID ) |
---|
54 | or die( 'Could not bind anonymously to ' . $this->_connectionID ); |
---|
55 | } |
---|
56 | return $this->_connectionID; |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | /* returns _queryID or false */ |
---|
61 | function _query($sql,$inputarr) |
---|
62 | { |
---|
63 | $rs = ldap_search( $this->_connectionID, $this->database, $sql ); |
---|
64 | return $rs; |
---|
65 | |
---|
66 | } |
---|
67 | |
---|
68 | /* closes the LDAP connection */ |
---|
69 | function _close() |
---|
70 | { |
---|
71 | @ldap_close( $this->_connectionID ); |
---|
72 | $this->_connectionID = false; |
---|
73 | } |
---|
74 | |
---|
75 | function ServerInfo() |
---|
76 | { |
---|
77 | if( is_array( $this->version ) ) return $this->version; |
---|
78 | $version = array(); |
---|
79 | /* |
---|
80 | Determines how aliases are handled during search. |
---|
81 | LDAP_DEREF_NEVER (0x00) |
---|
82 | LDAP_DEREF_SEARCHING (0x01) |
---|
83 | LDAP_DEREF_FINDING (0x02) |
---|
84 | LDAP_DEREF_ALWAYS (0x03) |
---|
85 | The LDAP_DEREF_SEARCHING value means aliases are dereferenced during the search but |
---|
86 | not when locating the base object of the search. The LDAP_DEREF_FINDING value means |
---|
87 | aliases are dereferenced when locating the base object but not during the search. |
---|
88 | Default: LDAP_DEREF_NEVER |
---|
89 | */ |
---|
90 | ldap_get_option( $this->_connectionID, LDAP_OPT_DEREF, $version['LDAP_OPT_DEREF'] ) ; |
---|
91 | switch ( $version['LDAP_OPT_DEREF'] ) { |
---|
92 | case 0: |
---|
93 | $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_NEVER'; |
---|
94 | case 1: |
---|
95 | $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_SEARCHING'; |
---|
96 | case 2: |
---|
97 | $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_FINDING'; |
---|
98 | case 3: |
---|
99 | $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_ALWAYS'; |
---|
100 | } |
---|
101 | |
---|
102 | /* |
---|
103 | A limit on the number of entries to return from a search. |
---|
104 | LDAP_NO_LIMIT (0) means no limit. |
---|
105 | Default: LDAP_NO_LIMIT |
---|
106 | */ |
---|
107 | ldap_get_option( $this->_connectionID, LDAP_OPT_SIZELIMIT, $version['LDAP_OPT_SIZELIMIT'] ); |
---|
108 | if ( $version['LDAP_OPT_SIZELIMIT'] == 0 ) { |
---|
109 | $version['LDAP_OPT_SIZELIMIT'] = 'LDAP_NO_LIMIT'; |
---|
110 | } |
---|
111 | |
---|
112 | /* |
---|
113 | A limit on the number of seconds to spend on a search. |
---|
114 | LDAP_NO_LIMIT (0) means no limit. |
---|
115 | Default: LDAP_NO_LIMIT |
---|
116 | */ |
---|
117 | ldap_get_option( $this->_connectionID, LDAP_OPT_TIMELIMIT, $version['LDAP_OPT_TIMELIMIT'] ); |
---|
118 | if ( $version['LDAP_OPT_TIMELIMIT'] == 0 ) { |
---|
119 | $version['LDAP_OPT_TIMELIMIT'] = 'LDAP_NO_LIMIT'; |
---|
120 | } |
---|
121 | |
---|
122 | /* |
---|
123 | Determines whether the LDAP library automatically follows referrals returned by LDAP servers or not. |
---|
124 | LDAP_OPT_ON |
---|
125 | LDAP_OPT_OFF |
---|
126 | Default: ON |
---|
127 | */ |
---|
128 | ldap_get_option( $this->_connectionID, LDAP_OPT_REFERRALS, $version['LDAP_OPT_REFERRALS'] ); |
---|
129 | if ( $version['LDAP_OPT_REFERRALS'] == 0 ) { |
---|
130 | $version['LDAP_OPT_REFERRALS'] = 'LDAP_OPT_OFF'; |
---|
131 | } else { |
---|
132 | $version['LDAP_OPT_REFERRALS'] = 'LDAP_OPT_ON'; |
---|
133 | |
---|
134 | } |
---|
135 | /* |
---|
136 | Determines whether LDAP I/O operations are automatically restarted if they abort prematurely. |
---|
137 | LDAP_OPT_ON |
---|
138 | LDAP_OPT_OFF |
---|
139 | Default: OFF |
---|
140 | */ |
---|
141 | ldap_get_option( $this->_connectionID, LDAP_OPT_RESTART, $version['LDAP_OPT_RESTART'] ); |
---|
142 | if ( $version['LDAP_OPT_RESTART'] == 0 ) { |
---|
143 | $version['LDAP_OPT_RESTART'] = 'LDAP_OPT_OFF'; |
---|
144 | } else { |
---|
145 | $version['LDAP_OPT_RESTART'] = 'LDAP_OPT_ON'; |
---|
146 | |
---|
147 | } |
---|
148 | /* |
---|
149 | This option indicates the version of the LDAP protocol used when communicating with the primary LDAP server. |
---|
150 | LDAP_VERSION2 (2) |
---|
151 | LDAP_VERSION3 (3) |
---|
152 | Default: LDAP_VERSION2 (2) |
---|
153 | */ |
---|
154 | ldap_get_option( $this->_connectionID, LDAP_OPT_PROTOCOL_VERSION, $version['LDAP_OPT_PROTOCOL_VERSION'] ); |
---|
155 | if ( $version['LDAP_OPT_PROTOCOL_VERSION'] == 2 ) { |
---|
156 | $version['LDAP_OPT_PROTOCOL_VERSION'] = 'LDAP_VERSION2'; |
---|
157 | } else { |
---|
158 | $version['LDAP_OPT_PROTOCOL_VERSION'] = 'LDAP_VERSION3'; |
---|
159 | |
---|
160 | } |
---|
161 | /* The host name (or list of hosts) for the primary LDAP server. */ |
---|
162 | ldap_get_option( $this->_connectionID, LDAP_OPT_HOST_NAME, $version['LDAP_OPT_HOST_NAME'] ); |
---|
163 | ldap_get_option( $this->_connectionID, OPT_ERROR_NUMBER, $version['OPT_ERROR_NUMBER'] ); |
---|
164 | ldap_get_option( $this->_connectionID, OPT_ERROR_STRING, $version['OPT_ERROR_STRING'] ); |
---|
165 | ldap_get_option( $this->_connectionID, LDAP_OPT_MATCHED_DN, $version['LDAP_OPT_MATCHED_DN'] ); |
---|
166 | |
---|
167 | return $this->version = $version; |
---|
168 | |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | /*-------------------------------------------------------------------------------------- |
---|
173 | Class Name: Recordset |
---|
174 | --------------------------------------------------------------------------------------*/ |
---|
175 | |
---|
176 | class ADORecordSet_ldap extends ADORecordSet{ |
---|
177 | |
---|
178 | var $databaseType = "ldap"; |
---|
179 | var $canSeek = false; |
---|
180 | var $_entryID; /* keeps track of the entry resource identifier */ |
---|
181 | |
---|
182 | function ADORecordSet_ldap($queryID,$mode=false) |
---|
183 | { |
---|
184 | if ($mode === false) { |
---|
185 | global $ADODB_FETCH_MODE; |
---|
186 | $mode = $ADODB_FETCH_MODE; |
---|
187 | } |
---|
188 | switch ($mode) |
---|
189 | { |
---|
190 | case ADODB_FETCH_NUM: |
---|
191 | $this->fetchMode = LDAP_NUM; |
---|
192 | break; |
---|
193 | case ADODB_FETCH_ASSOC: |
---|
194 | $this->fetchMode = LDAP_ASSOC; |
---|
195 | break; |
---|
196 | default: |
---|
197 | case ADODB_FETCH_DEFAULT: |
---|
198 | case ADODB_FETCH_BOTH: |
---|
199 | $this->fetchMode = LDAP_BOTH; |
---|
200 | break; |
---|
201 | } |
---|
202 | |
---|
203 | $this->ADORecordSet($queryID); |
---|
204 | } |
---|
205 | |
---|
206 | function _initrs() |
---|
207 | { |
---|
208 | /* |
---|
209 | This could be teaked to respect the $COUNTRECS directive from ADODB |
---|
210 | It's currently being used in the _fetch() function and the |
---|
211 | GetAssoc() function |
---|
212 | */ |
---|
213 | $this->_numOfRows = ldap_count_entries( $this->connection->_connectionID, $this->_queryID ); |
---|
214 | |
---|
215 | } |
---|
216 | |
---|
217 | /* |
---|
218 | Return whole recordset as a multi-dimensional associative array |
---|
219 | */ |
---|
220 | function &GetAssoc($force_array = false, $first2cols = false) |
---|
221 | { |
---|
222 | $records = $this->_numOfRows; |
---|
223 | $results = array(); |
---|
224 | for ( $i=0; $i < $records; $i++ ) { |
---|
225 | foreach ( $this->fields as $k=>$v ) { |
---|
226 | if ( is_array( $v ) ) { |
---|
227 | if ( $v['count'] == 1 ) { |
---|
228 | $results[$i][$k] = $v[0]; |
---|
229 | } else { |
---|
230 | array_shift( $v ); |
---|
231 | $results[$i][$k] = $v; |
---|
232 | } |
---|
233 | } |
---|
234 | } |
---|
235 | } |
---|
236 | |
---|
237 | return $results; |
---|
238 | } |
---|
239 | |
---|
240 | function &GetRowAssoc() |
---|
241 | { |
---|
242 | $results = array(); |
---|
243 | foreach ( $this->fields as $k=>$v ) { |
---|
244 | if ( is_array( $v ) ) { |
---|
245 | if ( $v['count'] == 1 ) { |
---|
246 | $results[$k] = $v[0]; |
---|
247 | } else { |
---|
248 | array_shift( $v ); |
---|
249 | $results[$k] = $v; |
---|
250 | } |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | return $results; |
---|
255 | } |
---|
256 | |
---|
257 | function GetRowNums() |
---|
258 | { |
---|
259 | $results = array(); |
---|
260 | foreach ( $this->fields as $k=>$v ) { |
---|
261 | static $i = 0; |
---|
262 | if (is_array( $v )) { |
---|
263 | if ( $v['count'] == 1 ) { |
---|
264 | $results[$i] = $v[0]; |
---|
265 | } else { |
---|
266 | array_shift( $v ); |
---|
267 | $results[$i] = $v; |
---|
268 | } |
---|
269 | $i++; |
---|
270 | } |
---|
271 | } |
---|
272 | return $results; |
---|
273 | } |
---|
274 | |
---|
275 | function _fetch() |
---|
276 | { |
---|
277 | if ( $this->_currentRow >= $this->_numOfRows && $this->_numOfRows >= 0 ) |
---|
278 | return false; |
---|
279 | |
---|
280 | if ( $this->_currentRow == 0 ) { |
---|
281 | $this->_entryID = ldap_first_entry( $this->connection->_connectionID, $this->_queryID ); |
---|
282 | } else { |
---|
283 | $this->_entryID = ldap_next_entry( $this->connection->_connectionID, $this->_entryID ); |
---|
284 | } |
---|
285 | |
---|
286 | $this->fields = ldap_get_attributes( $this->connection->_connectionID, $this->_entryID ); |
---|
287 | $this->_numOfFields = $this->fields['count']; |
---|
288 | switch ( $this->fetchMode ) { |
---|
289 | |
---|
290 | case LDAP_ASSOC: |
---|
291 | $this->fields = $this->GetRowAssoc(); |
---|
292 | break; |
---|
293 | |
---|
294 | case LDAP_NUM: |
---|
295 | $this->fields = $this->GetRowNums(); |
---|
296 | break; |
---|
297 | |
---|
298 | case LDAP_BOTH: |
---|
299 | default: |
---|
300 | break; |
---|
301 | } |
---|
302 | return ( is_array( $this->fields ) ); |
---|
303 | } |
---|
304 | |
---|
305 | function _close() { |
---|
306 | @ldap_free_result( $this->_queryID ); |
---|
307 | $this->_queryID = false; |
---|
308 | } |
---|
309 | |
---|
310 | } |
---|
311 | ?> |
---|