【php开源代码栏目提醒】:网学会员php开源代码为您提供adodb-ldap.inc.php参考,解决您在adodb-ldap.inc.php学习中工作中的难题,参考学习。
<?php
/*
V5.02 24 Sept 2007 (c) 2000-2007 John Lim (jlim#natsoft.com.my). All rights reserved.
Released under both BSD license and Lesser GPL library license.
Whenever there is any discrepancy between the two licenses,
the BSD license will take precedence.
Set tabs to 8.
Revision 1: (02/25/2005) Updated codebase to include the _inject_bind_options function. This allows
users to access the options in the ldap_set_option function appropriately. Most importantly
LDAP Version 3 is now supported. See the examples for more information. Also fixed some minor
bugs that surfaced when
PHP error levels were set high.
Joshua Eldridge (joshuae74#hotmail.com)
*/
// security - hide paths
if (!defined('ADODB_DIR')) die();
if (!defined('LDAP_ASSOC')) {
define('LDAP_ASSOC',ADODB_FETCH_ASSOC);
define('LDAP_NUM',ADODB_FETCH_NUM);
define('LDAP_BOTH',ADODB_FETCH_BOTH);
}
class ADODB_ldap extends ADOConnection {
var $databaseType = 'ldap';
var $dataProvider = 'ldap';
# Connection information
var $username = false;
var $password = false;
# Used during searches
var $filter;
var $dn;
var $version;
var $port = 389;
# Options configuration information
var $LDAP_CONNECT_OPTIONS;
# error on binding, eg. "Binding: invalid credentials"
var $_bind_errmsg = "Binding: %s";
function ADODB_ldap()
{
}
// returns true or false
function _connect( $host, $username, $pass
word, $ldapbase)
{
global $LDAP_CONNECT_OPTIONS;
if ( !function_exists( 'ldap_connect' ) ) return null;
$conn_info = array( $host,$this->port);
if ( strstr( $host, ':' ) ) {
$conn_info = split( ':', $host );
}
$this->_connectionID = @ldap_connect( $conn_info[0], $conn_info[1] );
if (!$this->_connectionID) {
$e = 'Could not connect to ' . $conn_info[0];
$this->_errorMsg = $e;
if ($this->debug) ADOConnection::outp($e);
return false;
}
if( count( $LDAP_CONNECT_OPTIONS ) > 0 ) {
$this->_inject_bind_options( $LDAP_CONNECT_OPTIONS );
}
if ($username) {
$bind = @ldap_bind( $this->_connectionID, $username, $password );
} else {
$username = 'anonymous';
$bind = @ldap_bind( $this->_connectionID );
}
if (!$bind) {
$e = sprintf($this->_bind_errmsg,ldap_error($this->_connectionID));
$this->_errorMsg = $e;
if ($this->debug) ADOConnection::outp($e);
return false;
}
$this->_errorMsg = '';
$this->database = $ldapbase;
return $this->_connectionID;
}
/*
Valid Domain Values for LDAP Options:
LDAP_OPT_DEREF (integer)
LDAP_OPT_SIZELIMIT (integer)
LDAP_OPT_TIMELIMIT (integer)
LDAP_OPT_PROTOCOL_VERSION (integer)
LDAP_OPT_ERROR_NUMBER (integer)
LDAP_OPT_REFERRALS (boolean)
LDAP_OPT_RESTART (boolean)
LDAP_OPT_HOST_NAME (string)
LDAP_OPT_ERROR_STRING (string)
LDAP_OPT_MATCHED_DN (string)
LDAP_OPT_SERVER_CONTROLS (array)
LDAP_OPT_CLIENT_CONTROLS (array)
Make sure to set this BEFORE calling Connect()
Example:
$LDAP_CONNECT_OPTIONS = Array(
Array (
"OPTION_NAME"=>LDAP_OPT_DEREF,
"OPTION_VALUE"=>2
),
Array (
"OPTION_NAME"=>LDAP_OPT_SIZELIMIT,
"OPTION_VALUE"=>100
),
Array (
"OPTION_NAME"=>LDAP_OPT_TIMELIMIT,
"OPTION_VALUE"=>30
),
Array (
"OPTION_NAME"=>LDAP_OPT_PROTOCOL_VERSION,
"OPTION_VALUE"=>3
),
Array (
"OPTION_NAME"=>LDAP_OPT_ERROR_NUMBER,
"OPTION_VALUE"=>13
),
Array (
"OPTION_NAME"=>LDAP_OPT_REFERRALS,
"OPTION_VALUE"=>FALSE
),
Array (
"OPTION_NAME"=>LDAP_OPT_RESTART,
"OPTION_VALUE"=>FALSE
)
);
*/
function _inject_bind_options( $options ) {
foreach( $options as $option ) {
ldap_set_option( $this->_connectionID, $option["OPTION_NAME"], $option["OPTION_VALUE"] )
or die( "Unable to set server option: " . $option["OPTION_NAME"] );
}
}
/* returns _queryID or false */
function _query($sql,$inputarr)
{
$rs = @ldap_search( $this->_connectionID, $this->database, $
sql );
$this->_errorMsg = ($rs) ? '' : 'Search error on '.$sql.': '.ldap_error($this->_connectionID);
return $rs;
}
function ErrorMsg()
{
return $this->_errorMsg;
}
function ErrorNo()
{
return @ldap_errno($this->_connectionID);
}
/* closes the LDAP connection */
function _close()
{
@ldap_close( $this->_connectionID );
$this->_connectionID = false;
}
function SelectDB($db) {
$this->database = $db;
return true;
} // SelectDB
function ServerInfo()
{
if( !empty( $this->version ) ) return $this->version;
$version = array();
/*
Det