Home
Download
Add-ons
Help
Forum
Organisation
Project
Welcome,
Guest
. Please
login
or
register
.
Did you miss your
activation email?
February 12, 2012, 03:42:02 AM
1 Hour
1 Day
1 Week
1 Month
Forever
Login with username, password and session length
Search:
Advanced search
Interested in joining the WebsiteBaker team?
For more Information read
here
or on our
new website
.
149621
Posts in
21098
Topics by
7537
Members
Latest Member:
lotte2
WebsiteBaker Community Forum
English
Droplets (PHP code for use with Droplet module) & Snippets (raw PHP code)
(Moderator:
Argos
)
filedownload protect with .htaccess and php code
Pages: [
1
]
Go Down
Author
Topic: filedownload protect with .htaccess and php code (Read 2113 times)
pcwacht
AddOn Development
Offline
Posts: 2814
filedownload protect with .htaccess and php code
«
on:
March 09, 2010, 02:14:09 PM »
You have some files wich are only for the registred visitors and you don't want any non registred visitor to get the file
Steps:
1 - create a file in the root of your installation and call it : WB-securedownload
Code:
<?php
// Secure download for websitebaker
// original by VotreEspace see thread : http://www.websitebaker2.org/forum/index.php/topic,16282.msg106944.html#msg106944
// adapted and improved by PCWacht (march-2010)
//
// Use as : WB-securedownload.php?file=/media/thisdocument.doc
//
//
if (isset(
$_GET
[
'file'
])) {
require(
"config.php"
);
# code the url
$DEC
=
urldecode
(
$_GET
[
'file'
]);
# set unallowed file, so nobody want to read /config.php or something
$unallowed_to_read
= array(
'php'
,
'html'
,
'htm'
,
'htaccess'
);
#Check for images, they should be shown
$allowed_to_read
= array(
'jpg'
,
'gif'
,
'png'
);
# remove any attempt to back up your folders
$fichier
=
str_replace
(
'../'
,
''
,
WB_PATH
.
'/media/'
.
urldecode
(
$_GET
[
'file'
]));
if(isset(
$_SESSION
[
'USER_ID'
]) &&
SESSION_STARTED
) {
if(
file_exists
(
$fichier
) && (!
in_array
(
end
(
explode
(
'.'
,
$fichier
)),
$unallowed_to_read
))) {
header
(
"Content-Type: "
.
mime_content_type
(
$fichier
));
header
(
"Content-Length: "
.
filesize
(
$fichier
));
header
(
"Content-Transfer-Encoding: binary"
);
header
(
"Cache-Control: private"
);
header
(
'Content-Disposition: attachment; filename="'
.
end
(
explode
(
'/'
,
$DEC
)).
'"'
);
echo
file_get_contents
(
$fichier
);
} else {
# in case of absent file or attempt at hacking
echo
'oups'
;
}
} else {
if(
file_exists
(
$fichier
) && (
in_array
(
end
(
explode
(
'.'
,
$fichier
)),
$allowed_to_read
))) {
header
(
"Content-Type: "
.
mime_content_type
(
$fichier
));
header
(
"Content-Length: "
.
filesize
(
$fichier
));
header
(
"Content-Transfer-Encoding: binary"
);
header
(
"Cache-Control: private"
);
header
(
'Content-Disposition: attachment; filename="'
.
end
(
explode
(
'/'
,
$DEC
)).
'"'
);
echo
file_get_contents
(
$fichier
);
} else {
# not logged in and no picture? forbidden!
header
(
'HTTP/1.0 403 forbidden'
,
TRUE
,
403
);
die(
'<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>FORBIDDEN</h1>
<p>Without being logged in you don'
t have permission to fetch
<
b
>
'.urldecode($_GET['
file
']).'
</
b
>
from this server server
.</
p
>
<
p
><
a href
=
"/account/login.php"
>
Click
for
login
.</
a
></
p
>
</
body
></
html
>
');
}
}
}else{
header('
Location
: ./
');
}
?>
2 - create a .htaccess file in the foor of your wb installation in it:
Code:
RewriteEngine on
RewriteRule ^media/(.*)\.(.*)$ /WB-securedownload.php?file=$1.$2 [R,L]
tip - - change the path /WB-securedownload to /wb-path/WB-securedownload if needed
The way it works
1 with a htaccess rewriterule we test to see if something is needed from teh media folder, if so redirect it to a script so we can test it
examplë:
http://www.someserver.com/media/somefolder/somefile.doc
will become:
http://www.someserver.com/WB-securedownload?file=somefolder/somefile.doc
The script in WB-securedir will test to see if:
1 - the parameter file was given
2 - if user is logged in
3 - if it is a picture, then show it
Have fun,
John
Logged
http://www.ictwacht.nl
= Dutch ICT info
http://www.pcwacht.nl
= My first
both still work in progress, since years.....
Ralph
Offline
Posts: 39
Re: filedownload protect with .htaccess and php code
«
Reply #1 on:
June 21, 2010, 11:46:21 AM »
Hallo John,
ich habe versucht Dein Beispiel in meine Seite einzubauen, leider ohne Erfolg. In dem Media Ordner hab ich PDF Dateien (auch Unterordner). Ich will verhindern das man ohne Anmeldung auf die PDF zugreifen kann wenn man den direkten Pfad kennt.
Die Datei 'WB-securedownload.php' hab ich ins Rootverzeichniss (
www.xyz.de/WB-securedownload.php
) des Webservers kopiert und die htaccess in das Rootverzeichnis der WebsiteBaker Installation.
Was mache ich falsch?
Leider ist mein Englisch sooo schlecht, das mir nichts übrig bleibt als in Deutsch anzufragen ob Du mir helfen kannst.
Gruß
Ralph
Logged
Ralph
Offline
Posts: 39
Re: filedownload protect with .htaccess and php code
«
Reply #2 on:
June 22, 2010, 02:22:59 PM »
Hi John,
I have a little now can achieve. But after calling a file, whether loggin or not I get a 404 Not Found, and the URL that is generated looks like this:
http://www.xyz.de/var/www/vhosts/xyz.de/WB-securedownload.php?file=download_gallery/xy.pdf
please help me
Ralph
Logged
kweitzel
Forum administrator
Offline
Posts: 6819
Re: filedownload protect with .htaccess and php code
«
Reply #3 on:
June 22, 2010, 02:51:56 PM »
That does sound as if you have the WB_URL in front of the WB_PATH ... did you just copy the script and the htaccess or did you change it?
cheers
Klaus
Logged
WebsiteBaker Org e.V. - for WebsiteBaker
pcwacht
AddOn Development
Offline
Posts: 2814
Re: filedownload protect with .htaccess and php code
«
Reply #4 on:
June 22, 2010, 02:56:18 PM »
Schau dir die .htaccess nach weil die linke:
http://www.xyz.de/var/www/vhosts/xyz.de/WB-securedownload.php?file=download_gallery/xy.pdf
soll
http://www.xyz.de/WB-securedownload.php?file=download_gallery/xy.pdf
sein
Ich glaube die .htaccess macht wass falsch, vielleicht hatte es etwas mit symlinks zu machen.
versuche mal diese .htaccess
Quote
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^media/(.*)\.(.*)$ /WB-securedownload.php?file=$1.$2 [R,L]
Wenn es nuhr fur einer unterordner unter media geben soll:
Quote
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^media/unterordner/(.*)\.(.*)$ /WB-securedownload.php?file=$1.$2 [R,L]
Ich hoffe du schaft es.
Spass,
John
Logged
http://www.ictwacht.nl
= Dutch ICT info
http://www.pcwacht.nl
= My first
both still work in progress, since years.....
pcwacht
AddOn Development
Offline
Posts: 2814
Re: filedownload protect with .htaccess and php code
«
Reply #5 on:
June 22, 2010, 02:59:15 PM »
RewriteBase /
is needed as well
try:
Code:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^media/(.*)\.(.*)$ /WB-securedownload.php?file=$1.$2 [R,L]
John
Logged
http://www.ictwacht.nl
= Dutch ICT info
http://www.pcwacht.nl
= My first
both still work in progress, since years.....
Ralph
Offline
Posts: 39
Re: filedownload protect with .htaccess and php code
«
Reply #6 on:
June 22, 2010, 03:35:58 PM »
with RewriteBase / sees the URL as it should be like this but it is a Error 400 "Bad Request
Your browser sent a request that this server could not understand.
Client sent malformed Host header"
and now?
Logged
pcwacht
AddOn Development
Offline
Posts: 2814
Re: filedownload protect with .htaccess and php code
«
Reply #7 on:
June 22, 2010, 03:40:55 PM »
So mit RewriteBase /
die url rewriting geht gut?
Sie bekommen etwas als :
http://www.xyz.de/WB-securedownload.php?file=download_gallery/xy.pdf
Die Bad Request komt von die WB-securedownload.php ??
sei sicher das die WB-securedownload stimmt mit dass wass ich als ersten geschrieben habe.
Wenn nur etwas fehlt bekommt du das
Sie könte es aber testen mit:
WB-securedownload.php?file=einer.pdf
Spass,
John
Logged
http://www.ictwacht.nl
= Dutch ICT info
http://www.pcwacht.nl
= My first
both still work in progress, since years.....
Ralph
Offline
Posts: 39
Re: filedownload protect with .htaccess and php code
«
Reply #8 on:
June 23, 2010, 08:20:33 AM »
Thanks John,
in the. htaccess I had before / WB securedownload.php still do .. / or he calls on not. Now looks like this:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^media/(.*)\.(.*)$ ../WB-securedownload.php?file=$1.$2 [R,L]
The / WB-securedownload.php (I have no changes from you) creates now this url:
Url:
http://www.xyz.de/WB-securedownload.php?file=download_gallery/xy.pdf
When you call, whether logged in or not here comes this error:
Bad Request 400
Your browser sent a request that this server could not understand.
Client sent malformed Host header
Can it deal here with an error:
header("Content-Type: " . mime_content_type($fichier));
Because there are pdf?
If I put both scripts in the WB folder and the two .. remove, then comes the error "not found".
I know now go no further
Logged
Ralph
Offline
Posts: 39
Re: filedownload protect with .htaccess and php code
«
Reply #9 on:
June 30, 2010, 05:31:17 PM »
Hallo John,
nochmals Danke für Deine Unterstützung, hab jetzt es jetzt zum Laufen gebracht (ein anderer Profi hat mich unterstützt
).
Ich habe beide Dateien in das Root Verzeichnis der WB Installation kopiert, mit der .htaccsess war alles in Ordnung. In den WB-Secure Script hatten sich zwei kleine Fehler eingeschlichen. Im letzten else Zweig beim don't das Hochkomma und weiss ich nicht mehr daher hier meine geänderte Fassung
Code:
<?php
// Secure download for websitebaker
// original by VotreEspace see thread : http://www.websitebaker2.org/forum/index.php/topic,16282.msg106944.html#msg106944
// adapted and improved by PCWacht (march-2010)
//
// Use as : WB-securedownload.php?file=/media/thisdocument.doc
//
//
if (isset(
$_GET
[
'file'
])) {
require(
"config.php"
);
# code the url
$DEC
=
urldecode
(
$_GET
[
'file'
]);
# set unallowed file, so nobody want to read /config.php or something
$unallowed_to_read
= array(
'php'
,
'html'
,
'htm'
,
'htaccess'
);
#Check for images, they should be shown
$allowed_to_read
= array(
'jpg'
,
'gif'
,
'png'
);
# remove any attempt to back up your folders
$fichier
=
str_replace
(
'../'
,
''
,
WB_PATH
.
'/media/'
.
urldecode
(
$_GET
[
'file'
]));
if(isset(
$_SESSION
[
'USER_ID'
]) &&
SESSION_STARTED
) {
if(
file_exists
(
$fichier
) && (!
in_array
(
end
(
explode
(
'.'
,
$fichier
)),
$unallowed_to_read
))) {
header
(
"Content-Type: "
.
mime_content_type
(
$fichier
));
header
(
"Content-Length: "
.
filesize
(
$fichier
));
header
(
"Content-Transfer-Encoding: binary"
);
header
(
"Cache-Control: private"
);
header
(
'Content-Disposition: attachment; filename="'
.
end
(
explode
(
'/'
,
$DEC
)).
'"'
);
echo
file_get_contents
(
$fichier
);
} else {
# in case of absent file or attempt at hacking
echo
'oups'
;
}
} else {
/*if(file_exists($fichier) && (in_array(end(explode('.',$fichier)),$allowed_to_read))) {
header("Content-Type: " . mime_content_type($fichier));
header("Content-Length: " . filesize($fichier));
header("Content-Transfer-Encoding: binary");
header("Cache-Control: private");
header('Content-Disposition: attachment; filename="'.end(explode('/',$DEC)).'"');
echo file_get_contents($fichier);
} else {*/
# not logged in and no picture? forbidden!
header
(
'HTTP/1.0 403 forbidden'
,
TRUE
,
403
);
die(
'<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>FORBIDDEN</h1>
<p>Without being logged in you dont have permission to fetch <b>'
.
urldecode
(
$_GET
[
'file'
]).
'</b> from this server server.</p>
<p><a href="/vWB_Verzeichnis/account/login.php">Click for login.</a></p>
</body></html> '
);
}
//}
}else{
header
(
'Location: ./'
);
}
?>
Was mich schon wundert ist, dass diesem Problem der Verzeichnissicherhe
it hier nicht mehr Aufmerksamkeit geschenkt wird bzw. scheint es den Nutzern noch nicht augefallen zu sein, dass man über den absoluten Pfad alles bekommen kann. Egal Problem gelöst , nehmen wir die nächten in Angriff. Ansonsten macht mir WB sehr viel Spass, man muß sich eben reindenken. Also Danke für Eure Hilfe bis bald
Ralph
Logged
DarkViper
Development Team
Offline
Posts: 1049
Re: filedownload protect with .htaccess and php code
«
Reply #10 on:
June 30, 2010, 07:04:56 PM »
Quote
Was mich schon wundert ist, dass...
What always makes
me
surprised is, that...
... such a lot of people use this three characters
CMS
... without knowing about the real meaning of.
Read this...
Logged
Anleitungen lesen und selber nachdenken ist anstrengend... Da lass ich doch lieber andere für mich denken...
In
1984
: Nineteen Eighty-Four is a unrealistic utopia!!
In
2012
: Nineteen Eighty-Four is a little piece only of our reality!!
Ralph
Offline
Posts: 39
Re: filedownload protect with .htaccess and php code
«
Reply #11 on:
June 30, 2010, 07:39:33 PM »
Hallo DarkViper,
wie ich schon gesagt habe, ist mein Englisch grotten schlecht aber wegen deinem Kommentar
... such a lot of people use this three characters CMS... without knowing about the real meaning of.
bin ich ja hier um Fragen zu stellen. Was nützt ein Super CMS wenn es bestimmte Sicherheitsfragen gibt. Aber an dieser Stelle genug, ich denke Websitbaker mit seiner Gemeinschaft hat das Zeug früher oder Später zü den ganz Großen zu gehören.
Gruß Ralph
Logged
Hans Toolbox
Offline
Posts: 910
Re: filedownload protect with .htaccess and php code
«
Reply #12 on:
June 30, 2010, 07:49:09 PM »
Es gibt in diesem Falle keine Sicherheitsfragen, da man -wie schon gesagt- ein CMS vor sich hat.
Will man "Dateien schützen", die nicht für die Öffentlichkeit bestimmt sind, greift man zu entsprechenden Systemen.
Logged
[Die Beleidigung gegenüber mir wurde durch mich gelöscht, User wurde von mir ausgeschlossen - kweitzel]
pcwacht
AddOn Development
Offline
Posts: 2814
Re: filedownload protect with .htaccess and php code
«
Reply #13 on:
June 30, 2010, 08:13:38 PM »
Quote
Will man "Dateien schützen", die nicht für die Öffentlichkeit bestimmt sind, greift man zu entsprechenden Systemen.
Hmmm.. nicht meiner meinung, es gibt viele wegen zum schutzen
Einer ist dieses mit htaccess
Ein andere wäre zum beispiel ein directory zu machen ausser das html bereich und es mit php control zu ubergeben
Ein andere ist um die dateien ins database zu verlegen.
Genau dass ist das beste an WB, ins herz ist es einfach, aber powerfull genug um es mit ein wenig code dass zu machen was man braucht.
WB kann fast alles! Aber nicht nur mit die core, stimmt.
John
Logged
http://www.ictwacht.nl
= Dutch ICT info
http://www.pcwacht.nl
= My first
both still work in progress, since years.....
Hans Toolbox
Offline
Posts: 910
Re: filedownload protect with .htaccess and php code
«
Reply #14 on:
June 30, 2010, 09:14:31 PM »
htaccess ist auf Apache beschränkt.
WebsiteBaker wird von mir empfohlen, weil es auf NGINX, LIGHTTPD oder
*
beliebig anderen http-Diensten funktioniert. Nicht anderes erwarte ich auch von einem PHP-CMS. Das ist eine hervorstechende Eigenschaft von websitebaker, die leider nur von wenigen erkannt wird. WB läuft überall!
*
: Serverspezifisches File-Management in WB ist überflüssig oder dürfte dann etwas umfangreicher ausfallen
Wenn WB weiterhin unabhängig funktionieren soll ist Dein Vorschlag "php control" richtungsweisend. Zwei meiner Anwender haben auch ihre Medien-Dateien unterhalb von wwwroot und verwalten diese über ein Script.
*
=
Textänderungen
Deine Lösung (htaccess/php) ist und bleibt natürlich trotzdem eine wirklich tolle (+tricky)
!
There is an English forum here, I finish my German responses
«
Last Edit: July 01, 2010, 01:10:08 AM by Hans Toolbox
»
Logged
[Die Beleidigung gegenüber mir wurde durch mich gelöscht, User wurde von mir ausgeschlossen - kweitzel]
Pages: [
1
]
Go Up
Jump to:
Please select a destination:
-----------------------------
General
-----------------------------
=> General Announcements
-----------------------------
English
-----------------------------
=> Help & Support
-----------------------------
General
-----------------------------
=> WebsiteBaker Website Showcase
-----------------------------
English
-----------------------------
=> Modules
=> Templates, Menus & Design
=> WebsiteBaker Language Files
=> Droplets (PHP code for use with Droplet module) & Snippets (raw PHP code)
-----------------------------
General
-----------------------------
=> Guest Area & Off-Topic
-----------------------------
English
-----------------------------
=> WebsiteBaker 2.x discussion
=> WebsiteBaker 3
-----------------------------
General
-----------------------------
=> Security Announcements
-----------------------------
Deutsch (German)
-----------------------------
=> Hilfe/Support
-----------------------------
General
-----------------------------
=> Documentation
-----------------------------
Francais (French)
-----------------------------
=> Help/Support
-----------------------------
Italiano (Italian)
-----------------------------
=> Help/Support
-----------------------------
Deutsch (German)
-----------------------------
=> Ankündigungen
=> Diskussion über WB
=> Off-Topic
=> Archiv für Themen bis 2007
=> Module & Snippets
-----------------------------
English
-----------------------------
=> Archive (posts up to 2007)
-----------------------------
Nederlands (Dutch)
-----------------------------
=> Aankondigingen
=> Hulp & Ondersteuning
=> Niet-Terzake (Off Topic)
-----------------------------
Deutsch (German)
-----------------------------
=> jQuery
=> Tutorials
=> Templates & Design
-----------------------------
English
-----------------------------
=> jQuery
-----------------------------
Bakery (WB shop module)
-----------------------------
=> Bakery English
=> Bakery Deutsch
-----------------------------
English
-----------------------------
=> WebsiteBaker 2.9
===> Announcements
===> Help/Support
===> Suggestions
-----------------------------
Deutsch (German)
-----------------------------
=> WebsiteBaker 2.9
===> Ankündigungen
===> Hilfe/Support
===> Vorschläge
-----------------------------
English
-----------------------------
===> Software bugs
-----------------------------
Deutsch (German)
-----------------------------
===> Softwarefehler
=====> Module / Extensions
-----------------------------
English
-----------------------------
=====> Modules / Extensions
-----------------------------
Deutsch (German)
-----------------------------
===> Erfahrungs und Testberichte
-----------------------------
KeepInTouch (Multi Contact Module)
-----------------------------
=> KeepInTouch English
=> KeepInTouch Deutsch
Loading...