限制对wordpress用户的访问权限到文件夹和php文件

时间:2018-10-31 09:23:42

标签: php wordpress

首先,对不起我的英语。

我正在开发可在wordpress上运行的Web应用程序。我有一个名为GSC的文件夹,并且在其中是我的应用程序。

我的问题是,当我必须控制对用户对该目录的访问时,因为所有部分都是私有的,仅适用于注册用户。

我尝试将某些插件用作愿望清单成员,并且仅保护主文件夹并且在子文件夹中没有控制权,因此该应用程序失败,因为它无法加载所有必需的东西。

所以我到处搜索了,找不到更多插件,或者我不知道是否应该通过在每个php文件中编程来控制是否存在用户会话,或者wordpress是否还有其他东西可以控制访问到文件夹,甚至看看是否可以配置htaccess,以便它取决于wordpress用户,但我不知道是否可以。

如果有人可以给我一些想法。

谢谢

更新:

我的htaccess

RewriteEngine on
RewriteCond %{REQUEST_URI} ^.*gsc/formulario-clientes/.*
RewriteRule ^(.*)$ /wp-private.php?file=$1 [QSA,L]

Wp-private.php

<?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');
require_once ABSPATH . WPINC . '/formatting.php';
require_once ABSPATH . WPINC . '/capabilities.php';
require_once ABSPATH . WPINC . '/user.php';
require_once ABSPATH . WPINC . '/meta.php';
require_once ABSPATH . WPINC . '/post.php';
require_once ABSPATH . WPINC . '/pluggable.php';
require_once ABSPATH .  'wp-admin/includes/file.php';


 $path = get_home_path()."gsc" ;

is_user_logged_in() ||  auth_redirect();

//list($basedir) = array_values(array_intersect_key(wp_upload_dir(), array('basedir' => 1)))+array(NULL);
$basedir = $path;

$file =  rtrim($basedir,'/').'/'.str_replace('..', '', isset($_GET[ 'file' ])?'formulario-clientes/'.$_GET[ 'file' ]:'');


if (!$basedir || !is_file($file)) {
    status_header(404);
   // wp_redirect(home_url());
   die('404 &#8212; File not found.'.$file);
    exit();
}


$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 );

它起作用了,它使我可以由wordpress用户访问,除了代码中显示的a且不知道为什么。

有什么主意吗?

1 个答案:

答案 0 :(得分:0)

可能的How to Protect Uploads, if User is not Logged In?重复项

TL; DR: 因此,您可以做的是重定向到php文件,并检查用户是否登录。

因此,如果要保护upload文件夹,请将其放在.htaccess中:

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

用于检查身份验证is found here的文件示例。