| |
| <?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
|
| function kic_create_uploads_htaccess_force_download() {
|
|
|
|
|
| $upload_dir = wp_upload_dir();
|
| $htaccess_file = trailingslashit( $upload_dir['basedir'] ) . '.htaccess';
|
|
|
|
|
| $needs_update = false;
|
|
|
| if ( ! file_exists( $htaccess_file ) ) {
|
| $needs_update = true;
|
| } else {
|
|
|
| $current_content = file_get_contents( $htaccess_file );
|
| if ( strpos( $current_content, '# KIC Force Download Rules' ) === false ) {
|
| $needs_update = true;
|
| }
|
| }
|
|
|
| if ( ! $needs_update ) {
|
| return;
|
| }
|
|
|
|
|
| $lines = array();
|
| $lines[] = '# KIC Force Download Rules for Text Files';
|
| $lines[] = '# Generated by KIC WPCode Snippet';
|
| $lines[] = '# DO NOT EDIT THIS SECTION MANUALLY';
|
| $lines[] = '';
|
| $lines[] = '<IfModule mod_rewrite.c>';
|
| $lines[] = 'RewriteEngine On';
|
| $lines[] = '';
|
| $lines[] = '# Force download for .txt files with ?download=1 parameter';
|
| $lines[] = 'RewriteCond %{QUERY_STRING} download=1';
|
| $lines[] = 'RewriteCond %{REQUEST_URI} \.txt$ [NC]';
|
| $lines[] = 'RewriteRule .* - [E=DOWNLOAD:1]';
|
| $lines[] = '</IfModule>';
|
| $lines[] = '';
|
| $lines[] = '<IfModule mod_headers.c>';
|
| $lines[] = '# Set Content-Disposition header to force download';
|
| $lines[] = 'Header set Content-Disposition "attachment" env=DOWNLOAD';
|
| $lines[] = '</IfModule>';
|
| $lines[] = '';
|
| $lines[] = '# END KIC Force Download Rules';
|
| $lines[] = '';
|
|
|
| $htaccess_content = implode( "\n", $lines );
|
|
|
|
|
| $existing_content = '';
|
| if ( file_exists( $htaccess_file ) ) {
|
| $existing_content = file_get_contents( $htaccess_file );
|
|
|
|
|
| $existing_content = preg_replace(
|
| '/# KIC Force Download Rules.*?# END KIC Force Download Rules\s*/s',
|
| '',
|
| $existing_content
|
| );
|
| }
|
|
|
|
|
| $final_content = $htaccess_content . "\n" . $existing_content;
|
|
|
|
|
| $result = @file_put_contents( $htaccess_file, $final_content );
|
|
|
| if ( $result === false ) {
|
|
|
| error_log( 'KIC: Failed to create/update .htaccess in uploads folder. Please create it manually.' );
|
|
|
|
|
| add_action( 'admin_notices', 'kic_htaccess_error_notice' );
|
| } else {
|
|
|
| add_action( 'admin_notices', 'kic_htaccess_success_notice' );
|
| }
|
| }
|
|
|
|
|
|
|
|
|
| function kic_htaccess_success_notice() {
|
| ?>
|
| <div class="notice notice-success is-dismissible">
|
| <p>
|
| <strong>✅ KIC Force Download:</strong>
|
| Successfully created/updated .htaccess in uploads folder!
|
| .txt files with ?download=1 will now force download.
|
| </p>
|
| </div>
|
| <?php
|
| }
|
|
|
| /**
|
| * Error notice
|
| */
|
| function kic_htaccess_error_notice() {
|
| ?>
|
| <div class="notice notice-warning is-dismissible">
|
| <p>
|
| <strong>KIC Force Download:</strong>
|
| Unable to automatically create .htaccess in uploads folder.
|
| Please create it manually or check folder permissions (should be 755).
|
| </p>
|
| </div>
|
| <?php
|
| }
|
|
|
| // Run on admin init
|
| add_action( 'admin_init', 'kic_create_uploads_htaccess_force_download', 5 );
|
| |
| |
Comments