From 0e4421bb2a9245cddc9545b93a3319c79caa6878 Mon Sep 17 00:00:00 2001 From: Sven Luethi Date: Thu, 21 Dec 2023 22:30:42 +0100 Subject: [PATCH] modified: README.md modified: db.sql modified: index.php - Move user/pass to DB - Implement umlaut normalization so ae, oe, ue, and ss may be used. This also avoids having to use mbstring php extension. --- README.md | 14 +++---- db.sql | 10 +++++ index.php | 112 +++++++++++++++++++++++++++++++++--------------------- 3 files changed, 84 insertions(+), 52 deletions(-) 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 '
' . "\n"; + echo '
' . $this->message . "
\n"; + echo '
'.($this->selectedCard == null ? '' : $this->selectedCard->front.' | '.$this->db->getClassNameByClassID($this->selectedCard->classID))."
\n"; + echo '
' . "\n"; + echo 'testInput == null ? '' : ' value="' . $this->testInput . '"') + .($this->selectedCard == null ? '' : ' autofocus') + .'>'."\n"; + echo '' . "\n"; + echo ''."\n"; + echo '
' . "\n"; + echo 'Case sensitive' . "\n"; + echo "
\n"; + } + private function selectCardIfBoxSelected() { // Select random card from selected box since none is selected yet: if($this->selectedBoxID != null && $this->selectedCard == null) { $this->selectNewCard(); @@ -180,18 +210,31 @@ class App { } */ } + } + private function runTestIfUserInput() { // Do some evaluation... if(isset($this->selectedCard) && $this->testInput != null) { - // Case sensitive... - if($this->checkCase) { - $a = $this->selectedCard->back; - $b = $this->testInput; - } + // Copy values to be mangled :) + $a = $this->selectedCard->back; + $b = $this->testInput; + // Normalize Umlauts + $umlauts = array( + "ä"=>"ae", + "Ä"=>"Ae", + "ö"=>"oe", + "Ö"=>"Oe", + "ü"=>"ue", + "Ü"=>"Ue", + "ß"=>"ss", + "ẞ"=>"Ss" + ); + $a = str_replace(array_keys($umlauts), array_values($umlauts), $a); + $b = str_replace(array_keys($umlauts), array_values($umlauts), $b); // Case insensitive... - else { - $a = strtolower($this->selectedCard->back); - $b = strtolower($this->testInput); + if(!$this->checkCase) { + $a = strtolower($a); + $b = strtolower($b); } //Answer correct: if($a == $b) { @@ -224,30 +267,13 @@ class App { */ $this->selectedCard->boxID = $this->db->getNewBoxIDOnIncorrectByBoxID($this->selectedCard->boxID); } + // Flush box_id update to database $this->db->updateCard($this->selectedCard); + // Clear the input field $this->testInput = ''; + // select the next card for the new round $this->selectNewCard(); } - - $this->renderTest(); - $this->renderBoxes(); - $this->renderConfigurator(); - } - - private function renderTest() { - echo '
' . "\n"; - echo '
' . $this->message . "
\n"; - echo '
'.($this->selectedCard == null ? '' : $this->selectedCard->front.' | '.$this->db->getClassNameByClassID($this->selectedCard->classID))."
\n"; - echo '
' . "\n"; - echo 'testInput == null ? '' : ' value="' . $this->testInput . '"') - .($this->selectedCard == null ? '' : ' autofocus') - .'>'."\n"; - echo '' . "\n"; - echo ''."\n"; - echo '
' . "\n"; - echo 'Case sensitive' . "\n"; - echo "
\n"; } private function renderBoxes() { @@ -271,8 +297,8 @@ class App { echo "\n"; } - private function renderConfigurator() { - echo '
' . "\n"; + private function renderEditor() { + echo '
' . "\n"; echo '
' . "\n"; echo '
' . "\n"; echo '
' . "\n"; @@ -295,7 +321,7 @@ class App { echo "
\n"; } - private function handleInput() { + private function handleAllInput() { if (isset($_REQUEST['box_id']) && $_REQUEST['box_id'] >= 0 && $_REQUEST['box_id'] < $this->db->getNumBoxes()) @@ -324,7 +350,7 @@ class App { isset($_REQUEST['card_front']) && $_REQUEST['card_front'] != null && isset($_REQUEST['card_back']) && $_REQUEST['card_back'] != null) { - $card = new Card(null, 3, $_REQUEST['class_id'], trim($_REQUEST['card_front']), trim($_REQUEST['card_back'])); + $card = new Card(null, (int)$this->db->getConfigValueByKey('NEW_CARD_BOX_ID'), $_REQUEST['class_id'], trim($_REQUEST['card_front']), trim($_REQUEST['card_back'])); $this->db->createCard($card); } // Modify an existing card in the database @@ -376,9 +402,9 @@ class App { unset($_GET[$key]); } } - -new Auth(); -new App(); +$db = new DB(); +new Auth($db); +new App($db); ?>