170 lines
4.1 KiB
PHP
Raw Normal View History

2017-08-25 13:59:19 +02:00
<?php
/* μlogger
*
* Copyright(C) 2017 Bartek Fabiszewski (www.fabiszewski.net)
*
* This is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
if (!defined('ROOT_DIR')) { define('ROOT_DIR', dirname(__DIR__)); }
2017-08-25 13:59:19 +02:00
require_once(ROOT_DIR . "/helpers/user.php");
require_once(ROOT_DIR . "/helpers/utils.php");
if (!defined('BASE_URL')) { define('BASE_URL', uUtils::getBaseUrl()); }
2017-08-25 13:59:19 +02:00
/**
* Authentication
*/
class uAuth {
private $isAuthenticated = false;
public $user = null;
public function __construct() {
$this->sessionStart();
$user = (new uUser())->getFromSession();
if ($user->isValid) {
$this->setAuthenticated($user);
}
}
/**
* Is user authenticated
*
* @return boolean True if authenticated, false otherwise
*/
public function isAuthenticated() {
return $this->isAuthenticated;
}
/**
* Is authenticated user admin
*
* @return boolean True if admin, false otherwise
*/
public function isAdmin() {
return ($this->isAuthenticated && $this->user->isAdmin);
}
/**
* Start php session
*
* @return void
*/
private function sessionStart() {
session_name("ulogger");
session_start();
}
/**
* Terminate php session
*
* @return void
*/
private function sessionEnd() {
$_SESSION = [];
2018-12-05 12:09:45 +01:00
if (ini_get("session.use_cookies") && isset($_COOKIE[session_name()])) {
2017-08-25 13:59:19 +02:00
$params = session_get_cookie_params();
2018-12-05 12:09:45 +01:00
setcookie(session_name(), '', time() - 42000,
2017-08-25 13:59:19 +02:00
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
session_destroy();
}
/**
* Clean session variables
*
* @return void
*/
private function sessionCleanup() {
$_SESSION = [];
}
/**
* Mark as authenticated, set user
*
2018-11-09 12:42:35 +01:00
* @param uUser $user
2017-08-25 13:59:19 +02:00
* @return void
*/
private function setAuthenticated($user) {
$this->isAuthenticated = true;
$this->user = $user;
}
/**
* Process log in request
*
* @return void
*/
2019-01-24 19:07:41 +01:00
public function checkLogin($login, $pass) {
if (!is_null($login) && !is_null($pass)) {
2017-08-25 13:59:19 +02:00
if (!empty($login) && !empty($pass)) {
$user = new uUser($login);
if ($user->isValid && $user->validPassword($pass)) {
$this->setAuthenticated($user);
$this->sessionCleanup();
$user->storeInSession();
2019-01-24 19:07:41 +01:00
return true;
2017-08-25 13:59:19 +02:00
}
}
}
2019-01-24 19:07:41 +01:00
return false;
2017-08-25 13:59:19 +02:00
}
/**
* Log out with redirect
*
* @param string $path URL path (without leading slash)
2017-08-25 13:59:19 +02:00
* @return void
*/
public function logOutWithRedirect($path = "") {
2017-08-25 13:59:19 +02:00
$this->sessionEnd();
$this->exitWithRedirect($path);
}
/**
* Send 401 headers
*
* @return void
*/
public function sendUnauthorizedHeader() {
header('WWW-Authenticate: OAuth realm="users@ulogger"');
header('HTTP/1.1 401 Unauthorized', true, 401);
}
/**
* Send 401 headers and exit
*
* @return void
*/
public function exitWithUnauthorized() {
$this->sendUnauthorizedHeader();
exit();
}
/**
* Redirect browser and exit
*
* @param string $path Redirect URL path (without leading slash)
2017-08-25 13:59:19 +02:00
* @return void
*/
public function exitWithRedirect($path = "") {
$location = BASE_URL . $path;
2018-01-30 13:50:40 +01:00
header("Location: $location");
2017-08-25 13:59:19 +02:00
exit();
}
2018-12-05 12:09:45 +01:00
}