3 * LDAP connection test script
5 * Script is extended version of LDAP test script from PHP LDAP extension
6 * manual. It does not suppress LDAP function errors. If some LDAP function
7 * fails, you should see PHP error messages. If function is missing, you should
8 * see errors too. If LDAP server returns unexpected output, you should see
11 * Change file extension from .phps to .php, if you want to use it. Don't store
12 * important information (like your luggage password) on this file.
13 * Copyright (c) 2006 The SquirrelMail Project
14 * License: script is licensed under GPL.
15 * See http://www.opensource.org/licenses/gpl-license.php
18 /** Configuration variables */
23 * You can use IP address, hostname or any other type of URL
24 * supported by your LDAP libraries. For example: you can add ldaps:// prefix
25 * for LDAP over SSL connection (636 port) or ldapi:// for LDAP socket
28 $ldap_host='localhost';
32 * If you don't know it, script will try to show first available basedn when
33 * it reads LDAP server's base.
35 $ldap_basedn='dc=example,dc=org';
37 * Controls use of LDAP v3 bind protocol
39 * PHP scripts default to v2 protocol and some LDAP servers (for example: newer
40 * OpenLDAP versions and ADS) don't support it.
44 * Controls use of LDAP STARTTLS
46 * Allows to enable TLS encryption on plain text LDAP connection.
47 * Requires PHP 4.2.0 or newer.
51 * ADS limit scope option
52 * http://msdn.microsoft.com/library/en-us/ldap/ldap/ldap_server_domain_scope_oid.asp
53 * Might be required for some Win2k3 ADS setups. Don't enable on other servers.
54 * Warning: LDAP base search will fail, if option is enabled.
56 $ldap_limit_scope=false;
58 * BindDN used for authentication
62 * Password used for authentication
66 /* end of configuration variables */
68 // modifications stop here.
70 /* set error reporting options */
71 ini_set('html_errors','off');
72 ini_set('display_errors','on');
73 error_reporting(E_ALL
);
75 /* set plain text header */
76 header('Content-Type: text/plain');
79 echo "LDAP query test\n\n";
80 echo "Connecting ...\n";
81 $ds=ldap_connect($ldap_host); // must be a valid LDAP server!
82 echo " connect result - ";
87 echo "\nSetting LDAP options:\n";
89 if (ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION
, 3)) {
90 echo " Using LDAPv3\n";
92 echo " Failed to set protocol version to 3\n";
95 echo " Using LDAPv2 (php default)\n";
100 if (ldap_start_tls($ds)) {
101 echo " Turned on TLS\n";
103 echo " Unable to turn on TLS\n";
106 echo " You must use LDAPv3 protocol with STARTTLS.\n";
109 echo " Not using LDAP STARTTLS.\n";
112 if ($ldap_limit_scope) {
114 $ctrl = array ( "oid" => "1.2.840.113556.1.4.1339", "iscritical" => TRUE );
115 if (ldap_set_option($ds, LDAP_OPT_SERVER_CONTROLS
, array($ctrl))) {
116 echo " Turned on limit_scope\n";
118 echo " Unable to turn on limit_scope\n";
121 echo " You must use LDAPv3 protocol with limit_scope option.\n";
124 echo " Not using limit_scope option.\n";
127 echo "\nReading LDAP base:\n";
128 if ($sr = ldap_read($ds,'',"(objectclass=*)")) {
129 $info = ldap_get_entries($ds, $sr);
130 echo " namingContexts:\n";
131 if (isset($info[0]['namingcontexts'])) {
132 for ($i=0; $i<$info[0]['namingcontexts']['count']; $i++
) {
133 echo ' ' . $i .': ' . $info[0]['namingcontexts'][$i] . "\n";
136 echo " unavailable\n";
139 echo " Unable to read LDAP base.\n";
143 echo "Authentication:\n";
145 if ($ldap_binddn!='') {
146 echo " with authenticated bind ...\n";
147 $r = ldap_bind($ds,$ldap_binddn,$ldap_bindpw);
149 echo " with anonymous bind ...\n";
152 echo " Bind result - ";
158 echo " Searching for (mail=*) ...\n";
159 // Search for mail entries
160 if ($sr=ldap_search($ds, $ldap_basedn, "(mail=*)")) {
162 echo " Search result - ";
166 echo " Number of entries: " . ldap_count_entries($ds, $sr) . "\n";
168 echo " Getting entries ...\n";
169 $info = ldap_get_entries($ds, $sr);
171 echo " Data for " . $info["count"] . " items returned:\n";
173 for ($i=0; $i<$info["count"]; $i++
) {
174 echo " dn is: " . $info[$i]["dn"] . "\n";
175 if (isset($info[$i]["cn"][0])) {
176 echo " first cn entry is: " . $info[$i]["cn"][0] . "\n";
178 echo " cn attribute is not available.";
180 echo " first email entry is: " . $info[$i]["mail"][0] . "\n------\n";
183 echo " LDAP search failed.\n";
186 echo "Closing connection\n";
190 echo "Unable to connect to LDAP server\n";