xref: /PHP-7.3/ext/ldap/tests/connect.inc (revision 3a1ba930)
1<?php
2
3/*
4Default values are "localhost", "cn=Manager,dc=my-domain,dc=com", and password "secret".
5Change the LDAP_TEST_* environment values if you want to use another configuration.
6*/
7
8$host			= getenv("LDAP_TEST_HOST")	? getenv("LDAP_TEST_HOST")	: "localhost";
9$port			= getenv("LDAP_TEST_PORT")	? getenv("LDAP_TEST_PORT")	: 389;
10$base			= getenv("LDAP_TEST_BASE")	? getenv("LDAP_TEST_BASE")	: "dc=my-domain,dc=com";
11$user			= getenv("LDAP_TEST_USER")	? getenv("LDAP_TEST_USER")	: "cn=Manager,$base";
12$sasl_user		= getenv("LDAP_TEST_SASL_USER")	? getenv("LDAP_TEST_SASL_USER")	: "Manager";
13$passwd			= getenv("LDAP_TEST_PASSWD")	? getenv("LDAP_TEST_PASSWD")	: "secret";
14$protocol_version	= getenv("LDAP_TEST_OPT_PROTOCOL_VERSION")	? getenv("LDAP_TEST_OPT_PROTOCOL_VERSION")	: 3;
15$skip_on_bind_failure	= getenv("LDAP_TEST_SKIP_BIND_FAILURE") ? getenv("LDAP_TEST_SKIP_BIND_FAILURE") : true;
16
17function ldap_connect_and_bind($host, $port, $user, $passwd, $protocol_version) {
18	$link = ldap_connect($host, $port);
19	ldap_set_option($link, LDAP_OPT_PROTOCOL_VERSION, $protocol_version);
20	ldap_bind($link, $user, $passwd);
21	return $link;
22}
23
24function test_bind($host, $port, $user, $passwd, $protocol_version) {
25	$link = ldap_connect($host, $port);
26	ldap_set_option($link, LDAP_OPT_PROTOCOL_VERSION, $protocol_version);
27	return ldap_bind($link, $user, $passwd);
28}
29
30function insert_dummy_data($link, $base) {
31	// Create root if not there
32	$testBase = ldap_read($link, $base, '(objectClass=*)', array('objectClass'));
33	if (ldap_count_entries($link, $testBase) < 1) {
34		ldap_add(
35			$link, "$base", array(
36				"objectClass"   => array(
37					"top",
38					"organization",
39					"dcObject"
40				),
41				"o" => "php ldap tests"
42			)
43		);
44	}
45	ldap_add($link, "o=test,$base", array(
46	        "objectClass"   => array(
47            "top",
48            "organization"),
49	        "o"             => "test",
50	));
51	ldap_add($link, "cn=userA,$base", array(
52	        "objectclass" => "person",
53	        "cn" => "userA",
54	        "sn" => "testSN1",
55	        "userPassword" => "oops",
56	        "telephoneNumber" => "xx-xx-xx-xx-xx",
57	        "description" => "user A",
58	));
59	ldap_add($link, "cn=userB,$base", array(
60	        "objectclass" => "person",
61	        "cn" => "userB",
62	        "sn" => "testSN2",
63	        "userPassword" => "oopsIDitItAgain",
64	        "description" => "user B",
65	));
66	ldap_add($link, "cn=userC,cn=userB,$base", array(
67	        "objectclass" => "person",
68	        "cn" => "userC",
69	        "sn" => "testSN3",
70	        "userPassword" => "0r1g1na1 passw0rd",
71	));
72	ldap_add($link, "o=test2,$base", array(
73	        "objectClass"   => array(
74            "top",
75            "organization"),
76	        "o"             => "test2",
77	        "l" => array("here", "there", "Antarctica"),
78	));
79}
80
81function remove_dummy_data($link, $base) {
82	ldap_delete($link, "cn=userC,cn=userB,$base");
83	ldap_delete($link, "cn=userA,$base");
84	ldap_delete($link, "cn=userB,$base");
85	ldap_delete($link, "o=test,$base");
86	ldap_delete($link, "o=test2,$base");
87}
88?>
89