forked from cosmocode/dokuwiki-plugin-authjoomla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.php
182 lines (148 loc) · 5.34 KB
/
auth.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
* DokuWiki Plugin authjoomlasso (Auth Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Andreas Gohr <[email protected]>
* @author Stephan Thamm <[email protected]>
*/
/**
* Class auth_plugin_authjoomla
*
*/
class auth_plugin_authjoomlasso extends auth_plugin_authpdo
{
/** @inheritdoc */
public function __construct()
{
$this->initializeConfiguration();
parent::__construct(); // PDO setup
$this->cando['external'] = true;
$this->cando['logout'] = false;
}
public function trustExternal($user, $pass, $sticky = true) {
if ($user) {
msg("This auth plugin does not support manual login");
}
return $this->loginWithCookie($this->getConf('backendcookie')) || $this->loginWithCookie($this->getConf('frontendcookie'));
}
protected function timedOut($now, $start) {
global $conf;
$timeout = $conf['auth_security_timeout'];
if (empty($timeout)) return false;
if ($timeout <= 0) return true;
return $now > $start + $timeout;
}
protected function loginWithCookie($cookie_name) {
global $USERINFO;
if (empty($cookie_name)) return false;
$session = $_COOKIE[$cookie_name];
if (empty($session)) return false;
$sql = $this->getConf('select-session');
$result = $this->_query($sql, ['session' => $session]);
if ($result === false) return false;
$user = $result[0]['user'];
if (empty($user)) return false;
$now = time();
if ($_SESSION[DOKU_COOKIE]['auth']['user'] == $user && !$this->timedOut($now, $_SESSION[DOKU_COOKIE]['auth']['time'])) {
$USERINFO = $_SESSION[DOKU_COOKIE]['auth']['info'];
$_SERVER['REMOTE_USER'] = $user;
return true;
} else {
$data = $this->getUserData($user, true);
if ($data == false) return false;
$USERINFO['name'] = $data['name'];
$USERINFO['mail'] = $data['mail'];
$USERINFO['grps'] = $data['grps'];
$_SERVER['REMOTE_USER'] = $user;
$_SESSION[DOKU_COOKIE]['auth']['user'] = $user;
$_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO;
$_SESSION[DOKU_COOKIE]['auth']['time'] = $now;
return true;
}
}
/**
* Initialize database configuration
*/
protected function initializeConfiguration()
{
$prefix = $this->getConf('tableprefix');
/** @noinspection SqlNoDataSourceInspection */
/** @noinspection SqlResolve */
$this->conf['select-user'] = '
SELECT `id` AS `uid`,
`username` AS `user`,
`name` AS `name`,
`password` AS `hash`,
`email` AS `mail`
FROM `' . $prefix . 'users`
WHERE `username` = :user
AND `block` = 0
AND `activation` = 0
';
/** @noinspection SqlNoDataSourceInspection */
/** @noinspection SqlResolve */
$this->conf['select-user-groups'] = '
SELECT
p.id AS `gid`,
(
SELECT GROUP_CONCAT(xp.`title` ORDER BY xp.`lft` SEPARATOR \'/\')
FROM `' . $prefix . 'usergroups` AS xp
WHERE p.`lft` BETWEEN xp.`lft` AND xp.`rgt`
) AS `group`
FROM `' . $prefix . 'user_usergroup_map` AS m,
`' . $prefix . 'usergroups` AS g,
`' . $prefix . 'usergroups` AS p
WHERE m.`user_id` = :uid
AND g.`id` = m.`group_id`
AND p.`lft` <= g.`lft`
AND p.`rgt` >= g.`rgt`
';
/** @noinspection SqlNoDataSourceInspection */
/** @noinspection SqlResolve */
$this->conf['select-groups'] = '
SELECT n.id AS `gid`, GROUP_CONCAT(p.`title` ORDER BY p.lft SEPARATOR \'/\') as `group`
FROM `' . $prefix . 'usergroups` AS n, `' . $prefix . 'usergroups` AS p
WHERE n.lft BETWEEN p.lft AND p.rgt
GROUP BY n.id
ORDER BY n.id
';
/** @noinspection SqlNoDataSourceInspection */
/** @noinspection SqlResolve */
$this->conf['select-session'] = '
SELECT s.`username` as `user`
FROM `' . $prefix . 'session` AS s,
`' . $prefix . 'users` AS u
WHERE s.`session_id` = :session
AND s.`userid` = u.`id`
AND `block` = 0
AND `activation` = 0
';
}
/**
* Sets up the language strings
*
* Needed to inherit from the parent class. It's abit ugly but currently no better way exists.
*/
public function setupLocale()
{
if ($this->localised) return;
global $conf;
// load authpdo language files
/** @var array $lang is loaded by include */
$path = DOKU_PLUGIN . 'authpdo/lang/';
@include($path . 'en/lang.php');
if ($conf['lang'] != 'en') @include($path . $conf['lang'] . '/lang.php');
$pdolang = $lang;
// load our authloomla language files and config overrides
parent::setupLocale();
// merge them both
$this->lang = array_merge($this->lang, $pdolang);
}
/** @inheritdoc */
public function isCaseSensitive()
{
return false;
}
}
// vim:ts=4:sw=4:et: