3
0
Fork 0
mirror of https://github.com/fsr/eseeva synced 2024-11-15 08:53:11 +01:00

fix errors in database interaction

This commit is contained in:
sjaster 2017-05-31 17:53:02 +02:00
parent fe8a426334
commit 576d90f074
3 changed files with 45 additions and 13 deletions

View file

@ -37,7 +37,7 @@
session_start(); session_start();
$formState = STATE_ACTION_NEWCODE; $formState = STATE_ACTION_NEWCODE;
$keyCode = ""; $keyCode = "";
$keyData = ReadKeyFile(KEYFILE); $keyData = ReadKeys(KEYFILE);
// if the variable is set, the form has been posted to itself // if the variable is set, the form has been posted to itself
// if the submission ids of the post and the form don't match, the // if the submission ids of the post and the form don't match, the
@ -55,8 +55,9 @@
$keyCode = $_POST["keyCode"]; $keyCode = $_POST["keyCode"];
//$keyData = ReadKeyFile(KEYFILE); //$keyData = ReadKeyFile(KEYFILE);
// if no action was performed on the entered key, simply display its state // if no action was performed on the entered key, simply display its state
if (!isset($_POST["action"])) if (!isset($_POST["action"])){
$formState = KeyStateToFormState(GetKeyState($keyData, $_POST["keyCode"])); $formState = KeyStateToFormState(GetKeyState(KEYFILE, $_POST["keyCode"]));
}
else else
{ {
// otherwise set the state of the form to the action that should be performed // otherwise set the state of the form to the action that should be performed
@ -65,8 +66,8 @@
// on the key and if the action was successful save the key file // on the key and if the action was successful save the key file
if ($formState != STATE_ACTION_NEWCODE) if ($formState != STATE_ACTION_NEWCODE)
{ {
if (SetKeyState($keyData, $keyCode, FormStateToKeyState($formState))) if (SetKeyState(KEYFILE, $keyCode, FormStateToKeyState($formState)))
WriteKeyFile(KEYFILE, $keyData); SetKeyState(KEYFILE, $keyCode, FormStateToKeyState($formState));
else else
$formState($STATE_ACTION_FAILED); $formState($STATE_ACTION_FAILED);
$_POST["action"] = STATE_ACTION_NEWCODE; $_POST["action"] = STATE_ACTION_NEWCODE;
@ -205,11 +206,11 @@
echo " <div class=\"col-6\">\n"; echo " <div class=\"col-6\">\n";
echo " <select class=\"form-control\" id=\"keyCode\" name=\"keyCode\" required>\n"; echo " <select class=\"form-control\" id=\"keyCode\" name=\"keyCode\" required>\n";
foreach ($keyData as $key => $value) { foreach ($keyData as $key => $value) {
echo " <option value=\"". $value[0] ."\" "; echo " <option value=\"". $value['KeyId'] ."\" ";
if ($keyCode != "" && $keyCode == $value[0]) { if ($keyCode != "" && $keyCode == $value['KeyId']) {
echo "selected=\"selected\""; echo "selected=\"selected\"";
} }
echo ">".$value[0]."</option>\n"; echo ">".$value['KeyId']."</option>\n";
} }
echo " </select>\n"; echo " </select>\n";
//echo " <input class=\"form-control\" type=\"text\" id=\"keyCode\" name=\"keyCode\" value=\"" . $keyCode . "\" required"; //echo " <input class=\"form-control\" type=\"text\" id=\"keyCode\" name=\"keyCode\" value=\"" . $keyCode . "\" required";

View file

@ -31,8 +31,9 @@
$i = 0; $i = 0;
while(isset($_POST["keyIndex" . $i])) while(isset($_POST["keyIndex" . $i]))
{ {
if (isset($_POST["keyDelete" . $i])) if (isset($_POST["keyDelete" . $i])){
DeleteKey($keyFile, $_POST["keyCode" . $i]); $delete = DeleteKey($keyFile, $_POST["keyCode" . $i]);
}
else else
SetKeyState($keyFile, $_POST["keyCode" . $i], $_POST["keyState" . $i]); SetKeyState($keyFile, $_POST["keyCode" . $i], $_POST["keyState" . $i]);
$i++; $i++;
@ -52,7 +53,7 @@
{ {
$amount = $_POST["keyAmount"]; $amount = $_POST["keyAmount"];
$keyFile = $_POST["keyFile"]; $keyFile = $_POST["keyFile"];
CreateKeys($amount, $keyFile); AppendKeys($amount, $keyFile);
$keyData = ReadKeys($keyFile); $keyData = ReadKeys($keyFile);
} }
else else

View file

@ -55,6 +55,36 @@
* @param string $fileName The name of the key database that should be created. * @param string $fileName The name of the key database that should be created.
*/ */
function CreateKeys($keyAmount, $fileName) function CreateKeys($keyAmount, $fileName)
{
$handle = new SQLite3($fileName);
if (!$handle)
return false;
$handle->exec("DROP TABLE answers;");
$handle->exec("CREATE TABLE IF NOT EXISTS 'answers'(KeyId text primary key,Status text,Student int,Answer);");
$keys = GenerateKeys($keyAmount);
$count = count($keys);
for ($i = 0; $i < $count; $i++)
{
$stmt = $handle->prepare("INSERT INTO answers (KeyId, Status) VALUES (:key,:status);");
if ($stmt)
{
$stmt->bindValue(':key', $keys[$i], SQLITE3_TEXT);
$stmt->bindValue(':status', KEYSTATE_UNISSUED, SQLITE3_TEXT);
$result = $stmt->execute();
}
else
{
$handle->close();
return false;
}
}
$handle->close();
return true;
}
function AppendKeys($keyAmount, $fileName)
{ {
$handle = new SQLite3($fileName, SQLITE3_OPEN_READWRITE); $handle = new SQLite3($fileName, SQLITE3_OPEN_READWRITE);
if (!$handle) if (!$handle)
@ -186,7 +216,7 @@
return false; return false;
//secure deleting by checking if key has been uesed already //secure deleting by checking if key has been uesed already
$stmt = $handle->prepare("DELETE FROM answers WHERE KeyId=:keyid AND Answer!=NULL;"); $stmt = $handle->prepare("DELETE FROM answers WHERE KeyId=:keyid;");
if ($stmt) if ($stmt)
{ {
$stmt->bindValue(':keyid', $key, SQLITE3_TEXT); $stmt->bindValue(':keyid', $key, SQLITE3_TEXT);