-
Notifications
You must be signed in to change notification settings - Fork 22
/
webdav_authenticate.php
85 lines (73 loc) · 3.04 KB
/
webdav_authenticate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
// ensure this file is being included by a parent file
if( !defined( '_JEXEC' ) && !defined( '_VALID_MOS' ) ) die( 'Restricted access' );
# Author: Vincent JAULIN
# Copyright: Keyphrene.com 2008 @ all rights reserved
// function to parse the http auth header
function http_digest_parse($txt)
{
// protect against missing data
$needed_parts = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1);
$data = array();
preg_match_all('@(\w+)=([\'"]?)([%a-zA-Z0-9=./\_-]+)\2@', $txt, $matches, PREG_SET_ORDER);
foreach ($matches as $m) {
$data[$m[1]] = $m[3];
unset($needed_parts[$m[1]]);
}
return $needed_parts ? false : $data;
}
function AuthenticationDigestHTTP($realm, $users, $phpcgi=0) {
if (empty($_SERVER['PHP_AUTH_DIGEST']) && empty($_SERVER['REDIRECT_REMOTE_USER'])){
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Digest realm="'.$realm.'" qop="auth" nonce="'.uniqid(rand(), true).'" opaque="'.md5($realm).'"');
die('401 Unauthorized');
}
// analyze the PHP_AUTH_DIGEST variable
$auth = $_SERVER['PHP_AUTH_DIGEST'];
if ($phpcgi == 1) {
$auth = $_SERVER['REDIRECT_REMOTE_USER'];
}
$data = http_digest_parse($auth);
if (!array_key_exists($data['username'], $users)) {
header('HTTP/1.1 401 Unauthorized');
die('401 Unauthorized');
}
// generate the valid response
$A1 = md5($data['username'] . ':' . $realm . ':' . $users[$data['username']]);
$A2 = md5($_SERVER['REQUEST_METHOD'].':'.$data['uri']);
$valid_response = md5($A1.':'.$data['nonce'].':'.$data['nc'].':'.$data['cnonce'].':'.$data['qop'].':'.$A2);
if ($data['response'] != $valid_response) {
header('HTTP/1.1 401 Unauthorized');
die('401 Unauthorized');
}
return TRUE;
}
function AuthenticationBasicHTTP($realm, $users, $phpcgi=0) {
if ((empty($_SERVER['PHP_AUTH_USER']) && empty($_SERVER['REDIRECT_REMOTE_USER'])) || empty($_SERVER['PHP_AUTH_PW'])) {
header('WWW-Authenticate: Basic realm="'.$realm.'"');
header('HTTP/1.0 401 Unauthorized');
die('401 Unauthorized');
}
$user = $_SERVER['PHP_AUTH_USER'];
if ($phpcgi == 1) {
$matches = explode(' ', $_SERVER['REDIRECT_REMOTE_USER']);
list($name, $password) = explode(':', base64_decode($matches[1]));
$_SERVER['PHP_AUTH_USER'] = $user = strip_tags($name);
$_SERVER['PHP_AUTH_PW'] = strip_tags($password);
}
if( !empty($GLOBALS['webdav_authentication_method']) && file_exists(_EXT_PATH.'/include/authentication/'.$GLOBALS['webdav_authentication_method'].'.php') ) {
require_once( _EXT_PATH.'/include/authentication/'.$GLOBALS['webdav_authentication_method'].'.php');
$classname = 'ext_'.$GLOBALS['webdav_authentication_method'].'_authentication';
$auth = new $classname();
} else {
require_once( _EXT_PATH.'/include/authentication/extplorer.php');
$auth = new ext_extplorer_authentication();
}
if( $auth->onAuthenticate(array('username' => $user, 'password' => $_SERVER['PHP_AUTH_PW'] )) !== false ) {
return TRUE;
}
header('WWW-Authenticate: Basic realm="'.$realm.'"');
header('HTTP/1.0 401 Unauthorized');
die('401 Unauthorized');
return FALSE;
}