123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- -- Copyright 2010 Jo-Philipp Wich <jow@openwrt.org>
- -- Licensed to the public under the Apache License 2.0.
- local fs = require "nixio.fs"
- local ip = require "luci.ip"
- local math = require "math"
- local util = require "luci.util"
- local tonumber, tostring, type, unpack, select = tonumber, tostring, type, unpack, select
- module "luci.cbi.datatypes"
- _M['or'] = function(v, ...)
- local i
- for i = 1, select('#', ...), 2 do
- local f = select(i, ...)
- local a = select(i+1, ...)
- if type(f) ~= "function" then
- if f == v then
- return true
- end
- i = i - 1
- elseif f(v, unpack(a)) then
- return true
- end
- end
- return false
- end
- _M['and'] = function(v, ...)
- local i
- for i = 1, select('#', ...), 2 do
- local f = select(i, ...)
- local a = select(i+1, ...)
- if type(f) ~= "function" then
- if f ~= v then
- return false
- end
- i = i - 1
- elseif not f(v, unpack(a)) then
- return false
- end
- end
- return true
- end
- function neg(v, ...)
- return _M['or'](v:gsub("^%s*!%s*", ""), ...)
- end
- function list(v, subvalidator, subargs)
- if type(subvalidator) ~= "function" then
- return false
- end
- local token
- for token in v:gmatch("%S+") do
- if not subvalidator(token, unpack(subargs)) then
- return false
- end
- end
- return true
- end
- function bool(val)
- if val == "1" or val == "yes" or val == "on" or val == "true" then
- return true
- elseif val == "0" or val == "no" or val == "off" or val == "false" then
- return true
- elseif val == "" or val == nil then
- return true
- end
- return false
- end
- function uinteger(val)
- local n = tonumber(val)
- if n ~= nil and math.floor(n) == n and n >= 0 then
- return true
- end
- return false
- end
- function integer(val)
- local n = tonumber(val)
- if n ~= nil and math.floor(n) == n then
- return true
- end
- return false
- end
- function ufloat(val)
- local n = tonumber(val)
- return ( n ~= nil and n >= 0 )
- end
- function float(val)
- return ( tonumber(val) ~= nil )
- end
- function ipaddr(val)
- return ip4addr(val) or ip6addr(val)
- end
- function ip4addr(val)
- if val then
- return ip.IPv4(val) and true or false
- end
- return false
- end
- function ip4prefix(val)
- val = tonumber(val)
- return ( val and val >= 0 and val <= 32 )
- end
- function ip6addr(val)
- if val then
- return ip.IPv6(val) and true or false
- end
- return false
- end
- function ip6prefix(val)
- val = tonumber(val)
- return ( val and val >= 0 and val <= 128 )
- end
- function port(val)
- val = tonumber(val)
- return ( val and val >= 0 and val <= 65535 )
- end
- function portrange(val)
- local p1, p2 = val:match("^(%d+)%-(%d+)$")
- if p1 and p2 and port(p1) and port(p2) then
- return true
- else
- return port(val)
- end
- end
- function macaddr(val)
- if val and val:match(
- "^[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+:" ..
- "[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+$"
- ) then
- local parts = util.split( val, ":" )
- for i = 1,6 do
- parts[i] = tonumber( parts[i], 16 )
- if parts[i] < 0 or parts[i] > 255 then
- return false
- end
- end
- return true
- end
- return false
- end
- function hostname(val)
- if val and (#val < 254) and (
- val:match("^[a-zA-Z_]+$") or
- (val:match("^[a-zA-Z0-9_][a-zA-Z0-9_%-%.]*[a-zA-Z0-9]$") and
- val:match("[^0-9%.]"))
- ) then
- return true
- end
- return false
- end
- function host(val, ipv4only)
- return hostname(val) or ((ipv4only == 1) and ip4addr(val)) or ((not (ipv4only == 1)) and ipaddr(val))
- end
- function network(val)
- return uciname(val) or host(val)
- end
- function hostport(val, ipv4only)
- local h, p = val:match("^([^:]+):([^:]+)$")
- return not not (h and p and host(h, ipv4only) and port(p))
- end
- function ip4addrport(val, bracket)
- local h, p = val:match("^([^:]+):([^:]+)$")
- return (h and p and ip4addr(h) and port(p))
- end
- function ip4addrport(val)
- local h, p = val:match("^([^:]+):([^:]+)$")
- return (h and p and ip4addr(h) and port(p))
- end
- function ipaddrport(val, bracket)
- local h, p = val:match("^([^%[%]:]+):([^:]+)$")
- if (h and p and ip4addr(h) and port(p)) then
- return true
- elseif (bracket == 1) then
- h, p = val:match("^%[(.+)%]:([^:]+)$")
- if (h and p and ip6addr(h) and port(p)) then
- return true
- end
- end
- h, p = val:match("^([^%[%]]+):([^:]+)$")
- return (h and p and ip6addr(h) and port(p))
- end
- function wpakey(val)
- if #val == 64 then
- return (val:match("^[a-fA-F0-9]+$") ~= nil)
- else
- return (#val >= 8) and (#val <= 63)
- end
- end
- function wepkey(val)
- if val:sub(1, 2) == "s:" then
- val = val:sub(3)
- end
- if (#val == 10) or (#val == 26) then
- return (val:match("^[a-fA-F0-9]+$") ~= nil)
- else
- return (#val == 5) or (#val == 13)
- end
- end
- function string(val)
- return true -- Everything qualifies as valid string
- end
- function directory( val, seen )
- local s = fs.stat(val)
- seen = seen or { }
- if s and not seen[s.ino] then
- seen[s.ino] = true
- if s.type == "dir" then
- return true
- elseif s.type == "lnk" then
- return directory( fs.readlink(val), seen )
- end
- end
- return false
- end
- function file( val, seen )
- local s = fs.stat(val)
- seen = seen or { }
- if s and not seen[s.ino] then
- seen[s.ino] = true
- if s.type == "reg" then
- return true
- elseif s.type == "lnk" then
- return file( fs.readlink(val), seen )
- end
- end
- return false
- end
- function device( val, seen )
- local s = fs.stat(val)
- seen = seen or { }
- if s and not seen[s.ino] then
- seen[s.ino] = true
- if s.type == "chr" or s.type == "blk" then
- return true
- elseif s.type == "lnk" then
- return device( fs.readlink(val), seen )
- end
- end
- return false
- end
- function uciname(val)
- return (val:match("^[a-zA-Z0-9_]+$") ~= nil)
- end
- function range(val, min, max)
- val = tonumber(val)
- min = tonumber(min)
- max = tonumber(max)
- if val ~= nil and min ~= nil and max ~= nil then
- return ((val >= min) and (val <= max))
- end
- return false
- end
- function min(val, min)
- val = tonumber(val)
- min = tonumber(min)
- if val ~= nil and min ~= nil then
- return (val >= min)
- end
- return false
- end
- function max(val, max)
- val = tonumber(val)
- max = tonumber(max)
- if val ~= nil and max ~= nil then
- return (val <= max)
- end
- return false
- end
- function rangelength(val, min, max)
- val = tostring(val)
- min = tonumber(min)
- max = tonumber(max)
- if val ~= nil and min ~= nil and max ~= nil then
- return ((#val >= min) and (#val <= max))
- end
- return false
- end
- function minlength(val, min)
- val = tostring(val)
- min = tonumber(min)
- if val ~= nil and min ~= nil then
- return (#val >= min)
- end
- return false
- end
- function maxlength(val, max)
- val = tostring(val)
- max = tonumber(max)
- if val ~= nil and max ~= nil then
- return (#val <= max)
- end
- return false
- end
- function phonedigit(val)
- return (val:match("^[0-9\*#!%.]+$") ~= nil)
- end
- function timehhmmss(val)
- return (val:match("^[0-6][0-9]:[0-6][0-9]:[0-6][0-9]$") ~= nil)
- end
- function dateyyyymmdd(val)
- if val ~= nil then
- yearstr, monthstr, daystr = val:match("^(%d%d%d%d)-(%d%d)-(%d%d)$")
- if (yearstr == nil) or (monthstr == nil) or (daystr == nil) then
- return false;
- end
- year = tonumber(yearstr)
- month = tonumber(monthstr)
- day = tonumber(daystr)
- if (year == nil) or (month == nil) or (day == nil) then
- return false;
- end
- local days_in_month = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
- local function is_leap_year(year)
- return (year % 4 == 0) and ((year % 100 ~= 0) or (year % 400 == 0))
- end
- function get_days_in_month(month, year)
- if (month == 2) and is_leap_year(year) then
- return 29
- else
- return days_in_month[month]
- end
- end
- if (year < 2015) then
- return false
- end
- if ((month == 0) or (month > 12)) then
- return false
- end
- if ((day == 0) or (day > get_days_in_month(month, year))) then
- return false
- end
- return true
- end
- return false
- end
|