-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
508 lines (452 loc) · 14.2 KB
/
index.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
<?php
/***********************************************************************
* Load required classes.
***********************************************************************/
foreach (glob(dirname(__FILE__) . '/classes/*.php') as $class) {
require_once($class);
}
unset($class);
/***********************************************************************
* Load config file.
***********************************************************************/
$config = array('ouras' => '', 'company' => '');
require_once('config.php');
/***********************************************************************
* Load Commands
***********************************************************************/
$commands = array();
$defaultCommand = '-1';
$commandCount = 0;
/**
* Register an object as a command.
*/
function registerCommand($name, $object) {
global $commands, $defaultCommand, $commandCount;
$id = abs(crc32($name));
$commands[$id] = array('Name' => $name, 'Object' => $object);
if ($defaultCommand == '-1') { $defaultCommand = $id; };
$commandCount++;
return $id;
}
foreach (glob(dirname(__FILE__) . '/classes/Commands/*Command.php') as $command) {
require_once($command);
}
unset($command);
/***********************************************************************
* Router related functions
***********************************************************************/
$routers = array();
$routerMap = array();
$defaultRouter = '-1';
$routerCount = 0;
/**
* Reset Router List.
*/
function resetRouters() {
global $routers, $routerMap, $defaultRouter, $routerCount;
$routers = array();
$routerMap = array();
$defaultRouter = '-1';
$routerCount = 0;
}
/**
* Register a router group.
*
* @param $name Name of group
* @param $description Description of group
*/
function registerRouterGroup($name, $description = '') {
global $routers;
if ($description == '') { $description = $name; }
$routers[$name] = array('Name' => $name,
'Description' => $description,
'Routers' => array());
}
/**
* Register a router.
*
* @param $name Name of group
* @param $object Router object.
*/
function registerRouter($name, $object, $group = 'Routers') {
global $routers, $routerMap, $defaultRouter, $routerCount;
if (!isset($routers[$group])) { registerRouterGroup($group); }
$id = abs(crc32($group)) . '-' . abs(crc32($name));
$routers[$group]['Routers'][] = array('ID' => $id, 'Name' => $name);
$routerMap[$id] = array('Name' => $name, 'Object' => $object);
if ($defaultRouter == '-1') { $defaultRouter = $id; };
$routerCount++;
return $id;
}
/**
* Set the default Router ID.
*
* @param $id ID of default router. If an unknown ID is given, the
* default will not change.
* @return The ID of the default router.
*/
function setDefaultRouter($id) {
global $defaultRouter, $routerMap;
$defaultRouter = isset($routerMap[$id]) ? $id : $defaultRouter;
return $defaultRouter;
}
/**
* Check if the current user is an admin.
*
* @return True if the user is an admin.
*/
function isAdmin() {
global $config;
return isset($_SERVER['REMOTE_ADDR']) && isset($config['admin']) && in_array($_SERVER['REMOTE_ADDR'], $config['admin']);
}
/**
* Get a link pointing to the requested command.
*/
function getLink($command = null, $input = null, $protocol = null, $router = null) {
global $defaultCommand, $commands, $routerMap, $defaultRouter;
$params = array();
$params['command'] = $defaultCommand;
if ($command != null) {
foreach ($commands as $id => $c) {
if (strtolower($command) == strtolower($c['Name']) || $command == $id) {
$params['command'] = $id;
break;
}
}
}
if ($protocol == null) { $protocol = $_REQUEST['protocol']; }
$params['protocol'] = ($protocol == 'ipv6') ? 'ipv6' : 'ipv4';
$params['router'] = isset($_REQUEST['router']) ? $_REQUEST['router'] : $defaultRouter;
if ($router !== null) {
foreach ($routerMap as $id => $r) {
if (strtolower($router) == strtolower($r['Name']) || $router == $id) {
$params['router'] = $id;
break;
}
}
}
$params['runcommand'] = 'Submit Query';
if ($input !== null) {
$params['input'] = $input;
}
$result = array();
foreach ($params as $k => $v) {
$result[] = urlencode($k) . '=' . urlencode($v);
}
return 'index.php?' . implode('&', $result);
}
/***********************************************************************
* Load routers file.
***********************************************************************/
if (file_exists(dirname(__FILE__) . '/routers.php')) {
resetRouters();
require_once(dirname(__FILE__) . '/routers.php');
}
$hasResult = false;
/***********************************************************************
* Begin
***********************************************************************/
?>
<HTML>
<HEAD>
<TITLE><?php echo $config['company']; ?> Looking Glass</TITLE>
<?php if (!isset($config['cssfile']) || !isset($config['hidedefaultcss']) || !$config['hidedefaultcss']) { ?>
<STYLE type="text/css">
.center {
margin-left: auto;
margin-right: auto;
text-align: center;
}
table.commandtable {
background: #EEE;
}
table.commandtable td {
}
table.commandtable tr th {
background: black;
color: white;
}
table.commandtable tr td {
color: black;
border: 0px;
}
div.footercontent {
text-align: right;
font-size: small;
width: 100%;
border-top: 1px solid black;
}
div.result {
margin: 5px auto;
border: none;
padding: 2px;
width: 95%;
min-width: 800px;
}
div.errorbox, div.successbox {
border: 1px dashed #cc0000;
background-color: #FBEEEB;
color: #cc0000;
/* font-weight: bold; */
text-align: center;
width: 800px;
padding: 10px;
margin: 10px auto;
}
div.successbox {
border: 1px dashed #00cc00;
background-color: #EEFBEB;
color: #009900;
}
div.runinfo table {
width: 800px;
}
div.runinfo table td {
text-align: left;
}
div.runinfo table th {
text-align: right;
padding-right: 5px;
width: 33%;
}
div.result div.output {
text-align: left;
min-width: 800px;
margin: 20px auto;
padding: 10px;
padding-right: none;
border-left: 4px solid black;
background: #EEE;
}
div.result div.output span.bad {
color: red;
}
div.result div.output span.good {
color: green;
font-weight: bold;
}
</STYLE>
<?php }
if (isset($config['cssfile'])) {
if (!is_array($config['cssfile'])) {
$config['cssfile'] = array($config['cssfile']);
}
foreach ($config['cssfile'] as $file) {
if (!empty($file)) {
echo '<link href="', $file, '" rel="stylesheet" type="text/css">';
}
}
}
if (isset($config['scriptfile'])) {
if (!is_array($config['scriptfile'])) {
$config['scriptfile'] = array($config['scriptfile']);
}
foreach ($config['scriptfile'] as $file) {
if (!empty($file)) {
echo '<script type="text/javascript" src="', $file, '"></script>';
}
}
}
?>
</HEAD>
<BODY>
<div class="page">
<div class="header">
<?php
if (isset($config['headerfile']) && !empty($config['headerfile']) && file_exists($config['headerfile'])) {
include($config['headerfile']);
} else {
if (isset($config['logo'])) {
echo '<div class="logo center">';
if (isset($config['url'])) {
echo '<a href="', $config['url'],'">';
}
echo '<img src="', $config['logo'], '" alt="', $config['company'], '">';
if (isset($config['url'])) {
echo '</a>';
}
echo '</div>';
echo '<h1 class="center">', $config['company'],' Looking Glass</h1>';
}
}
?>
</div>
<div class="body">
<?php
$showForm = true;
$formError = '';
if ($commandCount == 0) {
$formError = 'No commands found. Please check that '.dirname(__FILE__).'/classes/Commands/ exists.';
$showForm = false;
} else if ($routerCount == 0) {
$formError = 'No routers found. Please check your config file.';
$showForm = false;
}
if ($showForm && isset($_REQUEST['runcommand'])) {
$runcmd = array();
$runcmd['router'] = isset($_REQUEST['router']) && isset($routerMap[$_REQUEST['router']]) ? $routerMap[$_REQUEST['router']] : null;
$runcmd['command'] = isset($_REQUEST['command']) && isset($commands[$_REQUEST['command']]) ? $commands[$_REQUEST['command']] : null;
$runcmd['input'] = isset($_REQUEST['input']) && isset($_REQUEST['input']) ? $_REQUEST['input'] : '';
$runcmd['protocol'] = isset($_REQUEST['protocol']) ? $_REQUEST['protocol'] : 'ipv4';
// Run the command here to get the output.
ob_start();
if (empty($runcmd['router'])) {
$runcmd['error'] = 'No valid router selected.';
$runcmd['result'] = FALSE;
} else if (empty($runcmd['command'])) {
$runcmd['error'] = 'No valid command selected.';
$runcmd['result'] = FALSE;
} else {
$runcmd['command']['Object']->setProtocol($runcmd['protocol']);
$runcmd['router']['Object']->connect();
$runcmd['commandstring'] = $runcmd['command']['Object']->getCommandString($runcmd['router']['Object'], $runcmd['input']);
$runcmd['result'] = $runcmd['command']['Object']->run($runcmd['router']['Object'], $runcmd['input']);
}
$runcmd['output'] = ob_get_contents();
ob_end_clean();
}
?>
<?php if ($showForm) { ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="GET">
<table class="commandtable center" width="800px">
<tr>
<th width="33%">Command</th>
<th width="34%">Arguments</th>
<th width="33%">Router</th>
</tr>
<tr>
<td>
<table class="commands center">
<?php foreach ($commands as $id => $command) { ?>
<?php
if (isset($_REQUEST['command'])) {
$selected = ($id == $_REQUEST['command']) ? 'checked' : '';
} else {
$selected = ($id == $defaultCommand) ? 'checked' : '';
}
?>
<tr>
<td>
<input type="radio" name="command" value="<?php echo $id; ?>" <?php echo $selected; ?>/>
</td>
<td style="text-align: left">
<?php echo htmlspecialchars($command['Name']); ?>
</td>
</tr>
<? } ?>
<tr><td colspan=2> </td></tr>
<tr>
<td> </td>
<td>Protocol:
<select name="protocol">
<option value="ipv4" <?php echo (empty($_REQUEST['protocol']) || $_REQUEST['protocol'] == 'ipv4' ? 'selected' : '');?>>IPv4</option>
<option value="ipv6" <?php echo (!empty($_REQUEST['protocol']) && $_REQUEST['protocol'] == 'ipv6' ? 'selected' : '');?>>IPv6</option>
</select>
</td>
</tr>
</table>
</td>
<td>
<input type="text" name="input" style="width: 80%;" value="<?php echo isset($_REQUEST['input']) ? htmlspecialchars($_REQUEST['input']) : ''; ?>"></input>
</td>
<td>
<select name="router">
<?php foreach ($routers as $gid => $group) {
if (count($routers) > 1) {
echo '<optgroup label="', $group['Description'], '">';
}
foreach ($group['Routers'] as $router) {
if (isset($_REQUEST['router'])) {
$selected = ($router['ID'] == $_REQUEST['router']) ? 'selected' : '';
} else {
$selected = ($router['ID'] == $defaultRouter) ? 'selected' : '';
}
echo '<option value="', htmlspecialchars($router['ID']), '" ', $selected, '>', htmlspecialchars($router['Name']), '</option>';
}
if (count($routers) > 1) {
echo '</optgroup>';
}
} ?>
</select>
</td>
</tr>
<tr><td colspan="3"> </td></tr>
<tr>
<td colspan="2"> </td>
<td>
<input type="submit" name="runcommand" value="Submit Query">
<input type="reset" value="Reset">
</td>
</tr>
</table>
</form>
<?php } elseif (!empty($formError)) {
echo '<div class="errorbox">';
echo '<Strong>Error:</strong> ', $formError;
echo '</div>';
} ?>
</div>
<?php if (isset($runcmd)) { ?>
<div class="result">
<div class="runinfo">
<table class="center">
<?php if (!empty($runcmd['router'])) { ?>
<tr>
<th>Router</th>
<td><?php echo $runcmd['router']['Name']; ?></td>
</tr>
<?php } ?>
<?php if (!empty($runcmd['commandstring'])) { ?>
<tr>
<th>Command</th>
<td><?php echo $runcmd['commandstring']; ?></td>
</tr>
<?php } ?>
</table>
<?php
// If the command did not succeed, show the error.
if ($runcmd['result'] === FALSE) {
echo '<div class="errorbox">';
echo '<Strong>Error:</strong> There was an error running the requested command.';
if (isset($runcmd['error'])) {
$error = $runcmd['error'];
} else if (empty($runcmd['command'])) {
$error = 'Invalid command given.';
} else {
$error = $runcmd['command']['Object']->getError();
}
if (!empty($error)) {
echo '<br>';
echo $error;
}
echo '</div>';
}
?>
</div>
<?php if ($runcmd['result'] !== FALSE) { ?>
<div class="center output">
<?php echo $runcmd['output']; ?>
</div>
<?php } ?>
</div>
<?php } ?>
<div class="footer">
<?php
if (isset($config['footerfile']) && !empty($config['footerfile']) && file_exists($config['footerfile'])) {
include($config['footerfile']);
} else {
$year = '2011';
if (date('Y') != $year) {
$year .= ' - ';
$year .= date('Y');
}
?>
<div class="footercontent">
<a href="http://github.com/ShaneMcC/LookingGlass">PHP Looking Glass</a> © <?echo $year; ?> - Shane "Dataforce" Mc Cormack
</div>
<?php }
?>
</div>
</div>
</BODY>
</HTML>