]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/pear/File_Passwd.php
fixed pear/File_Passwd for Windows
[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.4 2004-04-07 23:13:19 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 (in_array(substr($user,0,1),array('#',';'))) continue;
107                 if (empty($array[1])) $array[1]='';
108                 $this->users[$user] = trim($array[1]);
109                 if (count($array) >= 3)
110                     $this->cvs[$user] = trim($array[2]);        
111             }
112         }
113         fclose($fp);
114     } // end func File_Passwd()
115
116     /**
117     * Adds a user
118     *
119     * @param $user new user id
120     * @param $pass password for new user
121     * @param $cvs  cvs user id (needed for pserver passwd files)
122     *
123     * @return mixed returns PEAR_Error, if the user already exists
124     * @access public
125     */
126     function addUser($user, $pass, $cvsuser = "") {
127         if(!isset($this->users[$user]) && $this->locked) {
128             $this->users[$user] = crypt($pass);
129             $this->cvs[$user] = $cvsuser;
130             return true;
131         } else {
132             return new PEAR_Error( "Couldn't add user '$user', because the user already exists!", 2, PEAR_ERROR_RETURN);
133         }
134     } // end func addUser()
135
136     /**
137     * Modifies a user
138     *
139     * @param $user user id
140     * @param $pass new password for user
141     * @param $cvs  cvs user id (needed for pserver passwd files)
142     *
143     * @return mixed returns PEAR_Error, if the user doesn't exists
144     * @access public
145     */
146
147     function modUser($user, $pass, $cvsuser="") {
148         if(isset($this->users[$user]) && $this->locked) {
149             $this->users[$user] = crypt($pass);
150             $this->cvs[$user] = $cvsuser;
151             return true;
152         } else {
153             return new PEAR_Error( "Couldn't modify user '$user', because the user doesn't exists!", 3, PEAR_ERROR_RETURN) ;
154         }
155     } // end func modUser()
156
157     /**
158     * Deletes a user
159     *
160     * @param $user user id
161     *
162     * @return mixed returns PEAR_Error, if the user doesn't exists
163     * @access public    
164     */
165     
166     function delUser($user) {
167         if (isset($this->users[$user]) && $this->locked) {
168             unset($this->users[$user]);
169             unset($this->cvs[$user]);
170         } else {
171             return new PEAR_Error( "Couldn't delete user '$user', because the user doesn't exists!", 3, PEAR_ERROR_RETURN) ; 
172         }
173     } // end func delUser()
174
175     /**
176     * Verifies a user's password
177     *
178     * @param $user user id
179     * @param $pass password for user
180     *
181     * @return boolean true if password is ok
182     * @access public            
183     */
184     function verifyPassword($user, $pass) {
185         //if ($this->users[$user] == crypt($pass, substr($this->users[$user], 0, 2)))
186         //  return true;
187         if (isset($this->users[$user])) {
188             $stored_password = $this->users[$user];
189             if (function_exists('crypt')) {
190                 if (crypt($pass, $stored_password) == $stored_password)
191                     return true; // matches encrypted password
192                 else
193                     return false;
194             } else {
195                 if ($pass == $stored_password)
196                     return true; // matches plaintext password
197                 else {
198                     trigger_error(_("The crypt function is not available in this version of PHP."),
199                                   E_USER_WARNING);
200                     return false;
201                 }
202             }
203         }
204         return false;
205     }
206
207     /**
208     * Return all users from passwd file
209     *
210     * @access public
211     * @return array
212     */
213     function listUsers() {
214         return $this->users;
215     } // end func listUsers()
216
217     /**
218     * Writes changes to passwd file and unlocks it
219     *
220     * @access public
221     */
222     function close() {
223         if($this->locked) {
224             foreach($this->users as $user => $pass) {
225                 if($this->cvs[$user]) {
226                     fputs($this->fplock, "$user:$pass:" . $this->cvs[$user] . "\n");
227                 } else {
228                     fputs($this->fplock, "$user:$pass\n");
229                 }
230             }
231             @unlink($this->filename.'.bak');
232             if (isWindows()) {
233               // windows doesn't allow renaming of open files
234               flock($this->fplock, LOCK_UN);
235               $this->locked = false;
236               fclose($this->fplock);
237               rename($this->filename,$this->filename.'.bak');
238               rename($this->lockfile, $this->filename);
239             } else {
240               rename($this->filename,$this->filename.'.bak');
241               rename($this->lockfile, $this->filename);
242               flock($this->fplock, LOCK_UN);
243               $this->locked = false;
244               fclose($this->fplock);
245             }
246             if (file_exists($this->filename))
247                 @unlink($this->filename.'.bak');
248             else {
249                 rename($this->filename.'.bak',$this->filename);
250             }
251         }
252     } // end func close()
253 }
254
255 // Local Variables:
256 // mode: php
257 // tab-width: 8
258 // c-basic-offset: 4
259 // c-hanging-comment-ender-p: nil
260 // indent-tabs-mode: nil
261 // End:
262 ?>