modules/luci-base: Fix ipaddrport validator to support ipv6
[project/luci.git] / modules / luci-base / htdocs / luci-static / resources / cbi.js
index 5e31bf2..19a1ede 100644 (file)
@@ -161,6 +161,49 @@ var cbi_validators = {
                        cbi_validators.host.apply(this);
        },
 
+       'hostport': function()
+       {
+               var hp = this.split(/:/);
+
+               if (hp.length == 2)
+                       return (cbi_validators.host.apply(hp[0]) &&
+                               cbi_validators.port.apply(hp[1]));
+
+               return false;
+       },
+
+       'ip4addrport': function()
+       {
+               var hp = this.split(/:/);
+
+               if (hp.length == 2)
+                       return (cbi_validators.ipaddr.apply(hp[0]) &&
+                               cbi_validators.port.apply(hp[1]));
+               return false;
+       },
+
+       'ipaddrport': function(bracket)
+       {
+               if (this.match(/^([^\[\]:]+):([^:]+)$/)) {
+                       var addr = RegExp.$1
+                       var port = RegExp.$2
+                       return (cbi_validators.ip4addr.apply(addr) &&
+                               cbi_validators.port.apply(port));
+                } else if ((bracket == 1) && (this.match(/^\[(.+)\]:([^:]+)$/))) {
+                       var addr = RegExp.$1
+                       var port = RegExp.$2
+                       return (cbi_validators.ip6addr.apply(addr) &&
+                               cbi_validators.port.apply(port));
+                } else if ((bracket != 1) && (this.match(/^([^\[\]]+):([^:]+)$/))) {
+                       var addr = RegExp.$1
+                       var port = RegExp.$2
+                       return (cbi_validators.ip6addr.apply(addr) &&
+                               cbi_validators.port.apply(port));
+               } else {
+                       return false;
+               }
+       },
+
        'wpakey': function()
        {
                var v = this;
@@ -300,6 +343,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;
+               }
        }
 };
 
@@ -545,7 +629,7 @@ function cbi_browser_init(id, respath, url, defpath)
        cbi_bind(btn, 'click', cbi_browser_btnclick);
 }
 
-function cbi_dynlist_init(name, respath, datatype, optional, choices)
+function cbi_dynlist_init(name, respath, datatype, optional, url, defpath, choices)
 {
        var input0 = document.getElementsByName(name)[0];
        var prefix = input0.name;
@@ -606,6 +690,11 @@ function cbi_dynlist_init(name, respath, datatype, optional, choices)
 
                        parent.appendChild(t);
                        parent.appendChild(b);
+                       if (datatype == 'file')
+                       {
+                               cbi_browser_init(t.id, respath, url, defpath);
+                       }
+
                        parent.appendChild(document.createElement('br'));
 
                        if (datatype)
@@ -616,13 +705,13 @@ function cbi_dynlist_init(name, respath, datatype, optional, choices)
                        if (choices)
                        {
                                cbi_combobox_init(t.id, choices[0], '', choices[1]);
-                               t.nextSibling.index = i;
+                               b.index = i;
 
-                               cbi_bind(t.nextSibling, 'keydown',  cbi_dynlist_keydown);
-                               cbi_bind(t.nextSibling, 'keypress', cbi_dynlist_keypress);
+                               cbi_bind(b, 'keydown',  cbi_dynlist_keydown);
+                               cbi_bind(b, 'keypress', cbi_dynlist_keypress);
 
                                if (i == focus || -i == focus)
-                                       t.nextSibling.focus();
+                                       b.focus();
                        }
                        else
                        {
@@ -758,20 +847,24 @@ function cbi_dynlist_init(name, respath, datatype, optional, choices)
                ev = ev ? ev : window.event;
 
                var se = ev.target ? ev.target : ev.srcElement;
+               var input = se.previousSibling;
+               while (input && input.name != name) {
+                       input = input.previousSibling;
+               }
 
                if (se.src.indexOf('remove') > -1)
                {
-                       se.previousSibling.value = '';
+                       input.value = '';
 
                        cbi_dynlist_keydown({
-                               target:  se.previousSibling,
+                               target:  input,
                                keyCode: 8
                        });
                }
                else
                {
                        cbi_dynlist_keydown({
-                               target:  se.previousSibling,
+                               target:  input,
                                keyCode: 13
                        });
                }