more work on user and group cleaning
[dokuwiki/radio.git] / inc / auth / plain.class.php
blob3983a7d444e9782566c286836217ac2a708d280f
1 <?php
2 /**
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>
8 */
10 define('DOKU_AUTH', dirname(__FILE__));
11 require_once(DOKU_AUTH.'/basic.class.php');
13 define('AUTH_USERFILE',DOKU_CONF.'users.auth.php');
15 class auth_plain extends auth_basic {
17 var $users = null;
18 var $_pattern = array();
20 /**
21 * Constructor
23 * Carry out sanity checks to ensure the object is
24 * able to operate. Set capabilities.
26 * @author Christopher Smith <chris@jalakai.co.uk>
28 function auth_plain() {
29 if (!@is_readable(AUTH_USERFILE)){
30 $this->success = false;
31 }else{
32 if(@is_writable(AUTH_USERFILE)){
33 $this->cando['addUser'] = true;
34 $this->cando['delUser'] = true;
35 $this->cando['modLogin'] = true;
36 $this->cando['modPass'] = true;
37 $this->cando['modName'] = true;
38 $this->cando['modMail'] = true;
39 $this->cando['modGroups'] = true;
41 $this->cando['getUsers'] = true;
42 $this->cando['getUserCount'] = true;
46 /**
47 * Check user+password [required auth function]
49 * Checks if the given user exists and the given
50 * plaintext password is correct
52 * @author Andreas Gohr <andi@splitbrain.org>
53 * @return bool
55 function checkPass($user,$pass){
57 $userinfo = $this->getUserData($user);
58 if ($userinfo === false) return false;
60 return auth_verifyPassword($pass,$this->users[$user]['pass']);
63 /**
64 * Return user info
66 * Returns info about the given user needs to contain
67 * at least these fields:
69 * name string full name of the user
70 * mail string email addres of the user
71 * grps array list of groups the user is in
73 * @author Andreas Gohr <andi@splitbrain.org>
75 function getUserData($user){
77 if($this->users === null) $this->_loadUserData();
78 return isset($this->users[$user]) ? $this->users[$user] : false;
81 /**
82 * Create a new User
84 * Returns false if the user already exists, null when an error
85 * occurred and true if everything went well.
87 * The new user will be added to the default group by this
88 * function if grps are not specified (default behaviour).
90 * @author Andreas Gohr <andi@splitbrain.org>
91 * @author Chris Smith <chris@jalakai.co.uk>
93 function createUser($user,$pwd,$name,$mail,$grps=null){
94 global $conf;
96 // user mustn't already exist
97 if ($this->getUserData($user) !== false) return false;
99 $pass = auth_cryptPassword($pwd);
101 // set default group if no groups specified
102 if (!is_array($grps)) $grps = array($conf['defaultgroup']);
104 // prepare user line
105 $groups = join(',',$grps);
106 $userline = join(':',array($user,$pass,$name,$mail,$groups))."\n";
108 if (io_saveFile(AUTH_USERFILE,$userline,true)) {
109 $this->users[$user] = compact('pass','name','mail','grps');
110 return $pwd;
113 msg('The '.AUTH_USERFILE.' file is not writable. Please inform the Wiki-Admin',-1);
114 return null;
118 * Modify user data
120 * @author Chris Smith <chris@jalakai.co.uk>
121 * @param $user nick of the user to be changed
122 * @param $changes array of field/value pairs to be changed (password will be clear text)
123 * @return bool
125 function modifyUser($user, $changes) {
126 global $conf;
127 global $ACT;
128 global $INFO;
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
135 $newuser = $user;
136 foreach ($changes as $field => $value) {
137 if ($field == 'user') {
138 $newuser = $value;
139 continue;
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);
150 return false;
153 if (!io_saveFile(AUTH_USERFILE,$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
156 $ACT == 'register';
157 return false;
160 $this->users[$newuser] = $userinfo;
161 return true;
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) {
173 if (!is_array($users) || empty($users)) return 0;
175 if ($this->users === null) $this->_loadUserData();
177 $deleted = array();
178 foreach ($users as $user) {
179 if (isset($this->users[$user])) $deleted[] = preg_quote($user,'/');
182 if (empty($deleted)) return 0;
184 $pattern = '/^('.join('|',$deleted).'):/';
186 if (io_deleteFromFile(AUTH_USERFILE,$pattern,true)) {
187 foreach ($deleted as $user) unset($this->users[$user]);
188 return count($deleted);
191 // problem deleting, reload the user list and count the difference
192 $count = count($this->users);
193 $this->_loadUserData();
194 $count -= count($this->users);
195 return $count;
199 * Return a count of the number of user which meet $filter criteria
201 * @author Chris Smith <chris@jalakai.co.uk>
203 function getUserCount($filter=array()) {
205 if($this->users === null) $this->_loadUserData();
207 if (!count($filter)) return count($this->users);
209 $count = 0;
210 $this->_constructPattern($filter);
212 foreach ($this->users as $user => $info) {
213 $count += $this->_filter($user, $info);
216 return $count;
220 * Bulk retrieval of user data
222 * @author Chris Smith <chris@jalakai.co.uk>
223 * @param start index of first user to be returned
224 * @param limit max number of users to be returned
225 * @param filter array of field/pattern pairs
226 * @return array of userinfo (refer getUserData for internal userinfo details)
228 function retrieveUsers($start=0,$limit=0,$filter=array()) {
230 if ($this->users === null) $this->_loadUserData();
232 ksort($this->users);
234 $i = 0;
235 $count = 0;
236 $out = array();
237 $this->_constructPattern($filter);
239 foreach ($this->users as $user => $info) {
240 if ($this->_filter($user, $info)) {
241 if ($i >= $start) {
242 $out[$user] = $info;
243 $count++;
244 if (($limit > 0) && ($count >= $limit)) break;
246 $i++;
250 return $out;
254 * Only valid pageid's (no namespaces) for usernames
256 function cleanUser($user){
257 global $conf;
258 return cleanID(str_replace(':',$conf['sepchar'],$user));
262 * Only valid pageid's (no namespaces) for groupnames
264 function cleanGroup($user){
265 global $conf;
266 return cleanID(str_replace(':',$conf['sepchar'],$group));
270 * Load all user data
272 * loads the user file into a datastructure
274 * @author Andreas Gohr <andi@splitbrain.org>
276 function _loadUserData(){
277 $this->users = array();
279 if(!@file_exists(AUTH_USERFILE)) return;
281 $lines = file(AUTH_USERFILE);
282 foreach($lines as $line){
283 $line = preg_replace('/#.*$/','',$line); //ignore comments
284 $line = trim($line);
285 if(empty($line)) continue;
287 $row = explode(":",$line,5);
288 $groups = array_values(array_filter(explode(",",$row[4])));
290 $this->users[$row[0]]['pass'] = $row[1];
291 $this->users[$row[0]]['name'] = urldecode($row[2]);
292 $this->users[$row[0]]['mail'] = $row[3];
293 $this->users[$row[0]]['grps'] = $groups;
298 * return 1 if $user + $info match $filter criteria, 0 otherwise
300 * @author Chris Smith <chris@jalakai.co.uk>
302 function _filter($user, $info) {
303 // FIXME
304 foreach ($this->_pattern as $item => $pattern) {
305 if ($item == 'user') {
306 if (!preg_match($pattern, $user)) return 0;
307 } else if ($item == 'grps') {
308 if (!count(preg_grep($pattern, $info['grps']))) return 0;
309 } else {
310 if (!preg_match($pattern, $info[$item])) return 0;
313 return 1;
316 function _constructPattern($filter) {
317 $this->_pattern = array();
318 foreach ($filter as $item => $pattern) {
319 // $this->_pattern[$item] = '/'.preg_quote($pattern,"/").'/i'; // don't allow regex characters
320 $this->_pattern[$item] = '/'.str_replace('/','\/',$pattern).'/i'; // allow regex characters
325 //Setup VIM: ex: et ts=2 enc=utf-8 :