diff --git a/README.md b/README.md index 59a416b..3eaa02c 100644 --- a/README.md +++ b/README.md @@ -32,17 +32,13 @@ Create a database from the provided db.sql file: sqlite db.db < db.sql ``` -## Edit index.php -Find following user/password lines and edit as needed. -``` -class Auth { - private $user = 'username'; - private $pass = 'password'; -``` +### Change the username/password +Find the USERNAME/PASSWORD rows in the config table inside the database and edit as needed. You can also comment out the line ``` -new Auth(); +new Auth($db); ``` -at the bottom to disable basic authentication completely. +at the bottom of index.php to disable basic authentication completely. + diff --git a/db.sql b/db.sql index 3f4925f..777d0b6 100644 --- a/db.sql +++ b/db.sql @@ -1,5 +1,13 @@ PRAGMA foreign_keys=OFF; BEGIN TRANSACTION; +CREATE TABLE config ( + id INTEGER PRIMARY KEY, + key TEXT NOT NULL, + 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'); CREATE TABLE boxes ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, @@ -140,4 +148,6 @@ INSERT INTO cards VALUES(100,4,9,'at (time)','um'); INSERT INTO cards VALUES(101,4,2,'to lie (telling untruth)','lügen'); INSERT INTO cards VALUES(102,4,2,'to take (time)','dauert'); INSERT INTO cards VALUES(103,4,1,'admission','Eintritt'); +INSERT INTO cards VALUES(104,4,0,'the change','die Änderung'); +INSERT INTO cards VALUES(105,4,0,'to change','ändern'); COMMIT; diff --git a/index.php b/index.php index fa52c73..7000dc9 100644 --- a/index.php +++ b/index.php @@ -12,10 +12,12 @@ error_reporting(E_ALL); ini_set('display_errors', 1); class Auth { - private $user = 'username'; - private $pass = 'password'; + private $db, $user, $pass; - public function __construct() { + 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']) || @@ -64,6 +66,12 @@ class DB { return $this->db->query("SELECT * FROM boxes"); } + public function getConfigValueByKey($key) { + $query = $this->db->prepare("SELECT value FROM config where key=?"); + $query->execute([$key]); + return $query->fetchColumn(); + } + public function getBoxNameByBoxID($boxID) { $query = $this->db->prepare("SELECT name FROM boxes WHERE id=? LIMIT 1"); $query->execute([$boxID]); @@ -142,8 +150,8 @@ class DB { public function createCard($card) { if($card->classID != null && $card->front != null && $card->back != null) { - $this->db->prepare("INSERT INTO cards (box_id,class_id,front,back) VALUES(3,?,?,?)") - ->execute([$card->classID, $card->front, $card->back]); + $this->db->prepare("INSERT INTO cards (box_id,class_id,front,back) VALUES(?,?,?,?)") + ->execute([$card->boxID, $card->classID, $card->front, $card->back]); } } @@ -165,11 +173,33 @@ class App { $message, $checkCase = true; - public function __construct() { - $this->db = new DB(); + public function __construct($db) { + $this->db = $db; + $this->handleAllInput(); + $this->selectCardIfBoxSelected(); + $this->runTestIfUserInput(); + $this->renderTest(); + $this->renderBoxes(); + $this->renderEditor(); + } - $this->handleInput(); + private function renderTest() { + echo '