~ubuntu-branches/ubuntu/raring/extplorer/raring-proposed

« back to all changes in this revision

Viewing changes to webdav_authenticate.php

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Goirand
  • Date: 2010-07-05 19:53:12 UTC
  • Revision ID: james.westby@ubuntu.com-20100705195312-i92s1udelus7gl52
Tags: upstream-2.1.0b6+dfsg
ImportĀ upstreamĀ versionĀ 2.1.0b6+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
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
 
6
 
 
7
// function to parse the http auth header
 
8
function http_digest_parse($txt)
 
9
{
 
10
        // protect against missing data
 
11
        $needed_parts = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1);
 
12
        $data = array();
 
13
 
 
14
        preg_match_all('@(\w+)=([\'"]?)([%a-zA-Z0-9=./\_-]+)\2@', $txt, $matches, PREG_SET_ORDER);
 
15
 
 
16
        foreach ($matches as $m) {
 
17
                $data[$m[1]] = $m[3];
 
18
                unset($needed_parts[$m[1]]);
 
19
        }
 
20
 
 
21
        return $needed_parts ? false : $data;
 
22
}
 
23
 
 
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');
 
29
        }
 
30
        // analyze the PHP_AUTH_DIGEST variable
 
31
        $auth = $_SERVER['PHP_AUTH_DIGEST'];
 
32
        if ($phpcgi == 1) {
 
33
                $auth = $_SERVER['REDIRECT_REMOTE_USER'];
 
34
        }
 
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');
 
39
        }
 
40
 
 
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);
 
45
 
 
46
        if ($data['response'] != $valid_response) {
 
47
                header('HTTP/1.1 401 Unauthorized');
 
48
                die('401 Unauthorized');
 
49
        }
 
50
        return TRUE;
 
51
}
 
52
 
 
53
function AuthenticationBasicHTTP($realm, $users, $phpcgi=0) {
 
54
 
 
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');
 
59
        }
 
60
 
 
61
        $user = $_SERVER['PHP_AUTH_USER'];
 
62
        if ($phpcgi == 1) {
 
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);
 
67
        }
 
68
 
 
69
        if (array_key_exists($user, $users) && $users[$user] == extEncodePassword($_SERVER['PHP_AUTH_PW']) ){
 
70
                activate_user($user, extEncodePassword($_SERVER['PHP_AUTH_PW']));
 
71
                return TRUE;
 
72
        }
 
73
 
 
74
        header('WWW-Authenticate: Basic realm="'.$realm.'"');
 
75
        header('HTTP/1.0 401 Unauthorized');
 
76
        die('401 Unauthorized');
 
77
        return FALSE;
 
78
}
 
79
?>
 
 
b'\\ No newline at end of file'