Created
December 10, 2018 13:19
-
-
Save vertexvaar/eb5bccb9515a90f259fae541079620e5 to your computer and use it in GitHub Desktop.
LDAP connection test script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $host = ''; | |
| $port = '636'; | |
| $ldaps = true; | |
| $version = 3; | |
| $username = ''; | |
| $password = ''; | |
| if (strpos($host, 'ldaps:') === 0) { | |
| $host = $host . ':' . $port; | |
| $connection = ldap_connect($host); | |
| } elseif (true === $ldaps) { | |
| $host = rtrim('ldaps://' . $host, '/') . ':' . $port; | |
| $connection = ldap_connect($host); | |
| } else { | |
| $connection = ldap_connect($host, $port); | |
| } | |
| if (!$connection) { | |
| fetchErrors($connection); | |
| } | |
| ldap_set_option($connection, LDAP_OPT_REFERRALS, 0); | |
| ldap_set_option($connection, LDAP_OPT_NETWORK_TIMEOUT, 3); | |
| if (null !== $version) { | |
| if(!ldap_set_option($connection, LDAP_OPT_PROTOCOL_VERSION, $version)) { | |
| fetchErrors($connection); | |
| } | |
| } | |
| if (!ldap_bind($connection, $username, $password)) { | |
| fetchErrors($connection); | |
| } | |
| function getInfo($connection) use ($fullInfo) | |
| { | |
| $info = []; | |
| ldap_get_option($connection, LDAP_OPT_DEREF, $info['LDAP_OPT_DEREF']); | |
| ldap_get_option($connection, LDAP_OPT_SIZELIMIT, $info['LDAP_OPT_SIZELIMIT']); | |
| ldap_get_option($connection, LDAP_OPT_TIMELIMIT, $info['LDAP_OPT_TIMELIMIT']); | |
| ldap_get_option($connection, LDAP_OPT_NETWORK_TIMEOUT, $info['LDAP_OPT_NETWORK_TIMEOUT']); | |
| ldap_get_option($connection, LDAP_OPT_PROTOCOL_VERSION, $info['LDAP_OPT_PROTOCOL_VERSION']); | |
| ldap_get_option($connection, LDAP_OPT_ERROR_NUMBER, $info['LDAP_OPT_ERROR_NUMBER']); | |
| ldap_get_option($connection, LDAP_OPT_REFERRALS, $info['LDAP_OPT_REFERRALS']); | |
| ldap_get_option($connection, LDAP_OPT_RESTART, $info['LDAP_OPT_RESTART']); | |
| ldap_get_option($connection, LDAP_OPT_HOST_NAME, $info['LDAP_OPT_HOST_NAME']); | |
| ldap_get_option($connection, LDAP_OPT_ERROR_STRING, $info['LDAP_OPT_ERROR_STRING']); | |
| ldap_get_option($connection, LDAP_OPT_MATCHED_DN, $info['LDAP_OPT_MATCHED_DN']); | |
| ldap_get_option($connection, LDAP_OPT_SERVER_CONTROLS, $info['LDAP_OPT_SERVER_CONTROLS']); | |
| ldap_get_option($connection, LDAP_OPT_CLIENT_CONTROLS, $info['LDAP_OPT_CLIENT_CONTROLS']); | |
| } | |
| function fetchErrors($connection) | |
| { | |
| $lastErrorCode = ldap_errno($connection); | |
| $lastErrorMessage = ldap_error($connection); | |
| if ($lastErrorCode !== 0) { | |
| getInfo($connection); | |
| echo "Error [$lastErrorCode]: $lastErrorMessage"; | |
| exit(1); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment