1<?php 2// must be run within Dokuwiki 3if(!defined('DOKU_INC')) die(); 4 5/** 6 * Plaintext authentication backend 7 * 8 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 9 * @author Andreas Gohr <andi@splitbrain.org> 10 * @author Chris Smith <chris@jalakai.co.uk> 11 * @author Jan Schumann <js@schumann-it.com> 12 */ 13class auth_plugin_authplain extends DokuWiki_Auth_Plugin { 14 /** @var array user cache */ 15 protected $users = null; 16 17 /** @var array filter pattern */ 18 protected $_pattern = array(); 19 20 /** @var bool safe version of preg_split */ 21 protected $_pregsplit_safe = false; 22 23 /** 24 * Constructor 25 * 26 * Carry out sanity checks to ensure the object is 27 * able to operate. Set capabilities. 28 * 29 * @author Christopher Smith <chris@jalakai.co.uk> 30 */ 31 public function __construct() { 32 parent::__construct(); 33 global $config_cascade; 34 35 if(!@is_readable($config_cascade['plainauth.users']['default'])) { 36 $this->success = false; 37 } else { 38 if(@is_writable($config_cascade['plainauth.users']['default'])) { 39 $this->cando['addUser'] = true; 40 $this->cando['delUser'] = true; 41 $this->cando['modLogin'] = true; 42 $this->cando['modPass'] = true; 43 $this->cando['modName'] = true; 44 $this->cando['modMail'] = true; 45 $this->cando['modGroups'] = true; 46 } 47 $this->cando['getUsers'] = true; 48 $this->cando['getUserCount'] = true; 49 } 50 51 $this->_pregsplit_safe = version_compare(PCRE_VERSION,'6.7','>='); 52 } 53 54 /** 55 * Check user+password 56 * 57 * Checks if the given user exists and the given 58 * plaintext password is correct 59 * 60 * @author Andreas Gohr <andi@splitbrain.org> 61 * @param string $user 62 * @param string $pass 63 * @return bool 64 */ 65 public function checkPass($user, $pass) { 66 $userinfo = $this->getUserData($user); 67 if($userinfo === false) return false; 68 69 return auth_verifyPassword($pass, $this->users[$user]['pass']); 70 } 71 72 /** 73 * Return user info 74 * 75 * Returns info about the given user needs to contain 76 * at least these fields: 77 * 78 * name string full name of the user 79 * mail string email addres of the user 80 * grps array list of groups the user is in 81 * 82 * @author Andreas Gohr <andi@splitbrain.org> 83 * @param string $user 84 * @param bool $requireGroups (optional) ignored by this plugin, grps info always supplied 85 * @return array|false 86 */ 87 public function getUserData($user, $requireGroups=true) { 88 if($this->users === null) $this->_loadUserData(); 89 return isset($this->users[$user]) ? $this->users[$user] : false; 90 } 91 92 /** 93 * Creates a string suitable for saving as a line 94 * in the file database 95 * (delimiters escaped, etc.) 96 * 97 * @param string $user 98 * @param string $pass 99 * @param string $name 100 * @param string $mail 101 * @param array $grps list of groups the user is in 102 * @return string 103 */ 104 protected function _createUserLine($user, $pass, $name, $mail, $grps) { 105 $groups = join(',', $grps); 106 $userline = array($user, $pass, $name, $mail, $groups); 107 $userline = str_replace('\\', '\\\\', $userline); // escape \ as \\ 108 $userline = str_replace(':', '\\:', $userline); // escape : as \: 109 $userline = join(':', $userline)."\n"; 110 return $userline; 111 } 112 113 /** 114 * Create a new User 115 * 116 * Returns false if the user already exists, null when an error 117 * occurred and true if everything went well. 118 * 119 * The new user will be added to the default group by this 120 * function if grps are not specified (default behaviour). 121 * 122 * @author Andreas Gohr <andi@splitbrain.org> 123 * @author Chris Smith <chris@jalakai.co.uk> 124 * 125 * @param string $user 126 * @param string $pwd 127 * @param string $name 128 * @param string $mail 129 * @param array $grps 130 * @return bool|null|string 131 */ 132 public function createUser($user, $pwd, $name, $mail, $grps = null) { 133 global $conf; 134 global $config_cascade; 135 136 // user mustn't already exist 137 if($this->getUserData($user) !== false) return false; 138 139 $pass = auth_cryptPassword($pwd); 140 141 // set default group if no groups specified 142 if(!is_array($grps)) $grps = array($conf['defaultgroup']); 143 144 // prepare user line 145 $userline = $this->_createUserLine($user, $pass, $name, $mail, $grps); 146 147 if(io_saveFile($config_cascade['plainauth.users']['default'], $userline, true)) { 148 $this->users[$user] = compact('pass', 'name', 'mail', 'grps'); 149 return $pwd; 150 } 151 152 msg( 153 'The '.$config_cascade['plainauth.users']['default']. 154 ' file is not writable. Please inform the Wiki-Admin', -1 155 ); 156 return null; 157 } 158 159 /** 160 * Modify user data 161 * 162 * @author Chris Smith <chris@jalakai.co.uk> 163 * @param string $user nick of the user to be changed 164 * @param array $changes array of field/value pairs to be changed (password will be clear text) 165 * @return bool 166 */ 167 public function modifyUser($user, $changes) { 168 global $ACT; 169 global $config_cascade; 170 171 // sanity checks, user must already exist and there must be something to change 172 if(($userinfo = $this->getUserData($user)) === false) return false; 173 if(!is_array($changes) || !count($changes)) return true; 174 175 // update userinfo with new data, remembering to encrypt any password 176 $newuser = $user; 177 foreach($changes as $field => $value) { 178 if($field == 'user') { 179 $newuser = $value; 180 continue; 181 } 182 if($field == 'pass') $value = auth_cryptPassword($value); 183 $userinfo[$field] = $value; 184 } 185 186 $userline = $this->_createUserLine($newuser, $userinfo['pass'], $userinfo['name'], $userinfo['mail'], $userinfo['grps']); 187 188 if(!io_replaceInFile($config_cascade['plainauth.users']['default'], '/^'.$user.':/', $userline, true)) { 189 msg('There was an error modifying your user data. You may need to register again.', -1); 190 // FIXME, io functions should be fail-safe so existing data isn't lost 191 $ACT = 'register'; 192 return false; 193 } 194 195 $this->users[$newuser] = $userinfo; 196 return true; 197 } 198 199 /** 200 * Remove one or more users from the list of registered users 201 * 202 * @author Christopher Smith <chris@jalakai.co.uk> 203 * @param array $users array of users to be deleted 204 * @return int the number of users deleted 205 */ 206 public function deleteUsers($users) { 207 global $config_cascade; 208 209 if(!is_array($users) || empty($users)) return 0; 210 211 if($this->users === null) $this->_loadUserData(); 212 213 $deleted = array(); 214 foreach($users as $user) { 215 if(isset($this->users[$user])) $deleted[] = preg_quote($user, '/'); 216 } 217 218 if(empty($deleted)) return 0; 219 220 $pattern = '/^('.join('|', $deleted).'):/'; 221 io_deleteFromFile($config_cascade['plainauth.users']['default'], $pattern, true); 222 223 // reload the user list and count the difference 224 $count = count($this->users); 225 $this->_loadUserData(); 226 $count -= count($this->users); 227 return $count; 228 } 229 230 /** 231 * Return a count of the number of user which meet $filter criteria 232 * 233 * @author Chris Smith <chris@jalakai.co.uk> 234 * 235 * @param array $filter 236 * @return int 237 */ 238 public function getUserCount($filter = array()) { 239 240 if($this->users === null) $this->_loadUserData(); 241 242 if(!count($filter)) return count($this->users); 243 244 $count = 0; 245 $this->_constructPattern($filter); 246 247 foreach($this->users as $user => $info) { 248 $count += $this->_filter($user, $info); 249 } 250 251 return $count; 252 } 253 254 /** 255 * Bulk retrieval of user data 256 * 257 * @author Chris Smith <chris@jalakai.co.uk> 258 * 259 * @param int $start index of first user to be returned 260 * @param int $limit max number of users to be returned 261 * @param array $filter array of field/pattern pairs 262 * @return array userinfo (refer getUserData for internal userinfo details) 263 */ 264 public function retrieveUsers($start = 0, $limit = 0, $filter = array()) { 265 266 if($this->users === null) $this->_loadUserData(); 267 268 ksort($this->users); 269 270 $i = 0; 271 $count = 0; 272 $out = array(); 273 $this->_constructPattern($filter); 274 275 foreach($this->users as $user => $info) { 276 if($this->_filter($user, $info)) { 277 if($i >= $start) { 278 $out[$user] = $info; 279 $count++; 280 if(($limit > 0) && ($count >= $limit)) break; 281 } 282 $i++; 283 } 284 } 285 286 return $out; 287 } 288 289 /** 290 * Only valid pageid's (no namespaces) for usernames 291 * 292 * @param string $user 293 * @return string 294 */ 295 public function cleanUser($user) { 296 global $conf; 297 return cleanID(str_replace(':', $conf['sepchar'], $user)); 298 } 299 300 /** 301 * Only valid pageid's (no namespaces) for groupnames 302 * 303 * @param string $group 304 * @return string 305 */ 306 public function cleanGroup($group) { 307 global $conf; 308 return cleanID(str_replace(':', $conf['sepchar'], $group)); 309 } 310 311 /** 312 * Load all user data 313 * 314 * loads the user file into a datastructure 315 * 316 * @author Andreas Gohr <andi@splitbrain.org> 317 */ 318 protected function _loadUserData() { 319 global $config_cascade; 320 321 $this->users = array(); 322 323 if(!file_exists($config_cascade['plainauth.users']['default'])) return; 324 325 $lines = file($config_cascade['plainauth.users']['default']); 326 foreach($lines as $line) { 327 $line = preg_replace('/#.*$/', '', $line); //ignore comments 328 $line = trim($line); 329 if(empty($line)) continue; 330 331 /* NB: preg_split can be deprecated/replaced with str_getcsv once dokuwiki is min php 5.3 */ 332 $row = $this->_splitUserData($line); 333 $row = str_replace('\\:', ':', $row); 334 $row = str_replace('\\\\', '\\', $row); 335 336 $groups = array_values(array_filter(explode(",", $row[4]))); 337 338 $this->users[$row[0]]['pass'] = $row[1]; 339 $this->users[$row[0]]['name'] = urldecode($row[2]); 340 $this->users[$row[0]]['mail'] = $row[3]; 341 $this->users[$row[0]]['grps'] = $groups; 342 } 343 } 344 345 protected function _splitUserData($line){ 346 // due to a bug in PCRE 6.6, preg_split will fail with the regex we use here 347 // refer github issues 877 & 885 348 if ($this->_pregsplit_safe){ 349 return preg_split('/(?<![^\\\\]\\\\)\:/', $line, 5); // allow for : escaped as \: 350 } 351 352 $row = array(); 353 $piece = ''; 354 $len = strlen($line); 355 for($i=0; $i<$len; $i++){ 356 if ($line[$i]=='\\'){ 357 $piece .= $line[$i]; 358 $i++; 359 if ($i>=$len) break; 360 } else if ($line[$i]==':'){ 361 $row[] = $piece; 362 $piece = ''; 363 continue; 364 } 365 $piece .= $line[$i]; 366 } 367 $row[] = $piece; 368 369 return $row; 370 } 371 372 /** 373 * return true if $user + $info match $filter criteria, false otherwise 374 * 375 * @author Chris Smith <chris@jalakai.co.uk> 376 * 377 * @param string $user User login 378 * @param array $info User's userinfo array 379 * @return bool 380 */ 381 protected function _filter($user, $info) { 382 foreach($this->_pattern as $item => $pattern) { 383 if($item == 'user') { 384 if(!preg_match($pattern, $user)) return false; 385 } else if($item == 'grps') { 386 if(!count(preg_grep($pattern, $info['grps']))) return false; 387 } else { 388 if(!preg_match($pattern, $info[$item])) return false; 389 } 390 } 391 return true; 392 } 393 394 /** 395 * construct a filter pattern 396 * 397 * @param array $filter 398 */ 399 protected function _constructPattern($filter) { 400 $this->_pattern = array(); 401 foreach($filter as $item => $pattern) { 402 $this->_pattern[$item] = '/'.str_replace('/', '\/', $pattern).'/i'; // allow regex characters 403 } 404 } 405}