modified: README.md

modified:   index.php
	renamed:    db.sql -> username.sql

Extended the Auth class to support multiple databases and users.
This commit is contained in:
Sven Luethi committed 2023-12-23 16:09:43 +01:00
1 parent 72dd67a6d2
commit 296f2a937d
3 files changed
+72 -24

No files matched your search

+22 -6
View File
@@ -23,19 +23,35 @@ I wrote this small application to help my wife learn german but then thought of
## Requirements
- php 7.0 or higher
- php 5.0 or higher
- php-sqlite3 extension
## Installation
Create a database from the provided db.sql file:
1. Set a password for your new user in username.sql. Find following line and replace 'password' by your preferred password.
```
sqlite db.db < db.sql
INSERT INTO config VALUES(1,'PASSWORD','password');
```
### Change the username/password
Find the USERNAME/PASSWORD rows in the config table inside the database and edit as needed.
Create a database from the provided username.sql file. Note that the filename without the '.db' determines the login username.
```
sqlite yourloginuser.db < username.sql
```
You can also disable authentication by removing the '//' part of following lines at the bottom of index.php
```
//$db = new DB('username.db');
//new App($db);
```
And adding '//' to the start of the following lines at the same spot in index.php
```
$auth = new Auth();
new App($auth->getDBHandle());
```
You can also comment out the line
```
new Auth($db);
+49 -16
View File
@@ -8,27 +8,46 @@
</head>
<body>
<?php
// Following 2 lines are for debugging only and may be removed for security.
error_reporting(E_ALL);
ini_set('display_errors', 1);
class Auth {
private $db, $user, $pass;
private $db;
public function __construct($db) {
$this->db = $db;
$this->user = $db->getConfigValueByKey('USERNAME');
$this->pass = $db->getConfigValueByKey('PASSWORD');
if (!isset($_SERVER['PHP_AUTH_USER']) ||
$_SERVER['PHP_AUTH_USER'] != $this->user ||
!isset($_SERVER['PHP_AUTH_PW']) ||
$_SERVER['PHP_AUTH_PW'] != $this->pass
) {
public function __construct() {
if (!isset($_SERVER['PHP_AUTH_USER']) || $_SERVER['PHP_AUTH_USER'] === '' ||
!isset($_SERVER['PHP_AUTH_PW']) || $_SERVER['PHP_AUTH_PW'] === '')
{
$this->respondWithAccessDenied();
}
$dbFileName = preg_replace("/[^A-Za-z0-9]/", '', $_SERVER['PHP_AUTH_USER']) . '.db';
if(!file_exists($dbFileName)) {
$this->respondWithAccessDenied();
}
$this->db = new DB($dbFileName);
if($this->db == null) {
$this->respondWithAccessDenied();
}
$pass = $this->db->getConfigValueByKey('PASSWORD');
if($_SERVER['PHP_AUTH_PW'] !== $pass) {
$this->respondWithAccessDenied();
}
}
public function getDBHandle() {
return $this->db;
}
private function respondWithAccessDenied() {
header('HTTP/1.0 401 Unauthorized');
header('WWW-Authenticate: Basic realm="Flashy"');
echo 'You are not authorized to access this site. Please log in.';
exit;
}
}
}
class Card {
@@ -49,15 +68,25 @@ class Card {
class DB {
private $db;
public function __construct() {
$this->db = new PDO('sqlite:db.db');
public function __construct($dbFileName) {
try {
$this->db = new PDO('sqlite:' . $dbFileName);
$this->db->query("PRAGMA foreign_keys = ON");
}
catch(PDOException $e) {
$this->db = null;
}
}
public function __destruct() {
$db = null;
}
public function databaseIsOpen() {
if($this->db != null) return true;
else return false;
}
public function getClasses() {
return $this->db->query("SELECT * FROM classes");
}
@@ -174,6 +203,10 @@ class App {
$checkCase = true;
public function __construct($db) {
if($db == null) {
echo 'App cannot start without an active database. Good bye!';
exit;
}
$this->db = $db;
$this->handleAllInput();
$this->selectCardIfBoxSelected();
@@ -402,9 +435,9 @@ class App {
unset($_GET[$key]);
}
}
$db = new DB();
new Auth($db);
new App($db);
$auth = new Auth();
new App($auth->getDBHandle());
?>
</body>
</html>
+1 -2
View File
@@ -6,8 +6,7 @@ CREATE TABLE config (
value TEXT NOT NULL
);
INSERT INTO config VALUES(0,'NEW_CARD_BOX_ID','4');
INSERT INTO config VALUES(1,'USERNAME','username');
INSERT INTO config VALUES(2,'PASSWORD','password');
INSERT INTO config VALUES(1,'PASSWORD','password');
CREATE TABLE boxes (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,