public function bind($dn = null, $password = null)
{
/* Fetch current bind credentials. */
if (is_null($dn)) {
$dn = $this->_config['binddn'];
}
if (is_null($password)) {
$password = $this->_config['bindpw'];
}
/* Connect first, if we haven't so far. This will also bind
* us to the server. */
if (!$this->_link) {
/* Store old credentials so we can revert them later, then
* overwrite config with new bind credentials. */
$olddn = $this->_config['binddn'];
$oldpw = $this->_config['bindpw'];
/* Overwrite bind credentials in config so
* _connect() knows about them. */
$this->_config['binddn'] = $dn;
$this->_config['bindpw'] = $password;
/* Try to connect with provided credentials. */
$msg = $this->_connect();
/* Reset to previous config. */
$this->_config['binddn'] = $olddn;
$this->_config['bindpw'] = $oldpw;
return;
}
/* Do the requested bind as we are asked to bind manually. */
if (empty($dn)) {
/* Anonymous bind. */
$msg = @ldap_bind($this->_link);
} else {
/* Privileged bind. */
$msg = @ldap_bind($this->_link, $dn, $password);
}
if (!$msg) {
throw new Horde_Ldap_Exception('Bind failed: ' . @ldap_error($this->_link), @ldap_errno($this->_link));
}
}