Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1712 jp_milcent 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
3
 
4
/**
5
 * Storage driver for use against a PHP Array
6
 *
7
 * PHP versions 4 and 5
8
 *
9
 * LICENSE: This source file is subject to version 3.01 of the PHP license
10
 * that is available through the world-wide-web at the following URI:
11
 * http://www.php.net/license/3_01.txt.  If you did not receive a copy of
12
 * the PHP License and are unable to obtain it through the web, please
13
 * send a note to license@php.net so we can mail you a copy immediately.
14
 *
15
 * @category   Authentication
16
 * @package    Auth
17
 * @author     georg_1 at have2 dot com
18
 * @author     Adam Ashley <aashley@php.net>
19
 * @copyright  2001-2006 The PHP Group
20
 * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
21
 * @version    CVS: $Id: Array.php,v 1.1.4.2 2007-11-19 14:54:05 jp_milcent Exp $
22
 * @since      File available since Release 1.4.0
23
 */
24
 
25
/**
26
 * Include Auth_Container base class
27
 */
28
require_once "Auth/Container.php";
29
/**
30
 * Include PEAR package for error handling
31
 */
32
require_once "PEAR.php";
33
 
34
/**
35
 * Storage driver for fetching authentication data from a PHP Array
36
 *
37
 * This container takes two options when configuring:
38
 *
39
 * cryptType:   The crypt used to store the password. Currently recognised
40
 *              are: none, md5 and crypt. default: none
41
 * users:       A named array of usernames and passwords.
42
 *              Ex:
43
 *              array(
44
 *                  'guest' => '084e0343a0486ff05530df6c705c8bb4', // password guest
45
 *                  'georg' => 'fc77dba827fcc88e0243404572c51325'  // password georg
46
 *              )
47
 *
48
 * Usage Example:
49
 * <?php
50
 * $AuthOptions = array(
51
 *      'users' => array(
52
 *          'guest' => '084e0343a0486ff05530df6c705c8bb4', // password guest
53
 *          'georg' => 'fc77dba827fcc88e0243404572c51325'  // password georg
54
 *      ),
55
 *      'cryptType'=>'md5',
56
 *  );
57
 *
58
 * $auth = new Auth("Array", $AuthOptions);
59
 * ?>
60
 *
61
 * @category   Authentication
62
 * @package    Auth
63
 * @author     georg_1 at have2 dot com
64
 * @author     Adam Ashley <aashley@php.net>
65
 * @copyright  2001-2006 The PHP Group
66
 * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
67
 * @version    Release: 1.5.4  File: $Revision: 1.1.4.2 $
68
 * @since      File available since Release 1.4.0
69
 */
70
 
71
class Auth_Container_Array extends Auth_Container {
72
 
73
    // {{{ properties
74
 
75
    /**
76
     * The users and their password to authenticate against
77
     *
78
     * @var array $users
79
     */
80
    var $users;
81
 
82
    /**
83
     * The cryptType used on the passwords
84
     *
85
     * @var string $cryptType
86
     */
87
    var $cryptType = 'none';
88
 
89
    // }}}
90
    // {{{ Auth_Container_Array()
91
 
92
    /**
93
     * Constructor for Array Container
94
     *
95
     * @param array $data Options for the container
96
     * @return void
97
     */
98
    function Auth_Container_Array($data)
99
    {
100
        if (!is_array($data)) {
101
            PEAR::raiseError('The options for Auth_Container_Array must be an array');
102
        }
103
        if (isset($data['users']) && is_array($data['users'])) {
104
            $this->users = $data['users'];
105
        } else {
106
            $this->users = array();
107
            PEAR::raiseError('Auth_Container_Array: no user data found in options array');
108
        }
109
        if (isset($data['cryptType'])) {
110
            $this->cryptType = $data['cryptType'];
111
        }
112
    }
113
 
114
    // }}}
115
    // {{{ fetchData()
116
 
117
    /**
118
     * Get user information from array
119
     *
120
     * This function uses the given username to fetch the corresponding
121
     * login data from the array. If an account that matches the passed
122
     * username and password is found, the function returns true.
123
     * Otherwise it returns false.
124
     *
125
     * @param  string Username
126
     * @param  string Password
127
     * @return boolean|PEAR_Error Error object or boolean
128
     */
129
    function fetchData($user, $pass)
130
    {
131
        $this->log('Auth_Container_Array::fetchData() called.', AUTH_LOG_DEBUG);
132
        if (   isset($this->users[$user])
133
            && $this->verifyPassword($pass, $this->users[$user], $this->cryptType)) {
134
            return true;
135
        }
136
        return false;
137
    }
138
 
139
    // }}}
140
    // {{{ listUsers()
141
 
142
    /**
143
     * Returns a list of users available within the container
144
     *
145
     * @return array
146
     */
147
    function listUsers()
148
    {
149
        $this->log('Auth_Container_Array::listUsers() called.', AUTH_LOG_DEBUG);
150
        $ret = array();
151
        foreach ($this->users as $username => $password) {
152
            $ret[]['username'] = $username;
153
        }
154
        return $ret;
155
    }
156
 
157
    // }}}
158
 
159
}
160
 
161
?>