librecat/login.php

97 lines
2.9 KiB
PHP
Raw Normal View History

2020-05-17 00:02:20 +00:00
<?php
session_start();
$db = new SQLite3('ask.db');
$users_table = $db->query("CREATE TABLE IF NOT EXISTS users(
id INTEGER PRIMARY KEY,
username TEXT,
shadow TEXT,
name TEXT,
bio TEXT
)");
$logErrorMsg = "";
$validUser = false;
if(isset($_SESSION["login"])) $validUser = $_SESSION["login"] === true;
if(isset($_POST["log"])){
2020-05-17 14:46:28 +00:00
if($_POST["logname"]){
$user = $_POST["logname"];
$shadow = $db->querySingle("SELECT shadow FROM users WHERE username = '$user'");
if(password_verify($_POST["password"],$shadow)){
echo("Logged in");
}
else{
$logErrorMsg = "Incorrect user or password";
}
}
else $logErrorMsg = "Please input a username";
2020-05-17 00:02:20 +00:00
$validUser = $_POST["logname"] == "admin" && password_verify($_POST["password"], $hash);
if(!$validUser) $errorMsg = "Invalid username or password.";
else $_SESSION["login"] = true;
}
if($validUser){
header("Location: /user/".$_POST["logname"]);
}
$regErrorMsg = "";
if(isset($_POST["reg"])){
if(password_verify($_POST["invite"],"$2y$10\$Mofyx6QsxEartbq..53zlu.FwTX0aMmUeRCNnISvfmrC44iA1SfSO")){
if($_POST["regname"]){
// Set user
$user = $_POST["regname"];
if(preg_match("/[^a-z0-9]/", $_POST["regname"])){
$regErrorMsg = "Only lowercase letters and numbers are allowed";
}
else if($db->querySingle("SELECT * FROM users WHERE username = '$user';")){
$regErrorMsg = "Username taken";
}
else{
if($_POST["password"]){
// Set password
$shadow = password_hash($_POST["password"], PASSWORD_DEFAULT);
// Insert user into DB
$db->exec("INSERT INTO users(username,shadow) VALUES ('$user','$shadow')");
unset($_POST["regname"],$_POST["password"],$_POST["invite"]);
$regErrorMsg = "Account created successfully";
}
else $regErrorMsg = "Please input a password";
}
}
else $regErrorMsg = "Please input a username";
}
else $regErrorMsg = "Wrong invitation code";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;chartset=utf8"/>
<title>Login</title>
</head>
<body>
<h2>Login</h2>
<form name="input" action="" method="post">
<label for="logname">Username:</label><input type="text" value="<?= $_POST["logname"] ?>" id="logname" name="logname"/><br/>
<label for="password">Password:</label><input type="password" id="password" name="password"/>
<?php if(isset($logErrorMsg)) echo "<p>$logErrorMsg</p>\n"; ?>
<input type="submit" value="Login" name="log"/>
</form>
<h2>Register</h2>
<form name="input" action="" method="post">
<label for="regname">Username:</label><input type="text" value="<?= $_POST["regname"] ?>" id="regname" name="regname"/><br/>
<label for="password">Password:</label><input type="password" id="password" name="password"/><br/>
<label for="invite">Invite code:</label><input type="text" value="<?= $_POST["invite"] ?>" id="invite" name="invite"/><br/>
<?php if(isset($regErrorMsg)) echo "<p>$regErrorMsg</p>\n"; ?>
<input type="submit" value="Register" name="reg"/>
</form>
</body>
</html>