<?php
/*
ldap_auth() - HTTP authentication to LDAP server using PHP
Copyright (C) 2005 Kjetil Valen http://nn.urbanturban.no <kjetil@urbanturban.no>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// ldap_auth()
//
// Usage: Require this file in your script:
// require('ldapauth.php');
//
// Call the ldap_auth function before any output.
//
// boolean ldap_auth ( string hostname[, int port[, string realmName]] )
//
function ldap_auth($hostname, $port, $area, $realmName) {
if ( ! isset($realmName) ) {
$realmName = "Please authenticate.";
}
if ( ! isset($port) ) {
$port = 389;
}
if ( ! isset($_SERVER['PHP_AUTH_USER']) ) {
header("WWW-Authenticate: Basic realm=\"$realmName\"");
header('HTTP/1.0 401 Unauthorized');
exit;
} else {
$connection = ldap_connect( $hostname, $port )
or die();
if (! ldap_bind( $connection, $_SERVER[PHP_AUTH_USER], $_SERVER[PHP_AUTH_PW] )) {
header("WWW-Authenticate: Basic realm=\"$realmName\"");
header('HTTP/1.0 401 Unauthorized');
exit;
}
}
}
?>