* core: Added shortcut translatef for translate and format
[project/luci.git] / applications / sgi-haserl / src / sgi / haserl.lua
1 --[[
2 FFLuCI - SGI-Module for Haserl
3
4 Description:
5 Server Gateway Interface for Haserl
6
7 FileId:
8 $Id: haserl.lua 2027 2008-05-07 21:16:35Z Cyrus $
9
10 License:
11 Copyright 2008 Steven Barth <steven@midlink.org>
12
13 Licensed under the Apache License, Version 2.0 (the "License");
14 you may not use this file except in compliance with the License.
15 You may obtain a copy of the License at 
16
17         http://www.apache.org/licenses/LICENSE-2.0 
18
19 Unless required by applicable law or agreed to in writing, software
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
24
25 ]]--
26 module("ffluci.sgi.haserl", package.seeall)
27 require("ffluci.fs")
28
29 -- Environment Table
30 ffluci.http.env = ENV
31
32 -- Returns the main dispatcher URL
33 function ffluci.http.dispatcher()
34         return ffluci.http.env.SCRIPT_NAME or ""
35 end
36
37 -- Returns the upload dispatcher URL
38 function ffluci.http.dispatcher_upload()
39         return ffluci.http.dispatcher() .. "-upload"
40 end
41
42 -- Returns a table of all COOKIE, GET and POST Parameters
43 function ffluci.http.formvalues()
44         return FORM
45 end
46
47 -- Gets form value from key
48 function ffluci.http.formvalue(key, default)
49         local c = ffluci.http.formvalues()
50         
51         for match in key:gmatch("[%w-_]+") do
52                 c = c[match]
53                 if c == nil then
54                         return default
55                 end
56         end
57         
58         return c
59 end
60
61 -- Gets a table of values with a certain prefix
62 function ffluci.http.formvaluetable(prefix)
63         return ffluci.http.formvalue(prefix, {})
64 end
65
66 -- Sends a custom HTTP-Header
67 function ffluci.http.header(key, value)
68         print(key .. ": " .. value)
69 end
70
71 -- Set Content-Type
72 function ffluci.http.prepare_content(type)
73         print("Content-Type: "..type.."\n")
74 end
75
76 -- Asks the browser to redirect to "url"
77 function ffluci.http.redirect(url)
78         ffluci.http.status(302, "Found")
79         ffluci.http.header("Location", url)
80         print()
81 end
82
83 -- Returns the path of an uploaded file
84 -- WARNING! File uploads can be easily spoofed! Do additional sanity checks!
85 function ffluci.http.upload(name)
86         local fpath = ffluci.http.formvalue(name)
87         local fname = ffluci.http.formvalue(name .. "_name")
88         
89         if fpath and fname and ffluci.fs.isfile(fpath) then
90                 return fpath
91         end
92 end
93
94 -- Sets HTTP-Status-Header
95 function ffluci.http.status(code, message)
96         print("Status: " .. tostring(code) .. " " .. message)
97 end