You are here

public function SimpleLdapConnection::connect in Simple LDAP 8

Connect to the Simple LDAP server based on configuration settings.

Throws

\Drupal\simple_ldap\SimpleLdapException

Overrides SimpleLdapConnectionInterface::connect

File

src/SimpleLdapConnection.php, line 51

Class

SimpleLdapConnection

Namespace

Drupal\simple_ldap

Code

public function connect() {
  if ($this->connection) {
    return;
  }
  $this->connection = @ldap_connect($this->connection_string);

  // Timeout after 10 seconds if connection is unsuccessful.
  @ldap_set_option($this->connection, LDAP_OPT_NETWORK_TIMEOUT, 10);
  if ($this->connection === FALSE) {
    throw new SimpleLdapException('Could not connect to LDAP server: ', $this->connection);
  }
  if ($this->config
    ->get('encryption') === 'tls' && ldap_start_tls($this->connection) === FALSE) {
    throw new SimpleLdapException('Could not start TLS connection: ', $this->connection);
  }
  @ldap_set_option($this->connection, LDAP_OPT_PROTOCOL_VERSION, 3);
  @ldap_set_option($this->connection, LDAP_OPT_REFERRALS, (int) $this->config
    ->get('opt_referrals'));
}