diff --git a/CHANGELOG b/CHANGELOG index d56c9c0..f39e678 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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) \ No newline at end of file diff --git a/functions.global.php b/functions.global.php index 3978fb1..5a83b96 100644 --- a/functions.global.php +++ b/functions.global.php @@ -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; +} + ?> diff --git a/upload.php b/upload.php index 0ef8ddf..7ce16f5 100644 --- a/upload.php +++ b/upload.php @@ -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 ""; - // Have a *relative* link accessible anyway in case the user has javascript disabled - echo ""; + // 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 ""; } else { echo "

Error uploading file

";