2 /* vim: set ts=4 sw=4: */
3 // +----------------------------------------------------------------------+
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 // +----------------------------------------------------------------------+
19 // $Id: File_Passwd.php,v 1.3 2004-03-11 13:30:47 rurban Exp $
21 // Manipulate standard UNIX passwd,.htpasswd and CVS pserver passwd files
23 require_once 'lib/pear/PEAR.php' ;
26 * Class to manage passwd-style files
28 * @author Rasmus Lerdorf <rasmus@php.net>
45 * hash list of csv-users
51 * filehandle for lockfile
63 * name of the lockfile
66 var $lockfile = './passwd.lock';
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!
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
85 function File_Passwd($file, $lock = 0, $lockfile = "") {
86 $this->filename = $file;
87 if( !empty( $lockfile) ) {
88 $this->lockfile = $lockfile ;
92 $this->fplock = fopen($this->lockfile, 'w');
93 flock($this->fplock, LOCK_EX);
97 $fp = fopen($file,'r') ;
99 return new PEAR_Error( "Couldn't open '$file'!", 1, PEAR_ERROR_RETURN) ;
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]);
113 } // end func File_Passwd()
118 * @param $user new user id
119 * @param $pass password for new user
120 * @param $cvs cvs user id (needed for pserver passwd files)
122 * @return mixed returns PEAR_Error, if the user already exists
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;
131 return new PEAR_Error( "Couldn't add user '$user', because the user already exists!", 2, PEAR_ERROR_RETURN);
133 } // end func addUser()
138 * @param $user user id
139 * @param $pass new password for user
140 * @param $cvs cvs user id (needed for pserver passwd files)
142 * @return mixed returns PEAR_Error, if the user doesn't exists
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;
152 return new PEAR_Error( "Couldn't modify user '$user', because the user doesn't exists!", 3, PEAR_ERROR_RETURN) ;
154 } // end func modUser()
159 * @param $user user id
161 * @return mixed returns PEAR_Error, if the user doesn't exists
165 function delUser($user) {
166 if(isset($this->users[$user]) && $this->locked) {
167 unset($this->users[$user]);
168 unset($this->cvs[$user]);
170 return new PEAR_Error( "Couldn't delete user '$user', because the user doesn't exists!", 3, PEAR_ERROR_RETURN) ;
172 } // end func delUser()
175 * Verifies a user's password
177 * @param $user user id
178 * @param $pass password for user
180 * @return boolean true if password is ok
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;
188 } // end func verifyPassword()
191 * Return all users from passwd file
196 function listUsers() {
198 } // end func listUsers()
201 * Writes changes to passwd file and unlocks it
207 foreach($this->users as $user => $pass) {
208 if($this->cvs[$user]) {
209 fputs($this->fplock, "$user:$pass:" . $this->cvs[$user] . "\n");
211 fputs($this->fplock, "$user:$pass\n");
214 rename($this->lockfile, $this->filename);
215 flock($this->fplock, LOCK_UN);
216 $this->locked = false;
217 fclose($this->fplock);
219 } // end func close()