問題描述
我使用 wordpress 的私人網站,用户上傳文件。如果用户沒有登錄,我使用 「私人 WordPress」 來阻止訪問該網站。
我想對上傳的文件夾中的文件也這樣做。
所以如果一個用户沒有登錄,他們將無法訪問:https://xxxxxxx.com/wp-content/uploads/2011/12/xxxxxxx.pdf 如果他們嘗試訪問,但他們沒有被記錄,那麼他們應該被重定向到登錄頁面,例如。
我發現一個名為私人文件的插件,但最後更新於 2009 年,並不適用於我的 wordpress 。
任何人都知道任何方法嗎? Hotlinking 的方法足以保護這個?
我也發現這個方法:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^.*uploads/private/.*
RewriteCond %{HTTP_COOKIE} !^.*wordpress_logged_in.*$ [NC]
RewriteRule . /index.php [R,L]
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
但是,任何複製 cookie 的用户都可以通過此權限?問候
最佳解決方案
只有檢查 cookie 是否存在,是不是很嚴格的保護。
要獲得更強的保護,您可以通過 php 腳本將”proxy” 的所有請求傳遞到上傳的文件夾 (示例性 uploads
在以下示例中):
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^wp-content/uploads/(.*)$ dl-file.php?file=$1 [QSA,L]
所有上傳文件 (包括帖子中的圖像) 的請求都將轉到 dl-file.php
,然後可以驗證用户是否登錄。
如果用户沒有登錄,您的網站將顯示 login-form 。用户登錄後,她將被重定向到該文件,並可以立即下載。
示例性 dl-file.php
。
在您的 WordPress 安裝中,可以在 wp-includesms-files.php
中找到類似的東西,但是一個用於多站點,並且沒有登錄檢查和重定向。
根據您擁有的流量,可以更好地將其與您的服務器進行集成,例如 X-Accel-Redirect
或 X-Sendfile
標題。
次佳解決方案
您還可以使用 init
鈎子和 get-value $_GET[ 'file' ];
編寫插件。如果用户擁有此 get-value,請跳入功能以檢查對文件的訪問權限:例如使用元框中的複選框。
add_action( 'init', 'fb_init' );
function fb_init() {
// this in a function for init-hook
if ( '' != $_GET[ 'file' ] ) {
fb_get_file( $_GET[ 'file' ] );
}
}
函數 get_file()
function fb_get_file( $file ) {
$upload = wp_upload_dir();
$the_file = $file;
$file = $upload[ 'basedir' ] . '/' . $file;
if ( !is_file( $file ) ) {
status_header( 404 );
die( '404 — File not found.' );
}
else {
$image = get_posts( array( 'post_type' => 'attachment', 'meta_query' => array( array( 'key' => '_wp_attached_file', 'value' => $the_file ) ) ) );
if ( 0 < count( $image ) && 0 < $image[0] -> post_parent ) { // attachment found and parent available
if ( post_password_required( $image[0] -> post_parent ) ) { // password for the post is not available
wp_die( get_the_password_form() );// show the password form
}
$status = get_post_meta( $image[0] -> post_parent, '_inpsyde_protect_content', true );
if ( 1 == $status && !is_user_logged_in() ) {
wp_redirect( wp_login_url( $upload[ 'baseurl' ] . '/' . $the_file ) );
die();
}
}
else {
// not a normal attachment check for thumbnail
$filename = pathinfo( $the_file );
$images = get_posts( array( 'post_type' => 'attachment', 'meta_query' => array( array( 'key' => '_wp_attachment_metadata', 'compare' => 'LIKE', 'value' => $filename[ 'filename' ] . '.' . $filename[ 'extension' ] ) ) ) );
if ( 0 < count( $images ) ) {
foreach ( $images as $SINGLEimage ) {
$meta = wp_get_attachment_metadata( $SINGLEimage -> ID );
if ( 0 < count( $meta[ 'sizes' ] ) ) {
$filepath = pathinfo( $meta[ 'file' ] );
if ( $filepath[ 'dirname' ] == $filename[ 'dirname' ] ) {// current path of the thumbnail
foreach ( $meta[ 'sizes' ] as $SINGLEsize ) {
if ( $filename[ 'filename' ] . '.' . $filename[ 'extension' ] == $SINGLEsize[ 'file' ] ) {
if ( post_password_required( $SINGLEimage -> post_parent ) ) { // password for the post is not available
wp_die( get_the_password_form() );// show the password form
}
die('dD');
$status = get_post_meta( $SINGLEimage -> post_parent, '_inpsyde_protect_content', true );
if ( 1 == $status && !is_user_logged_in() ) {
wp_redirect( wp_login_url( $upload[ 'baseurl' ] . '/' . $the_file ) );
die();
}
}
}
}
}
}
}
}
}
$mime = wp_check_filetype( $file );
if( false === $mime[ 'type' ] && function_exists( 'mime_content_type' ) )
$mime[ 'type' ] = mime_content_type( $file );
if( $mime[ 'type' ] )
$mimetype = $mime[ 'type' ];
else
$mimetype = 'image/' . substr( $file, strrpos( $file, '.' ) + 1 );
header( 'Content-type: ' . $mimetype ); // always send this
if ( false === strpos( $_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS' ) )
header( 'Content-Length: ' . filesize( $file ) );
$last_modified = gmdate( 'D, d M Y H:i:s', filemtime( $file ) );
$etag = '"' . md5( $last_modified ) . '"';
header( "Last-Modified: $last_modified GMT" );
header( 'ETag: ' . $etag );
header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + 100000000 ) . ' GMT' );
// Support for Conditional GET
$client_etag = isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ? stripslashes( $_SERVER['HTTP_IF_NONE_MATCH'] ) : false;
if( ! isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) )
$_SERVER['HTTP_IF_MODIFIED_SINCE'] = false;
$client_last_modified = trim( $_SERVER['HTTP_IF_MODIFIED_SINCE'] );
// If string is empty, return 0. If not, attempt to parse into a timestamp
$client_modified_timestamp = $client_last_modified ? strtotime( $client_last_modified ) : 0;
// Make a timestamp for our most recent modification...
$modified_timestamp = strtotime($last_modified);
if ( ( $client_last_modified && $client_etag )
? ( ( $client_modified_timestamp >= $modified_timestamp) && ( $client_etag == $etag ) )
: ( ( $client_modified_timestamp >= $modified_timestamp) || ( $client_etag == $etag ) )
) {
status_header( 304 );
exit;
}
// If we made it this far, just serve the file
readfile( $file );
die();
}
您還可以通過鈎子 generate_rewrite_rules
為文件添加自定義 URL
add_filter( 'generate_rewrite_rules', 'fb_generate_rewrite_rules' );
function fb_generate_rewrite_rules( $wprewrite ) {
$upload = wp_upload_dir();
$path = str_replace( site_url( '/' ), '', $upload[ 'baseurl' ] );
$wprewrite -> non_wp_rules = array( $path . '/(.*)' => 'index.php?file=$1' );
return $wprewrite;
}
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。