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 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
{
18 var $_pattern = array();
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;
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;
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>
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']);
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;
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){
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']);
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');
113 msg('The '.AUTH_USERFILE
.' file is not writable. Please inform the Wiki-Admin',-1);
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)
125 function modifyUser($user, $changes) {
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(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
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) {
173 if (!is_array($users) ||
empty($users)) return 0;
175 if ($this->users
=== null) $this->_loadUserData();
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
);
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
);
210 $this->_constructPattern($filter);
212 foreach ($this->users
as $user => $info) {
213 $count +
= $this->_filter($user, $info);
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();
237 $this->_constructPattern($filter);
239 foreach ($this->users
as $user => $info) {
240 if ($this->_filter($user, $info)) {
244 if (($limit > 0) && ($count >= $limit)) break;
254 * Only valid pageid's (no namespaces) for usernames
256 function cleanUser($user){
258 return cleanID(str_replace(':',$conf['sepchar'],$user));
262 * Only valid pageid's (no namespaces) for groupnames
264 function cleanGroup($user){
266 return cleanID(str_replace(':',$conf['sepchar'],$group));
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
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) {
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;
310 if (!preg_match($pattern, $info[$item])) return 0;
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 :