You are here

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

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

Connect Method.

5 calls to LdapServer::connect()
LdapServer::connectAndBindIfNotAlready in ldap_servers/LdapServer.class.php
LdapServer::createLdapEntry in ldap_servers/LdapServer.class.php
Create ldap entry.
LdapServer::delete in ldap_servers/LdapServer.class.php
Perform an LDAP delete.
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_test/LdapServerTest.class.php
Connect Method.

File

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

Class

LdapServer
LDAP Server Class.

Code

public function connect() {
  if (!function_exists('ldap_connect')) {
    watchdog('ldap_servers', 'PHP LDAP extension not found, aborting.');
    return LDAP_NOT_SUPPORTED;
  }
  if (!($con = ldap_connect($this->address, $this->port))) {
    watchdog('ldap_servers', '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, (int) $this->followrefs);

  // 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('ldap_servers', 'Could not get LDAP protocol version.');
      return LDAP_PROTOCOL_ERROR;
    }
    if ($vers != 3) {
      watchdog('ldap_servers', 'Could not start TLS, only supported by LDAP v3.');
      return LDAP_CONNECT_ERROR;
    }
    elseif (!function_exists('ldap_start_tls')) {
      watchdog('ldap_servers', '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).", [
        '%errno' => ldap_errno($con),
        '%error' => ldap_error($con),
      ]);
      watchdog('ldap_servers', $msg);
      return LDAP_CONNECT_ERROR;
    }
  }

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