You are here

function ldap_help_connect in Lightweight Directory Access Protocol (LDAP) 7.2

Same name and namespace in other branches
  1. 8.2 ldap_help/ldap_test_script/functions.inc \ldap_help_connect()
  2. 7 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 103

Code

function ldap_help_connect($address, $port, $tls, $test = FALSE) {
  if ($test) {

    // Test for ldap extensions that don't actually connect until bind.
    $false_con = ldap_connect("fakehostname.sdfserewerdfsdf.com", 389);
    if (ldap_errno($false_con) == LDAP_SUCCESS) {
      $con = ldap_connect($address, $port);
      return [
        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 [
      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 [
        LDAP_PROTOCOL_ERROR,
        "Could not get LDAP protocol version.",
      ];
    }
    if ($vers != 3) {
      return [
        LDAP_CONNECT_ERROR,
        'Could not start TLS, only supported by LDAP v3.',
      ];
    }
    elseif (!function_exists('ldap_start_tls')) {
      return [
        LDAP_CONNECT_ERROR,
        'Could not start TLS. It does not seem to be supported by this PHP setup.',
      ];
    }
    elseif (!ldap_start_tls($con)) {
      return [
        LDAP_CONNECT_ERROR,
        "Could not start TLS. (Error " . ldap_errno($con) . ":" . ldap_error($con) . ").",
      ];
    }
  }
  return [
    LDAP_SUCCESS,
    "Successful Connection!",
    $con,
  ];
}