Skip to content

Commit 276bdf0

Browse files
committed
Tidied up variables
1 parent 71501c8 commit 276bdf0

File tree

6 files changed

+89
-76
lines changed

6 files changed

+89
-76
lines changed

plugin/serversso/action.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
<?php
2-
// must be run within Dokuwiki
3-
if(!defined('DOKU_INC')) die();
2+
use dokuwiki\Extension\ActionPlugin;
43

5-
//$active = ( $conf['authtype'] == 'authserversso' );
6-
7-
class action_plugin_authserversso extends DokuWiki_Action_Plugin {
4+
class action_plugin_authserversso extends ActionPlugin {
5+
/*
86
public function __construct() {
97
global $active;
108
11-
$authClass = get_class($auth);
12-
139
//dbglog("authserversso: Action constructor: {$authClass}");
1410
//dbglog('authserversso: Disable login');
1511
$disableactions = explode(',', $conf['disableactions']);
@@ -20,9 +16,8 @@ public function __construct() {
2016
$conf['disableactions'] = implode(',', $disableactions);
2117
$conf['autopasswd'] = 0;
2218
}
19+
*/
2320
function register(Doku_Event_Handler $controller){
24-
global $auth;
25-
2621
// dbglog('authserversso: Register hooks');
2722
//$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'clean_global_auth', NULL);
2823
$controller->register_hook('ACTION_ACT_PREPROCESS', 'AFTER', $this, 'skip_login_action', NULL);
@@ -42,7 +37,7 @@ function clean_global_auth(&$event, $param) {
4237

4338
function skip_login_action(&$event, $param) {
4439
if ($event->data == 'login') {
45-
act_redirect($ID, 'show');
40+
send_redirect($ID, 'show');
4641
}
4742
}
4843

plugin/serversso/auth.php

Lines changed: 73 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
<?php
2-
// must be run within Dokuwiki
3-
if(!defined('DOKU_INC')) die();
2+
use dokuwiki\Extension\AuthPlugin;
3+
use dokuwiki\Logger;
44

5-
define('AUTH_USERFILE',DOKU_CONF.'users.auth.php');
6-
7-
class auth_plugin_authserversso extends DokuWiki_Auth_Plugin {
5+
class auth_plugin_authserversso extends AuthPlugin {
86
const CONF_VAR_AUTH_ID = 'auth_var_id';
97
const CONF_VAR_AUTH_EMAIL = 'auth_var_email';
108
const CONF_VAR_AUTH_REALNAME = 'auth_var_realname';
9+
const CONF_AUTH_USERFILE = 'auth_userfile';
1110

1211
protected $users = null;
1312

1413
protected $_pattern = array();
1514

16-
protected $_pregsplit_safe = false;
15+
protected $_pregsplit_safe = false;
1716

1817
protected $globalConf = array();
1918

@@ -22,60 +21,55 @@ class auth_plugin_authserversso extends DokuWiki_Auth_Plugin {
2221

2322
public function __construct() {
2423
parent::__construct();
25-
26-
if(!@is_readable(AUTH_USERFILE)) {
24+
25+
if(!@is_readable(this->getConf(self::CONF_AUTH_USERFILE))) {
2726
$this->success = false;
2827
} else {
2928
$this->cando['external'] = true;
3029

31-
if(@is_writable(AUTH_USERFILE)) {
32-
//$this->cando['addUser'] = true;
33-
//$this->cando['delUser'] = true;
34-
//$this->cando['modLogin'] = true;
35-
// $this->cando['modPass'] = true;
36-
$this->cando['modMail'] = true;
37-
$this->cando['modName'] = true;
30+
if(@is_writable(this->getConf(self::CONF_AUTH_USERFILE))) {
31+
$this->cando['addUser'] = true;
32+
//$this->cando['delUser'] = false;
33+
//$this->cando['modLogin'] = false;
34+
//$this->cando['modPass'] = false;
35+
$this->cando['modMail'] = true;
36+
$this->cando['modName'] = true;
3837
$this->cando['modGroups'] = true;
3938
}
40-
$this->cando['logout'] = false;
41-
$this->cando['getUsers'] = true;
39+
$this->cando['getUsers'] = true;
4240
$this->cando['getUserCount'] = true;
41+
$this->cando['getGroups'] = true;
4342
}
4443

4544
$this->_pregsplit_safe = version_compare(PCRE_VERSION,'6.7','>=');
4645
$this->loadConfig();
47-
$this->success = true;
46+
//$this->success = true;
4847
}
4948

5049
// Required
5150
public function checkPass($user, $pass) {
52-
dbglog("authserversso: checkPass '{$user}':'{$pass}' ");
53-
//return ($user == $this->cleanUser($_SERVER['PHP_AUTH_USER']) && $pass == $_SERVER['PHP_AUTH_PW']);
51+
msg("authserversso: checkPass '{$user}':'{$pass}' ");
5452
return $this->trustExternal($user, $pass);
55-
// $userinfo = $this->getUserData($user);
56-
// if($userinfo === false) return false;
57-
58-
// return auth_verifyPassword($pass, $this->users[$user]['pass']);
5953
}
6054

6155
public function getUserData($user, $requireGroups=true) {
62-
dbglog("authserversso: getUserData {$user}");
63-
if($this->users === null) $this->_loadUserData();
64-
return isset($this->users[$user]) ? $this->users[$user] : false;
56+
Logger::debug("authserversso: getUserData {$user}");
57+
if($this->users === null) $this->loadUserData();
58+
return $this->users[$user] ?? false;
6559
}
6660

6761
protected function _createUserLine($user, $pass, $name, $mail, $grps) {
68-
$groups = join(',', $grps);
69-
$userline = array($user, $pass, $name, $mail, $groups);
62+
$groups = implode(',', $grps);
63+
$userline = [$user, $pass, $name, $mail, $groups];
7064
$userline = str_replace('\\', '\\\\', $userline); // escape \ as \\
7165
$userline = str_replace(':', '\\:', $userline); // escape : as \:
72-
$userline = join(':', $userline)."\n";
66+
$userline = str_replace('#', '\\#', $userline); // escape # as \#
67+
$userline = implode(':', $userline)."\n";
7368
return $userline;
7469
}
7570

7671
public function createUser($user, $pwd, $name, $mail, $grps = null) {
77-
global $conf;
78-
dbglog("authserversso: createUser {$user}");
72+
msg("authserversso: createUser {$user}");
7973

8074
// user mustn't already exist
8175
if($this->getUserData($user) !== false) {
@@ -91,7 +85,7 @@ public function createUser($user, $pwd, $name, $mail, $grps = null) {
9185
// prepare user line
9286
$userline = $this->_createUserLine($user, $pass, $name, $mail, $grps);
9387

94-
if(!io_saveFile(AUTH_USERFILE, $userline, true)) {
88+
if(!io_saveFile(this->getConf(self::CONF_AUTH_USERFILE), $userline, true)) {
9589
msg($this->getLang('writefail'), -1);
9690
return null;
9791
}
@@ -102,7 +96,8 @@ public function createUser($user, $pwd, $name, $mail, $grps = null) {
10296

10397
public function modifyUser($user, $changes) {
10498
global $ACT;
105-
dbglog("authserversso: modifyUser {$user}");
99+
global $conf;
100+
Logger::debug("authserversso: modifyUser {$user}");
106101

107102
// sanity checks, user must already exist and there must be something to change
108103
if(($userinfo = $this->getUserData($user)) === false) {
@@ -131,7 +126,7 @@ public function modifyUser($user, $changes) {
131126

132127
$userline = $this->_createUserLine($newuser, $userinfo['pass'], $userinfo['name'], $userinfo['mail'], $userinfo['grps']);
133128

134-
if(!io_replaceInFile(AUTH_USERFILE, '/^'.$user.':/', $userline, true)) {
129+
if(!io_replaceInFile(this->getConf(self::CONF_AUTH_USERFILE), '/^'.$user.':/', $userline, true)) {
135130
msg('There was an error modifying your user data. You may need to register again.', -1);
136131
// FIXME, io functions should be fail-safe so existing data isn't lost
137132
$ACT = 'register';
@@ -144,9 +139,9 @@ public function modifyUser($user, $changes) {
144139

145140
public function deleteUsers($users) {
146141
if(!is_array($users) || empty($users)) return 0;
147-
dbglog('authserversso: deleteUsers');
142+
Logger::debug('authserversso: deleteUsers');
148143

149-
if($this->users === null) $this->_loadUserData();
144+
if($this->users === null) $this->loadUserData();
150145

151146
$deleted = array();
152147
foreach($users as $user) {
@@ -161,21 +156,21 @@ public function deleteUsers($users) {
161156
if(empty($deleted)) return 0;
162157

163158
$pattern = '/^('.join('|', $deleted).'):/';
164-
if (!io_deleteFromFile(AUTH_USERFILE, $pattern, true)) {
159+
if (!io_deleteFromFile(this->getConf(self::CONF_AUTH_USERFILE), $pattern, true)) {
165160
msg($this->getLang('writefail'), -1);
166161
return 0;
167162
}
168163

169164
// reload the user list and count the difference
170165
$count = count($this->users);
171-
$this->_loadUserData();
166+
$this->loadUserData();
172167
$count -= count($this->users);
173168
return $count;
174169
}
175170

176171
public function getUserCount($filter = array()) {
177-
dbglog('authserversso: getUserCount');
178-
if($this->users === null) $this->_loadUserData();
172+
Logger::debug('authserversso: getUserCount');
173+
if($this->users === null) $this->loadUserData();
179174

180175
if(!count($filter)) return count($this->users);
181176

@@ -190,8 +185,8 @@ public function getUserCount($filter = array()) {
190185
}
191186

192187
public function retrieveUsers($start = 0, $limit = 0, $filter = array()) {
193-
dbglog('authserversso: retrieveUsers');
194-
if($this->users === null) $this->_loadUserData();
188+
Logger::debug('authserversso: retrieveUsers');
189+
if($this->users === null) $this->loadUserData();
195190

196191
ksort($this->users);
197192

@@ -224,24 +219,32 @@ public function cleanGroup($group) {
224219
return cleanID(str_replace(':', $conf['sepchar'], $group));
225220
}
226221

227-
protected function _loadUserData(){
228-
dbglog('authserversso: load user data');
229-
$this->users = $this->_readUserFile(AUTH_USERFILE);
230-
222+
protected function loadUserData(){
223+
Logger::debug('authserversso: load user data');
224+
$this->users = $this->readUserFile(this->getConf(self::CONF_AUTH_USERFILE));
225+
/*
226+
if (!empty($config_cascade['plainauth.users']['protected'])) {
227+
$protected = $this->readUserFile($config_cascade['serversso.users']['protected']);
228+
foreach (array_keys($protected) as $key) {
229+
$protected[$key]['protected'] = true;
230+
}
231+
$this->users = array_merge($this->users, $protected);
232+
}
233+
*/
231234
}
232235

233-
protected function _readUserFile($file) {
236+
protected function readUserFile($file) {
234237
$users = array();
235238
if(!file_exists($file)) return $users;
236239

237-
dbglog('authserversso: read user file');
240+
Logger::debug('authserversso: read user file');
238241
$lines = file($file);
239242
foreach($lines as $line) {
240243
$line = preg_replace('/#.*$/', '', $line); //ignore comments
241244
$line = trim($line);
242245
if(empty($line)) continue;
243246

244-
$row = $this->_splitUserData($line);
247+
$row = $this->spliUserData($line);
245248
$row = str_replace('\\:', ':', $row);
246249
$row = str_replace('\\\\', '\\', $row);
247250

@@ -254,13 +257,19 @@ protected function _readUserFile($file) {
254257
}
255258
return $users;
256259
}
257-
protected function _splitUserData($line){
260+
protected function spliUserData($line){
258261
// due to a bug in PCRE 6.6, preg_split will fail with the regex we use here
259262
// refer github issues 877 & 885
260-
if ($this->_pregsplit_safe){
261-
return preg_split('/(?<![^\\\\]\\\\)\:/', $line, 5); // allow for : escaped as \:
263+
//if ($this->_pregsplit_safe){
264+
$row = preg_split('/(?<![^\\\\]\\\\)\:/', $line, 5); // allow for : escaped as \:
265+
//}
266+
267+
if (count($row) < 5) {
268+
$row = array_pad($row, 5, '');
269+
Logger::error('User row with less than 5 fields', $row);
262270
}
263271

272+
/*
264273
$row = array();
265274
$piece = '';
266275
$len = strlen($line);
@@ -277,7 +286,7 @@ protected function _splitUserData($line){
277286
$piece .= $line[$i];
278287
}
279288
$row[] = $piece;
280-
289+
*/
281290
return $row;
282291
}
283292

@@ -315,13 +324,13 @@ function trustExternal($user, $pass, $sticky=false) {
315324
global $conf;
316325
global $auth;
317326

318-
dbglog('authserversso: trustExternal');
327+
Logger::debug('authserversso: trustExternal');
319328

320329
//$do = array_key_exists('do', $_REQUEST) ? $_REQUEST['do'] : null;
321330

322331
//Got a session already ?
323332
if($this->hasSession()) {
324-
dbglog('authserversso: Session found');
333+
Logger::debug('authserversso: Session found');
325334
return true;
326335
}
327336
$userSso = $this->cleanUser($this->getSSOId());
@@ -336,41 +345,41 @@ function trustExternal($user, $pass, $sticky=false) {
336345
}
337346
}
338347
if($data == false) {
339-
dbglog('authserversso: could not get user');
348+
Logger::debug('authserversso: could not get user');
340349
return false;
341350
}
342351
$this->setSession($userSso, $data['grps'], $data['mail'], $data['name']);
343-
dbglog('authserversso: authenticated user');
352+
Logger::debug('authserversso: authenticated user');
344353
return true;
345354
}
346355

347356
private function getSSOId() {
348-
return $this->getServerVar($this->conf[self::CONF_VAR_AUTH_ID]);
357+
return $this->getServerVar($this->getConf(self::CONF_VAR_AUTH_ID));
349358
}
350359

351360
private function getSSOMail() {
352-
$mail = $this->getServerVar($this->conf[self::CONF_VAR_AUTH_EMAIL]);
361+
$mail = $this->getServerVar($this->getConf(self::CONF_VAR_AUTH_EMAIL));
353362
if(!$mail || !mail_isvalid($mail)) return null;
354363
return $mail;
355364
}
356365

357366
private function getSSOName() {
358-
return $this->getServerVar($this->conf[self::CONF_VAR_AUTH_REALNAME]);
367+
return $this->getServerVar($this->getConf(self::CONF_VAR_AUTH_REALNAME));
359368
}
360369

361370
private function getServerVar($varName) {
362371
if(is_null($varName)) return null;
363372
if(!array_key_exists($varName, $_SERVER)) return null;
364373
$varVal = $_SERVER[$varName];
365-
dbglog("authserversso: getServerVar {$varName}:{$varVal}");
374+
Logger::debug("authserversso: getServerVar {$varName}:{$varVal}");
366375
return $varVal;
367376
}
368377

369378
private function hasSession() {
370379
global $USERINFO;
371-
dbglog('authserversso: check hasSession');
380+
Logger::debug('authserversso: check hasSession');
372381
if(!empty($_SESSION[DOKU_COOKIE]['auth']['info'])) {
373-
dbglog('authserversso: Session found');
382+
Logger::debug('authserversso: Session found');
374383
$USERINFO['name'] = $_SESSION[DOKU_COOKIE]['auth']['info']['name'];
375384
$USERINFO['mail'] = $_SESSION[DOKU_COOKIE]['auth']['info']['mail'];
376385
$USERINFO['grps'] = $_SESSION[DOKU_COOKIE]['auth']['info']['grps'];

plugin/serversso/conf/default.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
$conf['auth_var_id'] = 'AUTH_USER';
3+
$conf['auth_var_email'] = 'USER_EMAIL';
4+
$conf['auth_var_realname'] = 'USER_DISPLAYNAME';
5+
$conf['auth_userfile'] = './conf/auth.users.php';
6+
?>

plugin/serversso/conf/metadata.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55

66
$meta['auth_var_id'] = array('string');
77
$meta['auth_var_email'] = array('string');
8-
$meta['auth_var_realname'] = array('string');
8+
$meta['auth_var_realname'] = array('string');
9+
$meta['auth_userfile'] = array('string');

plugin/serversso/lang/en/settings.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55

66
$lang['auth_var_id'] = 'Server variable that contains the unique identifier for each user';
77
$lang['auth_var_email'] = 'Server variable that contains the users Email address';
8-
$lang['auth_var_realname'] = 'Server variable to store the users real name';
8+
$lang['auth_var_realname'] = 'Server variable to store the users real name';
9+
$lang['auth_userfile'] = 'File for storage of user data';

plugin/serversso/plugin.info.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
base authserversso
12
author Chris Marr
23
34
date 2024-04-24

0 commit comments

Comments
 (0)