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.
This commit is contained in:
Sven Luethi authored and sven committed 2023-12-21 22:30:42 +01:00
1 parent 1963eaf1f9
commit 0e4421bb2a
3 files changed
+84 -52

No files matched your search

+5 -9
View File
@@ -32,17 +32,13 @@ Create a database from the provided db.sql file:
sqlite db.db < db.sql sqlite db.db < db.sql
``` ```
## Edit index.php ### Change the username/password
Find following user/password lines and edit as needed. Find the USERNAME/PASSWORD rows in the config table inside the database and edit as needed.
```
class Auth {
private $user = 'username';
private $pass = 'password';
```
You can also comment out the line 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.
+10
View File
@@ -1,5 +1,13 @@
PRAGMA foreign_keys=OFF; PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION; 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 ( CREATE TABLE boxes (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
name TEXT NOT NULL, 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(101,4,2,'to lie (telling untruth)','lügen');
INSERT INTO cards VALUES(102,4,2,'to take (time)','dauert'); INSERT INTO cards VALUES(102,4,2,'to take (time)','dauert');
INSERT INTO cards VALUES(103,4,1,'admission','Eintritt'); 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; COMMIT;
+69 -43
View File
@@ -12,10 +12,12 @@ error_reporting(E_ALL);
ini_set('display_errors', 1); ini_set('display_errors', 1);
class Auth { class Auth {
private $user = 'username'; private $db, $user, $pass;
private $pass = 'password';
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']) || if (!isset($_SERVER['PHP_AUTH_USER']) ||
$_SERVER['PHP_AUTH_USER'] != $this->user || $_SERVER['PHP_AUTH_USER'] != $this->user ||
!isset($_SERVER['PHP_AUTH_PW']) || !isset($_SERVER['PHP_AUTH_PW']) ||
@@ -64,6 +66,12 @@ class DB {
return $this->db->query("SELECT * FROM boxes"); 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) { public function getBoxNameByBoxID($boxID) {
$query = $this->db->prepare("SELECT name FROM boxes WHERE id=? LIMIT 1"); $query = $this->db->prepare("SELECT name FROM boxes WHERE id=? LIMIT 1");
$query->execute([$boxID]); $query->execute([$boxID]);
@@ -142,8 +150,8 @@ class DB {
public function createCard($card) { public function createCard($card) {
if($card->classID != null && $card->front != null && $card->back != null) { if($card->classID != null && $card->front != null && $card->back != null) {
$this->db->prepare("INSERT INTO cards (box_id,class_id,front,back) VALUES(3,?,?,?)") $this->db->prepare("INSERT INTO cards (box_id,class_id,front,back) VALUES(?,?,?,?)")
->execute([$card->classID, $card->front, $card->back]); ->execute([$card->boxID, $card->classID, $card->front, $card->back]);
} }
} }
@@ -165,11 +173,33 @@ class App {
$message, $message,
$checkCase = true; $checkCase = true;
public function __construct() { public function __construct($db) {
$this->db = new DB(); $this->db = $db;
$this->handleAllInput();
$this->selectCardIfBoxSelected();
$this->runTestIfUserInput();
$this->renderTest();
$this->renderBoxes();
$this->renderEditor();
}
$this->handleInput(); private function renderTest() {
echo '<div class="test_view">' . "\n";
echo '<div class="std_row">' . $this->message . "</div>\n";
echo '<div class="std_row">'.($this->selectedCard == null ? '' : $this->selectedCard->front.' | '.$this->db->getClassNameByClassID($this->selectedCard->classID))."</div>\n";
echo '<form method="POST">' . "\n";
echo '<input type="text" name="test_input" autocomplete="off"'
.($this->testInput == null ? '' : ' value="' . $this->testInput . '"')
.($this->selectedCard == null ? '' : ' autofocus')
.'>'."\n";
echo '<input class="btn_row" type="submit" value="Check">' . "\n";
echo '<input type="hidden" name="card_id" value="'.($this->selectedCard == null ? '' : $this->selectedCard->id).'">'."\n";
echo '</form>' . "\n";
echo '<a class="btn_row'.($this->checkCase ? ' selected' : '').'" href="'.$this->createExtendedGetParameterString('check_case', (int)!$this->checkCase).'">Case sensitive</a>' . "\n";
echo "</div>\n";
}
private function selectCardIfBoxSelected() {
// Select random card from selected box since none is selected yet: // Select random card from selected box since none is selected yet:
if($this->selectedBoxID != null && $this->selectedCard == null) { if($this->selectedBoxID != null && $this->selectedCard == null) {
$this->selectNewCard(); $this->selectNewCard();
@@ -180,18 +210,31 @@ class App {
} }
*/ */
} }
}
private function runTestIfUserInput() {
// Do some evaluation... // Do some evaluation...
if(isset($this->selectedCard) && $this->testInput != null) { if(isset($this->selectedCard) && $this->testInput != null) {
// Case sensitive... // Copy values to be mangled :)
if($this->checkCase) { $a = $this->selectedCard->back;
$a = $this->selectedCard->back; $b = $this->testInput;
$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... // Case insensitive...
else { if(!$this->checkCase) {
$a = strtolower($this->selectedCard->back); $a = strtolower($a);
$b = strtolower($this->testInput); $b = strtolower($b);
} }
//Answer correct: //Answer correct:
if($a == $b) { if($a == $b) {
@@ -224,30 +267,13 @@ class App {
*/ */
$this->selectedCard->boxID = $this->db->getNewBoxIDOnIncorrectByBoxID($this->selectedCard->boxID); $this->selectedCard->boxID = $this->db->getNewBoxIDOnIncorrectByBoxID($this->selectedCard->boxID);
} }
// Flush box_id update to database
$this->db->updateCard($this->selectedCard); $this->db->updateCard($this->selectedCard);
// Clear the input field
$this->testInput = ''; $this->testInput = '';
// select the next card for the new round
$this->selectNewCard(); $this->selectNewCard();
} }
$this->renderTest();
$this->renderBoxes();
$this->renderConfigurator();
}
private function renderTest() {
echo '<div class="test_view">' . "\n";
echo '<div class="std_row">' . $this->message . "</div>\n";
echo '<div class="std_row">'.($this->selectedCard == null ? '' : $this->selectedCard->front.' | '.$this->db->getClassNameByClassID($this->selectedCard->classID))."</div>\n";
echo '<form method="POST">' . "\n";
echo '<input type="text" name="test_input" autocomplete="off"'
.($this->testInput == null ? '' : ' value="' . $this->testInput . '"')
.($this->selectedCard == null ? '' : ' autofocus')
.'>'."\n";
echo '<input class="btn_row" type="submit" value="Check">' . "\n";
echo '<input type="hidden" name="card_id" value="'.($this->selectedCard == null ? '' : $this->selectedCard->id).'">'."\n";
echo '</form>' . "\n";
echo '<a class="btn_row'.($this->checkCase ? ' selected' : '').'" href="'.$this->createExtendedGetParameterString('check_case', (int)!$this->checkCase).'">Case sensitive</a>' . "\n";
echo "</div>\n";
} }
private function renderBoxes() { private function renderBoxes() {
@@ -271,8 +297,8 @@ class App {
echo "</div>\n"; echo "</div>\n";
} }
private function renderConfigurator() { private function renderEditor() {
echo '<div class="configurator_view">' . "\n"; echo '<div class="editor_view">' . "\n";
echo '<form method="POST">' . "\n"; echo '<form method="POST">' . "\n";
echo '<div><input type="text" name="card_front" value="'.($this->selectedCard != null ? $this->selectedCard->front : 'Front').'"></div>' . "\n"; echo '<div><input type="text" name="card_front" value="'.($this->selectedCard != null ? $this->selectedCard->front : 'Front').'"></div>' . "\n";
echo '<div><input type="text" name="card_back" value="'. ($this->selectedCard != null ? $this->selectedCard->back : 'Back') .'"></div>' . "\n"; echo '<div><input type="text" name="card_back" value="'. ($this->selectedCard != null ? $this->selectedCard->back : 'Back') .'"></div>' . "\n";
@@ -295,7 +321,7 @@ class App {
echo "</div>\n"; echo "</div>\n";
} }
private function handleInput() { private function handleAllInput() {
if (isset($_REQUEST['box_id']) && if (isset($_REQUEST['box_id']) &&
$_REQUEST['box_id'] >= 0 && $_REQUEST['box_id'] >= 0 &&
$_REQUEST['box_id'] < $this->db->getNumBoxes()) $_REQUEST['box_id'] < $this->db->getNumBoxes())
@@ -324,7 +350,7 @@ class App {
isset($_REQUEST['card_front']) && $_REQUEST['card_front'] != null && isset($_REQUEST['card_front']) && $_REQUEST['card_front'] != null &&
isset($_REQUEST['card_back']) && $_REQUEST['card_back'] != 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); $this->db->createCard($card);
} }
// Modify an existing card in the database // Modify an existing card in the database
@@ -376,9 +402,9 @@ class App {
unset($_GET[$key]); unset($_GET[$key]);
} }
} }
$db = new DB();
new Auth(); new Auth($db);
new App(); new App($db);
?> ?>
</body> </body>
</html> </html>