General improvements.

+ Add a way to configure the db handler (default DB : BerkeleyDB 4);
+ Add a way to launch a database optimization on delete;
* Designed Multiple votes checkbox;
* Others minor improvements.
This commit is contained in:
Madeorsk 2018-08-16 12:53:07 +02:00
parent 24f385d12d
commit b7ece5cdad
6 changed files with 53 additions and 23 deletions

View File

@ -11,4 +11,6 @@ $VERLAINE = [
"#FFAFEC", // Pink. "#FFAFEC", // Pink.
"#82FFE8", // Light blue. "#82FFE8", // Light blue.
], ],
"optimize_on_delete" => false,
"db_handler" => "db4", // See available handlers by using `php -r "var_dump(dba_handlers());"`
]; ];

View File

@ -125,23 +125,18 @@ Flight::route("GET|DELETE /polls/@id:[a-fA-F0-9]+/@token:[a-fA-F0-9]+", function
$poll = Poll::load_poll($id); $poll = Poll::load_poll($id);
if ($poll) if ($poll)
{ {
if (Flight::request()->type === "application/json") if ($poll->delete_token === $token)
{ {
if ($poll->delete_token === $token) $poll->delete();
{ if (Flight::request()->type === "application/json")
$poll->delete();
Flight::json(format_poll($poll), 204); Flight::json(format_poll($poll), 204);
}
else else
Flight::halt(401, "<h1>401 Unauthorized</h1><h3>Invalid token.</h3>"); Flight::redirect('/', 308);
} }
else else
{ {
if ($poll->delete_token === $token) if (Flight::request()->type === "application/json")
{ Flight::halt(401, "<h1>401 Unauthorized</h1><h3>Invalid token.</h3>");
$poll->delete();
Flight::redirect('/', 204);
}
else else
Flight::redirect('/', 401); Flight::redirect('/', 401);
} }

View File

@ -1,5 +1,7 @@
<?php <?php
require __DIR__ . "/../config/app.php";
define("SAVE_PATH", __DIR__ . "/../db"); define("SAVE_PATH", __DIR__ . "/../db");
class Poll class Poll
@ -38,7 +40,9 @@ class Poll
*/ */
public static function load_poll($id) public static function load_poll($id)
{ {
$db = dba_open(SAVE_PATH . "/polls.db", "rd"); global $VERLAINE;
$db = dba_open(SAVE_PATH . "/polls.db", "rd", $VERLAINE["db_handler"]);
if (dba_exists($id, $db)) if (dba_exists($id, $db))
{ {
@ -72,7 +76,9 @@ class Poll
private function gen_new_id() private function gen_new_id()
{ {
$db = dba_open(SAVE_PATH . "/polls.db", "rd"); global $VERLAINE;
$db = dba_open(SAVE_PATH . "/polls.db", "rd", $VERLAINE["db_handler"]);
function gen_id() function gen_id()
{ return bin2hex(openssl_random_pseudo_bytes(16)); } { return bin2hex(openssl_random_pseudo_bytes(16)); }
@ -109,7 +115,9 @@ class Poll
public function save() public function save()
{ {
$db = dba_open(SAVE_PATH . "/polls.db", "wd"); global $VERLAINE;
$db = dba_open(SAVE_PATH . "/polls.db", "wd", $VERLAINE["db_handler"]);
$func = (dba_exists($this->id, $db) ? "dba_replace" : "dba_insert"); $func = (dba_exists($this->id, $db) ? "dba_replace" : "dba_insert");
$func($this->id, json_encode([ $func($this->id, json_encode([
"title" => $this->title, "title" => $this->title,
@ -124,8 +132,12 @@ class Poll
public function delete() public function delete()
{ {
$db = dba_open(SAVE_PATH . "/polls.db", "wd"); global $VERLAINE;
$db = dba_open(SAVE_PATH . "/polls.db", "wd", $VERLAINE["db_handler"]);
dba_delete($this->id, $db); dba_delete($this->id, $db);
if ($VERLAINE["optimize_on_delete"])
dba_optimize($db);
dba_close($db); dba_close($db);
} }
} }

View File

@ -29,12 +29,16 @@ main
margin: 0 5%; margin: 0 5%;
} }
main p main p,
main label
{ {
display: block;
margin: 0.5em;
font-size: 1.5em; font-size: 1.5em;
text-align: center; text-align: center;
} }
main p strong main p strong,
main label strong
{ {
font-family: "PT Serif", serif; font-family: "PT Serif", serif;
font-size: 1.2em; font-size: 1.2em;
@ -50,6 +54,7 @@ main a.button
margin: auto; margin: auto;
padding: 1em; padding: 1em;
width: 25rem; width: 25rem;
border-radius: 0;
box-sizing: border-box; box-sizing: border-box;
background: #141414; background: #141414;
@ -76,6 +81,12 @@ main button:hover,
main a.button:hover main a.button:hover
{ background: #1D1D1D; } { background: #1D1D1D; }
main hr
{
border: solid #343434 thin;
width: 25rem;
}
footer footer
{ {
display: block; display: block;
@ -99,4 +110,6 @@ footer
width: 100%; width: 100%;
font-size: 1.2em; font-size: 1.2em;
} }
main hr
{ width: 100%; }
} }

View File

@ -52,10 +52,12 @@ h1.poll
{ {
flex: 1; flex: 1;
display: block; display: block;
margin: 0;
padding: 1em; padding: 1em;
font-family: "PT Serif", serif; font-family: "PT Serif", serif;
font-size: 1.2em; font-size: 1.2em;
cursor: pointer; cursor: pointer;
text-align: left;
} }
@media screen and (max-width: 640px) @media screen and (max-width: 640px)

View File

@ -5,24 +5,30 @@
<h1>Poll Verlaine</h1> <h1>Poll Verlaine</h1>
<main> <main>
<form action="#" id="newpoll"> <form action="#" id="newpoll">
<input type="text" name="title" placeholder="What do you want to ask?" /> <input type="text" name="title" placeholder="What do you want to ask?" required />
<div id="choices"> <div id="choices">
</div> </div>
<button type="button" id="add-choice">New choice</button> <button type="button" id="add-choice">New choice</button>
<input type="checkbox" name="unique_ip" value="unique_ip" checked /> <hr/>
<label for="unique_ip">Allow multiple votes from a single IP</label> <div class="option">
<input type="checkbox" name="unique_ip" value="unique_ip" id="unique_ip" checked />
<label for="unique_ip" class="check"></label>
<label for="unique_ip">Allow multiple votes from a single IP</label>
</div>
<input type="submit" value="Create poll" /> <input type="submit" value="Create poll" />
</form> </form>
<div id="result" hidden> <div id="result" hidden>
<p>Your poll <strong>:poll_title</strong> is ready!</p> <p>Your poll <strong>:poll_title</strong> is ready!</p>
<input type="text" name="pollurl" value="<?= $app_url ?>:poll_url" /> <label for="pollurl">Poll URL</label>
<input type="text" name="deleteurl" value="<?= $app_url ?>:delete_url" /> <input type="text" id="pollurl" name="pollurl" value="<?= $app_url ?>:poll_url" />
<label for="deleteurl">Delete URL</label>
<input type="text" id="deleteurl" name="deleteurl" value="<?= $app_url ?>:delete_url" />
<a class="button" href=":poll_url">See the poll!</a> <a class="button" href=":poll_url">See the poll!</a>
</div> </div>
</main> </main>
<template id="choice"> <template id="choice">
<input type="text" id="choice-:id" placeholder="Another choice" /> <input type="text" id="choice-:id" placeholder="Another choice" required />
<button type="button" class="delete" tabindex="-1" title="Delete" aria-label="Delete"></button> <button type="button" class="delete" tabindex="-1" title="Delete" aria-label="Delete"></button>
</template> </template>