Documentation TYPO3 par Ameos

adodb-ldap.inc.php

00001 <?php
00002 /*
00003   V4.93 10 Oct 2006  (c) 2000-2006 John Lim (jlim#natsoft.com.my). All rights reserved.
00004    Released under both BSD license and Lesser GPL library license. 
00005   Whenever there is any discrepancy between the two licenses, 
00006   the BSD license will take precedence.
00007   Set tabs to 8.
00008   
00009   Revision 1: (02/25/2005) Updated codebase to include the _inject_bind_options function. This allows
00010   users to access the options in the ldap_set_option function appropriately. Most importantly
00011   LDAP Version 3 is now supported. See the examples for more information. Also fixed some minor
00012   bugs that surfaced when PHP error levels were set high.
00013   
00014   Joshua Eldridge (joshuae74#hotmail.com)
00015 */ 
00016 
00017 // security - hide paths
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         // returns true or false
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         Valid Domain Values for LDAP Options:
00092 
00093         LDAP_OPT_DEREF (integer)
00094         LDAP_OPT_SIZELIMIT (integer)
00095         LDAP_OPT_TIMELIMIT (integer)
00096         LDAP_OPT_PROTOCOL_VERSION (integer)
00097         LDAP_OPT_ERROR_NUMBER (integer)
00098         LDAP_OPT_REFERRALS (boolean)
00099         LDAP_OPT_RESTART (boolean)
00100         LDAP_OPT_HOST_NAME (string)
00101         LDAP_OPT_ERROR_STRING (string)
00102         LDAP_OPT_MATCHED_DN (string)
00103         LDAP_OPT_SERVER_CONTROLS (array)
00104         LDAP_OPT_CLIENT_CONTROLS (array)
00105 
00106         Make sure to set this BEFORE calling Connect()
00107 
00108         Example:
00109 
00110         $LDAP_CONNECT_OPTIONS = Array(
00111                 Array (
00112                         "OPTION_NAME"=>LDAP_OPT_DEREF,
00113                         "OPTION_VALUE"=>2
00114                 ),
00115                 Array (
00116                         "OPTION_NAME"=>LDAP_OPT_SIZELIMIT,
00117                         "OPTION_VALUE"=>100
00118                 ),
00119                 Array (
00120                         "OPTION_NAME"=>LDAP_OPT_TIMELIMIT,
00121                         "OPTION_VALUE"=>30
00122                 ),
00123                 Array (
00124                         "OPTION_NAME"=>LDAP_OPT_PROTOCOL_VERSION,
00125                         "OPTION_VALUE"=>3
00126                 ),
00127                 Array (
00128                         "OPTION_NAME"=>LDAP_OPT_ERROR_NUMBER,
00129                         "OPTION_VALUE"=>13
00130                 ),
00131                 Array (
00132                         "OPTION_NAME"=>LDAP_OPT_REFERRALS,
00133                         "OPTION_VALUE"=>FALSE
00134                 ),
00135                 Array (
00136                         "OPTION_NAME"=>LDAP_OPT_RESTART,
00137                         "OPTION_VALUE"=>FALSE
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         /* returns _queryID or false */
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     /* closes the LDAP connection */
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         } // SelectDB
00168 
00169     function ServerInfo()
00170     {
00171         if( !empty( $this->version ) ) return $this->version;
00172         $version = array();
00173         /*
00174         Determines how aliases are handled during search. 
00175         LDAP_DEREF_NEVER (0x00)
00176         LDAP_DEREF_SEARCHING (0x01)
00177         LDAP_DEREF_FINDING (0x02)
00178         LDAP_DEREF_ALWAYS (0x03)
00179         The LDAP_DEREF_SEARCHING value means aliases are dereferenced during the search but 
00180         not when locating the base object of the search. The LDAP_DEREF_FINDING value means 
00181         aliases are dereferenced when locating the base object but not during the search.  
00182         Default: LDAP_DEREF_NEVER
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         A limit on the number of entries to return from a search. 
00198         LDAP_NO_LIMIT (0) means no limit.
00199         Default: LDAP_NO_LIMIT
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         A limit on the number of seconds to spend on a search. 
00208         LDAP_NO_LIMIT (0) means no limit.
00209         Default: LDAP_NO_LIMIT
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         Determines whether the LDAP library automatically follows referrals returned by LDAP servers or not. 
00218         LDAP_OPT_ON
00219         LDAP_OPT_OFF
00220         Default: ON
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         Determines whether LDAP I/O operations are automatically restarted if they abort prematurely. 
00231         LDAP_OPT_ON
00232         LDAP_OPT_OFF
00233         Default: OFF
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         This option indicates the version of the LDAP protocol used when communicating with the primary LDAP server.
00244         LDAP_VERSION2 (2)
00245         LDAP_VERSION3 (3)
00246         Default: LDAP_VERSION2 (2)
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         /* The host name (or list of hosts) for the primary LDAP server. */
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          Class Name: Recordset
00268 --------------------------------------------------------------------------------------*/
00269 
00270 class ADORecordSet_ldap extends ADORecordSet{   
00271         
00272         var $databaseType = "ldap";
00273         var $canSeek = false;
00274         var $_entryID; /* keeps track of the entry resource identifier */
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            This could be teaked to respect the $COUNTRECS directive from ADODB
00304            It's currently being used in the _fetch() function and the
00305            GetAssoc() function
00306        */
00307             $this->_numOfRows = ldap_count_entries( $this->connection->_connectionID, $this->_queryID );
00308 
00309         }
00310 
00311     /*
00312     Return whole recordset as a multi-dimensional associative array
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 ?>


Généré par Les spécialistes TYPO3 avec  doxygen 1.4.6