Subversion Repositories Applications.papyrus

Rev

Rev 320 | Rev 1713 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1173 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 generic password file
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     Stefan Ekman <stekman@sedata.org>
18
 * @author     Martin Jansen <mj@php.net>
19
 * @author     Mika Tuupola <tuupola@appelsiini.net>
20
 * @author     Michael Wallner <mike@php.net>
21
 * @author     Adam Ashley <aashley@php.net>
22
 * @copyright  2001-2006 The PHP Group
23
 * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
24
 * @version    CVS: $Id: File.php,v 1.2 2006-12-14 15:04:28 jp_milcent Exp $
25
 * @link       http://pear.php.net/package/Auth
26
 */
27
 
28
/**
29
 * Include PEAR File_Passwd package
30
 */
31
require_once "File/Passwd.php";
32
/**
33
 * Include Auth_Container base class
34
 */
35
require_once "Auth/Container.php";
36
/**
37
 * Include PEAR package for error handling
38
 */
39
require_once "PEAR.php";
40
 
41
/**
42
 * Storage driver for fetching login data from an encrypted password file.
43
 *
44
 * This storage container can handle CVS pserver style passwd files.
45
 *
46
 * @category   Authentication
47
 * @package    Auth
48
 * @author     Stefan Ekman <stekman@sedata.org>
49
 * @author     Martin Jansen <mj@php.net>
50
 * @author     Mika Tuupola <tuupola@appelsiini.net>
51
 * @author     Michael Wallner <mike@php.net>
52
 * @author     Adam Ashley <aashley@php.net>
53
 * @copyright  2001-2006 The PHP Group
54
 * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
55
 * @version    Release: 1.4.3  File: $Revision: 1.2 $
56
 * @link       http://pear.php.net/package/Auth
57
 */
58
class Auth_Container_File extends Auth_Container
59
{
60
 
61
    // {{{ properties
62
 
63
    /**
64
     * Path to passwd file
65
     *
66
     * @var string
67
     */
68
    var $pwfile = '';
69
 
70
    /**
71
     * Options for container
72
     *
73
     * @var array
74
     */
75
    var $options = array();
76
 
77
    // }}}
78
    // {{{ Auth_Container_File() [constructor]
79
 
80
    /**
81
     * Constructor of the container class
82
     *
83
     * @param  string $filename             path to passwd file
84
     * @return object Auth_Container_File   new Auth_Container_File object
85
     */
86
    function Auth_Container_File($filename) {
87
        $this->_setDefaults();
88
 
89
        // Only file is a valid option here
90
        if(is_array($filename)) {
91
            $this->pwfile = $filename['file'];
92
            $this->_parseOptions($filename);
93
        } else {
94
            $this->pwfile = $filename;
95
        }
96
    }
97
 
98
    // }}}
99
    // {{{ fetchData()
100
 
101
    /**
102
     * Authenticate an user
103
     *
104
     * @param   string  username
105
     * @param   string  password
106
     * @return  mixed   boolean|PEAR_Error
107
     */
108
    function fetchData($user, $pass)
109
    {
110
        return File_Passwd::staticAuth($this->options['type'], $this->pwfile, $user, $pass);
111
    }
112
 
113
    // }}}
114
    // {{{ listUsers()
115
 
116
    /**
117
     * List all available users
118
     *
119
     * @return   array
120
     */
121
    function listUsers()
122
    {
123
        $pw_obj = &$this->_load();
124
        if (PEAR::isError($pw_obj)) {
125
            return array();
126
        }
127
 
128
        $users  = $pw_obj->listUser();
129
        if (!is_array($users)) {
130
            return array();
131
        }
132
 
133
        foreach ($users as $key => $value) {
134
            $retVal[] = array("username" => $key,
135
                              "password" => $value['passwd'],
136
                              "cvsuser"  => $value['system']);
137
        }
138
 
139
        return $retVal;
140
    }
141
 
142
    // }}}
143
    // {{{ addUser()
144
 
145
    /**
146
     * Add a new user to the storage container
147
     *
148
     * @param string username
149
     * @param string password
150
     * @param mixed  Additional parameters to File_Password_*::addUser()
151
     *
152
     * @return boolean
153
     */
154
    function addUser($user, $pass, $additional='')
155
    {
156
        $params = array($user, $pass);
157
        if (is_array($additional)) {
158
            foreach ($additional as $item) {
159
                $params[] = $item;
160
            }
161
        } else {
162
            $params[] = $additional;
163
        }
164
 
165
        $pw_obj = &$this->_load();
166
        if (PEAR::isError($pw_obj)) {
167
            return false;
168
        }
169
 
170
        $res = call_user_func_array(array(&$pw_obj, 'addUser'), $params);
171
        if (PEAR::isError($res)) {
172
            return false;
173
        }
174
 
175
        $res = $pw_obj->save();
176
        if (PEAR::isError($res)) {
177
            return false;
178
        }
179
 
180
        return true;
181
    }
182
 
183
    // }}}
184
    // {{{ removeUser()
185
 
186
    /**
187
     * Remove user from the storage container
188
     *
189
     * @param   string  Username
190
     * @return  boolean
191
     */
192
    function removeUser($user)
193
    {
194
        $pw_obj = &$this->_load();
195
        if (PEAR::isError($pw_obj)) {
196
            return false;
197
        }
198
 
199
        $res = $pw_obj->delUser($user);
200
        if (PEAR::isError($res)) {
201
            return false;
202
        }
203
 
204
        $res = $pw_obj->save();
205
        if (PEAR::isError($res)) {
206
            return false;
207
        }
208
 
209
        return true;
210
    }
211
 
212
    // }}}
213
    // {{{ changePassword()
214
 
215
    /**
216
     * Change password for user in the storage container
217
     *
218
     * @param string Username
219
     * @param string The new password
220
     */
221
    function changePassword($username, $password)
222
    {
223
        $pw_obj = &$this->_load();
224
        if (PEAR::isError($pw_obj)) {
225
            return false;
226
        }
227
 
228
        $res = $pw_obj->changePasswd($username, $password);
229
        if (PEAR::isError($res)) {
230
            return false;
231
        }
232
 
233
        $res = $pw_obj->save();
234
        if (PEAR::isError($res)) {
235
            return false;
236
        }
237
 
238
        return true;
239
    }
240
 
241
    // }}}
242
    // {{{ _load()
243
 
244
    /**
245
     * Load and initialize the File_Passwd object
246
     *
247
     * @return  object  File_Passwd_Cvs|PEAR_Error
248
     */
249
    function &_load()
250
    {
251
        static $pw_obj;
252
 
253
        if (!isset($pw_obj)) {
254
            $pw_obj = File_Passwd::factory($this->options['type']);
255
            if (PEAR::isError($pw_obj)) {
256
                return $pw_obj;
257
            }
258
 
259
            $pw_obj->setFile($this->pwfile);
260
 
261
            $res = $pw_obj->load();
262
            if (PEAR::isError($res)) {
263
                return $res;
264
            }
265
        }
266
 
267
        return $pw_obj;
268
    }
269
 
270
    // }}}
271
    // {{{ _setDefaults()
272
 
273
    /**
274
     * Set some default options
275
     *
276
     * @access private
277
     * @return void
278
     */
279
    function _setDefaults()
280
    {
281
        $this->options['type']       = 'Cvs';
282
    }
283
 
284
    // }}}
285
    // {{{ _parseOptions()
286
 
287
    /**
288
     * Parse options passed to the container class
289
     *
290
     * @access private
291
     * @param  array
292
     */
293
    function _parseOptions($array)
294
    {
295
        foreach ($array as $key => $value) {
296
            if (isset($this->options[$key])) {
297
                $this->options[$key] = $value;
298
            }
299
        }
300
    }
301
 
302
    // }}}
303
 
304
}
305
?>