Started moving actions into a single php file to simplify things

This commit is contained in:
akuker 2020-08-07 11:34:07 -05:00
parent 8eb8273141
commit c3e8204542
3 changed files with 443 additions and 135 deletions

View File

@ -1,4 +1,3 @@
<!-- PHP source code for controlling the RaSCSI - 68kmla edition with a web interface. -->
<!-- Copyright (c) 2020 akuker -->
<!-- Distributed under the BSD-3 Clause License -->
@ -7,64 +6,64 @@
<?php
$FILE_PATH='/home/pi/images';
$FILE_PATH='/home/pi/images'. PHP_EOL;
function html_generate_header(){
echo ' <table width="100%" >';
echo ' <tr style="background-color: black;">';
echo ' <td style="background-color: black;"><a href=http://github.com/akuker/RASCSI><h1>RaSCSI - 68kmla Edition</h1></a></td>';
echo ' <td style="background-color: black;">';
echo ' <form action="/rascsi.php">';
echo ' <input type="submit" value="Refresh"/>';
echo ' <p style="color:white">'.time().'</p>';
echo ' </form>';
echo ' </td>';
echo ' </tr>';
echo ' </table>';
echo ' <table width="100%" >'. PHP_EOL;
echo ' <tr style="background-color: black;">'. PHP_EOL;
echo ' <td style="background-color: black;"><a href=http://github.com/akuker/RASCSI><h1>RaSCSI - 68kmla Edition</h1></a></td>'. PHP_EOL;
echo ' <td style="background-color: black;">'. PHP_EOL;
echo ' <form action="/rascsi.php">'. PHP_EOL;
echo ' <input type="submit" value="Refresh"/>'. PHP_EOL;
echo ' <p style="color:white">'.time().'</p>'. PHP_EOL;
echo ' </form>'. PHP_EOL;
echo ' </td>'. PHP_EOL;
echo ' </tr>'. PHP_EOL;
echo ' </table>'. PHP_EOL;
//echo(exec('whoami'));
}
function html_generate_scsi_id_select_list(){
echo '<select>';
echo '<select>'. PHP_EOL;
foreach(range(0,7) as $id){
echo '<option value="'.$id.'">'.$id.'</option>';
echo '<option value="'.$id.'">'.$id.'</option>'. PHP_EOL;
}
echo '</select>';
echo '</select>'. PHP_EOL;
}
function html_generate_scsi_type_select_list(){
echo '<select name=type>';
echo '<select name=type>'. PHP_EOL;
$options = array("Hard Disk", "CD-ROM", "Zip Drive", "Ethernet Tap", "Filesystem Bridge");
foreach($options as $type){
echo '<option value="'.$type.'">'.$type.'</option>';
echo '<option value="'.$type.'">'.$type.'</option>'. PHP_EOL;
}
echo '</select>';
echo '</select>'. PHP_EOL;
}
function html_generate_warning($message){
echo ' <table width="100%" >';
echo ' <tr style="background-color: red;">';
echo ' <td style="background-color: red;">';
echo ' <font size=+4>'.$message.'</font>';
echo ' </td>';
echo ' </tr>';
echo ' </table>';
echo ' <table width="100%" >'. PHP_EOL;
echo ' <tr style="background-color: red;">'. PHP_EOL;
echo ' <td style="background-color: red;">'. PHP_EOL;
echo ' <font size=+4>'.$message.'</font>'. PHP_EOL;
echo ' </td>'. PHP_EOL;
echo ' </tr>'. PHP_EOL;
echo ' </table>'. PHP_EOL;
}
function html_generate_success_message(){
echo ' <table width="100%" >';
echo ' <tr style="background-color: green;">';
echo ' <td style="background-color: green;">';
echo ' <font size=+2>Success!</font>';
echo ' </td>';
echo ' </tr>';
echo ' </table>';
echo ' <table width="100%" >'. PHP_EOL;
echo ' <tr style="background-color: green;">'. PHP_EOL;
echo ' <td style="background-color: green;">'. PHP_EOL;
echo ' <font size=+2>Success!</font>'. PHP_EOL;
echo ' </td>'. PHP_EOL;
echo ' </tr>'. PHP_EOL;
echo ' </table>'. PHP_EOL;
}
function html_generate_ok_to_go_home(){
echo ' <form action="/rascsi.php">';
echo ' <input type="submit" value="OK"/>';
echo ' </form>';
echo ' <form action="/rascsi.php">'. PHP_EOL;
echo ' <input type="submit" value="OK"/>'. PHP_EOL;
echo ' </form>'. PHP_EOL;
}
@ -72,15 +71,15 @@ function current_rascsi_config() {
$raw_output = shell_exec("/usr/local/bin/rasctl -l");
$rasctl_lines = explode(PHP_EOL, $raw_output);
echo ' <br>';
echo ' <h2>Current RaSCSI Configuration</h2>';
echo ' <table border="black">';
echo ' <tr>';
echo ' <td><b>SCSI ID</b></td>';
echo ' <td><b>Type</b></td>';
echo ' <td><b>Image File</b></td>';
echo ' <td><b>Actions</b></td>';
echo ' </tr>';
echo ' <br>'. PHP_EOL;
echo ' <h2>Current RaSCSI Configuration</h2>'. PHP_EOL;
echo ' <table border="black">'. PHP_EOL;
echo ' <tr>'. PHP_EOL;
echo ' <td><b>SCSI ID</b></td>'. PHP_EOL;
echo ' <td><b>Type</b></td>'. PHP_EOL;
echo ' <td><b>Image File</b></td>'. PHP_EOL;
echo ' <td><b>Actions</b></td>'. PHP_EOL;
echo ' </tr>'. PHP_EOL;
$scsi_ids = array();
@ -108,31 +107,43 @@ function current_rascsi_config() {
foreach (range(0,7) as $id){
echo ' <tr>';
echo ' <form>';
echo ' <td style="text-align:center">'.$id.'</td>';
echo ' <tr>'. PHP_EOL;
echo ' <td style="text-align:center">'.$id.'</td>'. PHP_EOL;
if(isset($scsi_ids[$id]))
{
echo ' <td style="text-align:center">'.$scsi_ids[$id]['type'].'</td>';
echo ' <td>'.$scsi_ids[$id]['file'].'</td>';
echo ' <td>';
echo ' <input type="button" value="Eject" onClick="eject_image(\''.$id.'\',\''.$scsi_ids[$id]['file'].'\')"/>';
echo ' <input type="button" value="Disconnect" onClick="remove_device('.$id.')"/>';
echo ' </td>';
echo ' <td style="text-align:center">'.$scsi_ids[$id]['type'].'</td>'. PHP_EOL;
echo ' <td>'.$scsi_ids[$id]['file'].'</td>'. PHP_EOL;
echo ' <td>'. PHP_EOL;
echo ' <form action="rascsi_action.php" method="post">'. PHP_EOL;
echo ' <input type="hidden" name="command" value="eject" />'. PHP_EOL;
echo ' <input type="hidden" name="id" value="'.$id.'" />'. PHP_EOL;
echo ' <input type="hidden" name="file" value="'.$scsi_ids[$id]['file'].'" />'. PHP_EOL;
echo ' <input type="submit" name="eject" value="Eject" />'. PHP_EOL;
echo ' </form>'. PHP_EOL;
echo ' <form action="rascsi_action.php" method="post">'. PHP_EOL;
echo ' <input type="hidden" name="command" value="remove_device" />'. PHP_EOL;
echo ' <input type="hidden" name="id" value="'.$id.'" />'. PHP_EOL;
echo ' <input type="submit" name="remove_device" value="Disconnect" />'. PHP_EOL;
echo ' </form>'. PHP_EOL;
echo ' </td>'. PHP_EOL;
}
else
{
echo ' <td style="text-align:center">-</td>';
echo ' <td>-</td>';
echo ' <td>';
echo ' <input type="button" value="Connect New Device" onClick="add_device('.$id.')"/>';
echo ' </td>';
echo ' <td style="text-align:center">-</td>'. PHP_EOL;
echo ' <td>-</td>'. PHP_EOL;
echo ' <td>'. PHP_EOL;
echo ' <form action="rascsi_action.php" method="post">'. PHP_EOL;
echo ' <input type="hidden" name="command" value="connect_new" />'. PHP_EOL;
echo ' <input type="hidden" name="id" value="'.$id.'" />'. PHP_EOL;
echo ' <input type="submit" name="connect_new" value="Connect New Device" />'. PHP_EOL;
echo ' </form>'. PHP_EOL;
echo ' </td>'. PHP_EOL;
}
echo ' </form>';
echo ' </tr>';
echo ' </form>'. PHP_EOL;
echo ' </tr>'. PHP_EOL;
}
echo '</table>';
echo '</table>'. PHP_EOL;
}
function get_all_files()
{
@ -189,4 +200,4 @@ function type_string_to_rasctl_type($typestr){
?>
?>

View File

@ -1,4 +1,3 @@
<!-- PHP source code for controlling the RaSCSI - 68kmla edition with a web interface. -->
<!-- Copyright (c) 2020 akuker -->
<!-- Distributed under the BSD-3 Clause License -->
@ -7,71 +6,58 @@
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="rascsi_styles.css">
<script>
function compute(f) {
if (confirm("Are you sure?"))
alert("Yes");
else
alert("No");
}
function eject_image(id,file){
<head>
<link rel="stylesheet" href="rascsi_styles.css">
<script>
function compute(f) {
if (confirm("Are you sure?"))
alert("Yes");
else
alert("No");
}
function eject_image(id, file) {
var url = "eject.php?id=" + encodeURIComponent(id) + "&file=" + encodeURIComponent(file);
window.location.href = url;
}
function insert_image(id,file){
if(confirm("Not implemented yet.... would insert " + file + " into " + id))
alert("OK");
}
function add_device(id){
var url = "add_device.php?id=" + encodeURIComponent(id);
window.location.href = url;
}
function remove_device(id){
confirm_message = "Are you sure you want to disconnect ID " + id + "? This may cause unexpected behavior on the host computer if it is still running";
if(confirm(confirm_message)){
var url = "disconnect.php?id=" + encodeURIComponent(id);
window.location.href=url;
}
}
}
function delete_file(f){
if(confirm("Are you sure you want to delete "+f+"?"))
alert("OK");
}
function insert_image(id, file) {
if (confirm("Not implemented yet.... would insert " + file + " into " + id))
alert("OK");
}
</script>
</head>
<body>
function add_device(id) {
var url = "add_device.php?id=" + encodeURIComponent(id);
window.location.href = url;
}
function remove_device(id) {
confirm_message = "Are you sure you want to disconnect ID " + id +
"? This may cause unexpected behavior on the host computer if it is still running";
if (confirm(confirm_message)) {
var url = "disconnect.php?id=" + encodeURIComponent(id);
window.location.href = url;
}
}
function delete_file(f) {
if (confirm("Are you sure you want to delete " + f + "?"))
alert("OK");
}
</script>
</head>
<body>
<?php
include 'lib_rascsi.php';
html_generate_header();
// parameter check
if(isset($_GET['restart_rascsi_service'])){
// Restart the RaSCSI service
exec("sudo /bin/systemctl restart rascsi.service");
} else if(isset($_GET['stop_rascsi_service'])){
// Stop the RaSCSI Service
exec("sudo /bin/systemctl stop rascsi.service");
} else if(isset($_GET['reboot_rasbperry_pi'])){
// Reboot the Raspberry Pi
exec("sudo /sbin/reboot");
} else if(isset($_GET['shutdown_raspberry_pi'])){
// Shut down the Raspberry Pi
echo "<h1>For now, shutdown is disabled....</h1>";
echo 'exec("sudo /sbin/shutdown -s -t 0");';
}
current_rascsi_config();
?>
<br>
<br>
<h2>Image File Management</h2>
<table border="black">
<tr>
@ -82,8 +68,8 @@ function delete_file(f){
<td><b>Date Modified</b></td>
<td><b>Actions</b></td>
</tr>
<tr>
<?php
<tr>
<?php
// Generate the table with all of the file names in it.....
$all_files = get_all_files();
foreach(explode(PHP_EOL, $all_files) as $this_file){
@ -101,18 +87,18 @@ function delete_file(f){
echo '<tr>';
echo ' <form>';
echo ' <form>';
echo ' <td>SD Card</td>';
echo ' <td>'.$file_name.'</td>';
echo ' <td>'.file_size_from_ls($this_file).'</td>';
echo ' <td>'.file_category_from_file_name($file_name).'</td>';
echo ' <td>'.mod_date_from_ls($this_file).'</td>';
echo ' <td>';
echo ' <input type="button" value="Delete" onClick="delete_file(\''.$file_name.'\')" data-arg1="'.$file_name.'"/>';
echo ' <input type="button" value="Copy to RAM Disk" disabled/>';
echo ' </td>';
echo ' </form>';
echo '</tr>';
echo ' <input type="button" value="Delete" onClick="delete_file(\''.$file_name.'\')" data-arg1="'.$file_name.'"/>';
echo ' <input type="button" value="Copy to RAM Disk" disabled/>';
echo ' </td>';
echo ' </form>';
echo '</tr>';
}
?>
</table>
@ -135,18 +121,50 @@ function delete_file(f){
</form>
<br>
<h2>RaSCSI Service Status</h2>
<form method="get" action="rascsi.php" id="service">
<input type="submit" name="restart_rascsi_service" value="Restart RaSCSI service"/>
<input type="submit" name="stop_rascsi_service" value="Stop RaSCSI service"/>
<h2>Create New Empty HD Image</h2>
<form action="rascsi_action.php" method="post">
<input type="hidden" name="command" value="create_new_image" />
<input type="submit" value="Create New">
</form>
<br>
<h2>Raspberry Pi Operations</h2>
<form id="pi stuff">
<input type="submit" name="reboot_rasbperry_pi" value="Reboot Raspberry Pi"/>
<input type="submit" name="shutdown_raspberry_pi" value="Shut Down Raspberry Pi"/>
</form>
<h2>RaSCSI Service Status</h2>
<table style="border: none">
<tr style="border: none">
<td style="border: none; vertical-align:top;">
<form action="rascsi_action.php" method="post">
<input type="hidden" name="command" value="restart_rascsi_service" />
<input type="submit" name="restart_rascsi_service" value="Restart RaSCSI service" />
</form>
</td>
<td style="border: none; vertical-align:top;">
<form action="rascsi_action.php" method="post">
<input type="hidden" name="command" value="stop_rascsi_service" />
<input type="submit" name="stop_rascsi_service" value="Stop RaSCSI service" />
</form>
</td>
</tr>
</table>
</body>
</html>
<br>
<h2>Raspberry Pi Operations</h2>
<table style="border: none">
<tr style="border: none">
<td style="border: none; vertical-align:top;">
<form action="rascsi_action.php" method="post">
<input type="hidden" name="command" value="reboot_raspberry_pi" />
<input type="submit" name="reboot_rasbperry_pi" value="Reboot Raspberry Pi" />
</form>
</td>
<td style="border: none; vertical-align:top;">
<form action="rascsi_action.php" method="post">
<input type="hidden" name="command" value="shutdown_raspberry_pi" />
<input type="submit" name="shutdown_raspberry_pi" value="Shut Down Raspberry Pi" />
</form>
</td>
</tr>
</table>
</body>
</html>

279
src/php/rascsi_action.php Normal file
View File

@ -0,0 +1,279 @@
<!-- PHP source code for controlling the RaSCSI - 68kmla edition with a web interface. -->
<!-- Copyright (c) 2020 akuker -->
<!-- Distributed under the BSD-3 Clause License -->
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="rascsi_styles.css">
</head>
<body>
<?php
include 'lib_rascsi.php';
html_generate_header();
echo "Post values......................".PHP_EOL;
echo '<br>'.PHP_EOL;
var_dump($_POST);
echo '<br>'.PHP_EOL;
if(isset($_POST['command']))
{
echo 'Running command.... '.$_POST['command'].'<br><br>'.PHP_EOL;
switch(strtolower($_POST['command'])){
case "eject_disk":
action_eject_disk();
break;
case "remove_device":
action_remove_device();
break;
case "connect_new_device":
action_connect_new_device();
break;
case "insert_disk":
action_insert_disk();
break;
case "delete_file":
action_delete_file();
break;
case "create_new_image":
action_create_new_image();
break;
case "restart_rascsi_service":
action_restart_rascsi_service();
break;
case "stop_rascsi_service":
action_stop_rascsi_service();
break;
case "reboot_raspberry_pi":
action_reboot_raspberry_pi();
break;
case "shutdown_raspberry_pi":
action_shutdown_raspberry_pi();
break;
default:
action_unknown_command();
break;
}
}
// // parameter check
// if(isset($_GET['restart_rascsi_service'])){
// // Restart the RaSCSI service
// exec("sudo /bin/systemctl restart rascsi.service");
// } else if(isset($_GET['stop_rascsi_service'])){
// // Stop the RaSCSI Service
// exec("sudo /bin/systemctl stop rascsi.service");
// } else if(isset($_GET['reboot_rasbperry_pi'])){
// // Reboot the Raspberry Pi
// exec("sudo /sbin/reboot");
// } else if(isset($_GET['shutdown_raspberry_pi'])){
// // Shut down the Raspberry Pi
// echo "<h1>For now, shutdown is disabled....</h1>";
// echo 'exec("sudo /sbin/shutdown -s -t 0");';
// }
// // Check if we're passed an ID
// if(isset($_GET['id'])){
// $id = $_GET['id'];
// }
// else {
// html_generate_warning('Page opened without arguments');
// }
// if(isset($_GET['type'])){
// $type = type_string_to_rasctl_type($_GET['type']);
// if(strlen($type) < 1){
// html_generate_warning('Unknown drive type: '.$_GET['type']);
// }
// $cmd = 'rasctl -i '.$id.' -c attach -t '.$type;
// // Check to see if the file name is specified
// if(isset($_GET['file'])){
// if(strcasecmp($_GET['file'],"None") != 0)
// {
// $cmd = $cmd.' -f '.$FILE_PATH.'/'.$_GET['file'];
// }
// }
// $result = "Command not ran.....";
// // Go do the actual action
// if(strlen($type) > 0){
// $result = exec($cmd);
// echo '<br>';
// echo 'Ran command: <pre>'.$cmd.'</pre>';
// echo '<br>';
// }
// // Check to see if the command succeeded
// if(strlen($result) > 0){
// html_generate_warning($result);
// }
// else {
// html_generate_success_message();
// }
// echo '<br>';
// html_generate_ok_to_go_home();
// }
// else {
// html_generate_add_new_device(trim($id));
function action_eject_disk(){}
function action_remove_device(){
// Check to see if the user has confirmed
if(isset($_POST['confirmed'])){
$command = 'rasctl -i '.$_POST['id'].' -c disconnect 2>&1';
echo '<br><br> Go execute...... '.$command.PHP_EOL;
// exec($command, $retArray, $result);
// check_result($result, $command,$retArray);
html_generate_ok_to_go_home();
}
else{
check_are_you_sure('Are you sure you want to disconnect SCSI ID ' . $_POST['id'].'? If the host is running, this could cause undesirable behavior.');
}
}
// function action_connect_new_device(){}
function action_insert_disk(){}
function action_create_new_image(){}
function action_delete_file(){
// Check to see if the user has confirmed
if(isset($_POST['confirmed'])){
echo '<br>exec(rm '.$_POST['file'].')'.PHP_EOL;
html_generate_ok_to_go_home();
}
else{
check_are_you_sure('Are you sure you want to PERMANENTLY delete '.$_POST['file'].'?');
}
}
function action_restart_rascsi_service(){
// Restart the RaSCSI service
$command = "sudo /bin/systemctl restart rascsi.service 2>&1";
exec($command, $retArray, $result);
check_result($result, $command,$retArray);
html_generate_ok_to_go_home();
}
function action_stop_rascsi_service(){
// Stop the RaSCSI service
$command = "sudo /bin/systemctl stop rascsi.service 2>&1";
exec($command, $retArray, $result);
check_result($result, $command,$retArray);
html_generate_ok_to_go_home();
}
function action_reboot_raspberry_pi(){
// Check to see if the user has confirmed
if(isset($_POST['confirmed'])){
echo('<br>exec(sudo reboot)');
// The unit should reboot at this point. Doesn't matter what we do now...
}
else{
check_are_you_sure("Are you sure you want to reboot the Raspberry Pi?");
}
}
function action_shutdown_raspberry_pi(){
// Check to see if the user has confirmed
if(isset($_POST['confirmed'])){
echo('<br>exec(sudo shutdown -h now)');
// The unit should reboot at this point. Doesn't matter what we do now...
html_generate_ok_to_go_home();
}
else{
check_are_you_sure("Are you sure you want to shut down the Raspberry Pi?");
}
}
function action_unknown_command(){
echo '<br><h2>Unknown command: '.$_POST['command'].'</h2>'.PHP_EOL;
html_generate_ok_to_go_home();
}
function check_result($result,$command,$output){
if(!$result){
echo '<br><h2>Command succeeded!<h2>'.PHP_EOL;
}
else{
echo '<br><h2>Command failed!</h2>'.PHP_EOL;
}
echo '<br><code>'.$command.'</code>'.PHP_EOL;
echo '<br>Output:<code>'.PHP_EOL;
foreach($output as $line){
echo '<br> Error message: '.$line.PHP_EOL;
}
echo '</code>'.PHP_EOL;
}
function check_are_you_sure($prompt){
echo '<br><h2>'.$prompt.'</h2>';
echo ' <table style="border: none">'.PHP_EOL;
echo ' <tr style="border: none">'.PHP_EOL;
echo ' <td style="border: none; vertical-align:top;">'.PHP_EOL;
echo ' <form action="rascsi.php" method="post">'.PHP_EOL;
echo ' <input type="submit" name="cancel" value="Cancel" />'.PHP_EOL;
echo ' </form>'.PHP_EOL;
echo ' </td>'.PHP_EOL;
echo ' <td style="border: none; vertical-align:top;">'.PHP_EOL;
echo ' <form action="rascsi_action.php" method="post">'.PHP_EOL;
foreach($_POST as $key => $value){
echo '<input type="hidden" name="'.$key.'" value="'.$value.'"/>'.PHP_EOL;
}
echo ' <input type="hidden" name="confirmed" value="yes" />'.PHP_EOL;
echo ' <input type="submit" name="do_it" value="Do it!" />'.PHP_EOL;
echo ' </form>'.PHP_EOL;
echo ' </td>'.PHP_EOL;
echo ' </tr>'.PHP_EOL;
echo '</table>'.PHP_EOL;
}
function action_connect_new_device($id){
echo '<h2>Add New Device</h2>';
echo '<form action=add_device.php method="post">';
echo ' <table style="border: none">';
echo ' <tr style="border: none">';
echo ' <td style="border: none">SCSI ID:</td>';
echo ' <td style="border: none">';
echo ' <input type="hidden" name=id value="'.$id.'"/>';
echo $id;
echo ' </td>';
echo ' <td style="border: none">Device:</td>';
echo ' <td style="border: none">';
html_generate_scsi_type_select_list();
echo ' </td>';
echo ' <td style="border: none">File:</td>';
echo ' <td style="border: none">';
echo ' <select name="file">';
echo ' <option value="None">None</option>';
$all_files = get_all_files();
foreach(explode(PHP_EOL, $all_files) as $this_file){
if(strpos($this_file, 'total') === 0){
continue;
}
$file_name = file_name_from_ls($this_file);
if(strlen($file_name) === 0){
continue;
}
// Ignore files that start with a .
if(strpos($file_name, '.') === 0){
continue;
}
echo '<option value="'.$file_name.'">'.$file_name.'</option>';
}
echo ' </select>';
echo ' </td>';
echo ' <td style="border: none">';
echo ' <INPUT type="submit" value="Add"/>';
echo ' </td>';
echo ' </tr>';
echo ' </table>';
}
?>
</body>
</html>