--- htusers_ldap.php 2002/02/15 09:54:05 1.1 +++ htusers_ldap.php 2003/01/08 16:38:58 1.2 @@ -7,10 +7,16 @@ Belenos INC For use with the DocMgr PHP scripts + Modified by Benjamin Baez (bbaez@biospectra.com) 2002-12-03 + arguments in docman.conf file are: $ldapServer="x.x.x.x"; This can be in Dotted Notation or a DNS FQN $ldapServerPort="389"; This is the default port and doesnt need to be changed $basedn="o=CompanyName"; Branch of tree that your search will start on + + If you don't enter following two parameters, it + will use credentials of user which is logged in! + $bind="cn=Manager, o=CompanyName"; Login that allows password searching $bindpw=""; Password for the above account @@ -31,32 +37,52 @@ */ -if ($ds = ldap_connect_bind($bind, $bindpw, $ldapServer, $ldapServerPort)) { - $sres = ldap_search($ds, $basedn, "uid=".$PHP_AUTH_USER); - If ($sres) { - $entries = ldap_get_entries($ds, $sres); - $gblUserName = $entries[0]["cn"][0]; //Full Name - $gblPw = $entries[0]["userpassword"][0]; //Password - $gblEmail = $entries[0]["mail"][0]; //Email - }else{ - Error("Not Found","LDAP Search returned false"); - } - ldap_close($ds); +// use hard-coded bind variables in docman.conf +if (isset($bind) && isset($bindpw)) { + $ds = ldap_connect_bind($bind, $bindpw, $ldapServer, $ldapServerPort)) { + $sres = ldap_search($ds, $basedn, "uid=".$PHP_AUTH_USER); + If ($sres) { + $entries = ldap_get_entries($ds, $sres); + $gblUserName = $entries[0]["cn"][0]; //Full Name + $gblPw = $entries[0]["userpassword"][0]; //Password + $gblEmail = $entries[0]["mail"][0]; //Email + } else { + Error("Not Found","LDAP Search returned false"); + } + ldap_close($ds); +// use crednetials of user which is currently logged in +} elseif (isset($PHP_AUTH_PW)) { // This is required so that auth dialog appears + if ($ds = ldap_connect_bind($PHP_AUTH_USER, $PHP_AUTH_PW, $ldapServer, $ldapServerPort, $basedn)) { + $sres = ldap_search($ds, $basedn, "uid=".$PHP_AUTH_USER); + if ($sres) { + $entries = ldap_get_entries($ds, $sres); + $gblUserName = $entries[0]["cn"][0]; //Full Name + $gblPw = md5($PHP_AUTH_USER.$PHP_AUTH_PW); //Password format for Web Server + $gblEmail = $entries[0]["mail"][0]; //Email + } else { + Error("Not Found","LDAP Search returned false"); + } + ldap_close($ds); + } } -function ldap_connect_bind($bindRDN, $bindpass, $ldapServer, $ldapServerPort) { - $linkid = ldap_connect($ldapServer, $ldapServerPort); - if ($linkid) { - if (!ldap_bind($linkid, $bindRDN, $bindpass)) { - Error("LDAP BIND","Unable to bind to LDAP server!"); - return 0; - } else { - return $linkid; - } - } else { - Error("LDAP CONNECT","Unable to connect to LDAP server!"); - return 0; - } +function ldap_connect_bind($bindDN_or_user, $passwd, $ldapServer, $ldapServerPort, $basedn) { + $linkid = ldap_connect($ldapServer, $ldapServerPort); + if (isset($basedn)) { + // make a username to bind DN + $bindDN_or_user = "uid=".$bindDN_or_user.",".$basedn; + } + if ($linkid) { + if (!@ldap_bind($linkid, $bindDN_or_user, $passwd)) { + Error("LDAP BIND","Unable to bind to LDAP server!"); + return 0; + } else { + return $linkid; + } + } else { + Error("LDAP CONNECT","Unable to connect to LDAP server!"); + return 0; + } } ?>