1
|
|
|
<?php |
2
|
|
|
class mod_auth_default { |
3
|
|
|
|
4
|
|
|
function __construct($config=Array()) { |
5
|
|
|
$this->config = $config; |
|
|
|
|
6
|
|
|
} |
7
|
|
|
|
8
|
|
|
function authExternalUser($login, $password, $requestedPath = "/") { |
|
|
|
|
9
|
|
|
return false; |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
function loadConfig($requestedPath = "/") { |
13
|
|
|
global $ARConfig, $store; |
14
|
|
|
if (!$requestedPath) { |
15
|
|
|
$requestedPath = "/"; |
16
|
|
|
} |
17
|
|
|
$_cache = $ARConfig->cache; |
18
|
|
View Code Duplication |
while ( $requestedPath && $requestedPath!='/' && !$store->exists($requestedPath) ) { |
19
|
|
|
$requestedPath = $store->make_path( $requestedPath, '..' ); |
20
|
|
|
} |
21
|
|
|
$site = current($store->call("system.get.phtml", "", $store->get($requestedPath))); |
22
|
|
|
if ($site) { |
23
|
|
|
$site_config = $site->loadUserConfig(); |
24
|
|
|
$this->config['siteConfig'] = $site_config['authentication']; |
25
|
|
|
} |
26
|
|
|
$ARConfig->cache = $_cache; |
27
|
|
|
return $this->config['siteConfig']; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
function authUser($login, $password, $ARLoginPath="") { |
31
|
|
|
global $store, $AR; |
32
|
|
|
// Make sure we always have a user. |
33
|
|
|
$this->getUser('public'); |
34
|
|
|
|
35
|
|
|
$criteria = array(); |
36
|
|
|
$criteria["object"]["implements"]["="]="puser"; |
37
|
|
|
$criteria["login"]["value"]["="]=$login; |
38
|
|
|
|
39
|
|
|
$siteConfig = $this->loadConfig($ARLoginPath); |
40
|
|
|
foreach ((array)$siteConfig['userdirs'] as $userdir) { |
41
|
|
|
|
42
|
|
|
$user = current($store->call("system.authenticate.phtml", array("ARPassword" => $password), |
43
|
|
|
$store->find($userdir, $criteria, 1, 0))); |
44
|
|
|
if ($user) { |
45
|
|
|
$ARUserDir = $userdir; |
46
|
|
|
break; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (!$user) { |
|
|
|
|
51
|
|
|
$user = $this->authExternalUser($login, $password, $ARLoginPath); |
52
|
|
|
$ARUserDir = $user->parent; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ($user) { |
56
|
|
|
if ((!$user->data->config || !$user->data->config->disabled)) { |
57
|
|
|
if ($login !== "public") { |
58
|
|
|
/* welcome to Ariadne :) */ |
59
|
|
|
ldSetCredentials($login, $ARUserDir); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
$ARLogin = $user->data->login; |
|
|
|
|
62
|
|
|
$ARPassword = 0; |
|
|
|
|
63
|
|
|
$AR->user = $user; |
64
|
|
|
$result = true; |
65
|
|
|
} else { |
66
|
|
|
debug("getUser: user('$login') has been disabled", "all"); |
67
|
|
|
$result = LD_ERR_ACCESS; |
68
|
|
|
} |
69
|
|
|
} else { |
70
|
|
|
debug("authUser: user('$login') could not authenticate", "all"); |
71
|
|
|
$result = LD_ERR_ACCESS; |
72
|
|
|
} |
73
|
|
|
return $result; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
function getUser($login, $ARUserDir="/system/users/") { |
77
|
|
|
global $store, $AR; |
78
|
|
|
if (!$ARUserDir) { |
79
|
|
|
$ARUserDir = "/system/users/"; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$criteria = array(); |
83
|
|
|
$criteria["object"]["implements"]["="]="puser"; |
84
|
|
|
$criteria["login"]["value"]["="]=$login; |
85
|
|
|
|
86
|
|
|
$user = current( |
87
|
|
|
$store->call( |
88
|
|
|
"system.get.phtml", |
89
|
|
|
Array(), |
90
|
|
|
$store->find($ARUserDir, $criteria) |
91
|
|
|
) |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
if ($user) { |
95
|
|
|
if ((!$user->data->config || !$user->data->config->disabled)) { |
96
|
|
|
$AR->user = $user; |
97
|
|
|
$result = true; |
98
|
|
|
} else { |
99
|
|
|
debug("getUser: user('$login') has been disabled", "all"); |
100
|
|
|
$result = LD_ERR_ACCESS; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
} else { |
104
|
|
|
debug("getUser: user('$login') not found", "all"); |
105
|
|
|
$result = LD_ERR_ACCESS; |
106
|
|
|
} |
107
|
|
|
return $result; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
function checkLogin($login, $password, $requestedPath="/") { |
111
|
|
|
global $ARCurrent, $AR; |
112
|
|
|
debug("checkLogin($login, [password])", "all"); |
113
|
|
|
$result = null; |
114
|
|
|
if ($login) { |
115
|
|
|
debug("checkLogin: initiating new login ($login)", "all"); |
116
|
|
|
if ($ARCurrent->session) { |
117
|
|
|
$ARUserDir = $ARCurrent->session->get("ARUserDir", true); |
118
|
|
|
if (!$ARUserDir) { |
119
|
|
|
$ARUserDir = "/system/users/"; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (!$ARCurrent->session->get("ARLogin") || |
123
|
|
|
$ARCurrent->session->get("ARLogin") == "public") { |
124
|
|
|
debug("checkLogin: logging into a public session (".$ARCurrent->session->id.")", "all"); |
125
|
|
|
$result = $this->authUser($login, $password, $requestedPath); |
126
|
|
|
if ($result !== true) { |
127
|
|
|
$this->getUser('public'); |
128
|
|
|
} |
129
|
|
|
} else { |
130
|
|
|
if (ldCheckCredentials($login)) { |
|
|
|
|
131
|
|
|
debug("checkLogin: succesfully logged into private session (".$ARCurrent->session->id.")", "all"); |
132
|
|
|
$result = $this->getUser($login, $ARUserDir); |
133
|
|
|
} else { |
134
|
|
|
if ($ARCurrent->session->get("ARLogin") == $login) { |
135
|
|
|
debug("checkLogin: user ($login) tries to login to his session without a cookie set @ $ARUserDir", "all"); |
136
|
|
|
$result = $this->authUser($login, $password, $ARUserDir); |
137
|
|
|
if ($result !== true) { |
138
|
|
|
$this->getUser('public'); |
139
|
|
|
} |
140
|
|
|
} else |
141
|
|
|
if (ldCheckCredentials($ARCurrent->session->get("ARLogin"))) { |
|
|
|
|
142
|
|
|
debug("checkLogin: user tries to login as another user", "all"); |
143
|
|
|
$result = $this->authUser($login, $password, $requestedPath); |
144
|
|
|
if ($result !== true) { |
145
|
|
|
$this->getUser('public'); |
146
|
|
|
} |
147
|
|
|
} else { |
148
|
|
|
debug("checkLogin: could not login to private session (".$ARCurrent->session->id."): creating a new one", "all"); |
149
|
|
|
ldStartSession(); |
150
|
|
|
$result = $this->authUser($login, $password, $ARUserDir); |
151
|
|
|
if ($result !== true) { |
152
|
|
|
$this->getUser('public'); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
} else { |
158
|
|
|
debug("checkLogin: trying to log on", "all"); |
159
|
|
|
$result = $this->authUser($login, $password, $requestedPath); |
160
|
|
|
if ($result !== true) { |
161
|
|
|
$this->getUser('public'); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
} |
165
|
|
|
} else { |
166
|
|
|
if ($ARCurrent->session) { |
167
|
|
|
$ARUserDir = $ARCurrent->session->get("ARUserDir", true); |
168
|
|
|
if (!$ARUserDir) { |
169
|
|
|
$ARUserDir = "/system/users/"; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if (!$ARCurrent->session->get("ARLogin")) { |
173
|
|
|
if ($ARCurrent->session->get("ARSessionTimedout", 1)) { |
174
|
|
|
$ARCurrent->session->put("ARSessionTimedout", 0, 1); |
175
|
|
|
} |
176
|
|
|
debug("checkLogin: logging in with public session (".$ARCurrent->session->id.")", "all"); |
177
|
|
|
$result = $this->checkLogin("public", "none"); |
178
|
|
|
} else |
179
|
|
|
if ($ARCurrent->session->get("ARSessionTimedout", 1)) { |
180
|
|
|
debug("checkLogin: session has been timedout, forcing login", "all"); |
181
|
|
|
// become public |
182
|
|
|
$this->getUser('public'); |
183
|
|
|
$result = LD_ERR_SESSION; |
184
|
|
|
} else { |
185
|
|
|
$login = $ARCurrent->session->get("ARLogin"); |
186
|
|
|
if (ldCheckCredentials($login)) { |
|
|
|
|
187
|
|
|
debug("checkLogin: logging ($login) into a private session (".$ARCurrent->session->id.") with credentials from cookie", "all"); |
188
|
|
|
$result = $this->getUser($login, $ARUserDir); |
189
|
|
|
} else { |
190
|
|
|
debug("checkLogin: could not login ($login) on private session (".$ARCurrent->session->id.") with credentials from cookie: removing cookie", "all"); |
191
|
|
|
// FIXME: only the loader should know about cookies for sessions |
192
|
|
|
setcookie("ARSessionCookie[".$ARCurrent->session->id."]", false); |
193
|
|
|
$this->getUser('public'); |
194
|
|
|
$result = LD_ERR_ACCESS; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
} else { |
198
|
|
|
if ($AR->arSessionRespawn) { |
199
|
|
|
debug("checkLogin: trying to respawn a session", "all"); |
200
|
|
|
$cookies = ldGetCredentials(); |
201
|
|
|
if (is_array($cookies)) { |
202
|
|
|
reset($cookies); |
203
|
|
|
while (!$result && (list($sid, $sval)=each($cookies))) { |
204
|
|
|
ldStartSession($sid); |
205
|
|
|
$login = $ARCurrent->session->get("ARLogin"); |
206
|
|
|
debug("checkLogin: trying to respawn session ($sid) for user ($login)", "all"); |
207
|
|
|
if (ldCheckCredentials($login)) { |
|
|
|
|
208
|
|
|
$ARUserDir = $ARCurrent->session->get("ARUserDir", true); |
209
|
|
|
if (!$ARUserDir) { |
210
|
|
|
$ARUserDir = "/system/users/"; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
debug("checkLogin: credentials matched, loading user", "all"); |
214
|
|
|
$result = $this->getUser($login, $ARUserDir); |
215
|
|
|
} else { |
216
|
|
|
debug("checkLogin: credentials didn't match", "all"); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
if (!$result) { |
222
|
|
|
debug("checkLogin: normal public login", "all"); |
223
|
|
|
$result = $this->authUser("public", "none"); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
return $result; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: