Initial code, moved here from the "jilo" repo.

main
Yasen Pramatarov 2024-06-28 20:05:32 +03:00
parent 79de371404
commit 051b461c40
20 changed files with 290 additions and 1 deletions

6
.gitignore vendored 100644
View File

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

View File

@ -1,2 +1,15 @@
# jilo-web # Jilo Web
## requirements
- web server (deb: apache | nginx)
- php support in the web server (deb: php-fpm | libapache2-mod-php)
- pdo and pdo_sqlite support in php (deb: php-db, php-sqlite3) uncomment in php.ini: ;extension=pdo_sqlite
## TODO
- jilo-web.db outside web root
- jilo-web.db writable by web server user

0
config.apache 100644
View File

0
config.nginx 100644
View File

3
install.sh 100755
View File

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

0
jilo-web.conf 100644
View File

5
jilo-web.schema 100644
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', $hashedPassword);
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,47 @@
<?php
unset($error);
if (isset($_GET['page'])) {
$page = $_GET['page'];
} elseif (isset($_POST['page'])) {
$page = $_POST['page'];
} else {
$page = 'front';
}
session_start();
if ( !isset($_SESSION['user_id']) && ($page !== 'login' && $page !== 'register') ) {
header('Location: index.php?page=login');
exit();
}
if ( isset($_SESSION['username']) ) {
$user = htmlspecialchars($_SESSION['username']);
}
if (isset($error)) {
echo "<p style='color: red;'>Error: $error</p>";
}
$allowed_urls = [
'front',
'login',
'logout',
'register',
'profile',
'config',
];
include 'templates/header.php';
if (in_array($page, $allowed_urls)) {
include "pages/{$page}.php";
} else {
include 'pages/front.php';
}
include 'templates/footer.php';
?>

View File

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

View File

@ -0,0 +1,28 @@
<?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) ) {
header('Location: index.php');
exit();
} else {
echo "Login failed.";
}
}
} catch (Exception $e) {
$error = $e->getMessage();
}
include 'templates/form-login.php';
?>

View File

@ -0,0 +1,10 @@
<?php
session_start();
session_unset();
session_destroy();
unset($error);
echo "You logged out.";
?>

View File

@ -0,0 +1,3 @@
<?php ?>
profile of <?= $user ?>

View File

@ -0,0 +1,27 @@
<?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/form-register.php';
?>

View File

@ -0,0 +1,38 @@
.menu-container {
display: flex;
justify-content: space-between;
align-items: center;
list-style: none;
padding: 0;
margin: 0;
margin-bottom: 10px;
background-color: #777;
}
.menu-left, .menu-right {
display: flex;
list-style: none;
padding: 0;
margin: 0;
}
.menu-left li, .menu-right li {
margin: 0 10px;
}
.menu-left a, .menu-right a {
text-decoration: none;
color: white;
}
.menu-left li a, .menu-right li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.menu-left li a:hover, .menu-right li a:hover {
background-color: #111;
}

View File

@ -0,0 +1,6 @@
<div id="footer">Jilo Web</div>
</body>
</html>

View File

@ -0,0 +1,6 @@
<form method="POST" action="?page=login">
<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,6 @@
<form method="POST" action="?page=register">
<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,28 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="static/all.css">
<title>Jilo Web</title>
</head>
<body>
<div class="menu-container">
<ul class="menu-left">
<li><a href="index.php">home</a></li>
<?php if ( isset($_SESSION['user_id']) ) { ?>
<li><a href="?page=config">config</a></li>
<?php } ?>
</ul>
<ul class="menu-right">
<?php if ( isset($_SESSION['user_id']) ) { ?>
<li><a href="?page=profile"><?= $user ?></a></li>
<li><a href="?page=logout">logout</a></li>
<?php } else { ?>
<li><a href="?page=login">login</a></li>
<li><a href="?page=register">register</a></li>
<?php } ?>
</ul>
</div>