ulogger-server/auth.php

140 lines
4.8 KiB
PHP
Raw Normal View History

2013-06-19 13:27:14 +02:00
<?php
2017-01-30 21:36:44 +01:00
/* μlogger
2013-06-19 13:27:14 +02:00
*
2017-01-30 21:36:44 +01:00
* Copyright(C) 2017 Bartek Fabiszewski (www.fabiszewski.net)
2013-06-19 13:27:14 +02:00
*
* This is free software; you can redistribute it and/or modify it under
* the terms of the GNU Library General Public License as published by
* the Free Software Foundation; either version 2 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 Library General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
require_once("config.php");
2013-06-23 23:43:09 +02:00
// if is set cookie overwrite config value
2017-01-30 21:36:44 +01:00
if (isset($_COOKIE["ulogger_api"])) { $mapapi = $_COOKIE["ulogger_api"]; }
if (isset($_COOKIE["ulogger_lang"])) { $lang = $_COOKIE["ulogger_lang"]; }
if (isset($_COOKIE["ulogger_units"])) { $units = $_COOKIE["ulogger_units"]; }
if (isset($_COOKIE["ulogger_interval"])) { $interval = $_COOKIE["ulogger_interval"]; }
2013-06-19 13:27:14 +02:00
require_once("lang.php");
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($mysqli->connect_errno) {
2017-01-30 21:36:44 +01:00
if (defined('headless')) {
header('HTTP/1.1 503 Service Unavailable', true, 503);
} else {
printf("Connect failed: %s\n", $mysqli->connect_error);
}
exit();
2013-06-19 13:27:14 +02:00
}
2013-07-25 23:34:13 +02:00
$mysqli->set_charset("utf8");
2013-06-19 13:27:14 +02:00
$auth = NULL;
2014-09-07 21:19:43 +02:00
$admin = NULL;
2017-01-30 21:36:44 +01:00
if ($require_authentication || defined('headless')) {
2013-06-19 13:27:14 +02:00
/* authentication */
2017-01-30 21:36:44 +01:00
session_name('ulogger');
2013-06-19 13:27:14 +02:00
session_start();
$sid = session_id();
2013-06-19 13:27:14 +02:00
$auth = (isset($_SESSION['auth']) ? $_SESSION['auth'] : "");
2014-09-07 21:19:43 +02:00
$admin = (isset($_SESSION['admin']) ? $_SESSION['admin'] : "");
2013-06-19 13:27:14 +02:00
$user = (isset($_REQUEST['user']) ? $_REQUEST['user'] : "");
2017-01-30 21:36:44 +01:00
$pass = (isset($_REQUEST['pass']) ? $_REQUEST['pass'] : "");
2014-03-05 20:12:36 +01:00
$ssl = ((!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == "" || $_SERVER['HTTPS'] == "off") ? "http" : "https");
2013-06-19 13:27:14 +02:00
$auth_error = (isset($_REQUEST['auth_error']) ? $_REQUEST['auth_error'] : 0);
2013-06-19 13:27:14 +02:00
// not authenticated and username not submited
// load form
if ((!$auth) && (!$user)){
2017-01-30 21:36:44 +01:00
if (defined('headless')) {
header('HTTP/1.1 401 Unauthorized', true, 401);
} else {
print
'<!DOCTYPE html>
<html>
<head>
<title>'.$lang_title.'</title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<link rel="stylesheet" type="text/css" href="main.css">
<script type="text/javascript">
function focus() {
document.forms[0].elements[0].focus();
}
</script>
</head>
<body onload="focus()">
<div id="login">
<div id="title">'.$lang_title.'</div>
<div id="subtitle">'.$lang_private.'</div>
<form action="index.php" method="post">
'.$lang_username.':<br />
<input type="text" name="user"><br />
'.$lang_password.':<br />
<input type="password" name="pass"><br />
<br />
<input type="submit" value="'.$lang_login.'">
</form>
<div id="error">'.(($auth_error==1) ? $lang_authfail : "").'</div>
</div>
</body>
</html>';
}
2013-06-19 13:27:14 +02:00
$mysqli->close();
2017-01-30 21:36:44 +01:00
exit();
2013-06-19 13:27:14 +02:00
}
2014-04-01 17:28:27 +11:00
2013-06-19 13:27:14 +02:00
// username submited
if ((!$auth) && ($user)){
2017-01-30 21:36:44 +01:00
$query = $mysqli->prepare("SELECT id, login, password FROM users WHERE login=? LIMIT 1");
2013-06-19 13:27:14 +02:00
$query->bind_param('s', $user);
$query->execute();
$query->bind_result($rec_id, $rec_user, $rec_pass);
2013-06-19 13:27:14 +02:00
$query->fetch();
$query->free_result();
//correct pass
2014-04-01 17:28:27 +11:00
2017-01-30 21:36:44 +01:00
if (($user == $rec_user) && password_verify($pass, $rec_pass)) {
2013-06-19 13:27:14 +02:00
// login successful
//delete old session
$_SESSION = NULL;
session_destroy();
2013-06-19 13:27:14 +02:00
// start new session
2017-01-30 21:36:44 +01:00
session_name('ulogger');
2013-06-19 13:27:14 +02:00
session_start();
2017-01-30 21:36:44 +01:00
if (($user == $admin_user) && !empty($admin_user)) {
2014-09-07 21:19:43 +02:00
$_SESSION['admin'] = $admin_user;
2014-04-01 17:28:27 +11:00
}
$_SESSION['auth'] = $rec_id;
$url = str_replace("//", "/", $_SERVER['HTTP_HOST'].dirname($_SERVER['SCRIPT_NAME'])."/index.php");
header("Location: $ssl://$url");
2017-01-30 21:36:44 +01:00
exit();
2013-06-19 13:27:14 +02:00
} else {
// unsuccessful
$error = "?auth_error=1";
// destroy session
$_SESSION = NULL;
2017-01-30 21:36:44 +01:00
if (isset($_COOKIE[session_name('ulogger')])) {
setcookie(session_name('ulogger'),'',time()-42000,'/');
2013-06-19 13:27:14 +02:00
}
session_destroy();
$mysqli->close();
2017-01-30 21:36:44 +01:00
if (defined('headless')) {
header('HTTP/1.1 401 Unauthorized', true, 401);
} else {
$url = str_replace("//", "/", $_SERVER['HTTP_HOST'].dirname($_SERVER['SCRIPT_NAME'])."/index.php");
header("Location: $ssl://$url$error");
}
exit();
2013-06-19 13:27:14 +02:00
}
2014-04-01 17:28:27 +11:00
}
2013-06-19 13:27:14 +02:00
/* end of authentication */
}
?>