From 311f46037b1df1d871dfc81f872191b21e44baf5 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Fri, 9 Nov 2012 17:29:24 +0100 Subject: fixed authplain * code/PHP5 cleanup --- lib/plugins/authplain/auth.php | 215 +++++++++++++++++++++++------------------ 1 file changed, 122 insertions(+), 93 deletions(-) (limited to 'lib/plugins') diff --git a/lib/plugins/authplain/auth.php b/lib/plugins/authplain/auth.php index 2e72bbd79..3416ede89 100644 --- a/lib/plugins/authplain/auth.php +++ b/lib/plugins/authplain/auth.php @@ -11,8 +11,11 @@ if(!defined('DOKU_INC')) die(); * @author Jan Schumann */ class auth_plugin_authplain extends DokuWiki_Auth_Plugin { - var $users = null; - var $_pattern = array(); + /** @var array user cache */ + protected $users = null; + + /** @var array filter pattern */ + protected $_pattern = array(); /** * Constructor @@ -22,21 +25,21 @@ class auth_plugin_authplain extends DokuWiki_Auth_Plugin { * * @author Christopher Smith */ - function __construct() { + public function __construct() { parent::__construct(); global $config_cascade; - if (!@is_readable($config_cascade['plainauth.users']['default'])){ + if(!@is_readable($config_cascade['plainauth.users']['default'])) { $this->success = false; - }else{ - if(@is_writable($config_cascade['plainauth.users']['default'])){ - $this->cando['addUser'] = true; - $this->cando['delUser'] = true; - $this->cando['modLogin'] = true; - $this->cando['modPass'] = true; - $this->cando['modName'] = true; - $this->cando['modMail'] = true; - $this->cando['modGroups'] = true; + } else { + if(@is_writable($config_cascade['plainauth.users']['default'])) { + $this->cando['addUser'] = true; + $this->cando['delUser'] = true; + $this->cando['modLogin'] = true; + $this->cando['modPass'] = true; + $this->cando['modName'] = true; + $this->cando['modMail'] = true; + $this->cando['modGroups'] = true; } $this->cando['getUsers'] = true; $this->cando['getUserCount'] = true; @@ -44,20 +47,21 @@ class auth_plugin_authplain extends DokuWiki_Auth_Plugin { } /** - * Check user+password [required auth function] + * Check user+password * * Checks if the given user exists and the given * plaintext password is correct * * @author Andreas Gohr + * @param string $user + * @param string $pass * @return bool */ - function checkPass($user,$pass){ - + public function checkPass($user, $pass) { $userinfo = $this->getUserData($user); - if ($userinfo === false) return false; + if($userinfo === false) return false; - return auth_verifyPassword($pass,$this->users[$user]['pass']); + return auth_verifyPassword($pass, $this->users[$user]['pass']); } /** @@ -71,9 +75,10 @@ class auth_plugin_authplain extends DokuWiki_Auth_Plugin { * grps array list of groups the user is in * * @author Andreas Gohr + * @param string $user + * @return array|bool */ - function getUserData($user){ - + public function getUserData($user) { if($this->users === null) $this->_loadUserData(); return isset($this->users[$user]) ? $this->users[$user] : false; } @@ -89,30 +94,39 @@ class auth_plugin_authplain extends DokuWiki_Auth_Plugin { * * @author Andreas Gohr * @author Chris Smith + * + * @param string $user + * @param string $pwd + * @param string $name + * @param string $mail + * @param array $grps + * @return bool|null|string */ - function createUser($user,$pwd,$name,$mail,$grps=null){ + public function createUser($user, $pwd, $name, $mail, $grps = null) { global $conf; global $config_cascade; // user mustn't already exist - if ($this->getUserData($user) !== false) return false; + if($this->getUserData($user) !== false) return false; $pass = auth_cryptPassword($pwd); // set default group if no groups specified - if (!is_array($grps)) $grps = array($conf['defaultgroup']); + if(!is_array($grps)) $grps = array($conf['defaultgroup']); // prepare user line - $groups = join(',',$grps); - $userline = join(':',array($user,$pass,$name,$mail,$groups))."\n"; + $groups = join(',', $grps); + $userline = join(':', array($user, $pass, $name, $mail, $groups))."\n"; - if (io_saveFile($config_cascade['plainauth.users']['default'],$userline,true)) { - $this->users[$user] = compact('pass','name','mail','grps'); + if(io_saveFile($config_cascade['plainauth.users']['default'], $userline, true)) { + $this->users[$user] = compact('pass', 'name', 'mail', 'grps'); return $pwd; } - msg('The '.$config_cascade['plainauth.users']['default']. - ' file is not writable. Please inform the Wiki-Admin',-1); + msg( + 'The '.$config_cascade['plainauth.users']['default']. + ' file is not writable. Please inform the Wiki-Admin', -1 + ); return null; } @@ -120,43 +134,41 @@ class auth_plugin_authplain extends DokuWiki_Auth_Plugin { * Modify user data * * @author Chris Smith - * @param $user nick of the user to be changed - * @param $changes array of field/value pairs to be changed (password will be clear text) + * @param string $user nick of the user to be changed + * @param array $changes array of field/value pairs to be changed (password will be clear text) * @return bool */ - function modifyUser($user, $changes) { - global $conf; + public function modifyUser($user, $changes) { global $ACT; - global $INFO; global $config_cascade; // sanity checks, user must already exist and there must be something to change - if (($userinfo = $this->getUserData($user)) === false) return false; - if (!is_array($changes) || !count($changes)) return true; + if(($userinfo = $this->getUserData($user)) === false) return false; + if(!is_array($changes) || !count($changes)) return true; // update userinfo with new data, remembering to encrypt any password $newuser = $user; - foreach ($changes as $field => $value) { - if ($field == 'user') { + foreach($changes as $field => $value) { + if($field == 'user') { $newuser = $value; continue; } - if ($field == 'pass') $value = auth_cryptPassword($value); + if($field == 'pass') $value = auth_cryptPassword($value); $userinfo[$field] = $value; } - $groups = join(',',$userinfo['grps']); - $userline = join(':',array($newuser, $userinfo['pass'], $userinfo['name'], $userinfo['mail'], $groups))."\n"; + $groups = join(',', $userinfo['grps']); + $userline = join(':', array($newuser, $userinfo['pass'], $userinfo['name'], $userinfo['mail'], $groups))."\n"; - if (!$this->deleteUsers(array($user))) { - msg('Unable to modify user data. Please inform the Wiki-Admin',-1); + if(!$this->deleteUsers(array($user))) { + msg('Unable to modify user data. Please inform the Wiki-Admin', -1); return false; } - if (!io_saveFile($config_cascade['plainauth.users']['default'],$userline,true)) { - msg('There was an error modifying your user data. You should register again.',-1); + if(!io_saveFile($config_cascade['plainauth.users']['default'], $userline, true)) { + msg('There was an error modifying your user data. You should register again.', -1); // FIXME, user has been deleted but not recreated, should force a logout and redirect to login page - $ACT == 'register'; + $ACT = 'register'; return false; } @@ -171,24 +183,24 @@ class auth_plugin_authplain extends DokuWiki_Auth_Plugin { * @param array $users array of users to be deleted * @return int the number of users deleted */ - function deleteUsers($users) { + public function deleteUsers($users) { global $config_cascade; - if (!is_array($users) || empty($users)) return 0; + if(!is_array($users) || empty($users)) return 0; - if ($this->users === null) $this->_loadUserData(); + if($this->users === null) $this->_loadUserData(); $deleted = array(); - foreach ($users as $user) { - if (isset($this->users[$user])) $deleted[] = preg_quote($user,'/'); + foreach($users as $user) { + if(isset($this->users[$user])) $deleted[] = preg_quote($user, '/'); } - if (empty($deleted)) return 0; + if(empty($deleted)) return 0; - $pattern = '/^('.join('|',$deleted).'):/'; + $pattern = '/^('.join('|', $deleted).'):/'; - if (io_deleteFromFile($config_cascade['plainauth.users']['default'],$pattern,true)) { - foreach ($deleted as $user) unset($this->users[$user]); + if(io_deleteFromFile($config_cascade['plainauth.users']['default'], $pattern, true)) { + foreach($deleted as $user) unset($this->users[$user]); return count($deleted); } @@ -203,17 +215,20 @@ class auth_plugin_authplain extends DokuWiki_Auth_Plugin { * Return a count of the number of user which meet $filter criteria * * @author Chris Smith + * + * @param array $filter + * @return int */ - function getUserCount($filter=array()) { + public function getUserCount($filter = array()) { if($this->users === null) $this->_loadUserData(); - if (!count($filter)) return count($this->users); + if(!count($filter)) return count($this->users); $count = 0; $this->_constructPattern($filter); - foreach ($this->users as $user => $info) { + foreach($this->users as $user => $info) { $count += $this->_filter($user, $info); } @@ -224,28 +239,29 @@ class auth_plugin_authplain extends DokuWiki_Auth_Plugin { * Bulk retrieval of user data * * @author Chris Smith - * @param start index of first user to be returned - * @param limit max number of users to be returned - * @param filter array of field/pattern pairs - * @return array of userinfo (refer getUserData for internal userinfo details) + * + * @param int $start index of first user to be returned + * @param int $limit max number of users to be returned + * @param array $filter array of field/pattern pairs + * @return array userinfo (refer getUserData for internal userinfo details) */ - function retrieveUsers($start=0,$limit=0,$filter=array()) { + public function retrieveUsers($start = 0, $limit = 0, $filter = array()) { - if ($this->users === null) $this->_loadUserData(); + if($this->users === null) $this->_loadUserData(); ksort($this->users); - $i = 0; + $i = 0; $count = 0; - $out = array(); + $out = array(); $this->_constructPattern($filter); - foreach ($this->users as $user => $info) { - if ($this->_filter($user, $info)) { - if ($i >= $start) { + foreach($this->users as $user => $info) { + if($this->_filter($user, $info)) { + if($i >= $start) { $out[$user] = $info; $count++; - if (($limit > 0) && ($count >= $limit)) break; + if(($limit > 0) && ($count >= $limit)) break; } $i++; } @@ -256,18 +272,24 @@ class auth_plugin_authplain extends DokuWiki_Auth_Plugin { /** * Only valid pageid's (no namespaces) for usernames + * + * @param string $user + * @return string */ - function cleanUser($user){ + public function cleanUser($user) { global $conf; - return cleanID(str_replace(':',$conf['sepchar'],$user)); + return cleanID(str_replace(':', $conf['sepchar'], $user)); } /** * Only valid pageid's (no namespaces) for groupnames + * + * @param string $group + * @return string */ - function cleanGroup($group){ + public function cleanGroup($group) { global $conf; - return cleanID(str_replace(':',$conf['sepchar'],$group)); + return cleanID(str_replace(':', $conf['sepchar'], $group)); } /** @@ -277,7 +299,7 @@ class auth_plugin_authplain extends DokuWiki_Auth_Plugin { * * @author Andreas Gohr */ - function _loadUserData(){ + protected function _loadUserData() { global $config_cascade; $this->users = array(); @@ -285,13 +307,13 @@ class auth_plugin_authplain extends DokuWiki_Auth_Plugin { if(!@file_exists($config_cascade['plainauth.users']['default'])) return; $lines = file($config_cascade['plainauth.users']['default']); - foreach($lines as $line){ - $line = preg_replace('/#.*$/','',$line); //ignore comments + foreach($lines as $line) { + $line = preg_replace('/#.*$/', '', $line); //ignore comments $line = trim($line); if(empty($line)) continue; - $row = explode(":",$line,5); - $groups = array_values(array_filter(explode(",",$row[4]))); + $row = explode(":", $line, 5); + $groups = array_values(array_filter(explode(",", $row[4]))); $this->users[$row[0]]['pass'] = $row[1]; $this->users[$row[0]]['name'] = urldecode($row[2]); @@ -301,29 +323,36 @@ class auth_plugin_authplain extends DokuWiki_Auth_Plugin { } /** - * return 1 if $user + $info match $filter criteria, 0 otherwise + * return true if $user + $info match $filter criteria, false otherwise * * @author Chris Smith + * + * @param string $user User login + * @param array $info User's userinfo array + * @return bool */ - function _filter($user, $info) { - // FIXME - foreach ($this->_pattern as $item => $pattern) { - if ($item == 'user') { - if (!preg_match($pattern, $user)) return 0; - } else if ($item == 'grps') { - if (!count(preg_grep($pattern, $info['grps']))) return 0; + protected function _filter($user, $info) { + foreach($this->_pattern as $item => $pattern) { + if($item == 'user') { + if(!preg_match($pattern, $user)) return false; + } else if($item == 'grps') { + if(!count(preg_grep($pattern, $info['grps']))) return false; } else { - if (!preg_match($pattern, $info[$item])) return 0; + if(!preg_match($pattern, $info[$item])) return false; } } - return 1; + return true; } - function _constructPattern($filter) { + /** + * construct a filter pattern + * + * @param array $filter + */ + protected function _constructPattern($filter) { $this->_pattern = array(); - foreach ($filter as $item => $pattern) { - // $this->_pattern[$item] = '/'.preg_quote($pattern,"/").'/i'; // don't allow regex characters - $this->_pattern[$item] = '/'.str_replace('/','\/',$pattern).'/i'; // allow regex characters + foreach($filter as $item => $pattern) { + $this->_pattern[$item] = '/'.str_replace('/', '\/', $pattern).'/i'; // allow regex characters } } } \ No newline at end of file -- cgit v1.2.3