connect.inc 3.0 KB

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