<?php
#  ________         ____________      _____ ________                       ________      
#  ___  __ )____  _____  /___  /_____ __  /____  __ \______________ ______ ___  __/      
#  __  __  |_  / / /__  / __  / _  _ \_  __/__  /_/ /__  ___/_  __ \_  __ \__  /_        
#  _  /_/ / / /_/ / _  /  _  /  /  __// /_  _  ____/ _  /    / /_/ // /_/ /_  __/        
#  /_____/  \__,_/  /_/   /_/   \___/ \__/  /_/      /_/     \____/ \____/ /_/           
#  ________                             _____ _____              ________                
#  __  ___/_____ ___________  _____________(_)__  /______  __    ___  __ \______________ 
#  _____ \ _  _ \_  ___/_  / / /__  ___/__  / _  __/__  / / /    __  /_/ /__  ___/_  __ \
#  ____/ / /  __// /__  / /_/ / _  /    _  /  / /_  _  /_/ /     _  ____/ _  /    / /_/ /
#  /____/  \___/ \___/  \__,_/  /_/     /_/   \__/  _\__, /      /_/      /_/     \____/ 
#                                                   /____/                               
# 42756C6C657450726F6F66 5365637572697479 50726F 
#
/*	The Copyright, AITpro Software Products License Information must remain
	intact or all BulletProof Security Pro warranties, guarantees, liabilities are void.
	
	Copyright (C) Edward Alexander, AIT-pro.com. All rights reserved.

	AITpro Software Products License Information:
	BY DOWNLOADING, INSTALLING, COPYING, ACCESSING, OR USING BulletProof Security Pro YOU AGREE TO THE TERMS OF THIS AGREEMENT. 
	IF YOU 	ARE ACCEPTING THESE TERMS ON BEHALF OF ANOTHER PERSON OR A COMPANY OR OTHER LEGAL ENTITY, YOU REPRESENT AND WARRANT
	THAT YOU HAVE FULL AUTHORITY TO BIND THAT PERSON, COMPANY, OR LEGAL ENTITY TO THESE TERMS. IF YOU DO NOT AGREE TO THESE TERMS,
	* DO NOT DOWNLOAD, INSTALL, COPY, ACCESS, OR USE BulletProof Security Pro; AND
	* PROMPTLY RETURN BulletProof Security Pro TO THE PARTY FROM WHOM YOU ACQUIRED IT. IF YOU DOWNLOADED BulletProof Security Pro
	FROM THE AITPRO WEBSITE, CONTACT AITPRO FOR A REFUND IF APPLICABLE.
	
	AITpro Software Products License Information continued:
	You agree to keep the AITpro Software Products License for BulletProof Security Pro, unmodified or altered in any way,
	with the original copy of BulletProof Security Pro that you have and any and all copies or partial copies of BulletProof
	Security Pro that You make. 
*/
// Direct calls to this file are Forbidden when core files are not present 
if ( ! current_user_can('manage_options') ) { 
		header('Status: 403 Forbidden');
		header('HTTP/1.1 403 Forbidden');
		exit();
}
	
// WBM Activation: copy and rename wpadmin-secure.htaccess Master file to wp-admin folder
// Do String Replacements for Custom Code AFTER new .htaccess file has been copied to wp-admin
if ( isset( $_POST['Submit-WBM-Activate'] ) && current_user_can('manage_options') ) {
	check_admin_referer( 'bulletproof_security_wbm_activate' );
	
	$HFiles_options = get_option('bulletproof_security_options_htaccess_files');

	if ( $HFiles_options['bps_htaccess_files'] == 'disabled' ) {
		echo $bps_topDiv;
		$text = '<font color="blue"><strong>'.__('htaccess Files Disabled: wp-admin htaccess file writing is disabled. ', 'bulletproof-security').'</strong></font>'.__('Click this link for help information: ', 'bulletproof-security').'<a href="https://forum.ait-pro.com/forums/topic/htaccess-files-disabled-setup-wizard-enable-disable-htaccess-files/" target="_blank" title="htaccess Files Disabled Forum Topic">'.__('htaccess Files Disabled Forum Topic', 'bulletproof-security').'</a><br>';			
		echo $text;
    	echo $bps_bottomDiv;
		return;
	}

	$BPS_wpadmin_Options = get_option('bulletproof_security_options_htaccess_res');
	$GDMW_options = get_option('bulletproof_security_options_GDMW');	
	
	if ( $BPS_wpadmin_Options['bps_wpadmin_restriction'] == 'disabled' || $GDMW_options['bps_gdmw_hosting'] == 'yes' ) {
		echo $bps_topDiv;
		$text = '<font color="#fb0101"><strong>'.__('wp-admin Folder BulletProof Mode was not activated. Either it is disabled on the Setup Wizard Options page or you have a Go Daddy Managed WordPress Hosting account. The wp-admin folder is restricted on GDMW hosting account types.', 'bulletproof-security').'</strong></font>';
		echo $text;
   		echo $bps_bottomDiv;		
	return;
	}
	
	$options = get_option('bulletproof_security_options_customcode_WPA');  
	$HtaccessMaster = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/wpadmin-secure.htaccess';
	$wpadminHtaccess = ABSPATH . 'wp-admin/.htaccess';
	$htaccessARwpadmin = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-admin/wpadmin.htaccess';
	$permsHtaccess = @substr(sprintf('%o', fileperms($wpadminHtaccess)), -4);
	$sapi_type = php_sapi_name();	
	$bpsString1 = "# CCWTOP";
	$bpsString2 = "# CCWPF";
	$bpsString3 = '/#\sBEGIN\sBPS\sWPADMIN\sDENY\sACCESS\sTO\sFILES(.*)#\sEND\sBPS\sWPADMIN\sDENY\sACCESS\sTO\sFILES/s';
	$bpsString4 = '/#\sBEGIN\sBPSQSE-check\sBPS\sQUERY\sSTRING\sEXPLOITS\sAND\sFILTERS(.*)#\sEND\sBPSQSE-check\sBPS\sQUERY\sSTRING\sEXPLOITS\sAND\sFILTERS/s';
	$bpsReplace1 = htmlspecialchars_decode($options['bps_customcode_one_wpa'], ENT_QUOTES);
	$bpsReplace2 = htmlspecialchars_decode($options['bps_customcode_two_wpa'], ENT_QUOTES);
	$bpsReplace3 = htmlspecialchars_decode($options['bps_customcode_deny_files_wpa'], ENT_QUOTES);	
	$bpsReplace4 = htmlspecialchars_decode($options['bps_customcode_bpsqse_wpa'], ENT_QUOTES);	
	
	if ( @substr($sapi_type, 0, 6) != 'apache' || @$permsHtaccess != '0666' || @$permsHtaccess != '0777') { // Windows IIS, XAMPP, etc
		@chmod($wpadminHtaccess, 0644);
	}		

	if ( ! copy($HtaccessMaster, $wpadminHtaccess) ) {
		echo $bps_topDiv;
		$text = '<font color="#fb0101"><strong>'.__('Failed to activate wp-admin Folder BulletProof Mode protection. Your wp-admin folder is NOT protected.', 'bulletproof-security').'</strong></font>';
		echo $text;
   		echo $bps_bottomDiv;
			
	} else {
	
		if ( file_exists($wpadminHtaccess) ) {
				
			if ( @$permsHtaccess != '0666' || @$permsHtaccess != '0777' ) { // Windows IIS, XAMPP, etc
				@chmod($wpadminHtaccess, 0644);
			}				
				
			$bpsBaseContent = file_get_contents($wpadminHtaccess);
		
			if ( $options['bps_customcode_deny_files_wpa'] != '') {        
				$bpsBaseContent = preg_replace('/#\sBEGIN\sBPS\sWPADMIN\sDENY\sACCESS\sTO\sFILES(.*)#\sEND\sBPS\sWPADMIN\sDENY\sACCESS\sTO\sFILES/s', $bpsReplace3, $bpsBaseContent);
			}
			
			if ( $options['bps_customcode_bpsqse_wpa'] != '') {        
				$bpsBaseContent = preg_replace('/#\sBEGIN\sBPSQSE-check\sBPS\sQUERY\sSTRING\sEXPLOITS\sAND\sFILTERS(.*)#\sEND\sBPSQSE-check\sBPS\sQUERY\sSTRING\sEXPLOITS\sAND\sFILTERS/s', $bpsReplace4, $bpsBaseContent);
			}
				
			$bpsBaseContent = str_replace($bpsString1, $bpsReplace1, $bpsBaseContent);
			$bpsBaseContent = str_replace($bpsString2, $bpsReplace2, $bpsBaseContent);
				
			if ( file_put_contents( $wpadminHtaccess, $bpsBaseContent ) ) {
				copy($wpadminHtaccess, $htaccessARwpadmin);

				echo $bps_topDiv;
				$text = '<font color="green"><strong>'.__('wp-admin Folder BulletProof Mode protection activated successfully.', 'bulletproof-security').'</strong></font>';
				echo $text;
				echo $bps_bottomDiv;
			}
		}
	}
}

// WBM Deactivation: delete the wp-admin folder htaccess file
if ( isset( $_POST['Submit-WBM-Deactivate'] ) && current_user_can('manage_options') ) {
	check_admin_referer( 'bulletproof_security_wbm_deactivate' );
	
	$HFiles_options = get_option('bulletproof_security_options_htaccess_files');

	if ( $HFiles_options['bps_htaccess_files'] == 'disabled' ) {
		echo $bps_topDiv;
		$text = '<font color="blue"><strong>'.__('htaccess Files Disabled: wp-admin htaccess file writing is disabled. ', 'bulletproof-security').'</strong></font>'.__('Click this link for help information: ', 'bulletproof-security').'<a href="https://forum.ait-pro.com/forums/topic/htaccess-files-disabled-setup-wizard-enable-disable-htaccess-files/" target="_blank" title="htaccess Files Disabled Forum Topic">'.__('htaccess Files Disabled Forum Topic', 'bulletproof-security').'</a><br>';
		echo $text;
    	echo $bps_bottomDiv;
		return;
	}

	$BPS_wpadmin_Options = get_option('bulletproof_security_options_htaccess_res');
	$GDMW_options = get_option('bulletproof_security_options_GDMW');	
	
	if ( $BPS_wpadmin_Options['bps_wpadmin_restriction'] == 'disabled' || $GDMW_options['bps_gdmw_hosting'] == 'yes' ) {
		echo $bps_topDiv;
		$text = '<font color="#fb0101"><strong>'.__('wp-admin Folder BulletProof Mode was not activated. Either it is disabled on the Setup Wizard Options page or you have a Go Daddy Managed WordPress Hosting account. The wp-admin folder is restricted on GDMW hosting account types.', 'bulletproof-security').'</strong></font>';
		echo $text;
   		echo $bps_bottomDiv;		
		return;
	}
	
	$wpadminHtaccess = ABSPATH . 'wp-admin/.htaccess';
	$htaccessARwpadmin = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-admin/wpadmin.htaccess';
	
	@unlink($htaccessARwpadmin);
	@unlink($wpadminHtaccess);
	
	if ( file_exists($wpadminHtaccess) ) {
		
		echo $bps_topDiv;
		$text = '<font color="#fb0101"><strong>'.__('Failed to deactivate wp-admin Folder BulletProof Mode. The wp-admin htaccess file does not exist. It may have been deleted or renamed already.', 'bulletproof-security').'</strong></font>';
		echo $text;
   		echo $bps_bottomDiv;
	
	} else {
		
		echo $bps_topDiv;
		$text = '<font color="green"><strong>'.__('wp-admin Folder BulletProof Mode deactivated successfully. The wp-admin htaccess file has been deleted.', 'bulletproof-security').'</strong></font><br>';
		echo $text;
		echo $bps_bottomDiv;
	}
}

// Plugin Firewall: Update Whitelist DB options & Create Firewall Master File Form
// BPS Pro 11: now using mod_rewrite and not mod_access_compat and mod_authz_core 
// 11.5 Change|BugFix: Whitelist by Hostname (domain name) and IP Address tool - previously called Allow from tool.
// 11.8: htaccess Files Disabled condition.
// 11.9: combined whitelist db options save into this form.
if ( isset( $_POST['Submit-PFW-Whitelist'] ) && current_user_can('manage_options') ) {
	check_admin_referer( 'bulletproof_security_pfw_whitelist' );

	$HFiles_options = get_option('bulletproof_security_options_htaccess_files');

	if ( $HFiles_options['bps_htaccess_files'] == 'disabled' ) {
		echo $bps_topDiv;
		$text = '<font color="blue"><strong>'.__('htaccess Files Disabled: Plugin Firewall htaccess file writing is disabled. ', 'bulletproof-security').'</strong></font>'.__('Click this link for help information: ', 'bulletproof-security').'<a href="https://forum.ait-pro.com/forums/topic/htaccess-files-disabled-setup-wizard-enable-disable-htaccess-files/" target="_blank" title="htaccess Files Disabled Forum Topic">'.__('htaccess Files Disabled Forum Topic', 'bulletproof-security').'</a><br>';
		echo $text;
    	echo $bps_bottomDiv;
		return;
	}
	
	$bps_pfw_paypal = ! empty($_POST['bps_pfw_paypal']) ? '1' : '';
	$bps_pfw_google = ! empty($_POST['bps_pfw_google']) ? '1' : '';
	$bps_pfw_amazon = ! empty($_POST['bps_pfw_amazon']) ? '1' : '';
	$bps_pfw_authorizenet = ! empty($_POST['bps_pfw_authorizenet']) ? '1' : '';
	$bps_pfw_whitelist = ! empty($_POST['bps_pfw_whitelist']) ? esc_html($_POST['bps_pfw_whitelist']) : '';	
	
	$PFW_Options = array( 
	'bps_pfw_paypal' 		=> $bps_pfw_paypal, 
	'bps_pfw_google' 		=> $bps_pfw_google, 
	'bps_pfw_amazon' 		=> $bps_pfw_amazon, 
	'bps_pfw_authorizenet'	=> $bps_pfw_authorizenet, 
	'bps_pfw_whitelist' 	=> $bps_pfw_whitelist 
	);
	
	foreach( $PFW_Options as $key => $value ) {
		update_option('bulletproof_security_options_pfirewall', $PFW_Options);
	}

	$options = get_option('bulletproof_security_options_pfirewall');
	$PluginsHtaccessMaster = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/plugins.htaccess';
	$PluginsHtaccessARQ = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/plugins/bulletproof-security/admin/htaccess/plugins.htaccess';
	$PluginsHtaccessARQDir = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/plugins/bulletproof-security/admin/htaccess/';
	$pluginsHtaccessMasterTXT = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/plugins-htaccess-master.txt';
	$pluginsHtaccessMasterTXTARQ = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/plugins/bulletproof-security/admin/htaccess/plugins-htaccess-master.txt';
	$bps_get_public_ip = bpsPro_get_proxy_real_ip_address();

	if ( ! is_writable($pluginsHtaccessMasterTXT) || ! is_writable($PluginsHtaccessMaster) ) {
		echo $bps_topDiv;
		$text = '<strong><font color="#fb0101">'.__('Error: Failed to Create the Plugin Firewall Master File.', 'bulletproof-security').'</font><br>'.__('Check the file and folder permissions for these files.', 'bulletproof-security').'</strong><br>'.$pluginsHtaccessMasterTXT.'<br>'.$PluginsHtaccessMaster; 
		echo $text; 
		echo $bps_bottomDiv;
	} else {
		echo $bps_topDiv;
		$text = '<font color="blue"><strong>'.__('Reminder: After clicking Save Whitelist Options button, click the Activate button to activate the Plugin Firewall with your new settings/rules/options.', 'bulletproof-security').'</strong></font>';
		echo $text;
		echo $bps_bottomDiv;			
	}

	if ( file_exists($PluginsHtaccessMaster) && is_writable($pluginsHtaccessMasterTXT) && is_writable($PluginsHtaccessMaster) ) {

		$bps_js_scripts = array( "/bulletproof-security/admin/js/bulletproof-security-admin-5.js, ", "/bulletproof-security/admin/js/bulletproof-security-admin-4.js, ","/bulletproof-security/admin/js/bulletproof-security-admin-3.js, ", "/bulletproof-security/admin/js/bulletproof-security-admin-2.js, ", "/bulletproof-security/admin/js/bulletproof-security-admin.js, ", "/bulletproof-security/admin/js/bps-tabs.js, ", "/bulletproof-security/admin/js/bps-accordion.js, ", "/bulletproof-security/admin/js/bps-dialog.js, " );		
		
		$bps_pfw_whitelist = array_filter( explode( ', ', str_replace( $bps_js_scripts, "", trim( $options['bps_pfw_whitelist'], ", \t\n\r") ) ) );
	
		if ( empty($bps_pfw_whitelist) ) {
			
			file_put_contents($pluginsHtaccessMasterTXT, "");
		
		} else {
		
			$whiteList = array();
		
			foreach ( $bps_pfw_whitelist as $Key => $Value ) {
				
				$Value = preg_replace( '/\//', "^", $Value, 1 );
				$whiteList[] = 'RewriteRule ' . $Value . ' - [L]' . "\n";

				file_put_contents($pluginsHtaccessMasterTXT, $whiteList);
			}
		}
		
		if ( $options['bps_pfw_paypal'] == '1' ) {
			$payPal = 'RewriteCond %{HTTP_HOST} !^paypal.com$' . "\n";
		} else {
			$payPal = '';
		}
	
		if ( $options['bps_pfw_google'] == '1' ) {
			$google = 'RewriteCond %{HTTP_HOST} !^google.com$' . "\n";
		} else {
			$google = '';
		}

		if ( $options['bps_pfw_amazon'] == '1' ) {
			$amazon = 'RewriteCond %{HTTP_HOST} !^amazon.com$' . "\n";
		} else {
			$amazon = '';
		}

		if ( $options['bps_pfw_authorizenet'] == '1' ) {
			$authorizeNet = 'RewriteCond %{HTTP_HOST} !^authorize.net$' . "\n";
		} else {
			$authorizeNet = '';
		}

		$whiteListMaster = file_get_contents($pluginsHtaccessMasterTXT);
		$stringReplace = file_get_contents($PluginsHtaccessMaster);
		
		$stringReplace = preg_replace('/BEGIN\sWHITELIST(.*)#\sEND\sPUBLIC\sIP/s', "BEGIN WHITELIST: Frontend Loading Website Plugin scripts/files\nRewriteRule ^bulletproof-security/400.php - [L]\nRewriteRule ^bulletproof-security/403.php - [L]\nRewriteRule ^bulletproof-security/405.php - [L]\nRewriteRule ^bulletproof-security/410.php - [L]\nRewriteRule ^bulletproof-security/isl-logout.php - [L]\n" . $whiteListMaster . "# END WHITELIST\n#\n# FORBID REMOTE ACCESS TO THESE PLUGIN FILE TYPES FROM ANYONE EXCEPT YOU\nRewriteCond %{REQUEST_URI} ^.*\.(7z|as|bat|bin|cgi|chm|chml|class|cmd|com|command|dat|db|db2|db3|dba|dll|DS_Store|exe|gz|hta|htaccess|htc|htm|html|html5|htx|ico|idc|ini|ins|isp|jar|jav|java|js|jse|jsfl|json|jsp|jsx|lib|lnk|out|php|phps|php5|php4|php3|phtml|phpt|pl|py|pyd|pyc|pyo|rar|shtm|shtml|sql|swf|sys|tar|taz|tgz|tpl|txt|vb|vbe|vbs|war|ws|wsf|xhtml|z|zip)\$ [NC]\n$payPal$google$amazon$authorizeNet# BEGIN PUBLIC IP\nRewriteCond %{REMOTE_ADDR} !^" . $bps_get_public_ip . "\$\n# END PUBLIC IP", $stringReplace);

		file_put_contents($PluginsHtaccessMaster, $stringReplace);
		
		/** Whitelist by Hostname (domain name) and IP Address tool. Previously called: Allow from Whitelist Rules **/
		$Allow_options = get_option('bulletproof_security_options_pfirewall_allow');
		$pluginsAllowFromTXT = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/plugins-allow-from.txt';
		$pluginsAllowFromTXTARQ = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/plugins/bulletproof-security/admin/htaccess/plugins-allow-from.txt';

		$pfw_allow_from = array_filter( explode( ', ', str_replace( array( "Allow from ", "allow from " ), "", trim( $Allow_options['bps_pfw_allow_from'], ", \t\n\r") ) ) );
		
		if ( empty($pfw_allow_from) ) {
			
			file_put_contents($pluginsAllowFromTXT, "");
		
		} else {
	
			$allow_whiteList = array();
			
			foreach ( $pfw_allow_from as $allow_Key => $allow_Value ) {
				
				if ( preg_match( '/[a-zA-Z]/', $allow_Value, $matches ) && ! preg_match( '/[:]/', $allow_Value, $matches ) ) {	
				
					$allow_whiteList[] = 'RewriteCond %{HTTP_HOST} !^' . $allow_Value . '$' . "\n";
				
				} else {
					
					$allow_whiteList[] .= 'RewriteCond %{REMOTE_ADDR} !^' . $allow_Value . '$' . "\n";

				}
				
				file_put_contents($pluginsAllowFromTXT, $allow_whiteList);
			}
		}
			
		// This is for DB value cleanup. The cleanup above is for file rule writing cleanup.
		$rule_cleanup = str_replace( array( "Allow from ", "allow from " ), "", trim( $Allow_options['bps_pfw_allow_from'], ", \t\n\r") );
				
			$Allow_Cleanup = array( 'bps_pfw_allow_from' => $rule_cleanup );

			foreach( $Allow_Cleanup as $key => $value ) {
				update_option('bulletproof_security_options_pfirewall_allow', $Allow_Cleanup);
			}

		// Get new updated DB option values again here
		$Allow_options = get_option('bulletproof_security_options_pfirewall_allow');

		$AllowFromRules = file_get_contents($pluginsAllowFromTXT);
		$stringReplace = file_get_contents($PluginsHtaccessMaster);
					
		if ( $Allow_options['bps_pfw_allow_from'] == '' ) {
			
			$stringReplace = preg_replace( '/(#\sBEGIN\sADDITIONAL\sALLOW\sFROM\sRULES(.*\s*)([a-zA-Z0-9].*\s*){1,}#\sEND\sADDITIONAL\sALLOW\sFROM\sRULES(.*\s*)){1,}/', "", $stringReplace );			

		} else {

			if ( preg_match( '/(#\sBEGIN\sADDITIONAL\sALLOW\sFROM\sRULES(.*\s*)([a-zA-Z0-9].*\s*){1,}#\sEND\sADDITIONAL\sALLOW\sFROM\sRULES(.*\s*)){1,}/', $stringReplace, $matches) ) {
				// 11.8: Remove duplicate blocks of Allow From Rules code. Matches 1 or multiple blocks of code. newline at END...RULES is critical.
				$stringReplace = preg_replace( '/(#\sBEGIN\sADDITIONAL\sALLOW\sFROM\sRULES(.*\s*)([a-zA-Z0-9].*\s*){1,}#\sEND\sADDITIONAL\sALLOW\sFROM\sRULES(.*\s*)){1,}/', "# BEGIN ADDITIONAL ALLOW FROM RULES\n" . $AllowFromRules . "# END ADDITIONAL ALLOW FROM RULES\n", $stringReplace );

			} else {
			
				$stringReplace = preg_replace( '/#\sEND\sPUBLIC\sIP/', "# END PUBLIC IP\n# BEGIN ADDITIONAL ALLOW FROM RULES\n" . $AllowFromRules . "# END ADDITIONAL ALLOW FROM RULES\n", $stringReplace );
				
			}
		}

		// Clean up - replace multiple newlines with 1 newline
		if ( preg_match('/(\n\n|\n\n\n|\n\n\n\n)/', $stringReplace, $matches) ) {			
			$stringReplace = preg_replace("/(\n\n|\n\n\n|\n\n\n\n)/", "\n", $stringReplace);
		}

		// Clean up - remove extra whitespaces before last RewriteRule
		if ( preg_match( '/\s{1,}RewriteRule\s\^\(\.\*\)\$\s\-\s\[F\]/', $stringReplace, $matches ) ) {
			$stringReplace = preg_replace('/\s{1,}RewriteRule\s\^\(\.\*\)\$\s\-\s\[F\]/s', "\nRewriteRule ^(.*)$ - [F]", $stringReplace);
		}
			
		file_put_contents($PluginsHtaccessMaster, $stringReplace);
		
		// Note: Copy the Master and text files to ARQ - PFW Activation copies the Master file to Live /plugins/ and ARQ
		if ( is_dir($PluginsHtaccessARQDir) ) {
			@copy($PluginsHtaccessMaster, $PluginsHtaccessARQ);
			@copy($pluginsHtaccessMasterTXT, $pluginsHtaccessMasterTXTARQ);
			@copy($pluginsAllowFromTXT, $pluginsAllowFromTXTARQ);
		}
	}
}

// Plugin Firewall: Save Plugin Firewall AutoPilot Form Options
if ( isset( $_POST['Submit-PFW-AutoPilot'] ) && current_user_can('manage_options') ) {
	check_admin_referer('bulletproof_security_pfw_autopilot');

	$autopilot_cron_frequency = $_POST['autopilot_cron_frequency'];
	
	if ( $autopilot_cron_frequency == '1' ) {
		$autopilot_cron_end = time() + 60;	
	}
	if ( $autopilot_cron_frequency == '5' ) {
		$autopilot_cron_end = time() + 300;	
	}	
	if ( $autopilot_cron_frequency == '10' ) {
		$autopilot_cron_end = time() + 600;	
	}
	if ( $autopilot_cron_frequency == '15' ) {
		$autopilot_cron_end = time() + 900;	
	}	
	if ( $autopilot_cron_frequency == '30' ) {
		$autopilot_cron_end = time() + 1800;	
	}	
	if ( $autopilot_cron_frequency == '60' ) {
		$autopilot_cron_end = time() + 3600;	
	}

	$PFWAP_Options = array( 
	'bps_pfw_autopilot_cron' 			=> $_POST['autopilot_on_off'], 
	'bps_pfw_autopilot_cron_frequency' 	=> $autopilot_cron_frequency, 
	'bps_pfw_autopilot_cron_end' 		=> $autopilot_cron_end
	);
	
	foreach( $PFWAP_Options as $key => $value ) {
		update_option('bulletproof_security_options_pfw_autopilot', $PFWAP_Options);
	}

	$BPS_pfw_new_install = array(
	'bps_pfw_new_install_start' 	=> time(), 
	'bps_pfw_new_install_end' 		=> time(), 
	'bps_pfw_new_install_completed' => 'Yes'
	);

	foreach( $BPS_pfw_new_install as $key => $value ) {
		update_option('bulletproof_security_options_pfw_new_install', $BPS_pfw_new_install);
	}

	if ( $_POST['autopilot_on_off'] == 'On' ) {
		echo $bps_topDiv;
		echo '<strong><font color="green">'.__('Plugin Firewall AutoPilot Mode has been turned On. The BPS Pro Dashboard Status Display will display: PFW: AutoPilot : 00 Min : 00:00 AM when AutoPilot Mode is turned On', 'bulletproof-security').'</font></strong><br>';
		echo $bps_bottomDiv;
	}

	if ( $_POST['autopilot_on_off'] == 'Off' ) {
		echo $bps_topDiv;
		echo '<strong><font color="green">'.__('Plugin Firewall AutoPilot Mode has been turned Off. The BPS Pro Dashboard Status Display will display: PFW: On if the Plugin Firewall is Turned On.', 'bulletproof-security').'</font></strong><br>';
		echo $bps_bottomDiv;
	}
}

// Plugin Firewall: Save Plugin Firewall Whitelist by Hostname (domain name) and IP Address Form Options
if ( isset( $_POST['Submit-PFW-Allow-From'] ) && current_user_can('manage_options') ) {
	check_admin_referer('bulletproof_security_pfw_allow_from');

	$PFW_Allow_Options = array( 'bps_pfw_allow_from' => stripslashes($_POST['bps_pfw_allow_from']) );
	
	foreach( $PFW_Allow_Options as $key => $value ) {
		update_option('bulletproof_security_options_pfirewall_allow', $PFW_Allow_Options);
	}	

	$PFW_allow_from_options = get_option('bulletproof_security_options_pfirewall_allow');
	
	echo $bps_topDiv;
	$text = '<strong><font color="green">'.__('Whitelist by Hostname (domain name) and IP Address settings saved successfully.', 'bulletproof-security').'<br>'.__('Current Whitelist by Hostname (domain name) and IP Address Rules: ', 'bulletproof-security').'</font><font color="black">'.$PFW_allow_from_options['bps_pfw_allow_from'].'</font><br><font color="blue">'.__('Reminder: After clicking the Save Hostname and IP Address Rules button, click the Activate button to activate the Plugin Firewall.', 'bulletproof-security').'</font></strong>';
	echo $text;
	echo $bps_bottomDiv;
}

// Plugin Firewall: Save Additional Roles IP Whitelist Form Options
// 12.5: Added Custom Roles
// 12.5.1: BugFix: save custom roles as an array instead of blank value
if ( isset( $_POST['Submit-PFW-Roles'] ) && current_user_can('manage_options') ) {
	check_admin_referer('bulletproof_security_pfw_roles');

	$Custom_Roles = empty($_POST['bps_pfw_custom_roles']) ? array( 'bps', '' ) : $_POST['bps_pfw_custom_roles'];
	
	switch( $_POST['Submit-PFW-Roles'] ) {
		case __('Save Additional Roles Options', 'bulletproof-security'):
		
		if ( ! empty($Custom_Roles) ) {
			
			$Custom_Roles_array = array();
			
			foreach ( $Custom_Roles as $key => $value ) {
				
				if ( $value == '1' ) {
					$Custom_Roles_array[$key] = $value;
				} 
			}
		
		} else {
			
			$Custom_Roles_array = array( 'bps', '' );
		}
	}

	$bps_pfw_administrator = ! empty($_POST['bps_pfw_administrator']) ? '1' : '';
	$bps_pfw_editor = ! empty($_POST['bps_pfw_editor']) ? '1' : '';
	$bps_pfw_author = ! empty($_POST['bps_pfw_author']) ? '1' : '';
	$bps_pfw_contributor = ! empty($_POST['bps_pfw_contributor']) ? '1' : '';

	$PFW_Roles = array( 
	'bps_pfw_administrator' => esc_html($bps_pfw_administrator), 
	'bps_pfw_editor' 		=> esc_html($bps_pfw_editor), 	
	'bps_pfw_author' 		=> esc_html($bps_pfw_author), 	
	'bps_pfw_contributor' 	=> esc_html($bps_pfw_contributor), 
	'bps_pfw_roles_hidden' 	=> esc_html($_POST['bps_pfw_roles_hidden']), 
	'bps_pfw_custom_roles' 	=> $Custom_Roles_array  
	);
	
	foreach( $PFW_Roles as $key => $value ) {
		update_option('bulletproof_security_options_pfirewall_roles', $PFW_Roles);
	}	

	echo $bps_topDiv;
	$text = '<strong><font color="green">'.__('Additional Roles IP Whitelist settings saved successfully.', 'bulletproof-security').'</font></strong>';
	echo $text;
	echo $bps_bottomDiv;
}

// Plugin Firewall Activation: copy plugins.htaccess Master file to plugins folder
// 11.8: New check and message for AutoPilot Mode On|Off when PFW is Activated.
// 14.1: New DB option created for PFW On|Off.
if ( isset( $_POST['Submit-PFW-Activate'] ) && current_user_can('manage_options') ) {
	check_admin_referer( 'bulletproof_security_pfw_activate' );
	
	$HFiles_options = get_option('bulletproof_security_options_htaccess_files');

	if ( $HFiles_options['bps_htaccess_files'] == 'disabled' ) {
		echo $bps_topDiv;
		$text = '<font color="blue"><strong>'.__('htaccess Files Disabled: Plugin Firewall htaccess file writing is disabled. ', 'bulletproof-security').'</strong></font>'.__('Click this link for help information: ', 'bulletproof-security').'<a href="https://forum.ait-pro.com/forums/topic/htaccess-files-disabled-setup-wizard-enable-disable-htaccess-files/" target="_blank" title="htaccess Files Disabled Forum Topic">'.__('htaccess Files Disabled Forum Topic', 'bulletproof-security').'</a><br>';
		echo $text;
    	echo $bps_bottomDiv;
		return;
	}

	$PluginsHtaccessMaster = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/plugins.htaccess';
	$PluginsHtaccess = WP_PLUGIN_DIR . '/.htaccess';
	$PluginsHtaccessARQplugins = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/plugins/.htaccess';
	$PluginsHtaccessARQ = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/plugins/bulletproof-security/admin/htaccess/plugins.htaccess';
	$PFWAP_options = get_option('bulletproof_security_options_pfw_autopilot');
	$PFW_OnOff = get_option('bulletproof_security_options_pfw');

	$BPS_PFW_OnOff = array( 'bps_pfw_on_off' => 'On' );		
		
	foreach( $BPS_PFW_OnOff as $key => $value ) {
		update_option('bulletproof_security_options_pfw', $BPS_PFW_OnOff);
	}

	$BPS_PFWTM_Options = array( 'bps_pfw_test_mode' => 'Off' );		
			
	foreach( $BPS_PFWTM_Options as $key => $value ) {
		update_option('bulletproof_security_options_pfw_test_mode', $BPS_PFWTM_Options);
	}

	if ( file_exists($PluginsHtaccessMaster) ) {

		if ( ! copy($PluginsHtaccessMaster, $PluginsHtaccess) ) {
			echo $bps_topDiv;
			$text = '<font color="#fb0101"><strong>'.__('Error: Failed to activate Plugin Firewall BulletProof Mode. Unable to Copy and Rename the plugins.htaccess file to /plugins/.htaccess', 'bulletproof-security').'</strong></font>';
			echo $text;
			echo $bps_bottomDiv;
			
		} else {
			
			@copy($PluginsHtaccess, $PluginsHtaccessARQ);
			@copy($PluginsHtaccess, $PluginsHtaccessARQplugins);

			if ( $PFWAP_options['bps_pfw_autopilot_cron'] == 'On' ) {
				echo $bps_topDiv;
				$text = '<font color="green"><strong>'.__('Plugin Firewall BulletProof Mode plugins folder protection activated. Your /', 'bulletproof-security').$bps_plugin_dir.__(' folder is now protected.', 'bulletproof-security').'</strong></font>';
				echo $text;
				echo $bps_bottomDiv;
			}

			if ( $PFWAP_options['bps_pfw_autopilot_cron'] == 'Off' ) {

				echo $bps_topDiv;
				$text = '<strong><font color="green">'.__('Plugin Firewall BulletProof Mode plugins folder protection activated. Your /', 'bulletproof-security').$bps_plugin_dir.__(' folder is now protected.', 'bulletproof-security').'</font><br><br><font color="blue">'.__('Important Note: ', 'bulletproof-security').'</font>'.__('Plugin Firewall AutoPilot Mode is not turned On. It is recommended that you turn On AutoPilot Mode so that Plugin Firewall whitelist rules will be automatically created for your website.', 'bulletproof-security').'</strong>';
				echo $text;
				echo $bps_bottomDiv;
			}
		}
	}
}

// Plugin Firewall Deactivation: delete /plugins/.htaccess file
// 11.8: Deactivating PFW unschedules the PFWAP cron and turns off AutoPilot Mode.
if ( isset( $_POST['Submit-PFW-Deactivate'] ) && current_user_can('manage_options') ) {
	check_admin_referer( 'bulletproof_security_pfw_deactivate' );
	
	$HFiles_options = get_option('bulletproof_security_options_htaccess_files');

	if ( $HFiles_options['bps_htaccess_files'] == 'disabled' ) {
		echo $bps_topDiv;
		$text = '<font color="blue"><strong>'.__('htaccess Files Disabled: Plugin Firewall htaccess file writing is disabled. ', 'bulletproof-security').'</strong></font>'.__('Click this link for help information: ', 'bulletproof-security').'<a href="https://forum.ait-pro.com/forums/topic/htaccess-files-disabled-setup-wizard-enable-disable-htaccess-files/" target="_blank" title="htaccess Files Disabled Forum Topic">'.__('htaccess Files Disabled Forum Topic', 'bulletproof-security').'</a><br>';
		echo $text;
    	echo $bps_bottomDiv;
		return;
	}

	$PluginsHtaccess = WP_PLUGIN_DIR . '/.htaccess';
	$PluginsHtaccessARQplugins = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/plugins/.htaccess';
	//$PFWAP_options = get_option('bulletproof_security_options_pfw_autopilot');
	$PFW_OnOff = get_option('bulletproof_security_options_pfw');

	if ( file_exists($PluginsHtaccess) ) {
		@unlink($PluginsHtaccess);
		@unlink($PluginsHtaccessARQplugins);

		wp_clear_scheduled_hook('bpsPro_PFWAP_check');

		/*
		$BPS_PFWAP_Options = array( 
		'bps_pfw_autopilot_cron' 			=> $PFWAP_options['bps_pfw_autopilot_cron'],  // was set to Off - not smart
		'bps_pfw_autopilot_cron_frequency' 	=> $PFWAP_options['bps_pfw_autopilot_cron_frequency'],  	
		'bps_pfw_autopilot_cron_end' 		=> $PFWAP_options['bps_pfw_autopilot_cron_end'] 
		);		
			
		foreach( $BPS_PFWAP_Options as $key => $value ) {
			update_option('bulletproof_security_options_pfw_autopilot', $BPS_PFWAP_Options);
		}
		*/
	
		$BPS_PFW_OnOff = array( 'bps_pfw_on_off' => 'Off' );		
			
		foreach( $BPS_PFW_OnOff as $key => $value ) {
			update_option('bulletproof_security_options_pfw', $BPS_PFW_OnOff);
		}	
	}
		
	$BPS_PFWTM_Options = array( 'bps_pfw_test_mode' => 'Off' );		
			
	foreach( $BPS_PFWTM_Options as $key => $value ) {
		update_option('bulletproof_security_options_pfw_test_mode', $BPS_PFWTM_Options);
	}

	if ( file_exists($PluginsHtaccess) ) {
		echo $bps_topDiv;
		$text = '<font color="#fb0101"><strong>'.__('Failed to delete the /plugins/.htaccess file. The file does not exist. It may have been deleted already.', 'bulletproof-security').'</strong></font>';
		echo $text;
   		echo $bps_bottomDiv;

	} else {
		
		echo $bps_topDiv;
		$text = '<font color="green"><strong>'.__('Plugin Firewall BulletProof Mode has been deactivated and AutoPilot Mode has been turned Off. If you are testing or troubleshooting then be sure to activate Plugin Firewall BulletProof Mode and turn AutoPilot Mode back On when you are done testing or troubleshooting.', 'bulletproof-security').'</strong></font><br><font color="#fb0101"><strong> '.__('Your WordPress /plugins folder is no longer protected.', 'bulletproof-security').'</strong></font>';
		echo $text;
		echo $bps_bottomDiv;
	}
}

// Plugin Firewall Test Mode: Set the IP address to: 127.0.0.9 in the Plugin Firewall htaccess file
if ( isset( $_POST['Submit-PFW-Test-Mode'] ) && current_user_can('manage_options') ) {
	check_admin_referer( 'bulletproof_security_pfw_test_mode' );
	
	$HFiles_options = get_option('bulletproof_security_options_htaccess_files');

	if ( $HFiles_options['bps_htaccess_files'] == 'disabled' ) {
		echo $bps_topDiv;
		$text = '<font color="blue"><strong>'.__('htaccess Files Disabled: Plugin Firewall htaccess file writing and testing is disabled. ', 'bulletproof-security').'</strong></font>'.__('Click this link for help information: ', 'bulletproof-security').'<a href="https://forum.ait-pro.com/forums/topic/htaccess-files-disabled-setup-wizard-enable-disable-htaccess-files/" target="_blank" title="htaccess Files Disabled Forum Topic">'.__('htaccess Files Disabled Forum Topic', 'bulletproof-security').'</a><br>';
		echo $text;
    	echo $bps_bottomDiv;
		return;
	}

	$PluginsHtaccess = WP_PLUGIN_DIR . '/.htaccess';
	$PluginsHtaccessMaster = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/plugins.htaccess';
	$PluginsHtaccessARQplugins = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/plugins/.htaccess';

	if ( ! file_exists($PluginsHtaccess) ) {
		copy($PluginsHtaccessMaster, $PluginsHtaccess);
	}

	$stringReplace = @file_get_contents($PluginsHtaccess);
	$pattern1 = '/#\sBEGIN\sPUBLIC\sIP(.*\s*)([a-zA-Z0-9].*\s*){1,}#\sEND\sPUBLIC\sIP/';
	$pattern2 = '/#\sBEGIN\sADDITIONAL\sALLOW\sFROM\sRULES(.*\s*)([a-zA-Z0-9].*\s*){1,}#\sEND\sADDITIONAL\sALLOW\sFROM\sRULES/';
	$pattern3 = '/#\sBEGIN\sADDITIONAL\sROLES\sIP(.*\s*)([a-zA-Z0-9].*\s*){1,}#\sEND\sADDITIONAL\sROLES\sIP/';	

	if ( preg_match( $pattern1, $stringReplace, $matches ) ) {
		$stringReplace = preg_replace( $pattern1, "# BEGIN PUBLIC IP\nRewriteCond %{REMOTE_ADDR} !^127.0.0.9$\n# END PUBLIC IP", $stringReplace );
	}
	
	if ( preg_match( $pattern2, $stringReplace, $matches ) ) {
		$stringReplace = preg_replace( $pattern2, "# BEGIN ADDITIONAL ALLOW FROM RULES\nRewriteCond %{REMOTE_ADDR} !^127.0.0.9$\n# END ADDITIONAL ALLOW FROM RULES", $stringReplace );
	}

	if ( preg_match( $pattern3, $stringReplace, $matches ) ) {
		$stringReplace = preg_replace( $pattern3, "# BEGIN ADDITIONAL ROLES IP\nRewriteCond %{REMOTE_ADDR} !^127.0.0.9$\n# END ADDITIONAL ROLES IP", $stringReplace );
	}

	if ( ! file_put_contents($PluginsHtaccess, $stringReplace ) ) {
		echo $bps_topDiv;
		$text = '<font color="#fb0101"><strong>'.__('ERROR: Plugin Firewall Test Mode was not activated. Unable to write to the /plugins/.htaccess file, which is either caused by a file or folder permission problem with the /plugins/ folder or the /plugins/.htaccess file.', 'bulletproof-security').'</strong></font><br>';			
		echo $text;
    	echo $bps_bottomDiv;
		return;

	} else {
		
		copy($PluginsHtaccess, $PluginsHtaccessARQplugins);

		echo $bps_topDiv;
		$text = '<strong><font color="green">'.__('Plugin Firewall Test Mode activated successfully. Test Mode changes your current whitelisted Plugin Firewall IP address, which simulates that you are a visitor to your website. Test Mode is used to check the frontend of your website for any problems or errors as if you are a visitor to your website. Note: Click the Plugin Firewall BulletProof Mode (PFW) Read Me help button for help information about how to use Test Mode.', 'bulletproof-security').'</font><br><br><font color="blue">'.__('Important Notes: ', 'bulletproof-security').'</font>'.__('Test Mode sets the Plugin Firewall AutoPilot Mode Cron frequency to 1 minute. If you do see a problem or error on the frontend of your website, wait 1 minute and check the problem or error again. AutoPilot Mode/Test Mode is designed to automatically create new Plugin Firewall whitelist rules for any problems or errors on the frontend of your website when the next Plugin Firewall AutoPilot Mode Cron job runs (once every 1 minute in Test Mode). After you are done using Test Mode, you can either leave the AutoPilot Mode Cron Check Frequency setting to 1 minute or you can change the Cron Check Frequency to a different Cron check frequency setting. Clicking the Plugin Firewall Activate or Deactivate button turns Off Plugin Firewall Test Mode.', 'bulletproof-security').'</strong>';
		echo $text;
		echo $bps_bottomDiv;		
	}

	$BPS_PFWAP_Options = array( 
	'bps_pfw_autopilot_cron' 			=> 'On',  
	'bps_pfw_autopilot_cron_frequency' 	=> '1',  	
	'bps_pfw_autopilot_cron_end' 		=> time() + 60 
	);		
			
	foreach( $BPS_PFWAP_Options as $key => $value ) {
		update_option('bulletproof_security_options_pfw_autopilot', $BPS_PFWAP_Options);
	}

	$BPS_PFWTM_Options = array( 'bps_pfw_test_mode' => 'On'	);		
			
	foreach( $BPS_PFWTM_Options as $key => $value ) {
		update_option('bulletproof_security_options_pfw_test_mode', $BPS_PFWTM_Options);
	}
}

// UAEG Activation:
// Rename UAEG Master .htaccess file to prevent automatic creation of UAEG .htaccess file
// 11.2: UAEG Custom Code added
// 11.6: New sapi check for LiteSpeed to create the new LiteSpeed mod_rewrite UAEG htaccess file version.
if ( isset( $_POST['Submit-UAEG-Activate'] ) && current_user_can('manage_options') ) {
	check_admin_referer( 'bulletproof_security_uaeg_activate' );

	$HFiles_options = get_option('bulletproof_security_options_htaccess_files');

	if ( $HFiles_options['bps_htaccess_files'] == 'disabled' ) {
		echo $bps_topDiv;
		$text = '<font color="blue"><strong>'.__('htaccess Files Disabled: UAEG htaccess file writing is disabled. ', 'bulletproof-security').'</strong></font>'.__('Click this link for help information: ', 'bulletproof-security').'<a href="https://forum.ait-pro.com/forums/topic/htaccess-files-disabled-setup-wizard-enable-disable-htaccess-files/" target="_blank" title="htaccess Files Disabled Forum Topic">'.__('htaccess Files Disabled Forum Topic', 'bulletproof-security').'</a><br>';
		echo $text;
    	echo $bps_bottomDiv;
		return;
	}

	$bps_Uploads_Dir = wp_upload_dir();	
	$UploadsHtaccessMaster = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/uploads.htaccess';
	$UploadsHtaccessMasterIfmodule = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/uploads-ifmodule.htaccess';
	$UploadsHtaccessRenamed = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/manual_uploads.htaccess';
	$UploadsHtaccessRenamedIfmodule = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/manual_uploads_ifmodule.htaccess';
	$UploadsHtaccessARQ = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/plugins/bulletproof-security/admin/htaccess/uploads.htaccess';
	$UploadsHtaccessARQIfmodule = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/plugins/bulletproof-security/admin/htaccess/uploads-ifmodule.htaccess';
	$UploadsHtaccessARQRenamed = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/plugins/bulletproof-security/admin/htaccess/manual_uploads.htaccess';
	$UploadsHtaccessARQRenamedIfmodule = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/plugins/bulletproof-security/admin/htaccess/manual_uploads_ifmodule.htaccess';
	$UploadsHtaccessLSMaster = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/uploads-litespeed.htaccess';
	$UploadsHtaccessLSRenamed = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/manual_uploads-litespeed.htaccess';	
	$UploadsHtaccessLSMasterARQ = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/plugins/bulletproof-security/admin/htaccess/uploads-litespeed.htaccess';
	$UploadsHtaccessLSRenamedARQ = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/plugins/bulletproof-security/admin/htaccess/manual_uploads-litespeed.htaccess';	
	$UploadsHtaccess = $bps_Uploads_Dir['basedir'] . '/.htaccess';
	$Apache_Mod_options = get_option('bulletproof_security_options_apache_modules');	
	$UAEG_CC_options = get_option('bulletproof_security_options_customcode_UAEG');
	$bps_customcode_uaeg = htmlspecialchars_decode( $UAEG_CC_options['bps_customcode_uaeg'], ENT_QUOTES ) . "\n";
	$sapi_type = php_sapi_name();

	// LiteSpeed UAEG htaccess file creation
	if ( @substr($sapi_type, 0, 9) == 'litespeed' ) {			

		if ( file_exists($UploadsHtaccessLSMaster) && ! file_exists($UploadsHtaccessLSRenamed) ) {

			if ( $UAEG_CC_options['bps_customcode_uaeg'] != '' ) {
				
				file_put_contents( $UploadsHtaccessLSMaster, $bps_customcode_uaeg );
			}
			
			if ( ! copy($UploadsHtaccessLSMaster, $UploadsHtaccess )) {
				echo $bps_topDiv;
				$text = '<font color="#fb0101"><strong>'.__('Failed to activate UAEG BulletProof Mode uploads folder protection. Unable to Copy and Rename the uploads.htaccess file to /uploads/.htaccess', 'bulletproof-security').'</strong></font>';
				echo $text;
				echo $bps_bottomDiv;
			}
 	
			if ( file_exists($UploadsHtaccess) ) {
				@copy($UploadsHtaccess, $UploadsHtaccessLSMasterARQ);
				echo $bps_topDiv;
				$text = '<font color="green"><strong>'.__('UAEG BulletProof Mode uploads folder protection activated. Your /uploads folder is now protected.', 'bulletproof-security').'</strong></font>';
				echo $text;
				echo $bps_bottomDiv;
			}
			
		} elseif ( ! file_exists($UploadsHtaccessLSMaster) && file_exists($UploadsHtaccessLSRenamed) ) { // end if file uploads-litespeed.htaccess file exists
  	
			if ( $UAEG_CC_options['bps_customcode_uaeg'] != '' ) {
				
				file_put_contents( $UploadsHtaccessLSRenamed, $bps_customcode_uaeg );
			}

			if ( ! copy($UploadsHtaccessLSRenamed, $UploadsHtaccess ) ) {
				echo $bps_topDiv;
				$text = '<font color="#fb0101"><strong>'.__('Failed to activate UAEG BulletProof Mode. Unable to Copy and Rename the manual_uploads-litespeed.htaccess file to /uploads/.htaccess', 'bulletproof-security').'</strong></font>';
				echo $text;
				echo $bps_bottomDiv;
			}

			if ( file_exists($UploadsHtaccess) ) {
				@copy($UploadsHtaccess, $UploadsHtaccessLSMasterARQ); // copies /uploads/.htaccess to ARQ and renames the file to uploads-litespeed.htaccess 
				rename($UploadsHtaccessLSRenamed, $UploadsHtaccessLSMaster); // renames the manual_uploads-litespeed.htaccess file to uploads-litespeed.htacces in /htaccess folder
				@unlink($UploadsHtaccessLSRenamed); // deletes the manual_uploads-litespeed.htaccess from ARQ

				echo $bps_topDiv;
				$text = '<font color="green"><strong>'.__('UAEG BulletProof Mode uploads folder protection activated. Your /uploads folder is now protected.', 'bulletproof-security').'</strong></font>';
				echo $text;
				echo $bps_bottomDiv;
			}
		}
	}

	#### Apache Mod IfModule No ####
	if ( $Apache_Mod_options['bps_apache_mod_ifmodule'] == 'No' && @substr($sapi_type, 0, 9) != 'litespeed' ) {

		if ( file_exists($UploadsHtaccessMaster) && ! file_exists($UploadsHtaccessRenamed) ) {

			if ( $UAEG_CC_options['bps_customcode_uaeg'] != '' ) {
				
				file_put_contents( $UploadsHtaccessMaster, $bps_customcode_uaeg );
			}
			
			if ( ! copy($UploadsHtaccessMaster, $UploadsHtaccess )) {
				echo $bps_topDiv;
				$text = '<font color="#fb0101"><strong>'.__('Failed to activate UAEG BulletProof Mode uploads folder protection. Unable to Copy and Rename the uploads.htaccess file to /uploads/.htaccess', 'bulletproof-security').'</strong></font>';
				echo $text;
				echo $bps_bottomDiv;
			}
 	
			if ( file_exists($UploadsHtaccess) ) {
				@copy($UploadsHtaccess, $UploadsHtaccessARQ);
				echo $bps_topDiv;
				$text = '<font color="green"><strong>'.__('UAEG BulletProof Mode uploads folder protection activated. Your /uploads folder is now protected.', 'bulletproof-security').'</strong></font>';
				echo $text;
				echo $bps_bottomDiv;
			}
			
		} elseif ( ! file_exists($UploadsHtaccessMaster) && file_exists($UploadsHtaccessRenamed) ) { // end if file uploads.htaccess file exists
  	
			if ( $UAEG_CC_options['bps_customcode_uaeg'] != '' ) {
				
				file_put_contents( $UploadsHtaccessRenamed, $bps_customcode_uaeg );
			}

			if ( ! copy($UploadsHtaccessRenamed, $UploadsHtaccess ) ) {
				echo $bps_topDiv;
				$text = '<font color="#fb0101"><strong>'.__('Failed to activate UAEG BulletProof Mode. Unable to Copy and Rename the manual_uploads.htaccess file to /uploads/.htaccess', 'bulletproof-security').'</strong></font>';
				echo $text;
				echo $bps_bottomDiv;
			}

			if ( file_exists($UploadsHtaccess) ) {
				@copy($UploadsHtaccess, $UploadsHtaccessARQ); // copies /uploads/.htaccess to ARQ and renames the file to uploads.htaccess 
				rename($UploadsHtaccessRenamed, $UploadsHtaccessMaster); // renames the manual_uploads.htaccess file to uploads.htacces in /htaccess folder
				@unlink($UploadsHtaccessARQRenamed); // deletes the manual_uploads.htaccess from ARQ

				echo $bps_topDiv;
				$text = '<font color="green"><strong>'.__('UAEG BulletProof Mode uploads folder protection activated. Your /uploads folder is now protected.', 'bulletproof-security').'</strong></font>';
				echo $text;
				echo $bps_bottomDiv;
			}
		}
	} // end if ( $Apache_Mod_options['bps_apache_mod_ifmodule'] == 'No' ) {
	
	#### Apache Mod IfModule Yes ####
	if ( $Apache_Mod_options['bps_apache_mod_ifmodule'] == 'Yes' && @substr($sapi_type, 0, 9) != 'litespeed' ) {

		if ( file_exists($UploadsHtaccessMasterIfmodule) && ! file_exists($UploadsHtaccessRenamedIfmodule) ) {

			if ( $UAEG_CC_options['bps_customcode_uaeg'] != '' ) {
				
				file_put_contents( $UploadsHtaccessMasterIfmodule, $bps_customcode_uaeg );
			}

			if ( ! copy($UploadsHtaccessMasterIfmodule, $UploadsHtaccess )) {
				echo $bps_topDiv;
				$text = '<font color="#fb0101"><strong>'.__('Failed to activate UAEG BulletProof Mode uploads folder protection. Unable to Copy and Rename the uploads.htaccess file to /uploads/.htaccess', 'bulletproof-security').'</strong></font>';
				echo $text;
				echo $bps_bottomDiv;
			}
 	
			if ( file_exists($UploadsHtaccess) ) {
				@copy($UploadsHtaccess, $UploadsHtaccessARQIfmodule);
				echo $bps_topDiv;
				$text = '<font color="green"><strong>'.__('UAEG BulletProof Mode uploads folder protection activated. Your /uploads folder is now protected.', 'bulletproof-security').'</strong></font>';
				echo $text;
				echo $bps_bottomDiv;
			}
		
		} elseif ( ! file_exists($UploadsHtaccessMasterIfmodule) && file_exists($UploadsHtaccessRenamedIfmodule) ) { // end if file uploads.htaccess file exists
  	
			if ( $UAEG_CC_options['bps_customcode_uaeg'] != '' ) {
				
				file_put_contents( $UploadsHtaccessRenamedIfmodule, $bps_customcode_uaeg );
			}

			if ( ! copy($UploadsHtaccessRenamedIfmodule, $UploadsHtaccess ) ) {
				echo $bps_topDiv;
				$text = '<font color="#fb0101"><strong>'.__('Failed to activate UAEG BulletProof Mode. Unable to Copy and Rename the manual_uploads.htaccess file to /uploads/.htaccess', 'bulletproof-security').'</strong></font>';
				echo $text;
				echo $bps_bottomDiv;
			}

			if ( file_exists($UploadsHtaccess) ) {
				@copy($UploadsHtaccess, $UploadsHtaccessARQIfmodule); // copies /uploads/.htaccess to ARQ and renames the file to uploads-ifmodule.htaccess 
				// renames the manual_uploads_ifmodule.htaccess file to uploads-ifmodule.htacces in /htaccess folder
				rename($UploadsHtaccessRenamedIfmodule, $UploadsHtaccessMasterIfmodule); 
				@unlink($UploadsHtaccessARQRenamedIfmodule); // deletes the manual_uploads_ifmodule.htaccess from ARQ

				echo $bps_topDiv;
				$text = '<font color="green"><strong>'.__('UAEG BulletProof Mode uploads folder protection activated. Your /uploads folder is now protected.', 'bulletproof-security').'</strong></font>';
				echo $text;
				echo $bps_bottomDiv;
			}
		}
	} // end if ( $Apache_Mod_options['bps_apache_mod_ifmodule'] == 'Yes' ) {
}

// UAEG Deactivation:
// Rename UAEG Master .htaccess file to prevent automatic creation of UAEG .htaccess file
// 11.2: UAEG Custom Code added
// 11.6: New sapi check for LiteSpeed to create the new LiteSpeed mod_rewrite UAEG htaccess file version.
if ( isset( $_POST['Submit-UAEG-Deactivate'] ) && current_user_can('manage_options') ) {
	check_admin_referer( 'bulletproof_security_uaeg_deactivate' );

	$HFiles_options = get_option('bulletproof_security_options_htaccess_files');

	if ( $HFiles_options['bps_htaccess_files'] == 'disabled' ) {
		echo $bps_topDiv;
		$text = '<font color="blue"><strong>'.__('htaccess Files Disabled: UAEG htaccess file writing is disabled. ', 'bulletproof-security').'</strong></font>'.__('Click this link for help information: ', 'bulletproof-security').'<a href="https://forum.ait-pro.com/forums/topic/htaccess-files-disabled-setup-wizard-enable-disable-htaccess-files/" target="_blank" title="htaccess Files Disabled Forum Topic">'.__('htaccess Files Disabled Forum Topic', 'bulletproof-security').'</a><br>';
		echo $text;
    	echo $bps_bottomDiv;
		return;
	}

	$bps_Uploads_Dir = wp_upload_dir();	
	$UploadsHtaccessMaster = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/uploads.htaccess';
	$UploadsHtaccessMasterIfmodule = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/uploads-ifmodule.htaccess';
	$UploadsHtaccessRenamed = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/manual_uploads.htaccess';
	$UploadsHtaccessRenamedIfmodule = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/manual_uploads_ifmodule.htaccess';
	$UploadsHtaccessARQ = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/plugins/bulletproof-security/admin/htaccess/uploads.htaccess';
	$UploadsHtaccessARQIfmodule = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/plugins/bulletproof-security/admin/htaccess/uploads-ifmodule.htaccess';
	$UploadsHtaccessARQRenamed = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/plugins/bulletproof-security/admin/htaccess/manual_uploads.htaccess';
	$UploadsHtaccessARQRenamedIfmodule = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/plugins/bulletproof-security/admin/htaccess/manual_uploads_ifmodule.htaccess';
	$UploadsHtaccessLSMaster = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/uploads-litespeed.htaccess';
	$UploadsHtaccessLSRenamed = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/manual_uploads-litespeed.htaccess';	
	$UploadsHtaccessLSMasterARQ = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/plugins/bulletproof-security/admin/htaccess/uploads-litespeed.htaccess';
	$UploadsHtaccessLSRenamedARQ = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/plugins/bulletproof-security/admin/htaccess/manual_uploads-litespeed.htaccess';	
	$UploadsHtaccess = $bps_Uploads_Dir['basedir'] . '/.htaccess';
	$Apache_Mod_options = get_option('bulletproof_security_options_apache_modules');	
	$UAEG_CC_options = get_option('bulletproof_security_options_customcode_UAEG');
	$bps_customcode_uaeg = htmlspecialchars_decode( $UAEG_CC_options['bps_customcode_uaeg'], ENT_QUOTES ) . "\n";
	$sapi_type = php_sapi_name();

	// LiteSpeed UAEG htaccess file creation
	if ( @substr($sapi_type, 0, 9) == 'litespeed' ) {			

		if ( $UAEG_CC_options['bps_customcode_uaeg'] != '' ) {
				
			file_put_contents( $UploadsHtaccessLSMaster, $bps_customcode_uaeg );
		}			
			
		@copy($UploadsHtaccessLSMaster, $UploadsHtaccessLSRenamed); // makes a copy of uploads-litespeed.htaccess named manual_uploads-litespeed.htaccess
		@copy($UploadsHtaccessLSRenamed, $UploadsHtaccessLSRenamedARQ); // copies the manual_uploads-litespeed.htaccess file to ARQ
		@unlink($UploadsHtaccess); // deletes /uploads/.htaccess
		@unlink($UploadsHtaccessLSMaster); // deletes /htaccess/uploads-litespeed.htaccess
		@unlink($UploadsHtaccessLSMasterARQ); // deletes the uploads-litespeed.htaccess file in ARQ
		
		if ( file_exists($UploadsHtaccess) ) {
			echo $bps_topDiv;
			$text = '<font color="#fb0101"><strong>'.__('Failed to delete the UAEG /uploads/.htaccess file! The file does not exist. It may have been deleted already.', 'bulletproof-security').'</strong></font>';
			echo $text;
   			echo $bps_bottomDiv;
		
		} else {
			
			echo $bps_topDiv;
			$text = '<font color="green"><strong>'.__('UAEG BulletProof Mode has been deactivated. If you are testing or troubleshooting then be sure to activate UAEG BulletProof Mode for your uploads folder when you are done testing or troubleshooting.', 'bulletproof-security').'</strong></font><br><font color="#fb0101"><strong>'.__('Your uploads folder is no longer protected.', 'bulletproof-security').'</strong></font>';
			echo $text;
			echo $bps_bottomDiv;
		} // end if /uploads/.htaccess file exists
	}

	#### Apache Mod IfModule No ####
	if ( $Apache_Mod_options['bps_apache_mod_ifmodule'] == 'No' && @substr($sapi_type, 0, 9) != 'litespeed' ) {

		if ( file_exists($UploadsHtaccessMaster) ) {	
			
			if ( $UAEG_CC_options['bps_customcode_uaeg'] != '' ) {
				
				file_put_contents( $UploadsHtaccessMaster, $bps_customcode_uaeg );
			}			
			
			@copy($UploadsHtaccessMaster, $UploadsHtaccessRenamed); // makes a copy of uploads.htaccess named manual_uploads.htaccess
			@copy($UploadsHtaccessRenamed, $UploadsHtaccessARQRenamed); // copies the manual_uploads.htaccess file to ARQ
			@unlink($UploadsHtaccess); // deletes /uploads/.htaccess
			@unlink($UploadsHtaccessMaster); // deletes /htaccess/uploads.htaccess
			@unlink($UploadsHtaccessARQ); // deletes the uploads.htaccess file in ARQ
		}
		
		if ( file_exists($UploadsHtaccess) ) {
			echo $bps_topDiv;
			$text = '<font color="#fb0101"><strong>'.__('Failed to delete the UAEG /uploads/.htaccess file! The file does not exist. It may have been deleted already.', 'bulletproof-security').'</strong></font>';
			echo $text;
   			echo $bps_bottomDiv;
		
		} else {
			
			echo $bps_topDiv;
			$text = '<font color="green"><strong>'.__('UAEG BulletProof Mode has been deactivated successfully.', 'bulletproof-security').'</strong></font>';
			echo $text;
			echo $bps_bottomDiv;
		} // end if /uploads/.htaccess file exists
	} // end if ( $Apache_Mod_options['bps_apache_mod_ifmodule'] == 'No' ) {
	
	#### Apache Mod IfModule Yes ####
	if ( $Apache_Mod_options['bps_apache_mod_ifmodule'] == 'Yes' && @substr($sapi_type, 0, 9) != 'litespeed' ) {

		if ( file_exists($UploadsHtaccessMasterIfmodule) ) {	
			
			if ( $UAEG_CC_options['bps_customcode_uaeg'] != '' ) {
				
				file_put_contents( $UploadsHtaccessMasterIfmodule, $bps_customcode_uaeg );
			}			
			
			@copy($UploadsHtaccessMasterIfmodule, $UploadsHtaccessRenamedIfmodule); // makes a copy of uploads-ifmodule.htaccess named manual_uploads_ifmodule.htaccess
			@copy($UploadsHtaccessRenamedIfmodule, $UploadsHtaccessARQRenamedIfmodule); // copies the manual_uploads_ifmodule.htaccess file to ARQ
			
			if ( file_exists($UploadsHtaccess) ) {
				@unlink($UploadsHtaccess); // deletes /uploads/.htaccess
			}
			if ( file_exists($UploadsHtaccessMasterIfmodule) ) {
				@unlink($UploadsHtaccessMasterIfmodule); // deletes /htaccess/uploads-ifmodule.htaccess
			}
			if ( file_exists($UploadsHtaccessARQIfmodule) ) {
				@unlink($UploadsHtaccessARQIfmodule); // deletes the uploads-ifmodule.htaccess file in ARQ
			}
		}
		
		if ( file_exists($UploadsHtaccess) ) {
			echo $bps_topDiv;
			$text = '<font color="#fb0101"><strong>'.__('Failed to delete the UAEG /uploads/.htaccess file! The file does not exist. It may have been deleted already.', 'bulletproof-security').'</strong></font>';
			echo $text;
   			echo $bps_bottomDiv;
		
		} else {
			
			echo $bps_topDiv;
			$text = '<font color="green"><strong>'.__('UAEG BulletProof Mode has been deactivated successfully.', 'bulletproof-security').'</strong></font>';
			echo $text;
			echo $bps_bottomDiv;
		} // end if /uploads/.htaccess file exists
	} // end if ( $Apache_Mod_options['bps_apache_mod_ifmodule'] == 'Yes' ) {
}

// Form: Reset UAEG htaccess File
// If UAEG is activated then replace the /uploads/.htaccess file and replace: uploads.htaccess, uploads-ifmodule.htaccess and uploads-litespeed.htaccess files 
// with the default UAEG files. If UAEG is deactivated then update/replace the manual_ files with the default file contents also.
if ( isset( $_POST['Submit-UAEG-Reset'] ) && current_user_can('manage_options') ) {
	check_admin_referer( 'bulletproof_security_uaeg_reset' );
	
	$HFiles_options = get_option('bulletproof_security_options_htaccess_files');

	if ( $HFiles_options['bps_htaccess_files'] == 'disabled' ) {
		echo $bps_topDiv;
		$text = '<font color="blue"><strong>'.__('htaccess Files Disabled: UAEG htaccess file writing is disabled. ', 'bulletproof-security').'</strong></font>'.__('Click this link for help information: ', 'bulletproof-security').'<a href="https://forum.ait-pro.com/forums/topic/htaccess-files-disabled-setup-wizard-enable-disable-htaccess-files/" target="_blank" title="htaccess Files Disabled Forum Topic">'.__('htaccess Files Disabled Forum Topic', 'bulletproof-security').'</a><br>';
		echo $text;
    	echo $bps_bottomDiv;
		return;
	}

	$bps_Uploads_Dir = wp_upload_dir();	
	$UploadsHtaccessMaster = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/uploads.htaccess';
	$UploadsHtaccessRenamed = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/manual_uploads.htaccess';
	$UploadsHtaccessDefault = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/uploads-default.htaccess';
	$UploadsHtaccessMasterIfmodule = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/uploads-ifmodule.htaccess';
	$UploadsHtaccessRenamedIfmodule = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/manual_uploads_ifmodule.htaccess';
	$UploadsHtaccessDefaultIfmodule = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/uploads-ifmodule-default.htaccess';
	$UploadsHtaccessLSMaster = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/uploads-litespeed.htaccess';
	$UploadsHtaccessLSRenamed = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/manual_uploads-litespeed.htaccess';	
	$UploadsHtaccessLSDefault = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/uploads-litespeed-default.htaccess';
	$UploadsHtaccessARQ = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/plugins/bulletproof-security/admin/htaccess/uploads.htaccess';
	$UploadsHtaccessARQIfmodule = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/plugins/bulletproof-security/admin/htaccess/uploads-ifmodule.htaccess';
	$UploadsHtaccessARQRenamed = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/plugins/bulletproof-security/admin/htaccess/manual_uploads.htaccess';
	$UploadsHtaccessARQRenamedIfmodule = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/plugins/bulletproof-security/admin/htaccess/manual_uploads_ifmodule.htaccess';
	$UploadsHtaccessLSMasterARQ = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/plugins/bulletproof-security/admin/htaccess/uploads-litespeed.htaccess';
	$UploadsHtaccessLSRenamedARQ = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/plugins/bulletproof-security/admin/htaccess/manual_uploads-litespeed.htaccess';	
	$UploadsHtaccess = $bps_Uploads_Dir['basedir'] . '/.htaccess';
	$Apache_Mod_options = get_option('bulletproof_security_options_apache_modules');	
	$UAEG_CC_options = get_option('bulletproof_security_options_customcode_UAEG');
	$sapi_type = php_sapi_name();	

	// LiteSpeed UAEG htaccess file reset
	if ( @substr($sapi_type, 0, 9) == 'litespeed' ) {	
	
		if ( file_exists($UploadsHtaccess) ) {
			@copy($UploadsHtaccessLSDefault, $UploadsHtaccess);
		}
		if ( file_exists($UploadsHtaccessLSMaster) ) {
			@copy($UploadsHtaccessLSDefault, $UploadsHtaccessLSMaster);
			@copy($UploadsHtaccessLSDefault, $UploadsHtaccessLSMasterARQ);		
		}
		if ( file_exists($UploadsHtaccessLSRenamed) ) {
			@copy($UploadsHtaccessLSDefault, $UploadsHtaccessLSRenamed);
			@copy($UploadsHtaccessLSDefault, $UploadsHtaccessLSRenamedARQ);		
		}
	}

	if ( $Apache_Mod_options['bps_apache_mod_ifmodule'] == 'No' && @substr($sapi_type, 0, 9) != 'litespeed' ) {
		
		if ( file_exists($UploadsHtaccess) ) {
			@copy($UploadsHtaccessDefault, $UploadsHtaccess);
		}
		if ( file_exists($UploadsHtaccessMaster) ) {
			@copy($UploadsHtaccessDefault, $UploadsHtaccessMaster);
			@copy($UploadsHtaccessDefault, $UploadsHtaccessARQ);		
		}
		if ( file_exists($UploadsHtaccessRenamed) ) {
			@copy($UploadsHtaccessDefault, $UploadsHtaccessRenamed);
			@copy($UploadsHtaccessDefault, $UploadsHtaccessARQRenamed);		
		}
	}

	if ( $Apache_Mod_options['bps_apache_mod_ifmodule'] == 'Yes' && @substr($sapi_type, 0, 9) != 'litespeed' ) {
		
		if ( file_exists($UploadsHtaccess) ) {
			@copy($UploadsHtaccessDefaultIfmodule, $UploadsHtaccess);
		}
		if ( file_exists($UploadsHtaccessMasterIfmodule) ) {
			@copy($UploadsHtaccessDefaultIfmodule, $UploadsHtaccessMasterIfmodule);
			@copy($UploadsHtaccessDefaultIfmodule, $UploadsHtaccessARQIfmodule);		
		}
		if ( file_exists($UploadsHtaccessRenamedIfmodule) ) {
			@copy($UploadsHtaccessDefaultIfmodule, $UploadsHtaccessRenamedIfmodule);
			@copy($UploadsHtaccessDefaultIfmodule, $UploadsHtaccessARQRenamedIfmodule);		
		}
	}
	
	if ( $UAEG_CC_options['bps_customcode_uaeg'] != '' ) {

		$UAEG_CC_Options = array( 'bps_customcode_uaeg' => '' );

		foreach( $UAEG_CC_Options as $key => $value ) {
			update_option('bulletproof_security_options_customcode_UAEG', $UAEG_CC_Options);
		}
	}
	
	echo $bps_topDiv;
	$text = '<font color="green"><strong>'.__('Your UAEG htaccess file has been reset to the default BPS Pro UAEG htaccess file/code.', 'bulletproof-security').'</strong></font>';
	echo $text;
	echo $bps_bottomDiv;
}

// Form: HPF Save Hidden Plugin Folders & Files Cron Form Options
if ( isset( $_POST['Submit-Hidden-Plugins'] ) && current_user_can('manage_options') ) {
	check_admin_referer('bulletproof_security_hpf_cron');

	$HPF_Options = array( 
	'bps_hidden_plugins_cron' 				=> esc_html($_POST['hpf_on_off']), 
	'bps_hidden_plugins_cron_frequency' 	=> esc_html($_POST['hpf_cron_frequency']), 
	'bps_hidden_plugins_cron_email' 		=> esc_html(''), 
	'bps_hidden_plugins_cron_alert' 		=> esc_html('')
	);
	
	foreach( $HPF_Options as $key => $value ) {
		update_option('bulletproof_security_options_hpf_cron', $HPF_Options);
	}
	
	$hpf_options = get_option('bulletproof_security_options_hpf_cron');
	
	if ( $hpf_options['bps_hidden_plugins_cron'] == 'On' ) {
	
		echo $bps_topDiv;
		$text = '<strong><font color="green">'.__('The Hidden Plugin Folders|Files (HPF) Cron is turned On.', 'bulletproof-security').'<br>'.__('The HPF Cron Check Frequency setting is: ', 'bulletproof-security').$hpf_options['bps_hidden_plugins_cron_frequency'].__(' minutes.', 'bulletproof-security').'</font></strong>';
		echo $text;
		echo $bps_bottomDiv;
		
	} elseif ( $hpf_options['bps_hidden_plugins_cron'] == 'Off' ) {
		
		wp_clear_scheduled_hook('bpsPro_HPF_check');
			
		echo $bps_topDiv;
		$text = '<strong><font color="green">'.__('Hidden Plugin Folders|Files (HPF) Cron has been turned Off.', 'bulletproof-security').'</font></strong><br>';
		echo $text;
		echo $bps_bottomDiv;		
	}
}

// Form: HPF Save Ignore Hidden Plugin Folders & Files Rules
if ( isset( $_POST['Hidden-Plugins-Ignore-Submit'] ) && current_user_can('manage_options') ) {
	check_admin_referer('bulletproof_security_hpf_cron_ignore');
	
	$HPFI_Options = array( 'bps_hidden_plugins_check' => esc_html( $_POST['bps_hidden_plugins_check'] ) );
	
	foreach( $HPFI_Options as $key => $value ) {
		update_option('bulletproof_security_options_hidden_plugins', $HPFI_Options);
	}

	$hpfi_options = get_option('bulletproof_security_options_hidden_plugins');
	
	echo $bps_topDiv;
	$text = '<strong><font color="green">'.__('Ignore Hidden Plugin Folders & Files settings saved.', 'bulletproof-security').'<br>'.__('Current HPF Ignore Rules: ', 'bulletproof-security').$hpfi_options['bps_hidden_plugins_check'].'</font></strong>';
	echo $text;
	echo $bps_bottomDiv;
}

// MBM Activation: copy Deny All htaccess file to BPS Master htaccess folder /htaccess
if ( isset( $_POST['Submit-MBM-Activate'] ) && current_user_can('manage_options') ) {
	check_admin_referer( 'bulletproof_security_mbm_activate' );
	
	$HFiles_options = get_option('bulletproof_security_options_htaccess_files');

	if ( $HFiles_options['bps_htaccess_files'] == 'disabled' ) {
		echo $bps_topDiv;
		$text = '<font color="blue"><strong>'.__('htaccess Files Disabled: Master htaccess file writing is disabled. ', 'bulletproof-security').'</strong></font>'.__('Click this link for help information: ', 'bulletproof-security').'<a href="https://forum.ait-pro.com/forums/topic/htaccess-files-disabled-setup-wizard-enable-disable-htaccess-files/" target="_blank" title="htaccess Files Disabled Forum Topic">'.__('htaccess Files Disabled Forum Topic', 'bulletproof-security').'</a><br>';
		echo $text;
    	echo $bps_bottomDiv;
		return;
	}

	$bps_rename_htaccess = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/deny-all.htaccess';
	$deny_all_ifmodule = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/deny-all-ifmodule.htaccess';
	$bps_rename_htaccess_renamed = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/.htaccess';
	
	$Apache_Mod_options = get_option('bulletproof_security_options_apache_modules');	

	if ( $Apache_Mod_options['bps_apache_mod_ifmodule'] == 'Yes' ) {
			
		if ( ! copy($deny_all_ifmodule, $bps_rename_htaccess_renamed) ) {
			echo $bps_topDiv;
			$text = '<font color="#fb0101"><strong>'.__('Failed to activate Master htaccess Folder BulletProof Mode. Check the file or folder permissions or Ownership for this folder: /bulletproof-security/admin/htaccess/.', 'bulletproof-security').'</strong></font>';
			echo $text;
   			echo $bps_bottomDiv;
		} else {
			echo $bps_topDiv;
			$text = '<font color="green"><strong>'.__('Master htaccess Folder BulletProof Mode activated successfully.', 'bulletproof-security').'</strong></font>';
			echo $text;
			echo $bps_bottomDiv;
		}			
		
	} else {
			
		if ( ! copy($bps_rename_htaccess, $bps_rename_htaccess_renamed) ) {
			echo $bps_topDiv;
			$text = '<font color="#fb0101"><strong>'.__('Failed to activate Master htaccess Folder BulletProof Mode. Check the file or folder permissions or Ownership for this folder: /bulletproof-security/admin/htaccess/.', 'bulletproof-security').'</strong></font>';
			echo $text;
   			echo $bps_bottomDiv;
		} else {
			echo $bps_topDiv;
			$text = '<font color="green"><strong>'.__('Master htaccess Folder BulletProof Mode activated successfully.', 'bulletproof-security').'</strong></font>';
			echo $text;
			echo $bps_bottomDiv;
		}			
	}
}

// MBM Deactivation: delete BPS Master htaccess folder /htaccess file
if ( isset( $_POST['Submit-MBM-Deactivate'] ) && current_user_can('manage_options') ) {
	check_admin_referer( 'bulletproof_security_mbm_deactivate' );
	
	$HFiles_options = get_option('bulletproof_security_options_htaccess_files');

	if ( $HFiles_options['bps_htaccess_files'] == 'disabled' ) {
		echo $bps_topDiv;
		$text = '<font color="blue"><strong>'.__('htaccess Files Disabled: Master htaccess file writing is disabled. ', 'bulletproof-security').'</strong></font>'.__('Click this link for help information: ', 'bulletproof-security').'<a href="https://forum.ait-pro.com/forums/topic/htaccess-files-disabled-setup-wizard-enable-disable-htaccess-files/" target="_blank" title="htaccess Files Disabled Forum Topic">'.__('htaccess Files Disabled Forum Topic', 'bulletproof-security').'</a><br>';
		echo $text;
    	echo $bps_bottomDiv;
		return;
	}

	$mbm_htaccess_file = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/.htaccess';

	@unlink($mbm_htaccess_file);
	
	if ( file_exists($mbm_htaccess_file) ) {
		
		echo $bps_topDiv;
		$text = '<font color="#fb0101"><strong>'.__('Failed to deactivate Master htaccess Folder BulletProof Mode. Check the file or folder permissions or Ownership for this folder: /bulletproof-security/admin/htaccess/.', 'bulletproof-security').'</strong></font>';
		echo $text;
   		echo $bps_bottomDiv;
	
	} else {
		
		echo $bps_topDiv;
		$text = '<font color="green"><strong>'.__('Master htaccess Folder BulletProof Mode deactivated successfully.', 'bulletproof-security').'</strong></font><br>';
		echo $text;
		echo $bps_bottomDiv;
	}
}

// BBM Activation: copy Deny All htaccess file to BPS Backup Folder /bps-backup
if ( isset( $_POST['Submit-BBM-Activate'] ) && current_user_can('manage_options') ) {
	check_admin_referer( 'bulletproof_security_bbm_activate' );
	
	$HFiles_options = get_option('bulletproof_security_options_htaccess_files');

	if ( $HFiles_options['bps_htaccess_files'] == 'disabled' ) {
		echo $bps_topDiv;
		$text = '<font color="blue"><strong>'.__('htaccess Files Disabled: BPS Backup htaccess file writing is disabled. ', 'bulletproof-security').'</strong></font>'.__('Click this link for help information: ', 'bulletproof-security').'<a href="https://forum.ait-pro.com/forums/topic/htaccess-files-disabled-setup-wizard-enable-disable-htaccess-files/" target="_blank" title="htaccess Files Disabled Forum Topic">'.__('htaccess Files Disabled Forum Topic', 'bulletproof-security').'</a><br>';
		echo $text;
    	echo $bps_bottomDiv;
		return;
	}

	$bps_rename_htaccess_backup = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/deny-all.htaccess';
	$deny_all_ifmodule = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/deny-all-ifmodule.htaccess';
	$bps_rename_htaccess_backup_online = WP_CONTENT_DIR . '/bps-backup/.htaccess';
	
	$Apache_Mod_options = get_option('bulletproof_security_options_apache_modules');	

	if ( $Apache_Mod_options['bps_apache_mod_ifmodule'] == 'Yes' ) {
			
		if ( ! copy($deny_all_ifmodule, $bps_rename_htaccess_backup_online) ) {
			echo $bps_topDiv;
			$text = '<font color="#fb0101"><strong>'.__('Failed to activate BPS Backup Folder BulletProof Mode. Check the file or folder permissions or Ownership for this folder: /', 'bulletproof-security').$bps_wpcontent_dir.__('/bps-backup.', 'bulletproof-security').'</strong></font>';
			echo $text;
   			echo $bps_bottomDiv;
		} else {
			echo $bps_topDiv;
			$text = '<font color="green"><strong>'.__('BPS Backup Folder BulletProof Mode activated successfully.', 'bulletproof-security').'</strong></font>';
			echo $text;
			echo $bps_bottomDiv;
		}			
		
	} else {
			
		if ( ! copy($bps_rename_htaccess_backup, $bps_rename_htaccess_backup_online) ) {
			echo $bps_topDiv;
			$text = '<font color="#fb0101"><strong>'.__('Failed to activate BPS Backup Folder BulletProof Mode. Check the file or folder permissions or Ownership for this folder: /', 'bulletproof-security').$bps_wpcontent_dir.__('/bps-backup.', 'bulletproof-security').'</strong></font>';
			echo $text;
   			echo $bps_bottomDiv;
		} else {
			echo $bps_topDiv;
			$text = '<font color="green"><strong>'.__('BPS Backup Folder BulletProof Mode activated successfully.', 'bulletproof-security').'</strong></font>';
			echo $text;
			echo $bps_bottomDiv;
		}			
	}
}

// BBM Deactivation: delete BPS Backup folder /bps-backup htaccess file
if ( isset( $_POST['Submit-BBM-Deactivate'] ) && current_user_can('manage_options') ) {
	check_admin_referer( 'bulletproof_security_bbm_deactivate' );
	
	$HFiles_options = get_option('bulletproof_security_options_htaccess_files');

	if ( $HFiles_options['bps_htaccess_files'] == 'disabled' ) {
		echo $bps_topDiv;
		$text = '<font color="blue"><strong>'.__('htaccess Files Disabled: BPS Backup htaccess file writing is disabled. ', 'bulletproof-security').'</strong></font>'.__('Click this link for help information: ', 'bulletproof-security').'<a href="https://forum.ait-pro.com/forums/topic/htaccess-files-disabled-setup-wizard-enable-disable-htaccess-files/" target="_blank" title="htaccess Files Disabled Forum Topic">'.__('htaccess Files Disabled Forum Topic', 'bulletproof-security').'</a><br>';
		echo $text;
    	echo $bps_bottomDiv;
		return;
	}

	$bbm_htaccess_file = WP_CONTENT_DIR . '/bps-backup/.htaccess';

	@unlink($bbm_htaccess_file);
	
	if ( file_exists($bbm_htaccess_file) ) {
		
		echo $bps_topDiv;
		$text = '<font color="#fb0101"><strong>'.__('Failed to deactivate BPS Backup Folder BulletProof Mode. Check the file or folder permissions or Ownership for this folder: /', 'bulletproof-security').$bps_wpcontent_dir.__('/bps-backup.', 'bulletproof-security').'</strong></font>';
		echo $text;
   		echo $bps_bottomDiv;
	
	} else {
		
		echo $bps_topDiv;
		$text = '<font color="green"><strong>'.__('BPS Backup Folder BulletProof Mode deactivated successfully.', 'bulletproof-security').'</strong></font><br>';
		echo $text;
		echo $bps_bottomDiv;
	}
}

// Form: Backup htaccess files
if ( isset( $_POST['Submit-Backup-htaccess-Files'] ) && current_user_can('manage_options') ) {
	check_admin_referer( 'bulletproof_security_backup_active_htaccess_files' );
	
	$HFiles_options = get_option('bulletproof_security_options_htaccess_files');

	if ( $HFiles_options['bps_htaccess_files'] == 'disabled' ) {
		echo $bps_topDiv;
		$text = '<font color="blue"><strong>'.__('htaccess Files Disabled: htaccess file Backup is disabled. ', 'bulletproof-security').'</strong></font>'.__('Click this link for help information: ', 'bulletproof-security').'<a href="https://forum.ait-pro.com/forums/topic/htaccess-files-disabled-setup-wizard-enable-disable-htaccess-files/" target="_blank" title="htaccess Files Disabled Forum Topic">'.__('htaccess Files Disabled Forum Topic', 'bulletproof-security').'</a><br>';
		echo $text;
    	echo $bps_bottomDiv;
		return;
	}

	$old_backroot = ABSPATH . '.htaccess';
	$new_backroot = WP_CONTENT_DIR . '/bps-backup/master-backups/root.htaccess';
	$old_backwpadmin = ABSPATH . 'wp-admin/.htaccess';
	$new_backwpadmin = WP_CONTENT_DIR . '/bps-backup/master-backups/wpadmin.htaccess';
	
	if ( ! file_exists($old_backroot) ) { 
		echo $bps_topDiv;
		$text = '<font color="#fb0101"><strong>'.__('You do not currently have an .htaccess file in your Root folder to backup.', 'bulletproof-security').'</strong></font>';
		echo $text;
		echo '</p></div>';
	
	} else {	
		
		if ( ! copy($old_backroot, $new_backroot) ) {
			echo $bps_topDiv;
			$text = '<font color="#fb0101"><strong>'.__('Failed to Backup Your Root .htaccess File. File copy function failed. Check the folder permissions for the /', 'bulletproof-security').$bps_wpcontent_dir.__('/bps-backup folder. Folder permissions should be set to 755.', 'bulletproof-security').'</strong></font>';
			echo $text;
			echo $bps_bottomDiv;
		
		} else {
			
			echo $bps_topDiv;
			$text = '<font color="green"><strong>'.__('Your currently active Root .htaccess file has been backed up successfully.', 'bulletproof-security').'</strong></font>';
			echo $text;
			echo $bps_bottomDiv;
		}
	}
		
	if ( ! file_exists($old_backwpadmin) ) { 
		echo $bps_topDiv;
		$text = '<font color="#fb0101"><strong>'.__('You do not currently have an htaccess file in your wp-admin folder to backup.', 'bulletproof-security').'</strong></font>';
		echo $text;
		echo $bps_bottomDiv;
		
	} else {
		
		if ( ! copy($old_backwpadmin, $new_backwpadmin) ) {
			echo $bps_topDiv;
			$text = '<font color="#fb0101"><strong>'.__('Failed to Backup Your wp-admin htaccess File. File copy function failed. Check the folder permissions for the /', 'bulletproof-security').$bps_wpcontent_dir.__('/bps-backup folder. Folder permissions should be set to 755.', 'bulletproof-security').'</strong></font>';
			echo $text;
			echo $bps_bottomDiv;
		
		} else {
			
			echo $bps_topDiv;
			$text = '<font color="green"><strong>'.__('Your currently active wp-admin htaccess file has been backed up successfully.', 'bulletproof-security').'</strong></font><br>';
			echo $text;
			echo $bps_bottomDiv;
		}
	}
}

// Form: Restore backed up htaccess files
if ( isset( $_POST['Submit-Restore-htaccess-Files'] ) && current_user_can('manage_options') ) {
	check_admin_referer( 'bulletproof_security_restore_active_htaccess_files' );
	
	$HFiles_options = get_option('bulletproof_security_options_htaccess_files');

	if ( $HFiles_options['bps_htaccess_files'] == 'disabled' ) {
		echo $bps_topDiv;
		$text = '<font color="blue"><strong>'.__('htaccess Files Disabled: htaccess file Restore is disabled. ', 'bulletproof-security').'</strong></font>'.__('Click this link for help information: ', 'bulletproof-security').'<a href="https://forum.ait-pro.com/forums/topic/htaccess-files-disabled-setup-wizard-enable-disable-htaccess-files/" target="_blank" title="htaccess Files Disabled Forum Topic">'.__('htaccess Files Disabled Forum Topic', 'bulletproof-security').'</a><br>';
		echo $text;
    	echo $bps_bottomDiv;
		return;
	}

	$AutoLockoptions = get_option('bulletproof_security_options_autolock');
	$old_restoreroot = WP_CONTENT_DIR . '/bps-backup/master-backups/root.htaccess';
	$new_restoreroot = ABSPATH . '.htaccess';
	$old_restorewpadmin = WP_CONTENT_DIR . '/bps-backup/master-backups/wpadmin.htaccess';
	$new_restorewpadmin = ABSPATH . 'wp-admin/.htaccess';
	$htaccessARRoot = WP_CONTENT_DIR . '/bps-backup/autorestore/root-files/auto_.htaccess';
	$htaccessARWpadmin = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-admin/wpadmin.htaccess';
	$permsRootHtaccess = @substr(sprintf('%o', fileperms($new_restoreroot)), -4);
	$sapi_type = php_sapi_name();		

	if ( file_exists($old_restoreroot) ) { 
		
		if ( @substr($sapi_type, 0, 6) != 'apache' && @$permsRootHtaccess != '0666' || @$permsRootHtaccess != '0777') { // Windows IIS, XAMPP, etc
			@chmod($new_restoreroot, 0644);
		}	
		
	if ( ! copy($old_restoreroot, $new_restoreroot) ) {
		echo $bps_topDiv;
		echo '<font color="#fb0101"><strong>'.__('Failed to Restore Your Root htaccess File. Either you DO NOT currently have a Backed up Root htaccess file or your current active Root htaccess file permissions do not allow the file to be replaced/restored.', 'bulletproof-security').'</strong></font>';
   		echo $bps_bottomDiv;
		
	} else {
			
		if ( copy($new_restoreroot, $htaccessARRoot) ) {
			
			if ( @substr($sapi_type, 0, 6) != 'apache' && $AutoLockoptions['bps_root_htaccess_autolock'] == 'On' ) {			
				@chmod($new_restoreroot, 0404);
			}
			
		echo $bps_topDiv;
		$textRoot = '<font color="green"><strong>'.__('Your Root htaccess file has been Restored successfully.', 'bulletproof-security').'</strong></font>';
		echo $textRoot;
		echo $bps_bottomDiv;
		}
	}
	}
		
	if ( file_exists($old_restorewpadmin) ) { 	
	
	if ( ! copy($old_restorewpadmin, $new_restorewpadmin) ) {
		echo $bps_topDiv;
		$text = '<font color="#fb0101"><strong>'.__('Failed to Restore Your wp-admin htaccess File. Either you DO NOT currently have a Backed up wp-admin htaccess file or your current active wp-admin htaccess file permissions do not allow the file to be replaced/restored.', 'bulletproof-security').'</strong></font>';
		echo $text;
   		echo $bps_bottomDiv;
		
	} else {
			
		copy($old_restorewpadmin, $htaccessARWpadmin);
		echo $bps_topDiv;
		$textWpadmin = '<font color="green"><strong>'.__('Your wp-admin htaccess file has been Restored successfully.', 'bulletproof-security').'</strong></font>';
		echo $textWpadmin;
		echo $bps_bottomDiv;
	}
	}
}

?>