81
General Help & Support / Re: How to protect images on a "Logged In Only" page
« Last post by sternchen8875 on August 24, 2025, 11:43:05 PM »and i need the used WB-Version, important for the image-handling inside the gallery
WebsiteBaker 2.13.9 R24 is now available!
R.I.P Dietmar (luisehahne) and thank you for all your valuable work for WB
https://forum.websitebaker.org/index.php/topic,32355.0.html
I am more looking for 'protected' MiniGallery-albums.
I could Make a ckeditor pluginThat would be wonderful already, although I am more looking for 'protected' MiniGallery-albums. But I understand it will be very complicated to get this to work.
<img alt="" class="img-responsive" height="473" src="http://wb1620/media/secure_file.php?file=tiger1.webp" style="" width="760">
<img alt="" class="img-responsive" height="473" src="http://wb1620/media/secure_file.php?file=tiger1.webp" style="" width="760">
<?php
// =========== DEBUGGING-SCHALTER ===========
// debug mode true or false (show's debug message in direct call
$debug = false;
// ------------------------------------------
// load config.php
require(__DIR__ . '/../config.php');
// build frontend object
if (!isset($wb) || (isset($wb) && !($wb instanceof \frontend))) {
$wb = new \frontend();
}
// set error report method
if ($debug) {
error_reporting(E_ALL);
ini_set('display_errors', 1);
}
// is logged in or not
if ($wb->is_authenticated()) {
// read path from link
if (empty($_GET['file'])) {
http_response_code(400); // Bad Request
die('Error: No file specified.');
}
$requestedFile = urldecode($_GET['file']);
$prefix = 'media/private/';
if (strpos($requestedFile, $prefix) === 0) {
$requestedFile = substr($requestedFile, strlen($prefix));
}
$fileName = basename($requestedFile);
// build the correct path
$filePath = WB_PATH . '/media/private/' . $fileName;
// security check - file must be inside of protected folder
$realBasePath = realpath(WB_PATH . '/media/private');
$realFilePath = realpath($filePath);
if ($realFilePath === false || strpos($realFilePath, $realBasePath) !== 0) {
if ($debug) die("DEBUG: Zugriff verweigert! Pfad ungültig. Gesucht wurde: " . htmlspecialchars($filePath));
header("HTTP/1.0 403 Forbidden");
die("Access Denied");
}
// Whitelist for filetypes, add more, if needed
$allowedTypes = ['image/gif', 'image/png', 'image/jpeg', 'image/webp'];
$mimeType = mime_content_type($realFilePath);
if (in_array($mimeType, $allowedTypes)) {
if (!$debug) {
header("Content-Type: " . $mimeType);
header("Content-Length: " . filesize($realFilePath));
header("Content-Disposition: inline; filename=\"" . basename($realFilePath) . "\"");
header("Cache-Control: private, no-cache, must-revalidate");
header("Pragma: no-cache");
ob_clean();
flush();
readfile($realFilePath);
exit();
} else {
// show debug messages
echo "<b>DEBUG-MODUS: Success!</b><br>";
echo "This script is working:<br>";
echo "<b>path:</b> " . htmlspecialchars($realFilePath);
die();
}
} else {
if ($debug) die("DEBUG: forbidden filetype!");
header("HTTP/1.0 403 Forbidden");
die("File type not allowed.");
}
} else {
// if not authenticated, send 403-error
if ($debug) die("DEBUG: not authenticated!");
header('HTTP/1.0 403 Forbidden', TRUE, 403);
die('<!DOCTYPE HTML><html><head><title>403 Forbidden</title></head><body><h1>Forbidden</h1><p>You don\'t have permission to access this file.</p></body></html>');
}
?>
# for Apache 2.4 and newer
Require all denied
# for Apache 2.2 and older (Fallback)
<IfModule !mod_authz_core.c>
Order Deny,Allow
Deny from all
</IfModule>
Situation: An image is on a page that you can only access once you're logged in. The images however, are stored in the /Media folder. So in theory, someone who knows the URL, could potentially acces the image file directly, bypassing the login-requirement. Also, crawler software would find all the images in de /Media folder(s). Even those images that you want to be accessible for logged in users only.
RewriteEngine On
# Block access if the request for an image is not coming from your own domain.
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [F]
RewriteEngine On
# Zugriff blockieren, wenn die Anfrage für ein Bild nicht von deiner eigenen Domain kommt.
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?deinedomain.de [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [F]