Reputation: 11
A terminal emulator has to be embedded in a web page, in which the user has to compile and run his java program created on server by web page access. I want to compile and run the program in terminal. The client not needed to install any application software.
Upvotes: 1
Views: 1065
Reputation: 2271
First, you can create a div for displaying results and a textarea for the commands. Check for keypress of the textarea and send the command to the server using AJAX when the user presses enter. You can style them to look more terminal-alike.
Beware that expose the console to the users can easily get hacked. Always check the user input to ensure that dangerous commands cannot be executed.
Sample code of how to filter commands:
<?php
$cmd = $_POST['cmd'];
$allowed = array('ls','git','grep','...');//The allowed commands
foreach($allowed as $part){
if(strpos($cmd,$part) === false){
echo 'Command not recognized!';
}
}
ob_start();
passthru($cmd);
$result = ob_end_clean();
echo json_encode(array('result'=>$result));
Hope this can help you.
Upvotes: 1
Reputation: 5841
I would recommend the following:
You should be very careful when opening up the console to the user though. You can further restrict the terminal's ability by explicitly prepending a function to all terminal commands, e.g. <?php echo exec('git '.$_GET['cmdtxt']); ?>
is one way to ensure that git is called, but it's still vulnerable since you could potential execute a second command. I'm sure you can figure out what your security needs are and properly validate them.
If this seems vague, it's only because your question isn't all too specific. Is this the answer you were looking for? Let me know :)
Upvotes: 3