-
Notifications
You must be signed in to change notification settings - Fork 50
/
actions.php
404 lines (319 loc) · 12 KB
/
actions.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
<?php
if (!function_exists('ssh2_connect')) {
echo "<b>php-ssh2</b> is not installed, this page will only work with installed php-ssh2.<br />\n";
exit();
}
$active_page = 'actions';
include_once('./include/config.php');
switch ($_REQUEST['action']) {
case 'kill_pid':
if($_REQUEST['pid']>0)
{
$cmd = 'sudo kill -9 '.$_REQUEST['pid'];
$message = 'Command "'.$cmd.'" executed';
}
else
{
$message = 'Process ID should be an integer';
}
break;
case 'kill_pname':
if(!empty($_REQUEST['pname']))
{
$cmd = 'sudo killall '.$_REQUEST['pname'];
$message = 'Command "'.$cmd.'" executed';
}
else
{
$message = 'Process Name shouldn\'t be an empty value';
}
break;
case 'start_sname':
if(!empty($_REQUEST['sname']))
{
$cmd = 'sudo service '.$_REQUEST['sname'].' start';
$message = 'Command "'.$cmd.'" executed';
}
else
{
$message = 'Service Name shouldn\'t be an empty value';
}
break;
case 'stop_sname':
if(!empty($_REQUEST['sname']))
{
$cmd = 'sudo service '.$_REQUEST['sname'].' stop';
$message = 'Command "'.$cmd.'" executed';
}
else
{
$message = 'Service Name shouldn\'t be an empty value';
}
break;
/*case 'update_sources':
$cmd = 'sudo apt-get update';
$message = 'Command "'.$cmd.'" executed';
break;
case 'update_firmware':
$cmd = 'sudo rpi-update';
$message = 'Command "'.$cmd.'" executed';
break;*/
case 'reboot':
$cmd = 'sudo reboot';
$message = 'Command "'.$cmd.'" executed';
break;
case 'cmd':
if(!empty($_REQUEST['cmd']))
{
$cmd = $_REQUEST['cmd'];
$message = 'Command "'.$cmd.'" executed';
}
else
{
$message = 'Command shouldn\'t be an empty value';
}
break;
}
?>
<?php
if(!file_exists(dirname(__FILE__).'/command_logs'))
{
$cmd = "sudo mkdir -m 777 ".dirname(__FILE__)."/command_logs && sudo chmod -R 777 ".dirname(__FILE__)."/command_logs";
}
else if(!is_writable ( dirname(__FILE__).'/command_logs' ))
{
$cmd = "sudo chmod -R 777 ".dirname(__FILE__)."/command_logs";
}
if(!empty($cmd))
{
$connection = ssh2_connect('localhost', SSH_PORT);
ssh2_auth_password($connection, SSH_USER, SSH_PASS);
$stream = ssh2_exec($connection, $cmd);
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
$message .= '<br/><b>Command output:</b><br/>'.nl2br(stream_get_contents($stream_out));
ssh2_exec($connection, 'exit');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="shortcut icon" href="./static/images/raspberry.png" type="image/png" />
<link rel="icon" href="./static/images/raspberry.png" type="image/png" />
<title>GumCP Actions</title>
<link href="./static/css.php" rel="stylesheet" type="text/css">
<script src="./static/js.php" type="text/javascript"></script>
<script>
$( document ).ready(function() {
$("#advanced_command").submit(function( event ) {
if(confirm("Are you sure you want to execute this command?"))
{
if($("#background_command").is(':checked'))
{
event.preventDefault();
$.post( "execute_command.php", $( "#advanced_command" ).serialize() )
.done(function( data ) {
alert("command sent!");
$('#cmd').val('');
});
return false;
}
else
{
return true;
}
}
else
{
event.preventDefault();
return false;
}
});
});
function delete_log(file)
{
if(confirm("Are you sure you want to delete this log?"))
{
event.preventDefault();
$.post( "ajax.php", {'action':'delete_log','log_file':file} )
.done(function( data ) {
alert(data);
$('div.'+file.replace(".", "_")).remove();
});
}
else
{
}
}
</script>
</head>
<body>
<div class="container">
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="./index.php"><img src="./static/images/raspberry.png" />GumCP</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<?php
include_once('./include/menu.php');
?>
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container-fluid -->
</nav>
<div id="system-status" class="panel panel-default" style="margin-bottom: 5px">
<div class="panel-heading">
<h3 class="panel-title">Actions</h3>
</div>
<div class="panel-body">
<?php
if(!empty($message))
{
echo '<div class="alert alert-info" role="alert" style="margin-bottom:20px;">'.$message.'</div>';
}
?>
<form method="post">
<div class="form-group row">
<label class="col-sm-2 form-control-label" for="pid">Kill Process by ID</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="pid" name="pid" placeholder="Process ID (PID)">
<input type="hidden" class="form-control" name="action" value="kill_pid">
<small class="text-muted">Provide the PID to kill.</small>
</div>
<div class="col-sm-4">
<button type="submit" class="btn btn-primary" onclick="return confirm('Are you sure?')">Kill</button>
</div>
</div>
</form>
<form method="post" style="margin-top: 20px;">
<div class="form-group row">
<label class="col-sm-2 form-control-label" for="pname">Kill Processes by Name</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="pname" name="pname" placeholder="Process Name">
<input type="hidden" class="form-control" name="action" value="kill_pname">
<small class="text-muted">Provide the process name to kill.</small>
</div>
<div class="col-sm-4">
<button type="submit" class="btn btn-primary" onclick="return confirm('Are you sure?')">Kill</button>
</div>
</div>
</form>
<form method="post" style="margin-top: 20px;">
<div class="form-group row">
<label class="col-sm-2 form-control-label" for="sname">Start Service by Name</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="sname" name="sname" placeholder="Service Name">
<input type="hidden" class="form-control" name="action" value="start_sname">
<small class="text-muted">Provide the service name to start.</small>
</div>
<div class="col-sm-4">
<button type="submit" class="btn btn-primary" onclick="return confirm('Are you sure?')">Start</button>
</div>
</div>
</form>
<form method="post" style="margin-top: 20px;">
<div class="form-group row">
<label class="col-sm-2 form-control-label" for="sname">Stop Service by Name</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="sname" name="sname" placeholder="Service Name">
<input type="hidden" class="form-control" name="action" value="stop_sname">
<small class="text-muted">Provide the service name to stop.</small>
</div>
<div class="col-sm-4">
<button type="submit" class="btn btn-primary" onclick="return confirm('Are you sure?')">Stop</button>
</div>
</div>
</form>
<!--form method="post" style="margin-top: 20px;">
<div class="form-group row">
<label class="col-sm-2 form-control-label">Update Sources</label>
<input type="hidden" class="form-control" name="action" value="update_sources">
<div class="col-sm-4">
<button type="submit" class="btn btn-primary" onclick="return confirm('Are you sure?')">Update</button>
</div>
</div>
</form>
<form method="post" style="margin-top: 20px;">
<div class="form-group row">
<label class="col-sm-2 form-control-label">Update Firmware</label>
<input type="hidden" class="form-control" name="action" value="update_firmware">
<div class="col-sm-4">
<button type="submit" class="btn btn-primary" onclick="return confirm('Are you sure?')">Update</button>
</div>
</div>
</form-->
<form method="post" style="margin-top: 20px;">
<div class="form-group row">
<label class="col-sm-2 form-control-label">Reboot RPi System</label>
<input type="hidden" class="form-control" name="action" value="reboot">
<div class="col-sm-4">
<button type="submit" class="btn btn-primary" onclick="return confirm('Are you sure to reboot RPi?')">Reboot</button>
</div>
</div>
</form>
<hr/>
<form method="post" id="advanced_command" style="margin-top: 20px;">
<div class="form-group row">
<label class="col-sm-2 form-control-label" for="cmd">Execute command <b>(Advanced users only!)</b></label>
<div class="col-sm-4">
<input type="text" class="form-control" id="cmd" name="cmd" placeholder="Command">
<input type="hidden" class="form-control" name="action" value="cmd">
<small class="text-muted">Command usually starts with sudo .....</small>
</div>
<div class="col-sm-4">
<div class="checkbox">
<label><input type="checkbox" id="background_command" value="">Execute in background</label>
</div>
</div>
<div class="col-sm-4">
<button type="submit" class="btn btn-primary">Execute</button>
</div>
</div>
</form>
<hr/>
<div class="well well-sm" id="command_logs"><h3>Useful commands:</h3>
<code>sudo python /folder_path_to_file/mypython_file.py</code> to execute a python script.<br/>
<code>sudo apt-get update</code> to update RPi sources.<br/>
<code>sudo rpi-update</code> to update RPi firmware.<br/>
<code>sudo reboot</code> to reboot RPi.<br/>
<code>cd <?php echo dirname(__FILE__); ?> && sudo git pull origin</code> to update GumCP (after this you have to edit the config.php file manually).<br/>
<code>sudo chmod 0777 <?php echo dirname(__FILE__); ?>/include/config.php</code> to make the config file writable in case you can't write any data to it.
</div>
<?php
$directory = dirname(__FILE__).'/command_logs/';
$scanned_directory = array_diff(scandir($directory), array('..', '.'));
if(is_array($scanned_directory))
{
echo '<div class="well well-sm" id="command_logs"><h3>Background command output logs:</h3>';
foreach($scanned_directory AS $file)
{
echo '<div class="'.str_replace(".", "_", $file).'"><a href="./command_logs/'.$file.'">'.$file.'</a> '. date ("F d Y H:i:s.", filemtime('./command_logs/'.$file)).' <a href="javascript:void(0);" onclick="javascript:delete_log(\''.$file.'\');" title="delete_log" style="color:red;"><i class="fa fa-trash-o" aria-hidden="true"></i></a></div>';
}
echo '</div>';
}
?>
</div>
</div>
</div>
<footer class="footer">
<div class="container">
<p class="text-muted">GumCP <a href="https://github.com/gumslone/GumCP">GitHub</a>. <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=VCWHQPACTXV5N"><img src="./static/images/Donate-PayPal-green.svg"/></a></p>
</div>
</footer>
<div id="dialog-placeholder"></div>
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
</body>
</html>