prevent script tags inside of error popup
[plstackapi.git] / planetstack / core / xoslib / static / js / xoslib / xos-util.js
index 1d135f4..6f4db85 100644 (file)
@@ -75,6 +75,12 @@ function validateField(validatorName, value, obj) {
                 return "must be a valid url";
             }
             break;
+
+        case "portspec":
+            if (! $.trim(value).match(portlist_regexp())) {
+                return "must be a valid portspec (example: 'tcp 123, udp 456-789')"
+            }
+            break;
     }
 
     return true;
@@ -93,6 +99,24 @@ function array_diff(a1, a2)
   return diff;\r
 }
 
+function array_subtract(a1, a2)
+{
+    result=[]
+    for (index in a1) {
+        value = a1[index];
+        if (!$.inArray(value, a2) >= 0) {
+            result.push(value);
+        }
+    }
+    return result;
+}
+
+function array_same_elements(arr1, arr2)
+{
+    // return true if arrays have same elements, even if order is different
+    return ($(arr1).not(arr2).length == 0 && $(arr2).not(arr1).length == 0);
+}
+
 function array_pair_lookup(x, names, values)
 {
     for (index in values) {
@@ -100,7 +124,7 @@ function array_pair_lookup(x, names, values)
             return names[index];
         }
     }
-    return undefined;
+    return "object #" + x;
 }
 
 function all_options(selector) {
@@ -111,3 +135,124 @@ function all_options(selector) {
     });
     return result;
 }
+
+function make_same_width(containerSelector, itemSelector) {
+    var maxWidth = 0;
+    $(containerSelector).find(itemSelector).each( function(index) { maxWidth = Math.max(maxWidth, $(this).width()); });\r
+    console.log(maxWidth);\r
+    $(containerSelector).find(itemSelector).each( function(index) { $(this).width(maxWidth); });
+}
+
+function strip_scripts(s) {
+    var div = document.createElement('div');\r
+    div.innerHTML = s;\r
+    var scripts = div.getElementsByTagName('script');\r
+    var i = scripts.length;\r
+    while (i--) {\r
+      scripts[i].parentNode.removeChild(scripts[i]);\r
+    }\r
+    return div.innerHTML;\r
+  }
+
+function parse_portlist(ports) {
+    /* Support a list of ports in the format "protocol:port, protocol:port, ..."
+        examples:
+            tcp 123
+            tcp 123:133
+            tcp 123, tcp 124, tcp 125, udp 201, udp 202
+
+        User can put either a "/" or a " " between protocol and ports
+        Port ranges can be specified with "-" or ":"
+
+        This is a straightforward port of the code in core/models/network.py
+    */
+
+    var nats = [];
+    if (ports) {
+        parts = ports.split(",")
+        $.each(parts, function(index, part) {
+            part = $.trim(part);
+            if (part.indexOf("/")>=0) {
+                parts2 = part.split("/",2);
+                protocol=parts2[0];
+                ports=parts2[1];
+            } else if (part.indexOf(" ")>=0) {
+                parts2 = part.split(" +",2);
+                protocol=parts2[0];
+                ports=parts2[1];
+            } else {
+                throw 'malformed port specifier ' + part + ', format example: "tcp 123, tcp 201:206, udp 333"';
+            }
+
+            protocol = $.trim(protocol);
+            ports = $.trim(ports);
+
+            console.log(ports);
+
+            if (protocol!="tcp" && protocol!="udp") {
+                throw 'unknown protocol ' + protocol;
+            }
+
+            if (ports.indexOf("-")>=0) {
+                parts2 = ports.split("-");
+                first = parseInt($.trim(parts2[0]));
+                last = parseInt($.trim(parts2[1]));
+                portStr = first + ":" + last;
+            } else if (ports.indexOf(":")>=0) {
+                parts2 = ports.split(":");
+                first = parseInt($.trim(parts2[0]));
+                last = parseInt($.trim(parts2[1]));
+                portStr = first + ":" + last;
+            } else {
+                portStr = parseInt(ports).toString();
+            }
+
+            nats.push( {l4_protocol: protocol, l4_port: portStr} );
+        }); /* end $.each(ports) */
+    }
+    return nats
+}
+
+function portlist_regexp() {
+    /* this constructs the big complicated regexp that validates port
+       specifiers. Saved here in long form, in case we need to change it
+       in the future.
+    */
+
+    paren = function(x) { return "(?:" + x + ")"; }
+    whitespace = " *";
+    protocol = paren("tcp|udp");
+    protocolSlash = protocol + paren(whitespace + "|\/");
+    numbers = paren("[0-9]+");
+    range = paren(numbers + paren("-|:") + numbers);
+    numbersOrRange = paren(numbers + "|" + range);
+    protoPorts = paren(protocolSlash + numbersOrRange);
+    protoPortsCommas = paren(paren(protoPorts + "," + whitespace)+"+");
+    multiProtoPorts = paren(protoPortsCommas + protoPorts);
+    portSpec = "^" + paren(protoPorts + "|" + multiProtoPorts) + "$";
+    return RegExp(portSpec);
+}
+
+function portlist_selftest() {
+    r = portlist_regexp();
+    assert(! "tcp".match(r), 'should not have matched: "tcp"');
+    assert("tcp 1".match(r), 'should have matched: "tcp 1"');
+    assert("tcp 123".match(r), 'should have matched: "tcp 123"');
+    assert("tcp  123".match(r), 'should have matched: "tcp 123"');
+    assert("tcp 123-456".match(r), 'should have matched: "tcp 123-456"');
+    assert("tcp 123:456".match(r), 'should have matched: "tcp 123:456"');
+    assert(! "tcp 123-".match(r), 'should have matched: "tcp 123-"');
+    assert(! "tcp 123:".match(r), 'should have matched: "tcp 123:"');
+    assert(! "foo 123".match(r), 'should not have matched "foo 123"');
+    assert("udp 123".match(r), 'should have matched: "udp 123"');
+    assert("tcp 123,udp 456".match(r), 'should have matched: "tcp 123,udp 456"');
+    assert("tcp 123, udp 456".match(r), 'should have matched: "tcp 123, udp 456"');
+    assert("tcp 123,  udp 456".match(r), 'should have matched: "tcp 123,  udp 456"');
+    assert("tcp 123-45, udp 456".match(r), 'should have matched: "tcp 123-45, udp 456"');
+    assert("tcp 123-45, udp 456, tcp 11, tcp 22:45, udp 76, udp 47:49, udp 60-61".match(r), 'should have matched: "tcp 123-45, udp 456, tcp 11, tcp 22:45, udp 76, udp 47:49, udp 60-61"');
+    return "done";
+}
+
+//portlist_selftest();
+
+