2025-01-05 21:35:38 +00:00
< ? php
/*
* upload2usb . php
* Upload2USB App Helper Functions
*/
2025-01-12 11:04:47 +00:00
set_exception_handler ( function ( Throwable $exception ) {
error_log ( 'UPLOAD2USB ERROR: ' . ( string ) $exception );
include ( " error.php " );
});
2025-01-05 21:35:38 +00:00
//return the first removable USB drive location
function getTargetUSBDriveLocation () {
// Get the first removal USB drive using
// lsblk --output NAME,TRAN,RM,MOUNTPOINT --pairs |grep RM=\"1\" | grep -v MOUNTPOINT=\"\" |grep -oP '[^/]MOUNTPOINT="\K[^"]*' -m 1
// lsblk --output NAME,TRAN,RM,MOUNTPOINT --pairs |grep RM=\"1\" | grep -v MOUNTPOINT=\"\" | cut -d " " -f 4 | cut -d "=" -f 2
2025-01-12 11:04:47 +00:00
# error if 1<>usb sticks are installed
$rmv_usb_path_count = shell_exec ( 'lsblk --output NAME,TRAN,RM,MOUNTPOINT --pairs |grep RM=\"1\" | grep -v MOUNTPOINT=\"\" | cut -d " " -f 4 | wc -l' );
if ( $rmv_usb_path_count == 0 ) {
2025-01-17 08:34:15 +00:00
throw new RuntimeException ( '0 USB sticks found. <br/><br/>' );
2025-01-12 11:04:47 +00:00
} elseif ( $rmv_usb_path_count > 1 ) {
2025-01-17 08:34:15 +00:00
throw new RuntimeException ( 'More than 1 USB sticks installed. <br/><br/>' );
2025-01-12 11:04:47 +00:00
}
$rmv_usb_path = trim ( str_replace ( '"' , '' , shell_exec ( 'lsblk --output NAME,TRAN,RM,MOUNTPOINT --pairs |grep RM=\"1\" | grep -v MOUNTPOINT=\"\" | cut -d " " -f 4 | cut -d "=" -f 2' )));
2025-01-05 21:35:38 +00:00
2025-01-12 11:04:47 +00:00
if ( empty ( $rmv_usb_path )) {
2025-01-17 08:34:15 +00:00
throw new RuntimeException ( 'Not able to find USB stick. <br/><br/>' );
2025-01-05 21:35:38 +00:00
} else {
2025-01-12 11:04:47 +00:00
return $rmv_usb_path . " / " ;
2025-01-05 21:35:38 +00:00
}
}
2025-01-06 03:48:19 +00:00
//returns folder path where file will be stored, if create_folder_p = 1, it will create the folder if it doesn't exist
2025-01-05 21:35:38 +00:00
function getTargetFolderPath ( $create_folder_p ) {
$parent_dir = getTargetUSBDriveLocation ();
2025-01-12 11:04:47 +00:00
2025-01-05 21:35:38 +00:00
$today_folder_name = " UPLOADS. " . date ( " Y-m-d " );
$target_folder_path = $parent_dir . $today_folder_name ;
if ( ! file_exists ( $target_folder_path ) && $create_folder_p ) {
2025-01-17 08:34:15 +00:00
mkdir ( $target_folder_path , 0777 ) or throw new RuntimeException ( " Not able to create upload directory. <br/>Make sure 'usb_lib_umask0000_for_kolibri' is set to 'True'. <br/><br/> " );
2025-01-05 21:35:38 +00:00
}
return $target_folder_path ;
}
//return number of files within a specified folder
function getFileCount ( $folder_path ) {
return count ( glob ( $folder_path . " /* " ));
}
2025-01-12 11:04:47 +00:00
//check if file mimetype is acceptable for upload
function isFileMimeTypeAcceptable ( $file ) {
$mimetype = strtolower ( mime_content_type ( $file ));
2025-01-23 05:51:43 +00:00
$invalid_mimetypes_str = array ( " compress " , " image/svg+xml " , " octet " , " text/xml " , " xhtml+xml " , " zip " );
2025-01-12 11:04:47 +00:00
foreach ( $invalid_mimetypes_str as $invalid_mt_str ) {
if ( str_contains ( $mimetype , $invalid_mt_str )) {
2025-01-23 05:51:43 +00:00
error_log ( 'UPLOAD2USB ERROR - MIMETYPE: ' . $mimetype );
2025-01-12 11:04:47 +00:00
return false ;
}
}
return true ;
2025-01-05 21:35:38 +00:00
}
2025-01-12 11:04:47 +00:00
//check file content to see if it's unique or not
function isFileContentUnique ( $target_folder_path , $file ) {
$file_to_upload_md5 = md5_file ( $file );
$usb_dir = array_diff ( scandir ( $target_folder_path ), array ( '..' , '.' ));
foreach ( $usb_dir as $dir_file ) {
$dir_file = $target_folder_path . " / " . $dir_file ;
2025-01-05 21:35:38 +00:00
2025-01-12 11:04:47 +00:00
if ( ! is_dir ( $dir_file )) {
$dir_file_md5 = md5_file ( $dir_file );
if ( $file_to_upload_md5 == $dir_file_md5 ) {
return false ;
}
}
}
return true ;
2025-01-05 21:35:38 +00:00
}
2025-01-12 11:04:47 +00:00
//return unique filename
function getUniqueFileName ( $target_folder_path , $filename ) {
$new_filename = $filename ;
$counter = 1 ;
while ( file_exists ( $target_folder_path . " / " . $new_filename )) {
$counter ++ ;
$new_filename = pathinfo ( $filename , 8 ) . '-' . $counter . " . " . pathinfo ( $filename , 4 );
}
return $new_filename ;
}
2025-01-05 21:35:38 +00:00
// Check file size - we are not going to check file size for now.
2025-01-06 03:48:19 +00:00
// elseif ($_FILES["uploaded_file"]["size"] > 5000000) {
2025-01-05 21:35:38 +00:00
// $upload_msg = "Your file is too large.";
// $upload_ok = 0;
// }
2025-01-12 11:04:47 +00:00
2025-01-05 21:35:38 +00:00
?>