ldap.inc 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. require_once(dirname(__FILE__).'/ldap_conf.inc');
  3. function ldap_authenticate()
  4. {
  5. global $pluginconfig;
  6. if($_SERVER['PHP_AUTH_USER']!="" && $_SERVER['PHP_AUTH_PW']!="")
  7. {
  8. $ds=ldap_connect($pluginconfig['host']);
  9. // if binding is required for LDAP search
  10. if(isset($pluginconfig['bind_dn']) && isset($pluginconfig['bind_passwd']))
  11. {
  12. @ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
  13. if(!($r=@ldap_bind($ds, $pluginconfig['bind_dn'], $pluginconfig['bind_passwd'])))
  14. return -2; // auth unsuccessful (bind error)
  15. }
  16. // perform the search
  17. if(($r=ldap_search($ds, $pluginconfig['basedn'], '(&('.$pluginconfig['user_attr'].'='.$_SERVER['PHP_AUTH_USER'].')'.(isset($pluginconfig['filter']) && $pluginconfig['filter']!='' ? '('.$pluginconfig['filter'].')' : '' ).')'))!==false)
  18. {
  19. $result=@ldap_get_entries($ds, $r);
  20. if($result[0])
  21. {
  22. @ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
  23. if(@ldap_bind($ds, $result[0]['dn'], $_SERVER['PHP_AUTH_PW']))
  24. {
  25. @ldap_unbind($bi);
  26. return 1; // auth successful
  27. }
  28. }
  29. }
  30. return -1; // auth unsuccessful
  31. }
  32. return 0; // empty username or password
  33. }
  34. ?>