This class was originally written to query the Sun Microsystems LDAP server
but can be applied to any LDAP server. Remember you must have php compiled with LDAP
for this to work
<?php
/*
# wapper ldap Class
#Copyright (C) 2006 Steven McCullie
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# ldap.class.php
#
# All the functions
# Relating to ldap server
# user for searching and
# auth.
# Steven@orinett.com
*/
class ldapauth
{
var $ds;
var $base_dn;
function ldapauth()
{
$this->ds = '';
$this->base_dn = '';
//end of conctruct
}
function _ldapCon()
{
//connect to LDAP Server
if($ds=ldap_connect($this->ds))
{
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
return $ds;
}else{
echo 'NO LDAP SERVER';
exit();
}
//end of function
}
function auth($sunid,$password)
{
$ds = $this->_ldapCon();
$dn = $this->ldapGetDetails($sunid);
$dn = $dn['dn'];
if($r = ldap_bind($ds,$dn,$password) && !$password == '')
{
return true;
}else{
return false;
}
//end of function
}
function ldapSearch($searchType,$str)
{
$ds = $this->_ldapCon();
if($r = ldap_bind($ds))
{
$sr=ldap_search($ds,$this->base_dn,$searchType.'='.$str.'');
$info = ldap_get_entries($ds, $sr);
return $info;
}else{
return false;
}
//end of function
}
//end of class
}
?>