3 * Plaintext authentication backend
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Andreas Gohr <andi@splitbrain.org>
7 * @author Chris Smith <chris@jalakai.co.uk>
10 class auth_plain extends auth_basic {
13 var $_pattern = array();
18 * Carry out sanity checks to ensure the object is
19 * able to operate. Set capabilities.
21 * @author Christopher Smith <chris@jalakai.co.uk>
23 function __construct() {
24 global $config_cascade;
26 if (!@is_readable($config_cascade['plainauth.users']['default'])){
27 $this->success = false;
29 if(@is_writable($config_cascade['plainauth.users']['default'])){
30 $this->cando['addUser'] = true;
31 $this->cando['delUser'] = true;
32 $this->cando['modLogin'] = true;
33 $this->cando['modPass'] = true;
34 $this->cando['modName'] = true;
35 $this->cando['modMail'] = true;
36 $this->cando['modGroups'] = true;
38 $this->cando['getUsers'] = true;
39 $this->cando['getUserCount'] = true;
44 * Check user+password [required auth function]
46 * Checks if the given user exists and the given
47 * plaintext password is correct
49 * @author Andreas Gohr <andi@splitbrain.org>
52 function checkPass($user,$pass){
54 $userinfo = $this->getUserData($user);
55 if ($userinfo === false) return false;
57 return auth_verifyPassword($pass,$this->users[$user]['pass']);
63 * Returns info about the given user needs to contain
64 * at least these fields:
66 * name string full name of the user
67 * mail string email addres of the user
68 * grps array list of groups the user is in
70 * @author Andreas Gohr <andi@splitbrain.org>
72 function getUserData($user){
74 if($this->users === null) $this->_loadUserData();
75 return isset($this->users[$user]) ? $this->users[$user] : false;
81 * Returns false if the user already exists, null when an error
82 * occurred and true if everything went well.
84 * The new user will be added to the default group by this
85 * function if grps are not specified (default behaviour).
87 * @author Andreas Gohr <andi@splitbrain.org>
88 * @author Chris Smith <chris@jalakai.co.uk>
90 function createUser($user,$pwd,$name,$mail,$grps=null){
92 global $config_cascade;
94 // user mustn't already exist
95 if ($this->getUserData($user) !== false) return false;
97 $pass = auth_cryptPassword($pwd);
99 // set default group if no groups specified
100 if (!is_array($grps)) $grps = array($conf['defaultgroup']);
103 $groups = join(',',$grps);
104 $userline = join(':',array($user,$pass,$name,$mail,$groups))."\n";
106 if (io_saveFile($config_cascade['plainauth.users']['default'],$userline,true)) {
107 $this->users[$user] = compact('pass','name','mail','grps');
111 msg('The '.$config_cascade['plainauth.users']['default'].
112 ' file is not writable. Please inform the Wiki-Admin',-1);
119 * @author Chris Smith <chris@jalakai.co.uk>
120 * @param $user nick of the user to be changed
121 * @param $changes array of field/value pairs to be changed (password will be clear text)
124 function modifyUser($user, $changes) {
128 global $config_cascade;
130 // sanity checks, user must already exist and there must be something to change
131 if (($userinfo = $this->getUserData($user)) === false) return false;
132 if (!is_array($changes) || !count($changes)) return true;
134 // update userinfo with new data, remembering to encrypt any password
136 foreach ($changes as $field => $value) {
137 if ($field == 'user') {
141 if ($field == 'pass') $value = auth_cryptPassword($value);
142 $userinfo[$field] = $value;
145 $groups = join(',',$userinfo['grps']);
146 $userline = join(':',array($newuser, $userinfo['pass'], $userinfo['name'], $userinfo['mail'], $groups))."\n";
148 if (!$this->deleteUsers(array($user))) {
149 msg('Unable to modify user data. Please inform the Wiki-Admin',-1);
153 if (!io_saveFile($config_cascade['plainauth.users']['default'],$userline,true)) {
154 msg('There was an error modifying your user data. You should register again.',-1);
155 // FIXME, user has been deleted but not recreated, should force a logout and redirect to login page
160 $this->users[$newuser] = $userinfo;
165 * Remove one or more users from the list of registered users
167 * @author Christopher Smith <chris@jalakai.co.uk>
168 * @param array $users array of users to be deleted
169 * @return int the number of users deleted
171 function deleteUsers($users) {
172 global $config_cascade;
174 if (!is_array($users) || empty($users)) return 0;
176 if ($this->users === null) $this->_loadUserData();
179 foreach ($users as $user) {
180 if (isset($this->users[$user])) $deleted[] = preg_quote($user,'/');
183 if (empty($deleted)) return 0;
185 $pattern = '/^('.join('|',$deleted).'):/';
187 if (io_deleteFromFile($config_cascade['plainauth.users']['default'],$pattern,true)) {
188 foreach ($deleted as $user) unset($this->users[$user]);
189 return count($deleted);
192 // problem deleting, reload the user list and count the difference
193 $count = count($this->users);
194 $this->_loadUserData();
195 $count -= count($this->users);
200 * Return a count of the number of user which meet $filter criteria
202 * @author Chris Smith <chris@jalakai.co.uk>
204 function getUserCount($filter=array()) {
206 if($this->users === null) $this->_loadUserData();
208 if (!count($filter)) return count($this->users);
211 $this->_constructPattern($filter);
213 foreach ($this->users as $user => $info) {
214 $count += $this->_filter($user, $info);
221 * Bulk retrieval of user data
223 * @author Chris Smith <chris@jalakai.co.uk>
224 * @param start index of first user to be returned
225 * @param limit max number of users to be returned
226 * @param filter array of field/pattern pairs
227 * @return array of userinfo (refer getUserData for internal userinfo details)
229 function retrieveUsers($start=0,$limit=0,$filter=array()) {
231 if ($this->users === null) $this->_loadUserData();
238 $this->_constructPattern($filter);
240 foreach ($this->users as $user => $info) {
241 if ($this->_filter($user, $info)) {
245 if (($limit > 0) && ($count >= $limit)) break;
255 * Only valid pageid's (no namespaces) for usernames
257 function cleanUser($user){
259 return cleanID(str_replace(':',$conf['sepchar'],$user));
263 * Only valid pageid's (no namespaces) for groupnames
265 function cleanGroup($group){
267 return cleanID(str_replace(':',$conf['sepchar'],$group));
273 * loads the user file into a datastructure
275 * @author Andreas Gohr <andi@splitbrain.org>
277 function _loadUserData(){
278 global $config_cascade;
280 $this->users = array();
282 if(!@file_exists($config_cascade['plainauth.users']['default'])) return;
284 $lines = file($config_cascade['plainauth.users']['default']);
285 foreach($lines as $line){
286 $line = preg_replace('/#.*$/','',$line); //ignore comments
288 if(empty($line)) continue;
290 $row = explode(":",$line,5);
291 $groups = array_values(array_filter(explode(",",$row[4])));
293 $this->users[$row[0]]['pass'] = $row[1];
294 $this->users[$row[0]]['name'] = urldecode($row[2]);
295 $this->users[$row[0]]['mail'] = $row[3];
296 $this->users[$row[0]]['grps'] = $groups;
301 * return 1 if $user + $info match $filter criteria, 0 otherwise
303 * @author Chris Smith <chris@jalakai.co.uk>
305 function _filter($user, $info) {
307 foreach ($this->_pattern as $item => $pattern) {
308 if ($item == 'user') {
309 if (!preg_match($pattern, $user)) return 0;
310 } else if ($item == 'grps') {
311 if (!count(preg_grep($pattern, $info['grps']))) return 0;
313 if (!preg_match($pattern, $info[$item])) return 0;
319 function _constructPattern($filter) {
320 $this->_pattern = array();
321 foreach ($filter as $item => $pattern) {
322 // $this->_pattern[$item] = '/'.preg_quote($pattern,"/").'/i'; // don't allow regex characters
323 $this->_pattern[$item] = '/'.str_replace('/','\/',$pattern).'/i'; // allow regex characters
328 //Setup VIM: ex: et ts=2 :