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