保护“内部文件”的策略;上载“;WordPress中的文件夹
我正在客户的网站上创建自定义giftcard PDF,希望保护它们不被公众访问,同时仍然能够在后端访问。通过Apache的.htaccess或nginx的位置阻止规则阻止特定目录可以正常工作,但现在如何在后端访问文件?Strategy#1 使用神秘的文件夹和文件名 在这种情况下,您不需要阻止通过Nginx或Apache访问PDF(除了遍历目录,但无论如何都应该打开)。PDF的路径可能如下所示,例如:保护“内部文件”的策略;上载“;WordPress中的文件夹,wordpress,wordpress-theming,Wordpress,Wordpress Theming,我正在客户的网站上创建自定义giftcard PDF,希望保护它们不被公众访问,同时仍然能够在后端访问。通过Apache的.htaccess或nginx的位置阻止规则阻止特定目录可以正常工作,但现在如何在后端访问文件?Strategy#1 使用神秘的文件夹和文件名 在这种情况下,您不需要阻止通过Nginx或Apache访问PDF(除了遍历目录,但无论如何都应该打开)。PDF的路径可能如下所示,例如: /wp content/uploads/plugin name/year-month-day/n
/wp content/uploads/plugin name/year-month-day/ndLr83nH06Vd63jnbm46Ghied7/giftcard-order-0815-item-8015.pdf
public function generatePDF(){
(....)
$upload_dir = ABSPATH . "wp-content/uploads/plugin-name/" . date( "Y-m-d" ) . "/" . $this->randomString(24);
if ( ! file_exists( $upload_dir ) ) {
mkdir( $upload_dir, 0775, true );
}
$path = $upload_dir . "/giftcard-order-$order_id-item-$item_id.pdf";
//mpdf is the library I am using to generate custom PDF's
$mpdf->Output( $path, \Mpdf\Output\Destination::FILE );
return $path;
}
public function randomString( $length = 4 ) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen( $characters );
$randomString = '';
for ( $i = 0; $i < $length; $i ++ ) {
$randomString .= $characters[ rand( 0, $charactersLength - 1 ) ];
}
return $randomString;
}
现在,您只需在服务器上设置fail2ban即可保护您的站点免受暴力攻击。或者您可以使用WordPress安全插件(WordFence、WP Cerber等)
战略#2
拒绝通过Nginx/Apache规则访问文件
此策略使阻止对要保护的文件的任何未经授权的访问变得非常简单。另一方面,现在更难编写函数,以便在需要时访问文件。使用此策略,您现在只能通过PHP代码访问文件。因此,如果您想打开或下载这些文件,您必须为此编写定制的前端应用程序,或者使用提供此类功能的外部插件
public function attachPDF( $pdf_path, $shop_coupon_id, $order_id ) {
$attachment = array(
'post_title' => "Giftcard PDF - Order #$order_id, Coupon #$shop_coupon_id",
'post_status' => 'private',
'post_parent' => $shop_coupon_id
);
$attach_id = wp_insert_attachment( $attachment, $pdf_path );
return $attach_id;
}