diff --git a/VERSION b/VERSION index 54fbff4..a5da1bd 100644 --- a/VERSION +++ b/VERSION @@ -1,4 +1,4 @@ latest -0.2.8 -0.2 +0.3.0 +0.3 0 diff --git a/php/core/Login.php b/php/core/Login.php index 275e69c..2122575 100644 --- a/php/core/Login.php +++ b/php/core/Login.php @@ -19,13 +19,15 @@ class Login { private JSONReader $groupList; - public function __construct( string $group = '', string $client = '', string $token = '' ) { + public function __construct( string $group = '', string $token = '', string $client = '' ) { $this->groupList = new JSONReader('groups'); if(!empty($group) && !empty($client) && !empty($token)){ $this->apiClientLogin($group, $client, $token); } - - if( TaskTimeTerminate === 'GUI' && session_status() === PHP_SESSION_ACTIVE ){ + else if(!empty($group) && !empty($token)){ + $this->sessionLogin($group, $token); + } + else if( TaskTimeTerminate === 'GUI' && session_status() === PHP_SESSION_ACTIVE ){ $this->userSessionLogin(); } } @@ -44,6 +46,18 @@ private function apiClientLogin(string $group, string $client, string $token) : $this->logUserOut(); } + public function sessionLogin(string $group, string $token) : void { + if( $this->groupList->isValue([$group]) ){ + $sid = $this->groupList->searchValue([$group, 'sessions'], $token, 'token'); + if( $sid !== false ){ + $this->logUserIn($group); + $this->groupList->setValue([$group, 'sessions', $sid, 'used'], time()); + return; + } + } + $this->logUserOut(); + } + private function userSessionLogin() : void { $this->loggedIn = isset($_SESSION['login']) && $_SESSION['login'] === true && $_SESSION['login_time'] + 600 > time(); @@ -56,14 +70,27 @@ private function userSessionLogin() : void { } } - public function userLogin(string $group, string $password) : void { + public function userLogin(string $group, string $password, bool $stayLoggedIn = false) : ?string { if( $this->groupList->isValue([$group]) ){ if(self::checkHashedPassword($password, $this->groupList->getValue([$group, 'passhash']))){ $this->logUserIn($group); - return; + + if( $stayLoggedIn ) { + $token = Utilities::randomCode(50, Utilities::ID); + $this->groupList->setValue([$group, 'sessions', null], array( + "browseros" => Utilities::getBrowserOS(), + "used" => 0, + "token" => $token + )); + return $token; + } + else{ + return null; + } } } $this->logUserOut(); + return null; } private function logUserIn(string $group, string $device = "") : void { diff --git a/php/core/ParamParser.php b/php/core/ParamParser.php index 6a2257c..cfe1af3 100644 --- a/php/core/ParamParser.php +++ b/php/core/ParamParser.php @@ -40,6 +40,11 @@ public function isLoginPost() : bool { !empty($_POST['group']) && !empty($_POST['password']); } + public function isSessionPost() : bool { + return $_SERVER['REQUEST_METHOD'] === 'POST' && + !empty($_POST['group']) && !empty($_POST['token']); + } + public function isLogoutGet() : bool { return $_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['logout']); @@ -57,6 +62,9 @@ public function loginPost(string $name) : string { } } } + else if($name === 'token' ){ + return !empty($_POST['token']) && is_string($_POST['token']) ? preg_replace('/[^A-Za-z0-9]/', '', $_POST['token']) : ''; + } return ''; } diff --git a/php/core/WebGUI.php b/php/core/WebGUI.php index 0887409..54c5859 100644 --- a/php/core/WebGUI.php +++ b/php/core/WebGUI.php @@ -31,14 +31,6 @@ public function __construct( ParamParser $param, Login $login ) { $this->param = $param; $this->mainTemp = new Template('main'); - if($this->login->isLoggedIn()){ - $this->mainTemp->setContent('DISPLAYLOGOUTBOX', ''); - $this->mainTemp->setContent('GROUP', $this->login->getGroup()); - } - else{ - $this->mainTemp->setContent('HOMELINK', ''); - } - $this->fillTemplateWithImprint($this->mainTemp); } @@ -152,6 +144,10 @@ public function accountManage() : void { } } + public function showLoginToken(string $token) : void { + $this->mainTemp->setContent('MOREHEADER', ''); + } + public function deviceManage() : void { $this->mainTemp->setContent('TITLE', 'Device Management'); $device = new Template('device'); @@ -159,7 +155,7 @@ public function deviceManage() : void { $r = $this->login->getGroupList(); $myGroup = $this->login->getGroup(); - if( !empty($_POST['device']) || !empty($_GET['regenerate']) || !empty($_GET['delete']) ){ + if( !empty($_POST['device']) || !empty($_GET['regenerate']) || !empty($_GET['delete']) || isset($_GET['remove']) ){ $device->setContent('NOTEDISABLE',''); if( !empty($_POST['device']) && InputParser::checkDeviceName($_POST['device']) ){ $name = $_POST['device']; @@ -208,6 +204,13 @@ public function deviceManage() : void { $device->setContent('NOTEMSG','Device does not exist!'); } } + else if( isset($_GET['remove']) && preg_match('/^[0-9]+$/', $_GET['remove'] ) === 1 ){ + $device->setContent( + 'NOTEMSG', + $r->isValue([$myGroup, 'sessions', $_GET['remove']]) && $r->setValue([$myGroup, 'sessions', $_GET['remove']], null) ? + 'Deleted session!': 'Error deleting session!' + ); + } else{ $device->setContent('NOTEMSG','Invalid format!'); } @@ -271,7 +274,8 @@ public function home() : void { if(!empty($e)){ $tasks[] = array( 'NAME' => $this->nameList[$k], - 'PARAM' => $e + 'PARAM' => $e, + 'ACTIVE' => $k === ParamParser::TASK_HOME ? 'active' : '' ); } } @@ -292,6 +296,14 @@ public function loginForm() : void { } public function __destruct(){ + if($this->login->isLoggedIn()){ + $this->mainTemp->setContent('DISPLAYLOGOUTBOX', ''); + $this->mainTemp->setContent('GROUP', $this->login->getGroup()); + } + else{ + $this->mainTemp->setContent('HOMELINK', ''); + } + $this->mainTemp->output(); } } diff --git a/php/core/api/API.php b/php/core/api/API.php index 669ea46..9771b77 100644 --- a/php/core/api/API.php +++ b/php/core/api/API.php @@ -35,7 +35,7 @@ public function __construct() { public function request(array $post) : void { $this->validatePost($post); if( !$this->hasError ){ - $this->login = new Login($post['group'], $post['client'], $post['token']); + $this->login = new Login($post['group'], $post['token'], $post['client']); if( $this->login->isLoggedIn()){ $this->handleAPITask(); } diff --git a/php/core/templates/home.json b/php/core/templates/home.json index 137c983..4fe27f8 100644 --- a/php/core/templates/home.json +++ b/php/core/templates/home.json @@ -2,7 +2,8 @@ "multiples" : { "Links" : { "%%NAME%%" : "", - "%%PARAM%%" : "" + "%%PARAM%%" : "", + "%%ACTIVE%%" : "" } } } \ No newline at end of file diff --git a/php/core/templates/home_en.html b/php/core/templates/home_en.html index 05e15a2..c385661 100644 --- a/php/core/templates/home_en.html +++ b/php/core/templates/home_en.html @@ -1,7 +1,7 @@