module("webuci", package.seeall)
function prepare_req(uri)
- REQUEST_URI = uri
+ env = {}
+ env.REQUEST_URI = uri
require("ffluci.menu").get()
end
function init_req(context)
- SERVER_PROTOCOL = context.server_proto
- REMOTE_ADDR = context.remote_addr
- REQUEST_METHOD = context.request_method
- PATH_INFO = "/" .. context.uri
- REMOTE_PORT = context.remote_port
- SERVER_ADDR = context.server_addr
- SCRIPT_NAME = REQUEST_URI:sub(1, #REQUEST_URI - #PATH_INFO)
+ env.SERVER_PROTOCOL = context.server_proto
+ env.REMOTE_ADDR = context.remote_addr
+ env.REQUEST_METHOD = context.request_method
+ env.PATH_INFO = "/" .. context.uri
+ env.REMOTE_PORT = context.remote_port
+ env.SERVER_ADDR = context.server_addr
+ env.SCRIPT_NAME = REQUEST_URI:sub(1, #REQUEST_URI - #PATH_INFO)
end
function handle_req(context)
module = module or "index"
action = action or "index"
- local pattern = ffluci.http.get_script_name() .. "/%s/%s/%s"
+ local pattern = ffluci.http.env.SCRIPT_NAME .. "/%s/%s/%s"
return pattern:format(category, module, action)
end
-- Sends a 404 error code and renders the "error404" template if available
function error404(message)
- ffluci.http.set_status(404, "Not Found")
+ ffluci.http.status(404, "Not Found")
message = message or "Not Found"
if not pcall(ffluci.template.render, "error404") then
- ffluci.http.set_content_type("text/plain")
+ ffluci.http.prepare_content("text/plain")
print(message)
end
return false
-- Sends a 500 error code and renders the "error500" template if available
function error500(message)
- ffluci.http.set_status(500, "Internal Server Error")
+ ffluci.http.status(500, "Internal Server Error")
if not pcall(ffluci.template.render, "error500", {message=message}) then
- ffluci.http.set_content_type("text/plain")
+ ffluci.http.prepare_content("text/plain")
print(message)
end
return false
-- Dispatches a request depending on the PATH_INFO variable
function httpdispatch()
- local pathinfo = ffluci.http.get_path_info() or ""
+ local pathinfo = ffluci.http.env.PATH_INFO or ""
local parts = pathinfo:gmatch("/[%w-]+")
local sanitize = function(s, default)
return data
end
--- Returns the content of file as array of lines
-function readfilel(filename)
- local fp, err = io.open(filename)
- local line = ""
- local data = {}
-
- if fp == nil then
- return nil, err
- end
-
- while true do
- line = fp:read()
- if (line == nil) then break end
- table.insert(data, line)
- end
-
- fp:close()
- return data
-end
-
-- Writes given data to a file
function writefile(filename, data)
local fp, err = io.open(filename, "w")
return dir
end
--- Alias for lfs.mkdir
-mkdir = posix.mkdir
\ No newline at end of file
+-- Alias for posix.mkdir
+mkdir = posix.mkdir
+
+-- Alias for posix.rmdir
+rmdir = posix.rmdir
\ No newline at end of file
require("ffluci.sgi.haserl")
elseif webuci then
require("ffluci.sgi.webuci")
+end
+
+-- Asks the browser to redirect to "url"
+function redirect(url, qs)
+ if qs then
+ url = url .. "?" .. qs
+ end
+
+ ffluci.http.status(302, "Found")
+ print("Location: " .. url .. "\n")
end
\ No newline at end of file
]]--
module("ffluci.i18n", package.seeall)
-require("ffluci.config")
require("ffluci.sys")
table = {}
-- Same as load but autocompletes the filename with .LANG from config.lang
function loadc(file)
- return load(file .. "." .. ffluci.config.main.lang)
+ return load(file .. "." .. require("ffluci.config").main.lang)
end
-- Returns the i18n-value defined by "key" or if there is no such: "default"
scope = {
translate = function(...) return require("ffluci.i18n").translate(...) end,
loadtrans = function(...) return require("ffluci.i18n").loadc(...) end,
- isfile = ffluci.fs.mtime
+ isfile = ffluci.fs.isfile
}
-- Local menu database
]]--
module("ffluci.sgi.haserl", package.seeall)
-ENV = ENV or {}
-FORM = FORM or {}
+-- Environment Table
+ffluci.http.env = ENV
--- HTTP interface
-- Returns a table of all COOKIE, GET and POST Parameters
function ffluci.http.formvalues()
return ffluci.http.formvalue(prefix, {})
end
--- Returns the path info
-function ffluci.http.get_path_info()
- return ENV.PATH_INFO
-end
-
--- Returns the User's IP
-function ffluci.http.get_remote_addr()
- return ENV.REMOTE_ADDR
-end
-
--- Returns the request URI
-function ffluci.http.get_request_uri()
- return ENV.REQUEST_URI
-end
-
--- Returns the script name
-function ffluci.http.get_script_name()
- return ENV.SCRIPT_NAME
-end
-
-
--- Asks the browser to redirect to "url"
-function ffluci.http.redirect(url, qs)
- if qs then
- url = url .. "?" .. qs
- end
-
- ffluci.http.set_status(302, "Found")
- print("Location: " .. url .. "\n")
-end
-
-- Set Content-Type
-function ffluci.http.set_content_type(type)
+function ffluci.http.prepare_content(type)
print("Content-Type: "..type.."\n")
end
-- Sets HTTP-Status-Header
-function ffluci.http.set_status(code, message)
+function ffluci.http.status(code, message)
print("Status: " .. tostring(code) .. " " .. message)
end
]]--
module("ffluci.sgi.webuci", package.seeall)
-local status_set = false
+-- Environment Table
+ffluci.http.env = webuci.env
+
--- HTTP interface
+local status_set = false
-- Returns a table of all COOKIE, GET and POST Parameters
function ffluci.http.formvalues()
return vals
end
--- Returns the path info
-function ffluci.http.get_path_info()
- return webuci.PATH_INFO
-end
-
--- Returns the User's IP
-function ffluci.http.get_remote_addr()
- return webuci.REMOTE_ADDR
-end
-
--- Returns the request URI
-function ffluci.http.get_request_uri()
- return webuci.REQUEST_URI
-end
-
-
--- Returns the script name
-function ffluci.http.get_script_name()
- return webuci.SCRIPT_NAME
-end
-
-
--- Asks the browser to redirect to "url"
-function ffluci.http.redirect(url, qs)
- if qs then
- url = url .. "?" .. qs
- end
-
- ffluci.http.set_status(302, "Found")
- print("Location: " .. url .. "\n")
-end
-
-- Set Content-Type
-function ffluci.http.set_content_type(type)
+function ffluci.http.prepare_content(type)
if not status_set then
- ffluci.http.set_status(200, "OK")
+ ffluci.http.status(200, "OK")
end
print("Content-Type: "..type.."\n")
end
-- Sets HTTP-Status-Header
-function ffluci.http.set_status(code, message)
- print(webuci.SERVER_PROTOCOL .. " " .. tostring(code) .. " " .. message)
+function ffluci.http.status(code, message)
+ print(webuci.env.SERVER_PROTOCOL .. " " .. tostring(code) .. " " .. message)
status_set = true
end
viewns = {
translate = function(...) return require("ffluci.i18n").translate(...) end,
config = function(...) return require("ffluci.model.uci").get(...) or "" end,
- controller = ffluci.http.get_script_name(),
+ controller = ffluci.http.env.SCRIPT_NAME,
media = ffluci.config.main.mediaurlbase,
write = io.write,
include = function(name) Template(name):render(getfenv(2)) end,
end
-- Removes whitespace from beginning and end of a string
-function trim(string)
- local s = string:gsub("^%s*(.-)%s*$", "%1")
+function trim(str)
+ local s = str:gsub("^%s*(.-)%s*$", "%1")
return s
end
<%+header%>
- <form method="post" action="<%=ffluci.http.get_request_uri()%>">
+ <form method="post" action="<%=ffluci.http.env.REQUEST_URI%>">
<div>
<script type="text/javascript" src="<%=media%>/cbi.js"></script>
<input type="hidden" name="cbi.submit" value="1" />
<%+header%>
<h1>404 Not Found</h1>
<p>Sorry, the object you requested was not found.</p>
-<tt>Unable to dispatch: <%=ffluci.http.get_path_info()%></tt>
+<tt>Unable to dispatch: <%=ffluci.http.env.PATH_INFO%></tt>
<%+footer%>
\ No newline at end of file
local menu = require("ffluci.menu").get()[req.category]
menu = menu or {}
require("ffluci.i18n").loadc("default")
-require("ffluci.http").set_content_type("text/html")
+require("ffluci.http").prepare_content("text/html")
%><?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
module("ffluci.controller.splash.splash", package.seeall)
function action_activate()
- local mac = ffluci.sys.net.ip4mac(ffluci.http.remote_addr())
+ local mac = ffluci.sys.net.ip4mac(ffluci.http.env.REMOTE_ADDR)
if mac and ffluci.http.formvalue("accept") then
os.execute("luci-splash add "..mac.." >/dev/null 2>&1")
ffluci.http.redirect(ffluci.model.uci.get("freifunk", "community", "homepage"))
<%
-ffluci.http.set_content_type("text/plain")
+ffluci.http.prepare_content("text/plain")
for k, v in pairs(ffluci.sys.wifi.getiwconfig()) do
%>
<tr>
<%
-ffluci.http.set_content_type("text/plain")
+ffluci.http.prepare_content("text/plain")
for iface, cells in pairs(ffluci.sys.wifi.iwscan()) do
for i, cell in ipairs(cells) do
%>
function action_index()
local uci = ffluci.model.uci.StateSession()
- ffluci.http.set_content_type("text/plain")
+ ffluci.http.prepare_content("text/plain")
-- General
print("luciinfo.api=1")