You are here

function LdapServer::connect in Lightweight Directory Access Protocol (LDAP) 7

Same name and namespace in other branches
  1. 8.2 ldap_servers/LdapServer.class.php \LdapServer::connect()
  2. 7.2 ldap_servers/LdapServer.class.php \LdapServer::connect()

Connect Method

2 calls to LdapServer::connect()
LdapServer::search in ldap_servers/LdapServer.class.php
Perform an LDAP search.
LdapServer::__invoke in ldap_servers/LdapServer.class.php
Invoke Method
1 method overrides LdapServer::connect()
LdapServerTest::connect in ldap_servers/tests/LdapServerTest.class.php
Connect Method

File

ldap_servers/LdapServer.class.php, line 203
Defines server classes and related functions.

Class

LdapServer
LDAP Server Class

Code

function connect() {
  if (!($con = ldap_connect($this->address, $this->port))) {
    watchdog('user', 'LDAP Connect failure to ' . $this->address . ':' . $this->port);
    return LDAP_CONNECT_ERROR;
  }
  ldap_set_option($con, LDAP_OPT_PROTOCOL_VERSION, 3);
  ldap_set_option($con, LDAP_OPT_REFERRALS, 0);

  // Use TLS if we are configured and able to.
  if ($this->tls) {
    ldap_get_option($con, LDAP_OPT_PROTOCOL_VERSION, $vers);
    if ($vers == -1) {
      watchdog('user', 'Could not get LDAP protocol version.');
      return LDAP_PROTOCOL_ERROR;
    }
    if ($vers != 3) {
      watchdog('user', 'Could not start TLS, only supported by LDAP v3.');
      return LDAP_CONNECT_ERROR;
    }
    elseif (!function_exists('ldap_start_tls')) {
      watchdog('user', 'Could not start TLS. It does not seem to be supported by this PHP setup.');
      return LDAP_CONNECT_ERROR;
    }
    elseif (!ldap_start_tls($con)) {
      $msg = t("Could not start TLS. (Error %errno: %error).", array(
        '%errno' => ldap_errno($con),
        '%error' => ldap_error($con),
      ));
      watchdog('user', $msg);
      return LDAP_CONNECT_ERROR;
    }
  }

  // Store the resulting resource
  $this->connection = $con;
  return LDAP_SUCCESS;
}