connect.inc 3.0 KB

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