Subversion Repositories Applications.papyrus

Rev

Rev 1713 | 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
1713 jp_milcent 17
 * @author     Stefan Ekman <stekman@sedata.org>
1173 jp_milcent 18
 * @author     Martin Jansen <mj@php.net>
1713 jp_milcent 19
 * @author     Mika Tuupola <tuupola@appelsiini.net>
1173 jp_milcent 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
2150 mathias 24
 * @version    CVS: $Id: File.php,v 1.25 2007/06/12 03:11:26 aashley Exp $
1173 jp_milcent 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
1713 jp_milcent 48
 * @author     Stefan Ekman <stekman@sedata.org>
1173 jp_milcent 49
 * @author     Martin Jansen <mj@php.net>
1713 jp_milcent 50
 * @author     Mika Tuupola <tuupola@appelsiini.net>
1173 jp_milcent 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
2150 mathias 55
 * @version    Release: 1.5.4  File: $Revision: 1.25 $
1173 jp_milcent 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
1713 jp_milcent 65
     *
1173 jp_milcent 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();
1713 jp_milcent 88
 
1173 jp_milcent 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
    {
1713 jp_milcent 110
        $this->log('Auth_Container_File::fetchData() called.', AUTH_LOG_DEBUG);
1173 jp_milcent 111
        return File_Passwd::staticAuth($this->options['type'], $this->pwfile, $user, $pass);
112
    }
113
 
114
    // }}}
115
    // {{{ listUsers()
1713 jp_milcent 116
 
1173 jp_milcent 117
    /**
118
     * List all available users
1713 jp_milcent 119
     *
1173 jp_milcent 120
     * @return   array
121
     */
122
    function listUsers()
123
    {
1713 jp_milcent 124
        $this->log('Auth_Container_File::listUsers() called.', AUTH_LOG_DEBUG);
125
 
1173 jp_milcent 126
        $pw_obj = &$this->_load();
127
        if (PEAR::isError($pw_obj)) {
128
            return array();
129
        }
130
 
131
        $users  = $pw_obj->listUser();
132
        if (!is_array($users)) {
133
            return array();
134
        }
135
 
136
        foreach ($users as $key => $value) {
1713 jp_milcent 137
            $retVal[] = array("username" => $key,
1173 jp_milcent 138
                              "password" => $value['passwd'],
139
                              "cvsuser"  => $value['system']);
140
        }
141
 
1713 jp_milcent 142
        $this->log('Found '.count($retVal).' users.', AUTH_LOG_DEBUG);
143
 
1173 jp_milcent 144
        return $retVal;
145
    }
146
 
147
    // }}}
148
    // {{{ addUser()
149
 
150
    /**
151
     * Add a new user to the storage container
152
     *
153
     * @param string username
154
     * @param string password
155
     * @param mixed  Additional parameters to File_Password_*::addUser()
156
     *
157
     * @return boolean
158
     */
159
    function addUser($user, $pass, $additional='')
160
    {
1713 jp_milcent 161
        $this->log('Auth_Container_File::addUser() called.', AUTH_LOG_DEBUG);
1173 jp_milcent 162
        $params = array($user, $pass);
163
        if (is_array($additional)) {
164
            foreach ($additional as $item) {
165
                $params[] = $item;
166
            }
167
        } else {
168
            $params[] = $additional;
169
        }
170
 
171
        $pw_obj = &$this->_load();
172
        if (PEAR::isError($pw_obj)) {
173
            return false;
174
        }
1713 jp_milcent 175
 
1173 jp_milcent 176
        $res = call_user_func_array(array(&$pw_obj, 'addUser'), $params);
177
        if (PEAR::isError($res)) {
178
            return false;
179
        }
1713 jp_milcent 180
 
1173 jp_milcent 181
        $res = $pw_obj->save();
182
        if (PEAR::isError($res)) {
183
            return false;
184
        }
1713 jp_milcent 185
 
1173 jp_milcent 186
        return true;
187
    }
188
 
189
    // }}}
190
    // {{{ removeUser()
191
 
192
    /**
193
     * Remove user from the storage container
194
     *
195
     * @param   string  Username
196
     * @return  boolean
197
     */
198
    function removeUser($user)
199
    {
1713 jp_milcent 200
        $this->log('Auth_Container_File::removeUser() called.', AUTH_LOG_DEBUG);
1173 jp_milcent 201
        $pw_obj = &$this->_load();
202
        if (PEAR::isError($pw_obj)) {
203
            return false;
204
        }
1713 jp_milcent 205
 
1173 jp_milcent 206
        $res = $pw_obj->delUser($user);
207
        if (PEAR::isError($res)) {
208
            return false;
209
        }
1713 jp_milcent 210
 
1173 jp_milcent 211
        $res = $pw_obj->save();
212
        if (PEAR::isError($res)) {
213
            return false;
214
        }
1713 jp_milcent 215
 
1173 jp_milcent 216
        return true;
217
    }
218
 
219
    // }}}
220
    // {{{ changePassword()
221
 
222
    /**
223
     * Change password for user in the storage container
224
     *
225
     * @param string Username
1713 jp_milcent 226
     * @param string The new password
1173 jp_milcent 227
     */
228
    function changePassword($username, $password)
229
    {
1713 jp_milcent 230
        $this->log('Auth_Container_File::changePassword() called.', AUTH_LOG_DEBUG);
1173 jp_milcent 231
        $pw_obj = &$this->_load();
232
        if (PEAR::isError($pw_obj)) {
233
            return false;
234
        }
1713 jp_milcent 235
 
1173 jp_milcent 236
        $res = $pw_obj->changePasswd($username, $password);
237
        if (PEAR::isError($res)) {
238
            return false;
239
        }
1713 jp_milcent 240
 
1173 jp_milcent 241
        $res = $pw_obj->save();
242
        if (PEAR::isError($res)) {
243
            return false;
244
        }
1713 jp_milcent 245
 
1173 jp_milcent 246
        return true;
247
    }
248
 
249
    // }}}
250
    // {{{ _load()
1713 jp_milcent 251
 
1173 jp_milcent 252
    /**
253
     * Load and initialize the File_Passwd object
1713 jp_milcent 254
     *
1173 jp_milcent 255
     * @return  object  File_Passwd_Cvs|PEAR_Error
256
     */
257
    function &_load()
258
    {
259
        static $pw_obj;
1713 jp_milcent 260
 
1173 jp_milcent 261
        if (!isset($pw_obj)) {
1713 jp_milcent 262
            $this->log('Instanciating File_Password object of type '.$this->options['type'], AUTH_LOG_DEBUG);
1173 jp_milcent 263
            $pw_obj = File_Passwd::factory($this->options['type']);
264
            if (PEAR::isError($pw_obj)) {
265
                return $pw_obj;
266
            }
1713 jp_milcent 267
 
1173 jp_milcent 268
            $pw_obj->setFile($this->pwfile);
1713 jp_milcent 269
 
1173 jp_milcent 270
            $res = $pw_obj->load();
271
            if (PEAR::isError($res)) {
272
                return $res;
273
            }
274
        }
1713 jp_milcent 275
 
1173 jp_milcent 276
        return $pw_obj;
277
    }
278
 
279
    // }}}
280
    // {{{ _setDefaults()
281
 
282
    /**
283
     * Set some default options
284
     *
285
     * @access private
286
     * @return void
287
     */
288
    function _setDefaults()
289
    {
290
        $this->options['type']       = 'Cvs';
291
    }
292
 
293
    // }}}
294
    // {{{ _parseOptions()
295
 
296
    /**
297
     * Parse options passed to the container class
298
     *
299
     * @access private
300
     * @param  array
301
     */
302
    function _parseOptions($array)
303
    {
304
        foreach ($array as $key => $value) {
305
            if (isset($this->options[$key])) {
306
                $this->options[$key] = $value;
307
            }
308
        }
309
    }
310
 
311
    // }}}
312
 
313
}
314
?>