Repository navigation
Expand file tree
/
Copy pathadmin.php
More file actions
289 lines (230 loc) 路 6.74 KB
/
Copy pathadmin.php
File metadata and controls
289 lines (230 loc) 路 6.74 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
# Eleanor CMS 漏 2025 --> https://eleanor-cms.com
namespace CMS;
/** Minimum interval between login attempts, in seconds. */
const AUTH_RETRY_INTERVAL=5;
use Eleanor\Assign,
CMS\Classes\Uri4AdminPanel,
CMS\Interfaces\AdminPanel;
use CMS\Enums\{Events, SignInAttempt};
use Eleanor\Classes\{Output, Totp};
use const Eleanor\SITEDIR;
/** Script start time, used to display service information at the bottom of the page. */
\define('CMS\STARTED',\hrtime(true));
# Reject unsupported browser request contexts
if(!\in_array($_SERVER['HTTP_SEC_FETCH_DEST'] ?? '',['','empty','document'],true))
return \http_response_code(403);
require __DIR__.'/cms/core.php';
/** Error page
* @param int $code Error code 4XX
* @param string $allow405 'Allow' header value for 405 error */
function Halt(int$code=404,string$allow405='DELETE'):never
{
if(CMS::$json)
{
$output=\json_encode([
'ok'=>false,
'code'=>$code,
],JSON);
Output::SendHeaders(Output::JSON, $code);
}
else
{
$output=(CMS::$T)('error',code:$code);
Output::SendHeaders(Output::HTML,$code);
}
if($code===405 and $allow405)
\header('Allow: '.$allow405);
die($output);
}
CMS::$A=new Authorization('a11n_adminpanel',0,require CMS.'external.php');
# Access to admin panel is allowed only for users with root or team roles
while(CMS::$A->current and !array_intersect(['root','team'],CMS::$P->roles))
CMS::$A->SignOut();
# Arguments for the Template constructor (template source)
if(!CMS::$json and CMS::$T instanceof Assign)
CMS::$T->args[0]=CMS.'admin-panel';
# Sign in & sign out mechanics work for JSON requests with empty query string only
elseif(!$_SERVER['QUERY_STRING'])
{
# Sign out
if(CMS::$delete)
{
CMS::$A->SignOut();
JSON([
'ok'=>true
]);
}
# Sign in
if(!\is_string($_POST['username'] ?? 0) or !\is_string($_POST['password'] ?? 0))
JSON([
'ok'=>false,
'error'=>'INSUFFICIENT'
]);
$R=CMS::$Db->Execute(<<<SQL
SELECT `id`, `name`, `password_hash`, `totp_secret`, `totp_digits`, `totp_used`, `recovery_codes`, TIMESTAMPDIFF(SECOND,`last_login_attempt`,NOW()) `seconds`
FROM `users`
WHERE `name`=?
SQL ,[$_POST['username']]);
if(!$user=SingleFetch($R))
JSON([
'ok'=>false,
'error'=>'NOT_FOUND'
]);
$id=(int)$user['id'];
if(CMS::$A->current==$id)
JSON([
'ok'=>true,
'error'=>'ALREADY'
]);
# Recovery access
$recovery=\is_string($_POST['totp'] ?? 0) && is_string($_POST['recovery_code'] ?? 0);
# TOTP available but not provided
if(!$recovery and $user['totp_secret'] and empty($_POST['totp']))
JSON([
'ok'=>false,
'error'=>'TOTP'
]);
CMS::$Db->Update('users',['last_login_attempt'=>fn()=>'NOW()'],'`id`='.$user['id']);
# Too often and no captcha
if($user['seconds']!==null and $user['seconds']<AUTH_RETRY_INTERVAL and !Classes\hCaptcha::Check('captcha'))
JSON([
'ok'=>false,
'error'=>'W8',
'seconds'=>AUTH_RETRY_INTERVAL,
'remain'=>AUTH_RETRY_INTERVAL-$user['seconds']
]);
# Check rights
$P=Permissions($id);
if(!\array_intersect(['root','team'],$P->roles))
JSON([
'ok'=>false,
'error'=>'ACCESS_DENIED'
]);
$totp_used=false;
if($recovery)
{
$counter=0;
# TOTP verification
if($user['totp_secret'] and $user['totp_used']!=$_POST['totp'] and Totp::Verify($user['totp_secret'],$_POST['totp'],$user['totp_digits']))
{
$counter++;
$totp_used=true;
}
# Password verification
if(\password_verify($_POST['password'],$user['password_hash']))
$counter++;
if($counter===2)
$recovery=false;
if($counter===1 and $user['recovery_codes'])
{
include CMS.'recovery-codes.php';
if(VerifyRecoveryCode($_POST['recovery_code'],$user['recovery_codes'],$id))
$counter++;
else
SignInAttempt::WrongRecoveryCode->Log($id);
}
if($counter<2)
JSON([
'ok'=>false,
'error'=>'WRONG_CREDENTIALS'
]);
}
else
{
# TOTP verification
if($user['totp_secret'])
{
if($user['totp_used']==$_POST['totp'] or !Totp::Verify($user['totp_secret'],$_POST['totp'],$user['totp_digits']))
{
SignInAttempt::WrongTOTP->Log($id);
JSON([
'ok'=>false,
'error'=>'WRONG_TOTP'
]);
}
$totp_used=true;
}
# Emergency password reset: accept and store a new password if the hash was manually cleared
$emergency=$_POST['password'] && $user['password_hash']==='';
# Password verification
if(!$emergency and !\password_verify($_POST['password'],$user['password_hash']))
{
SignInAttempt::WrongPassword->Log($id);
JSON([
'ok'=>false,
'error'=>'WRONG_PASSWORD'
]);
}
# Keep password hash up to date
if($emergency or \password_needs_rehash($user['password_hash'],\PASSWORD_DEFAULT))
CMS::$Db->Update('users',['password_hash'=>\password_hash($_POST['password'],\PASSWORD_DEFAULT)],'`id`='.$user['id']);
}
if($totp_used)
CMS::$Db->Update('users',['totp_used'=>(int)$_POST['totp']],'`id`='.$user['id']);
$way=$recovery ? 'recovery' : 'sign-in';
CMS::$A->SignIn($id,false,['way'=>$way]);
Events::UserSignedIn->Trigger([
'id'=>$id,
'way'=>$way,
'where'=>'admin-panel',
'ip'=>CMS::$ip ? $_SERVER['REMOTE_ADDR'] : null,
'ua'=>$_SERVER['HTTP_USER_AGENT'] ?? ''
]);
SignInAttempt::Ok->Log($id);
JSON([
'ok'=>true,
'id'=>$id,
'recovery'=>$recovery,
]);
}
# Localization
L10n::$code=L10N;
if(L10NS)
{
$l10n=CMS::$A->current ? GetUserData('l10n') : '';
if(\in_array($l10n,L10NS))
L10n::$code=$l10n;
elseif(L10N!==$l10n)
{
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Accept-Language - HAL is already sorted
$hal=isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? \explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']) : [];
$hal=\array_map(fn($item)=>\substr(\trim($item),0,2),$hal);
$l10n=\array_find($hal,fn($item)=>$item===L10N || \in_array($item,L10NS,true));
if($l10n)
L10n::$code=$l10n;
}
}
if(CMS::$A->current)
{
# Admin panel file can be renamed, so update its name in the system cache
$fn=\basename(__FILE__);
if($fn!==CMS::$Cache->Get('admin-panel',true))
CMS::$Cache->Put('admin-panel',$fn,0,true);
Uri4AdminPanel::$base=SITEDIR.$fn;
# Arguments for the Template constructor (default variables)
if(!CMS::$json and CMS::$T instanceof Assign)
CMS::$T->args[1]['links']['home']=Uri4AdminPanel::$base;
# Redirect to the main unit
if(!\is_string($_GET['u'] ?? 0))
Redirect(Uri4AdminPanel::$base.'?u=main');
$unit=$_GET['u'];
$CMS=new CMS;# Global object to carry shared data inside cms
if(\preg_match('#[^a-z\d\-_.]#i',$unit)==0 and \is_file($f=CMS."units/$unit.php"))
{
/** @var AdminPanel $U */
$U=require$f;
$CMS->$unit=$U;
if($U instanceof AdminPanel)
$U->AdminPanel(new Uri4AdminPanel(u:$unit));
}
if(CMS::$json)
JSON(['ok'=>false],404);
else
Halt();
}
else
{
$out=(CMS::$T)('SignIn',hcaptcha:CMS::$config['system']['captcha'] ? CMS::$config['system']['hcaptcha'] : '');
HTML($out,401);
}