From 5bc03fb3628024dbd2f6a8a3ca86da23390482d6 Mon Sep 17 00:00:00 2001 From: Banana Date: Sat, 26 Apr 2025 10:00:48 +0200 Subject: [PATCH] moved some logic and addede currect mime type info to response Signed-off-by: Banana --- CHANGELOG | 1 + webroot/index.php | 48 +++--------------------------------- webroot/view/created.inc.php | 31 +++++++++++++++++++++-- webroot/view/view.inc.php | 27 ++++++++++++++------ webroot/view/welcome.inc.php | 3 ++- 5 files changed, 55 insertions(+), 55 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 42a674e..a70e7d1 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,6 +2,7 @@ yyyymmdd version 1.x Neon * Moved and added settings to config file. See update.txt * Better logging * Updated client/webclient +* Setting the correct mime type to responses for the browser. FF had a problem with images. 20231009 version 1.5 Fluorine * Maintenance release. Updated requirements to current versions if possible diff --git a/webroot/index.php b/webroot/index.php index 0078112..c3df630 100644 --- a/webroot/index.php +++ b/webroot/index.php @@ -13,7 +13,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0. * - * 2019 - 2023 https://://www.bananas-playground.net/projekt/selfpaste + * 2019 - 2025 https://://www.bananas-playground.net/projekt/selfpaste */ # Encoding and error reporting setting @@ -63,61 +63,19 @@ if(isset($_POST['dl']) && !empty($_POST['dl']) $_create = true; } +# default values $contentType = 'Content-type: text/html; charset=UTF-8'; $contentView = 'welcome'; $httpResponseCode = 200; if(!empty($_short)) { - $contentType = 'Content-type: text/plain; charset=UTF-8'; $contentView = 'view'; - $httpResponseCode = 404; - $contentBody = 'File not found.'; - - $_t = Summoner::b64sl_unpack_id($_short); - $_t = (string)$_t; - $_p = Summoner::forwardslashStringToPath($_t); - $_requestFile = str_ends_with(SELFPASTE_UPLOAD_DIR,'/') ? SELFPASTE_UPLOAD_DIR : SELFPASTE_UPLOAD_DIR.'/'; - $_requestFile .= $_p; - $_requestFile .= $_t; - if(is_readable($_requestFile)) { - $contentBody = $_requestFile; - $httpResponseCode = 200; - } } elseif ($_create === true) { $contentView = 'created'; - $contentType = 'Content-type:application/json;charset=utf-8'; - $httpResponseCode = 400; - $_message = 'Something went wrong.'; - - $_file = $_FILES['pasty']; - - $_fileObj = new Mancubus(); - if($_fileObj->load($_FILES['pasty']) === true) { - $_fileObj->setSaveFilename(); - $_fileObj->setShort(); - $_fileObj->setStoragePath(); - $_fileObj->setShortURL(); - - $_do = $_fileObj->process(); - $_message = $_do['message']; - if($_do['status'] === true) { - $httpResponseCode = 200; - if(defined('LOG_CREATION') && LOG_CREATION === true) { - Summoner::createLog($_message." ".SELFPASTE_UPLOAD_SECRET[$_POST['dl']]); - } - } - } - - $contentBody = array( - 'message' => $_message, - 'status' => $httpResponseCode - ); } -header('X-PROVIDED-BY: selfpaste'); -header($contentType); -http_response_code($httpResponseCode); +header('X-Provided-By: selfpaste'); if(file_exists('view/'.$contentView.'.inc.php')) { require_once 'view/'.$contentView.'.inc.php'; } diff --git a/webroot/view/created.inc.php b/webroot/view/created.inc.php index e4a0c30..e9933cb 100644 --- a/webroot/view/created.inc.php +++ b/webroot/view/created.inc.php @@ -13,6 +13,33 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0. * - * 2019 - 2023 https://://www.bananas-playground.net/projekt/selfpaste + * 2019 - 2025 https://://www.bananas-playground.net/projekt/selfpaste */ -echo json_encode($contentBody)."\n"; +$contentType = 'Content-type:application/json;charset=utf-8'; +$httpResponseCode = 400; +$_message = 'Something went wrong.'; + +$_file = $_FILES['pasty']; + +$_fileObj = new Mancubus(); +if($_fileObj->load($_FILES['pasty']) === true) { + $_fileObj->setSaveFilename(); + $_fileObj->setShort(); + $_fileObj->setStoragePath(); + $_fileObj->setShortURL(); + + $_do = $_fileObj->process(); + $_message = $_do['message']; + if($_do['status'] === true) { + $httpResponseCode = 200; + if(defined('LOG_CREATION') && LOG_CREATION === true) { + Summoner::createLog($_message." ".SELFPASTE_UPLOAD_SECRET[$_POST['dl']]); + } + } +} + +http_response_code($httpResponseCode); +header('Content-type:application/json;charset=utf-8'); +echo json_encode(array( + 'message' => $_message, + 'status' => $httpResponseCode))."\n"; diff --git a/webroot/view/view.inc.php b/webroot/view/view.inc.php index d83e83e..2f1e8b4 100644 --- a/webroot/view/view.inc.php +++ b/webroot/view/view.inc.php @@ -13,15 +13,28 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0. * - * 2019 - 2023 https://://www.bananas-playground.net/projekt/selfpaste + * 2019 - 2025 https://://www.bananas-playground.net/projekt/selfpaste */ -if (file_exists($contentBody)) { + +$_t = Summoner::b64sl_unpack_id($_short); +$_t = (string)$_t; +$_p = Summoner::forwardslashStringToPath($_t); +$_requestFile = str_ends_with(SELFPASTE_UPLOAD_DIR,'/') ? SELFPASTE_UPLOAD_DIR : SELFPASTE_UPLOAD_DIR.'/'; +$_requestFile .= $_p; +$_requestFile .= $_t; +if(is_readable($_requestFile)) { + $finfo = finfo_open(FILEINFO_MIME_TYPE); + $mime = finfo_file($finfo, $_requestFile); + finfo_close($finfo); + + http_response_code(200); + header('Content-type: '.$mime); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); - readfile($contentBody); - exit; -} -else { - echo $contentBody; + readfile($_requestFile); +} else { + http_response_code(404); + header('Content-type: text/plain; charset=UTF-8'); + $contentBody = 'File not found.'; } diff --git a/webroot/view/welcome.inc.php b/webroot/view/welcome.inc.php index 4fe7eec..5d0e7de 100644 --- a/webroot/view/welcome.inc.php +++ b/webroot/view/welcome.inc.php @@ -13,8 +13,9 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0. * - * 2019 - 2023 https://://www.bananas-playground.net/projekt/selfpaste + * 2019 - 2025 https://://www.bananas-playground.net/projekt/selfpaste */ +header('Content-type: text/html; charset=UTF-8'); ?> -- 2.39.5