-- ********************************************************************** -- * LUNA API -- * -- * version 0.2.1 -- * -- * (c) 2005,2006 Hi-Project Ltd. -- ********************************************************************** LUNAAPI_VERSION = "0.3.1" -------------------------------------------------------------------------- require (LUNAROOT.."lib/compat-5.1") package.path = PORTALROOT.."?.lua;"..LUNAROOT.."?.lua;"..LUNAROOT.."lib/?.lua;"..LUNAROOT.."lib/lua/?.lua;" -- WINDOWS package.cpath = PORTALROOT.."?.dll;"..LUNAROOT.."?.dll;"..LUNAROOT.."lib/bin/?.dll;"..LUNAROOT.."l?.dll;" -- LINUX -- package.cpath = PORTALROOT.."?.so;"..LUNAROOT.."?.so;"..LUNAROOT.."lib/bin/?.so;"..LUNAROOT.."l?.so;" -------------------------------------------------------------------------- require "webserver.htmlparser" require "webserver.encoding" require "webserver.mimetypes" require "common.languages" require "common.fileutils" require "common.errors" require "common.functions" require "sql.sql" -- GLOBAL VARIABLES ------------------------------------------------------ CLOSESESSION = "KEEP" -- set to 'CLOSE' to close session -- SQLDB - defined in sql -- LOGIN - defined in functions -- LANGUAGE - defined in languages -- ENCODING - defined in encoding -- WEB PAGE -- WebRoot = PORTALROOT.."webroot/" HtmlTemplatePath = PORTALROOT.."templates/" -------------------------------------------------------------------------- -- Html page (frame) cache ----------------------------------------------- ResponseCache = {} local MAXFRAMES=3 -------------------------------------------------------------------------- function LUNAAPI_GET(requestheader,requestdata,rawdata) if requestdata and table.getn(requestdata)>0 then return LUNAAPI_POST(requestheader,requestdata,rawdata) end requestdata = table_decode(requestdata) local fn = requestheader.document or DefaultPage -- check cache local content = nil table.foreach(ResponseCache,function(k,v) if v["name"] == fn or "/"..v["name"] == fn then content = v["content"] end end) if content then return content end -- files to download -- N/A -- files to load/and parse if file.exists(HtmlTemplatePath..fn) then return htemplate.parsebuffer(file.load(HtmlTemplatePath..fn),requestdata) elseif file.exists(WebRoot..fn) then return htemplate.parsebuffer(file.load(WebRoot..fn),requestdata) elseif file.exists(WebRoot..DefaultPage) then return htemplate.parsebuffer(file.load(WebRoot..DefaultPage),requestdata) else LUNAERROR.seterrorbyname("PAGENOTFOUND") return LUNAERROR.geterrormessage() end end function LUNAAPI_POST(requestheader,requestdata,rawdata) requestdata = table_decode(requestdata) LUNAERROR.clearerror() -- the name of the posted object must be LUNACMD -- the value contains the public name of the requested function see. functions local funcname = requestdata.LUNACMD if funcname == nil then LUNAERROR.seterrorbyname("BADFUNCTIONREQUEST") return LUNAERROR.geterrormessage() end local func = LUNAFUNC.findfunc(funcname) if func == nil or func.call == nil then LUNAERROR.seterrorbyname("FUNCTIONNOTFOUND") return LUNAERROR.geterrormessage() end if not func.active then LUNAERROR.seterrorbyname("FUNCTIONNOTACTIVE") return LUNAERROR.geterrormessage() end if func.needlogin and not LoggedIn then LUNAERROR.seterrorbyname("FUNCTIONNEEDSLOGIN") return LUNAERROR.geterrormessage() end -- DEBUG to LOG print("DEMOAPP",funcname) -- now lets go local fn_result, fn_response, fn_name = func.call(requestdata) -- EXEC OK if fn_result then -- RESPONSE OK if fn_response or fn_name then return fn_response or {filename=fn_name} else -- RESPONSE ERROR (set by function) return LUNAERROR.geterrormessage() end else return LUNAERROR.geterrormessage() end end function LUNAAPI_SESSIONCLOSEQUERY(requestheader,requestdata,rawdata) return CLOSESESSION end function LUNAAPI_PUT(requestheader,requestdata,rawdata) -- not implemented return nil end function LUNAAPI_DELETE(requestheader,requestdata,rawdata) -- not implemented return nil end -------------------------------------------------------------------------- -- Initialization -------------------------------------------------------------------------- -- API errors 1-999 LUNAERROR.adderror("API_ERROR", 1, "LUNA API ERROR", "EN") LUNAERROR.adderror("PAGENOTFOUND", 2, "REQUESTED PAGE NOT FOUND", "EN") LUNAERROR.adderror("FUNCTIONNOTFOUND", 3, "REQUESTED FUNCTION NOT FOUND", "EN") LUNAERROR.adderror("BADFUNCTIONREQUEST", 4, "BAD FUNCTION REQUEST", "EN") LUNAERROR.adderror("FUNCTIONNOTACTIVE", 5, "REQUESTED FUNCTION NOT ACTIVE", "EN") LUNAERROR.adderror("FUNCTIONNEEDSLOGIN", 6, "NOT LOGGED IN", "EN") LUNAERROR.adderror("FUNCTIONEXEC", 7, "ERROR EXECUTING FUNCTION", "EN")