mirror of
https://github.com/iiab/iiab.git
synced 2025-03-09 15:40:17 +00:00
Merge pull request #3875 from avni/master
Initial commit of upload2usb App for homework assignments for students and teachers worldwide! [and, upload photos/projects to community's USB stick]
This commit is contained in:
commit
dd4a9088b4
10 changed files with 299 additions and 0 deletions
|
@ -5,10 +5,25 @@ location / {
|
|||
location /usb {
|
||||
alias /library/www/html/local_content/;
|
||||
fancyindex on; # autoindex on;
|
||||
add_before_body /usb/upload/button.html;
|
||||
}
|
||||
|
||||
location ~ ^/usb/upload/(.*)\.php$ {
|
||||
alias /library/www/html/local_content/upload/$1.php;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $remote_addr;
|
||||
proxy_set_header Host $host;
|
||||
fastcgi_pass php;
|
||||
fastcgi_index index.php;
|
||||
fastcgi_split_path_info ^(.+\.php)(/.+)$;
|
||||
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
|
||||
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
|
||||
include fastcgi_params;
|
||||
}
|
||||
|
||||
location /local_content/ {
|
||||
fancyindex on; # autoindex on;
|
||||
add_before_body /usb/upload/button.html;
|
||||
}
|
||||
|
||||
location /info {
|
||||
|
|
19
roles/usb_lib/files/upload/button.html
Normal file
19
roles/usb_lib/files/upload/button.html
Normal file
|
@ -0,0 +1,19 @@
|
|||
<style>
|
||||
.button {
|
||||
background-color: #343a40;
|
||||
border: none;
|
||||
border-radius:.25rem;
|
||||
color: white;
|
||||
padding: .5rem .75rem;
|
||||
text-align: center;
|
||||
text-decoration: none;
|
||||
display: inline-block;
|
||||
font-size: 1rem;
|
||||
font-weight:bold;
|
||||
float:right;
|
||||
}
|
||||
.button:hover {
|
||||
color: #ddd;
|
||||
}
|
||||
</style>
|
||||
<a class="button" href="/usb/upload/">Upload to USB</a>
|
16
roles/usb_lib/files/upload/error.php
Normal file
16
roles/usb_lib/files/upload/error.php
Normal file
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
/*
|
||||
* error.php
|
||||
* Upload2USB App error
|
||||
*/
|
||||
|
||||
?>
|
||||
|
||||
ERROR: Please make sure <span style="color:red; font-weight:bold;"> one and ONLY one </span>(no more, no less) removable USB stick is plugged into your Internet-in-a-Box. Please see IIAB FAQs for additional support: <a href="https://wiki.iiab.io/go/FAQ#Can_teachers_display_their_own_content%3F">FAQ #4 - Can teachers display their own content?</a>, <a href="https://wiki.iiab.io/go/FAQ#What_are_the_best_places_for_community_support%3F">FAQ #49 - What are the best places for community support?.</a>
|
||||
|
||||
<br/><br/>
|
||||
|
||||
<pre><?php if (isset($exception)) {echo (string)$exception;} ?></pre>
|
||||
|
||||
|
||||
|
14
roles/usb_lib/files/upload/footer.php
Normal file
14
roles/usb_lib/files/upload/footer.php
Normal file
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
/*
|
||||
* footer.php
|
||||
* Upload2USB App Footer for all User Facing Pages
|
||||
*/
|
||||
|
||||
?>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
32
roles/usb_lib/files/upload/header.php
Normal file
32
roles/usb_lib/files/upload/header.php
Normal file
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
/*
|
||||
* header.php
|
||||
* Upload2USB App Header for all User Facing Pages
|
||||
*/
|
||||
|
||||
|
||||
include("upload2usb.php");
|
||||
|
||||
?>
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title><?php echo $title ?></title>
|
||||
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
|
||||
<link rel="stylesheet" href="/common/css/bootstrap4.min.css"/>
|
||||
<link rel="stylesheet" href="/common/css/fa.all.min.css"/>
|
||||
<link rel="stylesheet" href="/common/css/font-faces.css"/>
|
||||
<script src="/common/js/jquery.min.js"></script>
|
||||
<script src="/common/js/bootstrap4.min.js"></script>
|
||||
</head>
|
||||
<body class="text-center" style="background-color:#f5f5f5;">
|
||||
<div id="container" class="container">
|
||||
<div class="row">
|
||||
<div class="col-sm-6 offset-sm-3 text-center" style="padding:15px;">
|
||||
|
||||
<img class="mb-4" src="uk-swing.png" alt="" width="75">
|
||||
<h1 class="h3 mb-3 font-weight-normal"><?php echo $title ?></h1>
|
24
roles/usb_lib/files/upload/index.php
Normal file
24
roles/usb_lib/files/upload/index.php
Normal file
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
/*
|
||||
* index.php
|
||||
* Upload2USB App Index Page
|
||||
*/
|
||||
|
||||
$title = "Upload to USB";
|
||||
include("header.php");
|
||||
|
||||
//Check if folder for today exists, and get file count if it does
|
||||
$file_count = getFileCount(getTargetFolderPath(0));
|
||||
|
||||
?>
|
||||
|
||||
<form action="upload-file.php" id="upload2usb_form" method="post" enctype="multipart/form-data">
|
||||
<label for="upload2usb" style="font-weight:bold;padding-bottom:10px;">Upload your file here!</label><br/>
|
||||
<input type="file" name="uploaded_file" id="uploaded_file"><br/><br/>
|
||||
<button class="btn btn-dark" name="submit" type="submit" style="width:150px;">Upload</button>
|
||||
</form>
|
||||
<br/>
|
||||
<?php echo $file_count ?> files have been uploaded today!
|
||||
|
||||
|
||||
<?php include ("footer.php"); ?>
|
BIN
roles/usb_lib/files/upload/uk-swing.png
Normal file
BIN
roles/usb_lib/files/upload/uk-swing.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.8 KiB |
57
roles/usb_lib/files/upload/upload-file.php
Normal file
57
roles/usb_lib/files/upload/upload-file.php
Normal file
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
/*
|
||||
* upload-file.php
|
||||
* Upload2USB App - Process Submission
|
||||
*/
|
||||
|
||||
$title = "Upload to USB Results";
|
||||
include("header.php");
|
||||
|
||||
//get folder path where file will be stored
|
||||
$target_folder_path = getTargetFolderPath(1);
|
||||
$uploaded_filename = basename($_FILES["uploaded_file"]["name"]);
|
||||
$target_file = $target_folder_path . "/" . $uploaded_filename;
|
||||
$upload_ok = 1;
|
||||
$upload_msg = "";
|
||||
|
||||
if(!isset($_POST["submit"]) || !is_uploaded_file($_FILES['uploaded_file']['tmp_name'])) {
|
||||
$upload_msg = "No file uploaded!";
|
||||
$upload_ok = 0;
|
||||
} elseif (!isFileMimeTypeAcceptable($_FILES["uploaded_file"]["tmp_name"])) {
|
||||
$upload_msg = "You cannot upload zips, executables, xml, or binary files!";
|
||||
$upload_ok = 0;
|
||||
} elseif (file_exists($target_file)) {
|
||||
|
||||
if (!isFileContentUnique($target_folder_path, $_FILES["uploaded_file"]["tmp_name"])) {
|
||||
$upload_msg = "This file already exists!";
|
||||
$upload_ok = 0;
|
||||
} else {
|
||||
// rename file so name is unique
|
||||
$new_filename = getUniqueFileName($target_folder_path, $uploaded_filename);
|
||||
$target_file = $target_folder_path . "/" . $new_filename;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if $upload_ok is set to 0 by an error
|
||||
if ($upload_ok == 0) {
|
||||
$upload_msg = "❌ Your file was not uploaded. " . $upload_msg;
|
||||
|
||||
// if everything is ok, try to upload file
|
||||
} else {
|
||||
if (move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], $target_file)) {
|
||||
$upload_msg = "😊 ✅ Your file <span style=\"font-weight:bold; font-style:italic;\">". htmlspecialchars( $uploaded_filename ). "</span> was successfully uploaded!";
|
||||
} else {
|
||||
$upload_msg = "❌ There was an error uploading your file. " . $_FILES["upload_file"]["error"] . $upload_msg;
|
||||
}
|
||||
}
|
||||
|
||||
$file_count = getFileCount($target_folder_path);
|
||||
|
||||
?>
|
||||
|
||||
<?php echo $upload_msg ?> <br/>
|
||||
<?php echo $file_count ?> files have been uploaded today!
|
||||
|
||||
<?php include ("footer.php"); ?>
|
||||
|
||||
|
104
roles/usb_lib/files/upload/upload2usb.php
Normal file
104
roles/usb_lib/files/upload/upload2usb.php
Normal file
|
@ -0,0 +1,104 @@
|
|||
<?php
|
||||
/*
|
||||
* upload2usb.php
|
||||
* Upload2USB App Helper Functions
|
||||
*/
|
||||
|
||||
set_exception_handler(function (Throwable $exception) {
|
||||
error_log('UPLOAD2USB ERROR: ' . (string)$exception);
|
||||
|
||||
include ("error.php");
|
||||
});
|
||||
|
||||
|
||||
//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
|
||||
|
||||
# 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) {
|
||||
throw new RuntimeException('0 USB sticks found. <br/><br/>');
|
||||
} elseif ($rmv_usb_path_count > 1) {
|
||||
throw new RuntimeException('More than 1 USB sticks installed. <br/><br/>');
|
||||
}
|
||||
|
||||
$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')));
|
||||
|
||||
if (empty($rmv_usb_path)) {
|
||||
throw new RuntimeException('Not able to find USB stick. <br/><br/>');
|
||||
} else {
|
||||
return $rmv_usb_path . "/";
|
||||
}
|
||||
}
|
||||
|
||||
//returns folder path where file will be stored, if create_folder_p = 1, it will create the folder if it doesn't exist
|
||||
function getTargetFolderPath ($create_folder_p) {
|
||||
$parent_dir = getTargetUSBDriveLocation();
|
||||
|
||||
$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) {
|
||||
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/>");
|
||||
}
|
||||
return $target_folder_path;
|
||||
}
|
||||
|
||||
//return number of files within a specified folder
|
||||
function getFileCount ($folder_path) {
|
||||
return count(glob($folder_path . "/*"));
|
||||
}
|
||||
|
||||
//check if file mimetype is acceptable for upload
|
||||
function isFileMimeTypeAcceptable ($file) {
|
||||
$mimetype = strtolower(mime_content_type($file));
|
||||
$invalid_mimetypes_str = array ("compress", "image/svg+xml", "octet", "text/xml", "xhtml+xml", "zip");
|
||||
foreach ($invalid_mimetypes_str as $invalid_mt_str) {
|
||||
if (str_contains($mimetype, $invalid_mt_str)) {
|
||||
error_log('UPLOAD2USB ERROR - MIMETYPE: ' . $mimetype);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
//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;
|
||||
|
||||
if (!is_dir($dir_file)) {
|
||||
$dir_file_md5 = md5_file($dir_file);
|
||||
if ($file_to_upload_md5 == $dir_file_md5) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
//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;
|
||||
}
|
||||
|
||||
// Check file size - we are not going to check file size for now.
|
||||
// elseif ($_FILES["uploaded_file"]["size"] > 5000000) {
|
||||
// $upload_msg = "Your file is too large.";
|
||||
// $upload_ok = 0;
|
||||
// }
|
||||
|
||||
|
||||
|
||||
?>
|
|
@ -7,6 +7,9 @@
|
|||
# https://github.com/rbrito/usbmount/blob/master/README.md (2018-08-10)
|
||||
# https://github.com/rbrito/usbmount/blob/master/usbmount.conf (2010-04-25)
|
||||
|
||||
# usb_lib_umask0000_for_kolibri (e.g., in /etc/iiab/local_vars.yml) must be set to true in order to be able to write to mounted USB sticks
|
||||
# If you are still not able to write to a mounted USB stick, you can unmount the drive (sudo umount <mountpoint>) and then remount it setting umask to 0000 manually (sudo mount -o umask=0000 <device name> <mountpoint>).
|
||||
|
||||
|
||||
- name: Record (initial) disk space used
|
||||
shell: df -B1 --output=used / | tail -1
|
||||
|
@ -76,6 +79,21 @@
|
|||
- { src: 'iiab-usb_lib-show-all-off', dest: '/usr/bin/', mode: '0755' }
|
||||
- { src: 'iiab-clean-usb.sh', dest: '/usr/sbin/', mode: '0755' }
|
||||
|
||||
- name: '2025-01-05: Add upload2usb app (#3875) directory to local_content'
|
||||
file:
|
||||
state: directory
|
||||
path: "{{ doc_root }}/local_content/upload"
|
||||
owner: "{{ apache_user }}"
|
||||
group: "{{ apache_user }}"
|
||||
mode: 0755
|
||||
|
||||
- name: '2025-01-05: Copy upload2usb app (#3875) files from files/upload/ to local_content'
|
||||
copy:
|
||||
src: "{{ item }}"
|
||||
dest: "{{ doc_root }}/local_content/upload" # /library/www/html
|
||||
with_fileglob:
|
||||
- upload/*
|
||||
|
||||
# 2021-03-21: If usbmount is repackaged by apt as a result of Linux kernel 5.4+
|
||||
# supporting exFAT, the stanza below (might) in future no longer be needed...
|
||||
# SEE ALSO: https://github.com/iiab/iiab/blob/586bfc5cb1abf6b4333a21d3fa89695f115432dc/roles/2-common/tasks/packages.yml#L11-L12
|
||||
|
|
Loading…
Add table
Reference in a new issue