Separate Display and Search of Organization, and person names
[openemr.git] / library / adodb / drivers / adodb-ldap.inc.php
blob16f38ad755fb0101695b7cbeb7fe9b9d37165034
1 <?php
2 /*
3 V5.14 8 Sept 2011 (c) 2000-2011 John Lim (jlim#natsoft.com). 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.
9 Revision 1: (02/25/2005) Updated codebase to include the _inject_bind_options function. This allows
10 users to access the options in the ldap_set_option function appropriately. Most importantly
11 LDAP Version 3 is now supported. See the examples for more information. Also fixed some minor
12 bugs that surfaced when PHP error levels were set high.
14 Joshua Eldridge (joshuae74#hotmail.com)
15 */
17 // security - hide paths
18 if (!defined('ADODB_DIR')) die();
20 if (!defined('LDAP_ASSOC')) {
21 define('LDAP_ASSOC',ADODB_FETCH_ASSOC);
22 define('LDAP_NUM',ADODB_FETCH_NUM);
23 define('LDAP_BOTH',ADODB_FETCH_BOTH);
26 class ADODB_ldap extends ADOConnection {
27 var $databaseType = 'ldap';
28 var $dataProvider = 'ldap';
30 # Connection information
31 var $username = false;
32 var $password = false;
34 # Used during searches
35 var $filter;
36 var $dn;
37 var $version;
38 var $port = 389;
40 # Options configuration information
41 var $LDAP_CONNECT_OPTIONS;
43 # error on binding, eg. "Binding: invalid credentials"
44 var $_bind_errmsg = "Binding: %s";
46 function ADODB_ldap()
50 // returns true or false
52 function _connect( $host, $username, $password, $ldapbase)
54 global $LDAP_CONNECT_OPTIONS;
56 if ( !function_exists( 'ldap_connect' ) ) return null;
58 if (strpos('ldap://',$host) === 0 || strpos('ldaps://',$host) === 0) {
59 $this->_connectionID = @ldap_connect($host);
60 } else {
61 $conn_info = array( $host,$this->port);
63 if ( strstr( $host, ':' ) ) {
64 $conn_info = explode( ':', $host );
67 $this->_connectionID = @ldap_connect( $conn_info[0], $conn_info[1] );
69 if (!$this->_connectionID) {
70 $e = 'Could not connect to ' . $conn_info[0];
71 $this->_errorMsg = $e;
72 if ($this->debug) ADOConnection::outp($e);
73 return false;
75 if( count( $LDAP_CONNECT_OPTIONS ) > 0 ) {
76 $this->_inject_bind_options( $LDAP_CONNECT_OPTIONS );
79 if ($username) {
80 $bind = @ldap_bind( $this->_connectionID, $username, $password );
81 } else {
82 $username = 'anonymous';
83 $bind = @ldap_bind( $this->_connectionID );
86 if (!$bind) {
87 $e = sprintf($this->_bind_errmsg,ldap_error($this->_connectionID));
88 $this->_errorMsg = $e;
89 if ($this->debug) ADOConnection::outp($e);
90 return false;
92 $this->_errorMsg = '';
93 $this->database = $ldapbase;
94 return $this->_connectionID;
98 Valid Domain Values for LDAP Options:
100 LDAP_OPT_DEREF (integer)
101 LDAP_OPT_SIZELIMIT (integer)
102 LDAP_OPT_TIMELIMIT (integer)
103 LDAP_OPT_PROTOCOL_VERSION (integer)
104 LDAP_OPT_ERROR_NUMBER (integer)
105 LDAP_OPT_REFERRALS (boolean)
106 LDAP_OPT_RESTART (boolean)
107 LDAP_OPT_HOST_NAME (string)
108 LDAP_OPT_ERROR_STRING (string)
109 LDAP_OPT_MATCHED_DN (string)
110 LDAP_OPT_SERVER_CONTROLS (array)
111 LDAP_OPT_CLIENT_CONTROLS (array)
113 Make sure to set this BEFORE calling Connect()
115 Example:
117 $LDAP_CONNECT_OPTIONS = Array(
118 Array (
119 "OPTION_NAME"=>LDAP_OPT_DEREF,
120 "OPTION_VALUE"=>2
122 Array (
123 "OPTION_NAME"=>LDAP_OPT_SIZELIMIT,
124 "OPTION_VALUE"=>100
126 Array (
127 "OPTION_NAME"=>LDAP_OPT_TIMELIMIT,
128 "OPTION_VALUE"=>30
130 Array (
131 "OPTION_NAME"=>LDAP_OPT_PROTOCOL_VERSION,
132 "OPTION_VALUE"=>3
134 Array (
135 "OPTION_NAME"=>LDAP_OPT_ERROR_NUMBER,
136 "OPTION_VALUE"=>13
138 Array (
139 "OPTION_NAME"=>LDAP_OPT_REFERRALS,
140 "OPTION_VALUE"=>FALSE
142 Array (
143 "OPTION_NAME"=>LDAP_OPT_RESTART,
144 "OPTION_VALUE"=>FALSE
149 function _inject_bind_options( $options ) {
150 foreach( $options as $option ) {
151 ldap_set_option( $this->_connectionID, $option["OPTION_NAME"], $option["OPTION_VALUE"] )
152 or die( "Unable to set server option: " . $option["OPTION_NAME"] );
156 /* returns _queryID or false */
157 function _query($sql,$inputarr=false)
159 $rs = @ldap_search( $this->_connectionID, $this->database, $sql );
160 $this->_errorMsg = ($rs) ? '' : 'Search error on '.$sql.': '.ldap_error($this->_connectionID);
161 return $rs;
164 function ErrorMsg()
166 return $this->_errorMsg;
169 function ErrorNo()
171 return @ldap_errno($this->_connectionID);
174 /* closes the LDAP connection */
175 function _close()
177 @ldap_close( $this->_connectionID );
178 $this->_connectionID = false;
181 function SelectDB($db) {
182 $this->database = $db;
183 return true;
184 } // SelectDB
186 function ServerInfo()
188 if( !empty( $this->version ) ) return $this->version;
189 $version = array();
191 Determines how aliases are handled during search.
192 LDAP_DEREF_NEVER (0x00)
193 LDAP_DEREF_SEARCHING (0x01)
194 LDAP_DEREF_FINDING (0x02)
195 LDAP_DEREF_ALWAYS (0x03)
196 The LDAP_DEREF_SEARCHING value means aliases are dereferenced during the search but
197 not when locating the base object of the search. The LDAP_DEREF_FINDING value means
198 aliases are dereferenced when locating the base object but not during the search.
199 Default: LDAP_DEREF_NEVER
201 ldap_get_option( $this->_connectionID, LDAP_OPT_DEREF, $version['LDAP_OPT_DEREF'] ) ;
202 switch ( $version['LDAP_OPT_DEREF'] ) {
203 case 0:
204 $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_NEVER';
205 case 1:
206 $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_SEARCHING';
207 case 2:
208 $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_FINDING';
209 case 3:
210 $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_ALWAYS';
214 A limit on the number of entries to return from a search.
215 LDAP_NO_LIMIT (0) means no limit.
216 Default: LDAP_NO_LIMIT
218 ldap_get_option( $this->_connectionID, LDAP_OPT_SIZELIMIT, $version['LDAP_OPT_SIZELIMIT'] );
219 if ( $version['LDAP_OPT_SIZELIMIT'] == 0 ) {
220 $version['LDAP_OPT_SIZELIMIT'] = 'LDAP_NO_LIMIT';
224 A limit on the number of seconds to spend on a search.
225 LDAP_NO_LIMIT (0) means no limit.
226 Default: LDAP_NO_LIMIT
228 ldap_get_option( $this->_connectionID, LDAP_OPT_TIMELIMIT, $version['LDAP_OPT_TIMELIMIT'] );
229 if ( $version['LDAP_OPT_TIMELIMIT'] == 0 ) {
230 $version['LDAP_OPT_TIMELIMIT'] = 'LDAP_NO_LIMIT';
234 Determines whether the LDAP library automatically follows referrals returned by LDAP servers or not.
235 LDAP_OPT_ON
236 LDAP_OPT_OFF
237 Default: ON
239 ldap_get_option( $this->_connectionID, LDAP_OPT_REFERRALS, $version['LDAP_OPT_REFERRALS'] );
240 if ( $version['LDAP_OPT_REFERRALS'] == 0 ) {
241 $version['LDAP_OPT_REFERRALS'] = 'LDAP_OPT_OFF';
242 } else {
243 $version['LDAP_OPT_REFERRALS'] = 'LDAP_OPT_ON';
247 Determines whether LDAP I/O operations are automatically restarted if they abort prematurely.
248 LDAP_OPT_ON
249 LDAP_OPT_OFF
250 Default: OFF
252 ldap_get_option( $this->_connectionID, LDAP_OPT_RESTART, $version['LDAP_OPT_RESTART'] );
253 if ( $version['LDAP_OPT_RESTART'] == 0 ) {
254 $version['LDAP_OPT_RESTART'] = 'LDAP_OPT_OFF';
255 } else {
256 $version['LDAP_OPT_RESTART'] = 'LDAP_OPT_ON';
260 This option indicates the version of the LDAP protocol used when communicating with the primary LDAP server.
261 LDAP_VERSION2 (2)
262 LDAP_VERSION3 (3)
263 Default: LDAP_VERSION2 (2)
265 ldap_get_option( $this->_connectionID, LDAP_OPT_PROTOCOL_VERSION, $version['LDAP_OPT_PROTOCOL_VERSION'] );
266 if ( $version['LDAP_OPT_PROTOCOL_VERSION'] == 2 ) {
267 $version['LDAP_OPT_PROTOCOL_VERSION'] = 'LDAP_VERSION2';
268 } else {
269 $version['LDAP_OPT_PROTOCOL_VERSION'] = 'LDAP_VERSION3';
272 /* The host name (or list of hosts) for the primary LDAP server. */
273 ldap_get_option( $this->_connectionID, LDAP_OPT_HOST_NAME, $version['LDAP_OPT_HOST_NAME'] );
274 ldap_get_option( $this->_connectionID, LDAP_OPT_ERROR_NUMBER, $version['LDAP_OPT_ERROR_NUMBER'] );
275 ldap_get_option( $this->_connectionID, LDAP_OPT_ERROR_STRING, $version['LDAP_OPT_ERROR_STRING'] );
276 ldap_get_option( $this->_connectionID, LDAP_OPT_MATCHED_DN, $version['LDAP_OPT_MATCHED_DN'] );
278 return $this->version = $version;
283 /*--------------------------------------------------------------------------------------
284 Class Name: Recordset
285 --------------------------------------------------------------------------------------*/
287 class ADORecordSet_ldap extends ADORecordSet{
289 var $databaseType = "ldap";
290 var $canSeek = false;
291 var $_entryID; /* keeps track of the entry resource identifier */
293 function ADORecordSet_ldap($queryID,$mode=false)
295 if ($mode === false) {
296 global $ADODB_FETCH_MODE;
297 $mode = $ADODB_FETCH_MODE;
299 switch ($mode)
301 case ADODB_FETCH_NUM:
302 $this->fetchMode = LDAP_NUM;
303 break;
304 case ADODB_FETCH_ASSOC:
305 $this->fetchMode = LDAP_ASSOC;
306 break;
307 case ADODB_FETCH_DEFAULT:
308 case ADODB_FETCH_BOTH:
309 default:
310 $this->fetchMode = LDAP_BOTH;
311 break;
314 $this->ADORecordSet($queryID);
317 function _initrs()
320 This could be teaked to respect the $COUNTRECS directive from ADODB
321 It's currently being used in the _fetch() function and the
322 GetAssoc() function
324 $this->_numOfRows = ldap_count_entries( $this->connection->_connectionID, $this->_queryID );
329 Return whole recordset as a multi-dimensional associative array
331 function GetAssoc($force_array = false, $first2cols = false)
333 $records = $this->_numOfRows;
334 $results = array();
335 for ( $i=0; $i < $records; $i++ ) {
336 foreach ( $this->fields as $k=>$v ) {
337 if ( is_array( $v ) ) {
338 if ( $v['count'] == 1 ) {
339 $results[$i][$k] = $v[0];
340 } else {
341 array_shift( $v );
342 $results[$i][$k] = $v;
348 return $results;
351 function GetRowAssoc()
353 $results = array();
354 foreach ( $this->fields as $k=>$v ) {
355 if ( is_array( $v ) ) {
356 if ( $v['count'] == 1 ) {
357 $results[$k] = $v[0];
358 } else {
359 array_shift( $v );
360 $results[$k] = $v;
365 return $results;
368 function GetRowNums()
370 $results = array();
371 foreach ( $this->fields as $k=>$v ) {
372 static $i = 0;
373 if (is_array( $v )) {
374 if ( $v['count'] == 1 ) {
375 $results[$i] = $v[0];
376 } else {
377 array_shift( $v );
378 $results[$i] = $v;
380 $i++;
383 return $results;
386 function _fetch()
388 if ( $this->_currentRow >= $this->_numOfRows && $this->_numOfRows >= 0 )
389 return false;
391 if ( $this->_currentRow == 0 ) {
392 $this->_entryID = ldap_first_entry( $this->connection->_connectionID, $this->_queryID );
393 } else {
394 $this->_entryID = ldap_next_entry( $this->connection->_connectionID, $this->_entryID );
397 $this->fields = ldap_get_attributes( $this->connection->_connectionID, $this->_entryID );
398 $this->_numOfFields = $this->fields['count'];
399 switch ( $this->fetchMode ) {
401 case LDAP_ASSOC:
402 $this->fields = $this->GetRowAssoc();
403 break;
405 case LDAP_NUM:
406 $this->fields = array_merge($this->GetRowNums(),$this->GetRowAssoc());
407 break;
409 case LDAP_BOTH:
410 default:
411 $this->fields = $this->GetRowNums();
412 break;
414 return ( is_array( $this->fields ) );
417 function _close() {
418 @ldap_free_result( $this->_queryID );
419 $this->_queryID = false;