Moved several php functions to a library that can be reused. Updated to use css for styles

This commit is contained in:
akuker
2020-08-01 20:53:11 -05:00
parent 9d94cdeec6
commit a30889f7c1
3 changed files with 191 additions and 162 deletions

144
src/php/lib_rascsi.php Normal file
View File

@@ -0,0 +1,144 @@
<!-- PHP source code for controlling the RaSCSI - 68kmla edition with a web interface. -->
<!-- Copyright (c) 2020 akuker -->
<!-- Distributed under the BSD-3 Clause License -->
<!-- Note: the origina rascsi-php project was under MIT license.-->
<?php
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 ' </form>';
echo ' </td>';
echo ' </tr>';
echo ' </table>';
echo 'Debug timestamp: ';
$t=time();
echo($t . "<br>");
echo(exec('whoami'));
}
function html_generate_scsi_id_select_list(){
echo '<select>';
foreach(range(0,7) as $id){
echo '<option value="'.$id.'">'.$id.'</option>';
}
echo '</select>';
}
function html_generate_scsi_type_select_list(){
echo '<select>';
$options = array("Hard Disk", "CD-ROM", "Zip Drive", "Ethernet Tap", "Filesystem Bridge");
foreach($options as $type){
echo '<option value="'.$type.'">'.$type.'</option>';
}
echo '</select>';
}
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>';
$scsi_ids = array();
foreach ($rasctl_lines as $current_line)
{
if(strlen($current_line) === 0){
continue;
}
if(strpos($current_line, '+----') === 0){
continue;
}
if(strpos($current_line, '| ID | UN') === 0){
continue;
}
$segments = explode("|", $current_line);
$id_config = array();
$id_config['id'] = trim($segments[1]);
$id_config['type'] = trim($segments[3]);
$id_config['file'] = trim($segments[4]);
$scsi_ids[$id_config['id']] = $id_config;
}
foreach (range(0,7) as $id){
echo ' <tr>';
echo ' <form>';
echo ' <td style="text-align:center">'.$id.'</td>';
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>';
}
else
{
echo ' <td style="text-align:center">-</td>';
echo ' <td>-</td>';
echo ' <td>';
echo ' <input type="button" value="Connect New Device" onClick="attach_device('.$id.')"/>';
echo ' </td>';
}
echo ' </form>';
echo ' </tr>';
}
echo '</table>';
}
function get_all_files()
{
$raw_ls_output = shell_exec('ls --time-style="+\"%Y-%m-%d %H:%M:%S\"" -alh --quoting-style=c /home/pi/images/');
return $raw_ls_output;
}
function mod_date_from_ls($value){
$ls_pieces = explode("\"", $value);
if(count($ls_pieces)<1){
return "";
}
return $ls_pieces[1];
}
function file_name_from_ls($value){
$ls_pieces = explode("\"", $value);
if(count($ls_pieces) < 4){
return "";
}
return $ls_pieces[3];
}
function file_size_from_ls($value){
$ls_pieces = explode("\"", $value);
$file_props = preg_split("/\s+/", $ls_pieces[0]);
return $file_props[4];
}
function file_category_from_file_name($value){
if(strpos($value,".iso") > 0){
return "CD-ROM Image";
}
if(strpos($value,".hda") > 0){
return "Hard Disk Image";
}
return "Unknown type: " . $value;
}
?>