From 67efb77b0d4f35f87d68cfc840a6a41ed3c458e1 Mon Sep 17 00:00:00 2001 From: Madeorsk Date: Sun, 26 Aug 2018 12:04:29 +0200 Subject: [PATCH] Subdirectory handling. --- index.php | 15 +++++++++------ static/js/new.js | 3 ++- views/home.php | 6 +++--- views/layout.php | 3 ++- views/poll.php | 4 ++-- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/index.php b/index.php index 4b06053..858c989 100644 --- a/index.php +++ b/index.php @@ -4,6 +4,8 @@ require __DIR__ . "/src/models/Poll.php"; require __DIR__ . "/src/Format.php"; require __DIR__ . "/config/app.php"; +Flight::set("flight.base_url", $VERLAINE["app_url"]); + Flight::route("POST /polls", function () { $request = Flight::request(); if ($request->type === "application/json") @@ -20,6 +22,7 @@ Flight::route("POST /polls", function () { }); Flight::route("GET /polls/@id:[a-fA-F0-9]+", function ($id) { + global $VERLAINE; $poll = Poll::load_poll($id); if ($poll) { @@ -32,7 +35,7 @@ Flight::route("GET /polls/@id:[a-fA-F0-9]+", function ($id) { Flight::redirect("/polls/$id/results"); // A vote is already registered with this IP: redirect. else { - Flight::render("poll", ["poll" => $poll], "body_content"); + Flight::render("poll", ["app_url" => $VERLAINE["app_url"], "poll" => $poll], "body_content"); Flight::render("layout"); } } @@ -73,16 +76,16 @@ Flight::route("POST /polls/@id:[a-fA-F0-9]+/vote", function ($id) { if($poll->vote($selected_options)) // Vote for the selected option. { $poll->save(); - Flight::redirect("/polls/$id/results"); // Redirect to the results. + Flight::redirect("/polls/$id/results", 301); // Redirect to the results. } else - Flight::redirect("/polls/$id"); // Error: Redirect to the vote page. + Flight::redirect("/polls/$id", 301); // Error: Redirect to the vote page. } else - Flight::redirect("/polls/$id"); // Error: Redirect to the vote page. + Flight::redirect("/polls/$id", 301); // Error: Redirect to the vote page. } else - Flight::redirect("/polls/$id"); // Error: Redirect to the vote page. + Flight::redirect("/polls/$id", 301); // Error: Redirect to the vote page. //TODO Error code in query parameters? } } @@ -125,7 +128,7 @@ Flight::route("GET|DELETE /polls/@id:[a-fA-F0-9]+/@token:[a-fA-F0-9]+", function if (Flight::request()->type === "application/json") Flight::halt(401, "

401 Unauthorized

Invalid token.

"); else - Flight::redirect('/'); + Flight::redirect('/', 301); } } else diff --git a/static/js/new.js b/static/js/new.js index 4380b3a..55c6d06 100644 --- a/static/js/new.js +++ b/static/js/new.js @@ -20,6 +20,7 @@ document.addEventListener("DOMContentLoaded", () => { while(next_id < 4) create_choice(); let form = document.getElementById("newpoll"); + console.log(`${location.href}/polls`); form.addEventListener("submit", (event) => { event.preventDefault(); @@ -32,7 +33,7 @@ document.addEventListener("DOMContentLoaded", () => { return choices; } - fetch("/polls", { + fetch(`${location.href}${ location.href.endsWith("/") ? "" : "/" }polls`, { method: "POST", body: JSON.stringify({ title: form.querySelector(`input[name="title"]`).value, diff --git a/views/home.php b/views/home.php index 016bbc4..2bcac19 100644 --- a/views/home.php +++ b/views/home.php @@ -1,6 +1,6 @@ - - + +

Poll Verlaine

@@ -28,7 +28,7 @@ - See the poll! + See the poll!
diff --git a/views/layout.php b/views/layout.php index f416f1e..4408a06 100644 --- a/views/layout.php +++ b/views/layout.php @@ -1,10 +1,11 @@ + Poll Verlaine - + /static/css/main.css" /> diff --git a/views/poll.php b/views/poll.php index db00454..45649e8 100644 --- a/views/poll.php +++ b/views/poll.php @@ -1,6 +1,6 @@

title ?>

-
+ options as $id => $option): ?>
" name="options[]" value="" id="option-" /> @@ -10,5 +10,5 @@ - Jump to results + Jump to results
\ No newline at end of file