You are here

function ldap_help_connect in Lightweight Directory Access Protocol (LDAP) 7

Same name and namespace in other branches
  1. 8.2 ldap_help/ldap_test_script/functions.inc \ldap_help_connect()
  2. 7.2 ldap_help/ldap_test_script/functions.inc \ldap_help_connect()
1 call to ldap_help_connect()
test.php in ldap_help/ldap_test_script/test.php

File

ldap_help/ldap_test_script/functions.inc, line 96

Code

function ldap_help_connect($address, $port, $tls, $test = FALSE) {
  if ($test) {
    $false_con = ldap_connect("fakehostname.sdfserewerdfsdf.com", 389);

    // test for ldap extensions that don't actually connect until bind
    if (ldap_errno($false_con) == LDAP_SUCCESS) {
      $con = ldap_connect($address, $port);
      return array(
        LDAP_OTHER,
        "ldap_connect does not actually connect until bind with installed extension, so connect is not a valid test.",
        $con,
      );
    }
  }
  $con = ldap_connect($address, $port);
  if (!$con || ldap_errno($con) != LDAP_SUCCESS) {
    $err = ldap_errno($con) . ":" . ldap_error($con) . ":" . ldap_err2str(ldap_errno($con)) . "!";
    return array(
      LDAP_CONNECT_ERROR,
      "LDAP Connect failure to  {$address} : {$port}. {$err}",
    );
  }

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