From 63c02d5082528d9d0c69520e0034cbb52522208d Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 27 Nov 2019 11:22:11 +0100 Subject: [js_ip/locations] Mode cidr/ip handling to own module --- modules-available/js_ip/clientscript.js | 67 ++++++++++++++++++++++ modules-available/js_ip/config.json | 7 +++ modules-available/locations/clientscript.js | 66 --------------------- .../locations/pages/locations.inc.php | 1 + .../locations/templates/locations.html | 12 ++-- 5 files changed, 79 insertions(+), 74 deletions(-) create mode 100644 modules-available/js_ip/clientscript.js create mode 100644 modules-available/js_ip/config.json delete mode 100644 modules-available/locations/clientscript.js diff --git a/modules-available/js_ip/clientscript.js b/modules-available/js_ip/clientscript.js new file mode 100644 index 00000000..930292b1 --- /dev/null +++ b/modules-available/js_ip/clientscript.js @@ -0,0 +1,67 @@ +'use strict'; + +function ip2long(IP) { + var i = 0; + IP = IP.match(/^([1-9]\d*|0[0-7]*|0x[\da-f]+)(?:\.([1-9]\d*|0[0-7]*|0x[\da-f]+))?(?:\.([1-9]\d*|0[0-7]*|0x[\da-f]+))?(?:\.([1-9]\d*|0[0-7]*|0x[\da-f]+))?$/i); + if (!IP) { + return false; + } + IP.push(0, 0, 0, 0); + for (i = 1; i < 5; i += 1) { + IP[i] = parseInt(IP[i]) || 0; + if (IP[i] < 0 || IP[i] > 255) + return false; + } + return IP[1] * 16777216 + IP[2] * 65536 + IP[3] * 256 + IP[4] * 1; +} + +function long2ip(a) { + return [ + a >>> 24, + 255 & a >>> 16, + 255 & a >>> 8, + 255 & a + ].join('.'); +} + +function cidrToRange(cidr) { + var range = []; + cidr = cidr.split('/'); + if (cidr.length !== 2) + return false; + var cidr_1 = parseInt(cidr[1]); + if (cidr_1 <= 0 || cidr_1 > 32) + return false; + var param = ip2long(cidr[0]); + if (param === false) + return false; + range[0] = long2ip((param) & ((-1 << (32 - cidr_1)))); + var start = ip2long(range[0]); + range[1] = long2ip(start + Math.pow(2, (32 - cidr_1)) - 1); + return range; +} + +/** + * Add listener to start IP input; when it loses focus, see if we have a + * CIDR notation and fill out start+end field. + */ +function slxAttachCidr() { + $('.cidrmagic').each(function () { + var t = $(this); + var s = t.find('input.cidrstart'); + var e = t.find('input.cidrend'); + if (!s || !e) + return; + t.removeClass('cidrmagic'); + s.focusout(function () { + var res = cidrToRange(s.val()); + if (res === false) + return; + s.val(res[0]); + e.val(res[1]); + }); + }); +} + +// Attach +slxAttachCidr(); \ No newline at end of file diff --git a/modules-available/js_ip/config.json b/modules-available/js_ip/config.json new file mode 100644 index 00000000..96c02bce --- /dev/null +++ b/modules-available/js_ip/config.json @@ -0,0 +1,7 @@ +{ + "dependencies": [], + "scripts": [ + "clientscript.js" + ], + "client-plugin": true +} \ No newline at end of file diff --git a/modules-available/locations/clientscript.js b/modules-available/locations/clientscript.js deleted file mode 100644 index ad3e6c43..00000000 --- a/modules-available/locations/clientscript.js +++ /dev/null @@ -1,66 +0,0 @@ -function ip2long(IP) { - var i = 0; - IP = IP.match(/^([1-9]\d*|0[0-7]*|0x[\da-f]+)(?:\.([1-9]\d*|0[0-7]*|0x[\da-f]+))?(?:\.([1-9]\d*|0[0-7]*|0x[\da-f]+))?(?:\.([1-9]\d*|0[0-7]*|0x[\da-f]+))?$/i); - if (!IP) { - return false; - } - IP.push(0, 0, 0, 0); - for (i = 1; i < 5; i += 1) { - IP[i] = parseInt(IP[i]) || 0; - if (IP[i] < 0 || IP[i] > 255) - return false; - } - return IP[1] * 16777216 + IP[2] * 65536 + IP[3] * 256 + IP[4] * 1; -} - -function long2ip(a) { - return [ - a >>> 24, - 255 & a >>> 16, - 255 & a >>> 8, - 255 & a - ].join('.'); -} - -function cidrToRange(cidr) { - var range = [2]; - cidr = cidr.split('/'); - var cidr_1 = parseInt(cidr[1]); - if (cidr_1 <= 0 || cidr_1 > 32) - return false; - var param = ip2long(cidr[0]); - if (param === false) - return false; - range[0] = long2ip((param) & ((-1 << (32 - cidr_1)))); - var start = ip2long(range[0]); - range[1] = long2ip(start + Math.pow(2, (32 - cidr_1)) - 1); - return range; -} - -/** - * Add listener to start IP input; when it loses focus, see if we have a - * CIDR notation and fill out start+end field. - */ -function slxAttachCidr() { - $('.cidrmagic').each(function () { - var t = $(this); - var s = t.find('input.cidrstart'); - var e = t.find('input.cidrend'); - if (!s || !e) - return; - t.removeClass('cidrmagic'); - s.focusout(function () { - var val = s.val(); - if (val.match(/^[0-9]+\.[0-9]+(\.[0-9]+(\.[0-9]+)?)?\/[0-9]{2}$/)) { - var res = cidrToRange(val); - if (res === false) - return; - s.val(res[0]); - e.val(res[1]); - } - }); - }); -} - -// Attach -slxAttachCidr(); \ No newline at end of file diff --git a/modules-available/locations/pages/locations.inc.php b/modules-available/locations/pages/locations.inc.php index a8cd2e63..54f44554 100644 --- a/modules-available/locations/pages/locations.inc.php +++ b/modules-available/locations/pages/locations.inc.php @@ -242,6 +242,7 @@ class SubPage // depends on permissions in the according modules, not this one Permission::addGlobalTags($data['perms'], NULL, ['subnets.edit', 'location.add']); Render::addTemplate('locations', $data); + Module::isAvailable('js_ip'); // For CIDR magic } private static function propagateFields(&$locationList, $defaultValue, $name, $class) diff --git a/modules-available/locations/templates/locations.html b/modules-available/locations/templates/locations.html index f30ebcab..e2224bc0 100644 --- a/modules-available/locations/templates/locations.html +++ b/modules-available/locations/templates/locations.html @@ -209,7 +209,7 @@ function slxOpenLocation(e, lid) { tr.append(td); $(e).closest('tr').addClass('active slx-bold').after(tr); td.load('?do=Locations&page=details&action=showlocation&locationid=' + lid, function() { - slxAttachCidr(); + if (slxAttachCidr) slxAttachCidr(); scollIntoView(tr); }); slxLastLocation = tr; @@ -230,18 +230,14 @@ function scollIntoView(el) { function slxAddSubnetRow(e, lid) { var tr = $('#loc-sub-' + lid); - tr.before('\ + tr.before('\ #\ \ \ - \ + \ '); slxAddCounter++; - slxAttachCidr(); -} - -function removeNewSubnetRow(r) { - $("#row"+r).remove(); + if (slxAttachCidr) slxAttachCidr(); } function deleteSubnetWarning(locid) { -- cgit v1.2.3-55-g7522