luci-base: Add time and data datatypes for use with firewall app
authorDaniel Dickinson <openwrt@daniel.thecshore.com>
Wed, 2 Dec 2015 04:43:18 +0000 (23:43 -0500)
committerDaniel Dickinson <openwrt@daniel.thecshore.com>
Tue, 15 Dec 2015 04:26:26 +0000 (23:26 -0500)
Adding LuCI configuation of the firewall time and data uci options
is in progress and this adds the necessary datatypes for validating
those fields.

modules/luci-base/htdocs/luci-static/resources/cbi.js
modules/luci-base/luasrc/cbi/datatypes.lua

index 8a3cb6fca7c3c963a5d3c60f6e44ceeea2babde5..4b7227f1a4e959c225dfa0fecac9e78a64d58c19 100644 (file)
@@ -322,6 +322,47 @@ var cbi_validators = {
        'phonedigit': function()
        {
                return (this.match(/^[0-9\*#!\.]+$/) != null);
+       },
+        'timehhmmss': function()
+       {
+               return (this.match(/^[0-6][0-9]:[0-6][0-9]:[0-6][0-9]$/) != null);
+       },
+       'dateyyyymmdd': function()
+       {
+               if (this == null) {
+                       return false;
+               }
+               if (this.match(/^(\d\d\d\d)-(\d\d)-(\d\d)/)) {
+                       var year = RegExp.$1;
+                       var month = RegExp.$2;
+                       var day = RegExp.$2
+
+                       var days_in_month = [ 31, 28, 31, 30, 31, 30, 31, 31, 30 , 31, 30, 31 ];
+                       function is_leap_year(year) {
+                               return ((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0);
+                       }
+                       function get_days_in_month(month, year) {
+                               if ((month == 2) && is_leap_year(year)) {
+                                       return 29;
+                               } else {
+                                       return days_in_month[month];
+                               }
+                       }
+                       /* Firewall rules in the past don't make sense */
+                       if (year < 2015) {
+                               return false;
+                       }
+                       if ((month <= 0) || (month > 12)) {
+                               return false;
+                       }
+                       if ((day <= 0) || (day > get_days_in_month(month, year))) {
+                               return false;
+                       }
+                       return true;
+
+               } else {
+                       return false;
+               }
        }
 };
 
index 52f90afee675488cf1eb19557264ed77a9716f5a..4c003be2a1bc50ad80301d84a6708f3125b2c96c 100644 (file)
@@ -341,3 +341,48 @@ 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
+