]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/pear/File_Passwd.php
fixed File Auth for user and group
[SourceForge/phpwiki.git] / lib / pear / File_Passwd.php
1 <?php
2 /* vim: set ts=4 sw=4: */
3 // +----------------------------------------------------------------------+
4 // | PHP Version 4                                                        |
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2002 The PHP Group                                |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 2.0 of the PHP license,       |
9 // | that is bundled with this package in the file LICENSE, and is        |
10 // | available at through the world-wide-web at                           |
11 // | http://www.php.net/license/2_02.txt.                                 |
12 // | If you did not receive a copy of the PHP license and are unable to   |
13 // | obtain it through the world-wide-web, please send a note to          |
14 // | license@php.net so we can mail you a copy immediately.               |
15 // +----------------------------------------------------------------------+
16 // | Author: Rasmus Lerdorf <rasmus@php.net>                              |
17 // +----------------------------------------------------------------------+
18 //
19 // $Id: File_Passwd.php,v 1.3 2004-03-11 13:30:47 rurban Exp $
20 //
21 // Manipulate standard UNIX passwd,.htpasswd and CVS pserver passwd files
22
23 require_once 'lib/pear/PEAR.php' ;
24
25 /**
26 * Class to manage passwd-style files
27 *
28 * @author Rasmus Lerdorf <rasmus@php.net>
29 */
30 class File_Passwd {
31
32     /**
33     * Passwd file
34     * @var string
35     */
36     var $filename ;
37
38     /**
39     * Hash list of users
40     * @var array
41     */
42     var $users ;
43     
44     /**
45     * hash list of csv-users
46     * @var array
47     */
48     var $cvs ;
49     
50     /**
51     * filehandle for lockfile
52     * @var int
53     */
54     var $fplock ;
55     
56     /**
57     * locking state
58     * @var boolean
59     */
60     var $locked ;
61     
62     /**
63     * name of the lockfile
64     * @var string    
65     */ 
66     var $lockfile = './passwd.lock';
67
68     /**
69     * Constructor
70     * Requires the name of the passwd file. This functions opens the file and read it.
71     * Changes to this file will written first in the lock file, so it is still possible
72     * to access the passwd file by another programs. The lock parameter controls the locking
73     * oft the lockfile, not of the passwd file! ( Swapping $lock and $lockfile would
74     * breaks bc to v1.3 and smaller).
75     * Don't forget to call close() to save changes!
76     * 
77     * @param $file              name of the passwd file
78     * @param $lock              if 'true' $lockfile will be locked
79     * @param $lockfile  name of the temp file, where changes are saved
80     *
81     * @access public
82     * @see close() 
83     */
84
85     function File_Passwd($file, $lock = 0, $lockfile = "") {
86         $this->filename = $file;
87         if( !empty( $lockfile) ) {
88             $this->lockfile = $lockfile ;
89         }
90
91         if($lock) {
92             $this->fplock = fopen($this->lockfile, 'w');
93             flock($this->fplock, LOCK_EX);
94             $this->locked = true;
95         }
96     
97         $fp = fopen($file,'r') ;
98         if( !$fp) {
99             return new PEAR_Error( "Couldn't open '$file'!", 1, PEAR_ERROR_RETURN) ;
100         }
101         while(!feof($fp)) {
102             $line = fgets($fp, 128);
103             $array = explode(':', $line);
104             if (count($array) and strlen(trim($array[0]))) {
105                 $user = trim($array[0]);
106                 if (empty($array[1])) $array[1]='';
107                 $this->users[$user] = trim($array[1]);
108                 if (count($array) >= 3)
109                     $this->cvs[$user] = trim($array[2]);        
110             }
111         }
112         fclose($fp);
113     } // end func File_Passwd()
114
115     /**
116     * Adds a user
117     *
118     * @param $user new user id
119     * @param $pass password for new user
120     * @param $cvs  cvs user id (needed for pserver passwd files)
121     *
122     * @return mixed returns PEAR_Error, if the user already exists
123     * @access public
124     */
125     function addUser($user, $pass, $cvsuser = "") {
126         if(!isset($this->users[$user]) && $this->locked) {
127             $this->users[$user] = crypt($pass);
128             $this->cvs[$user] = $cvsuser;
129             return true;
130         } else {
131             return new PEAR_Error( "Couldn't add user '$user', because the user already exists!", 2, PEAR_ERROR_RETURN);
132         }
133     } // end func addUser()
134
135     /**
136     * Modifies a user
137     *
138     * @param $user user id
139     * @param $pass new password for user
140     * @param $cvs  cvs user id (needed for pserver passwd files)
141     *
142     * @return mixed returns PEAR_Error, if the user doesn't exists
143     * @access public
144     */
145
146     function modUser($user, $pass, $cvsuser="") {
147         if(isset($this->users[$user]) && $this->locked) {
148             $this->users[$user] = crypt($pass);
149             $this->cvs[$user] = $cvsuser;
150             return true;
151         } else {
152             return new PEAR_Error( "Couldn't modify user '$user', because the user doesn't exists!", 3, PEAR_ERROR_RETURN) ;
153         }
154     } // end func modUser()
155
156     /**
157     * Deletes a user
158     *
159     * @param $user user id
160     *
161     * @return mixed returns PEAR_Error, if the user doesn't exists
162     * @access public    
163     */
164     
165     function delUser($user) {
166         if(isset($this->users[$user]) && $this->locked) {
167             unset($this->users[$user]);
168             unset($this->cvs[$user]);
169         } else {
170             return new PEAR_Error( "Couldn't delete user '$user', because the user doesn't exists!", 3, PEAR_ERROR_RETURN) ; 
171         }
172     } // end func delUser()
173
174     /**
175     * Verifies a user's password
176     *
177     * @param $user user id
178     * @param $pass password for user
179     *
180     * @return boolean true if password is ok
181     * @access public            
182     */
183     function verifyPassword($user, $pass) {
184         if(isset($this->users[$user])) {
185             if($this->users[$user] == crypt($pass, substr($this->users[$user], 0, 2))) return true;
186         }
187         return false;
188     } // end func verifyPassword()
189
190     /**
191     * Return all users from passwd file
192     *
193     * @access public
194     * @return array
195     */
196     function listUsers() {
197         return $this->users;
198     } // end func listUsers()
199
200     /**
201     * Writes changes to passwd file and unlocks it
202     *
203     * @access public
204     */
205     function close() {
206         if($this->locked) {
207             foreach($this->users as $user => $pass) {
208                 if($this->cvs[$user]) {
209                     fputs($this->fplock, "$user:$pass:" . $this->cvs[$user] . "\n");
210                 } else {
211                     fputs($this->fplock, "$user:$pass\n");
212                 }
213             }
214             rename($this->lockfile, $this->filename);
215             flock($this->fplock, LOCK_UN);
216             $this->locked = false;
217             fclose($this->fplock);
218         }
219     } // end func close()
220 }
221 ?>