2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Provides an interface to HTTP basic authentication.
|
2008-02-25 03:10:37 +01:00
|
|
|
* @package sapphire
|
|
|
|
* @subpackage security
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class BasicAuth extends Object {
|
2008-04-09 13:17:39 +02:00
|
|
|
|
|
|
|
/**
|
2008-08-27 12:52:03 +02:00
|
|
|
* Site-wide basic auth is disabled by default but can be enabled as needed in _config.php by calling BasicAuth::enable()
|
2008-04-09 13:17:39 +02:00
|
|
|
* @var boolean
|
|
|
|
*/
|
2008-08-27 12:52:03 +02:00
|
|
|
static protected $enabled = false;
|
2008-04-09 13:17:39 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Require basic authentication. Will request a username and password if none is given.
|
2008-04-09 13:17:39 +02:00
|
|
|
*
|
|
|
|
* @usedby Controller::init()
|
|
|
|
* @param string $realm
|
|
|
|
* @param string|array $permissionCode
|
|
|
|
* @return Member $member
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
static function requireLogin($realm, $permissionCode) {
|
2008-08-27 12:52:03 +02:00
|
|
|
if(!self::$enabled) return true;
|
2008-08-09 06:38:44 +02:00
|
|
|
if(!Security::database_is_ready() || Director::is_cli()) return true;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
if(isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
|
2008-02-25 03:10:37 +01:00
|
|
|
$member = MemberAuthenticator::authenticate(array(
|
|
|
|
'Email' => $_SERVER['PHP_AUTH_USER'],
|
|
|
|
'Password' => $_SERVER['PHP_AUTH_PW'],
|
|
|
|
), null);
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($member) {
|
|
|
|
$authenticated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've failed the authentication mechanism, then show the login form
|
|
|
|
if(!isset($authenticated)) {
|
|
|
|
header("WWW-Authenticate: Basic realm=\"$realm\"");
|
|
|
|
header('HTTP/1.0 401 Unauthorized');
|
|
|
|
|
|
|
|
if(isset($_SERVER['PHP_AUTH_USER'])) {
|
2007-10-25 04:47:45 +02:00
|
|
|
echo _t('BasicAuth.ERRORNOTREC', "That username / password isn't recognised");
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2007-10-25 04:47:45 +02:00
|
|
|
echo _t('BasicAuth.ENTERINFO', "Please enter a username and password.");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
|
2007-10-02 06:30:20 +02:00
|
|
|
if(!Permission::checkMember($member->ID, $permissionCode)) {
|
2007-07-19 12:40:28 +02:00
|
|
|
header("WWW-Authenticate: Basic realm=\"$realm\"");
|
|
|
|
header('HTTP/1.0 401 Unauthorized');
|
|
|
|
|
|
|
|
if(isset($_SERVER['PHP_AUTH_USER'])) {
|
2007-10-25 04:47:45 +02:00
|
|
|
echo _t('BasicAuth.ERRORNOTADMIN', "That user is not an administrator.");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
die();
|
|
|
|
}
|
2007-10-02 06:32:11 +02:00
|
|
|
|
|
|
|
return $member;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2008-08-27 12:52:03 +02:00
|
|
|
static function enable() {
|
|
|
|
self::$enabled = true;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
static function disable() {
|
2008-08-27 12:52:03 +02:00
|
|
|
self::$enabled = false;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|