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.
"#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);
if ($poll)
{
if (Flight::request()->type === "application/json")
if ($poll->delete_token === $token)
{
if ($poll->delete_token === $token)
{
$poll->delete();
$poll->delete();
if (Flight::request()->type === "application/json")
Flight::json(format_poll($poll), 204);
}
else
Flight::halt(401, "<h1>401 Unauthorized</h1><h3>Invalid token.</h3>");
Flight::redirect('/', 308);
}
else
{
if ($poll->delete_token === $token)
{
$poll->delete();
Flight::redirect('/', 204);
}
if (Flight::request()->type === "application/json")
Flight::halt(401, "<h1>401 Unauthorized</h1><h3>Invalid token.</h3>");
else
Flight::redirect('/', 401);
}

View File

@ -1,5 +1,7 @@
<?php
require __DIR__ . "/../config/app.php";
define("SAVE_PATH", __DIR__ . "/../db");
class Poll
@ -38,7 +40,9 @@ class Poll
*/
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))
{
@ -72,7 +76,9 @@ class Poll
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()
{ return bin2hex(openssl_random_pseudo_bytes(16)); }
@ -109,7 +115,9 @@ class Poll
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($this->id, json_encode([
"title" => $this->title,
@ -124,8 +132,12 @@ class Poll
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);
if ($VERLAINE["optimize_on_delete"])
dba_optimize($db);
dba_close($db);
}
}

View File

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

View File

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

View File

@ -5,24 +5,30 @@
<h1>Poll Verlaine</h1>
<main>
<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>
<button type="button" id="add-choice">New choice</button>
<input type="checkbox" name="unique_ip" value="unique_ip" checked />
<label for="unique_ip">Allow multiple votes from a single IP</label>
<hr/>
<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" />
</form>
<div id="result" hidden>
<p>Your poll <strong>:poll_title</strong> is ready!</p>
<input type="text" name="pollurl" value="<?= $app_url ?>:poll_url" />
<input type="text" name="deleteurl" value="<?= $app_url ?>:delete_url" />
<label for="pollurl">Poll URL</label>
<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>
</div>
</main>
<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>
</template>