moth/www/cgi-bin/token.cgi

39 lines
1.1 KiB
Plaintext
Raw Permalink Normal View History

2015-06-03 15:31:26 -06:00
#! /usr/bin/env lua
package.path = "?.lua;cgi-bin/?.lua;www/cgi-bin/?.lua"
local cgi = require "cgi"
local koth = require "koth"
local team = cgi.fields['t'] or ""
local token = cgi.fields['k'] or ""
-- Check answer
local needle = token
local haystack = koth.path("tokens.txt")
local found, err = koth.anchored_search(haystack, needle)
if (not found) then
koth.page("Unrecognized token", err)
end
2015-06-03 15:42:03 -06:00
local category, points = token:match("^(.*):(.*):")
if ((category == nil) or (points == nil)) then
2015-06-03 15:31:26 -06:00
koth.page("Unrecognized token", "Something doesn't look right about that token")
end
points = tonumber(points)
2015-06-03 15:42:03 -06:00
-- Defang category name; prevent directory traversal
category = category:gsub("[^A-Za-z0-9]", "-")
2015-06-03 15:31:26 -06:00
local ok, err = koth.award_points(team, category, points, token)
if (not ok) then
koth.page("Error awarding points",
"<p>You entered a valid token, but there was a problem trying to give you points:</p>" ..
"<p>" .. err .. "</p>")
end
koth.page("Points awarded",
"<p>" .. points .. " points for " .. team .. "!</p>" ..
"<p><a href=\"../puzzles.html\">Back to puzzles</a></p>")