mirror of
https://git.cant.at/Madeorsk/PollVerlaine
synced 2024-11-22 08:14:52 +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:
parent
b763b1d9e6
commit
a6e986fb16
16
index.php
16
index.php
@ -60,11 +60,17 @@ Flight::route("POST /polls/@id:[a-fA-F0-9]+/vote", function ($id) {
|
|||||||
if (isset(Flight::request()->data["options"]) && is_array(Flight::request()->data["options"]))
|
if (isset(Flight::request()->data["options"]) && is_array(Flight::request()->data["options"]))
|
||||||
{ // Check that an options id array exists.
|
{ // Check that an options id array exists.
|
||||||
//TODO Check that only the authorized number of options are selected.
|
//TODO Check that only the authorized number of options are selected.
|
||||||
$poll->vote(Flight::request()->data["options"]); // Vote for the given options.
|
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.
|
// Then save and show poll data.
|
||||||
$poll->save();
|
$poll->save();
|
||||||
Flight::json(format_poll($poll));
|
Flight::json(format_poll($poll));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
Flight::halt(403, "<h1>403 Forbidden</h1><h3>Invalid data.</h3>");
|
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"];
|
$selected_options = Flight::request()->data["options"];
|
||||||
if (is_string($selected_options))
|
if (is_string($selected_options))
|
||||||
{ // If it is a string, input[type="radio"] were used so only one option is selected.
|
{ // 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.
|
if($poll->vote([intval($selected_options)]) === false) // Vote for the selected option.
|
||||||
|
{
|
||||||
|
Flight::redirect('/', 401);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
$poll->save();
|
$poll->save();
|
||||||
Flight::redirect("/polls/$id/results"); // Redirect to the results.
|
Flight::redirect("/polls/$id/results"); // Redirect to the results.
|
||||||
|
}
|
||||||
} //TODO: Multiple options case.
|
} //TODO: Multiple options case.
|
||||||
else
|
else
|
||||||
Flight::redirect("/polls/$id"); // Error: Redirect to the vote page.
|
Flight::redirect("/polls/$id"); // Error: Redirect to the vote page.
|
||||||
|
@ -21,6 +21,7 @@ class Poll
|
|||||||
"votes" => 0,
|
"votes" => 0,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
$poll->settings = $request_data->settings;
|
||||||
$poll->gen_new_id();
|
$poll->gen_new_id();
|
||||||
$poll->delete_token = bin2hex(openssl_random_pseudo_bytes(16));
|
$poll->delete_token = bin2hex(openssl_random_pseudo_bytes(16));
|
||||||
$poll->save();
|
$poll->save();
|
||||||
@ -48,6 +49,8 @@ class Poll
|
|||||||
$poll->creation_date = $saved_poll_data->creation_date;
|
$poll->creation_date = $saved_poll_data->creation_date;
|
||||||
$poll->options = $saved_poll_data->options;
|
$poll->options = $saved_poll_data->options;
|
||||||
$poll->delete_token = $saved_poll_data->delete_token;
|
$poll->delete_token = $saved_poll_data->delete_token;
|
||||||
|
$poll->settings = $saved_poll_data->settings;
|
||||||
|
$poll->ips = $saved_poll_data->ips;
|
||||||
|
|
||||||
dba_close($db);
|
dba_close($db);
|
||||||
return $poll;
|
return $poll;
|
||||||
@ -63,6 +66,8 @@ class Poll
|
|||||||
public $title;
|
public $title;
|
||||||
public $creation_date;
|
public $creation_date;
|
||||||
public $options = [];
|
public $options = [];
|
||||||
|
public $settings = [];
|
||||||
|
public $ips = [];
|
||||||
public $delete_token;
|
public $delete_token;
|
||||||
|
|
||||||
private function gen_new_id()
|
private function gen_new_id()
|
||||||
@ -83,13 +88,23 @@ class Poll
|
|||||||
/**
|
/**
|
||||||
* Vote for a list of options.
|
* Vote for a list of options.
|
||||||
* @param array $options - Array of integers containing voted options.
|
* @param array $options - Array of integers containing voted options.
|
||||||
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function vote(array $options)
|
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.
|
// For each option in the list, add 1 to the vote number in the poll data.
|
||||||
foreach ($options as $option)
|
foreach ($options as $option)
|
||||||
if (isset($this->options[intval($option)])) // Check invalid options id.
|
if (isset($this->options[intval($option)])) // Check invalid options id.
|
||||||
$this->options[intval($option)]->votes++;
|
$this->options[intval($option)]->votes++;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function save()
|
public function save()
|
||||||
@ -100,7 +115,9 @@ class Poll
|
|||||||
"title" => $this->title,
|
"title" => $this->title,
|
||||||
"creation_date" => $this->creation_date,
|
"creation_date" => $this->creation_date,
|
||||||
"options" => $this->options,
|
"options" => $this->options,
|
||||||
"delete_token" => $this->delete_token
|
"delete_token" => $this->delete_token,
|
||||||
|
"ips" => $this->ips,
|
||||||
|
"settings" => $this->settings
|
||||||
]), $db);
|
]), $db);
|
||||||
dba_close($db);
|
dba_close($db);
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,9 @@ document.addEventListener("DOMContentLoaded", () => {
|
|||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
title: form.querySelector(`input[name="title"]`).value,
|
title: form.querySelector(`input[name="title"]`).value,
|
||||||
options: get_choices(form),
|
options: get_choices(form),
|
||||||
|
settings: {
|
||||||
|
"unique_ip": form.querySelector(`input[name="unique_ip"]`).checked,
|
||||||
|
}
|
||||||
}),
|
}),
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
|
@ -9,6 +9,8 @@
|
|||||||
<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 />
|
||||||
|
<label for="unique_ip">Allow multiple votes from a single IP</label>
|
||||||
<input type="submit" value="Create poll" />
|
<input type="submit" value="Create poll" />
|
||||||
</form>
|
</form>
|
||||||
<div id="result" hidden>
|
<div id="result" hidden>
|
||||||
|
Loading…
Reference in New Issue
Block a user