1
0
mirror of https://git.cant.at/Madeorsk/PollVerlaine synced 2024-11-21 20:44:32 +01:00

WIP: unique_ip ! do not merge

+ Unique_ip check
+ Checkbox

TODO: Add some style
TODO: Fix a bug when you vote
This commit is contained in:
Tagadda 2018-08-13 20:23:43 +02:00
parent b763b1d9e6
commit a6e986fb16
4 changed files with 42 additions and 8 deletions

View File

@ -60,10 +60,16 @@ Flight::route("POST /polls/@id:[a-fA-F0-9]+/vote", function ($id) {
if (isset(Flight::request()->data["options"]) && is_array(Flight::request()->data["options"]))
{ // Check that an options id array exists.
//TODO Check that only the authorized number of options are selected.
$poll->vote(Flight::request()->data["options"]); // Vote for the given options.
// Then save and show poll data.
$poll->save();
Flight::json(format_poll($poll));
if($poll->vote(Flight::request()->data["options"]) === false) // Vote for the given options.
{
Flight::halt(403, "<h1>403 Forbidden</h1><h3>Too many votes for this IP address.</h3>");
}
else
{
// Then save and show poll data.
$poll->save();
Flight::json(format_poll($poll));
}
}
else
Flight::halt(403, "<h1>403 Forbidden</h1><h3>Invalid data.</h3>");
@ -75,9 +81,15 @@ Flight::route("POST /polls/@id:[a-fA-F0-9]+/vote", function ($id) {
$selected_options = Flight::request()->data["options"];
if (is_string($selected_options))
{ // If it is a string, input[type="radio"] were used so only one option is selected.
$poll->vote([intval($selected_options)]); // Vote for the selected option.
$poll->save();
Flight::redirect("/polls/$id/results"); // Redirect to the results.
if($poll->vote([intval($selected_options)]) === false) // Vote for the selected option.
{
Flight::redirect('/', 401);
}
else
{
$poll->save();
Flight::redirect("/polls/$id/results"); // Redirect to the results.
}
} //TODO: Multiple options case.
else
Flight::redirect("/polls/$id"); // Error: Redirect to the vote page.

View File

@ -21,6 +21,7 @@ class Poll
"votes" => 0,
];
}
$poll->settings = $request_data->settings;
$poll->gen_new_id();
$poll->delete_token = bin2hex(openssl_random_pseudo_bytes(16));
$poll->save();
@ -48,6 +49,8 @@ class Poll
$poll->creation_date = $saved_poll_data->creation_date;
$poll->options = $saved_poll_data->options;
$poll->delete_token = $saved_poll_data->delete_token;
$poll->settings = $saved_poll_data->settings;
$poll->ips = $saved_poll_data->ips;
dba_close($db);
return $poll;
@ -63,6 +66,8 @@ class Poll
public $title;
public $creation_date;
public $options = [];
public $settings = [];
public $ips = [];
public $delete_token;
private function gen_new_id()
@ -83,13 +88,23 @@ class Poll
/**
* Vote for a list of options.
* @param array $options - Array of integers containing voted options.
* @return bool
*/
public function vote(array $options)
{
if($this->settings['unique_ip'] === true)
{
if(isset($this->ips[Flight::request()->query["ip"]]))
return false;
else
$this->ips["test"] = true;
}
// For each option in the list, add 1 to the vote number in the poll data.
foreach ($options as $option)
if (isset($this->options[intval($option)])) // Check invalid options id.
$this->options[intval($option)]->votes++;
return true;
}
public function save()
@ -100,7 +115,9 @@ class Poll
"title" => $this->title,
"creation_date" => $this->creation_date,
"options" => $this->options,
"delete_token" => $this->delete_token
"delete_token" => $this->delete_token,
"ips" => $this->ips,
"settings" => $this->settings
]), $db);
dba_close($db);
}

View File

@ -37,6 +37,9 @@ document.addEventListener("DOMContentLoaded", () => {
body: JSON.stringify({
title: form.querySelector(`input[name="title"]`).value,
options: get_choices(form),
settings: {
"unique_ip": form.querySelector(`input[name="unique_ip"]`).checked,
}
}),
headers: {
"Content-Type": "application/json",

View File

@ -9,6 +9,8 @@
<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>
<input type="submit" value="Create poll" />
</form>
<div id="result" hidden>