From ab9790ca81397622fcfd58faf56a69b68bd36257 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Tue, 29 Aug 2023 20:30:59 +0200 Subject: [PATCH] Apply rector fixes to auth plugins --- _test/rector.php | 3 ++ lib/plugins/authad/auth.php | 74 ++++++++++++++------------- lib/plugins/authldap/auth.php | 71 +++++++++++++------------- lib/plugins/authpdo/auth.php | 112 +++++++++++++---------------------------- lib/plugins/authplain/auth.php | 49 ++++++++++-------- 5 files changed, 137 insertions(+), 172 deletions(-) diff --git a/_test/rector.php b/_test/rector.php index 414fb5307..b6131a73e 100644 --- a/_test/rector.php +++ b/_test/rector.php @@ -67,6 +67,8 @@ return static function (RectorConfig $rectorConfig): void { __DIR__ . '/../lib/tpl/*/_test/*', __DIR__ . '/../lib/plugins/*/lang/*', __DIR__ . '/../lib/tpl/*/lang/*', + __DIR__ . '/../lib/plugins/*/conf/*', + __DIR__ . '/../lib/tpl/*/conf/*', __DIR__ . '/../lib/plugins/*/vendor/*', __DIR__ . '/../lib/tpl/*/vendor/*', __DIR__ . '/../lib/plugins/*/skel/*', // dev plugin @@ -106,5 +108,6 @@ return static function (RectorConfig $rectorConfig): void { RemoveExtraParametersRector::class, // this actually broke code RemoveUnusedNonEmptyArrayBeforeForeachRector::class, // seems unreliable when checking on array keys RemoveAlwaysTrueIfConditionRector::class, // fails with if(defined(...)) constructs + RemoveUnreachableStatementRector::class, // fails GOTO in authpdo -> should be rewritten with exceptions ]); }; diff --git a/lib/plugins/authad/auth.php b/lib/plugins/authad/auth.php index 0c3690f0a..e471ab1bd 100644 --- a/lib/plugins/authad/auth.php +++ b/lib/plugins/authad/auth.php @@ -1,4 +1,6 @@ conf['additional'])) { $this->conf['additional'] = str_replace(' ', '', $this->conf['additional']); $this->conf['additional'] = explode(',', $this->conf['additional']); - } else $this->conf['additional'] = array(); + } else $this->conf['additional'] = []; // ldap extension is needed if (!function_exists('ldap_connect')) { @@ -102,7 +104,7 @@ class auth_plugin_authad extends DokuWiki_Auth_Plugin if ($this->getConf('sso_charset')) { $INPUT->server->set('REMOTE_USER', iconv($this->getConf('sso_charset'), 'UTF-8', $INPUT->server->str('REMOTE_USER'))); - } elseif (!\dokuwiki\Utf8\Clean::isUtf8($INPUT->server->str('REMOTE_USER'))) { + } elseif (!Clean::isUtf8($INPUT->server->str('REMOTE_USER'))) { $INPUT->server->set('REMOTE_USER', utf8_encode($INPUT->server->str('REMOTE_USER'))); } @@ -201,11 +203,11 @@ class auth_plugin_authad extends DokuWiki_Auth_Plugin global $ID; global $INPUT; $adldap = $this->initAdLdap($this->getUserDomain($user)); - if (!$adldap) return array(); + if (!$adldap) return []; - if ($user == '') return array(); + if ($user == '') return []; - $fields = array('mail', 'displayname', 'samaccountname', 'lastpwd', 'pwdlastset', 'useraccountcontrol'); + $fields = ['mail', 'displayname', 'samaccountname', 'lastpwd', 'pwdlastset', 'useraccountcontrol']; // add additional fields to read $fields = array_merge($fields, $this->conf['additional']); @@ -215,17 +217,17 @@ class auth_plugin_authad extends DokuWiki_Auth_Plugin //get info for given user $result = $adldap->user()->info($this->getUserName($user), $fields); if ($result == false) { - return array(); + return []; } //general user info - $info = array(); + $info = []; $info['name'] = $result[0]['displayname'][0]; $info['mail'] = $result[0]['mail'][0]; $info['uid'] = $result[0]['samaccountname'][0]; $info['dn'] = $result[0]['dn']; //last password set (Windows counts from January 1st 1601) - $info['lastpwd'] = $result[0]['pwdlastset'][0] / 10000000 - 11644473600; + $info['lastpwd'] = $result[0]['pwdlastset'][0] / 10_000_000 - 11_644_473_600; //will it expire? $info['expires'] = !($result[0]['useraccountcontrol'][0] & 0x10000); //ADS_UF_DONT_EXPIRE_PASSWD @@ -271,7 +273,7 @@ class auth_plugin_authad extends DokuWiki_Auth_Plugin ) { $msg = sprintf($this->getLang('authpwdexpire'), $info['expiresin']); if ($this->canDo('modPass')) { - $url = wl($ID, array('do'=> 'profile')); + $url = wl($ID, ['do'=> 'profile']); $msg .= ' '.$lang['btn_profile'].''; } msg($msg); @@ -300,7 +302,7 @@ class auth_plugin_authad extends DokuWiki_Auth_Plugin $group = str_replace('\\', '', $group); $group = str_replace('#', '', $group); $group = preg_replace('[\s]', '_', $group); - $group = \dokuwiki\Utf8\PhpString::strtolower(trim($group)); + $group = PhpString::strtolower(trim($group)); return $group; } @@ -318,15 +320,15 @@ class auth_plugin_authad extends DokuWiki_Auth_Plugin $domain = ''; // get NTLM or Kerberos domain part - list($dom, $user) = sexplode('\\', $user, 2, ''); + [$dom, $user] = sexplode('\\', $user, 2, ''); if (!$user) $user = $dom; if ($dom) $domain = $dom; - list($user, $dom) = sexplode('@', $user, 2, ''); + [$user, $dom] = sexplode('@', $user, 2, ''); if ($dom) $domain = $dom; // clean up both - $domain = \dokuwiki\Utf8\PhpString::strtolower(trim($domain)); - $user = \dokuwiki\Utf8\PhpString::strtolower(trim($user)); + $domain = PhpString::strtolower(trim($domain)); + $user = PhpString::strtolower(trim($user)); // is this a known, valid domain or do we work without account suffix? if not discard if ((!isset($this->conf[$domain]) || !is_array($this->conf[$domain])) && @@ -385,14 +387,14 @@ class auth_plugin_authad extends DokuWiki_Auth_Plugin * @param array $filter $filter array of field/pattern pairs, empty array for no filter * @return int number of users */ - public function getUserCount($filter = array()) + public function getUserCount($filter = []) { $adldap = $this->initAdLdap(null); if (!$adldap) { Logger::debug("authad/auth.php getUserCount(): _adldap not set."); return -1; } - if ($filter == array()) { + if ($filter == []) { $result = $adldap->user()->all(); } else { $searchString = $this->constructSearchString($filter); @@ -499,21 +501,21 @@ class auth_plugin_authad extends DokuWiki_Auth_Plugin * @param array $filter array of field/pattern pairs, null for no filter * @return array userinfo (refer getUserData for internal userinfo details) */ - public function retrieveUsers($start = 0, $limit = 0, $filter = array()) + public function retrieveUsers($start = 0, $limit = 0, $filter = []) { $adldap = $this->initAdLdap(null); - if (!$adldap) return array(); + if (!$adldap) return []; //if (!$this->users) { //get info for given user $result = $adldap->user()->all(false, $this->constructSearchString($filter)); - if (!$result) return array(); + if (!$result) return []; $this->users = array_fill_keys($result, false); //} $i = 0; $count = 0; - $result = array(); + $result = []; if (!isset($filter['grps'])) { /** @var admin_plugin_usermanager $usermanager */ @@ -546,7 +548,7 @@ class auth_plugin_authad extends DokuWiki_Auth_Plugin $start+$limit - count($this->grpsusers[$this->filterToString($filter)]) +1 ); } - if (!$this->grpsusers[$this->filterToString($filter)]) return array(); + if (!$this->grpsusers[$this->filterToString($filter)]) return []; foreach ($this->grpsusers[$this->filterToString($filter)] as $user => &$info) { if ($i++ < $start) { continue; @@ -586,20 +588,20 @@ class auth_plugin_authad extends DokuWiki_Auth_Plugin } // changing user data - $adchanges = array(); + $adchanges = []; if (isset($changes['name'])) { // get first and last name $parts = explode(' ', $changes['name']); $adchanges['surname'] = array_pop($parts); - $adchanges['firstname'] = join(' ', $parts); + $adchanges['firstname'] = implode(' ', $parts); $adchanges['display_name'] = $changes['name']; } if (isset($changes['mail'])) { $adchanges['email'] = $changes['mail']; } - if (count($adchanges)) { + if ($adchanges !== []) { try { - $return = $return & $adldap->user()->modify($this->getUserName($user), $adchanges); + $return &= $adldap->user()->modify($this->getUserName($user), $adchanges); } catch (adLDAPException $e) { if ($this->conf['debug']) msg('AD Auth: '.$e->getMessage(), -1); $return = false; @@ -651,7 +653,7 @@ class auth_plugin_authad extends DokuWiki_Auth_Plugin */ public function getUserDomain($user) { - list(, $domain) = sexplode('@', $user, 2, ''); + [, $domain] = sexplode('@', $user, 2, ''); return $domain; } @@ -666,7 +668,7 @@ class auth_plugin_authad extends DokuWiki_Auth_Plugin public function getUserName($user) { if ($this->conf['account_suffix'] !== '') { - list($user) = explode('@', $user, 2); + [$user] = explode('@', $user, 2); } return $user; } @@ -733,7 +735,7 @@ class auth_plugin_authad extends DokuWiki_Auth_Plugin */ public function getConfiguredDomains() { - $domains = array(); + $domains = []; if (empty($this->conf['account_suffix'])) return $domains; // not configured yet // add default domain, using the name from account suffix @@ -768,8 +770,8 @@ class auth_plugin_authad extends DokuWiki_Auth_Plugin if (!preg_match($pattern, $user)) return false; } elseif ($item == 'grps') { if (!count(preg_grep($pattern, $info['grps']))) return false; - } else { - if (!preg_match($pattern, $info[$item])) return false; + } elseif (!preg_match($pattern, $info[$item])) { + return false; } } return true; @@ -784,7 +786,7 @@ class auth_plugin_authad extends DokuWiki_Auth_Plugin */ protected function constructPattern($filter) { - $this->pattern = array(); + $this->pattern = []; foreach ($filter as $item => $pattern) { $this->pattern[$item] = '/'.str_replace('/', '\/', $pattern).'/i'; // allow regex characters } diff --git a/lib/plugins/authldap/auth.php b/lib/plugins/authldap/auth.php index 71b2584cb..8649d38b2 100644 --- a/lib/plugins/authldap/auth.php +++ b/lib/plugins/authldap/auth.php @@ -1,4 +1,5 @@ makeFilter( $this->getConf('binddn'), - array('user' => $user, 'server' => $this->getConf('server')) + ['user' => $user, 'server' => $this->getConf('server')] ); } elseif (strpos($this->getConf('usertree'), '%{user}')) { // direct user bind $dn = $this->makeFilter( $this->getConf('usertree'), - array('user' => $user, 'server' => $this->getConf('server')) + ['user' => $user, 'server' => $this->getConf('server')] ); - } else { + } elseif (!@ldap_bind($this->con)) { // Anonymous bind - if (!@ldap_bind($this->con)) { - msg("LDAP: can not bind anonymously", -1); - $this->debug('LDAP anonymous bind: ' . hsc(ldap_error($this->con)), 0, __LINE__, __FILE__); - return false; - } + msg("LDAP: can not bind anonymously", -1); + $this->debug('LDAP anonymous bind: ' . hsc(ldap_error($this->con)), 0, __LINE__, __FILE__); + return false; } // Try to bind to with the dn if we have one. @@ -162,28 +161,28 @@ class auth_plugin_authldap extends DokuWiki_Auth_Plugin protected function fetchUserData($user, $inbind = false) { global $conf; - if (!$this->openLDAP()) return array(); + if (!$this->openLDAP()) return []; // force superuser bind if wanted and not bound as superuser yet if ($this->getConf('binddn') && $this->getConf('bindpw') && $this->bound < 2) { // use superuser credentials if (!@ldap_bind($this->con, $this->getConf('binddn'), conf_decodeString($this->getConf('bindpw')))) { $this->debug('LDAP bind as superuser: ' . hsc(ldap_error($this->con)), 0, __LINE__, __FILE__); - return array(); + return []; } $this->bound = 2; } elseif ($this->bound == 0 && !$inbind) { // in some cases getUserData is called outside the authentication workflow // eg. for sending email notification on subscribed pages. This data might not // be accessible anonymously, so we try to rebind the current user here - list($loginuser, $loginsticky, $loginpass) = auth_getCookie(); + [$loginuser, $loginsticky, $loginpass] = auth_getCookie(); if ($loginuser && $loginpass) { $loginpass = auth_decrypt($loginpass, auth_cookiesalt(!$loginsticky, true)); $this->checkPass($loginuser, $loginpass); } } - $info = array(); + $info = []; $info['user'] = $user; $this->debug('LDAP user to find: ' . hsc($info['user']), 0, __LINE__, __FILE__); @@ -214,7 +213,7 @@ class auth_plugin_authldap extends DokuWiki_Auth_Plugin if (!is_array($result)) { // no objects found $this->debug('LDAP search returned non-array result: ' . hsc(print($result)), -1, __LINE__, __FILE__); - return array(); + return []; } // Don't accept more or less than one response @@ -228,7 +227,7 @@ class auth_plugin_authldap extends DokuWiki_Auth_Plugin //for($i = 0; $i < $result["count"]; $i++) { //$this->_debug('result: '.hsc(print_r($result[$i])), 0, __LINE__, __FILE__); //} - return array(); + return []; } $this->debug('LDAP search found single result !', 0, __LINE__, __FILE__); @@ -241,7 +240,7 @@ class auth_plugin_authldap extends DokuWiki_Auth_Plugin $info['gid'] = $user_result['gidnumber'][0] ?? null; $info['mail'] = $user_result['mail'][0]; $info['name'] = $user_result['cn'][0]; - $info['grps'] = array(); + $info['grps'] = []; // overwrite if other attribs are specified. if (is_array($this->getConf('mapping'))) { @@ -276,14 +275,14 @@ class auth_plugin_authldap extends DokuWiki_Auth_Plugin $base, $filter, $this->getConf('groupscope'), - array($this->getConf('groupkey')) + [$this->getConf('groupkey')] ); $this->debug('LDAP group search: ' . hsc(ldap_error($this->con)), 0, __LINE__, __FILE__); $this->debug('LDAP search at: ' . hsc($base . ' ' . $filter), 0, __LINE__, __FILE__); if (!$sr) { msg("LDAP: Reading group memberships failed", -1); - return array(); + return []; } $result = ldap_get_entries($this->con, $sr); ldap_free_result($sr); @@ -305,7 +304,7 @@ class auth_plugin_authldap extends DokuWiki_Auth_Plugin } // always add the default group to the list of groups - if (!$info['grps'] or !in_array($conf['defaultgroup'], $info['grps'])) { + if (!$info['grps'] || !in_array($conf['defaultgroup'], $info['grps'])) { $info['grps'][] = $conf['defaultgroup']; } return $info; @@ -336,7 +335,7 @@ class auth_plugin_authldap extends DokuWiki_Auth_Plugin $dn = $info['dn']; // find the old password of the user - list($loginuser, $loginsticky, $loginpass) = auth_getCookie(); + [$loginuser, $loginsticky, $loginpass] = auth_getCookie(); if ($loginuser !== null) { // the user is currently logged in $secret = auth_cookiesalt(!$loginsticky, true); $pass = auth_decrypt($loginpass, $secret); @@ -363,11 +362,11 @@ class auth_plugin_authldap extends DokuWiki_Auth_Plugin } // Generate the salted hashed password for LDAP - $phash = new \dokuwiki\PassHash(); + $phash = new PassHash(); $hash = $phash->hash_ssha($changes['pass']); // change the password - if (!@ldap_mod_replace($this->con, $dn, array('userpassword' => $hash))) { + if (!@ldap_mod_replace($this->con, $dn, ['userpassword' => $hash])) { $this->debug( 'LDAP mod replace failed: ' . hsc($dn) . ': ' . hsc(ldap_error($this->con)), 0, @@ -399,9 +398,9 @@ class auth_plugin_authldap extends DokuWiki_Auth_Plugin * @return array of userinfo (refer getUserData for internal userinfo details) * @author Dominik Eckelmann */ - public function retrieveUsers($start = 0, $limit = 0, $filter = array()) + public function retrieveUsers($start = 0, $limit = 0, $filter = []) { - if (!$this->openLDAP()) return array(); + if (!$this->openLDAP()) return []; if (is_null($this->users)) { // Perform the search and grab all their details @@ -412,20 +411,20 @@ class auth_plugin_authldap extends DokuWiki_Auth_Plugin } $sr = ldap_search($this->con, $this->getConf('usertree'), $all_filter); $entries = ldap_get_entries($this->con, $sr); - $users_array = array(); + $users_array = []; $userkey = $this->getConf('userkey'); for ($i = 0; $i < $entries["count"]; $i++) { - array_push($users_array, $entries[$i][$userkey][0]); + $users_array[] = $entries[$i][$userkey][0]; } Sort::asort($users_array); $result = $users_array; - if (!$result) return array(); + if (!$result) return []; $this->users = array_fill_keys($result, false); } $i = 0; $count = 0; $this->constructPattern($filter); - $result = array(); + $result = []; foreach ($this->users as $user => &$info) { if ($i++ < $start) { @@ -486,8 +485,8 @@ class auth_plugin_authldap extends DokuWiki_Auth_Plugin if (!preg_match($pattern, $user)) return false; } elseif ($item == 'grps') { if (!count(preg_grep($pattern, $info['grps']))) return false; - } else { - if (!preg_match($pattern, $info[$item])) return false; + } elseif (!preg_match($pattern, $info[$item])) { + return false; } } return true; @@ -503,7 +502,7 @@ class auth_plugin_authldap extends DokuWiki_Auth_Plugin */ protected function constructPattern($filter) { - $this->pattern = array(); + $this->pattern = []; foreach ($filter as $item => $pattern) { $this->pattern[$item] = '/' . str_replace('/', '\/', $pattern) . '/i'; // allow regex characters } @@ -523,9 +522,7 @@ class auth_plugin_authldap extends DokuWiki_Auth_Plugin // see https://github.com/adldap/adLDAP/issues/22 return preg_replace_callback( '/([\x00-\x1F\*\(\)\\\\])/', - function ($matches) { - return "\\" . join("", unpack("H2", $matches[1])); - }, + static fn($matches) => "\\" . implode("", unpack("H2", $matches[1])), $string ); } @@ -653,7 +650,7 @@ class auth_plugin_authldap extends DokuWiki_Auth_Plugin $sizelimit = 0 ) { - if (is_null($attributes)) $attributes = array(); + if (is_null($attributes)) $attributes = []; if ($scope == 'base') { return @ldap_read( diff --git a/lib/plugins/authpdo/auth.php b/lib/plugins/authpdo/auth.php index f58ad0247..4e194ab34 100644 --- a/lib/plugins/authpdo/auth.php +++ b/lib/plugins/authpdo/auth.php @@ -1,4 +1,5 @@ getConf('dsn'), $this->getConf('user'), conf_decodeString($this->getConf('pass')), - array( + [ PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, // always fetch as array PDO::ATTR_EMULATE_PREPARES => true, // emulating prepares allows us to reuse param names PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // we want exceptions, not error codes - ) + ] ); } catch (PDOException $e) { $this->debugMsg($e); @@ -59,94 +60,52 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin // can Users be created? $this->cando['addUser'] = $this->checkConfig( - array( - 'select-user', - 'select-user-groups', - 'select-groups', - 'insert-user', - 'insert-group', - 'join-group' - ) + ['select-user', 'select-user-groups', 'select-groups', 'insert-user', 'insert-group', 'join-group'] ); // can Users be deleted? $this->cando['delUser'] = $this->checkConfig( - array( - 'select-user', - 'select-user-groups', - 'select-groups', - 'leave-group', - 'delete-user' - ) + ['select-user', 'select-user-groups', 'select-groups', 'leave-group', 'delete-user'] ); // can login names be changed? $this->cando['modLogin'] = $this->checkConfig( - array( - 'select-user', - 'select-user-groups', - 'update-user-login' - ) + ['select-user', 'select-user-groups', 'update-user-login'] ); // can passwords be changed? $this->cando['modPass'] = $this->checkConfig( - array( - 'select-user', - 'select-user-groups', - 'update-user-pass' - ) + ['select-user', 'select-user-groups', 'update-user-pass'] ); // can real names be changed? $this->cando['modName'] = $this->checkConfig( - array( - 'select-user', - 'select-user-groups', - 'update-user-info:name' - ) + ['select-user', 'select-user-groups', 'update-user-info:name'] ); // can real email be changed? $this->cando['modMail'] = $this->checkConfig( - array( - 'select-user', - 'select-user-groups', - 'update-user-info:mail' - ) + ['select-user', 'select-user-groups', 'update-user-info:mail'] ); // can groups be changed? $this->cando['modGroups'] = $this->checkConfig( - array( - 'select-user', - 'select-user-groups', - 'select-groups', - 'leave-group', - 'join-group', - 'insert-group' - ) + ['select-user', 'select-user-groups', 'select-groups', 'leave-group', 'join-group', 'insert-group'] ); // can a filtered list of users be retrieved? $this->cando['getUsers'] = $this->checkConfig( - array( - 'list-users' - ) + ['list-users'] ); // can the number of users be retrieved? $this->cando['getUserCount'] = $this->checkConfig( - array( - 'count-users' - ) + ['count-users'] ); // can a list of available groups be retrieved? $this->cando['getGroups'] = $this->checkConfig( - array( - 'select-groups' - ) + ['select-groups'] ); $this->success = true; @@ -166,7 +125,7 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin if ($userdata == false) return false; // password checking done in SQL? - if ($this->checkConfig(array('check-pass'))) { + if ($this->checkConfig(['check-pass'])) { $userdata['clear'] = $pass; $userdata['hash'] = auth_cryptPassword($pass); $result = $this->query($this->getConf('check-pass'), $userdata); @@ -177,7 +136,7 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin // we do password checking on our own if (isset($userdata['hash'])) { // hashed password - $passhash = new \dokuwiki\PassHash(); + $passhash = new PassHash(); return $passhash->verify_hash($pass, $userdata['hash']); } else { // clear text password in the database O_o @@ -243,11 +202,11 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin } // prepare data - if ($grps == null) $grps = array(); + if ($grps == null) $grps = []; array_unshift($grps, $conf['defaultgroup']); $grps = array_unique($grps); $hash = auth_cryptPassword($clear); - $userdata = compact('user', 'clear', 'hash', 'name', 'mail'); + $userdata = ['user' => $user, 'clear' => $clear, 'hash' => $hash, 'name' => $name, 'mail' => $mail]; // action protected by transaction $this->pdo->beginTransaction(); @@ -406,10 +365,10 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin public function retrieveUsers($start = 0, $limit = -1, $filter = null) { if ($limit < 0) $limit = 10000; // we don't support no limit - if (is_null($filter)) $filter = array(); + if (is_null($filter)) $filter = []; if (isset($filter['grps'])) $filter['group'] = $filter['grps']; - foreach (array('user', 'name', 'mail', 'group') as $key) { + foreach (['user', 'name', 'mail', 'group'] as $key) { if (!isset($filter[$key])) { $filter[$key] = '%'; } else { @@ -421,13 +380,13 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin $filter['limit'] = (int)$limit; $result = $this->query($this->getConf('list-users'), $filter); - if (!$result) return array(); - $users = array(); + if (!$result) return []; + $users = []; if (is_array($result)) { foreach ($result as $row) { if (!isset($row['user'])) { $this->debugMsg("list-users statement did not return 'user' attribute", -1, __LINE__); - return array(); + return []; } $users[] = $this->getUserData($row['user']); } @@ -443,12 +402,12 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin * @param array $filter array of field/pattern pairs, empty array for no filter * @return int */ - public function getUserCount($filter = array()) + public function getUserCount($filter = []) { - if (is_null($filter)) $filter = array(); + if (is_null($filter)) $filter = []; if (isset($filter['grps'])) $filter['group'] = $filter['grps']; - foreach (array('user', 'name', 'mail', 'group') as $key) { + foreach (['user', 'name', 'mail', 'group'] as $key) { if (!isset($filter[$key])) { $filter[$key] = '%'; } else { @@ -473,7 +432,7 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin { $sql = $this->getConf('insert-group'); - $result = $this->query($sql, array(':group' => $group)); + $result = $this->query($sql, [':group' => $group]); $this->clearGroupCache(); if ($result === false) return false; return true; @@ -491,7 +450,7 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin public function retrieveGroups($start = 0, $limit = 0) { $groups = array_keys($this->selectGroups()); - if ($groups === false) return array(); + if ($groups === false) return []; if (!$limit) { return array_splice($groups, $start); @@ -510,7 +469,7 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin { $sql = $this->getConf('select-user'); - $result = $this->query($sql, array(':user' => $user)); + $result = $this->query($sql, [':user' => $user]); if (!$result) return false; if (count($result) > 1) { @@ -525,7 +484,7 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin $this->debugMsg("Statement did not return 'user' attribute", -1, __LINE__); $dataok = false; } - if (!isset($data['hash']) && !isset($data['clear']) && !$this->checkConfig(array('check-pass'))) { + if (!isset($data['hash']) && !isset($data['clear']) && !$this->checkConfig(['check-pass'])) { $this->debugMsg("Statement did not return 'clear' or 'hash' attribute", -1, __LINE__); $dataok = false; } @@ -587,7 +546,7 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin $result = $this->query($sql, $userdata); if ($result === false) return false; - $groups = array($conf['defaultgroup']); // always add default config + $groups = [$conf['defaultgroup']]; // always add default config if (is_array($result)) { foreach ($result as $row) { if (!isset($row['group'])) { @@ -618,7 +577,7 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin $result = $this->query($sql); if ($result === false) return false; - $groups = array(); + $groups = []; if (is_array($result)) { foreach ($result as $row) { if (!isset($row['group'])) { @@ -685,7 +644,7 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin * @param array $arguments Named parameters to be used in the statement * @return array|int|bool The result as associative array for SELECTs, affected rows for others, false on error */ - protected function query($sql, $arguments = array()) + protected function query($sql, $arguments = []) { $sql = trim($sql); if (empty($sql)) { @@ -694,7 +653,7 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin } // execute - $params = array(); + $params = []; $sth = $this->pdo->prepare($sql); $result = false; try { @@ -703,7 +662,7 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin if (is_array($value)) continue; if (is_object($value)) continue; if ($key[0] != ':') $key = ":$key"; // prefix with colon if needed - if (strpos($sql, $key) === false) continue; // skip if parameter is missing + if (strpos($sql, (string) $key) === false) continue; // skip if parameter is missing if (is_int($value)) { $sth->bindValue($key, $value, PDO::PARAM_INT); @@ -742,7 +701,6 @@ class auth_plugin_authpdo extends DokuWiki_Auth_Plugin $this->debugMsg("SQL:
$dsql
", -1, $line); } $sth->closeCursor(); - $sth = null; return $result; } diff --git a/lib/plugins/authplain/auth.php b/lib/plugins/authplain/auth.php index e0c1b9291..8cfe35115 100644 --- a/lib/plugins/authplain/auth.php +++ b/lib/plugins/authplain/auth.php @@ -14,10 +14,10 @@ use dokuwiki\Utf8\Sort; class auth_plugin_authplain extends DokuWiki_Auth_Plugin { /** @var array user cache */ - protected $users = null; + protected $users; /** @var array filter pattern */ - protected $pattern = array(); + protected $pattern = []; /** @var bool safe version of preg_split */ protected $pregsplit_safe = false; @@ -90,7 +90,7 @@ class auth_plugin_authplain extends DokuWiki_Auth_Plugin public function getUserData($user, $requireGroups = true) { if ($this->users === null) $this->loadUserData(); - return isset($this->users[$user]) ? $this->users[$user] : false; + return $this->users[$user] ?? false; } /** @@ -107,11 +107,11 @@ class auth_plugin_authplain extends DokuWiki_Auth_Plugin */ protected function createUserLine($user, $pass, $name, $mail, $grps) { - $groups = join(',', $grps); - $userline = array($user, $pass, $name, $mail, $groups); + $groups = implode(',', $grps); + $userline = [$user, $pass, $name, $mail, $groups]; $userline = str_replace('\\', '\\\\', $userline); // escape \ as \\ $userline = str_replace(':', '\\:', $userline); // escape : as \: - $userline = join(':', $userline)."\n"; + $userline = implode(':', $userline)."\n"; return $userline; } @@ -148,7 +148,7 @@ class auth_plugin_authplain extends DokuWiki_Auth_Plugin $pass = auth_cryptPassword($pwd); // set default group if no groups specified - if (!is_array($grps)) $grps = array($conf['defaultgroup']); + if (!is_array($grps)) $grps = [$conf['defaultgroup']]; // prepare user line $userline = $this->createUserLine($user, $pass, $name, $mail, $grps); @@ -158,7 +158,12 @@ class auth_plugin_authplain extends DokuWiki_Auth_Plugin return null; } - $this->users[$user] = compact('pass', 'name', 'mail', 'grps'); + $this->users[$user] = [ + 'pass' => $pass, + 'name' => $name, + 'mail' => $mail, + 'grps' => $grps + ]; return $pwd; } @@ -187,7 +192,7 @@ class auth_plugin_authplain extends DokuWiki_Auth_Plugin return false; } - if (!is_array($changes) || !count($changes)) return true; + if (!is_array($changes) || $changes === []) return true; // update userinfo with new data, remembering to encrypt any password $newuser = $user; @@ -231,11 +236,11 @@ class auth_plugin_authplain extends DokuWiki_Auth_Plugin { global $config_cascade; - if (!is_array($users) || empty($users)) return 0; + if (!is_array($users) || $users === []) return 0; if ($this->users === null) $this->loadUserData(); - $deleted = array(); + $deleted = []; foreach ($users as $user) { // don't delete protected users if (!empty($this->users[$user]['protected'])) { @@ -245,9 +250,9 @@ class auth_plugin_authplain extends DokuWiki_Auth_Plugin if (isset($this->users[$user])) $deleted[] = preg_quote($user, '/'); } - if (empty($deleted)) return 0; + if ($deleted === []) return 0; - $pattern = '/^('.join('|', $deleted).'):/'; + $pattern = '/^('.implode('|', $deleted).'):/'; if (!io_deleteFromFile($config_cascade['plainauth.users']['default'], $pattern, true)) { msg($this->getLang('writefail'), -1); return 0; @@ -268,12 +273,12 @@ class auth_plugin_authplain extends DokuWiki_Auth_Plugin * @param array $filter * @return int */ - public function getUserCount($filter = array()) + public function getUserCount($filter = []) { if ($this->users === null) $this->loadUserData(); - if (!count($filter)) return count($this->users); + if ($filter === []) return count($this->users); $count = 0; $this->constructPattern($filter); @@ -295,7 +300,7 @@ class auth_plugin_authplain extends DokuWiki_Auth_Plugin * @param array $filter array of field/pattern pairs * @return array userinfo (refer getUserData for internal userinfo details) */ - public function retrieveUsers($start = 0, $limit = 0, $filter = array()) + public function retrieveUsers($start = 0, $limit = 0, $filter = []) { if ($this->users === null) $this->loadUserData(); @@ -304,7 +309,7 @@ class auth_plugin_authplain extends DokuWiki_Auth_Plugin $i = 0; $count = 0; - $out = array(); + $out = []; $this->constructPattern($filter); foreach ($this->users as $user => $info) { @@ -334,7 +339,7 @@ class auth_plugin_authplain extends DokuWiki_Auth_Plugin $groups = []; if ($this->users === null) $this->loadUserData(); - foreach($this->users as $user => $info) { + foreach($this->users as $info) { $groups = array_merge($groups, array_diff($info['grps'], $groups)); } Sort::ksort($groups); @@ -404,7 +409,7 @@ class auth_plugin_authplain extends DokuWiki_Auth_Plugin */ protected function readUserFile($file) { - $users = array(); + $users = []; if (!file_exists($file)) return $users; $lines = file($file); @@ -459,8 +464,8 @@ class auth_plugin_authplain extends DokuWiki_Auth_Plugin if (!preg_match($pattern, $user)) return false; } elseif ($item == 'grps') { if (!count(preg_grep($pattern, $info['grps']))) return false; - } else { - if (!preg_match($pattern, $info[$item])) return false; + } elseif (!preg_match($pattern, $info[$item])) { + return false; } } return true; @@ -473,7 +478,7 @@ class auth_plugin_authplain extends DokuWiki_Auth_Plugin */ protected function constructPattern($filter) { - $this->pattern = array(); + $this->pattern = []; foreach ($filter as $item => $pattern) { $this->pattern[$item] = '/'.str_replace('/', '\/', $pattern).'/i'; // allow regex characters } -- 2.11.4.GIT