summaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorNatanael Copa <natanael.copa@gmail.com>2007-07-27 12:53:38 +0000
committerNatanael Copa <natanael.copa@gmail.com>2007-07-27 12:53:38 +0000
commitdc53423183a0c459284ebd139022b707f01af006 (patch)
tree8a67a2904ec991028bddd429d57eec114b05baab /app
parent275c80281ba2e84b8d810bdb1c2b7f8c9a4333d9 (diff)
downloadacf-core-dc53423183a0c459284ebd139022b707f01af006.tar.bz2
acf-core-dc53423183a0c459284ebd139022b707f01af006.tar.xz
moved core files to new dir structurev2.0_alpha1
git-svn-id: svn://svn.alpinelinux.org/acf/core/trunk@219 ab2d0c66-481e-0410-8bed-d214d4d58bed
Diffstat (limited to 'app')
-rw-r--r--app/Makefile49
-rw-r--r--app/README48
-rw-r--r--app/acf-util/logon-controller.lua60
-rw-r--r--app/acf-util/logon-html.lsp19
-rw-r--r--app/acf-util/logon-model.lua61
-rw-r--r--app/acf_www-controller.lua202
-rw-r--r--app/dummy/bar-controller.lua35
-rw-r--r--app/dummy/bar-html.lsp3
-rw-r--r--app/dummy/bar.menu3
-rw-r--r--app/foo-controller.lua36
-rw-r--r--app/foo-html.lsp3
-rw-r--r--app/foo.menu2
-rw-r--r--app/menuhints.menu8
-rw-r--r--app/template-html.lsp99
-rw-r--r--app/welcome-controller.lua27
-rw-r--r--app/welcome-html.lsp3
-rw-r--r--app/welcome.menu1
17 files changed, 659 insertions, 0 deletions
diff --git a/app/Makefile b/app/Makefile
new file mode 100644
index 0000000..4c12bea
--- /dev/null
+++ b/app/Makefile
@@ -0,0 +1,49 @@
+include ../config.mk
+
+APP_DIST= acf-util/logon-controller.lua\
+ acf-util/logon-html.lsp\
+ acf-util/logon-model.lua\
+ dummy/bar-controller.lua\
+ dummy/bar-html.lsp\
+ dummy/bar.menu\
+ acf_www-controller.lua\
+ foo-controller.lua\
+ foo-html.lsp\
+ foo.menu\
+ menuhints.menu\
+ template-html.lsp\
+ welcome-controller.lua\
+ welcome-html.lsp\
+ welcome.menu\
+
+EXTRA_DIST=README Makefile
+DISTFILES=$(APP_DIST) $(EXTRA_DIST)
+
+install_dir=$(DESTDIR)/$(appdir)
+
+phony+=all
+all:
+
+phony+=clean
+clean:
+
+phony+=distdir
+distdir: $(DISTFILES)
+ mkdir -p "$(DISTDIR)/app"
+ for i in $(DISTFILES); do\
+ dest=`dirname "$(DISTDIR)/app/$$i"`;\
+ mkdir -p "$$dest";\
+ cp "$$i" "$$dest";\
+ done
+
+phony+=install
+install:
+ mkdir -p $(install_dir)
+ for i in $(APP_DIST); do\
+ dest=`dirname "$(install_dir)/$$i"`;\
+ mkdir -p "$$dest";\
+ cp "$$i" "$$dest";\
+ done
+
+
+.PHONY: $(phony)
diff --git a/app/README b/app/README
new file mode 100644
index 0000000..83e69af
--- /dev/null
+++ b/app/README
@@ -0,0 +1,48 @@
+Each directory has a collection of controllers, models and views
+for a specific package.
+
+
+How to make a new package
+-------------------------
+
+Create a directory for your package with the package name.
+In this dir you put your controller, model, menu files.
+
+Create a Makefile that describes the the following:
+
+ * package name
+
+ * version of package
+
+ * what files should be installed on the running system
+
+ * what additional files should be included in the distribution package
+ (tar.gz source package). The Makefile and acf-pkg.mk wil automatically be
+ added so only extra files like README and ChangeLog needs to be specified
+ here.
+
+ * just include the acf-pkg.mk makefile which contains the rules to install
+ the package and build the dist package-
+
+If you have an acf-foo package it should look like this:
+
+PACAKGE = foo
+VERSION = 0.2.0
+INSTALL_FILES = foo.controller.lua foo.model.lua
+EXTRA_DIST = ChangeLog README
+include acf-pkg.mk
+
+
+Then you just need to create a link to ../acf-pkg.mk
+
+ ln -s ../acf-pkg.mk acf-pkg.mk
+
+To build the distribution package you then run:
+
+ make dist
+
+To install the files in the running system:
+
+ make install
+
+
diff --git a/app/acf-util/logon-controller.lua b/app/acf-util/logon-controller.lua
new file mode 100644
index 0000000..8359c18
--- /dev/null
+++ b/app/acf-util/logon-controller.lua
@@ -0,0 +1,60 @@
+-- Logon / Logoff functions
+
+module (..., package.seeall)
+
+require ("session")
+
+mvc.on_load = function(self, parent)
+ -- If they specify an invalid action or try to run init, then redirect
+ -- to the read function.
+ if ( self.conf.action == nil or self.conf.action == "init" ) then
+ -- do what?
+ end
+
+end
+
+
+logon = function(self)
+
+ local username=cfe({ name="username" })
+ local password=cfe({ name="password" })
+ local logon=cfe({ name="Logon", type="submit"})
+ local s = ""
+
+ if self.clientdata.username and self.clientdata.password then
+ if self.model.logon(self, self.clientdata.username, self.clientdata.password) == false then
+ username.value = self.clientdata.username
+ if self.session.id then
+ username.errtxt = "You are already logged in. Logout first."
+ else
+ username.errtxt = "There was a problem logging in"
+ end
+ else
+ self.conf.controller = ""
+ self.conf.action = ""
+ self.conf.prefix = ""
+ self.conf.type = "redir"
+ error(self.conf)
+ end
+ end
+ -- If we reach this point, just give them the login page
+ return ( cfe ({type="form",
+ option={ script=ENV["SCRIPT_NAME"],
+ prefix=self.conf.prefix,
+ controller = self.conf.controller,
+ action = "logon" },
+ value = { username, password, logon } }))
+end
+
+
+logout = function(self)
+ self.model.logout(self, session.id)
+
+
+ -- and raise an error to go to the homepage
+ self.conf.action = ""
+ self.conf.prefix = ""
+ self.conf.controller = ""
+ self.conf.type = "redir"
+ error(self.conf)
+end
diff --git a/app/acf-util/logon-html.lsp b/app/acf-util/logon-html.lsp
new file mode 100644
index 0000000..cdac2bf
--- /dev/null
+++ b/app/acf-util/logon-html.lsp
@@ -0,0 +1,19 @@
+<? local form = ... ?>
+<h1>Logon</h1>
+
+<form action="<?= form.option.script .. form.option.prefix ..
+ form.option.controller .. "/" .. form.option.action ?>" method="POST">
+<table>
+<? local myform = form.value
+ for k,v in pairs(myform) do ?>
+<tr><td><?= v.name ?></td><td>
+<? if v.type == "submit" then ?>
+ <input type="submit" name="<?= v.name ?>" value="Logon">
+<? else ?>
+ <input type="text" name="<?= v.name ?>">
+ <font color=red><?= v.errtxt ?></font>
+<? end ?>
+</td></tr>
+<? end ?>
+</table>
+</form>
diff --git a/app/acf-util/logon-model.lua b/app/acf-util/logon-model.lua
new file mode 100644
index 0000000..dbd8522
--- /dev/null
+++ b/app/acf-util/logon-model.lua
@@ -0,0 +1,61 @@
+-- Logon / Logoff model functions
+
+module (..., package.seeall)
+
+local sess = require ("session")
+
+local pvt = {}
+
+
+-- return a sessionid if username / password is valid, false
+-- /etc/acf/passwd should be lines of userid:passwd:user name:role1[,role2[,role3]]
+pvt.logon = function (self, id, passwd )
+ -- if we already have sessionid... then you are already logged in
+ if self.session.id then
+ return false
+ end
+
+ id = id or ""
+ passwd = passwd or ""
+
+ -- open our hokey password file,
+ local f = io.open(self.conf.confdir .. "/passwd" )
+ if f then
+ m = f:read("*all") .. "\n"
+ f:close()
+
+ for l in string.gmatch(m, "(%C*)\n") do
+ local userid, password, username, roles =
+ string.match(l, "([^:]*):([^:]*):([^:]*):(.*)")
+ if userid == id and password == passwd then
+ self.session.id = sess.random_hash(512)
+ self.session.name = username
+ self.session.roles = roles
+ break
+ end
+ end
+ end
+ if self.session.id then
+ local x = require("session")
+ x.save_session(self.conf.sessiondir, self.session.id, self.session)
+ x=nil
+ return self.session.id
+ else
+ return false
+ end
+end
+
+-- invalidate the session, or return false if the session wasn't valid
+pvt.logout = function (self, sessionid)
+
+ sess.invalidate_session ( self.conf.sessiondir, sessionid)
+ self.session = {}
+
+end
+
+-------------------------------------------------------------------------
+-- Public Methods
+-------------------------------------------------------------------------
+
+logon = pvt.logon
+logout = pvt.logout
diff --git a/app/acf_www-controller.lua b/app/acf_www-controller.lua
new file mode 100644
index 0000000..742533a
--- /dev/null
+++ b/app/acf_www-controller.lua
@@ -0,0 +1,202 @@
+--[[ Code for the Alpine Configuration WEB framework
+ Written for Alpine Configuration Framework (ACF) -- see www.alpinelinux.org
+ Copyright (C) 2007 Nathan Angelacos
+ Licensed under the terms of GPL2
+ ]]--
+module(..., package.seeall)
+
+-- We use the parent exception handler in a last-case situation
+local parent_exception_handler
+
+mvc = {}
+mvc.on_load = function (self, parent)
+
+ -- Make sure we have some kind of sane defaults for libdir and sessiondir
+ self.conf.libdir = self.conf.libdir or ( self.conf.appdir .. "/lib/" )
+ self.conf.sessiondir = self.conf.sessiondir or "/tmp/"
+ self.conf.appuri = "http://" .. ENV.HTTP_HOST .. ENV.SCRIPT_NAME
+ self.conf.default_controller = "welcome"
+ self.clientdata = FORM
+
+
+ parent_exception_handler = parent.exception_handler
+
+ -- this sets the package path for us and our children
+ package.path= self.conf.libdir .. "?.lua;" .. package.path
+
+ self.session = {}
+ local x=require("session")
+ if FORM.sessionid then
+ local timestamp
+ timestamp , self.session = x.load_session(self.conf.sessiondir , FORM.sessionid)
+ self.session.id = FORM.sessionid
+ else
+ self.session.id = nil
+ end
+ logit ("acf_www-controller mvc.on_load activated" )
+end
+
+mvc.pre_exec = function ()
+ logit ("acf_www-controller mvc.pre_exec activated")
+end
+
+mvc.post_exec = function ()
+ logit ("acf_www-controller mvc.post_exec activated")
+end
+
+
+-- look for a template
+-- ctlr-action-view, then ctlr-view, then action-view, then view
+
+find_template = function ( appdir, prefix, controller, action, viewtype )
+ local targets = {
+ appdir .. prefix .. "template-" .. controller .. "-" ..
+ action .. "-" .. viewtype .. ".lsp",
+ appdir .. prefix .. "template-" .. controller .. "-" ..
+ viewtype .. ".lsp",
+ appdir .. prefix .. "template-" .. action .. "-" ..
+ viewtype .. ".lsp",
+ appdir .. prefix .. "template-" .. viewtype .. ".lsp"
+ }
+ local file
+ for k,v in pairs(targets) do
+ file = io.open (v)
+ if file then
+ io.close (file)
+ return v
+ end
+ end
+ -- not found, so try one level higher
+ if prefix == "" then -- already at the top level - fail
+ return false
+ end
+ prefix = dirname (prefix)
+ return find_template ( appdir, prefix, controller, action, viewtype )
+end
+
+
+
+-- Overload the MVC's view resolver with our own
+
+view_resolver = function(self)
+ local file
+ local viewname
+ local viewtype = self.conf.viewtype or "html"
+ local names = { self.conf.appdir .. self.conf.prefix .. self.conf.controller ..
+ "-" .. self.conf.action .. "-" .. viewtype .. ".lsp",
+ self.conf.appdir .. self.conf.prefix .. self.conf.controller ..
+ "-" .. viewtype .. ".lsp" }
+
+
+ -- FIXME: MVC doesn't have a way to call a function after the controller is run
+ -- so we serialize the session in the view resolver. MVC probably should have
+ -- a postinit or postrun method...
+ if self.session.id then
+ local x = require("session")
+ x.save_session(self.conf.sessiondir, self.session.id, self.session)
+ x=nil
+ end
+
+ -- search for template
+ local template = find_template ( self.conf.appdir, self.conf.prefix,
+ self.conf.controller, self.conf.action, "html")
+
+ -- search for view
+ for i,filename in ipairs (names) do
+ file = io.open(filename)
+ if file then
+ file:close()
+ viewname = filename
+ break
+ end
+ end
+
+ -- We have a template
+ if template then
+ -- ***************************************************
+ -- This is how to call another controller (APP or self
+ -- can be used... m will contain worker and model,
+ -- with conf, and other "missing" parts pointing back
+ -- to APP or self
+ -- ***************************************************
+ local m = self:new("alpine-baselayout/hostname")
+ local h = m.worker.read(m)
+
+ local pageinfo = { viewfile = viewname,
+ controller = m.conf.controller,
+ -- ^^^ see.. m.conf doesnt exist - but it works
+ action = self.conf.action,
+ hostname = h.hostname.value,
+ prefix = self.conf.prefix,
+ script = self.conf.appuri
+ }
+
+ -- Build the menu table
+ m=require("menubuilder")
+ local menu = m.get_menuitems(self.conf.appdir)
+
+ -- A quick hack
+ submenu = {}
+ for k,v in pairs ( self.worker ) do
+ if type ( self.worker[k] ) == "function" then
+ table.insert (submenu, k)
+ end
+ end
+
+ return function (viewtable)
+ local template = haserl.loadfile (template)
+ return template ( pageinfo, menu, submenu, viewtable, self.session )
+ end
+ end
+
+ -- No template, but have a view
+ if viewname then
+ return haserl.loadfile (viewname)
+ else
+ return function() end
+ end
+end
+
+exception_handler = function (self, message )
+ local html = require ("html")
+ if type(message) == "table" then
+ if message.type == "redir" then
+ io.write ("Status: 302 Moved\n")
+ io.write ("Location: " .. ENV["SCRIPT_NAME"] ..
+ message.prefix .. message.controller ..
+ "/" .. message.action ..
+ (message.extra or "" ) .. "\n")
+ if self.session.id then
+ io.write (html.cookie.set("sessionid", self.session.id))
+ else
+ io.write (html.cookie.unset("sessionid"))
+ end
+ io.write ( "Content-Type: text/html\n\n" )
+ elseif message.type == "dispatch" then
+ parent_exception_handler(self, message)
+ end
+ else
+ parent_exception_handler( self, message)
+ end
+end
+
+-- create a Configuration Framework Entity (cfe)
+-- returns a table with at least "value", "type", "option" and "errtxt"
+cfe = function ( optiontable )
+ optiontable = optiontable or {}
+ me = { value="",
+ type="text",
+ option="",
+ errtxt="",
+ name="" }
+ for key,value in pairs(optiontable) do
+ me[key] = value
+ end
+ return me
+end
+
+
+-- syslog something
+logit = function ( ... )
+ os.execute ( "logger \"" .. ... .. "\"" )
+end
diff --git a/app/dummy/bar-controller.lua b/app/dummy/bar-controller.lua
new file mode 100644
index 0000000..e0b9999
--- /dev/null
+++ b/app/dummy/bar-controller.lua
@@ -0,0 +1,35 @@
+-- A standin controller for testing
+
+module (..., package.seeall)
+
+-- Cause an http redirect to our "read" action
+-- We use the self.conf table because it already has prefix,controller,etc
+-- The redir code is defined in the application error handler (acf-controller)
+local list_redir = function (self)
+ self.conf.action = "read"
+ self.conf.type = "redir"
+ error (self.conf)
+end
+
+
+--init ( init by definition is a public method)
+init = function(self, parent)
+ -- If they specify an invalid action or try to run init, then redirect
+ -- to the read function.
+ if ( self.conf.action == nil or self.conf.action == "init" ) then
+ list_redir(self)
+ end
+ -- Make a function by the action name
+ self.worker[self.conf.action] = function ()
+ return cfe ( {name=self.conf.action, value=self.conf.action} )
+
+ end
+
+end
+
+
+read = function (self )
+ return ( { } )
+end
+
+
diff --git a/app/dummy/bar-html.lsp b/app/dummy/bar-html.lsp
new file mode 100644
index 0000000..708e1ad
--- /dev/null
+++ b/app/dummy/bar-html.lsp
@@ -0,0 +1,3 @@
+<? view = ... ?>
+<h1>Action <?= view.name ?></h1>
+<p>This is a null controller for menu and authorization testing.</p>
diff --git a/app/dummy/bar.menu b/app/dummy/bar.menu
new file mode 100644
index 0000000..1fb42b8
--- /dev/null
+++ b/app/dummy/bar.menu
@@ -0,0 +1,3 @@
+# Prefix and controller are already known at this point
+# Cat Group Tab Action
+Test Bar_controller bar read
diff --git a/app/foo-controller.lua b/app/foo-controller.lua
new file mode 100644
index 0000000..f25cbaf
--- /dev/null
+++ b/app/foo-controller.lua
@@ -0,0 +1,36 @@
+-- A standin controller for testing
+
+module (..., package.seeall)
+
+x = require ("session")
+
+-- Cause an http redirect to our "read" action
+-- We use the self.conf table because it already has prefix,controller,etc
+-- The redir code is defined in the application error handler (acf-controller)
+local list_redir = function (self)
+ self.conf.action = "read"
+ self.conf.type = "redir"
+ error (self.conf)
+end
+
+
+mvc.on_load = function(self, parent)
+ -- If they specify an invalid action or try to run init, then redirect
+ -- to the read function.
+ if ( self.conf.action == nil or self.conf.action == "init" ) then
+ list_redir(self)
+ end
+ -- Make a function by the action name
+ self.worker[self.conf.action] = function ()
+ return cfe ( {name=self.conf.action, value=self.conf.action} )
+
+ end
+
+end
+
+
+read = function (self )
+ return ( { } )
+end
+
+
diff --git a/app/foo-html.lsp b/app/foo-html.lsp
new file mode 100644
index 0000000..708e1ad
--- /dev/null
+++ b/app/foo-html.lsp
@@ -0,0 +1,3 @@
+<? view = ... ?>
+<h1>Action <?= view.name ?></h1>
+<p>This is a null controller for menu and authorization testing.</p>
diff --git a/app/foo.menu b/app/foo.menu
new file mode 100644
index 0000000..e9244f0
--- /dev/null
+++ b/app/foo.menu
@@ -0,0 +1,2 @@
+#cat Group tab action
+Test foo_controller foo read
diff --git a/app/menuhints.menu b/app/menuhints.menu
new file mode 100644
index 0000000..5d36abb
--- /dev/null
+++ b/app/menuhints.menu
@@ -0,0 +1,8 @@
+# This gives basic priorities to the groups in the menu
+# That way, individual menu files don't have to say "80Setup" everywhere
+# Note, however, that if ANY individual menu sets Setup to less than 80, then
+# That number will override the defaults set here.
+# (i.e. Lowest specifically set priority wins)
+10Home
+80Setup
+100Test
diff --git a/app/template-html.lsp b/app/template-html.lsp
new file mode 100644
index 0000000..784bb4e
--- /dev/null
+++ b/app/template-html.lsp
@@ -0,0 +1,99 @@
+<? local pageinfo , mainmenu, submenu, viewtable, session = ...
+ html=require("html") ?>
+Status: 200 OK
+Content-Type: text/html
+<? if (session.id) then
+ io.write( html.cookie.set("sessionid", session.id) )
+ else
+ io.write (html.cookie.unset("sessionid"))
+ end
+?>
+
+<!DOCTYPE HTML PUBLIC "-//W3C//DDD HTML 4.01 Transitional//EN">
+<html lang="en">
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
+<title><?= pageinfo.hostname .. " - " .. pageinfo.controller .. "->" .. pageinfo.action ?></title>
+<link rel="stylesheet" type="text/css" href="/static/webconf.css">
+</head>
+<body>
+
+<div id=head>
+<h1>Alpine</h1>
+<p>Host: <em><?= pageinfo.hostname ?></em></p>
+<? -- a quick hack
+ local ctlr = pageinfo.script .. "/acf-util/logon/"
+?>
+<? if session.id == nil then
+ io.write ( html.link( { label = "Log in", value = ctlr .. "logon" } ) )
+ else
+ io.write (html.link( { label = "Logout as " .. ( session.name or "unkown") , value = ctlr .. "logout" } ) )
+ end ?></p>
+</div>
+
+<div id="mainmenu">
+<?
+ -- FIXME: This needs to go in a library function somewhere (menubuilder?)
+ local cat, group
+ local liston=false
+ local selected
+ for k,v in ipairs(mainmenu) do
+ if v.cat ~= cat then
+ if liston == true then
+ io.write ("</ul>\n")
+ liston=false
+ end
+ cat = v.cat
+ io.write (string.format("<h3>%s</h3>\n", cat))
+ group = ""
+ end
+ if v.group ~= group then
+ group = v.group
+ if liston == false then
+ io.write ("<ul>")
+ liston=true
+ end
+ if pageinfo.prefix == v.prefix .. "/" and
+ pageinfo.controller == v.controller then
+ selected=" id=\"selected\""
+ else
+ selected=""
+ end
+ io.write (string.format("<li%s><a href=\"%s%s/%s/%s\">%s</a></li>\n",
+ selected, ENV.SCRIPT_NAME,v.prefix, v.controller, v.action, v.group))
+ end
+ end
+?>
+</ul>
+</div>
+
+
+<div id="submenu">
+<h2><?= pageinfo.prefix ?> > <?= pageinfo.controller .. " > " .. pageinfo.action ?></h2>
+<ul>
+<? for k,v in pairs(submenu) do
+ if v == pageinfo.action then
+ io.write (string.format('<li id="selected">%s</li>\n',
+ v, v ))
+ else
+ io.write (string.format('<li><a href="%s">%s</a></li>\n',
+ v, v ))
+ end
+ end
+?>
+</ul>
+</div>
+
+
+
+<div id="content">
+<? local func = haserl.loadfile(pageinfo.viewfile)
+ func (viewtable) ?>
+</div>
+
+
+<div id="footer">
+<p><center>Made with care by acf</center></p>
+</div>
+</body>
+</html>
diff --git a/app/welcome-controller.lua b/app/welcome-controller.lua
new file mode 100644
index 0000000..450952c
--- /dev/null
+++ b/app/welcome-controller.lua
@@ -0,0 +1,27 @@
+-- A standin controller for testing
+
+module (..., package.seeall)
+
+-- Cause an http redirect to our "read" action
+-- We use the self.conf table because it already has prefix,controller,etc
+-- The redir code is defined in the application error handler (acf-controller)
+local list_redir = function (self)
+ self.conf.action = "read"
+ self.conf.type = "redir"
+ error (self.conf)
+end
+
+mvc = {}
+mvc.on_load = function(self, parent)
+ -- It doesn't matter what action they choose - we only support read
+ if ( self.conf.action ~= "read") then
+ list_redir(self)
+ end
+end
+
+
+read = function (self )
+ return ( { } )
+end
+
+
diff --git a/app/welcome-html.lsp b/app/welcome-html.lsp
new file mode 100644
index 0000000..7f205fe
--- /dev/null
+++ b/app/welcome-html.lsp
@@ -0,0 +1,3 @@
+<? view = ... ?>
+<h1>Alpine Configuration Framework</h1>
+<p>Welcome.</p>
diff --git a/app/welcome.menu b/app/welcome.menu
new file mode 100644
index 0000000..51c25f1
--- /dev/null
+++ b/app/welcome.menu
@@ -0,0 +1 @@
+10Home Welcome Welcome Read