Skip to content
This repository was archived by the owner on Mar 30, 2024. It is now read-only.

Commit ab0a8fd

Browse files
committed
Fix #3
1 parent 5d9f2cd commit ab0a8fd

File tree

12 files changed

+143
-31
lines changed

12 files changed

+143
-31
lines changed

VERSION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
latest
2-
0.2.8
3-
0.2
2+
0.3.0
3+
0.3
44
0

php/core/Login.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ class Login {
1919

2020
private JSONReader $groupList;
2121

22-
public function __construct( string $group = '', string $client = '', string $token = '' ) {
22+
public function __construct( string $group = '', string $token = '', string $client = '' ) {
2323
$this->groupList = new JSONReader('groups');
2424
if(!empty($group) && !empty($client) && !empty($token)){
2525
$this->apiClientLogin($group, $client, $token);
2626
}
27-
28-
if( TaskTimeTerminate === 'GUI' && session_status() === PHP_SESSION_ACTIVE ){
27+
else if(!empty($group) && !empty($token)){
28+
$this->sessionLogin($group, $token);
29+
}
30+
else if( TaskTimeTerminate === 'GUI' && session_status() === PHP_SESSION_ACTIVE ){
2931
$this->userSessionLogin();
3032
}
3133
}
@@ -44,6 +46,18 @@ private function apiClientLogin(string $group, string $client, string $token) :
4446
$this->logUserOut();
4547
}
4648

49+
public function sessionLogin(string $group, string $token) : void {
50+
if( $this->groupList->isValue([$group]) ){
51+
$sid = $this->groupList->searchValue([$group, 'sessions'], $token, 'token');
52+
if( $sid !== false ){
53+
$this->logUserIn($group);
54+
$this->groupList->setValue([$group, 'sessions', $sid, 'used'], time());
55+
return;
56+
}
57+
}
58+
$this->logUserOut();
59+
}
60+
4761
private function userSessionLogin() : void {
4862
$this->loggedIn = isset($_SESSION['login']) && $_SESSION['login'] === true
4963
&& $_SESSION['login_time'] + 600 > time();
@@ -56,14 +70,27 @@ private function userSessionLogin() : void {
5670
}
5771
}
5872

59-
public function userLogin(string $group, string $password) : void {
73+
public function userLogin(string $group, string $password, bool $stayLoggedIn = false) : ?string {
6074
if( $this->groupList->isValue([$group]) ){
6175
if(self::checkHashedPassword($password, $this->groupList->getValue([$group, 'passhash']))){
6276
$this->logUserIn($group);
63-
return;
77+
78+
if( $stayLoggedIn ) {
79+
$token = Utilities::randomCode(50, Utilities::ID);
80+
$this->groupList->setValue([$group, 'sessions', null], array(
81+
"browseros" => Utilities::getBrowserOS(),
82+
"used" => 0,
83+
"token" => $token
84+
));
85+
return $token;
86+
}
87+
else{
88+
return null;
89+
}
6490
}
6591
}
6692
$this->logUserOut();
93+
return null;
6794
}
6895

6996
private function logUserIn(string $group, string $device = "") : void {

php/core/ParamParser.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public function isLoginPost() : bool {
4040
!empty($_POST['group']) && !empty($_POST['password']);
4141
}
4242

43+
public function isSessionPost() : bool {
44+
return $_SERVER['REQUEST_METHOD'] === 'POST' &&
45+
!empty($_POST['group']) && !empty($_POST['token']);
46+
}
47+
4348
public function isLogoutGet() : bool {
4449
return $_SERVER['REQUEST_METHOD'] === 'GET' &&
4550
isset($_GET['logout']);
@@ -57,6 +62,9 @@ public function loginPost(string $name) : string {
5762
}
5863
}
5964
}
65+
else if($name === 'token' ){
66+
return !empty($_POST['token']) && is_string($_POST['token']) ? preg_replace('/[^A-Za-z0-9]/', '', $_POST['token']) : '';
67+
}
6068
return '';
6169
}
6270

php/core/WebGUI.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,6 @@ public function __construct( ParamParser $param, Login $login ) {
3131
$this->param = $param;
3232

3333
$this->mainTemp = new Template('main');
34-
if($this->login->isLoggedIn()){
35-
$this->mainTemp->setContent('DISPLAYLOGOUTBOX', '');
36-
$this->mainTemp->setContent('GROUP', $this->login->getGroup());
37-
}
38-
else{
39-
$this->mainTemp->setContent('HOMELINK', '');
40-
}
41-
4234
$this->fillTemplateWithImprint($this->mainTemp);
4335
}
4436

@@ -152,14 +144,18 @@ public function accountManage() : void {
152144
}
153145
}
154146

147+
public function showLoginToken(string $token) : void {
148+
$this->mainTemp->setContent('MOREHEADER', '<script>localStorage.setItem("loginToken", "'. $token .','. $this->login->getGroup() .'");</script>');
149+
}
150+
155151
public function deviceManage() : void {
156152
$this->mainTemp->setContent('TITLE', 'Device Management');
157153
$device = new Template('device');
158154
$this->mainTemp->includeTemplate($device);
159155

160156
$r = $this->login->getGroupList();
161157
$myGroup = $this->login->getGroup();
162-
if( !empty($_POST['device']) || !empty($_GET['regenerate']) || !empty($_GET['delete']) ){
158+
if( !empty($_POST['device']) || !empty($_GET['regenerate']) || !empty($_GET['delete']) || isset($_GET['remove']) ){
163159
$device->setContent('NOTEDISABLE','');
164160
if( !empty($_POST['device']) && InputParser::checkDeviceName($_POST['device']) ){
165161
$name = $_POST['device'];
@@ -208,6 +204,13 @@ public function deviceManage() : void {
208204
$device->setContent('NOTEMSG','Device does not exist!');
209205
}
210206
}
207+
else if( isset($_GET['remove']) && preg_match('/^[0-9]+$/', $_GET['remove'] ) === 1 ){
208+
$device->setContent(
209+
'NOTEMSG',
210+
$r->isValue([$myGroup, 'sessions', $_GET['remove']]) && $r->setValue([$myGroup, 'sessions', $_GET['remove']], null) ?
211+
'Deleted session!': 'Error deleting session!'
212+
);
213+
}
211214
else{
212215
$device->setContent('NOTEMSG','Invalid format!');
213216
}
@@ -271,7 +274,8 @@ public function home() : void {
271274
if(!empty($e)){
272275
$tasks[] = array(
273276
'NAME' => $this->nameList[$k],
274-
'PARAM' => $e
277+
'PARAM' => $e,
278+
'ACTIVE' => $k === ParamParser::TASK_HOME ? 'active' : ''
275279
);
276280
}
277281
}
@@ -292,6 +296,14 @@ public function loginForm() : void {
292296
}
293297

294298
public function __destruct(){
299+
if($this->login->isLoggedIn()){
300+
$this->mainTemp->setContent('DISPLAYLOGOUTBOX', '');
301+
$this->mainTemp->setContent('GROUP', $this->login->getGroup());
302+
}
303+
else{
304+
$this->mainTemp->setContent('HOMELINK', '');
305+
}
306+
295307
$this->mainTemp->output();
296308
}
297309
}

php/core/api/API.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct() {
3535
public function request(array $post) : void {
3636
$this->validatePost($post);
3737
if( !$this->hasError ){
38-
$this->login = new Login($post['group'], $post['client'], $post['token']);
38+
$this->login = new Login($post['group'], $post['token'], $post['client']);
3939
if( $this->login->isLoggedIn()){
4040
$this->handleAPITask();
4141
}

php/core/templates/home.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"multiples" : {
33
"Links" : {
44
"%%NAME%%" : "",
5-
"%%PARAM%%" : ""
5+
"%%PARAM%%" : "",
6+
"%%ACTIVE%%" : ""
67
}
78
}
89
}

php/core/templates/home_en.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<center>
2-
<ul class="list-group">
2+
<div class="list-group">
33
<!--MULTIPLE-Links-BEGIN-->
4-
<li class="list-group-item"><a href="%%SERVERURL%%/?task=%%PARAM%%">%%NAME%%</a></li>
4+
<a href="%%SERVERURL%%/?task=%%PARAM%%" class="list-group-item list-group-item-action %%ACTIVE%%">%%NAME%%</a>
55
<!--MULTIPLE-Links-END-->
6-
</ul>
6+
</div>
77
</center>

php/core/templates/login_en.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515
</div>
1616
<div class="form-group row">
1717
<div class="col-sm-2">&nbsp;</div>
18-
<div class="col-sm-10">
18+
<div class="col-sm-2">
1919
<input type="submit" value="Login" class="btn btn-secondary">
2020
</div>
21+
<div class="col-sm-8 form-group form-check">
22+
<input type="checkbox" name="stayloggedin" id="stayloggedin" value="yes" class="form-check-input">
23+
<label class="form-check-label" for="stayloggedin">Stay logged in</label>
24+
</div>
2125
</div>
2226
<div class="alert alert-info %%COOKIEBANNER%%" role="alert">
2327
<h4 class="alert-heading">Cookies</h4>
@@ -27,5 +31,4 @@ <h4 class="alert-heading">Cookies</h4>
2731
For more information see about cookies and privacy see <a href="%%IMPRESSUMURL%%" target="_blank">%%IMPRESSUMNAME%%</a>.
2832
</div>
2933
</form>
30-
31-
34+
<script>checkForLoginCode();</script>

php/index.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,31 @@
1515

1616
$param = new ParamParser();
1717
$login = new Login();
18-
if( !$login->isLoggedIn() && $param->isLoginPost() ){
19-
$login->userLogin($param->loginPost('group'), $param->loginPost('password'));
18+
$gui = new WebGUI($param, $login);
19+
20+
if( $login->isLoggedIn() ){
21+
if($param->isLogoutGet()){
22+
$login->logUserOut();
23+
}
2024
}
21-
if($login->isLoggedIn() && $param->isLogoutGet()){
22-
$login->logUserOut();
25+
else {
26+
if( $param->isLoginPost() ){
27+
$token = $login->userLogin(
28+
$param->loginPost('group'),
29+
$param->loginPost('password'),
30+
!empty($_POST['stayloggedin']) && $_POST['stayloggedin'] === 'yes'
31+
);
32+
if(!is_null($token)){
33+
$gui->showLoginToken($token);
34+
}
35+
}
36+
else if( $param->isSessionPost() ) {
37+
$login->sessionLogin(
38+
$param->loginPost('group'),
39+
$param->loginPost('token')
40+
);
41+
}
2342
}
24-
$gui = new WebGUI($param, $login);
2543

2644
if( isset($_GET['err']) && in_array($_GET['err'], array(404, 403)) ){
2745
$gui->errorPage($_GET['err']);

php/load/main.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,18 @@ $(() => {
3434
}
3535
});
3636
});
37-
});
37+
});
38+
39+
function checkForLoginCode(){
40+
var url = window.location.href;
41+
if( localStorage.hasOwnProperty("loginToken") &&
42+
( !sessionStorage.hasOwnProperty("tokenUsed") || parseInt(sessionStorage.getItem('tokenUsed')) + 10000 < Date.now() ) &&
43+
url.substring(url.length - 6) !== 'logout'
44+
){
45+
sessionStorage.setItem("tokenUsed", Date.now());
46+
let data = localStorage.getItem("loginToken").split(',');
47+
$.post(url, { "group": data[1], "token": data[0]}, () => {
48+
window.location.reload();
49+
});
50+
}
51+
}

0 commit comments

Comments
 (0)