summaryrefslogtreecommitdiffstats
path: root/modules-available
diff options
context:
space:
mode:
authorSimon Rettberg2016-06-24 17:08:24 +0200
committerSimon Rettberg2016-06-24 17:08:24 +0200
commit3d0cb99c70dd9714655cf26893504e4bd007b036 (patch)
tree8b5cdaee9c0196aedb6a5c3f8138005a18091fa8 /modules-available
parentmodified the editor toolbar. (diff)
downloadslx-admin-3d0cb99c70dd9714655cf26893504e4bd007b036.tar.gz
slx-admin-3d0cb99c70dd9714655cf26893504e4bd007b036.tar.xz
slx-admin-3d0cb99c70dd9714655cf26893504e4bd007b036.zip
[locations] Support CIDR notion (start field), tweak templates a bit
Diffstat (limited to 'modules-available')
-rw-r--r--modules-available/locations/clientscript.js66
-rw-r--r--modules-available/locations/page.inc.php72
-rw-r--r--modules-available/locations/templates/location-subnets.html34
-rw-r--r--modules-available/locations/templates/locations.html27
-rw-r--r--modules-available/locations/templates/subnets.html6
5 files changed, 164 insertions, 41 deletions
diff --git a/modules-available/locations/clientscript.js b/modules-available/locations/clientscript.js
new file mode 100644
index 00000000..ad3e6c43
--- /dev/null
+++ b/modules-available/locations/clientscript.js
@@ -0,0 +1,66 @@
+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/page.inc.php b/modules-available/locations/page.inc.php
index de724350..f747592f 100644
--- a/modules-available/locations/page.inc.php
+++ b/modules-available/locations/page.inc.php
@@ -21,8 +21,43 @@ class Page_Locations extends Page
$this->updateLocation();
} elseif ($this->action === 'addlocations') {
$this->addLocations();
+ } elseif ($this->action === 'updatesubnets') {
+ $this->updateSubnets();
}
}
+
+ private function updateSubnets()
+ {
+ $count = 0;
+ $starts = Request::post('startaddr', false);
+ $ends = Request::post('endaddr', false);
+ $locs = Request::post('location', false);
+ if (!is_array($starts) || !is_array($ends) || !is_array($locs)) {
+ Message::addError('main.empty-field');
+ Util::redirect('?do=Locations');
+ }
+ $existingLocs = Location::getLocationsAssoc();
+ $stmt = Database::prepare("UPDATE subnet SET startaddr = :startLong, endaddr = :endLong, locationid = :loc WHERE subnetid = :subnetid");
+ foreach ($starts as $subnetid => $start) {
+ if (!isset($ends[$subnetid]) || !isset($locs[$subnetid]))
+ continue;
+ $loc = (int)$locs[$subnetid];
+ $end = $ends[$subnetid];
+ if (!isset($existingLocs[$loc])) {
+ Message::addError('main.value-invalid', 'locationid', $loc);
+ continue;
+ }
+ $range = $this->rangeToLongVerbose($start, $end);
+ if ($range === false)
+ continue;
+ list($startLong, $endLong) = $range;
+ if ($stmt->execute(compact('startLong', 'endLong', 'loc', 'subnetid'))) {
+ $count += $stmt->rowCount();
+ }
+ }
+ Message::addSuccess('updated-x-entries', $count);
+ Util::redirect('?do=Locations');
+ }
private function addLocations()
{
@@ -147,7 +182,7 @@ class Page_Locations extends Page
Message::addSuccess('location-updated', $newName);
}
}
-
+
private function updateLocationSubnets()
{
// Deletion first
@@ -177,18 +212,10 @@ class Page_Locations extends Page
foreach ($starts as $key => $start) {
if (!isset($ends[$key]) || !is_numeric($key)) continue;
$end = $ends[$key];
- list($startLong, $endLong) = $this->rangeToLong($start, $end);
- if ($startLong === false) {
- Message::addWarning('main.value-invalid', 'start addr', $start);
- }
- if ($endLong === false) {
- Message::addWarning('main.value-invalid', 'end addr', $start);
- }
- if ($startLong === false || $endLong === false) continue;
- if ($startLong > $endLong) {
- Message::addWarning('main.value-invalid', 'range', $start . ' - ' . $end);
+ $range = $this->rangeToLongVerbose($start, $end);
+ if ($range === false)
continue;
- }
+ list($startLong, $endLong) = $range;
if ($stmt->execute(array('id' => $key, 'start' => $startLong, 'end' => $endLong))) {
$count += $stmt->rowCount();
}
@@ -390,6 +417,7 @@ class Page_Locations extends Page
. ' INNER JOIN sat.lecture_x_location ll ON (l.lectureid = ll.lectureid AND ll.locationid = :lid)',
array('lid' => $locationId));
$data['lectures'] = $lectures['cnt'];
+ $data['haveDozmod'] = true;
}
// Get clients matching this location's subnet(s)
$count = $online = $used = 0;
@@ -407,6 +435,7 @@ class Page_Locations extends Page
}
}
}
+ $data['haveStatistics'] = true;
}
$data['machines'] = $count;
$data['machines_online'] = $online;
@@ -432,4 +461,23 @@ class Page_Locations extends Page
return array($startLong, $endLong);
}
+ private function rangeToLongVerbose($start, $end)
+ {
+ $result = $this->rangeToLong($start, $end);
+ list($startLong, $endLong) = $result;
+ if ($startLong === false) {
+ Message::addWarning('main.value-invalid', 'start addr', $start);
+ }
+ if ($endLong === false) {
+ Message::addWarning('main.value-invalid', 'end addr', $start);
+ }
+ if ($startLong === false || $endLong === false)
+ return false;
+ if ($startLong > $endLong) {
+ Message::addWarning('main.value-invalid', 'range', $start . ' - ' . $end);
+ return false;
+ }
+ return $result;
+ }
+
}
diff --git a/modules-available/locations/templates/location-subnets.html b/modules-available/locations/templates/location-subnets.html
index b0353416..23035002 100644
--- a/modules-available/locations/templates/location-subnets.html
+++ b/modules-available/locations/templates/location-subnets.html
@@ -43,31 +43,35 @@
<th title="{{lang_deleteSubnet}}"><span class="glyphicon glyphicon-trash"></span></th>
</tr>
{{#list}}
- <tr>
+ <tr class="cidrmagic">
<td>{{subnetid}}</td>
- <td><input class="form-control" type="text" name="startaddr[{{subnetid}}]" value="{{startaddr}}" pattern="\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"></td>
- <td><input class="form-control" type="text" name="endaddr[{{subnetid}}]" value="{{endaddr}}" pattern="\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"></td>
+ <td><input class="form-control cidrstart" type="text" name="startaddr[{{subnetid}}]" value="{{startaddr}}" pattern="\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"></td>
+ <td><input class="form-control cidrend" type="text" name="endaddr[{{subnetid}}]" value="{{endaddr}}" pattern="\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"></td>
<td class="danger" align="center"><input type="checkbox" name="deletesubnet[{{subnetid}}]" value="on"></td>
</tr>
{{/list}}
<tr id="loc-sub-{{locationid}}">
- <td colspan="2">
+ <td colspan="4">
<button class="btn btn-success btn-sm" type="button" onclick="slxAddSubnetRow(this, {{locationid}})" title="{{lang_addNewSubnet}}">
<span class="glyphicon glyphicon-plus-sign"></span> {{lang_subnet}}
</button>
</td>
- <td colspan="2" align="right">
- <button type="submit" class="btn btn-primary">{{lang_save}}</button>
- </td>
</tr>
</table>
+ <br>
+ <div class="pull-right">
+ <button type="submit" class="btn btn-primary">{{lang_save}}</button>
+ </div>
+ <div class="slx-bold">{{lang_locationInfo}}</div>
+ {{#haveDozmod}}
+ <div>
+ <span class="slx-ga2">{{lang_referencingLectures}}:</span> {{lectures}}
+ </div>
+ {{/haveDozmod}}
+ {{#haveStatistics}}
+ <div>
+ <span class="slx-ga2">{{lang_matchingMachines}}:</span> <a href="?do=Statistics&amp;filter=location&amp;argument={{locationid}}">{{machines}} / {{machines_online}} / {{machines_used}} ({{used_percent}}%)</a>
+ </div>
+ {{/haveStatistics}}
</form>
- <br>
- <div class="slx-bold">{{lang_locationInfo}}</div>
- <div>
- <span class="slx-ga2">{{lang_referencingLectures}}:</span> {{lectures}}
- </div>
- <div>
- <span class="slx-ga2">{{lang_matchingMachines}}:</span> <a href="?do=Statistics&amp;filter=location&amp;argument={{locationid}}">{{machines}} / {{machines_online}} / {{machines_used}} ({{used_percent}}%)</a>
- </div>
</div> \ No newline at end of file
diff --git a/modules-available/locations/templates/locations.html b/modules-available/locations/templates/locations.html
index 2971105b..1fb60926 100644
--- a/modules-available/locations/templates/locations.html
+++ b/modules-available/locations/templates/locations.html
@@ -6,13 +6,15 @@
<table class="table table-condensed" style="margin-bottom:0px">
<tr>
<th width="100%">{{lang_locationName}}</th>
- <th>{{#havestatistics}}{{lang_machineCount}}{{/havestatistics}}</th>
- <th class="slx-nowrap">{{lang_editConfigVariables}}</th>
- {{#havesysconfig}}
+ <th>
+ {{#havestatistics}}{{lang_machineCount}}{{/havestatistics}}
+ </th>
+ <th class="slx-nowrap">
+ {{#havebaseconfig}}{{lang_editConfigVariables}}{{/havebaseconfig}}
+ </th>
<th class="slx-nowrap">
- {{#havebaseconfig}}{{lang_sysConfig}}{{/havebaseconfig}}
+ {{#havesysconfig}}{{lang_sysConfig}}{{/havesysconfig}}
</th>
- {{/havesysconfig}}
</tr>
{{#list}}
<tr>
@@ -37,13 +39,13 @@
{{/havebaseconfig}}
</td>
<td class="slx-nowrap">
+ {{#havesysconfig}}
<div class="pull-right">
<a class="btn btn-default btn-xs" href="?do=sysconfig&amp;locationid={{locationid}}"><span class="glyphicon glyphicon-edit"></span></a>
</div>
<span class="{{configClass}}">
{{configName}}&emsp;&emsp;
</span>
- {{#havesysconfig}}
{{/havesysconfig}}
</td>
</tr>
@@ -123,23 +125,26 @@ function slxOpenLocation(e, lid) {
}
return;
}
- var td = $('<td>').attr('colspan', '2').css('padding', '0px 0px 12px');
+ var td = $('<td>').attr('colspan', '4').css('padding', '0px 0px 12px');
var tr = $('<tr>').attr('id', 'location-details-' + lid);
tr.append(td);
$(e).closest('tr').addClass('active slx-bold').after(tr);
- td.load('?do=Locations&action=showlocation&locationid=' + lid);
+ td.load('?do=Locations&action=showlocation&locationid=' + lid, function() {
+ slxAttachCidr();
+ });
slxLastLocation = tr;
}
function slxAddSubnetRow(e, lid) {
var tr = $('#loc-sub-' + lid);
- tr.before('<tr>\
+ tr.before('<tr class="cidrmagic">\
<td>#</td>\
- <td><input class="form-control" type="text" name="newstartaddr[' + slxAddCounter + ']" pattern="\\d{1,3}\.\\d{1,3}\.\\d{1,3}\.\\d{1,3}"></td>\
- <td><input class="form-control" type="text" name="newendaddr[' + slxAddCounter + ']" pattern="\\d{1,3}\.\\d{1,3}\.\\d{1,3}\.\\d{1,3}"></td>\
+ <td><input class="form-control cidrstart" type="text" name="newstartaddr[' + slxAddCounter + ']" pattern="\\d{1,3}\.\\d{1,3}\.\\d{1,3}\.\\d{1,3}"></td>\
+ <td><input class="form-control cidrend" type="text" name="newendaddr[' + slxAddCounter + ']" pattern="\\d{1,3}\.\\d{1,3}\.\\d{1,3}\.\\d{1,3}"></td>\
<td></td>\
</tr>');
slxAddCounter++;
+ slxAttachCidr();
}
function slxConfirm() {
diff --git a/modules-available/locations/templates/subnets.html b/modules-available/locations/templates/subnets.html
index 2294f42b..0320e333 100644
--- a/modules-available/locations/templates/subnets.html
+++ b/modules-available/locations/templates/subnets.html
@@ -14,10 +14,10 @@
<th>{{lang_location}}</th>
</tr>
{{#list}}
- <tr>
+ <tr class="cidrmagic">
<td>{{subnetid}}</td>
- <td><input class="form-control" type="text" name="startaddr[{{subnetid}}]" value="{{startaddr}}"></td>
- <td><input class="form-control" type="text" name="endaddr[{{subnetid}}]" value="{{endaddr}}"></td>
+ <td><input class="form-control cidrstart" type="text" name="startaddr[{{subnetid}}]" value="{{startaddr}}"></td>
+ <td><input class="form-control cidrend" type="text" name="endaddr[{{subnetid}}]" value="{{endaddr}}"></td>
<td>
<select class="form-control" name="location[{{subnetid}}]">
{{#locations}}