ACF Libraries: Difference between revisions

From Alpine Linux
Line 8: Line 8:
== [[fs.lua]] ==
== [[fs.lua]] ==
Used for various filespecific functions
Used for various filespecific functions
    * is_dir
*is_dir - test if the a directory
    * is_file
*is_file - test if is a file
    * is_link
*is_link - test if file is a link
    * read_file / read_file_as_array
*read_file / read_file_as_array - will read a file as a string / array
    * write_file
*dostounix - converts dos line endings to unix
    * find
*remove_blanks_comments - takes out the blank and commented lines
*write_file - replace contents of a file
*wrie_line_file - appends lines to a file
*find - Give a path and a filename and search for it


== html.lua ==
== html.lua ==

Revision as of 14:54, 22 November 2007

ACF Libraries (Using Lua Posix. Most will go away)

Because of using lua, a very small language, the need arises to build everything from scratch. This can lead to some confusion, apprehension, or just feeling overwhelmed. Here is documentation on what we have now in the form of libraries and common functions. We hope to build some more and use this information to get the juices flowing in regard to ACF.

Some of these are lua libs or ones written for ACF.


fs.lua

Used for various filespecific functions

  • is_dir - test if the a directory
  • is_file - test if is a file
  • is_link - test if file is a link
  • read_file / read_file_as_array - will read a file as a string / array
  • dostounix - converts dos line endings to unix
  • remove_blanks_comments - takes out the blank and commented lines
  • write_file - replace contents of a file
  • wrie_line_file - appends lines to a file
  • find - Give a path and a filename and search for it

html.lua

Functions used by web_elements.lua. Written by nangel for ACF.

cookie.set

INPUT:
This library required the following inputs/parameters.

  • name
  • value
  • path

OUTPUT:
This library deliverers the following output/parameters.

  • String to set the cookie

CODING EXAMPLE:

-- Set variable/Call for this library

html_escape

INPUT:
This library required the following inputs/parameters.

  • text-string

OUTPUT:
This library deliverers the following output/parameters.

  • String with &,>,< signs encoded

CODING EXAMPLE:

-- Set variable/Call for this library
bobo = require "html"
format = bobo.html_escape("This is > a test < string &")
print(format)

This is > a test < string &

nv_pair

INPUT:
This library required the following inputs/parameters.

  • name
  • value

OUTPUT:
This library deliverers the following output/parameters.

  • name="value"

CODING EXAMPLE:

-- Set variable/Call for this library
bobo = require "html"
format = bobo.nv_pair("foo", "bar")
print(format)
foo="bar"

join.lua (not working)

INPUT:
This library required the following inputs/parameters.

  • Delimiter
    • Could be one or more chars.
  • Array
    • Data which is to be joined into a string.

OUTPUT:
This library deliverers the following output/parameters.

  • String
    • Could be something like "Word, Word, Word, Word"

CODING EXAMPLE:

-- Set variable/Call for this library
ourlib = require("join")
-- Create a array of data (Not sure if the next row is correct)
arraytojoin = "Bird", "Fish", "Cow", "Hammer"
-- Process the data (note the delimiter)
liboutput = ourlib(";", arraytojoin)

'liboutput' would contain:

Bird;Fish;Cow;Hammer

log_view.lua

menubuilder.lua

Written by nangel for ACF

pidof.lua

pidof

find the process ID of a running program

pidof.pidof(program)

INPUT

  • program

OUTPUT returns the PID's of running program or nil if there are none.

CODING EXAMPLE

require "pidof"
if pidof.pidof("mini_httpd") ~= nil then
  print("mini_httpd is running")
else
  print("mini_httpd is not running")
end

privsep.lua

drop_privs

Drop privileges while allowing a few functions still have root permissions.

privsep.drop_privs(user, group, functable)

Depends on posix and json.

INPUT:

  • user
  • group
  • functable
    • a table of functions that will run as root

OUTPUT: returns a table identical to functable, except the funcs are wrapped and runs in a separate process with root permissions.

CODING EXAMPLE:

#!/usr/bin/env lua

require "posix" 
require "privsep"

-- register those as privileged funcs 
a = {}
function a.getuid()
        return posix.getpid().euid
end

-- main ---------------------------------------------------------
priv = privsep.drop_privs("nobody", "nogroup", a)
if priv == nil then
        error("failed to drop privileges")
end

print("current uid:", posix.getpid().euid)
print("privileged uid:", priv.getuid())


service_controller.lua

This is comprised of many local functions. Will just go through the exported ones.

create_services_controller

service_model.lua

Has many functions inside the one below.

create_service_model

INPUT

  • cfglist
  • loglist
  • servlist
  • notepath

OUPUT

  • Returns a me table:
    • Looks like it contains information about the service.
      • Processes running
      • If there is an /etc/init.d/ script
      • Where is the programs config file
      • Where does it log to

session.lua

Written by nangel for ACF.

random_hash

Returns a base64 encoded hash, using _- as the extra characters, as these are safe for using in a URL.

INPUT:
hash size, in bits OUTPUT:
A base64 encoded hash of at least bits length.

  • HASH
    • Comes from reading /dev/urandom

CODING EXAMPLE:

-- Set variable/Call for this library
bobo = require "session"
print(bobo.random_hash(100))

will output a hash from /dev/urandom that is 17 char long

hash_ip_address

INPUT:
This library required the following inputs/parameters.

  • ip address

OUTPUT:
This library deliverers the following output/parameters.

  • HEX incoded ip address

CODING EXAMPLE:

-- Set variable/Call for this library
bobo = require "session"
print(bobo.has_ip_address("192.168.10.1))

Output could be:

c0a80a01

ip_addr_from_hash

INPUT:
This library required the following inputs/parameters.

  • HEX encoded ip address

OUTPUT:
This library deliverers the following output/parameters.

  • ip address

CODING EXAMPLE:

-- Set variable/Call for this library
bobo = require "session"
print(bobo.ip_addr_from_hash("c0a80a01")

Output could be:

192.168.10.1

serialize

INPUT:
This library required the following inputs/parameters.

  • name
    • What to use for the new elements
  • value
    • value: table to serialize
  • saved
    • not sure what its used of

OUTPUT:
This library deliverers the following output/parameters.

  • string with the table serialized

CODING EXAMPLE:

-- Set variable/Call for this library
bobo = require "session"
t = {foo={1,2,3,4}, "one", "two", "bar", "baz"}
stuff = bobo.serialize(1,t)
print(stuff)
1 = {}
1[1] = "one"
1[2] = "two"
1[3] = "bar"
1[4] = "baz"
1["foo"] = {}
1["foo"][1] = 1
1["foo"][2] = 2
1["foo"][3] = 3
1["foo"][4] = 4

save_session

INPUT:
This library required the following inputs/parameters.

  • sessionpath,session,sessiontable

OUTPUT:
This library deliverers the following output/parameters.

  • true is success, false if error

CODING EXAMPLE:

-- Set variable/Call for this library
bobo = require "session"
print(bobo.save_session("tmp", session, sessiontable)
-- will print true is success
-- false if failed

split.lua

INPUT:
This library required the following inputs/parameters.

  • Delimiter
    • Could be one or more chars.
  • String or line from a file: bobo~foo~bar~baz~1
    • Data is to be split into a table

OUTPUT:
This library deliverers the following output/parameters.

  • Table
    • { 1 = "bobo", 2 = "foo", 3 = "bar", 4 = "baz", 5 = 1}

CODING EXAMPLE:

-- Set variable/Call for this library
strsplit = require("split")
-- Grab the line from a file or input it into the funtcion
line = "bobo~foo~bar~baz~1"
-- Process the data (note the delimiter)
t = ourlib("~", line)
't' would contain: 
{ 1 = "bobo", 2 = "foo", 3 = "bar", 4 = "baz", 5 = 1}
for a,b in ipairs(t) do print(a,b) end
1 bobo
2 foo
3 bar
4 baz
5 1

validator.lua

This contains multiple different functions that each will validate input in there own way.

is_ipv4 (function)

INPUT:
This library function required the following inputs/parameters.

  • String in form of ip address
    • Example: 192.168.24.10

OUTPUT:
This library function deliverers the following output/parameters.

  • false or "true"

CODING EXAMPLE:

-- Include/Call for this library
require("validator")
-- Check if url is a folder
liboutput = validator.is_ip("192.168.24.10")

If the string is a valid ip 'liboutput' would contain string:

true

If the string is missing or an invalid ip 'liboutput' would contain false.

is_mac (function)

INPUT:
This library function required the following inputs/parameters.

  • String in form of mac address
    • Example: 00:ca:ba:de:00:12

OUTPUT:
This library function deliverers the following output/parameters.

  • false or "true"

CODING EXAMPLE:

-- Include/Call for this library
require("validator")
-- Check if url is a folder
liboutput = validator.is_mac("00:ca:ba:de:00:12")

If the string is a valid mac address 'liboutput' would contain string:

true

If the string is missing or an invalid ip 'liboutput' would contain false.

is_integer(function)

INPUT:
This library function required the following inputs/parameters.

  • Number
    • Example: 100

OUTPUT:
This library function deliverers the following output/parameters.

  • false or "true"

CODING EXAMPLE:

-- Include/Call for this library
require("validator")
-- Check if url is a folder
liboutput = validator.is_integer("100")

If the string is a valid ip 'liboutput' would contain string:

true

If the string is missing or an invalid ip 'liboutput' would contain false.

is_integer_in_range (function)

INPUT:
This library function required the following inputs/parameters.

  • 3 numbers --- number to test, min, max
    • Example: (10,5,12)

OUTPUT:
This library function deliverers the following output/parameters.

  • false or "true"

CODING EXAMPLE:

-- Include/Call for this library
require("validator")
-- Check if url is a folder
liboutput = validator.is_integer_in_range(10,5,15)

If the string is a valid ip 'liboutput' would contain string:

true

If the string is missing or an invalid ip 'liboutput' would contain false.

is_port (function)

INPUT:
This library function required the following inputs/parameters.

  • Number - is it between 1 and 65535
    • Example: 45

OUTPUT:
This library function deliverers the following output/parameters.

  • false or "true"

CODING EXAMPLE:

-- Include/Call for this library
require("validator")
-- Check if url is a folder
liboutput = validator.is_port("45")

If the string is a valid ip 'liboutput' would contain string:

true

If the string is missing or an invalid ip 'liboutput' would contain false.

web_elements.lua

Written by nangel for ACF.

render_table

INPUT:
This library function required the following inputs/parameters.

  • Element
    • The element should be type 'table'
    • Element could be: group, label, html, log, link, form
  • Level
    • Level should be a 'number'

OUTPUT:
This library function deliverers the following output/parameters.

  • HTML
    • HTML formated output/elements
    • The output is partially generated by html.lua

render_mainmenu

INPUT:
This library function required the following inputs/parameters.

  • menu
  • prefix
  • controller
  • action

OUTPUT:
This library function deliverers the following output/parameters.

  • MainMenu (HTML)
    • HTML formated output/elements
    • The output is partially generated by html.lua

render_submenu

INPUT:
This library function required the following inputs/parameters.

  • menu
  • group
  • cat
  • subcat

OUTPUT:
This library function deliverers the following output/parameters.

  • SubMenu (HTML)
    • HTML formated output/elements
    • The output is partially generated by html.lua