This commit is contained in:
Andrew S. Rightenburg 2023-04-27 22:50:26 -04:00
parent 567d95ef7f
commit 799e95eafe
Signed by: rail5
GPG Key ID: A0CB570AB6629159
3 changed files with 75 additions and 13 deletions

View File

@ -5,6 +5,12 @@ TODO
CHANGELOG
2023-04-27:
* upload.php now uses pure PHP rather than JavaScript to obtain the file download link
* The "copy-to-clipboard" button for download links now displays a message telling the user the link was copied
* The link display on upload.php now force-sets the cursor to "text" mode, to inform the user that the text can be selected
* The "copy-to-clipboard" button no longer displays if the server is not running either (1) via HTTPS or (2) on localhost. The reason for this is that the JavaScript navigator.clipboard API (which is necessary for the button to work) is only available if we're using HTTPS or we're on localhost.
2023-04-26:
* Updated the upload page to display the file's download link immediately after uploading
* SimpleFS now automatically creates a ZIP file when a user uploads multiple files (requires php-zip module)

View File

@ -26,4 +26,45 @@ function contactDB($query, $column) {
return $dbresult;
}
function isLocalhost() {
// Returns true if the site is being served through 127.0.0.1
// This is used by upload.php in determining whether we can access the browser navigator.clipboard API from JavaScript to copy download links to the user's clipboard
return ( ($_SERVER['HTTP_HOST'] == "127.0.0.1") || ($_SERVER['HTTP_HOST'] == "localhost") );
}
function isSSL() {
// Returns true if on HTTPS, false if on HTTP
return ( (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443);
}
function get_download_link($file_id) {
// Returns the full (absolute) link to download.php?id= ($file_id)
// Check if we're on HTTPS or HTTP
if (isSSL()) {
$url = "https://";
} else {
$url = "http://";
}
// Get the URL of the current page
// e.g: https://127.0.0.1/some/directory/SimpleFS/upload.php
$url .= $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
// Find the last slash (the directory we're in, rather than the file)
$position_of_last_slash = strrpos($url, "/");
// Trim the URL to just that directory
// e.g: https://127.0.0.1/some/directory/SimpleFS/
$url = substr($url, 0, $position_of_last_slash + 1);
// Append download.php?id= (the id supplied)
// e.g: https://127.0.0.1/some/directory/SimpleFS/download.php?id=12345
$url .= "download.php?id=$file_id";
return $url;
}
?>

View File

@ -99,24 +99,39 @@ if ($_POST['fsubmitted'] == "true") {
/* Provide the download link */
// We'll be using javascript to get the *absolute* link
$download_link = get_download_link($newFileId);
// Javascript is used for the "copy to clipboard" button
echo "<script type=\"text/javascript\">
var downloadlink = window.location.href.slice(0, window.location.href.lastIndexOf('/')+1) + \"download.php?id=$newFileId\";
function resetCopyButton() {
document.getElementById(\"copybutton\").innerHTML = \"<strong>Copy Link</strong>\";
}
function copyToClipboard() {
navigator.clipboard.writeText(downloadlink);
let downloadlink = \"$download_link\";
if (navigator.clipboard.writeText(downloadlink)) {
document.getElementById(\"copybutton\").innerHTML = \"<strong>Copied!</strong>\";
const resetButtonTimeout = setTimeout(resetCopyButton, 3000);
} else {
document.getElementById(\"copybutton\").innerHTML = \"<strong>Could not copy</strong>\";
const resetButtonTimeout = setTimeout(resetCopyButton, 3000);
}
}
function giveLink() {
let linkHTML = \"<input type='text' value='\" + downloadlink + \"' style='width: 40% !important;' disabled><div style='font-size: 5px !important;'>&nbsp;</div><button onclick='copyToClipboard()' style='background-color: rgba(255,255,255,0.3) !important; font-size: 18px !important;'><strong>Copy Link</strong></button>\";
document.getElementById(\"download-link\").innerHTML = linkHTML;
}
window.onload = giveLink;
</script>";
// Have a *relative* link accessible anyway in case the user has javascript disabled
echo "<div align='center' id='download-link' style='pointer-events: auto !important;'><a href='download.php?id=$newFileId'>Download Link</a></div>";
// Display the link
// If we're over SSL or on localhost, display a "copy to clipboard" button
// (The browser navigator.clipboard API is only available over SSL or localhost)
// (This is the answer to the GH bug report #7)
echo "<div align='center' id='download-link' style='pointer-events: auto !important;'><input type='text' value='$download_link' style='width: 40% !important; cursor: text !important;' disabled>";
$javascript_can_copy_to_clipboard = (isSSL() || isLocalhost());
if ($javascript_can_copy_to_clipboard) {
echo "<div style='font-size: 5px !important;'>&nbsp;</div><button id='copybutton' onclick='copyToClipboard()' style='background-color: rgba(255,255,255,0.3) !important; font-size: 18px !important;'><strong>Copy Link</strong></button>";
}
echo "</div>";
} else {
echo "<div align='center'><h1>Error uploading file</h1></div>";