在WordPress支持网站上工作,该网站只注册了用户内容,包括上传的PDF和ZIP文件。
我正在寻找一种方法来防止不使用插件直接访问wp content/uploads目录中的PDF和ZIP文件。
通读以前的问题,这真的很接近(但评论已关闭):https://wordpress.stackexchange.com/a/37743/18608 当它检测到对文件的直接访问时,会进行一些登录以保存请求的文件,并检查用户是否已登录。如果没有,他们将被重定向到WordPress登录。如果他们已登录,则会加载文件。
这是原始htaccess:
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^wp-content/uploads/(.*)$ dl-file.php?file=$1 [QSA,L]
但是,脚本阻止访问
all wp content/uploads目录中的文件,包括图像-因此,除非用户登录,否则即使博客帖子图像(不需要保护)也会隐藏在前端。
我正在修改重写规则以检查only PDF或ZIP文件,以便dl文件。仅当请求的是PDF或ZIP文件时,才会调用php文件。
我尝试了以下操作,但它返回“404文件未找到”当用户登录时访问PDF或ZIP时,因此即使文件类型检查似乎有效,dl文件仍然有效。php检查失败。
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^wp-content/uploads/([^/]*\\.(pdf|zip))$ filecheck.php?file=$1 [QSA,L]
如何修改它,使其只调用dl文件。php,如果请求的是pdf或zip文件,但仍将正确的信息传递给dl文件。php?
谢谢你,乔纳森
/*
* dl-file.php
*
* Protect uploaded files with login.
*
* @link http://wordpress.stackexchange.com/questions/37144/protect-wordpress-uploads-if-user-is-not-logged-in
*
* @author hakre <http://hakre.wordpress.com/>
* @license GPL-3.0+
* @registry SPDX
*/
require_once(\'wp-load.php\');
is_user_logged_in() || auth_redirect();
list($basedir) = array_values(array_intersect_key(wp_upload_dir(), array(\'basedir\' => 1)))+array(NULL);
$file = rtrim($basedir,\'/\').\'/\'.str_replace(\'..\', \'\', isset($_GET[ \'file\' ])?$_GET[ \'file\' ]:\'\');
if (!$basedir || !is_file($file)) {
status_header(404);
die(\'404 — File not found.\');
}
$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 );