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 8713 2006-12-01 05:08:34Z kakinaka $ |
---|
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.4.2 File: $Revision: 8713 $ |
---|
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 inoptions 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 | if ( isset($this->users[$user]) |
---|
132 | && $this->verifyPassword($pass, $this->users[$user], $this->cryptType)) { |
---|
133 | return true; |
---|
134 | } |
---|
135 | return false; |
---|
136 | } |
---|
137 | |
---|
138 | // }}} |
---|
139 | // {{{ listUsers() |
---|
140 | |
---|
141 | /** |
---|
142 | * Returns a list of users available within the container |
---|
143 | * |
---|
144 | * @return array |
---|
145 | */ |
---|
146 | function listUsers() |
---|
147 | { |
---|
148 | $ret = array(); |
---|
149 | foreach ($this->users as $username => $password) { |
---|
150 | $ret[]['username'] = $username; |
---|
151 | } |
---|
152 | return $ret; |
---|
153 | } |
---|
154 | |
---|
155 | // }}} |
---|
156 | |
---|
157 | } |
---|
158 | |
---|
159 | ?> |
---|