|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
function ldGetCurrentTemplate( $function ) { |
|
4
|
|
|
if ( isset($function) ) { |
|
5
|
|
|
return $function; |
|
6
|
|
|
} else { |
|
7
|
|
|
$me = ar_ariadneContext::getObject(); |
|
8
|
|
|
if ($me) { |
|
9
|
|
|
$context = $me->getContext(); |
|
10
|
|
|
return $context['arCallFunction']; |
|
11
|
|
|
} |
|
12
|
|
|
} |
|
13
|
|
|
return null; |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
function ldSetCredentials($login, $ARUserDir="/system/users/") { |
|
|
|
|
|
|
17
|
|
|
global $ARCurrent, $AR; |
|
18
|
|
|
if (!$ARUserDir || $ARUserDir == "") { |
|
19
|
|
|
$ARUserDir = "/system/users/"; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
// Make sure the login is lower case. Because of the |
|
23
|
|
|
// numerous checks on "admin". |
|
24
|
|
|
$login = strtolower( $login ); |
|
25
|
|
|
|
|
26
|
|
|
debug("ldSetCredentials($login)","object"); |
|
27
|
|
|
|
|
28
|
|
|
if (!$ARCurrent->session) { |
|
29
|
|
|
ldStartSession(); |
|
30
|
|
|
} else { |
|
31
|
|
|
/* use the same sessionid if the user didn't login before */ |
|
32
|
|
|
ldStartSession($ARCurrent->session->id); |
|
33
|
|
|
} |
|
34
|
|
|
$ARCurrent->session->put("ARLogin", $login); |
|
35
|
|
|
$ARCurrent->session->put("ARUserDir", $ARUserDir, true); |
|
36
|
|
|
|
|
37
|
|
|
/* create the session key */ |
|
38
|
|
|
$session_key = bin2hex(random_bytes(16)); |
|
39
|
|
|
|
|
40
|
|
|
$ARCurrent->session->put("ARSessionKey", $session_key, true); |
|
41
|
|
|
$ARCurrent->session->put("ARSessionTimedout", 0, 1); |
|
42
|
|
|
|
|
43
|
|
|
/* now save our session */ |
|
44
|
|
|
$ARCurrent->session->save(); |
|
45
|
|
|
|
|
46
|
|
|
$cookies = (array)$_COOKIE["ARSessionCookie"]; |
|
47
|
|
|
$https = ($_SERVER['HTTPS']=='on'); |
|
48
|
|
|
|
|
49
|
|
|
$currentCookies = array(); |
|
50
|
|
|
|
|
51
|
|
|
foreach($cookies as $sessionid => $cookie){ |
|
52
|
|
|
if(!$AR->hideSessionIDfromURL){ |
|
53
|
|
|
if (!$ARCurrent->session->sessionstore->exists("/$sessionid/")) { |
|
54
|
|
|
$data = ldDecodeCookie($cookie); |
|
55
|
|
|
if(is_array($data)) { |
|
56
|
|
|
// don't just kill it, it may be from another ariadne installation |
|
57
|
|
|
if ($data['timestamp']<(time()-86400)) { |
|
58
|
|
|
// but do kill it if it's older than one day |
|
59
|
|
|
unset($cookies[$sessionid]); |
|
60
|
|
|
setcookie("ARSessionCookie[".$sessionid."]",false); |
|
61
|
|
|
} else { |
|
62
|
|
|
$currentCookies[$sessionid] = $data['timestamp']; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// Keep a maximum of 15 session cookies for one client |
|
70
|
|
|
if (sizeof($currentCookies) > 15) { |
|
71
|
|
|
sort($currentCookies); |
|
72
|
|
|
$removed = array_slice(array_keys($input), 15); // grab the session ids for all the older sessions |
|
|
|
|
|
|
73
|
|
|
foreach ($removed as $sessionid) { |
|
74
|
|
|
// and kill those sessions |
|
75
|
|
|
unset($cookies[$sessionid]); |
|
76
|
|
|
setcookie("ARSessionCookie[".$sessionid."]",false); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if( $ARCurrent->session->id !== 0) { |
|
81
|
|
|
$data = array(); |
|
82
|
|
|
|
|
83
|
|
|
$data['login']=$login; |
|
84
|
|
|
$data['timestamp']=time(); |
|
85
|
|
|
$data['check']=ldGenerateSessionKeyCheck(); |
|
86
|
|
|
|
|
87
|
|
|
$cookie=ldEncodeCookie($data); |
|
88
|
|
|
$cookiename = "ARSessionCookie[".$ARCurrent->session->id."]"; |
|
89
|
|
|
|
|
90
|
|
|
header('P3P: CP="NOI CUR OUR"'); |
|
91
|
|
|
setcookie('ARCurrentSession', $ARCurrent->session->id, 0, '/', false, $https, true); |
|
92
|
|
|
setcookie($cookiename,$cookie, 0, '/', false, $https, true); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
View Code Duplication |
function ldAccessTimeout($path, $message, $args = null, $function = null) { |
|
|
|
|
|
|
97
|
|
|
global $ARCurrent, $store; |
|
98
|
|
|
/* |
|
99
|
|
|
since there is no 'peek' function, we need to pop and push |
|
100
|
|
|
the arCallArgs variable. |
|
101
|
|
|
*/ |
|
102
|
|
|
|
|
103
|
|
|
if( isset( $args ) ) { |
|
104
|
|
|
$arCallArgs = $args; |
|
105
|
|
|
} else { |
|
106
|
|
|
$arCallArgs = @array_pop($ARCurrent->arCallStack); |
|
107
|
|
|
@array_push($ARCurrent->arCallStack, $arCallArgs); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$eventData = new baseObject(); |
|
111
|
|
|
$eventData->arCallPath = $path; |
|
|
|
|
|
|
112
|
|
|
$eventData->arCallFunction = ldGetCurrentTemplate( $function ); |
|
|
|
|
|
|
113
|
|
|
$eventData->arCallArgs = $arCallArgs; |
|
|
|
|
|
|
114
|
|
|
$eventData->arLoginMessage = $message; |
|
|
|
|
|
|
115
|
|
|
$eventData->arReason = 'access timeout'; |
|
|
|
|
|
|
116
|
|
|
$eventData = ar_events::fire( 'onaccessdenied', $eventData ); |
|
117
|
|
|
if ( $eventData ) { |
|
118
|
|
|
|
|
119
|
|
|
$arCallArgs = $eventData->arCallArgs; |
|
120
|
|
|
$arCallArgs["arLoginMessage"] = $eventData->message; |
|
121
|
|
|
|
|
122
|
|
|
if (!$ARCurrent->arLoginSilent) { |
|
123
|
|
|
$ARCurrent->arLoginSilent = true; |
|
124
|
|
|
$store->call("user.session.timeout.html", |
|
125
|
|
|
$arCallArgs, |
|
126
|
|
|
$store->get($path) ); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
View Code Duplication |
function ldAccessDenied($path, $message, $args = null, $function = null) { |
|
|
|
|
|
|
133
|
|
|
global $ARCurrent, $store; |
|
134
|
|
|
/* |
|
135
|
|
|
since there is no 'peek' function, we need to pop and push |
|
136
|
|
|
the arCallArgs variable. |
|
137
|
|
|
*/ |
|
138
|
|
|
|
|
139
|
|
|
if( isset( $args ) ) { |
|
140
|
|
|
$arCallArgs = $args; |
|
141
|
|
|
} else { |
|
142
|
|
|
$arCallArgs = @array_pop($ARCurrent->arCallStack); |
|
143
|
|
|
@array_push($ARCurrent->arCallStack, $arCallArgs); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$eventData = new baseObject(); |
|
147
|
|
|
$eventData->arCallPath = $path; |
|
|
|
|
|
|
148
|
|
|
$eventData->arCallFunction = ldGetCurrentTemplate( $function ); |
|
|
|
|
|
|
149
|
|
|
$eventData->arCallArgs = $arCallArgs; |
|
|
|
|
|
|
150
|
|
|
$eventData->arLoginMessage = $message; |
|
|
|
|
|
|
151
|
|
|
$eventData->arReason = 'access denied'; |
|
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
$eventData = ar_events::fire( 'onaccessdenied', $eventData ); |
|
154
|
|
|
|
|
155
|
|
|
if ( $eventData ) { |
|
156
|
|
|
|
|
157
|
|
|
$arCallArgs = $eventData->arCallArgs; |
|
158
|
|
|
$arCallArgs["arLoginMessage"] = $eventData->message; |
|
159
|
|
|
|
|
160
|
|
|
if (!$ARCurrent->arLoginSilent) { |
|
161
|
|
|
$ARCurrent->arLoginSilent = true; |
|
162
|
|
|
$store->call("user.login.html", |
|
163
|
|
|
$arCallArgs, |
|
164
|
|
|
$store->get($path) ); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
View Code Duplication |
function ldAccessPasswordExpired($path, $message, $args=null, $function = null) { |
|
|
|
|
|
|
170
|
|
|
global $ARCurrent, $store; |
|
171
|
|
|
/* |
|
172
|
|
|
since there is no 'peek' function, we need to pop and push |
|
173
|
|
|
the arCallArgs variable. |
|
174
|
|
|
*/ |
|
175
|
|
|
|
|
176
|
|
|
if( isset( $args ) ) { |
|
177
|
|
|
$arCallArgs = $args; |
|
178
|
|
|
} else { |
|
179
|
|
|
$arCallArgs = @array_pop($ARCurrent->arCallStack); |
|
180
|
|
|
@array_push($ARCurrent->arCallStack, $arCallArgs); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
$eventData = new baseObject(); |
|
184
|
|
|
$eventData->arCallPath = $path; |
|
|
|
|
|
|
185
|
|
|
$eventData->arCallFunction = ldGetCurrentTemplate( $function ); |
|
|
|
|
|
|
186
|
|
|
$eventData->arLoginMessage = $message; |
|
|
|
|
|
|
187
|
|
|
$eventData->arReason = 'password expired'; |
|
|
|
|
|
|
188
|
|
|
$eventData->arCallArgs = $arCallArgs; |
|
|
|
|
|
|
189
|
|
|
$eventData = ar_events::fire( 'onaccessdenied', $eventData ); |
|
190
|
|
|
if ( $eventData ) { |
|
191
|
|
|
|
|
192
|
|
|
$arCallArgs = $eventData->arCallArgs; |
|
193
|
|
|
$arCallArgs["arLoginMessage"] = $eventData->arLoginMessage; |
|
194
|
|
|
|
|
195
|
|
|
if (!$ARCurrent->arLoginSilent) { |
|
196
|
|
|
$ARCurrent->arLoginSilent = true; |
|
197
|
|
|
$store->call("user.password.expired.html", |
|
198
|
|
|
$arCallArgs, |
|
199
|
|
|
$store->get($path) ); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
View Code Duplication |
function ldGenerateSessionKeyCheck() { |
|
|
|
|
|
|
206
|
|
|
global $ARCurrent; |
|
207
|
|
|
$session_key = $ARCurrent->session->get('ARSessionKey', true); |
|
208
|
|
|
$ARUserDir = $ARCurrent->session->get('ARUserDir', true); |
|
209
|
|
|
$login = $ARCurrent->session->get('ARLogin'); |
|
210
|
|
|
return "{".md5($login.$ARUserDir.$session_key)."}"; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
function ldGetCredentials() { |
|
|
|
|
|
|
214
|
|
|
debug("ldGetCredentials()","object"); |
|
215
|
|
|
$ARSessionCookie = $_COOKIE["ARSessionCookie"]; |
|
216
|
|
|
return $ARSessionCookie; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
View Code Duplication |
function ldCheckCredentials($login) { |
|
|
|
|
|
|
220
|
|
|
global $ARCurrent, $AR; |
|
221
|
|
|
debug("ldCheckCredentials($login)","object"); |
|
222
|
|
|
$result=false; |
|
223
|
|
|
$cookie=ldGetCredentials(); |
|
224
|
|
|
$data = ldDecodeCookie($cookie[$ARCurrent->session->id]); |
|
225
|
|
|
if ($login === $data['login'] |
|
226
|
|
|
&& ($saved=$data['check'])) { |
|
227
|
|
|
$check=ldGenerateSessionKeyCheck(); |
|
228
|
|
|
if ($check === $saved && !$ARCurrent->session->get('ARSessionTimedout', 1)) { |
|
229
|
|
|
$result=true; |
|
230
|
|
|
} else { |
|
231
|
|
|
debug("login check failed","all"); |
|
232
|
|
|
} |
|
233
|
|
|
} else { |
|
234
|
|
|
$ARSessionKeyCheck = $_GET['ARSessionKeyCheck']; |
|
235
|
|
|
if (!$ARSessionKeyCheck) { |
|
236
|
|
|
$ARSessionKeyCheck = $_POST['ARSessionKeyCheck']; |
|
237
|
|
|
} |
|
238
|
|
|
if ($ARSessionKeyCheck) { |
|
239
|
|
|
debug("ldCheckCredentials: trying ARSessionKeyCheck ($ARSessionKeyCheck)"); |
|
240
|
|
|
if ($ARSessionKeyCheck == ldGenerateSessionKeyCheck()) { |
|
241
|
|
|
$result = true; |
|
242
|
|
|
} |
|
243
|
|
|
} else { |
|
244
|
|
|
debug("wrong login or corrupted cookie","all"); |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
return $result; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
View Code Duplication |
function ldDecodeCookie($cookie) { |
|
|
|
|
|
|
251
|
|
|
global $AR; |
|
252
|
|
|
$data = json_decode($cookie,true); |
|
253
|
|
|
if(is_null($data)){ |
|
254
|
|
|
if(isset($AR->sessionCryptoKey) && function_exists('mcrypt_encrypt') ) { |
|
255
|
|
|
$key = base64_decode($AR->sessionCryptoKey); |
|
256
|
|
|
$crypto = new ar_crypt($key,MCRYPT_RIJNDAEL_256,1); |
|
257
|
|
|
$data = json_decode($crypto->decrypt($cookie),true); |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
return $data; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
View Code Duplication |
function ldEncodeCookie($cookie) { |
|
|
|
|
|
|
265
|
|
|
global $AR; |
|
266
|
|
|
$data = json_encode($cookie); |
|
267
|
|
|
if(isset($AR->sessionCryptoKey) && function_exists('mcrypt_encrypt') ) { |
|
268
|
|
|
$key = base64_decode($AR->sessionCryptoKey); |
|
269
|
|
|
$crypto = new ar_crypt($key,MCRYPT_RIJNDAEL_256,1); |
|
270
|
|
|
$encdata = $crypto->crypt($data); |
|
271
|
|
|
if($encdata !== false) { |
|
272
|
|
|
$data = $encdata; |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
return $data; |
|
276
|
|
|
} |
|
277
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: