forked from jakepaulus/collate-network
/
statics.php
511 lines (429 loc) · 21.6 KB
/
statics.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
<?php
/**
* Please see /include/common.php for documentation on common.php, the $COLLATE global array used by this program, and the AccessControl function used widely.
*/
require_once('./include/common.php');
$op = (empty($_GET['op'])) ? 'default' : $_GET['op'];
switch($op){
case "add";
AccessControl("2", null);
add_static();
break;
case "submit";
submit_static();
break;
case "submit_acl";
submit_acl();
break;
default:
AccessControl("1", "Static IP list viewed");
list_statics();
break;
}
require_once('./include/footer.php');
function add_static(){
global $COLLATE;
global $dbo;
$subnet_id = (isset($_GET['subnet_id']) && is_numeric($_GET['subnet_id'])) ? $_GET['subnet_id'] : '';
$name = (isset($_GET['name'])) ? $_GET['name'] : '';
$ip_addr = (isset($_GET['ip_addr'])) ? $_GET['ip_addr'] : '';
$note = (isset($_GET['note'])) ? $_GET['note'] : '';
$contact = (isset($COLLATE['user']['username'])) ? $COLLATE['user']['username'] : 'system'; # set a default
$contact = (isset($_GET['contact'])) ? $_GET['contact'] : $contact; # let user input override the default
if(empty($subnet_id)){
$notice = "invalidrequest";
header("Location: blocks.php?notice=$notice");
}
$return = find_free_statics($subnet_id);
$ipspace = $return['ipspace'];
if($ipspace['0'] === false){
$notice = "invalidrequest";
header("Location: blocks.php?notice=$notice");
exit();
}
require_once('./include/header.php');
echo "<h1>".$COLLATE['languages']['selected']['ReserveaStaticIP']."</h1>\n".
"<p style=\"text-align: right;\"><a href=\"#\"
onclick=\"new Ajax.Updater('helper', '_statics.php?op=guidance&subnet_id=$subnet_id', { evalScripts: true }); return false; \">".
$COLLATE['languages']['selected']['IPGuidance']."</a></p>\n".
"<form action=\"statics.php?op=submit\" method=\"post\">\n".
"<div style=\"float: left; width: 28%;\">\n".
" <p><b>".$COLLATE['languages']['selected']['Name'].":</b><br /><input type=\"text\" name=\"name\" value=\"$name\" required minlength=\"3\" maxlength=\"25\" /></p>\n".
" <p><b>".$COLLATE['languages']['selected']['IPAddress'].":</b><br /><select id=\"ip\" name=\"ip_addr\">\n";
while(!empty($ipspace)){
$ip = long2ip(array_pop($ipspace));
if($ip === $ip_addr){
echo "<option selected=\"selected\" value=\"$ip\">$ip</option>\n";
}
else{
echo "<option value=\"$ip\">$ip</option>\n";
}
}
echo "</select>".
" <a href=\"#\" onclick=\"
new Element.update('helper', '<p><img src="images/loading.gif" alt="" /></p>');
new Ajax.Updater('helper', '_statics.php?op=ping&ip=' + document.forms[0].ip.value); return false;\">[".$COLLATE['languages']['selected']['Ping']."]</a>\n".
" </p> \n".
" <p><b>".$COLLATE['languages']['selected']['ContactPerson'].":</b><br />".
" <input type=\"text\" name=\"contact\" value=\"$contact\" required minlength=\"3\" maxlength=\"100\" /></p>\n".
" <p><b>".$COLLATE['languages']['selected']['Note'].":</b> ".$COLLATE['languages']['selected']['Optional']."<br />".
" <input type=\"text\" name=\"note\" value=\"$note\" maxlength=\"80\" /></p>\n".
" </div>\n".
" <div id=\"helper\" style=\"float: left; width: 70%; padding-left: 10px; border-left: 1px solid #000;\">\n";
$sql = "SELECT guidance FROM subnets WHERE id='$subnet_id'";
$result = $dbo -> query($sql);
$guidance = $result -> fetchColumn();
if(empty($guidance) && empty($COLLATE['settings']['guidance'])){
$help = '';
}
elseif(!empty($guidance)){
$help = $guidance;
}
else{
$help = $COLLATE['settings']['guidance'];
}
echo "<pre><span id=\"guidance\">$help</span></pre>";
if($COLLATE['user']['accesslevel'] >= '3' || $COLLATE['settings']['perms'] > '3'){
echo "<script type=\"text/javascript\"><!--\n".
" new Ajax.InPlaceEditorWithEmptyText('guidance', '_statics.php?op=edit_guidance&subnet_id=$subnet_id',
{
clickToEditText: '".$COLLATE['languages']['selected']['ClicktoEdit']."',
highlightcolor: '#a5ddf8', rows: '7', cols: '49',
callback:
function(form) {
new Element.update('notice', '');
return Form.serialize(form);
},
onFailure:
function(transport) {
new Element.update('notice', transport.responseText.stripTags());
}}
);
--></script>";
}
echo "</div> <p style=\"clear: left;\"><input type=\"hidden\" name=\"subnet_id\" value=\"$subnet_id\" />\n".
"<input type=\"submit\" value=\" ".$COLLATE['languages']['selected']['Go']." \" /></p>\n".
"</form>";
} // Ends add_static function
function submit_static(){
global $COLLATE;
global $dbo;
include 'include/validation_functions.php';
$name = (empty($_POST['name'])) ? '' : clean($_POST['name']);
$ip_addr = (empty($_POST['ip_addr'])) ? '' : clean($_POST['ip_addr']);
$long_ip_addr = ip2decimal($ip_addr);
$note = (empty($_POST['note'])) ? '' : clean($_POST['note']);
$contact = (empty($_POST['contact'])) ? '' : clean($_POST['contact']);
$subnet_id = (empty($_POST['subnet_id'])) ? '' : clean($_POST['subnet_id']);
$username = (!isset($COLLATE['user']['username'])) ? 'system' : $COLLATE['user']['username'];
if(empty($name) || empty($ip_addr) || empty($contact) || empty($subnet_id)){
$notice = "blankfield-notice";
header("Location: statics.php?op=add&subnet_id=$subnet_id&name=$name&ip_addr=$ip_addr&contact=$contact¬e=$note¬ice=$notice");
exit();
}
$validate_ip = validate_static_ip($ip_addr);
if ($validate_ip['0'] === false){
$notice = $validate_ip['error'];
header("Location: statics.php?op=add&subnet_id=$subnet_id&name=$name&ip_addr=$ip_addr&contact=$contact¬e=$note¬ice=$notice");
exit();
}
else{
$long_mask = $validate_ip['long_mask'];
$mask = long2ip($long_mask);
}
$sql = "INSERT INTO statics (ip, name, contact, note, subnet_id, modified_by, modified_at)
VALUES('$long_ip_addr', '$name', '$contact', '$note', '$subnet_id', '$username', now())";
$accesslevel = "2";
$message = "Static IP Reserved: $ip_addr ($name)";
AccessControl($accesslevel, $message); // No need to generate logs if nothing is happening. Here, we know data is about to be written to the db.
$dbo -> query($sql);
// Everything looks good so here's a success page with all of the information.
require_once('./include/header.php');
$sql = "SELECT ip FROM statics WHERE subnet_id = '$subnet_id' AND note = 'Default Gateway'";
$result = $dbo -> query($sql);
if($result -> rowCount() == '1'){
$long_gateway = $result -> fetchColumn();
$gateway = long2ip($long_gateway);
$error = ''; #none
}
else{
$gateway = "*";
$error = "<p><b>*</b>".$COLLATE['languages']['selected']['nogateway']."</p><br />";
}
echo "<h1>".$COLLATE['languages']['selected']['IPReserved']."</h1><br />\n".
"<p><b>".$COLLATE['languages']['selected']['Name'].":</b> $name</p>\n".
"<p><b>".$COLLATE['languages']['selected']['IPAddress'].":</b> $ip_addr</p>\n".
"<p><b>".$COLLATE['languages']['selected']['SubnetMask'].":</b> $mask</p>\n".
"<p><b>".$COLLATE['languages']['selected']['Gateway'].":</b> $gateway</p>\n".
"<p><b>".$COLLATE['languages']['selected']['DNSServers'].":</b> ".$COLLATE['settings']['dns']."</p><br />\n".
"$error".
"<br />\n".
"<p><b><a href=\"statics.php?subnet_id=$subnet_id\">".$COLLATE['languages']['selected']['continuetostatics']."</a></b></p>\n";
} // Ends submit_static function
function list_statics(){
global $COLLATE;
global $dbo;
$subnet_id = (isset($_GET['subnet_id']) && preg_match("/[0-9]*/", $_GET['subnet_id'])) ? $_GET['subnet_id'] : '';
if(empty($_GET['subnet_id'])){
$notice = "invalidrequest";
header("Location: blocks.php?notice=$notice");
exit();
}
$sort = (!isset($_GET['sort'])) ? '' : $_GET['sort'];
if ($sort != 'name' && $sort != 'contact') {
$sort = 'ip';
}
$sql = "SELECT name, start_ip, mask, stalescan_enabled FROM subnets WHERE id='$subnet_id'";
$results = $dbo -> query($sql);
if($results -> rowCount() != '1') {
$notice = "invalidrequest";
header("Location: blocks.php?notice=$notice");
exit();
}
require_once('./include/header.php');
list($subnet_name, $subnet_number, $subnet_mask, $stalescan_enabled) = $results -> fetch(PDO::FETCH_NUM);
$subnet_number = long2ip($subnet_number);
$subnet_mask = long2ip($subnet_mask);
$page = (!isset($_GET['page'])) ? "1" : $_GET['page'];
echo "<h1>Static IPs in $subnet_name: $subnet_number / $subnet_mask</h1>\n".
"<div style=\"float: left; width: 70%;\">";
$sql = "SELECT id, ip, name, contact, note, failed_scans FROM statics WHERE subnet_id='$subnet_id' ORDER BY `$sort` ASC";
$hiddenformvars="<input type=\"hidden\" name=\"subnet_id\" value=\"$subnet_id\" />".
"<input type=\"hidden\" name=\"sort\" value=\"$sort\" />";
$updatedsql = pageselector($sql, $hiddenformvars);
$row = $dbo -> query($updatedsql);
$rows = $row -> rowCount();
echo "</div>";
echo "<div style=\"float: left; width: 25%; text-align:right; padding:5px;\">".
"<a href=\"statics.php?op=add&subnet_id=$subnet_id\">".
"<img src=\"./images/add.gif\" alt=\"\" /> ".$COLLATE['languages']['selected']['ReserveIP'].
"</a></div><p style=\"clear: left; display: done;\">\n";
echo "<table style=\"width: 100%\">".
"<tr><th><a href=\"statics.php?subnet_id=$subnet_id\">".$COLLATE['languages']['selected']['IPAddress']."</a></th>".
"<th><a href=\"statics.php?subnet_id=$subnet_id&sort=name\">".$COLLATE['languages']['selected']['Name']."</a></th>".
"<th><a href=\"statics.php?subnet_id=$subnet_id&sort=contact\">".$COLLATE['languages']['selected']['Contact']."</a></th>".
"<th><a href=\"statics.php?subnet_id=$subnet_id&sort=failed_scans\">".$COLLATE['languages']['selected']['FailedScans']."</a></th><th></th></tr>".
"<tr><td colspan=\"5\"><hr class=\"head\" /></td></tr>\n";
$javascript = ''; # this gets concatenated to below
while(list($static_id,$ip,$name,$contact,$note,$failed_scans) = $row -> fetch(PDO::FETCH_NUM)){
$ip = long2ip($ip);
echo "<tr id=\"static_".$static_id."_row_1\">".
"<td><img src=\"images/static.png\" alt=\"\" /> $ip</td><td><span id=\"edit_name_".$static_id."\">$name</span></td>".
"<td><span id=\"edit_contact_".$static_id."\">$contact</span></td>".
"<td>$failed_scans</td>".
"<td>";
if($COLLATE['user']['accesslevel'] >= '2' || $COLLATE['settings']['perms'] > '2'){
echo "<a href=\"#\" onclick=\"
if (confirm('".$COLLATE['languages']['selected']['confirmdelete']."')) {
new Element.update('static_".$static_id."_notice', '');
new Ajax.Updater('static_".$static_id."_notice', '_statics.php?op=delete&static_ip=$ip', {onSuccess:function(){
new Effect.Parallel( [
new Effect.Fade('static_".$static_id."_row_1'),
new Effect.Fade('static_".$static_id."_row_2'),
new Effect.Fade('static_".$static_id."_row_3'),
new Effect.Fade('static_".$static_id."_row_4')
]);
}});
};
return false;
\"><img src=\"./images/remove.gif\" alt=\"X\" title=\"".$COLLATE['languages']['selected']['deletestatic']."\" /></a>";
}
echo "</td></tr>\n";
echo "<tr id=\"static_".$static_id."_row_2\">".
" <td colspan=\"3\"><span id=\"edit_note_".$static_id."\">$note</span></td>".
" <td>";
if($stalescan_enabled == false){ # disabled at the subnet level
echo "<img src=\"./images/skipping_disabled.png\" alt=\"\" title=\"".$COLLATE['languages']['selected']['StaleScandisabled']."\" /></td>";
}
elseif($failed_scans == '-1'){ # disabled at the static ip level
if($COLLATE['user']['accesslevel'] >= '2' || $COLLATE['settings']['perms'] > '2'){
echo "<a href=\"#\" onclick=\"new Ajax.Updater('stalescan_status_icon".$static_id."', '_statics.php?op=toggle_stale-scan&static_ip=$ip')\">";
echo "<span id=\"stalescan_status_icon".$static_id."\">";
}
echo "<img src=\"./images/skipping.png\" alt=\"\" title=\"".$COLLATE['languages']['selected']['enablestalescan']."\" />";
if($COLLATE['user']['accesslevel'] >= '2' || $COLLATE['settings']['perms'] > '2'){
echo "</span></a>";
}
}
else{ # currently enabled
if($COLLATE['user']['accesslevel'] >= '2' || $COLLATE['settings']['perms'] > '2'){
echo "<a href=\"#\" onclick=\"new Ajax.Updater('stalescan_status_icon".$static_id."', '_statics.php?op=toggle_stale-scan&static_ip=$ip')\">";
echo "<span id=\"stalescan_status_icon".$static_id."\">";
}
echo "<img src=\"./images/scanning.png\" alt=\"\" title=\"".$COLLATE['languages']['selected']['disablestalescan']."\" />";
if($COLLATE['user']['accesslevel'] >= '2' || $COLLATE['settings']['perms'] > '2'){
echo "</span></a>";
}
}
echo "</td><td></td></tr>\n";
echo "<tr id=\"static_".$static_id."_row_3\"><td colspan=\"5\"><span id=\"static_".$static_id."_notice\" class=\"tip\"></span></td></tr>\n";
echo "<tr id=\"static_".$static_id."_row_4\"><td colspan=\"5\"><hr class=\"division\" /></td></tr>\n";
if($COLLATE['user']['accesslevel'] >= '2' || $COLLATE['settings']['perms'] > '2'){
$javascript .=
"<script type=\"text/javascript\"><!--\n".
" new Ajax.InPlaceEditorWithEmptyText('edit_name_".$static_id."', '_statics.php?op=edit&static_id=$static_id&edit=name',
{
clickToEditText: '".$COLLATE['languages']['selected']['ClicktoEdit']."',
highlightcolor: '#a5ddf8',
callback:
function(form) {
new Element.update('static_".$static_id."_notice', '');
return Form.serialize(form);
},
onFailure:
function(transport, ipe) {
new Element.update('static_".$static_id."_notice', ipe.responseText.stripTags());
}
}
);\n".
" new Ajax.InPlaceEditorWithEmptyText('edit_contact_".$static_id."', '_statics.php?op=edit&static_id=$static_id&edit=contact',
{
clickToEditText: '".$COLLATE['languages']['selected']['ClicktoEdit']."',
highlightcolor: '#a5ddf8',
callback:
function(form) {
new Element.update('static_".$static_id."_notice', '');
return Form.serialize(form);
},
onFailure:
function(transport, ipe) {
new Element.update('static_".$static_id."_notice', ipe.responseText.stripTags());
}
}
);\n".
" new Ajax.InPlaceEditorWithEmptyText('edit_note_".$static_id."', '_statics.php?op=edit&static_id=$static_id&edit=note',
{
clickToEditText: '".$COLLATE['languages']['selected']['ClicktoEdit']."',
highlightcolor: '#a5ddf8',
callback:
function(form) {
new Element.update('static_".$static_id."_notice', '');
return Form.serialize(form);
},
onFailure:
function(transport, ipe) {
new Element.update('static_".$static_id."_notice', ipe.responseText.stripTags());
}
}
);\n".
"--></script>\n";
}
}
echo "</table><br />";
if($rows < 1){
echo "<p>".$COLLATE['languages']['selected']['nostatics']."</p>";
}
pageselector($sql,$hiddenformvars);
$sql = "SELECT id, name, start_ip, end_ip FROM acl WHERE subnet_id='$subnet_id' ORDER BY name ASC";
$result = $dbo -> query($sql);
echo "<h1>".$COLLATE['languages']['selected']['ACL']."</h1>\n";
if($COLLATE['user']['accesslevel'] >= '3' || $COLLATE['settings']['perms'] > '3'){
echo "<p style=\"text-align: right;\"><a href=\"#\" onclick=\"Effect.toggle($('add_acl'),'appear',{duration:0}); return false;\">\n".
"<img src=\"./images/add.gif\" alt=\"Add\" /> ".$COLLATE['languages']['selected']['AddACL']." </a></p>\n";
}
else{
echo "<p></p>";
}
echo "<form action=\"statics.php?op=submit_acl&subnet_id=$subnet_id\" method=\"post\"><table style=\"width: 100%\">".
"<tr><th>".$COLLATE['languages']['selected']['Name'].
"</th><th>".$COLLATE['languages']['selected']['StartingIPAddress']."</th>".
"<th>".$COLLATE['languages']['selected']['EndingIPAddress']."</th><th></th></tr>".
"<tr><td colspan=\"4\"><hr class=\"head\" /></td></tr>";
while(list($acl_id,$acl_name, $long_acl_start, $long_acl_end) = $result -> fetch(PDO::FETCH_NUM)){
$acl_start = long2ip($long_acl_start);
$acl_end = long2ip($long_acl_end);
echo "<tr id=\"acl_".$acl_id."\">
<td><span id=\"edit_acl_name_".$acl_id."\">$acl_name</span></td>
<td>$acl_start</td>
<td>$acl_end</td>
<td>";
if($COLLATE['user']['accesslevel'] >= '3' || $COLLATE['settings']['perms'] > '3'){
echo " <a href=\"#\" onclick=\"
if (confirm('Are you sure you want to delete this object?')) {
new Element.update('notice', '');
new Ajax.Updater('notice', '_statics.php?op=delete_acl&acl_id=$acl_id', {onSuccess:function(){
new Effect.Fade('acl_".$acl_id."');
}});
}; return false;\"><img src=\"./images/remove.gif\" alt=\"X\" /></a>";
}
echo "</td>
</tr>\n";
if($COLLATE['user']['accesslevel'] >= '3' || $COLLATE['settings']['perms'] > '3'){
$javascript .=
"<script type=\"text/javascript\"><!-- \n".
" new Ajax.InPlaceEditorWithEmptyText('edit_acl_name_".$acl_id."', '_statics.php?op=edit_acl&acl_id=$acl_id',
{
clickToEditText: '".$COLLATE['languages']['selected']['ClicktoEdit']."',
highlightcolor: '#a5ddf8',
callback:
function(form) {
new Element.update('notice', '');
return Form.serialize(form);
},
onFailure:
function(transport) {
new Element.update('notice', transport.responseText.stripTags());
}
}
);\n".
"--></script>\n";
}
}
echo "<tr style=\"display: none;\" id=\"add_acl\">
<td>
<input type=\"text\" name=\"acl_name\" /><br />
<input type=\"submit\" value=\" ".$COLLATE['languages']['selected']['Go']." \" />
<a href=\"#\" onclick=\"Effect.toggle($('add_acl'),'appear',{duration:0}); return false;\">".$COLLATE['languages']['selected']['altcancel']."</a>
</td>
<td style=\"vertical-align: top\"><input type=\"text\" name=\"acl_start\" /></td>
<td style=\"vertical-align: top\"><input type=\"text\" name=\"acl_end\" /></td>
</tr>
";
echo "</table></form>\n";
echo $javascript;
} // Ends list_statics function
function submit_acl(){
global $dbo;
include 'include/validation_functions.php';
$subnet_id = (isset($_GET['subnet_id']) && is_numeric($_GET['subnet_id'])) ? $_GET['subnet_id'] : '';
$acl_name = (isset($_POST['acl_name'])) ? $_POST['acl_name'] : '';
$acl_start = (isset($_POST['acl_start'])) ? $_POST['acl_start'] : '';
$acl_end = (isset($_POST['acl_end'])) ? $_POST['acl_end'] : '';
if(empty($subnet_id)){
$notice = "invalidrequest";
header("Location: blocks.php?notice=$notice");
exit();
}
if(empty($acl_name) || empty($acl_start) || empty($acl_end)){
$notice = "blankfield-notice";
header("Location: statics.php?subnet_id=$subnet_id¬ice=$notice");
exit();
}
$result = validate_text($acl_name,'aclname');
if($result['0'] === false){
$notice = $result['error'];
header("Location: statics.php?subnet_id=$subnet_id¬ice=$notice");
exit();
}
else{
$acl_name = $result['1'];
}
$result = validate_ip_range($acl_start,$acl_end,'acl',$subnet_id);
if($result['0'] === false){
$notice = $result['error'];
header("Location: statics.php?subnet_id=$subnet_id¬ice=$notice");
exit();
}
else{
$long_acl_start = $result['long_start_ip'];
$long_acl_end = $result['long_end_ip'];
$subnet_name = $result['subnet_name'];
}
AccessControl('3', "$acl_name ACL for $subnet_name subnet edited");
$sql = "INSERT INTO acl (name, start_ip, end_ip, subnet_id) VALUES ('$acl_name', '$long_acl_start', '$long_acl_end', '$subnet_id')";
$dbo -> query($sql);
$notice = "acladded-notice";
header("Location: statics.php?subnet_id=$subnet_id¬ice=$notice");
exit();
}
?>