2
// ensure this file is being included by a parent file
3
if( !defined( '_JEXEC' ) && !defined( '_VALID_MOS' ) ) die( 'Restricted access' );
4
# Author: Vincent JAULIN
5
# Copyright: Keyphrene.com 2008 @ all rights reserved
7
// function to parse the http auth header
8
function http_digest_parse($txt)
10
// protect against missing data
11
$needed_parts = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1);
14
preg_match_all('@(\w+)=([\'"]?)([%a-zA-Z0-9=./\_-]+)\2@', $txt, $matches, PREG_SET_ORDER);
16
foreach ($matches as $m) {
18
unset($needed_parts[$m[1]]);
21
return $needed_parts ? false : $data;
24
function AuthenticationDigestHTTP($realm, $users, $phpcgi=0) {
25
if (empty($_SERVER['PHP_AUTH_DIGEST']) && empty($_SERVER['REDIRECT_REMOTE_USER'])){
26
header('HTTP/1.1 401 Unauthorized');
27
header('WWW-Authenticate: Digest realm="'.$realm.'" qop="auth" nonce="'.uniqid(rand(), true).'" opaque="'.md5($realm).'"');
28
die('401 Unauthorized');
30
// analyze the PHP_AUTH_DIGEST variable
31
$auth = $_SERVER['PHP_AUTH_DIGEST'];
33
$auth = $_SERVER['REDIRECT_REMOTE_USER'];
35
$data = http_digest_parse($auth);
36
if (!array_key_exists($data['username'], $users)) {
37
header('HTTP/1.1 401 Unauthorized');
38
die('401 Unauthorized');
41
// generate the valid response
42
$A1 = md5($data['username'] . ':' . $realm . ':' . $users[$data['username']]);
43
$A2 = md5($_SERVER['REQUEST_METHOD'].':'.$data['uri']);
44
$valid_response = md5($A1.':'.$data['nonce'].':'.$data['nc'].':'.$data['cnonce'].':'.$data['qop'].':'.$A2);
46
if ($data['response'] != $valid_response) {
47
header('HTTP/1.1 401 Unauthorized');
48
die('401 Unauthorized');
53
function AuthenticationBasicHTTP($realm, $users, $phpcgi=0) {
55
if (empty($_SERVER['PHP_AUTH_USER']) && empty($_SERVER['REDIRECT_REMOTE_USER'])) {
56
header('WWW-Authenticate: Basic realm="'.$realm.'"');
57
header('HTTP/1.0 401 Unauthorized');
58
die('401 Unauthorized');
61
$user = $_SERVER['PHP_AUTH_USER'];
63
$matches = explode(' ', $_SERVER['REDIRECT_REMOTE_USER']);
64
list($name, $password) = explode(':', base64_decode($matches[1]));
65
$_SERVER['PHP_AUTH_USER'] = $user = strip_tags($name);
66
$_SERVER['PHP_AUTH_PW'] = strip_tags($password);
69
if (array_key_exists($user, $users) && $users[$user] == extEncodePassword($_SERVER['PHP_AUTH_PW']) ){
70
activate_user($user, extEncodePassword($_SERVER['PHP_AUTH_PW']));
74
header('WWW-Authenticate: Basic realm="'.$realm.'"');
75
header('HTTP/1.0 401 Unauthorized');
76
die('401 Unauthorized');
b'\\ No newline at end of file'