From dfa28091e4c12854c4ec8d293139f839554e5f7f Mon Sep 17 00:00:00 2001 From: akuker Date: Fri, 7 Aug 2020 16:59:42 -0500 Subject: [PATCH] Added file upload functionality. Disabled some debug messages. --- src/php/.editorconfig | 8 ++ src/php/lib_rascsi.php | 121 ++++++++++++++------ src/php/rascsi.php | 11 +- src/php/rascsi_action.php | 227 +++++++++++++++----------------------- src/php/rascsi_upload.php | 86 +++++++++++++++ 5 files changed, 279 insertions(+), 174 deletions(-) create mode 100644 src/php/.editorconfig create mode 100644 src/php/rascsi_upload.php diff --git a/src/php/.editorconfig b/src/php/.editorconfig new file mode 100644 index 00000000..d03ab4e6 --- /dev/null +++ b/src/php/.editorconfig @@ -0,0 +1,8 @@ +root = true + +[*.{html,php,htm}] +indent_style = space +indent_size = 3 +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true diff --git a/src/php/lib_rascsi.php b/src/php/lib_rascsi.php index dd4158f6..59b01f7c 100644 --- a/src/php/lib_rascsi.php +++ b/src/php/lib_rascsi.php @@ -6,7 +6,11 @@ '. PHP_EOL; @@ -14,8 +18,10 @@ function html_generate_header(){ echo '

RaSCSI - 68kmla Edition

'. PHP_EOL; echo ' '. PHP_EOL; echo '
'. PHP_EOL; - echo ' '. PHP_EOL; - echo '

'.time().'

'. PHP_EOL; + echo ' '. PHP_EOL; + if($GLOBALS['DEBUG_ENABLE']){ + echo '

Debug Timestamp: '.time().'

'. PHP_EOL; + } echo '
'. PHP_EOL; echo ' '. PHP_EOL; echo ' '. PHP_EOL; @@ -23,6 +29,26 @@ function html_generate_header(){ //echo(exec('whoami')); } +function html_generate_image_file_select_list(){ + $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 ''.PHP_EOL; + } +} + + function html_generate_scsi_id_select_list(){ echo ''. PHP_EOL; - echo ' '. PHP_EOL; - echo ' '. PHP_EOL; - echo ' '. PHP_EOL; - echo ' '. PHP_EOL; - echo '
'. PHP_EOL; - echo ' '. PHP_EOL; - echo ' '. PHP_EOL; - echo ' '. PHP_EOL; - echo '
'. PHP_EOL; - echo ' '. PHP_EOL; + echo ' '. PHP_EOL; + echo ' '.$id.''. PHP_EOL; + if(isset($scsi_ids[$id])) + { + echo ' '.$scsi_ids[$id]['type'].''. PHP_EOL; + if(strtolower($scsi_ids[$id]['file']) == "no media"){ + echo ' '.PHP_EOL; + echo '
'. PHP_EOL; + echo ' '.PHP_EOL; + echo ' '. PHP_EOL; + echo ' '. PHP_EOL; + echo ' '. PHP_EOL; + echo ' '.PHP_EOL; + echo ' '. PHP_EOL; + echo '
'. PHP_EOL; + echo ' '.PHP_EOL; + } + else{ + // rascsi inserts "WRITEPROTECT" for the read-only drives. We want to display that differently. + echo '
'. PHP_EOL; + echo ' '.str_replace('(WRITEPROTECT)', '', $scsi_ids[$id]['file']). PHP_EOL; + echo ' '.PHP_EOL; + if(strtolower($scsi_ids[$id]['type']) == 'sccd'){ + echo ' '. PHP_EOL; + echo ' '. PHP_EOL; + echo ' '. PHP_EOL; + echo ' '. PHP_EOL; + } + echo ' '.PHP_EOL; + echo '
'. PHP_EOL; + } + echo ' '. PHP_EOL; + echo '
'. PHP_EOL; + echo ' '. PHP_EOL; + echo ' '. PHP_EOL; + echo ' '. PHP_EOL; + echo '
'. PHP_EOL; + echo ' '. PHP_EOL; } else { - echo ' -'. PHP_EOL; - echo ' -'. PHP_EOL; - echo ' '. PHP_EOL; + echo ' -'. PHP_EOL; + echo ' -'. PHP_EOL; + echo ' '. PHP_EOL; + echo ' '. PHP_EOL; echo '
'. PHP_EOL; echo ' '. PHP_EOL; echo ' '. PHP_EOL; - echo ' '. PHP_EOL; + echo ' '. PHP_EOL; echo '
'. PHP_EOL; echo ' '. PHP_EOL; @@ -200,4 +257,4 @@ function type_string_to_rasctl_type($typestr){ -?> \ No newline at end of file +?> diff --git a/src/php/rascsi.php b/src/php/rascsi.php index b2ec3471..b539f303 100644 --- a/src/php/rascsi.php +++ b/src/php/rascsi.php @@ -8,7 +8,8 @@ - + RaSCSI Main Control Page + @@ -71,14 +72,14 @@

Upload New Image File

-
+
-

+

- +
@@ -131,4 +132,4 @@ - \ No newline at end of file + diff --git a/src/php/rascsi_action.php b/src/php/rascsi_action.php index 97c6df38..581b196e 100644 --- a/src/php/rascsi_action.php +++ b/src/php/rascsi_action.php @@ -5,7 +5,8 @@ - + RaSCSI Action Page + @@ -13,17 +14,19 @@ include 'lib_rascsi.php'; html_generate_header(); - echo '
'; - echo ''.PHP_EOL; - echo ' '.PHP_EOL; - echo '

Debug stuff

'; - - echo '

Post values......................'.PHP_EOL; - echo '
'.PHP_EOL; - var_dump($_POST); - echo '

Running command.... '.$_POST['command'].PHP_EOL; - echo '

'.PHP_EOL; - echo '
'; + echo '
'; + if($GLOBALS['DEBUG_ENABLE']){ + echo ''.PHP_EOL; + echo ' '.PHP_EOL; + echo '

Debug stuff

'; + + echo '

Post values......................'.PHP_EOL; + echo '
'.PHP_EOL; + var_dump($_POST); + echo '

Running command.... '.$_POST['command'].PHP_EOL; + echo '

'.PHP_EOL; + echo '
'; + } if(isset($_POST['command'])) { @@ -61,81 +64,27 @@ default: action_unknown_command(); break; - } + } } + else{ + html_generate_warning("HTTP command was missing POST information. Are you trying to access this page directly? That won't work"); + echo "
".PHP_EOL; + html_generate_ok_to_go_home(); + } -// // 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 "

For now, shutdown is disabled....

"; -// echo 'exec("sudo /sbin/shutdown -s -t 0");'.PHP_EOL; -// } +function action_eject_disk(){ + $command = 'rasctl -i '.$_POST['id'].' -c eject 2>&1'.PHP_EOL; + exec($command, $retArray, $result); + check_result($result, $command,$retArray); + html_generate_ok_to_go_home(); +} - - // // 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 '
'.PHP_EOL; - // echo 'Ran command:
'.$cmd.'
'.PHP_EOL; - // echo '
'.PHP_EOL; - // } - // // Check to see if the command succeeded - // if(strlen($result) > 0){ - // html_generate_warning($result); - // } - // else { - // html_generate_success_message(); - // } - // echo '
'.PHP_EOL; - // 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 + // Check to see if the user has confirmed if(isset($_POST['confirmed'])){ $command = 'rasctl -i '.$_POST['id'].' -c disconnect 2>&1'.PHP_EOL; - echo '

Go execute...... '.$command.PHP_EOL; - // exec($command, $retArray, $result); - // check_result($result, $command,$retArray); + exec($command, $retArray, $result); + check_result($result, $command,$retArray); html_generate_ok_to_go_home(); } else{ @@ -143,15 +92,19 @@ function action_remove_device(){ } } // function action_connect_new_device(){} -function action_insert_disk(){} +function action_insert_disk(){ + $command = 'rasctl -i '.$_POST['id'].' -c insert -f '.$GLOBALS['FILE_PATH'].'/'.$_POST['file_name'].' 2>&1'.PHP_EOL; + exec($command, $retArray, $result); + check_result($result, $command,$retArray); + html_generate_ok_to_go_home(); +} function action_create_new_image(){ // If we already know the size & filename, we can go create the image... if(isset($_POST['size']) && isset($_POST['file_name'])){ $command = 'dd if=/dev/zero of='.$GLOBALS['FILE_PATH'].'/'.$_POST['file_name'].' bs=1M count='.$_POST['size']; exec($command, $retArray, $result); - echo '

'.$command.'

'; check_result($result, $command, $retArray); - html_generate_ok_to_go_home(); + html_generate_ok_to_go_home(); } else{ echo '

Create a new empty file

'.PHP_EOL; @@ -183,7 +136,7 @@ function action_create_new_image(){ } function action_delete_file(){ - // Check to see if the user has confirmed + // Check to see if the user has confirmed if(isset($_POST['confirmed'])){ $command = 'rm '.$GLOBALS['FILE_PATH'].'/'.$_POST['file_name']; exec($command, $retArray, $result); @@ -212,10 +165,11 @@ function action_stop_rascsi_service(){ } function action_reboot_raspberry_pi(){ - // Check to see if the user has confirmed + // Check to see if the user has confirmed if(isset($_POST['confirmed'])){ echo('
exec(sleep 2 && sudo reboot)'); // 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 reboot the Raspberry Pi?"); @@ -223,7 +177,7 @@ function action_reboot_raspberry_pi(){ } function action_shutdown_raspberry_pi(){ - // Check to see if the user has confirmed + // Check to see if the user has confirmed if(isset($_POST['confirmed'])){ echo('
exec(sleep 2 && sudo shutdown -h now)'); // The unit should reboot at this point. Doesn't matter what we do now... @@ -235,16 +189,16 @@ function action_shutdown_raspberry_pi(){ } function action_unknown_command(){ - echo '

Unknown command: '.$_POST['command'].'

'.PHP_EOL; + html_generate_warning('

Unknown command: '.$_POST['command'].'

'); html_generate_ok_to_go_home(); } function check_result($result,$command,$output){ if(!$result){ - echo '

Command succeeded!

'.PHP_EOL; + html_generate_success_message('Command succeeded!'); } else{ - echo '

Command failed!

'.PHP_EOL; + html_generate_warning('Command failed!'); } echo '
'.$command.'
'.PHP_EOL; if(count($output) > 0){ @@ -260,11 +214,6 @@ function check_are_you_sure($prompt){ echo '

'.$prompt.'

'.PHP_EOL; echo ' '.PHP_EOL; echo ' '.PHP_EOL; - echo ' '.PHP_EOL; echo ' '.PHP_EOL; + echo ' '.PHP_EOL; echo ' '.PHP_EOL; echo '
'.PHP_EOL; - echo ' '.PHP_EOL; - echo ' '.PHP_EOL; - echo ' '.PHP_EOL; - echo ' '.PHP_EOL; echo '
'.PHP_EOL; foreach($_POST as $key => $value){ @@ -274,53 +223,57 @@ function check_are_you_sure($prompt){ echo ' '.PHP_EOL; echo '
'.PHP_EOL; echo '
'.PHP_EOL; + echo '
'.PHP_EOL; + echo ' '.PHP_EOL; + echo '
'.PHP_EOL; + echo '
'.PHP_EOL; } function action_connect_new_device(){ + // If we already know the type & filename, we can go connect the device... + if(isset($_POST['type']) && isset($_POST['file_name'])){ + $command = 'rasctl -i '.$_POST['id'].' -c attach -t '.type_string_to_rasctl_type($_POST['type']); + if($_POST['file_name'] != "None"){ + $command = $command.' -f '.$GLOBALS['FILE_PATH'].'/'.$_POST['file_name']; + } + exec($command, $retArray, $result); + check_result($result, $command, $retArray); + html_generate_ok_to_go_home(); + } + else{ + + $id = $_POST['id']; - echo '

Add New Device

'.PHP_EOL; - echo '
'.PHP_EOL; - echo ' '.PHP_EOL; - echo ' '.PHP_EOL; - echo ' '.PHP_EOL; - echo ' '.PHP_EOL; - echo ' '.PHP_EOL; - echo ' '.PHP_EOL; - echo ' '.PHP_EOL; - echo ' '.PHP_EOL; - echo ' '.PHP_EOL; - echo ' '.PHP_EOL; - echo ' '.PHP_EOL; - echo '
SCSI ID:'.PHP_EOL; - echo ' '.PHP_EOL; - echo $id; - echo ' Device:'.PHP_EOL; - html_generate_scsi_type_select_list(); - echo ' File:'.PHP_EOL; - echo ' '.PHP_EOL; - echo ' '.PHP_EOL; - echo ' '.PHP_EOL; - echo '
'.PHP_EOL; + echo '

Add New Device

'.PHP_EOL; + echo ''.PHP_EOL; + echo ' '.PHP_EOL; + echo ' '.PHP_EOL; + echo ' '.PHP_EOL; + echo ' '.PHP_EOL; + echo ' '.PHP_EOL; + echo ' '.PHP_EOL; + echo ' '.PHP_EOL; + echo ' '.PHP_EOL; + echo ' '.PHP_EOL; + echo ' '.PHP_EOL; + echo ' '.PHP_EOL; + echo '
SCSI ID:'.PHP_EOL; + echo ' '.PHP_EOL; + echo $id; + echo ' Device:'.PHP_EOL; + html_generate_scsi_type_select_list(); + echo ' File:'.PHP_EOL; + echo ' '.PHP_EOL; + echo ' '.PHP_EOL; + echo ' '.PHP_EOL; + echo '
'.PHP_EOL; + } } function get_new_filename(){ @@ -337,4 +290,4 @@ function get_new_filename(){ - \ No newline at end of file + diff --git a/src/php/rascsi_upload.php b/src/php/rascsi_upload.php new file mode 100644 index 00000000..e34fba89 --- /dev/null +++ b/src/php/rascsi_upload.php @@ -0,0 +1,86 @@ + + + + + + + + RaSCSI Upload Page + + + + + '; + + if($GLOBALS['DEBUG_ENABLE']){ + echo ''.PHP_EOL; + echo ' '.PHP_EOL; + echo '

Debug stuff

'; + + echo '

Post values......................'.PHP_EOL; + echo '
'.PHP_EOL; + var_dump($_POST); + echo '

'.PHP_EOL; + var_dump($_FILES); + echo '

'.PHP_EOL; + echo '
'; + } + + $target_dir = $GLOBALS['FILE_PATH'].'/'; + $target_file = $target_dir.basename($_FILES['file_name']['name']); + $upload_ok=1; + $file_type = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); + + if(isset($_POST['submit'])) + { + // Check if file already exists + if ($upload_ok && (file_exists($target_file))) { + html_generate_warning('Error: File '.$target_file.' already exists.'); + $upload_ok = 0; + } + + // Check file size. Limit is specified in lib_rascsi.php + if ($upload_ok && ($_FILES["file_name"]["size"] > $GLOBALS['MAX_UPLOAD_FILE_SIZE'])) { + html_generate_warning("Error: your file is larger than the maximum size of " . $GLOBALS['MAX_UPLOAD_FILE_SIZE'] . "bytes"); + $upload_ok = 0; + } + + // Allow certain file formats, also specified in lib_rascsi.php + if($upload_ok && (!in_array(strtolower($file_type),$GLOBALS['ALLOWED_FILE_TYPES']))){ + $error_string = 'File type "'. $file_type. '" is not currently allowed.'. + 'Only the following file types are allowed:
'. + '
    '.PHP_EOL; + foreach($GLOBALS['ALLOWED_FILE_TYPES'] as $ft){ + $error_string = $error_string. '
  • '.$ft.'
  • '.PHP_EOL; + } + $error_string = $error_string.'
'; + $error_string = $error_string.'
'; + html_generate_warning($error_string); + $upload_ok = 0; + } + + //Check if $upload_ok is set to 0 by an error + if ($upload_ok != 0) { + if (move_uploaded_file($_FILES["file_name"]["tmp_name"], $target_file)) { + html_generate_success_message(basename( $_FILES["file_name"]["name"]). " has been uploaded."); + } else { + html_generate_warning("There was an unknown error uploading your file."); + } + } + } + else + { + html_generate_warning('The Submit POST information was not populated. Something went wrong'); + } + echo '
'; + + html_generate_ok_to_go_home(); +?> + + + +