-
Notifications
You must be signed in to change notification settings - Fork 9
/
snippets.php
160 lines (132 loc) · 4.57 KB
/
snippets.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
<?php
require_once('php/session.php');
session_write_close();
require_once('php/functions.php');
require_once('config.php');
require_once('php/switchoperations.php');
require_once('lang.php');
$cSwitch = null;
if(!empty($_GET['switch'])) {
$cSwitch = getSwitchByAddr($_GET['switch']);
}
$port = "";
if(!empty($_GET['port']))
$port = removeInvalidChars($_GET['port']);
$cSnippet = null;
$info = null;
$infoclass = null;
if(!empty($_POST['snippet'])) {
$cSnippet = getSnippetById($_POST['snippet']);
}
$cmdResponse = null;
if($cSwitch != null && $cSnippet != null) {
if(empty($cSnippet['users']) || in_array($_SESSION['username'], $cSnippet['users'])) {
$cmd = '';
if($cSnippet['scope'] == 'port') {
if(empty($port)) {
$info = translate('Port-Snippet cannot be executed because no port name was given', false).' ('.$cSnippet['name'].')';
$infoclass = 'error';
} else {
$cmd = str_replace('%PORT%', $port, $cSnippet['cmd']) . "\n" . 'exit' . "\n";
}
} else {
$cmd = $cSnippet['cmd'] . "\n" . 'exit' . "\n";
}
if(!empty($cmd)) {
$cmdResponse = executeRawShell($cSwitch['addr'], $cmd);
$info = translate('Snippet executed', false).' ('.$cSnippet['name'].')';
$infoclass = 'ok';
//$info = translate('Execution Preview:', false).' '.$cmd.'';
//$infoclass = 'info';
}
} else {
$info = translate('You are not allowed to run this snippet.', false).' ('.$cSnippet['name'].')';
$infoclass = 'error';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title><?php translate('Snippets'); ?> - <?php translate('Switchconfig'); ?></title>
<?php require('head.inc.php'); ?>
<link rel='stylesheet' type='text/css' href='css/intstatuslist.css'>
</head>
<body>
<script>
function beginFadeOutAnimation() {
document.getElementById('imgSwitch').style.opacity = 0;
document.getElementById('imgLoading').style.opacity = 1;
var buttons = document.getElementsByTagName("button");
for(i = 0 ; i < buttons.length ; i++) {
buttons[i].classList.add("disabled");
}
document.getElementsByTagName("body")[0].classList.add("loading");
}
</script>
<div id='container'>
<h1 id='title'><div id='logo'></div></h1>
<div id='splash' <?php if($cmdResponse !== null) { ?>class='big'<?php } ?>>
<?php if($cSwitch != null) { ?>
<h2>
<?php echo $cSwitch['name']; ?>
<?php if(!empty($port)) echo "<br>".htmlspecialchars($port); ?>
</h2>
<?php } else { ?>
<div class='infobox warn'><?php translate('No switch selected'); ?></div>
<?php } ?>
<hr/>
<div id='subtitle'>
<div id='imgContainer'>
<img id='imgLoading' src='img/loading.svg'></img>
<img id='imgSwitch' src='img/switch.png'></img>
</div>
<?php translate('Send pre-defined commands to your switch.'); ?>
</div>
<table width='100%' id='logintable'>
<tr><td>
<?php if($info != null) {?>
<div class='infobox <?php echo htmlspecialchars($infoclass); ?>'><?php echo htmlspecialchars($info); ?></div>
<?php } ?>
<?php
if($cSwitch != null) {
if($cmdResponse === null) {
// show snippet list
foreach(SNIPPETS as $snippet) {
if(!empty($snippet['users'])) {
if(!in_array($_SESSION['username'], $snippet['users'])) continue;
}
if($snippet['scope'] == 'switch' || ($snippet['scope'] == 'port' && !empty($port))) {
$img = '';
if($snippet['scope'] == 'switch') $img = 'img/switch.png';
if($snippet['scope'] == 'port') $img = 'img/notconnected.svg';
echo "<form method='POST'>";
echo "<input type='hidden' name='snippet' value='".htmlspecialchars($snippet['id'])."'>";
echo "<button class='fullwidth slubbutton secondary notypo marginbottom' onclick='beginFadeOutAnimation();'><img class='smallimg' src='".$img."'> ".htmlspecialchars($snippet['name'])."</button>";
echo "</form>";
}
}
} else {
// show cmd output
echo "<textarea id='intstatustext' wrap='off' readonly='true'>" . htmlspecialchars($cmdResponse) . "</textarea>";
}
}
?>
</td></tr>
<tr>
<td>
<div class='spread-toolbar toolbar-margin-top'>
<a href='index.php?switch=<?php echo urlencode($cSwitch['addr']); ?>&port=<?php echo urlencode($port); ?>' onclick='beginFadeOutAnimation();'>><?php translate('Back'); ?></a>
<?php if($cmdResponse !== null) { ?>
<a href='snippets.php?switch=<?php echo urlencode($cSwitch['addr']); ?>&port=<?php echo urlencode($port); ?>' onclick='beginFadeOutAnimation();'>><?php translate('Execute Another Snippet'); ?></a>
<?php } ?>
</div>
</td>
</tr>
</table>
</div>
<?php require('foot.inc.php'); ?>
</div>
<?php require('menu.inc.php'); ?>
</body>
</html>