保护直接访问PDF和ZIP,除非用户登录(无插件)

时间:2017-09-29 作者:d38

在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 &#8212; 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 );

1 个回复
最合适的回答,由SO网友:MrWhite 整理而成

RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^wp-content/uploads/([^/]*\\.(pdf|zip))$ filecheck.php?file=$1 [QSA,L]
这实际上看起来还可以,除非在/uploads 目录另一种方法是在原始规则中包含一个附加条件,仅当请求以.pdf.zip. 例如:

RewriteCond %{REQUEST_URI} \\.(pdf|zip)$ [NC]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^wp-content/uploads/(.*)$ dl-file.php?file=$1 [QSA,L]
这并不重要,但要确保这在WordPress前端控制器之前进行。

结束

相关推荐

Altered Media Library URLs

我有一个客户的网站,是在他们离开另一家代理机构后我找到的。该机构使用了一个专有主题和自己的自托管页面生成器,以防止其在除他们之外的任何其他托管环境中更新或编辑。它的另一个方面是重新映射主题的URL并上载目录。因此,例如,代替WP在中查找主题文件http://domain.com/wp-content/themes/…. 它在里面找他们http://domain.com/t/….同样,对于图像上载,也可以在http://domain.com/wp-content/uploads/…, 它在里面找他们http