/**
* Makes the initial connection to LDAP, sets connection options, and starts TLS if specified.
*
* @param null|string $server
* @throws LdapConnectionException
*/
protected function initiateLdapConnection($server = null)
{
list($ldapUrl, $server) = $this->getLdapUrl($server);
$this->connection = @ldap_connect($ldapUrl);
if (!$this->connection) {
throw new LdapConnectionException(sprintf("Failed to initiate LDAP connection with URI: %s", $ldapUrl));
}
foreach ($this->config->getLdapOptions() as $option => $value) {
if (!ldap_set_option($this->connection, $option, $value)) {
throw new LdapConnectionException("Failed to set LDAP connection option.");
}
}
if ($this->config->getUseTls() && !@ldap_start_tls($this->connection)) {
throw new LdapConnectionException(sprintf("Failed to start TLS: %s", $this->getLastError()), $this->getExtendedErrorNumber());
}
$this->server = $server;
}