1 -- Copyright 2010 Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
4 local fs = require "nixio.fs"
5 local ip = require "luci.ip"
6 local math = require "math"
7 local util = require "luci.util"
8 local tonumber, tostring, type, unpack, select = tonumber, tostring, type, unpack, select
11 module "luci.cbi.datatypes"
14 _M['or'] = function(v, ...)
16 for i = 1, select('#', ...), 2 do
17 local f = select(i, ...)
18 local a = select(i+1, ...)
19 if type(f) ~= "function" then
24 elseif f(v, unpack(a)) then
31 _M['and'] = function(v, ...)
33 for i = 1, select('#', ...), 2 do
34 local f = select(i, ...)
35 local a = select(i+1, ...)
36 if type(f) ~= "function" then
41 elseif not f(v, unpack(a)) then
49 return _M['or'](v:gsub("^%s*!%s*", ""), ...)
52 function list(v, subvalidator, subargs)
53 if type(subvalidator) ~= "function" then
57 for token in v:gmatch("%S+") do
58 if not subvalidator(token, unpack(subargs)) then
66 if val == "1" or val == "yes" or val == "on" or val == "true" then
68 elseif val == "0" or val == "no" or val == "off" or val == "false" then
70 elseif val == "" or val == nil then
77 function uinteger(val)
78 local n = tonumber(val)
79 if n ~= nil and math.floor(n) == n and n >= 0 then
87 local n = tonumber(val)
88 if n ~= nil and math.floor(n) == n then
96 local n = tonumber(val)
97 return ( n ~= nil and n >= 0 )
101 return ( tonumber(val) ~= nil )
105 return ip4addr(val) or ip6addr(val)
108 function ip4addr(val)
110 return ip.IPv4(val) and true or false
116 function ip4prefix(val)
118 return ( val and val >= 0 and val <= 32 )
121 function ip6addr(val)
123 return ip.IPv6(val) and true or false
129 function ip6prefix(val)
131 return ( val and val >= 0 and val <= 128 )
135 return ipmask4(val) or ipmask6(val)
138 function ipmask4(val)
139 local ip, mask = val:match("^([^/]+)/([^/]+)$")
140 local bits = tonumber(mask)
142 if bits and (bits < 0 or bits > 32) then
146 if not bits and mask and not ip4addr(mask) then
150 return ip4addr(ip or val)
153 function ipmask6(val)
154 local ip, mask = val:match("^([^/]+)/([^/]+)$")
155 local bits = tonumber(mask)
157 if bits and (bits < 0 or bits > 128) then
161 if not bits and mask and not ip6addr(mask) then
165 return ip6addr(ip or val)
168 function ip6hostid(val)
169 if val and val:match("^[a-fA-F0-9:]+$") and (#val > 2) then
170 return (ip6addr("2001:db8:0:0" .. val) or ip6addr("2001:db8:0:0:" .. val))
178 return ( val and val >= 0 and val <= 65535 )
181 function portrange(val)
182 local p1, p2 = val:match("^(%d+)%-(%d+)$")
183 if p1 and p2 and port(p1) and port(p2) then
190 function macaddr(val)
191 if val and val:match(
192 "^[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+:" ..
193 "[a-fA-F0-9]+:[a-fA-F0-9]+:[a-fA-F0-9]+$"
195 local parts = util.split( val, ":" )
198 parts[i] = tonumber( parts[i], 16 )
199 if parts[i] < 0 or parts[i] > 255 then
210 function hostname(val)
211 if val and (#val < 254) and (
212 val:match("^[a-zA-Z_]+$") or
213 (val:match("^[a-zA-Z0-9_][a-zA-Z0-9_%-%.]*[a-zA-Z0-9]$") and
214 val:match("[^0-9%.]"))
221 function host(val, ipv4only)
222 return hostname(val) or ((ipv4only == 1) and ip4addr(val)) or ((not (ipv4only == 1)) and ipaddr(val))
225 function network(val)
226 return uciname(val) or host(val)
229 function hostport(val, ipv4only)
230 local h, p = val:match("^([^:]+):([^:]+)$")
231 return not not (h and p and host(h, ipv4only) and port(p))
234 function ip4addrport(val, bracket)
235 local h, p = val:match("^([^:]+):([^:]+)$")
236 return (h and p and ip4addr(h) and port(p))
239 function ip4addrport(val)
240 local h, p = val:match("^([^:]+):([^:]+)$")
241 return (h and p and ip4addr(h) and port(p))
244 function ipaddrport(val, bracket)
245 local h, p = val:match("^([^%[%]:]+):([^:]+)$")
246 if (h and p and ip4addr(h) and port(p)) then
248 elseif (bracket == 1) then
249 h, p = val:match("^%[(.+)%]:([^:]+)$")
250 if (h and p and ip6addr(h) and port(p)) then
254 h, p = val:match("^([^%[%]]+):([^:]+)$")
255 return (h and p and ip6addr(h) and port(p))
260 return (val:match("^[a-fA-F0-9]+$") ~= nil)
262 return (#val >= 8) and (#val <= 63)
267 if val:sub(1, 2) == "s:" then
271 if (#val == 10) or (#val == 26) then
272 return (val:match("^[a-fA-F0-9]+$") ~= nil)
274 return (#val == 5) or (#val == 13)
278 function hexstring(val)
280 return (val:match("^[a-fA-F0-9]+$") ~= nil)
285 function hex(val, maxbytes)
286 maxbytes = tonumber(maxbytes)
287 if val and maxbytes ~= nil then
288 return ((val:match("^0x[a-fA-F0-9]+$") ~= nil) and (#val <= 2 + maxbytes * 2))
295 return (val:match("^[a-zA-Z0-9/+]+=?=?$") ~= nil) and (math.fmod(#val, 4) == 0)
301 return true -- Everything qualifies as valid string
304 function directory( val, seen )
305 local s = fs.stat(val)
308 if s and not seen[s.ino] then
310 if s.type == "dir" then
312 elseif s.type == "lnk" then
313 return directory( fs.readlink(val), seen )
320 function file( val, seen )
321 local s = fs.stat(val)
324 if s and not seen[s.ino] then
326 if s.type == "reg" then
328 elseif s.type == "lnk" then
329 return file( fs.readlink(val), seen )
336 function device( val, seen )
337 local s = fs.stat(val)
340 if s and not seen[s.ino] then
342 if s.type == "chr" or s.type == "blk" then
344 elseif s.type == "lnk" then
345 return device( fs.readlink(val), seen )
352 function uciname(val)
353 return (val:match("^[a-zA-Z0-9_]+$") ~= nil)
356 function range(val, min, max)
361 if val ~= nil and min ~= nil and max ~= nil then
362 return ((val >= min) and (val <= max))
368 function min(val, min)
372 if val ~= nil and min ~= nil then
379 function max(val, max)
383 if val ~= nil and max ~= nil then
390 function rangelength(val, min, max)
395 if val ~= nil and min ~= nil and max ~= nil then
396 return ((#val >= min) and (#val <= max))
402 function minlength(val, min)
406 if val ~= nil and min ~= nil then
413 function maxlength(val, max)
417 if val ~= nil and max ~= nil then
424 function phonedigit(val)
425 return (val:match("^[0-9\*#!%.]+$") ~= nil)
428 function timehhmmss(val)
429 return (val:match("^[0-6][0-9]:[0-6][0-9]:[0-6][0-9]$") ~= nil)
432 function dateyyyymmdd(val)
434 yearstr, monthstr, daystr = val:match("^(%d%d%d%d)-(%d%d)-(%d%d)$")
435 if (yearstr == nil) or (monthstr == nil) or (daystr == nil) then
438 year = tonumber(yearstr)
439 month = tonumber(monthstr)
440 day = tonumber(daystr)
441 if (year == nil) or (month == nil) or (day == nil) then
445 local days_in_month = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
447 local function is_leap_year(year)
448 return (year % 4 == 0) and ((year % 100 ~= 0) or (year % 400 == 0))
451 function get_days_in_month(month, year)
452 if (month == 2) and is_leap_year(year) then
455 return days_in_month[month]
458 if (year < 2015) then
461 if ((month == 0) or (month > 12)) then
464 if ((day == 0) or (day > get_days_in_month(month, year))) then