Initial version of jilo-web, web interface to Jilo.

main
Yasen Pramatarov 2024-06-27 14:03:30 +03:00
parent b0667fa73a
commit 766cacd40f
18 changed files with 232 additions and 0 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
*.log *.log
*.log.* *.log.*
jilo.db jilo.db
jilo-web.db
packaging/deb-package/ packaging/deb-package/
packaging/rpm-package/ packaging/rpm-package/

View File

View File

View File

@ -0,0 +1,3 @@
#!/usr/bin/env bash
echo 'todo install script'

View File

View File

@ -0,0 +1,5 @@
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL UNIQUE,
password TEXT NOT NULL
);

View File

@ -0,0 +1,25 @@
<?php
class Database {
private $pdo;
public function __construct($dbFile) {
if ( !extension_loaded('pdo_sqlite') ) {
throw new Exception('PDO extension for SQLite not loaded.');
}
try {
$this->pdo = new PDO("sqlite:" . $dbFile);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
throw new Exception('DB connection failed: ' . $e->getMessage());
}
}
public function getConnection() {
return $this-pdo;
}
}
?>

View File

@ -0,0 +1,37 @@
<?php
class User {
private $db;
public function __construct($database) {
$this->db = $database->getConnection();
}
public function register($username, $password) {
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
$query = $this->db->prepare("INSERT INTO users (username, password) VALUES (:username, :password)");
$query->bindParam(':username', $username);
$query->bindParam(':password', $username);
return $query->execute();
}
public function login($username, $password) {
$query = $this->db->prepare("SELECT * FROM users WHERE username = :username");
$query->bindParam(':username', $username);
$query->execute();
$user = $query->fetch(PDO::FETCH_ASSOC);
if ( $user && password_verify($password, $user['password'])) {
session_start();
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
return true;
} else {
return false;
}
}
}
?>

View File

@ -0,0 +1,8 @@
<?php
unset($error);
include 'templates/header.php';
include 'templates/body.php';
include 'templates.footer.php';
?>

View File

@ -0,0 +1,30 @@
<?php
require_once 'classes/database.php';
require 'classes/user.php';
unset($error);
try {
$db = new Database('./jilo-web.db');
$user = new User($db);
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
$username = $_POST['username'];
$password = $_POST['password'];
if ( $user->login($username, $password) ) {
echo "Login successful.";
} else {
echo "Login failed.";
}
}
} catch (Exception $e) {
$error = $e->getMessage();
}
include 'templates/header.php';
include 'templates/form-login.php';
include 'templates/footer.php';
?>

View File

@ -0,0 +1,14 @@
<?php
session_start();
session_unset();
session_destroy();
unset($error);
echo "You logged out.";
include 'templates/header.php';
include 'templates/body.php';
include 'templates/footer.php';
?>

View File

@ -0,0 +1,29 @@
<?php
require_once 'classes/database.php';
require 'classes/user.php';
unset($error);
try {
$db = new Database('./jilo-web.db');
$user = new User($db);
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
$username = $_POST['username'];
$password = $_POST['password'];
if ( $user->register($username, $password) ) {
echo "Registration successful.";
} else {
echo "Registration failed.";
}
}
} catch (Exception $e) {
$error = $e->getMessage();
}
include 'templates/header.php';
include 'templates/form-register.php';
include 'templates/footer.php';
?>

View File

@ -0,0 +1,25 @@
ul.menu {
list-style-type: none;
margin: 0;
margin-bottom: 10px;
padding: 0;
overflow: hidden;
background-color: #333;
}
ul.menu li {
float: right;
}
ul.menu li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
ul.menu li a:hover {
background-color: #111;
}

View File

@ -0,0 +1 @@
<?php ?>

View File

@ -0,0 +1,5 @@
<?php ?>
</body>
</html>

View File

@ -0,0 +1,7 @@
<?php ?>
<form method="POST" action="login.php">
<input type="text" name="username" placeholder="Username" required />
<input type="password" name="password" placeholder="Password" required />
<button type="submit">Login</button>
</form>

View File

@ -0,0 +1,7 @@
<?php ?>
<form method="POST" action="register.php">
<input type="text" name="username" placeholder="Username" required />
<input type="password" name="password" placeholder="Password" required />
<button type="submit">Register</button>
</form>

View File

@ -0,0 +1,35 @@
<?php
session_start();
$scriptname = basename($_SERVER['SCRIPT_NAME']);
if ( !isset($_SESSION['user_id']) && ($scriptname !== 'login.php' && $scriptname !== 'register.php') ) {
header('Location: login.php');
exit();
}
if ( isset($_SESSION['username']) ) {
echo "Welcome, " . htmlspecialchars($_SESSION['username']) . "!";
}
if (isset($error)) {
echo "<p style='color: red;'>Error: $error</p>";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="templates/all.css">
<title>Jilo Web</title>
</head>
<body>
<ul class="menu">
<li><a href="login.php">login</a></li>
<li><a href="logout.php">logout</a></li>
<li><a href="register.php">register</a></li>
</ul>