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:
1 parent
72dd67a6d2
commit
296f2a937d
3 files changed
+72
-24
No files matched your search
@@ -23,19 +23,35 @@ I wrote this small application to help my wife learn german but then thought of
|
|||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
- php 7.0 or higher
|
- php 5.0 or higher
|
||||||
- php-sqlite3 extension
|
- php-sqlite3 extension
|
||||||
|
|
||||||
## Installation
|
## 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
|
Create a database from the provided username.sql file. Note that the filename without the '.db' determines the login username.
|
||||||
Find the USERNAME/PASSWORD rows in the config table inside the database and edit as needed.
|
```
|
||||||
|
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);
|
new Auth($db);
|
||||||
|
|||||||
@@ -8,28 +8,47 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<?php
|
<?php
|
||||||
|
// Following 2 lines are for debugging only and may be removed for security.
|
||||||
error_reporting(E_ALL);
|
error_reporting(E_ALL);
|
||||||
ini_set('display_errors', 1);
|
ini_set('display_errors', 1);
|
||||||
|
|
||||||
class Auth {
|
class Auth {
|
||||||
private $db, $user, $pass;
|
private $db;
|
||||||
|
|
||||||
public function __construct($db) {
|
public function __construct() {
|
||||||
$this->db = $db;
|
if (!isset($_SERVER['PHP_AUTH_USER']) || $_SERVER['PHP_AUTH_USER'] === '' ||
|
||||||
$this->user = $db->getConfigValueByKey('USERNAME');
|
!isset($_SERVER['PHP_AUTH_PW']) || $_SERVER['PHP_AUTH_PW'] === '')
|
||||||
$this->pass = $db->getConfigValueByKey('PASSWORD');
|
{
|
||||||
if (!isset($_SERVER['PHP_AUTH_USER']) ||
|
$this->respondWithAccessDenied();
|
||||||
$_SERVER['PHP_AUTH_USER'] != $this->user ||
|
}
|
||||||
!isset($_SERVER['PHP_AUTH_PW']) ||
|
|
||||||
$_SERVER['PHP_AUTH_PW'] != $this->pass
|
$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('HTTP/1.0 401 Unauthorized');
|
||||||
header('WWW-Authenticate: Basic realm="Flashy"');
|
header('WWW-Authenticate: Basic realm="Flashy"');
|
||||||
echo 'You are not authorized to access this site. Please log in.';
|
echo 'You are not authorized to access this site. Please log in.';
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
class Card {
|
class Card {
|
||||||
public $id, $boxID, $classID, $front, $back;
|
public $id, $boxID, $classID, $front, $back;
|
||||||
@@ -49,15 +68,25 @@ class Card {
|
|||||||
class DB {
|
class DB {
|
||||||
private $db;
|
private $db;
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct($dbFileName) {
|
||||||
$this->db = new PDO('sqlite:db.db');
|
try {
|
||||||
|
$this->db = new PDO('sqlite:' . $dbFileName);
|
||||||
$this->db->query("PRAGMA foreign_keys = ON");
|
$this->db->query("PRAGMA foreign_keys = ON");
|
||||||
}
|
}
|
||||||
|
catch(PDOException $e) {
|
||||||
|
$this->db = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function __destruct() {
|
public function __destruct() {
|
||||||
$db = null;
|
$db = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function databaseIsOpen() {
|
||||||
|
if($this->db != null) return true;
|
||||||
|
else return false;
|
||||||
|
}
|
||||||
|
|
||||||
public function getClasses() {
|
public function getClasses() {
|
||||||
return $this->db->query("SELECT * FROM classes");
|
return $this->db->query("SELECT * FROM classes");
|
||||||
}
|
}
|
||||||
@@ -174,6 +203,10 @@ class App {
|
|||||||
$checkCase = true;
|
$checkCase = true;
|
||||||
|
|
||||||
public function __construct($db) {
|
public function __construct($db) {
|
||||||
|
if($db == null) {
|
||||||
|
echo 'App cannot start without an active database. Good bye!';
|
||||||
|
exit;
|
||||||
|
}
|
||||||
$this->db = $db;
|
$this->db = $db;
|
||||||
$this->handleAllInput();
|
$this->handleAllInput();
|
||||||
$this->selectCardIfBoxSelected();
|
$this->selectCardIfBoxSelected();
|
||||||
@@ -402,9 +435,9 @@ class App {
|
|||||||
unset($_GET[$key]);
|
unset($_GET[$key]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$db = new DB();
|
|
||||||
new Auth($db);
|
$auth = new Auth();
|
||||||
new App($db);
|
new App($auth->getDBHandle());
|
||||||
?>
|
?>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
@@ -6,8 +6,7 @@ CREATE TABLE config (
|
|||||||
value TEXT NOT NULL
|
value TEXT NOT NULL
|
||||||
);
|
);
|
||||||
INSERT INTO config VALUES(0,'NEW_CARD_BOX_ID','4');
|
INSERT INTO config VALUES(0,'NEW_CARD_BOX_ID','4');
|
||||||
INSERT INTO config VALUES(1,'USERNAME','username');
|
INSERT INTO config VALUES(1,'PASSWORD','password');
|
||||||
INSERT INTO config VALUES(2,'PASSWORD','password');
|
|
||||||
CREATE TABLE boxes (
|
CREATE TABLE boxes (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
name TEXT NOT NULL,
|
name TEXT NOT NULL,
|
||||||
Reference in new issue
Block a user