diff --git a/README.md b/README.md index 3eaa02c..ea60dbb 100644 --- a/README.md +++ b/README.md @@ -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); diff --git a/index.php b/index.php index 7000dc9..999c535 100644 --- a/index.php +++ b/index.php @@ -8,26 +8,45 @@ 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 - ) { - 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; + 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; } } @@ -49,15 +68,25 @@ class Card { class DB { private $db; - public function __construct() { - $this->db = new PDO('sqlite:db.db'); - $this->db->query("PRAGMA foreign_keys = ON"); + 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()); ?> diff --git a/db.sql b/username.sql similarity index 98% rename from db.sql rename to username.sql index 777d0b6..b35cd25 100644 --- a/db.sql +++ b/username.sql @@ -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,