如果用户未登录,如何保护上传?


79

我将wordpress用于用户上传文件的私人站点。如果用户未登录,我将使用“ Private WordPress”阻止访问该网站。

我想对uploads文件夹中上传的文件执行相同的操作。

因此,如果未登录的用户将无法访问:https : //xxxxxxx.com/wp-content/uploads/2011/12/xxxxxxx.pdf 如果他们尝试访问但未登录,则应该例如重定向到登录页面。

我找到了一个名为“私人文件”的插件,但是上次更新是在2009年,它似乎在我的wordpress上不起作用。

有人知道什么方法吗?热链接方法足以保护这一点吗?

我也发现了这种方法:

# 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的用户都可以通过此权限吗?问候


1
为什么不能使用其他上载目录(例如网站根目录之外的目录)有什么原因?
onetrickpony

并非如此,但是我已经将大量文件附加到该目录中的帖子中,如果我能找到合适的解决方案,我不介意四处走动
chifliiiii 2011年

Answers:


86

仅检查cookie是否存在并不是很严格的保护。

为了获得更强大的保护,您可以通过uploadsphp脚本将所有请求传递或“代理”到上传的文件夹(在以下示例中为示例):

RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^wp-content/uploads/(.*)$ dl-file.php?file=$1 [QSA,L]

对上传文件的所有请求(包括帖子中的图像)都将转到该请求dl-file.php,然后可以验证用户是否登录。

如果用户未登录,将显示您的网站登录表单。用户登录后,她将被重定向回该文件并可以立即下载。

示范性的dl-file.php

\wp-includes\ms-files.php在您的wordpress安装中可以找到类似的东西,但是那是针对多站点的,并且没有登录检查和重定向。

根据多少流量有,它可能是明智的,以便更好地与您的服务器,如集成这X-Accel-RedirectX-Sendfile头。


1
如果我要将文件存储到wp-content / uploads / secure等子目录中,如何调整dl-file.php?

这是唯一真正安全的解决方案。您可以在网络上找到的任何其他内容(如检查引荐标头,检查cookie,禁止目录列表)只是一半的措施,因为您可以轻松地欺骗HTTP请求标头来解决它。
路加福音

伙计们..这对我来说似乎是一个完美的解决方案。问题是,我使用Mozilla的PDFJS从上载文件夹访问一些PDF,而PDFJS使用部分内容标头仅获取其感兴趣的页面。 .so这个解决方案对我来说是行不通的。有什么建议么??
Otto Nascarella 2014年

@OttoNascarella:到目前为止,对PHP的部分内容请求已经解决,这独立于此wordpress问题。实际上,问题已经很老了:使用PHP发送文件时可恢复下载?
hakre 2014年

@hakre那在网站首页上使用的那些图像和任何用户来访问该网站的图像怎么样?如果我没有登录,它会给我404错误。
Dhaval Panchal '18年

14

您还可以使用inithook和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 &#8212; 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();
}

您还可以通过挂钩添加文件的自定义URL generate_rewrite_rules

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;
}

这对我没有用,有人知道为什么吗?我完全复制。
Ryan S

保护仅适用于pdf。其他文件扩展名无法正常工作:doc,docx,jpg等...
Patel

1

如果您希望使用基于插件的方法来解决此问题,这里是(最终)我发现的一个相当不错的解决方案:

  1. 安装插件“下载监控器”,网址为:https
    //wordpress.org/plugins/download-monitor/
  2. 在WordPress仪表板中,转到新的“下载”菜单项并添加一个新的“下载”,如插件文档网站上所述:https : //www.download-monitor.com/kb/adding-downloads/。记下为您提供的“下载”简码(例如,保存到记事本)。请注意,文件保存在/wp-content/uploads/dlm_uploads/
  3. 在“下载选项”元框中,指定“仅会员”(如https://www.download-monitor.com/kb/download-options/所示),然后单击“发布”。
  4. 在您只希望成员下载的页面上,添加在步骤2中记下的简码,然后“发布/更新”页面,如此处记录:https : //www.download-monitor.com / kb / shortcode-download /。您可以按照https://www.download-monitor.com/kb/content-templates/所述更改下载链接模板,也可以创建自己的模板(例如,删除“下载”计数)
  5. 浏览到您的页面,您应该看到一个下载链接(但不会显示下载文件的URL)。如果在新的浏览器窗口(或隐身窗口)中浏览到同一页面,则应该发现下载不再起作用。

这意味着未登录的任何人都无法下载文件或查看文件的真实URL。如果有人未经授权找出文件的URL,则该插件还会通过阻止对/wp-content/uploads/dlm_uploads/文件夹的访问来阻止用户浏览真实文件的URL 。

奖励:如果您在需要用户只能以“会员”身份登录(但没有诸如页面编辑或管理员身份的WordPress权限)的网站上进行此操作,请安装“会员”插件https:// wordpress .org / plugins / members /,创建一个名为“成员”的新用户角色,并赋予其“读取”的单一功能,在WordPress中创建一个新用户,并确保为其赋予一个“成员”角色。

如果您想保护页面的内容,可以使用“成员”插件提供一些选项,或者那里还有其他插件。如果要使登录页面的主题比WordPress默认登录表单更好,请使用“主题我的登录”之类的内容:https : //wordpress.org/plugins/theme-my-login/


我在上面已经描述也说明在这里,但你可以看到它并没有具体到只是PDF文件的过程:thedigitalcrowd.com/website-development/wordpress/...
马蒂Ĵ
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.