00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 if (!defined('ADODB_DIR')) die();
00019
00020 if (!defined('LDAP_ASSOC')) {
00021 define('LDAP_ASSOC',ADODB_FETCH_ASSOC);
00022 define('LDAP_NUM',ADODB_FETCH_NUM);
00023 define('LDAP_BOTH',ADODB_FETCH_BOTH);
00024 }
00025
00026 class ADODB_ldap extends ADOConnection {
00027 var $databaseType = 'ldap';
00028 var $dataProvider = 'ldap';
00029
00030 # Connection information
00031 var $username = false;
00032 var $password = false;
00033
00034 # Used during searches
00035 var $filter;
00036 var $dn;
00037 var $version;
00038 var $port = 389;
00039
00040 # Options configuration information
00041 var $LDAP_CONNECT_OPTIONS;
00042
00043 function ADODB_ldap()
00044 {
00045 }
00046
00047
00048
00049 function _connect( $host, $username, $password, $ldapbase)
00050 {
00051 global $LDAP_CONNECT_OPTIONS;
00052
00053 if ( !function_exists( 'ldap_connect' ) ) return null;
00054
00055 $conn_info = array( $host,$this->port);
00056
00057 if ( strstr( $host, ':' ) ) {
00058 $conn_info = split( ':', $host );
00059 }
00060
00061 $this->_connectionID = ldap_connect( $conn_info[0], $conn_info[1] );
00062 if (!$this->_connectionID) {
00063 $e = 'Could not connect to ' . $conn_info[0];
00064 $this->_errorMsg = $e;
00065 if ($this->debug) ADOConnection::outp($e);
00066 return false;
00067 }
00068 if( count( $LDAP_CONNECT_OPTIONS ) > 0 ) {
00069 $this->_inject_bind_options( $LDAP_CONNECT_OPTIONS );
00070 }
00071
00072 if ($username) {
00073 $bind = ldap_bind( $this->_connectionID, $username, $password );
00074 } else {
00075 $username = 'anonymous';
00076 $bind = ldap_bind( $this->_connectionID );
00077 }
00078
00079 if (!$bind) {
00080 $e = 'Could not bind to ' . $conn_info[0] . " as ".$username;
00081 $this->_errorMsg = $e;
00082 if ($this->debug) ADOConnection::outp($e);
00083 return false;
00084 }
00085 $this->_errorMsg = '';
00086 $this->database = $ldapbase;
00087 return $this->_connectionID;
00088 }
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 function _inject_bind_options( $options ) {
00143 foreach( $options as $option ) {
00144 ldap_set_option( $this->_connectionID, $option["OPTION_NAME"], $option["OPTION_VALUE"] )
00145 or die( "Unable to set server option: " . $option["OPTION_NAME"] );
00146 }
00147 }
00148
00149
00150 function _query($sql,$inputarr)
00151 {
00152 $rs = ldap_search( $this->_connectionID, $this->database, $sql );
00153 $this->_errorMsg = ($rs) ? '' : 'Search error on '.$sql;
00154 return $rs;
00155 }
00156
00157
00158 function _close()
00159 {
00160 @ldap_close( $this->_connectionID );
00161 $this->_connectionID = false;
00162 }
00163
00164 function SelectDB($db) {
00165 $this->database = $db;
00166 return true;
00167 }
00168
00169 function ServerInfo()
00170 {
00171 if( !empty( $this->version ) ) return $this->version;
00172 $version = array();
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184 ldap_get_option( $this->_connectionID, LDAP_OPT_DEREF, $version['LDAP_OPT_DEREF'] ) ;
00185 switch ( $version['LDAP_OPT_DEREF'] ) {
00186 case 0:
00187 $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_NEVER';
00188 case 1:
00189 $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_SEARCHING';
00190 case 2:
00191 $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_FINDING';
00192 case 3:
00193 $version['LDAP_OPT_DEREF'] = 'LDAP_DEREF_ALWAYS';
00194 }
00195
00196
00197
00198
00199
00200
00201 ldap_get_option( $this->_connectionID, LDAP_OPT_SIZELIMIT, $version['LDAP_OPT_SIZELIMIT'] );
00202 if ( $version['LDAP_OPT_SIZELIMIT'] == 0 ) {
00203 $version['LDAP_OPT_SIZELIMIT'] = 'LDAP_NO_LIMIT';
00204 }
00205
00206
00207
00208
00209
00210
00211 ldap_get_option( $this->_connectionID, LDAP_OPT_TIMELIMIT, $version['LDAP_OPT_TIMELIMIT'] );
00212 if ( $version['LDAP_OPT_TIMELIMIT'] == 0 ) {
00213 $version['LDAP_OPT_TIMELIMIT'] = 'LDAP_NO_LIMIT';
00214 }
00215
00216
00217
00218
00219
00220
00221
00222 ldap_get_option( $this->_connectionID, LDAP_OPT_REFERRALS, $version['LDAP_OPT_REFERRALS'] );
00223 if ( $version['LDAP_OPT_REFERRALS'] == 0 ) {
00224 $version['LDAP_OPT_REFERRALS'] = 'LDAP_OPT_OFF';
00225 } else {
00226 $version['LDAP_OPT_REFERRALS'] = 'LDAP_OPT_ON';
00227
00228 }
00229
00230
00231
00232
00233
00234
00235 ldap_get_option( $this->_connectionID, LDAP_OPT_RESTART, $version['LDAP_OPT_RESTART'] );
00236 if ( $version['LDAP_OPT_RESTART'] == 0 ) {
00237 $version['LDAP_OPT_RESTART'] = 'LDAP_OPT_OFF';
00238 } else {
00239 $version['LDAP_OPT_RESTART'] = 'LDAP_OPT_ON';
00240
00241 }
00242
00243
00244
00245
00246
00247
00248 ldap_get_option( $this->_connectionID, LDAP_OPT_PROTOCOL_VERSION, $version['LDAP_OPT_PROTOCOL_VERSION'] );
00249 if ( $version['LDAP_OPT_PROTOCOL_VERSION'] == 2 ) {
00250 $version['LDAP_OPT_PROTOCOL_VERSION'] = 'LDAP_VERSION2';
00251 } else {
00252 $version['LDAP_OPT_PROTOCOL_VERSION'] = 'LDAP_VERSION3';
00253
00254 }
00255
00256 ldap_get_option( $this->_connectionID, LDAP_OPT_HOST_NAME, $version['LDAP_OPT_HOST_NAME'] );
00257 ldap_get_option( $this->_connectionID, LDAP_OPT_ERROR_NUMBER, $version['LDAP_OPT_ERROR_NUMBER'] );
00258 ldap_get_option( $this->_connectionID, LDAP_OPT_ERROR_STRING, $version['LDAP_OPT_ERROR_STRING'] );
00259 ldap_get_option( $this->_connectionID, LDAP_OPT_MATCHED_DN, $version['LDAP_OPT_MATCHED_DN'] );
00260
00261 return $this->version = $version;
00262
00263 }
00264 }
00265
00266
00267
00268
00269
00270 class ADORecordSet_ldap extends ADORecordSet{
00271
00272 var $databaseType = "ldap";
00273 var $canSeek = false;
00274 var $_entryID;
00275
00276 function ADORecordSet_ldap($queryID,$mode=false)
00277 {
00278 if ($mode === false) {
00279 global $ADODB_FETCH_MODE;
00280 $mode = $ADODB_FETCH_MODE;
00281 }
00282 switch ($mode)
00283 {
00284 case ADODB_FETCH_NUM:
00285 $this->fetchMode = LDAP_NUM;
00286 break;
00287 case ADODB_FETCH_ASSOC:
00288 $this->fetchMode = LDAP_ASSOC;
00289 break;
00290 case ADODB_FETCH_DEFAULT:
00291 case ADODB_FETCH_BOTH:
00292 default:
00293 $this->fetchMode = LDAP_BOTH;
00294 break;
00295 }
00296
00297 $this->ADORecordSet($queryID);
00298 }
00299
00300 function _initrs()
00301 {
00302
00303
00304
00305
00306
00307 $this->_numOfRows = ldap_count_entries( $this->connection->_connectionID, $this->_queryID );
00308
00309 }
00310
00311
00312
00313
00314 function &GetAssoc($force_array = false, $first2cols = false)
00315 {
00316 $records = $this->_numOfRows;
00317 $results = array();
00318 for ( $i=0; $i < $records; $i++ ) {
00319 foreach ( $this->fields as $k=>$v ) {
00320 if ( is_array( $v ) ) {
00321 if ( $v['count'] == 1 ) {
00322 $results[$i][$k] = $v[0];
00323 } else {
00324 array_shift( $v );
00325 $results[$i][$k] = $v;
00326 }
00327 }
00328 }
00329 }
00330
00331 return $results;
00332 }
00333
00334 function &GetRowAssoc()
00335 {
00336 $results = array();
00337 foreach ( $this->fields as $k=>$v ) {
00338 if ( is_array( $v ) ) {
00339 if ( $v['count'] == 1 ) {
00340 $results[$k] = $v[0];
00341 } else {
00342 array_shift( $v );
00343 $results[$k] = $v;
00344 }
00345 }
00346 }
00347
00348 return $results;
00349 }
00350
00351 function GetRowNums()
00352 {
00353 $results = array();
00354 foreach ( $this->fields as $k=>$v ) {
00355 static $i = 0;
00356 if (is_array( $v )) {
00357 if ( $v['count'] == 1 ) {
00358 $results[$i] = $v[0];
00359 } else {
00360 array_shift( $v );
00361 $results[$i] = $v;
00362 }
00363 $i++;
00364 }
00365 }
00366 return $results;
00367 }
00368
00369 function _fetch()
00370 {
00371 if ( $this->_currentRow >= $this->_numOfRows && $this->_numOfRows >= 0 )
00372 return false;
00373
00374 if ( $this->_currentRow == 0 ) {
00375 $this->_entryID = ldap_first_entry( $this->connection->_connectionID, $this->_queryID );
00376 } else {
00377 $this->_entryID = ldap_next_entry( $this->connection->_connectionID, $this->_entryID );
00378 }
00379
00380 $this->fields = ldap_get_attributes( $this->connection->_connectionID, $this->_entryID );
00381 $this->_numOfFields = $this->fields['count'];
00382 switch ( $this->fetchMode ) {
00383
00384 case LDAP_ASSOC:
00385 $this->fields = $this->GetRowAssoc();
00386 break;
00387
00388 case LDAP_NUM:
00389 $this->fields = array_merge($this->GetRowNums(),$this->GetRowAssoc());
00390 break;
00391
00392 case LDAP_BOTH:
00393 default:
00394 $this->fields = $this->GetRowNums();
00395 break;
00396 }
00397 return ( is_array( $this->fields ) );
00398 }
00399
00400 function _close() {
00401 @ldap_free_result( $this->_queryID );
00402 $this->_queryID = false;
00403 }
00404
00405 }
00406 ?>