2018-08-12 14:30:38 +02:00
|
|
|
<?php
|
|
|
|
require __DIR__ . "/vendor/autoload.php";
|
2018-08-26 11:11:48 +02:00
|
|
|
require __DIR__ . "/src/models/Poll.php";
|
|
|
|
require __DIR__ . "/src/Format.php";
|
2018-08-12 19:29:13 +02:00
|
|
|
require __DIR__ . "/config/app.php";
|
2018-08-12 14:30:38 +02:00
|
|
|
|
|
|
|
Flight::route("POST /polls", function () {
|
|
|
|
$request = Flight::request();
|
|
|
|
if ($request->type === "application/json")
|
|
|
|
{
|
|
|
|
$request_json = $request->data;
|
|
|
|
$poll = Poll::create_poll($request_json);
|
|
|
|
if ($poll)
|
2018-08-26 11:11:48 +02:00
|
|
|
Flight::json(array_merge(Format::poll($poll), ["delete_token" => $poll->delete_token]), 201);
|
2018-08-12 14:30:38 +02:00
|
|
|
else
|
|
|
|
Flight::halt(403, "<h1>403 Forbidden</h1><h3>Invalid data.</h3>");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Flight::halt(403, "<h1>403 Forbidden</h1><h3>Invalid Content-Type.</h3>");
|
|
|
|
});
|
2018-08-13 13:02:09 +02:00
|
|
|
|
2018-08-12 14:30:38 +02:00
|
|
|
Flight::route("GET /polls/@id:[a-fA-F0-9]+", function ($id) {
|
|
|
|
$poll = Poll::load_poll($id);
|
|
|
|
if ($poll)
|
|
|
|
{
|
|
|
|
if (Flight::request()->type === "application/json")
|
2018-08-26 11:11:48 +02:00
|
|
|
Flight::json(Format::poll($poll));
|
2018-08-12 14:30:38 +02:00
|
|
|
else
|
|
|
|
{
|
2018-08-16 14:09:37 +02:00
|
|
|
// If unique_ip option is enabled => Only allow unregistered IPs.
|
|
|
|
if (!$poll->settings->unique_ip && isset($poll->ips[Flight::request()->ip]))
|
|
|
|
Flight::redirect("/polls/$id/results"); // A vote is already registered with this IP: redirect.
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Flight::render("poll", ["poll" => $poll], "body_content");
|
|
|
|
Flight::render("layout");
|
|
|
|
}
|
2018-08-12 14:30:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Flight::notFound();
|
|
|
|
});
|
|
|
|
|
2018-08-13 14:51:21 +02:00
|
|
|
// POST /polls/:id/vote
|
|
|
|
// Take an array of options and add a vote for each given option.
|
|
|
|
Flight::route("POST /polls/@id:[a-fA-F0-9]+/vote", function ($id) {
|
|
|
|
$poll = Poll::load_poll($id);
|
|
|
|
if ($poll)
|
|
|
|
{
|
|
|
|
if (Flight::request()->type === "application/json")
|
|
|
|
{
|
|
|
|
if (isset(Flight::request()->data["options"]) && is_array(Flight::request()->data["options"]))
|
|
|
|
{ // Check that an options id array exists.
|
2018-08-16 14:09:37 +02:00
|
|
|
if ($poll->vote(Flight::request()->data["options"])) // Vote for the given options.
|
2018-08-13 20:23:43 +02:00
|
|
|
{
|
|
|
|
// Then save and show poll data.
|
|
|
|
$poll->save();
|
2018-08-26 11:11:48 +02:00
|
|
|
Flight::json(Format::poll($poll));
|
2018-08-13 20:23:43 +02:00
|
|
|
}
|
2018-08-16 14:09:37 +02:00
|
|
|
else
|
|
|
|
Flight::halt(403, "<h1>403 Forbidden</h1><h3>Too many votes for this IP address or too many options selected.</h3>");
|
2018-08-13 14:51:21 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
Flight::halt(403, "<h1>403 Forbidden</h1><h3>Invalid data.</h3>");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (isset(Flight::request()->data["options"]))
|
|
|
|
{ // Check that any data has been sent.
|
|
|
|
$selected_options = Flight::request()->data["options"];
|
2018-08-16 14:09:37 +02:00
|
|
|
if (is_array($selected_options))
|
|
|
|
{
|
|
|
|
if($poll->vote($selected_options)) // Vote for the selected option.
|
2018-08-13 20:23:43 +02:00
|
|
|
{
|
|
|
|
$poll->save();
|
|
|
|
Flight::redirect("/polls/$id/results"); // Redirect to the results.
|
|
|
|
}
|
2018-08-16 14:09:37 +02:00
|
|
|
else
|
|
|
|
Flight::redirect("/polls/$id"); // Error: Redirect to the vote page.
|
|
|
|
}
|
2018-08-13 14:51:21 +02:00
|
|
|
else
|
|
|
|
Flight::redirect("/polls/$id"); // Error: Redirect to the vote page.
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Flight::redirect("/polls/$id"); // Error: Redirect to the vote page.
|
|
|
|
//TODO Error code in query parameters?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Flight::notFound();
|
|
|
|
});
|
|
|
|
|
2018-08-13 17:02:06 +02:00
|
|
|
Flight::route("GET /polls/@id:[a-fA-F0-9]+/results", function ($id) {
|
2018-08-13 21:25:14 +02:00
|
|
|
global $VERLAINE;
|
2018-08-13 17:02:06 +02:00
|
|
|
$poll = Poll::load_poll($id);
|
|
|
|
if ($poll)
|
|
|
|
{
|
|
|
|
if (Flight::request()->type === "application/json")
|
2018-08-26 11:11:48 +02:00
|
|
|
Flight::json(Format::poll($poll)); //TODO Add a svg for results?
|
2018-08-13 17:02:06 +02:00
|
|
|
else
|
|
|
|
{
|
2018-08-13 21:25:14 +02:00
|
|
|
Flight::render("svg/results", ["poll" => $poll, "colors" => $VERLAINE["chart_colors"]], "results_chart");
|
|
|
|
Flight::render("results", ["poll" => $poll, "chart_colors" => $VERLAINE["chart_colors"]], "body_content");
|
2018-08-13 17:02:06 +02:00
|
|
|
Flight::render("layout");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Flight::notFound();
|
|
|
|
});
|
|
|
|
|
2018-08-13 15:20:06 +02:00
|
|
|
Flight::route("GET|DELETE /polls/@id:[a-fA-F0-9]+/@token:[a-fA-F0-9]+", function ($id, $token) {
|
|
|
|
$poll = Poll::load_poll($id);
|
|
|
|
if ($poll)
|
|
|
|
{
|
2018-08-16 12:53:07 +02:00
|
|
|
if ($poll->delete_token === $token)
|
2018-08-13 16:22:24 +02:00
|
|
|
{
|
2018-08-16 12:53:07 +02:00
|
|
|
$poll->delete();
|
|
|
|
if (Flight::request()->type === "application/json")
|
2018-08-26 11:11:48 +02:00
|
|
|
Flight::json(Format::poll($poll), 204);
|
2018-08-13 16:22:24 +02:00
|
|
|
else
|
2018-08-16 12:53:07 +02:00
|
|
|
Flight::redirect('/', 308);
|
2018-08-13 16:22:24 +02:00
|
|
|
}
|
2018-08-13 15:20:06 +02:00
|
|
|
else
|
|
|
|
{
|
2018-08-16 12:53:07 +02:00
|
|
|
if (Flight::request()->type === "application/json")
|
|
|
|
Flight::halt(401, "<h1>401 Unauthorized</h1><h3>Invalid token.</h3>");
|
2018-08-13 16:22:24 +02:00
|
|
|
else
|
2018-08-26 11:11:48 +02:00
|
|
|
Flight::redirect('/');
|
2018-08-13 15:20:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Flight::notFound();
|
|
|
|
});
|
|
|
|
|
2018-08-12 14:30:38 +02:00
|
|
|
Flight::route("/", function () {
|
2018-08-12 19:29:13 +02:00
|
|
|
global $VERLAINE;
|
|
|
|
Flight::render("home", ["app_url" => $VERLAINE["app_url"]], "body_content");
|
2018-08-12 14:30:38 +02:00
|
|
|
Flight::render("layout");
|
|
|
|
});
|
|
|
|
|
|
|
|
Flight::start();
|