<?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 ( ! function_exists ('add_action') ) {
		header('Status: 403 Forbidden');
		header('HTTP/1.1 403 Forbidden');
		exit();
}

/******************************************************* */
/* ARQ - AutoRestore Cron intervals, schedules, function */
/******************************************************* */
add_filter('cron_schedules', 'bpsPro_add_ARCM_intervals'); 

// Add BPS Pro Add Cron Schedule Intervals - 1, 2, 3, 4, 5, 10, 15, 30, 60 minutes
// Intervals only need to be setup once - other cron jobs can hook into and use these new intervals
// would need a weekly and monthly cron and use wp's hourly and daily crons
// 7 days = 604800 - 30 days = 2592000 - not doing 31 days
function bpsPro_add_ARCM_intervals($schedules) {
	$schedules['minutes_1'] = array(
		'interval' 	=> 60,
		'display' 	=> __('Once every minute')
	);
	$schedules['minutes_2'] = array(
		'interval' 	=> 120,
		'display' 	=> __('Once every 2 minutes')
	);
	$schedules['minutes_3'] = array(
		'interval' 	=> 180,
		'display' 	=> __('Once every 3 minutes')
	);
	$schedules['minutes_4'] = array(
		'interval' 	=> 240,
		'display' 	=> __('Once every 4 minutes')
	);
	$schedules['minutes_5'] = array(
		'interval' 	=> 300,
		'display' 	=> __('Once every 5 minutes')
	);
	$schedules['minutes_10'] = array(
		'interval' 	=> 600,
		'display' 	=> __('Once every 10 minutes')
	);
	$schedules['minutes_15'] = array(
		'interval' 	=> 900,
		'display' 	=> __('Once every 15 minutes')
	);
	$schedules['minutes_30'] = array(
		'interval' 	=> 1800,
		'display' 	=> __('Once every 30 minutes')
	);
	$schedules['minutes_60'] = array(
		'interval' 	=> 3600,
		'display' 	=> __('Once every 60 minutes')
	);
	$schedules['hours_3'] = array( 
		'interval' => 10800, 
		'display' => __('Once every 3 hours') 
	);
	$schedules['hours_6'] = array( 
		'interval' => 21600, 
		'display' => __('Once every 6 hours') 
	);
	$schedules['hours_12'] = array( 
		'interval' => 43200, 
		'display' => __('Once every 12 hours') 
	);
	$schedules['hours_24'] = array( 
		'interval' => 86400, 
		'display' => __('Once every 24 hours') 
	);
	$schedules['daily'] = array( 
		'interval' => 86400, 
		'display' => __('Once Daily') 
	);

	return $schedules;
}

// bps_Master_ARQ_Fire() located around code line 2050
add_action('bpsPro_AutoRestore_check', 'bps_Master_ARQ_Fire');

// BPS pro AutoRestore & Quarantine Cron schedule checks based on DB options saved
function bpsPro_schedule_AutoRestore_checks() {
$options = get_option('bulletproof_security_options_ARCM');
$killit = '';
	
	if ( ! get_option('bulletproof_security_options_ARCM') || ! $options['bps_autorestore_cron'] || $options['bps_autorestore_cron'] == '' ) {
		return $killit;
	}	
	
	if ( $options['bps_autorestore_cron'] == 'On' ) {
	
		$bpsCronCheck = wp_get_schedule('bpsPro_AutoRestore_check');

	if ( $options['bps_autorestore_cron_frequency'] == '1' ) {
	if ( $bpsCronCheck == 'minutes_2' || $bpsCronCheck == 'minutes_3' || $bpsCronCheck == 'minutes_4' || $bpsCronCheck == 'minutes_5' || $bpsCronCheck == 'minutes_10' || $bpsCronCheck == 'minutes_15' || $bpsCronCheck == 'minutes_30' || $bpsCronCheck == 'minutes_60' ) {
		wp_clear_scheduled_hook('bpsPro_AutoRestore_check');
	}
	
	if ( ! wp_next_scheduled('bpsPro_AutoRestore_check') ) {
		wp_schedule_event( time(), 'minutes_1', 'bpsPro_AutoRestore_check' );
	}
	}
	
	if ( $options['bps_autorestore_cron_frequency'] == '2' ) {
	if ( $bpsCronCheck == 'minutes_1' || $bpsCronCheck == 'minutes_3' || $bpsCronCheck == 'minutes_4' || $bpsCronCheck == 'minutes_5' || $bpsCronCheck == 'minutes_10' || $bpsCronCheck == 'minutes_15' || $bpsCronCheck == 'minutes_30' || $bpsCronCheck == 'minutes_60' ) {
		wp_clear_scheduled_hook('bpsPro_AutoRestore_check');
	}
	
	if ( ! wp_next_scheduled('bpsPro_AutoRestore_check') ) {
		wp_schedule_event( time(), 'minutes_2', 'bpsPro_AutoRestore_check' );
	}
	}

	if ( $options['bps_autorestore_cron_frequency'] == '3' ) {
	if ( $bpsCronCheck == 'minutes_1' || $bpsCronCheck == 'minutes_2' || $bpsCronCheck == 'minutes_4' || $bpsCronCheck == 'minutes_5' || $bpsCronCheck == 'minutes_10' || $bpsCronCheck == 'minutes_15' || $bpsCronCheck == 'minutes_30' || $bpsCronCheck == 'minutes_60' ) {
		wp_clear_scheduled_hook('bpsPro_AutoRestore_check');
	}
	
	if ( ! wp_next_scheduled('bpsPro_AutoRestore_check') ) {
		wp_schedule_event( time(), 'minutes_3', 'bpsPro_AutoRestore_check' );
	}
	}

	if ( $options['bps_autorestore_cron_frequency'] == '4' ) {
	if ( $bpsCronCheck == 'minutes_1' || $bpsCronCheck == 'minutes_2' || $bpsCronCheck == 'minutes_3' || $bpsCronCheck == 'minutes_5' || $bpsCronCheck == 'minutes_10' || $bpsCronCheck == 'minutes_15' || $bpsCronCheck == 'minutes_30' || $bpsCronCheck == 'minutes_60' ) {
		wp_clear_scheduled_hook('bpsPro_AutoRestore_check');
	}
	
	if ( ! wp_next_scheduled('bpsPro_AutoRestore_check') ) {
		wp_schedule_event( time(), 'minutes_4', 'bpsPro_AutoRestore_check' );
	}
	}

	if ( $options['bps_autorestore_cron_frequency'] == '5' ) {
	if ( $bpsCronCheck == 'minutes_1' || $bpsCronCheck == 'minutes_2' || $bpsCronCheck == 'minutes_3' || $bpsCronCheck == 'minutes_4' || $bpsCronCheck == 'minutes_10' || $bpsCronCheck == 'minutes_15' || $bpsCronCheck == 'minutes_30' || $bpsCronCheck == 'minutes_60' ) {
		wp_clear_scheduled_hook('bpsPro_AutoRestore_check');
	}
	
	if ( ! wp_next_scheduled('bpsPro_AutoRestore_check') ) {
		wp_schedule_event( time(), 'minutes_5', 'bpsPro_AutoRestore_check' );
	}
	}
	
	if ( $options['bps_autorestore_cron_frequency'] == '10' ) {
	if ( $bpsCronCheck == 'minutes_1' || $bpsCronCheck == 'minutes_2' || $bpsCronCheck == 'minutes_3' || $bpsCronCheck == 'minutes_4' || $bpsCronCheck == 'minutes_5' || $bpsCronCheck == 'minutes_15' || $bpsCronCheck == 'minutes_30' || $bpsCronCheck == 'minutes_60' ) {
		wp_clear_scheduled_hook('bpsPro_AutoRestore_check');
	}
	
	if ( ! wp_next_scheduled('bpsPro_AutoRestore_check') ) {
		wp_schedule_event( time(), 'minutes_10', 'bpsPro_AutoRestore_check' );
	}
	}
	
	if ( $options['bps_autorestore_cron_frequency'] == '15' ) {
	if ( $bpsCronCheck == 'minutes_1' || $bpsCronCheck == 'minutes_2' || $bpsCronCheck == 'minutes_3' || $bpsCronCheck == 'minutes_4' || $bpsCronCheck == 'minutes_5' || $bpsCronCheck == 'minutes_10' || $bpsCronCheck == 'minutes_30' || $bpsCronCheck == 'minutes_60' ) {
		wp_clear_scheduled_hook('bpsPro_AutoRestore_check');
	}
	
	if ( ! wp_next_scheduled('bpsPro_AutoRestore_check') ) {
		wp_schedule_event( time(), 'minutes_15', 'bpsPro_AutoRestore_check' );
	}
	}
	
	if ( $options['bps_autorestore_cron_frequency'] == '30' ) {
	if ( $bpsCronCheck == 'minutes_1' || $bpsCronCheck == 'minutes_2' || $bpsCronCheck == 'minutes_3' || $bpsCronCheck == 'minutes_4' || $bpsCronCheck == 'minutes_5' || $bpsCronCheck == 'minutes_10' || $bpsCronCheck == 'minutes_15' || $bpsCronCheck == 'minutes_60' ) {
		wp_clear_scheduled_hook('bpsPro_AutoRestore_check');
	}
	
	if ( ! wp_next_scheduled('bpsPro_AutoRestore_check') ) {
		wp_schedule_event( time(), 'minutes_30', 'bpsPro_AutoRestore_check' );
	}
	}

	if ( $options['bps_autorestore_cron_frequency'] == '60' ) {
	if ( $bpsCronCheck == 'minutes_1' || $bpsCronCheck == 'minutes_2' || $bpsCronCheck == 'minutes_3' || $bpsCronCheck == 'minutes_4' || $bpsCronCheck == 'minutes_5' || $bpsCronCheck == 'minutes_10' || $bpsCronCheck == 'minutes_15' || $bpsCronCheck == 'minutes_30' ) {
		wp_clear_scheduled_hook('bpsPro_AutoRestore_check');
	}
	
	if ( ! wp_next_scheduled('bpsPro_AutoRestore_check') ) {
		wp_schedule_event( time(), 'minutes_60', 'bpsPro_AutoRestore_check' );
	}
	}

	} // end if bps_autorestore_cron On
	elseif ( $options['bps_autorestore_cron'] == 'Off' ) { 
		wp_clear_scheduled_hook('bpsPro_AutoRestore_check');
	}
}

add_action('init', 'bpsPro_schedule_AutoRestore_checks');

add_action('bpsPro_AFS_check', 'bpsPro_ARQ_upgrader_FailSafe_check');

// 12.6: Changed the AFS Cron to 1 minute
function bpsPro_schedule_AFS_checks() {

	$bpsCronCheck = wp_get_schedule('bpsPro_AFS_check');

	if ( $bpsCronCheck == 'minutes_3' ) {
		wp_clear_scheduled_hook('bpsPro_AFS_check');
	}
	
	if ( ! wp_next_scheduled('bpsPro_AFS_check') ) {
		wp_schedule_event( time(), 'minutes_1', 'bpsPro_AFS_check' );
	}
}

add_action('init', 'bpsPro_schedule_AFS_checks');

// AFS Cron: Turns ARQ back On after upgrades and installations have occurred.
// Note: The AFS Cron does a /languages/ folder backup - language translation files are installed "late" "after the fact" after ARQ backup has already occurred.
// If the ARQ FS file exists exit and do not turn ARQ On. An AutoRestore Automation error will be displayed.
function bpsPro_ARQ_upgrader_FailSafe_check() {
global $blog_id;

	if ( is_multisite() && $blog_id != 1 ) {
		exit();
	}
	
	$ARQoptions = get_option('bulletproof_security_options_ARCM');
	$options = get_option('bulletproof_security_options_ARQ_status');
	$ARQ_upgrade = get_option('bulletproof_security_options_ARQ_upgrade');
	$arq_failsafe_file = WP_CONTENT_DIR . '/bps-backup/master-backups/arq-failsafe.txt';
	
	if ( file_exists($arq_failsafe_file) ) {
		@fwrite($fh, '[Internal Usage: AFS Cron: ARQ FS File Exists: ARQ Cron was not turned On | '.$timestamp."]\r\n");
		exit();
	}

	// not used now:  && $ARQ_upgrade['bps_arq_upgrade'] != 'wp_core_automatic_update_major'
	// if there is still a problem with WP Core Major Automatic updates in the future then use this new condition.
	// 13.2: Removing the additional 30 second condition: time() + 30. Since the AFS Cron runs every minute then + 30 is probably not necesary.
	if ( isset($options['bps_arq_status']) && $options['bps_arq_status'] == 'On' && time() > $options['bps_arq_status_timestamp'] && $ARQoptions['bps_autorestore_cron'] == 'Off' ) {
		
		$bpsProLog = WP_CONTENT_DIR . '/bps-backup/logs/http_error_log.txt';
		$gmt_offset = get_option( 'gmt_offset' ) * 3600;
		$timeNow = time();
		$timestamp = date_i18n(get_option('date_format'), strtotime("11/15-1976")) . ' - ' . date_i18n(get_option('time_format'), $timeNow + $gmt_offset);
		$fh = @fopen($bpsProLog, 'a');
		$source = WP_CONTENT_DIR . '/languages';
		$dest = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/languages/';
		
		bpsPro_recursive_copy_languages($source, $dest);

		@fwrite($fh, '[Internal Usage: AFS Cron: Prior ARQ Cron Status: '.$options['bps_arq_status'].' | '.$timestamp."]\r\n");
 		@fwrite($fh, '[Internal Usage: AFS Cron: WP /languages/ folder backup | '.$timestamp."]\r\n");

		if ( $ARQoptions['bps_autorestore_cron_override'] != 'On' ) {	
		
			$ARQ_Options = array(
			'bps_autorestore_cron' 				=> 'On', 
			'bps_autorestore_cron_frequency' 	=> $ARQoptions['bps_autorestore_cron_frequency'], 
			'bps_autorestore_cron_override' 	=> $ARQoptions['bps_autorestore_cron_override'], 
			'bps_autorestore_cron_filecheck' 	=> $ARQoptions['bps_autorestore_cron_filecheck'], 
			'bps_autorestore_cron_forced' 		=> $ARQoptions['bps_autorestore_cron_forced'], 
			'bps_autorestore_cron_end' 			=> $ARQoptions['bps_autorestore_cron_end'] 
			);	

			foreach( $ARQ_Options as $key => $value ) {
				update_option('bulletproof_security_options_ARCM', $ARQ_Options);
			}		
 			
			// Get new updated ARQ Cron DB option values
			$ARQoptions = get_option('bulletproof_security_options_ARCM');
			@fwrite($fh, '[Internal Usage: AFS Cron: ARQ Cron was turned '.$ARQoptions['bps_autorestore_cron'].' | '.$timestamp."]\r\n");			
		}
		
		// IMPORTANT!!! The ARQ Status needs to be cleared here - this SHOULD be '' - NEVER CHANGE THIS value
		$ARQ_status_Options = array( 
		'bps_arq_status' 			=> '', 
		'bps_arq_status_timestamp' 	=> time() 
		);
	
		foreach( $ARQ_status_Options as $key => $value ) {
			update_option('bulletproof_security_options_ARQ_status', $ARQ_status_Options);
		}
	
		if ( $ARQoptions['bps_autorestore_cron_override'] == 'On' ) {
			@fwrite($fh, '[Internal Usage: AFS Cron: ARQ Cron was Not turned back On | '.$timestamp."]\r\n");
			@fwrite($fh, '[Internal Usage: AFS Cron Reason: ARQ Cron Override is turned On | '.$timestamp."]\r\n");
		}		
		
		@fclose($fh);
	}
}

// wp-content languages folder ARQ Backup: Copy wp-content languages folders and files to autorestore backup folder - maintain exact directory structure
// Exclude: /bps-backup, /uploads, /upgrade & /blogs.dir
function bpsPro_recursive_copy_languages($source, $dest) {
	
	if ( ! is_dir( WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/languages' ) ) {
		@mkdir( WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/languages', 0755, true );
		@chmod( WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/languages/', 0755 );
	}

	$source = WP_CONTENT_DIR . '/languages';
	$dest = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/languages/';
	
	if ( is_dir($source) ) {
		$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);	
		
		foreach ( $iterator as $file ) {
			if ( $file->isDir() )	{
				@mkdir($dest.DIRECTORY_SEPARATOR.$iterator->getSubPathName());
			} else {
				copy($file, $dest.DIRECTORY_SEPARATOR.$iterator->getSubPathName());
			}
		}

	} else {
		@copy($source, $dest);
	}
}

// ARQ Cron FailSafe Shutdown - WP Dashboard Notice
// The ARQ cron functions will each exit their scripts if the directories or files do not exist - this is a notice function
function bps_ARQFailSafeShutdown_wp() {

	if ( current_user_can('manage_options') ) {

		$ARQoptions = get_option('bulletproof_security_options_ARCM');
		$ARQ_Status_options = get_option('bulletproof_security_options_ARQ_status');
		
		// These are the same conditions that exist in the AFS Cron failsafe function, but I am adding a 5 minute delay for the time condition.
		// ARQ Status is set to On before file backups occur. ARQ Status is set to a blank value after file backups have occurred.
		// ARQ Status timestamp is set to the current Linux time() (without GMT) before AND after file backups occur.
		if ( isset($ARQ_Status_options['bps_arq_status']) && $ARQ_Status_options['bps_arq_status'] == 'On' && isset($ARQoptions['bps_autorestore_cron']) && $ARQoptions['bps_autorestore_cron'] == 'Off' && isset($ARQ_Status_options['bps_arq_status_timestamp']) && time() > $ARQ_Status_options['bps_arq_status_timestamp'] + 300 ) {
			
			$text = '<div class="update-nag" style="background-color:#dfecf2;border:1px solid #999;font-size:1em;font-weight:600;padding:2px 5px;margin-top:2px;-moz-border-radius-topleft:3px;-webkit-border-top-left-radius:3px;-khtml-border-top-left-radius:3px;border-top-left-radius:3px;-moz-border-radius-topright:3px;-webkit-border-top-right-radius:3px;-khtml-border-top-right-radius:3px;border-top-right-radius:3px;-webkit-box-shadow: 3px 3px 5px -1px rgba(153,153,153,0.7);-moz-box-shadow: 3px 3px 5px -1px rgba(153,153,153,0.7);box-shadow: 3px 3px 5px -1px rgba(153,153,153,0.7);"><font color="blue">'.__('ARQ Cron FailSafe Shutdown Notice', 'bulletproof-security').'</font><br>'.__('The BPS Pro ', 'bulletproof-security').'<a href="'.admin_url( 'admin.php?page=bulletproof-security/admin/wizard/wizard.php' ).'">'.__('Pre-Installation Wizard and Setup Wizard', 'bulletproof-security').'</a>'.__(' need to be run again.', 'bulletproof-security').'</div>';
			echo $text;		
		}

		//$ARQoptions = get_option('bulletproof_security_options_ARCM');
		//$ARQRoot1 = WP_CONTENT_DIR . '/bps-backup/autorestore/root-files/auto_.htaccess';
		//$ARQRoot2 = WP_CONTENT_DIR . '/bps-backup/autorestore/root-files/wp-config.php';
		$ARQRoot3 = WP_CONTENT_DIR . '/bps-backup/autorestore/root-files/index.php';
		$ARQDirWPadmin =  WP_CONTENT_DIR . '/bps-backup/autorestore/wp-admin/';
		$ARQDirWPincludes =  WP_CONTENT_DIR . '/bps-backup/autorestore/wp-includes/';
		$ARQDirWPcontent =  WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/';
		$ARQFSWPadmin =  WP_CONTENT_DIR . '/bps-backup/autorestore/wp-admin/admin.php';
		$ARQFSWPincludes =  WP_CONTENT_DIR . '/bps-backup/autorestore/wp-includes/functions.php';
		$ARQFSWPcontent =  WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/index.php';
		$bpsClassFile = WP_PLUGIN_DIR . '/bulletproof-security/includes/class.php';
		$bpsClassFileARQMB = WP_CONTENT_DIR . '/bps-backup/master-backups/class.php';	
	
		if ( isset($ARQoptions['bps_autorestore_cron']) && $ARQoptions['bps_autorestore_cron'] == 'On' ) {
		
			if ( file_exists($bpsClassFileARQMB) && @filemtime($bpsClassFileARQMB) != @filemtime($bpsClassFile) ) {
				$text = '<div class="update-nag" style="background-color:#dfecf2;border:1px solid #999;font-size:1em;font-weight:600;padding:2px 5px;margin-top:2px;-moz-border-radius-topleft:3px;-webkit-border-top-left-radius:3px;-khtml-border-top-left-radius:3px;border-top-left-radius:3px;-moz-border-radius-topright:3px;-webkit-border-top-right-radius:3px;-khtml-border-top-right-radius:3px;border-top-right-radius:3px;-webkit-box-shadow: 3px 3px 5px -1px rgba(153,153,153,0.7);-moz-box-shadow: 3px 3px 5px -1px rgba(153,153,153,0.7);box-shadow: 3px 3px 5px -1px rgba(153,153,153,0.7);"><font color="blue">'.__('ARQ Cron FailSafe Shutdown Notice', 'bulletproof-security').'</font><br>'.__('This FailSafe notice should go away automatically by clicking anywhere in your WordPress Dashboard.', 'bulletproof-security').'<br>'.__('If this FailSafe notice does not go away automatically then go to the BPS Pro Setup Menu and run the Pre-Installation Wizard and the Setup Wizard again.', 'bulletproof-security').'</div>';
				echo $text;
			}
		
			if ( ! file_exists($ARQRoot3) ) {
				$text = '<div class="update-nag" style="background-color:#dfecf2;border:1px solid #999;font-size:1em;font-weight:600;padding:2px 5px;margin-top:2px;-moz-border-radius-topleft:3px;-webkit-border-top-left-radius:3px;-khtml-border-top-left-radius:3px;border-top-left-radius:3px;-moz-border-radius-topright:3px;-webkit-border-top-right-radius:3px;-khtml-border-top-right-radius:3px;border-top-right-radius:3px;-webkit-box-shadow: 3px 3px 5px -1px rgba(153,153,153,0.7);-moz-box-shadow: 3px 3px 5px -1px rgba(153,153,153,0.7);box-shadow: 3px 3px 5px -1px rgba(153,153,153,0.7);">'.__('ARQ Cron FailSafe Shutdown: File Backup is Required before the ARQ Cron will run.', 'bulletproof-security').'<br><a href="'.admin_url( 'admin.php?page=bulletproof-security/admin/autorestore/autorestore.php' ).'">'.__('Click Here', 'bulletproof-security').'</a>'.__(' to go to AutoRestore to Backup your Root Files.', 'bulletproof-security').'</div>';
				echo $text;	
			}
			
			if ( ! is_dir($ARQDirWPadmin) || ! file_exists($ARQFSWPadmin) ) {	
				$text = '<div class="update-nag" style="background-color:#dfecf2;border:1px solid #999;font-size:1em;font-weight:600;padding:2px 5px;margin-top:2px;-moz-border-radius-topleft:3px;-webkit-border-top-left-radius:3px;-khtml-border-top-left-radius:3px;border-top-left-radius:3px;-moz-border-radius-topright:3px;-webkit-border-top-right-radius:3px;-khtml-border-top-right-radius:3px;border-top-right-radius:3px;-webkit-box-shadow: 3px 3px 5px -1px rgba(153,153,153,0.7);-moz-box-shadow: 3px 3px 5px -1px rgba(153,153,153,0.7);box-shadow: 3px 3px 5px -1px rgba(153,153,153,0.7);">'.__('ARQ Cron FailSafe Shutdown: File Backup is Required before the ARQ Cron will run.', 'bulletproof-security').'<br><a href="'.admin_url( 'admin.php?page=bulletproof-security/admin/autorestore/autorestore.php' ).'">'.__('Click Here', 'bulletproof-security').'</a>'.__(' to go to AutoRestore to Backup your wp-admin Files.', 'bulletproof-security').'</div>';
				echo $text;
			}
			
			if ( ! is_dir($ARQDirWPincludes) || ! file_exists($ARQFSWPincludes) ) {	
				$text = '<div class="update-nag" style="background-color:#dfecf2;border:1px solid #999;font-size:1em;font-weight:600;padding:2px 5px;margin-top:2px;-moz-border-radius-topleft:3px;-webkit-border-top-left-radius:3px;-khtml-border-top-left-radius:3px;border-top-left-radius:3px;-moz-border-radius-topright:3px;-webkit-border-top-right-radius:3px;-khtml-border-top-right-radius:3px;border-top-right-radius:3px;-webkit-box-shadow: 3px 3px 5px -1px rgba(153,153,153,0.7);-moz-box-shadow: 3px 3px 5px -1px rgba(153,153,153,0.7);box-shadow: 3px 3px 5px -1px rgba(153,153,153,0.7);">'.__('ARQ Cron FailSafe Shutdown: File Backup is Required before the ARQ Cron will run.', 'bulletproof-security').'<br><a href="'.admin_url( 'admin.php?page=bulletproof-security/admin/autorestore/autorestore.php' ).'">'.__('Click Here', 'bulletproof-security').'</a>'.__(' to go to AutoRestore to Backup your wp-includes Files.', 'bulletproof-security').'</div>';
				echo $text;
			}
		
			if ( ! is_dir($ARQDirWPcontent) || ! file_exists($ARQFSWPcontent) ) {	
				$text = '<div class="update-nag" style="background-color:#dfecf2;border:1px solid #999;font-size:1em;font-weight:600;padding:2px 5px;margin-top:2px;-moz-border-radius-topleft:3px;-webkit-border-top-left-radius:3px;-khtml-border-top-left-radius:3px;border-top-left-radius:3px;-moz-border-radius-topright:3px;-webkit-border-top-right-radius:3px;-khtml-border-top-right-radius:3px;border-top-right-radius:3px;-webkit-box-shadow: 3px 3px 5px -1px rgba(153,153,153,0.7);-moz-box-shadow: 3px 3px 5px -1px rgba(153,153,153,0.7);box-shadow: 3px 3px 5px -1px rgba(153,153,153,0.7);">'.__('ARQ Cron FailSafe Shutdown: File Backup is Required before the ARQ Cron will run.', 'bulletproof-security').'<br><a href="'.admin_url( 'admin.php?page=bulletproof-security/admin/autorestore/autorestore.php' ).'">'.__('Click Here', 'bulletproof-security').'</a>'.__(' to go to AutoRestore to Backup your wp-content Files.', 'bulletproof-security').'<br>'.__('Check that the /wp-content/index.php file exists. If it does not exist then upload a new WP index.php file to your wp-content folder.', 'bulletproof-security').'</div>';
				echo $text;
			}	
		}
	}
}
add_action('admin_notices', 'bps_ARQFailSafeShutdown_wp');

// WP Automatic Update: This function is called in the ARQ Fire function.
// IMPORTANT!!!: pre and post upgrader filters are not used for WP Core Manual Bulk updates.
// Turn Off ARQ, Backup Files, Turn ARQ back On, lock index.php & wp-blog-header.php, clear php error log alert, write log entries.
// If someone is using automatic updates for plugins and themes then all new files will be logged IF a WP Core update is being installed at the same time.
// FailSafe: Each ARQ Cron function will not run if BPS DB time stamp is less than the actual file time stamp. The reason for
// that is WP files would be quarantined right after the automatic update occurs.
// Updates the BPS DB timestamp & wp version after it has been run once so that the BPS DB timestamp is greater than the file timestamp by 15 minutes.
// The actual last modified time of the version.php file is not important from a now vs a past point of view.
// Internal Usage: BPS DB Value filemtime +15 sets the filemtime 15 minutes into the future so in order to do a clean retest wait 15 minutes.
// 12.6: Added ARQ Status & ARQ Upgrade DB options.
// Note: Cannot reliably use the the WP Upgrader pre_install and post_install filters for WP Core Automatic, Remote and web host forced WP upgrades.
// 15.2: Rebuilt this function. Fixed a critical bug for WP Automatic Updates that had new folders. Folders where not being created in ARQ Backup.
function bpsPro_ARQ_wp_autoupdate() {
global $wp_version;
	
	$Autoupdateoptions = get_option('bulletproof_security_options_wp_version');
	$versionphp = ABSPATH . 'wp-includes/version.php';
	$versionphp_arq = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-includes/version.php';
	$gmt_offset = get_option( 'gmt_offset' ) * 3600;
	$last_modified_time_versionphp = date("F d Y H:i", filemtime($versionphp) + $gmt_offset);
	$last_modified_time_arq_versionphp = date("F d Y H:i", filemtime($versionphp_arq) + $gmt_offset);
	
	// The ARQ version.php file timestamp will be greater than or equal to (new/newer) the actual Live file timestamp if a file backup has occurred.
	// Or if additional ARQ file backups have occurred after a WP update. Each time an ARQ file backup occurs the last modified time of the file is updated.
	if ( strtotime($last_modified_time_arq_versionphp) >= strtotime($last_modified_time_versionphp) ) {
		return;		
	}	

	// The ARQ version.php file timestamp will be less than (older) the actual Live file time stamp if a file backup has NOT occurred.
	if ( strtotime($last_modified_time_arq_versionphp) < strtotime($last_modified_time_versionphp) ) {
		set_time_limit(300);
		
		$FlockOptions = get_option('bulletproof_security_options_flock');
		$ARQoptions = get_option('bulletproof_security_options_ARCM');
		$bpsProLog = WP_CONTENT_DIR . '/bps-backup/logs/http_error_log.txt';
		$timeNow = time();
		$timestamp = date_i18n(get_option('date_format'), strtotime("11/15-1976")) . ' - ' . date_i18n(get_option('time_format'), $timeNow + $gmt_offset);
		$index = ABSPATH . 'index.php';
		$wpblogheader = ABSPATH . 'wp-blog-header.php';
		//$last_modified_time_versionphpPlus5 = date("F d Y H:i", filemtime($versionphp) + $gmt_offset + 300);		
		$fh = @fopen($bpsProLog, 'a');
		
		$ARQ_status_Options = array( 
		'bps_arq_status' 			=> $ARQoptions['bps_autorestore_cron'], 
		'bps_arq_status_timestamp' 	=> time() 
		);
			
		foreach( $ARQ_status_Options as $key => $value ) {
			update_option('bulletproof_security_options_ARQ_status', $ARQ_status_Options);
		}
		
		$ARQ_status = get_option('bulletproof_security_options_ARQ_status');
			
		if ( $ARQ_status['bps_arq_status'] == 'Off' ) {
			@fwrite($fh, "\r\n". '[WP Automatic|Remote|Forced Update: ARQ Cron will not be turned back On | '.$timestamp."]\r\n");
			@fwrite($fh, '[Reason: ARQ Cron was turned Off prior to the WP Update | '.$timestamp."]\r\n");
			
		} else {
			@fwrite($fh, "\r\n". '[WP Automatic|Remote|Forced Update: Prior ARQ Cron Status: '.$ARQ_status['bps_arq_status'].' | '.$timestamp."]\r\n");
			@fwrite($fh, "[WP Automatic|Remote|Forced Update: ARQ Cron was turned Off | $timestamp]\r\n");
		}		
		
		// Turn Off the AutoRestore Cron
		$ARQoptions2 = ! $ARQoptions['bps_autorestore_cron_frequency'] ? '2' : $ARQoptions['bps_autorestore_cron_frequency'];
		$ARQoptions3 = ! $ARQoptions['bps_autorestore_cron_forced'] ? '1' : $ARQoptions['bps_autorestore_cron_forced'];
		$ARQoptions4 = ! $ARQoptions['bps_autorestore_cron_override'] ? 'Off' : $ARQoptions['bps_autorestore_cron_override'];
		$ARQoptions5 = ! $ARQoptions['bps_autorestore_cron_filecheck'] ? 'Off' : $ARQoptions['bps_autorestore_cron_filecheck'];
		$ARQoptions6 = ! $ARQoptions['bps_autorestore_cron_end'] ? '' : $ARQoptions['bps_autorestore_cron_end'];		
		
		$ARQ_Options = array(
		'bps_autorestore_cron' 				=> 'Off', 
		'bps_autorestore_cron_frequency' 	=> $ARQoptions2, 
		'bps_autorestore_cron_forced' 		=> $ARQoptions3, 
		'bps_autorestore_cron_override' 	=> $ARQoptions4, 
		'bps_autorestore_cron_filecheck' 	=> $ARQoptions5, 
		'bps_autorestore_cron_end' 			=> $ARQoptions6
		);	

		foreach( $ARQ_Options as $key => $value ) {
			update_option('bulletproof_security_options_ARCM', $ARQ_Options);
		}
		
		// Note: Using a require or include for the swizard-arq-functions.php file will not work.
		// Root Files Backup
		if ( ! is_dir( WP_CONTENT_DIR . '/bps-backup/autorestore/root-files' ) ) {
			mkdir( WP_CONTENT_DIR . '/bps-backup/autorestore/root-files', 0755, true );
			@chmod( WP_CONTENT_DIR . '/bps-backup/autorestore/root-files/', 0755 );
		}

		$Rootsource = rtrim(ABSPATH, "/");
		$Rootdest = WP_CONTENT_DIR . '/bps-backup/autorestore/root-files';
						
		if ( is_dir($Rootsource) ) {
			$Rootiterator = new DirectoryIterator($Rootsource);
	
			foreach ( $Rootiterator as $Rootfile ) {
				try {
					if ( $Rootfile->isFile() ) {
					
						if ( $Rootfile->getSize() < 5120000 ) {
						
							if ( $Rootfile->getFilename() == '.htaccess' ) {
								copy($Rootfile->getPathname(), $Rootdest.DIRECTORY_SEPARATOR.'auto_'.$Rootfile->getFilename());
							} else {
								copy($Rootfile->getPathname(), $Rootdest.DIRECTORY_SEPARATOR.$Rootfile->getFilename());
							}
						}
					}
				} catch (RuntimeException $e) {}
			}
		}

		@fwrite($fh, "[WP Automatic|Remote|Forced Update: ARQ Root File Backup | $timestamp]\r\n");
		
		// wp-admin Files Backup
		if ( ! is_dir( WP_CONTENT_DIR . '/bps-backup/autorestore/wp-admin' ) ) {
			mkdir( WP_CONTENT_DIR . '/bps-backup/autorestore/wp-admin', 0755, true );
			@chmod( WP_CONTENT_DIR . '/bps-backup/autorestore/wp-admin/', 0755 );
		}		
		
		$wpadmin_source = ABSPATH . 'wp-admin';
		$wpadmin_dest = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-admin/';
		$htaccessARQ = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-admin/.htaccess';
		$htaccessARQRename = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-admin/wpadmin.htaccess';		

		if ( is_dir($wpadmin_source) ) {
			$wpadmin_iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($wpadmin_source), RecursiveIteratorIterator::SELF_FIRST);	
			
			foreach ( $wpadmin_iterator as $wpadmin_file ) {
				if ( $wpadmin_file->isDir() )	{
					@mkdir($wpadmin_dest.DIRECTORY_SEPARATOR.$wpadmin_iterator->getSubPathName());
				} else {
					copy($wpadmin_file, $wpadmin_dest.DIRECTORY_SEPARATOR.$wpadmin_iterator->getSubPathName());
				}
			}
	
		} else {
			copy($wpadmin_source, $wpadmin_dest);
		}
		
		if ( file_exists($htaccessARQ) ) {
			rename($htaccessARQ, $htaccessARQRename);
		}

		@fwrite($fh, "[WP Automatic|Remote|Forced Update: ARQ wp-admin File Backup | $timestamp]\r\n");
		
		// wp-content Files Backup					
		if ( ! is_dir( WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content' ) ) {
			mkdir( WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content', 0755, true );
			@chmod( WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/', 0755 );
		}

		$wpcontent_source = WP_CONTENT_DIR;
		$wpcontent_dest = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/';					
					
		if ( is_dir($wpcontent_source) ) {
			$wpcontent_dirItr    = new RecursiveDirectoryIterator($wpcontent_source);		
			$wpcontent_filterItr = new BPSCopyWPCRecursiveFilterIterator($wpcontent_dirItr);
			$wpcontent_iterator  = new RecursiveIteratorIterator($wpcontent_filterItr, RecursiveIteratorIterator::SELF_FIRST); 
			
			foreach ( $wpcontent_iterator as $wpcontent_file ) {
				if ( $wpcontent_file->isDir() ) {
					@mkdir($wpcontent_dest.DIRECTORY_SEPARATOR.$wpcontent_iterator->getSubPathName());
				} else {
					
					if ( $wpcontent_file->getSize() < 5120000 ) {
						@copy($wpcontent_file, $wpcontent_dest.DIRECTORY_SEPARATOR.$wpcontent_iterator->getSubPathName());
					}
				}
			}

		} else {
			@copy($wpcontent_source, $wpcontent_dest);
		}

		@fwrite($fh, "[WP Automatic|Remote|Forced Update: ARQ wp-content File Backup | $timestamp]\r\n");

		// wp-includes Files Backup					
		if ( ! is_dir( WP_CONTENT_DIR . '/bps-backup/autorestore/wp-includes' ) ) {
			mkdir( WP_CONTENT_DIR . '/bps-backup/autorestore/wp-includes', 0755, true );
			@chmod( WP_CONTENT_DIR . '/bps-backup/autorestore/wp-includes/', 0755 );
		}

		$wpincludes_source = ABSPATH . 'wp-includes';
		$wpincludes_dest = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-includes/';					
					
		if ( is_dir($wpincludes_source) ) {
			$wpincludes_iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($wpincludes_source), RecursiveIteratorIterator::SELF_FIRST);
			
			foreach ( $wpincludes_iterator as $wpincludes_file ) {
				if ( $wpincludes_file->isDir() )	{
					@mkdir($wpincludes_dest.DIRECTORY_SEPARATOR.$wpincludes_iterator->getSubPathName());
				} else {
					copy($wpincludes_file, $wpincludes_dest.DIRECTORY_SEPARATOR.$wpincludes_iterator->getSubPathName());
				}
			}

		} else {
			copy($wpincludes_source, $wpincludes_dest);
		}

		@fwrite($fh, "[WP Automatic|Remote|Forced Update: ARQ wp-includes File Backup | $timestamp]\r\n");

		// Lock the index.php and wp-blog-header.php files if they were previously locked		
		$fl_override_options = get_option('bulletproof_security_options_flock_eol');
		
		if ( $fl_override_options['bps_file_lock_override'] == 'On' ) {

			if ( $FlockOptions['bps_lock_index_php'] == 'yes' ) {
				@chmod($index, 0400);
				
				@fwrite($fh, "[WP Automatic|Remote|Forced Update: index.php file locked: $timestamp]\r\n");
			}
			if ( $FlockOptions['bps_lock_wpblog_header'] == 'yes' ) {
				@chmod($wpblogheader, 0400);
				
				@fwrite($fh, "[WP Automatic|Remote|Forced Update: wp-blog-header.php file locked | $timestamp]\r\n");
			}		
		}
		
		// Sync php error log alert time
		$optionsElogLocation = get_option('bulletproof_security_options2');
		$Elogfile = $optionsElogLocation['bps_error_log_location'];
		$last_modified_time_file = date("F d Y H:i:s", filemtime($Elogfile) + $gmt_offset);
		$Elog_time_db = 'bulletproof_security_options_elog';
				
		$BPS_db_Elog = array( 'bps_error_log_date_mod' => $last_modified_time_file );	
				
		foreach( $BPS_db_Elog as $key => $value ) {
			update_option('bulletproof_security_options_elog', $BPS_db_Elog);
		}
		
 		@fwrite($fh, "[WP Automatic|Remote|Forced Update: PHP Error Log timestamp synchronized | $timestamp]\r\n");

		// Sync the wp version & set/save the last modified file time of version.php file + 15 minutes to DB
		$WPver_Options = array( 
		'bps_wp_version' 					=> $wp_version, 
		'bps_wp_version_last_modified_time' => date("F d Y H:i", filemtime($versionphp) + $gmt_offset + 900) 
		);	
	
		foreach ( $WPver_Options as $key => $value ) {
			update_option('bulletproof_security_options_wp_version', $WPver_Options);
		}

		@fwrite($fh, "\r\n[WP Automatic|Remote|Forced Update: WP Update Time | $timestamp]\r\n");
		@fwrite($fh, '[WP Automatic|Remote|Forced Update: WP Version Synchronized' . "]\r\n");
		@fwrite($fh, '[WP Automatic|Remote|Forced Update: Last Modified Time | ' . $last_modified_time_versionphp . "]\r\n");
		@fwrite($fh, '[WP Automatic|Remote|Forced Update: BPS DB Value filemtime +15 | ' . date("F d Y H:i", filemtime($versionphp) + $gmt_offset + 900) . "]\r\n");

		// Turn AutoRestore back On if ARQ Cron Override is not set to On 
		if ( $ARQoptions['bps_autorestore_cron_override'] != 'On' ) {		
		
			$ARQoptions2 = ! $ARQoptions['bps_autorestore_cron_frequency'] ? '2' : $ARQoptions['bps_autorestore_cron_frequency'];
			$ARQoptions3 = ! $ARQoptions['bps_autorestore_cron_forced'] ? '1' : $ARQoptions['bps_autorestore_cron_forced'];
			$ARQoptions4 = ! $ARQoptions['bps_autorestore_cron_override'] ? 'Off' : $ARQoptions['bps_autorestore_cron_override'];
			$ARQoptions5 = ! $ARQoptions['bps_autorestore_cron_filecheck'] ? 'Off' : $ARQoptions['bps_autorestore_cron_filecheck'];
			$ARQoptions6 = ! $ARQoptions['bps_autorestore_cron_end'] ? '' : $ARQoptions['bps_autorestore_cron_end'];		
		
			$ARQ_Options = array(
			'bps_autorestore_cron' 				=> 'On', 
			'bps_autorestore_cron_frequency' 	=> $ARQoptions2, 
			'bps_autorestore_cron_forced' 		=> $ARQoptions3, 
			'bps_autorestore_cron_override' 	=> $ARQoptions4, 
			'bps_autorestore_cron_filecheck' 	=> $ARQoptions5, 
			'bps_autorestore_cron_end' 			=> $ARQoptions6
			);	

			// IMPORTANT!!! The ARQ Status needs to be cleared here - this SHOULD be '' - NEVER CHANGE THIS value
			$ARQ_status_Options = array( 
			'bps_arq_status' 			=> '', 
			'bps_arq_status_timestamp' 	=> time() 
			);
	
			foreach( $ARQ_status_Options as $key => $value ) {
				update_option('bulletproof_security_options_ARQ_status', $ARQ_status_Options);
			}
				
			foreach( $ARQ_Options as $key => $value ) {
				update_option('bulletproof_security_options_ARCM', $ARQ_Options);
			}					
			
			// Only use minor and major in the future if needed if there is still a problem with Major WP Automatic Updates
			// wp_core_automatic_update_minor > wp_core_automatic_update_major
			$BPS_arq_upgrade = array( 'bps_arq_upgrade' => 'wp_core_automatic_update' );	
	
			foreach ( $BPS_arq_upgrade as $key => $value ) {
				update_option('bulletproof_security_options_ARQ_upgrade', $BPS_arq_upgrade);
			}

			@fwrite($fh, '[WP Automatic|Remote|Forced Update: Backup performed | '.$timestamp."]\r\n");
 			@fwrite($fh, "[WP Automatic|Remote|Forced Update: ARQ Cron was turned On | $timestamp]\r\n");
		}
	 	@fclose($fh);	
	}
}

// IMPORTANT: AJAX is fired first and then the pre install filter.
// Notes: Checking DB option values here does not work for AJAX since that would be after the fact since AJAX is fired first.
// Checking DB option values works for Automatic Plugin or Theme updates.
// Shiny, Manual, Bulk and Automatic Plugin and Theme installations and updates trigger the pre upgrader filter. WP Core does not.
// $response is boolean: True or False. $hook_extra is an array.
function bpsPro_arq_automation_plugins_themes_pre_install( $response, $hook_extra ) {
	
	if ( ! is_wp_error( $response ) ) {	
	
		$bpsProLog = WP_CONTENT_DIR . '/bps-backup/logs/http_error_log.txt';
		$timeNow = time();
		$gmt_offset = get_option( 'gmt_offset' ) * 3600;
		$timestamp = date_i18n(get_option('date_format'), strtotime("11/15-1976")) . ' - ' . date_i18n(get_option('time_format'), $timeNow + $gmt_offset);
		$fh = @fopen($bpsProLog, 'a');

		$ARQoptions = get_option('bulletproof_security_options_ARCM');
		
		// IMPORTANT: Anything sent from AJAX Actions will see the previous state of the ARQ Cron DB option value even before AJAX is triggered.
		// An Automatic Plugin or Theme update will see the current ARQ Cron DB option value, which should be On and set the ARQ Status.
		if ( $ARQoptions['bps_autorestore_cron'] == 'On' ) {
			
			$ARQ_status_Options = array( 
			'bps_arq_status' 			=> $ARQoptions['bps_autorestore_cron'], 
			'bps_arq_status_timestamp' 	=> time() 
			);
			
			foreach( $ARQ_status_Options as $key => $value ) {
				update_option('bulletproof_security_options_ARQ_status', $ARQ_status_Options);
			}		
			
			// Spacer for Automatic Plugin or Theme Updates
			@fwrite($fh, "\r\n\r\n");
		}

		$ARQ_status = get_option('bulletproof_security_options_ARQ_status');
		
		@fwrite($fh, '[Internal Usage: upgrader_pre_install Filter: Triggered | '.$timestamp."]\r\n");
		@fwrite($fh, '[Internal Usage: upgrader_pre_install Filter: Prior ARQ Cron Status: '.$ARQ_status['bps_arq_status'].' | '.$timestamp."]\r\n");

		// FailSafe in case AJAX fails and does not turn ARQ Off and for older WP versions that do not call AJAX.
		// Also turns ARQ Off for WP Automatic Updates for Plugins or Themes
		$BPS_Options = array(
		'bps_autorestore_cron' 				=> 'Off', 
		'bps_autorestore_cron_frequency' 	=> $ARQoptions['bps_autorestore_cron_frequency'], 
		'bps_autorestore_cron_override' 	=> $ARQoptions['bps_autorestore_cron_override'], 
		'bps_autorestore_cron_filecheck' 	=> $ARQoptions['bps_autorestore_cron_filecheck'], 
		'bps_autorestore_cron_forced' 		=> $ARQoptions['bps_autorestore_cron_forced'], 
		'bps_autorestore_cron_end' 			=> $ARQoptions['bps_autorestore_cron_end'] 
		);	

		foreach ( $BPS_Options as $key => $value ) {
			update_option('bulletproof_security_options_ARCM', $BPS_Options);
		}
		
		// Note: This is just for Security Logging purposes to get the Total Log Entry by Type.
		@fwrite($fh, '[Internal Usage: upgrader_pre_install Filter: ARQ Cron was turned Off | '.$timestamp."]\r\n");
		@fclose($fh);
	}
	return $response;
}

add_filter( 'upgrader_pre_install', 'bpsPro_arq_automation_plugins_themes_pre_install', 10, 2 );

// Copy & Touch master backup class.php file - overwrite class.php and synchronize last modifed time
// The ARQ Cron will not run if the timestamps do not match. Does not need to be GMT time
// Prevents wp-content files from being quarantined that have ARQ wp-content folder exclude rules in the class.php file.
function bpsPro_class_backup_file_copy() {
	
	$bpsClassFile = WP_PLUGIN_DIR . '/bulletproof-security/includes/class.php';
	$bpsClassFileARQMB = WP_CONTENT_DIR . '/bps-backup/master-backups/class.php';
	$bpsClassFileARQ = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/plugins/bulletproof-security/includes/class.php';
	
	if ( file_exists($bpsClassFileARQMB) && filemtime($bpsClassFileARQMB) != filemtime($bpsClassFile) ) {
		
		$bpsClassFileARQMB_contents = @file_get_contents($bpsClassFileARQMB);
	
		if ( preg_match( '/http:\/\/api.ait-pro.com/', $bpsClassFileARQMB_contents ) ) {
			
			$stringReplace = @file_get_contents($bpsClassFileARQMB);
			$stringReplace = preg_replace('/http:\/\/api.ait-pro.com/', "https://api.ait-pro.com", $stringReplace);
			file_put_contents($bpsClassFileARQMB, $stringReplace);
		}
		
		@copy($bpsClassFileARQMB, $bpsClassFile);
		@copy($bpsClassFileARQMB, $bpsClassFileARQ);
		@touch($bpsClassFile);
		@touch($bpsClassFileARQMB);
	}
}

// Bulk, Shiny Plugin, Theme & WP Automatic Updates for plugins and themes automatic updates. bpsPro_ARQ_wp_autoupdate() handles WP Core automatic updates.
// wp-content folder file backup, Turn ARQ On and write log entries.
// $response is boolean: True or False. $hook_extra is an array. $result is an array.
// Flow|Sequence: Update or Install Now button clicked > AJAX Action Function Triggered & ARQ Turned Off > upgrader_pre_install Filter Triggered - ARQ Turned Off FailSafe > 
// upgrader_post_install Filter Triggered - performs backup > AFS Cron FailSafe - Turns ARQ On unless the ARQ Cron Override option is On. 
function bpsPro_arq_automation_plugins_themes_post_install( $response, $hook_extra, $result ) {
global $wp_version;	
	
	if ( ! is_wp_error( $response ) ) {	
	
		$bpsProLog = WP_CONTENT_DIR . '/bps-backup/logs/http_error_log.txt';
		$timeNow = time();
		$gmt_offset = get_option( 'gmt_offset' ) * 3600;
		$timestamp = date_i18n(get_option('date_format'), strtotime("11/15-1976")) . ' - ' . date_i18n(get_option('time_format'), $timeNow + $gmt_offset);
		$fh = @fopen($bpsProLog, 'a');
		
		@fwrite($fh, '[Internal Usage: upgrader_post_install Filter: Triggered | '.$timestamp."]\r\n");
		
		if ( ! is_dir( WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content' ) ) {
			mkdir( WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content', 0755, true );
			@chmod( WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/', 0755 );
		}

		bpsPro_class_backup_file_copy();

		$source = WP_CONTENT_DIR;
		$dest = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/';
		$bps_wpcontent_dir = str_replace( ABSPATH, '', WP_CONTENT_DIR);	
	
		if ( is_dir($source) ) {
			$dirItr    = new RecursiveDirectoryIterator($source);		
			$filterItr = new BPSCopyWPCRecursiveFilterIterator($dirItr);
			$iterator  = new RecursiveIteratorIterator($filterItr, RecursiveIteratorIterator::SELF_FIRST); 
		
			foreach ( $iterator as $file ) {
				if ( $file->isDir() ) {
					@mkdir($dest.DIRECTORY_SEPARATOR.$iterator->getSubPathName());
				} else {
					@copy($file, $dest.DIRECTORY_SEPARATOR.$iterator->getSubPathName());
				}
			}
	
		} else {
			@copy($source, $dest);
		}	
		
		@fwrite($fh, '[Internal Usage: upgrader_post_install Filter: Backup performed | '.$timestamp."]\r\n");
		
		// 12.6: BugFix: Delete the arq-failsafe.txt file after the wp-content folder has been backed up.
		// Used in all Manual Plugin and Theme Bulk Updates
		$ARQoptions = get_option('bulletproof_security_options_ARCM');
		$arq_failsafe_file = WP_CONTENT_DIR . '/bps-backup/master-backups/arq-failsafe.txt';
			
		if ( file_exists($arq_failsafe_file) ) {
			if ( $ARQoptions['bps_autorestore_cron_override'] != 'On' ) {
			
				unlink($arq_failsafe_file);
				@fwrite($fh, '[Internal Usage: upgrader_post_install Filter: ARQ FS File Deletion: Successful | '.$timestamp."]\r\n");
			}
			if ( $ARQoptions['bps_autorestore_cron_override'] == 'On' ) {
			
				@fwrite($fh, '[Internal Usage: upgrader_post_install Filter: ARQ Cron Override On: ARQ FS File Deletion: No | '.$timestamp."]\r\n");
			}
		}
		
		// Sync php error log alert time
		$gmt_offset = get_option( 'gmt_offset' ) * 3600;
		$optionsElogLocation = get_option('bulletproof_security_options2');
		$Elogfile = $optionsElogLocation['bps_error_log_location'];
		$last_modified_time_file = date("F d Y H:i:s", filemtime($Elogfile) + $gmt_offset);
		$Elog_time_db = 'bulletproof_security_options_elog';
				
		$BPS_db_Elog = array( 'bps_error_log_date_mod' => $last_modified_time_file );	
				
		foreach( $BPS_db_Elog as $key => $value ) {
			update_option('bulletproof_security_options_elog', $BPS_db_Elog);
		}

		@fclose($fh);
	}
	return $result;
}

add_filter( 'upgrader_post_install', 'bpsPro_arq_automation_plugins_themes_post_install', 10, 3 );

// AutoRestore|Quarantine Master ARQ Fire loads each folder Cron & saves a new timestamp if forced == 2
// Standard WP Cron = bps_autorestore_cron_forced == '1'
// WP Cron with Time Restriction = bps_autorestore_cron_forced == '2'
// Code Line 105: add_action('bpsPro_AutoRestore_check', 'bps_Master_ARQ_Fire');
// 12.6: Condition correction for "cron forced".
// 12.6: ARQ FailSafe for WP Manual Updates: arq-failsafe.txt file check added
// 12.6: Moved bpsPro_ARQ_wp_autoupdate(), Cron Override, ARCM get_option & version.php last mod file time code/conditions here
// 13.2: Removed +30 seconds from the AFS Cron so adding another FailSafe condition here
function bps_Master_ARQ_Fire() {
$options = get_option('bulletproof_security_options_ARCM');	
$arq_failsafe_file = WP_CONTENT_DIR . '/bps-backup/master-backups/arq-failsafe.txt';

	if ( ! get_option('bulletproof_security_options_ARCM' ) ) {
		exit();
	}

	if ( file_exists($arq_failsafe_file) ) {
		exit();
	}

	// ARQ Cron Override is On - do not check/monitor files
	if ( $options['bps_autorestore_cron_override'] == 'On' ) {
		exit();	
	}

	// AFS Cron FailSafe: Prevents the ARQ Cron functions from running during Core, plugin or theme updates.
	// Adds a 1 minute buffer before & after the AFS Cron has fired.
	$ARQ_status = get_option('bulletproof_security_options_ARQ_status');

	if ( time() < $ARQ_status['bps_arq_status_timestamp'] + 60 ) {
		exit;
	}

	// This condition is correct for the bpsPro_ARQ_wp_autoupdate() function. The bpsPro_ARQ_wp_autoupdate() function turns ARQ Off before backing up files.
	if ( $options['bps_autorestore_cron'] == 'On' ) {

		// WordPress Automatic, Remote and Forced Updates
		bpsPro_ARQ_wp_autoupdate();
		
		$Autoupdateoptions = get_option('bulletproof_security_options_wp_version');
		$versionphp = ABSPATH . 'wp-includes/version.php';
		$versionphp_arq = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-includes/version.php';
		$gmt_offset = get_option( 'gmt_offset' ) * 3600;	
		$last_modified_time_versionphp = date("F d Y H:i", filemtime($versionphp) + $gmt_offset);
		$last_modified_time_arq_versionphp = date("F d Y H:i", filemtime($versionphp_arq) + $gmt_offset);
		$timeNow_plus_2 = time() + $gmt_offset + 120;
						
		// The ARQ version.php file timestamp will be less than (older) the actual Live file time stamp if a file backup has NOT occurred.
		if ( strtotime($last_modified_time_arq_versionphp) < strtotime($last_modified_time_versionphp) ) {
			exit();		
		}

		// Add a 2 minute delay/buffer to make sure all files were backed up in the bpsPro_ARQ_wp_autoupdate() function.
		if ( $timeNow_plus_2 > strtotime($last_modified_time_arq_versionphp) ) {

			if ( $options['bps_autorestore_cron_forced'] == '1' ) {
	
				bps_ARQ_Root();
				bps_ARQ_wpadmin();
				bps_ARQ_wpincludes();
				bps_ARQ_wpcontent();
				bps_ARQ_addedFiles();
			}
		
			if ( $options['bps_autorestore_cron_forced'] == '2' ) {
			
				if ( time() < $options['bps_autorestore_cron_end'] ) {
					exit();
				
				} else {
					
					bps_ARQ_Root();
					bps_ARQ_wpadmin();
					bps_ARQ_wpincludes();
					bps_ARQ_wpcontent();
					bps_ARQ_addedFiles();				
				}
			}
		
			if ( $options['bps_autorestore_cron_frequency'] == '1' ) {
				$arq_cron_end = time() + 60;	
			}
			if ( $options['bps_autorestore_cron_frequency'] == '2' ) {
				$arq_cron_end = time() + 120;	
			}
			if ( $options['bps_autorestore_cron_frequency'] == '3' ) {
				$arq_cron_end = time() + 180;	
			}	
			if ( $options['bps_autorestore_cron_frequency'] == '4' ) {
				$arq_cron_end = time() + 240;	
			}
			if ( $options['bps_autorestore_cron_frequency'] == '5' ) {
				$arq_cron_end = time() + 300;	
			}	
			if ( $options['bps_autorestore_cron_frequency'] == '10' ) {
				$arq_cron_end = time() + 600;	
			}
			if ( $options['bps_autorestore_cron_frequency'] == '15' ) {
				$arq_cron_end = time() + 900;	
			}	
			if ( $options['bps_autorestore_cron_frequency'] == '30' ) {
				$arq_cron_end = time() + 1800;	
			}	
			if ( $options['bps_autorestore_cron_frequency'] == '60' ) {
				$arq_cron_end = time() + 3600;	
			}
	
			$BPS_Options = array(
			'bps_autorestore_cron' 				=> $options['bps_autorestore_cron'], 
			'bps_autorestore_cron_frequency' 	=> $options['bps_autorestore_cron_frequency'], 
			'bps_autorestore_cron_override' 	=> $options['bps_autorestore_cron_override'], 
			'bps_autorestore_cron_filecheck' 	=> $options['bps_autorestore_cron_filecheck'], 
			'bps_autorestore_cron_forced' 		=> $options['bps_autorestore_cron_forced'], 
			'bps_autorestore_cron_end' 			=> $arq_cron_end
			);	
		
			foreach ( $BPS_Options as $key => $value ) {
				update_option('bulletproof_security_options_ARCM', $BPS_Options);
			}
		}
	}
}

// AutoRestore|Quarantine Root Files
// 11.2: duplicate file code removed|mkdir code added|Qpath + dir
// 13.3: Added a 5MB file size limit condition for files
function bps_ARQ_Root() {
global $wpdb, $wp_version, $bpspro_version;

	$indexARQ = WP_CONTENT_DIR . '/bps-backup/autorestore/root-files/index.php';
	
	// ARQ Cron FailSafe Shutdown
	if ( ! file_exists($indexARQ) ) {
		exit();	
	}

	$options = get_option('bulletproof_security_options_ARCM');
	
	if ( $options['bps_autorestore_cron'] == 'On' ) {

	$bpsClassFile = WP_PLUGIN_DIR . '/bulletproof-security/includes/class.php';
	$bpsClassFileARQMB = WP_CONTENT_DIR . '/bps-backup/master-backups/class.php';
	
	// Failsafe for class.php ARQ Exclude rules
	if ( file_exists($bpsClassFileARQMB) && file_exists($bpsClassFile) && @filemtime($bpsClassFileARQMB) == @filemtime($bpsClassFile) ) {

	$options2 = get_option('bulletproof_security_options_autolock');
	$Flockoptions = get_option('bulletproof_security_options_flock'); 
	$table_name = $wpdb->prefix . "bpspro_arq_quarantine";
	$bpspro_arq_exclude_table = $wpdb->prefix . "bpspro_arq_exclude";
	$bpsProLog = WP_CONTENT_DIR . '/bps-backup/logs/autorestore_log.txt';
	$timeNow = time();
	$gmt_offset = get_option( 'gmt_offset' ) * 3600;
	$timestamp = date_i18n(get_option('date_format'), strtotime("11/15-1976")) . ' ' . date_i18n(get_option('time_format'), $timeNow + $gmt_offset);
	$sapi_type = php_sapi_name();
	bpsPro_Core_Files_Check();
	$htaccessARQ = WP_CONTENT_DIR . '/bps-backup/autorestore/root-files/auto_.htaccess';
	$htaccess = ABSPATH.'.htaccess';
	$index = ABSPATH.'index.php';
	$wpconfig = ABSPATH.'wp-config.php';
	$wpconfigARQ = WP_CONTENT_DIR . '/bps-backup/autorestore/root-files/wp-config.php';
	$wpblogheader = ABSPATH.'wp-blog-header.php';
	$wpblogheaderARQ = WP_CONTENT_DIR . '/bps-backup/autorestore/root-files/wp-blog-header.php';
	$Qpath = WP_CONTENT_DIR . '/bps-backup/quarantine/';

	// Bulky but necessary for Live Hosted servers. includes all source and backup path possibilities
	$XARQPath1 = WP_CONTENT_DIR . '/bps-backup/autorestore/root-files/auto_.htaccess';
	$XARQPath2 = WP_CONTENT_DIR . '/bps-backup/autorestore/root-files\auto_.htaccess';
	$XARQPath3 = WP_CONTENT_DIR . '/bps-backup/autorestore/root-files/.htaccess';
	$XARQPath4 = WP_CONTENT_DIR . '/bps-backup/autorestore/root-files\.htaccess';	
	$XARQPath5 = WP_CONTENT_DIR . '/bps-backup/autorestore/root-files/index.php';
	$XARQPath6 = WP_CONTENT_DIR . '/bps-backup/autorestore/root-files\index.php';
	$XARQPath7 = WP_CONTENT_DIR . '/bps-backup/autorestore/root-files/wp-config.php';
	$XARQPath8 = WP_CONTENT_DIR . '/bps-backup/autorestore/root-files\wp-config.php';	
	$XARQPath9 = WP_CONTENT_DIR . '/bps-backup/autorestore/root-files/wp-blog-header.php';
	$XARQPath10 = WP_CONTENT_DIR . '/bps-backup/autorestore/root-files\wp-blog-header.php';	
	$xamppPath1 = $_SERVER['DOCUMENT_ROOT'].'\.htaccess';
	$xamppPath1b = $_SERVER['DOCUMENT_ROOT'].'/.htaccess';
	$xamppPath2 = $_SERVER['DOCUMENT_ROOT'].'\index.php';
	$xamppPath2b = $_SERVER['DOCUMENT_ROOT'].'/index.php';
	$xamppPath3 = $_SERVER['DOCUMENT_ROOT'].'\wp-config.php';
	$xamppPath3b = $_SERVER['DOCUMENT_ROOT'].'/wp-config.php';
	$xamppPath4 = $_SERVER['DOCUMENT_ROOT'].'\wp-blog-header.php';
	$xamppPath4b = $_SERVER['DOCUMENT_ROOT'].'/wp-blog-header.php';
	$normalPath1 = ABSPATH.'.htaccess';
	$normalPath2 = ABSPATH.'index.php';
	$normalPath3 = ABSPATH.'wp-config.php';
	$normalPath4 = ABSPATH.'wp-blog-header.php';
	
	$pathArray = array($xamppPath1, $xamppPath1b, $normalPath1, $xamppPath2, $xamppPath2b, $normalPath2, $xamppPath3, $xamppPath3b, $normalPath3, $xamppPath4, $xamppPath4b, $normalPath4, $XARQPath1, $XARQPath2, $XARQPath3, $XARQPath4, $XARQPath5, $XARQPath6, $XARQPath7, $XARQPath8, $XARQPath9, $XARQPath10);

	$forward_slash = '/'; // return all rows with a forward slash in them or in other words all rows	
	$getARQExcludeRows = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $bpspro_arq_exclude_table WHERE arq_exclude_source LIKE %s", "%$forward_slash%") );		
	
	$ExcludedPaths = array();
				
	foreach ( $getARQExcludeRows as $row ) {
		$ExcludedPaths[] = $row->arq_exclude_source;
	}
	
	$ExcludedPaths_slash = array();
	
	foreach( $ExcludedPaths as $key => $value ) {
		$ExcludedPaths_slash[] = str_replace( array( '\\', '//' ), "/", $value );
	}
	
	// 11.2: duplicate file code removed|mkdir code added|Qpath + root-files dir
	// Special handling of 4 Root files - .htaccess, index.php, wp-config.php & wp-blog-header.php - Excluded from the ARQ foreach statement
	// Root .htaccess file will be renamed to /bps-backup/autorestore/auto_.htaccess & also in Quarantine folder
	if ( file_exists($htaccessARQ) && @filesize($htaccess) != @filesize($htaccessARQ) && ! in_array($pathArray, $ExcludedPaths) && ! in_array(ABSPATH.'.htaccess', $ExcludedPaths) ) {
		$permsHtaccess = @substr(sprintf('%o', fileperms($htaccess)), -4);
		
		if ( ! is_dir( $Qpath . 'root-files' ) ) {
			@mkdir( $Qpath . 'root-files', 0755, true );
		}

		$log_contents = "\r\n" . '[BPS Pro '.$bpspro_version.': Root File AutoRestore Logged: ' . $timestamp . ']' . "\r\n" . 'Quarantined Filename: Root .htaccess file renamed to auto_.htaccess' .  "\r\n" . 'Quarantine Path: '.$Qpath.'root-files/auto_.htaccess'."\r\n" . 'Restore Path: '.$htaccess."\r\n";

		$handle = @fopen( $bpsProLog, 'a' );
		@fwrite($handle, $log_contents);
		fclose($handle);		
		
		@copy($htaccess, $Qpath.'root-files/auto_.htaccess');
		
		$rows_affected = $wpdb->insert( $table_name, array( 'time' => current_time('mysql'), 'arq_quarantine_source' => $htaccess, 'arq_quarantine_backup' => $htaccessARQ, 'arq_quarantine_qpath' => $Qpath.'root-files/auto_.htaccess') );
		
		@copy($htaccessARQ, $htaccess);	

		if ( substr($sapi_type, 0, 6) != 'apache' || @$permsHtaccess != '0666' || @$permsHtaccess != '0777') { // Windows IIS, XAMPP, etc
			@chmod($htaccess, 0644);
		}
			
		if ( @$permsHtaccess == '0644' && substr($sapi_type, 0, 6) != 'apache' && $options2['bps_root_htaccess_autolock'] == 'On') {			
			@chmod($htaccess, 0404);
		}	
		
		bps_smonitor_autorestore_email();
	}

	if ( file_exists($indexARQ) && @filesize($index) != @filesize($indexARQ) && ! in_array($pathArray, $ExcludedPaths) && ! in_array(ABSPATH.'index.php', $ExcludedPaths) ) {
		$permsIndex = @substr(sprintf('%o', fileperms($index)), -4);

		if ( ! is_dir( $Qpath . 'root-files' ) ) {
			@mkdir( $Qpath . 'root-files', 0755, true );
		}		

		$log_contents = "\r\n" . '[BPS Pro '.$bpspro_version.': Root File AutoRestore Logged: ' . $timestamp . ']' . "\r\n" . 'Quarantined Filename: index.php' .  "\r\n" . 'Quarantine Path: '.$Qpath.'root-files/index.php'."\r\n" . 'Restore Path: '.$index."\r\n";

		$handle = @fopen( $bpsProLog, 'a' );
		@fwrite($handle, $log_contents);
		fclose($handle);		
			
		copy($index, $Qpath.'root-files/index.php');
		
		$rows_affected = $wpdb->insert( $table_name, array( 'time' => current_time('mysql'), 'arq_quarantine_source' => $index, 'arq_quarantine_backup' => $indexARQ, 'arq_quarantine_qpath' => $Qpath.'root-files/index.php') );
		
		copy($indexARQ, $index);	

		if ( substr($sapi_type, 0, 6) != 'apache' || @$permsIndex != '0666' || @$permsIndex != '0777') { // Windows IIS, XAMPP, etc
			@chmod($index, 0644);
		}

		$fl_override_options = get_option('bulletproof_security_options_flock_eol');
		
		if ( $fl_override_options['bps_file_lock_override'] == 'On' ) {

			if ( @$permsIndex == '0644' && substr($sapi_type, 0, 6) != 'apache' && $Flockoptions['bps_lock_index_php'] == 'yes') {			
				@chmod($index, 0400);
			}	
		}
		
		bps_smonitor_autorestore_email();
	}

	if ( file_exists($wpconfigARQ) && @filesize($wpconfig) != @filesize($wpconfigARQ) && ! in_array($pathArray, $ExcludedPaths) && ! in_array(ABSPATH.'wp-config.php', $ExcludedPaths) ) {
		$permsWpconfig = @substr( sprintf('%o', fileperms($wpconfig) ), -4);

		if ( ! is_dir( $Qpath . 'root-files' ) ) {
			@mkdir( $Qpath . 'root-files', 0755, true );
		}
		
		$log_contents = "\r\n" . '[BPS Pro '.$bpspro_version.': Root File AutoRestore Logged: ' . $timestamp . ']' . "\r\n" . 'Quarantined Filename: wp-config.php' .  "\r\n" . 'Quarantine Path: '.$Qpath.'root-files/wp-config.php'."\r\n" . 'Restore Path: '.$wpconfig."\r\n";

		$handle = @fopen( $bpsProLog, 'a' );
		@fwrite($handle, $log_contents);
		fclose($handle);			

		copy($wpconfig, $Qpath.'root-files/wp-config.php');
		
		$rows_affected = $wpdb->insert( $table_name, array( 'time' => current_time('mysql'), 'arq_quarantine_source' => $wpconfig, 'arq_quarantine_backup' => $wpconfigARQ, 'arq_quarantine_qpath' => $Qpath.'root-files/wp-config.php') );
		
		copy($wpconfigARQ, $wpconfig);	

		if ( substr($sapi_type, 0, 6) != 'apache' || @$permsWpconfig != '0666' || @$permsWpconfig != '0777') { // Windows IIS, XAMPP, etc
			@chmod($wpconfig, 0644);
		}

		$fl_override_options = get_option('bulletproof_security_options_flock_eol');
		
		if ( $fl_override_options['bps_file_lock_override'] == 'On' ) {

			if ( @$permsWpconfig == '0644' && substr($sapi_type, 0, 6) != 'apache' && $Flockoptions['bps_lock_wpconfig'] == 'yes') {			
				@chmod($wpconfig, 0400);
			}	
		}
		
		bps_smonitor_autorestore_email();
	}

	if ( file_exists($wpblogheaderARQ) && @filesize($wpblogheader) != @filesize($wpblogheaderARQ) && ! in_array($pathArray, $ExcludedPaths) && ! in_array(ABSPATH.'wp-blog-header.php', $ExcludedPaths) ) {
		$permsWpblogheader = @substr( sprintf('%o', fileperms($wpblogheader) ), -4);

		if ( ! is_dir( $Qpath . 'root-files' ) ) {
			@mkdir( $Qpath . 'root-files', 0755, true );
		}
		
		$log_contents = "\r\n" . '[BPS Pro '.$bpspro_version.': Root File AutoRestore Logged: ' . $timestamp . ']' . "\r\n" . 'Quarantined Filename: wp-blog-header.php' .  "\r\n" . 'Quarantine Path: '.$Qpath.'root-files/wp-blog-header.php'."\r\n" . 'Restore Path: '.$wpblogheader."\r\n";

		$handle = @fopen( $bpsProLog, 'a' );
		@fwrite($handle, $log_contents);
		fclose($handle);		

		copy($wpblogheader, $Qpath.'root-files/wp-blog-header.php');
		
		$rows_affected = $wpdb->insert( $table_name, array( 'time' => current_time('mysql'), 'arq_quarantine_source' => $wpblogheader, 'arq_quarantine_backup' => $wpblogheaderARQ, 'arq_quarantine_qpath' => $Qpath.'root-files/wp-blog-header.php') );
		
		copy($wpblogheaderARQ, $wpblogheader);	

		if ( substr($sapi_type, 0, 6) != 'apache' || @$permsWpblogheader != '0666' || @$permsWpblogheader != '0777') { // Windows IIS, XAMPP, etc
			@chmod($wpblogheader, 0644);
		}

		$fl_override_options = get_option('bulletproof_security_options_flock_eol');
		
		if ( $fl_override_options['bps_file_lock_override'] == 'On' ) {

			if ( @$permsWpblogheader == '0644' && substr($sapi_type, 0, 6) != 'apache' && $Flockoptions['bps_lock_wpblog_header'] == 'yes') {			
				@chmod($wpblogheader, 0400);
			}	
		}
		
		bps_smonitor_autorestore_email();
	}

	$fileSize1 = array();
	$filePath1 = array();
	//$path1 = $_SERVER['DOCUMENT_ROOT']; // using this causes a subfolder site to see root site files
	// could have other interesting uses such as designating a separate site to monitor other sites root files
	$path1 = rtrim(ABSPATH, "/");
	$objects1 = new DirectoryIterator($path1);
	
	// Code Contribution by Hansjörg Schwarz hansjoerg-schwarz@nefkom.net
	// Fixes a problem with open_basedir root directory restrictions
	// PHP Fatal error: Uncaught exception 'RuntimeException' with message 'SplFileInfo::isFile(): open_basedir restriction in effect.
	foreach ( $objects1 as $files1 ) {
		try {
			if ( $files1->isFile() ) {
				
				if ( $files1->getPathname() != (ABSPATH.'.htaccess') && $files1->getPathname() != preg_replace( '/\//', "\\", ABSPATH.'.htaccess' ) && $files1->getPathname() != (ABSPATH.'index.php') && $files1->getPathname() != preg_replace( '/\//', "\\", ABSPATH.'index.php' ) && $files1->getPathname() != (ABSPATH.'wp-config.php') && $files1->getPathname() != preg_replace( '/\//', "\\", ABSPATH.'wp-config.php' ) && $files1->getPathname() != (ABSPATH.'wp-blog-header.php') && $files1->getPathname() != preg_replace( '/\//', "\\", ABSPATH.'wp-blog-header.php' ) ) {

     				if ( $files1->getSize() < 5120000 ) {
						$fileSize1[] = $files1->getSize();
     					$filePath1[] = $files1->getPathname();
					}
				}
     		}
		} catch (RuntimeException $e) {}
     }

	$fileSize2 = array();
	$filePath2 = array();
	$path2 = WP_CONTENT_DIR . '/bps-backup/autorestore/root-files/';
	$path2NS = WP_CONTENT_DIR . '/bps-backup/autorestore/root-files';
	$objects2 = new DirectoryIterator($path2);
	
	foreach ( $objects2 as $files2 ) {
		if ( $files2->isFile() ) {
			if ( $files2->getSize() < 5120000 ) {
				$fileSize2[] = $files2->getSize();
				$filePath2[] = $files2->getPathname();
			}
		}
	}
	
	$send_email = '';
	$result = array_diff($fileSize1, $fileSize2);
	
	foreach( $result as $diffKey => $diffValue ) {
		if ( array_key_exists($diffKey, $filePath1) ) {
			$sliceValues = array_slice($filePath1, $diffKey, 1);
			
			foreach( $sliceValues as $Akey => $pathValue ) {
				$pathValueARCM = str_replace($path1, $path2NS, $pathValue);
				$Basefilename = basename($pathValue);
				$pathValue_slash = str_replace( array( '\\', '//' ), "/", $pathValue );
				// 11.2: duplicate file code removed|mkdir code added|Qpath + root-files dir
				// 12.9: slash replacement condition added for XAMPP, WAMP, MAMP & double forward slash // common coding mistake.
				if ( file_exists($pathValueARCM) && ! in_array($pathValue_slash, $ExcludedPaths_slash ) ) {					
					$dir_path = 'root-files';
					
					if ( ! is_dir( $Qpath . $dir_path ) ) {
						@mkdir( $Qpath . $dir_path, 0755, true );
					}
					
					if ( is_dir( $Qpath . $dir_path ) ) {
						copy($pathValue, $Qpath.$dir_path.'/'.$Basefilename);
					}	

					$log_contents = "\r\n" . '[BPS Pro '.$bpspro_version.': Root File AutoRestore Logged: ' . $timestamp . ']' . "\r\n" . 'Quarantined Filename: ' . $Basefilename .  "\r\n" . 'Quarantine Path: '.$Qpath.$dir_path.'/'.$Basefilename."\r\n" . 'Restore Path: '.$pathValue."\r\n";

					$handle = @fopen( $bpsProLog, 'a' );
					@fwrite($handle, $log_contents);
					fclose($handle);			
					
					// XAMPP, WAMP or MAMP string replace slashes
					if ( @substr($sapi_type, 0, 6) == 'apache' && preg_match( '#\\\\#', ABSPATH, $matches ) ) {
						$pathValue = str_replace( array( '\\', '//' ), "/", $pathValue );
						$pathValueARCM = str_replace( array( '\\', '//' ), "/", $pathValueARCM );
						$Qpath = str_replace( array( '\\', '//' ), "/", $Qpath );
					}					
					
					if ( $rows_affected = $wpdb->insert( $table_name, array( 'time' => current_time('mysql'), 'arq_quarantine_source' => $pathValue, 'arq_quarantine_backup' => $pathValueARCM, 'arq_quarantine_qpath' => $Qpath.$dir_path.'/'.$Basefilename ) ) ) {
						$send_email = 'send';	
					}
					
					copy($pathValueARCM, $pathValue);

				} // end if file_exists $pathValueARCM
			} // end foreach $sliceValues
		} // end array_key_exists 
	} // end foreach $result
			
	// Start Quarantine ONLY processing			
	$PValueARCM = array();
	
	foreach ( $filePath1 as $PKey => $PValue ) {
		$PValueARCM[] = str_replace($path1, $path2NS, $PValue);	 
	}

	$resultPathNameSR = array_diff($PValueARCM, $filePath2);
	
	foreach ( $resultPathNameSR as $SRKeyBack => $SRValueBack ) {
		$SRValueSource = str_replace($path2NS, $path1, $SRValueBack);
		$BasefilenameQ = basename($SRValueBack);
		$SRValueSource_slash = str_replace( array( '\\', '//' ), "/", $SRValueSource );
		// 11.2: duplicate file code removed|mkdir code added|Qpath + root-files dir		
		// 12.9: slash replacement condition added for XAMPP, WAMP, MAMP & double forward slash // common coding mistake.
		// Quarantine ONLY
		if ( ! file_exists($SRValueBack) && ! in_array($SRValueSource_slash, $ExcludedPaths_slash ) ) {
				
			$Qdir_path = 'root-files';

			if ( ! is_dir( $Qpath . $Qdir_path ) ) {
				@mkdir( $Qpath . $Qdir_path, 0755, true );
			}
					
			if ( is_dir( $Qpath . $Qdir_path ) ) {

				rename($SRValueSource, $Qpath.$Qdir_path.'/'.$BasefilenameQ);
			}

			$log_contents = "\r\n" . '[BPS Pro '.$bpspro_version.': Root File Quarantine Logged: ' . $timestamp . ']' . "\r\n" . 'Quarantined Filename: ' . $BasefilenameQ .  "\r\n" . 'Quarantine Path: '.$Qpath.$Qdir_path.'/'.$BasefilenameQ."\r\n" . 'Restore Path: '.$SRValueSource."\r\n";

			$handle = @fopen( $bpsProLog, 'a' );
			@fwrite($handle, $log_contents);
			fclose($handle);
					
			// XAMPP, WAMP or MAMP string replace slashes
			if ( @substr($sapi_type, 0, 6) == 'apache' && preg_match('#\\\\#', ABSPATH, $matches) ) {
				$SRValueSource = str_replace( array( '\\', '//' ), "/", $SRValueSource );
				$SRValueBack = str_replace( array( '\\', '//' ), "/", $SRValueBack );
				$Qpath = str_replace( array( '\\', '//' ), "/", $Qpath );
			}

			if ( $rows_affected = $wpdb->insert( $table_name, array( 'time' => current_time('mysql'), 'arq_quarantine_source' => $SRValueSource, 'arq_quarantine_backup' => $SRValueBack, 'arq_quarantine_qpath' => $Qpath.$Qdir_path.'/'.$BasefilenameQ ) ) ) {
				$send_email = 'send';	
			}
		}
	} // end foreach $resultPathNameSR

	// ONLY Send 1 email alert
	if ( $send_email != '' ) {
		bps_smonitor_autorestore_email();
	}	
	} // end if ( file_exists($bpsClassFileARQMB) && file_exists($bpsClassFile) && @filemtime($bpsClassFileARQMB) == @filemtime($bpsClassFile) ) {
	} // end if ARQ Cron is On
}

// AutoRestore|Quarantine wp-admin
// 11.2: duplicate file code removed|mkdir code added|Qpath + dir
// 13.3: Added a 5MB file size limit condition for files
function bps_ARQ_wpadmin() {
global $wpdb, $wp_version, $bpspro_version;

	$GDMW_options = get_option('bulletproof_security_options_GDMW');	

	if ( $GDMW_options['bps_gdmw_hosting'] == 'yes' ) {
		exit();
	}

	$ARQDir =  WP_CONTENT_DIR . '/bps-backup/autorestore/wp-admin/';
	$ARQFSWPadmin =  WP_CONTENT_DIR . '/bps-backup/autorestore/wp-admin/admin.php';
	
	// ARQ Cron FailSafe Shutdown
	if ( ! is_dir($ARQDir) || ! file_exists($ARQFSWPadmin) ) {
		exit();
	}
	
	$options = get_option('bulletproof_security_options_ARCM');

	if ( $options['bps_autorestore_cron'] == 'On' ) {
	
	$bpsClassFile = WP_PLUGIN_DIR . '/bulletproof-security/includes/class.php';
	$bpsClassFileARQMB = WP_CONTENT_DIR . '/bps-backup/master-backups/class.php';

	// Failsafe for class.php ARQ Exclude rules
	if ( file_exists($bpsClassFileARQMB) && file_exists($bpsClassFile) && @filemtime($bpsClassFileARQMB) == @filemtime($bpsClassFile) ) {

	$table_name = $wpdb->prefix . "bpspro_arq_quarantine";
	$bpspro_arq_exclude_table = $wpdb->prefix . "bpspro_arq_exclude";
	$bpsProLog = WP_CONTENT_DIR . '/bps-backup/logs/autorestore_log.txt';
	$timeNow = time();
	$gmt_offset = get_option( 'gmt_offset' ) * 3600;
	$timestamp = date_i18n(get_option('date_format'), strtotime("11/15-1976")) . ' ' . date_i18n(get_option('time_format'), $timeNow + $gmt_offset);
	$htaccessARQ = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-admin/wpadmin.htaccess';
	$htaccess = ABSPATH.'wp-admin/.htaccess';
	$Qpath = WP_CONTENT_DIR . '/bps-backup/quarantine/';

	// Bulky but necessary for Live Hosted servers. includes all source and backup path possibilities
	$xamppPath1 = ABSPATH.'wp-admin\.htaccess';
	$normalPath = ABSPATH.'wp-admin/.htaccess';
	$XARQPath1 = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-admin/wpadmin.htaccess';
	$XARQPath2 = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-admin\wpadmin.htaccess';
	$XARQPath3 = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-admin/.htaccess';
	$XARQPath4 = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-admin\.htaccess';
	
	$pathArray = array($xamppPath1, $normalPath, $XARQPath1, $XARQPath2, $XARQPath3, $XARQPath4);

	$forward_slash = '/'; // return all rows with a forward slash in them or in other words all rows	
	$getARQExcludeRows = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $bpspro_arq_exclude_table WHERE arq_exclude_source LIKE %s", "%$forward_slash%") );	

	$ExcludedPaths = array();
				
	foreach ( $getARQExcludeRows as $row ) {
		$ExcludedPaths[] = $row->arq_exclude_source;
	}

	$ExcludedPaths_slash = array();
	
	foreach( $ExcludedPaths as $key => $value ) {
		$ExcludedPaths_slash[] = str_replace( array( '\\', '//' ), "/", $value );
	}

	// 11.2: duplicate file code removed|mkdir code added|Qpath + wp-admin dir
	// Special handling of /wp-admin/.htaccess file here
	if ( file_exists($htaccessARQ) && @filesize($htaccess) != @filesize($htaccessARQ) && ! in_array($pathArray, $ExcludedPaths) && ! in_array(ABSPATH.'wp-admin/.htaccess', $ExcludedPaths) && ! in_array(ABSPATH.'wp-admin\.htaccess', $ExcludedPaths) ) {

		if ( ! is_dir( $Qpath . 'wp-admin' ) ) {
			@mkdir( $Qpath . 'wp-admin', 0755, true );
		}
		
		$log_contents = "\r\n" . '[BPS Pro '.$bpspro_version.': wp-admin File AutoRestore Logged: ' . $timestamp . ']' . "\r\n" . 'Quarantined Filename: /wp-admin/.htaccess file renamed to wpadmin.htaccess' .  "\r\n" . 'Quarantine Path: '.$Qpath.'wp-admin/wpadmin.htaccess'."\r\n" . 'Restore Path: '.$htaccess."\r\n";

		$handle = @fopen( $bpsProLog, 'a' );
		@fwrite($handle, $log_contents);
		fclose($handle);

		@copy($htaccess, $Qpath.'wp-admin/wpadmin.htaccess');
		
		$rows_affected = $wpdb->insert( $table_name, array( 'time' => current_time('mysql'), 'arq_quarantine_source' => $htaccess, 'arq_quarantine_backup' => $htaccessARQ, 'arq_quarantine_qpath' => $Qpath.'wp-admin/wpadmin.htaccess') );
		
		@copy($htaccessARQ, $htaccess);
		
		bps_smonitor_autorestore_email();
	}

	$fileSize1 = array();
	$filePath1 = array();
	$path1 = ABSPATH.'wp-admin';
	$objects1 = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path1), RecursiveIteratorIterator::SELF_FIRST);
	
	foreach ( $objects1 as $files1 ) {
		if ( $files1->isFile() ) {
			
			if ( $files1->getPathname() != (ABSPATH.'wp-admin/.htaccess') && $files1->getPathname() != (ABSPATH.'wp-admin\.htaccess') && $files1->getPathname() != preg_replace( '/\//', "\\", ABSPATH.'wp-admin/.htaccess' ) ) {
				
   				if ( $files1->getSize() < 5120000 ) {
					$fileSize1[] = $files1->getSize();
     				$filePath1[] = $files1->getPathname();
				}
			}
		}
	}

	$fileSize2 = array();
	$filePath2 = array();
	$path2 = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-admin/';
	$path2NS = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-admin';
	$objects2 = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path2), RecursiveIteratorIterator::SELF_FIRST);	
	
	foreach ( $objects2 as $files2 ) {
		if ( $files2->isFile() ) {
			
 			if ( $files2->getSize() < 5120000 ) {
				$fileSize2[] = $files2->getSize();
				$filePath2[] = $files2->getPathname();
			}
		}	
	}
	
	$send_email = '';
	$result = array_diff($fileSize1, $fileSize2);

	foreach( $result as $diffKey => $diffValue ){
		if ( array_key_exists($diffKey, $filePath1) ) {
			$sliceValues = array_slice($filePath1, $diffKey, 1);
			
			foreach( $sliceValues as $Akey => $pathValue ) {
				$pathValueARCM = str_replace($path1, $path2NS, $pathValue);
				$Basefilename = basename($pathValue);
				$pathValue_slash = str_replace( array( '\\', '//' ), "/", $pathValue );
				// 11.2: duplicate file code removed|mkdir code added|Qpath + wp-admin dir
				// 12.9: slash replacement condition added for XAMPP, WAMP, MAMP & double forward slash // common coding mistake.
				// AutoRestore & Quarantine
				if ( file_exists($pathValueARCM) && ! in_array($pathValue_slash, $ExcludedPaths_slash ) ) {
					
					$path_parts = pathinfo($pathValue);
					$dir_path = str_replace( ABSPATH, '', $path_parts['dirname'] );
					
					if ( ! is_dir( $Qpath . $dir_path ) ) {
						@mkdir( $Qpath . $dir_path, 0755, true );
					}
					
					if ( is_dir( $Qpath . $dir_path ) ) {
						copy($pathValue, $Qpath.$dir_path.'/'.$Basefilename);
					}					

					$log_contents = "\r\n" . '[BPS Pro '.$bpspro_version.': wp-admin File AutoRestore Logged: ' . $timestamp . ']' . "\r\n" . 'Quarantined Filename: ' . $Basefilename .  "\r\n" . 'Quarantine Path: '.$Qpath.$dir_path.'/'.$Basefilename."\r\n" . 'Restore Path: '.$pathValue."\r\n";
					
					$handle = @fopen( $bpsProLog, 'a' );
					@fwrite($handle, $log_contents);
					fclose($handle);
					
					// logically doing only an INSERT is ok, but need to check what happens with duplicate rows. 
					// may need to do an UPDATE, but they will use lots more juice
					if ( $rows_affected = $wpdb->insert( $table_name, array( 'time' => current_time('mysql'), 'arq_quarantine_source' => $pathValue, 'arq_quarantine_backup' => $pathValueARCM, 'arq_quarantine_qpath' => $Qpath.$dir_path.'/'.$Basefilename) ) ) {
						$send_email = 'send';	
					}
					
					copy($pathValueARCM, $pathValue);
				} // end if file_exists $pathValueARCM
			} // end foreach $sliceValues
		} // end array_key_exists 
	} // end foreach $result
			
	// Start Quarantine ONLY processing			
	$PValueARCM = array();
	
	foreach ( $filePath1 as $PKey => $PValue ) {
		$PValueARCM[] = str_replace($path1, $path2NS, $PValue);	 
	}
	
	$resultPathNameSR = array_diff($PValueARCM, $filePath2);

	foreach ( $resultPathNameSR as $SRKeyBack => $SRValueBack ) {
		$SRValueSource = str_replace($path2NS, $path1, $SRValueBack);
		$BasefilenameQ = basename($SRValueBack);	
		$SRValueSource_slash = str_replace( array( '\\', '//' ), "/", $SRValueSource );
		// 11.2: duplicate file code removed|mkdir code added|Qpath + wp-admin dir|if ARQ backup file exists
		// 12.9: slash replacement condition added for XAMPP, WAMP, MAMP & double forward slash // common coding mistake.
		// Quarantine ONLY
		if ( ! file_exists($SRValueBack) && ! in_array($SRValueSource_slash, $ExcludedPaths_slash ) ) {	
				
			$Qpath_parts = pathinfo($SRValueSource);
			$Qdir_path = str_replace( ABSPATH, '', $Qpath_parts['dirname'] );
					
			if ( ! is_dir( $Qpath . $Qdir_path ) ) {
				@mkdir( $Qpath . $Qdir_path, 0755, true );
			}
					
			if ( is_dir( $Qpath . $Qdir_path ) ) {

				rename($SRValueSource, $Qpath.$Qdir_path.'/'.$BasefilenameQ);
			}

			$log_contents = "\r\n" . '[BPS Pro '.$bpspro_version.': wp-admin File Quarantine Logged: ' . $timestamp . ']' . "\r\n" . 'Quarantined Filename: ' . $BasefilenameQ .  "\r\n" . 'Quarantine Path: '.$Qpath.$Qdir_path.'/'.$BasefilenameQ."\r\n" . 'Restore Path: '.$SRValueSource."\r\n";

			$handle = @fopen( $bpsProLog, 'a' );
			@fwrite($handle, $log_contents);
			fclose($handle);				
					
			if ( $rows_affected = $wpdb->insert( $table_name, array( 'time' => current_time('mysql'), 'arq_quarantine_source' => $SRValueSource, 'arq_quarantine_backup' => $SRValueBack, 'arq_quarantine_qpath' => $Qpath.$Qdir_path.'/'.$BasefilenameQ) ) ) {
				$send_email = 'send';
			}
		}
	} // end foreach $resultPathNameSR

	// ONLY Send 1 email alert
	if ( $send_email != '' ) {
		bps_smonitor_autorestore_email();
	}	
	} // end if ( file_exists($bpsClassFileARQMB) && file_exists($bpsClassFile) && @filemtime($bpsClassFileARQMB) == @filemtime($bpsClassFile) ) {
	} // end if ARQ Cron is On
}
			
// AutoRestore|Quarantine wp-includes
// 11.2: duplicate file code removed|mkdir code added|Qpath + dir
// 13.3: Added a 5MB file size limit condition for files
function bps_ARQ_wpincludes() {
global $wpdb, $wp_version, $bpspro_version;

	$GDMW_options = get_option('bulletproof_security_options_GDMW');

	if ( $GDMW_options['bps_gdmw_hosting'] == 'yes' ) {
		exit();
	}

	$ARQDir =  WP_CONTENT_DIR . '/bps-backup/autorestore/wp-includes/';
	$ARQFSWPincludes =  WP_CONTENT_DIR . '/bps-backup/autorestore/wp-includes/functions.php';

	// ARQ Cron FailSafe Shutdown
	if ( ! is_dir($ARQDir) || ! file_exists($ARQFSWPincludes) ) {
		exit();
	}
	
	$options = get_option('bulletproof_security_options_ARCM');
	
	if ( $options['bps_autorestore_cron'] == 'On' ) {
	
	$bpsClassFile = WP_PLUGIN_DIR . '/bulletproof-security/includes/class.php';
	$bpsClassFileARQMB = WP_CONTENT_DIR . '/bps-backup/master-backups/class.php';

	// Failsafe for class.php ARQ Exclude rules
	if ( file_exists($bpsClassFileARQMB) && file_exists($bpsClassFile) && @filemtime($bpsClassFileARQMB) == @filemtime($bpsClassFile) ) {

	$table_name = $wpdb->prefix . "bpspro_arq_quarantine";
	$bpspro_arq_exclude_table = $wpdb->prefix . "bpspro_arq_exclude";
	$bpsProLog = WP_CONTENT_DIR . '/bps-backup/logs/autorestore_log.txt';
	$timeNow = time();
	$gmt_offset = get_option( 'gmt_offset' ) * 3600;
	$timestamp = date_i18n(get_option('date_format'), strtotime("11/15-1976")) . ' ' . date_i18n(get_option('time_format'), $timeNow + $gmt_offset);

	$fileSize1 = array();
	$filePath1 = array();
	$path1 = ABSPATH.'wp-includes';
	$Qpath = WP_CONTENT_DIR . '/bps-backup/quarantine/';
	$objects1 = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path1), RecursiveIteratorIterator::SELF_FIRST);	
	
	foreach ( $objects1 as $files1 ) {
		if ( $files1->isFile() ) {
			if ( $files1->getSize() < 5120000 ) {
				$fileSize1[] = $files1->getSize();
     			$filePath1[] = $files1->getPathname();
			}
		}
	}

	$fileSize2 = array();
	$filePath2 = array();
	$path2 = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-includes/';
	$path2NS = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-includes';
	$objects2 = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path2), RecursiveIteratorIterator::SELF_FIRST);	
	
	foreach ( $objects2 as $files2 ) {
		if ( $files2->isFile() ) {
 			if ( $files2->getSize() < 5120000 ) {
				$fileSize2[] = $files2->getSize();
				$filePath2[] = $files2->getPathname();
			}
		}	
	}
	
	$send_email = '';
	$result = array_diff($fileSize1, $fileSize2);
	
	$forward_slash = '/'; // return all rows with a forward slash in them or in other words all rows	
	$getARQExcludeRows = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $bpspro_arq_exclude_table WHERE arq_exclude_source LIKE %s", "%$forward_slash%") );	
	$ExcludedPaths = array();
				
	foreach ( $getARQExcludeRows as $row ) {
		$ExcludedPaths[] = $row->arq_exclude_source;
	}
	
	$ExcludedPaths_slash = array();
	
	foreach( $ExcludedPaths as $key => $value ) {
		$ExcludedPaths_slash[] = str_replace( array( '\\', '//' ), "/", $value );
	}

	foreach( $result as $diffKey => $diffValue ){
		if ( array_key_exists($diffKey, $filePath1) ) {
			$sliceValues = array_slice($filePath1, $diffKey, 1);
			
			foreach( $sliceValues as $Akey => $pathValue ) {
				$pathValueARCM = str_replace($path1, $path2NS, $pathValue);
				$Basefilename = basename($pathValue);
				$pathValue_slash = str_replace( array( '\\', '//' ), "/", $pathValue );
				// 11.2: duplicate file code removed|mkdir code added|Qpath + wp-includes dir
				// 12.9: slash replacement condition added for XAMPP, WAMP, MAMP & double forward slash // common coding mistake.
				// AutoRestore & Quarantine
				if ( file_exists($pathValueARCM) && ! in_array($pathValue_slash, $ExcludedPaths_slash) ) {
					
					$path_parts = pathinfo($pathValue);
					$dir_path = str_replace( ABSPATH, '', $path_parts['dirname'] );
					
					if ( ! is_dir( $Qpath . $dir_path ) ) {
						@mkdir( $Qpath . $dir_path, 0755, true );
					}
					
					if ( is_dir( $Qpath . $dir_path ) ) {
						copy($pathValue, $Qpath.$dir_path.'/'.$Basefilename);
					}					

					$log_contents = "\r\n" . '[BPS Pro '.$bpspro_version.': wp-includes File AutoRestore Logged: ' . $timestamp . ']' . "\r\n" . 'Quarantined Filename: ' . $Basefilename .  "\r\n" . 'Quarantine Path: '.$Qpath.$dir_path.'/'.$Basefilename."\r\n" . 'Restore Path: '.$pathValue."\r\n";

					$handle = @fopen( $bpsProLog, 'a' );
					@fwrite($handle, $log_contents);
					fclose($handle);
				
					if ( $rows_affected = $wpdb->insert( $table_name, array( 'time' => current_time('mysql'), 'arq_quarantine_source' => $pathValue, 'arq_quarantine_backup' => $pathValueARCM, 'arq_quarantine_qpath' => $Qpath.$dir_path.'/'.$Basefilename ) ) ) {
						$send_email = 'send';	
					}
					
					copy($pathValueARCM, $pathValue);
				} // end if file_exists $pathValueARCM
			} // end foreach $sliceValues
		} // end array_key_exists 
	} // end foreach $result
			
	// Start Quarantine ONLY processing			
	$PValueARCM = array();
	
	foreach ( $filePath1 as $PKey => $PValue ) {
		$PValueARCM[] = str_replace($path1, $path2NS, $PValue);	 
	}

	$resultPathNameSR = array_diff($PValueARCM, $filePath2);

	foreach ( $resultPathNameSR as $SRKeyBack => $SRValueBack ) {
		$SRValueSource = str_replace($path2NS, $path1, $SRValueBack);
		$BasefilenameQ = basename($SRValueBack);
		$SRValueSource_slash = str_replace( array( '\\', '//' ), "/", $SRValueSource );			
		// 11.2: duplicate file code removed|mkdir code added|Qpath + wp-includes dir|if ARQ backup file exists		
		// 12.9: slash replacement condition added for XAMPP, WAMP, MAMP & double forward slash // common coding mistake.
		// Quarantine ONLY
		if ( ! file_exists($SRValueBack) && ! in_array($SRValueSource_slash, $ExcludedPaths_slash) ) {	
				
			$Qpath_parts = pathinfo($SRValueSource);
			$Qdir_path = str_replace( ABSPATH, '', $Qpath_parts['dirname'] );
					
			if ( ! is_dir( $Qpath . $Qdir_path ) ) {
				@mkdir( $Qpath . $Qdir_path, 0755, true );
			}
					
			if ( is_dir( $Qpath . $Qdir_path ) ) {

				rename($SRValueSource, $Qpath.$Qdir_path.'/'.$BasefilenameQ);
			}

			$log_contents = "\r\n" . '[BPS Pro '.$bpspro_version.': wp-includes File Quarantine Logged: ' . $timestamp . ']' . "\r\n" . 'Quarantined Filename: ' . $BasefilenameQ .  "\r\n" . 'Quarantine Path: '.$Qpath.$Qdir_path.'/'.$BasefilenameQ."\r\n" . 'Restore Path: '.$SRValueSource."\r\n";

			$handle = @fopen( $bpsProLog, 'a' );
			@fwrite($handle, $log_contents);
			fclose($handle);				
					
			if ( $rows_affected = $wpdb->insert( $table_name, array( 'time' => current_time('mysql'), 'arq_quarantine_source' => $SRValueSource, 'arq_quarantine_backup' => $SRValueBack, 'arq_quarantine_qpath' => $Qpath.$Qdir_path.'/'.$BasefilenameQ ) ) ) {
				$send_email = 'send';	
			}
		}
	} // end foreach $resultPathNameSR
	
	// ONLY Send 1 email alert
	if ( $send_email != '' ) {
		bps_smonitor_autorestore_email();
	}	
	} // end if ( file_exists($bpsClassFileARQMB) && file_exists($bpsClassFile) && @filemtime($bpsClassFileARQMB) == @filemtime($bpsClassFile) ) {
	} // end if ARQ Cron is On
}

// AutoRestore|Quarantine wp-content
// 11: Extra insurance against plugin files being quarantined in case of a server hiccup|delay during upgrades
// 11.2: duplicate file code removed|mkdir code added|Qpath + dir
// 13.3: Added a 5MB file size limit condition for files
function bps_ARQ_wpcontent() {
global $wpdb, $wp_version, $bpspro_version;

	$ARQDir =  WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/';
	$ARQFSWPcontent =  WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/index.php';	
	
	// ARQ Cron FailSafe Shutdown
	if ( ! is_dir($ARQDir) || ! file_exists($ARQFSWPcontent) ) {
		exit();
	}

	$options = get_option('bulletproof_security_options_ARCM');

	if ( $options['bps_autorestore_cron'] == 'On' ) {

	$bpsClassFile = WP_PLUGIN_DIR . '/bulletproof-security/includes/class.php';
	$bpsClassFileARQMB = WP_CONTENT_DIR . '/bps-backup/master-backups/class.php';

	// Failsafe for class.php ARQ Exclude rules
	if ( file_exists($bpsClassFileARQMB) && file_exists($bpsClassFile) && @filemtime($bpsClassFileARQMB) == @filemtime($bpsClassFile) ) {

	$table_name = $wpdb->prefix . "bpspro_arq_quarantine";
	$bpspro_arq_exclude_table = $wpdb->prefix . "bpspro_arq_exclude";
	$bpsProLog = WP_CONTENT_DIR . '/bps-backup/logs/autorestore_log.txt';
	$timeNow = time();
	$gmt_offset = get_option( 'gmt_offset' ) * 3600;
	$timestamp = date_i18n(get_option('date_format'), strtotime("11/15-1976")) . ' ' . date_i18n(get_option('time_format'), $timeNow + $gmt_offset);

	$fileSize1 = array();
	$filePath1 = array();
	$path1 = WP_CONTENT_DIR;
	$Qpath = WP_CONTENT_DIR . '/bps-backup/quarantine/';
	$dirItr = new RecursiveDirectoryIterator($path1);
	$filterItr = new BPSWPCSourceCronRecursiveFilterIterator($dirItr);
	$objects1 = new RecursiveIteratorIterator($filterItr, RecursiveIteratorIterator::SELF_FIRST); 

	foreach ( $objects1 as $files1 ) {
		if ( $files1->isFile() ) {
			
			// 12.9: Moved from Quarantine conditions
			if ( $files1->getPathname() != (WP_CONTENT_DIR . '/mysql.sql') && $files1->getPathname() != (WP_CONTENT_DIR . '\mysql.sql') && $files1->getPathname() != preg_replace( '/\//', "\\", WP_CONTENT_DIR . '/mysql.sql' ) ) {
				
    			if ( $files1->getSize() < 5120000 ) {
					$fileSize1[] = $files1->getSize();
     				$filePath1[] = $files1->getPathname();
				}
			}
		}	
	}

	$fileSize2 = array();
	$filePath2 = array();
	$path2 = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/';
	$path2NS = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content';
	$dirItr = new RecursiveDirectoryIterator($path2);
	$filterItr = new BPSWPCBackupCronRecursiveFilterIterator($dirItr);
	$objects2 = new RecursiveIteratorIterator($filterItr, RecursiveIteratorIterator::SELF_FIRST); 

	foreach ( $objects2 as $files2 ) {
		if ( $files2->isFile() ) {
 			if ( $files2->getSize() < 5120000 ) {
				$fileSize2[] = $files2->getSize();
				$filePath2[] = $files2->getPathname();
			}                   
		}
	}

	$send_email = '';
	$result = array_diff($fileSize1, $fileSize2);
	
	// 11: check plugins exclude rule
	$ARQ_wpcontent_folder_exclude = get_option('bulletproof_security_options_exclude_folder');

	foreach ( $ARQ_wpcontent_folder_exclude as $Ekey => $Evalue ) {
		
		if ( ! preg_match('/_label/', $Ekey, $matches ) && $Evalue == 'plugins'  ) {
			
			$exclude_plugins_folder = 'exclude';
		}
	}

	$forward_slash = '/'; // return all rows with a forward slash in them or in other words all rows	
	$getARQExcludeRows = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $bpspro_arq_exclude_table WHERE arq_exclude_source LIKE %s", "%$forward_slash%") );	
	$ExcludedPaths = array();
				
	foreach ( $getARQExcludeRows as $row ) {
		$ExcludedPaths[] = $row->arq_exclude_source;
	}	
	
	$ExcludedPaths_slash = array();
	
	foreach( $ExcludedPaths as $key => $value ) {
		$ExcludedPaths_slash[] = str_replace( array( '\\', '//' ), "/", $value );
	}

	foreach ( $result as $diffKey => $diffValue ) {
		if ( array_key_exists($diffKey, $filePath1) ) {
			$sliceValues = array_slice($filePath1, $diffKey, 1);
			
			foreach( $sliceValues as $Akey => $pathValue ) {
				$pathValueARCM = str_replace($path1, $path2NS, $pathValue);
				$Basefilename = basename($pathValue);
				$pathValue_slash = str_replace( array( '\\', '//' ), "/", $pathValue );
				// 11: check plugins exclude rule
				// 11.2: duplicate file code removed|mkdir code added|Qpath + wp-content dir				
				// 12.9: slash replacement condition added for XAMPP, WAMP, MAMP & double forward slash // common coding mistake.
				// AutoRestore & Quarantine
				if ( file_exists($pathValueARCM) && ! in_array($pathValue_slash, $ExcludedPaths_slash) ) {
					
					$path_parts = pathinfo($pathValue);
					$dir_path = str_replace( ABSPATH, '', $path_parts['dirname'] );
					
					// extra insurance against plugin files being quarantined in case of a server hiccup|delay during upgrades
					// Note: If someone is not excluding the plugins folder then normal processing would occur for the plugins folder.
					if ( $exclude_plugins_folder == 'exclude' ) {
					
						if ( ! preg_match('/\/plugins\//', $pathValue, $matches ) ) {
						
							if ( ! is_dir( $Qpath . $dir_path ) ) {
								@mkdir( $Qpath . $dir_path, 0755, true );
							}

							if ( is_dir( $Qpath . $dir_path ) ) {
								copy($pathValue, $Qpath.$dir_path.'/'.$Basefilename);
							}					

							$log_contents = "\r\n" . '[BPS Pro '.$bpspro_version.': wp-content File AutoRestore Logged: ' . $timestamp . ']' . "\r\n" . 'Quarantined Filename: ' . $Basefilename .  "\r\n" . 'Quarantine Path: '.$Qpath.$dir_path.'/'.$Basefilename."\r\n" . 'Restore Path: '.$pathValue."\r\n";

							$handle = @fopen( $bpsProLog, 'a' );
							@fwrite($handle, $log_contents);
							fclose($handle);					
						
							if ( $rows_affected = $wpdb->insert( $table_name, array( 'time' => current_time('mysql'), 'arq_quarantine_source' => $pathValue, 'arq_quarantine_backup' => $pathValueARCM, 'arq_quarantine_qpath' => $Qpath.$dir_path.'/'.$Basefilename ) ) ) {
							$send_email = 'send';	
							}
				
							copy($pathValueARCM, $pathValue);
						}
					
					} else {
						
						if ( ! is_dir( $Qpath . $dir_path ) ) {
							@mkdir( $Qpath . $dir_path, 0755, true );
						}

						if ( is_dir( $Qpath . $dir_path ) ) {
							copy($pathValue, $Qpath.$dir_path.'/'.$Basefilename);
						}					

						$log_contents = "\r\n" . '[BPS Pro '.$bpspro_version.': wp-content File AutoRestore Logged: ' . $timestamp . ']' . "\r\n" . 'Quarantined Filename: ' . $Basefilename .  "\r\n" . 'Quarantine Path: '.$Qpath.$dir_path.'/'.$Basefilename."\r\n" . 'Restore Path: '.$pathValue."\r\n";

						$handle = @fopen( $bpsProLog, 'a' );
						@fwrite($handle, $log_contents);
						fclose($handle);					
						
						if ( $rows_affected = $wpdb->insert( $table_name, array( 'time' => current_time('mysql'), 'arq_quarantine_source' => $pathValue, 'arq_quarantine_backup' => $pathValueARCM, 'arq_quarantine_qpath' => $Qpath.$dir_path.'/'.$Basefilename ) ) ) {
						$send_email = 'send';	
						}
				
						copy($pathValueARCM, $pathValue);
					} // end if ( $exclude_plugins_folder == 'exclude' ) {
				} // end if file_exists $pathValueARCM
			} // end foreach $sliceValues
		} // end array_key_exists 
	} // end foreach $result
			
	// Start Quarantine ONLY processing			
	$PValueARCM = array();
	
	foreach ( $filePath1 as $PKey => $PValue ) {
		$PValueARCM[] = str_replace($path1, $path2NS, $PValue);	 
	}

	$resultPathNameSR = array_diff($PValueARCM, $filePath2);

	foreach ( $resultPathNameSR as $SRKeyBack => $SRValueBack ) {
		$SRValueSource = str_replace($path2NS, $path1, $SRValueBack);
		$BasefilenameQ = basename($SRValueBack);
		$SRValueSource_slash = str_replace( array( '\\', '//' ), "/", $SRValueSource );
		// 11: check plugins exclude rule
		// 11.2: duplicate file code removed|mkdir code added|Qpath + wp-content dir|if ARQ backup file exists		
		// 11.4 late: WPEngine mysql.sql file special condition added. 
		// 12.9: moved/changed WPEngine mysql.sql condition to file Iterator: && $SRValueSource != WP_CONTENT_DIR . '/mysql.sql'
		// Quarantine ONLY
		if ( ! file_exists($SRValueBack) && ! in_array($SRValueSource_slash, $ExcludedPaths_slash) ) {	
				
			$Qpath_parts = pathinfo($SRValueSource);
			$Qdir_path = str_replace( ABSPATH, '', $Qpath_parts['dirname'] );

			// extra insurance against plugin files being quarantined in case of a server hiccup|delay during upgrades
			// Note: If someone is not excluding the plugins folder then normal processing would occur for the plugins folder.
			if ( $exclude_plugins_folder == 'exclude' ) {
					
				if ( ! preg_match('/\/plugins\//', $SRValueSource, $matches ) ) {

					if ( ! is_dir( $Qpath . $Qdir_path ) ) {
						@mkdir( $Qpath . $Qdir_path, 0755, true );
					}
					
					if ( is_dir( $Qpath . $Qdir_path ) ) {

						rename($SRValueSource, $Qpath.$Qdir_path.'/'.$BasefilenameQ);
					}

					$log_contents = "\r\n" . '[BPS Pro '.$bpspro_version.': wp-content File Quarantine Logged: ' . $timestamp . ']' . "\r\n" . 'Quarantined Filename: ' . $BasefilenameQ .  "\r\n" . 'Quarantine Path: '.$Qpath.$Qdir_path.'/'.$BasefilenameQ."\r\n" . 'Restore Path: '.$SRValueSource."\r\n";

					$handle = @fopen( $bpsProLog, 'a' );
					@fwrite($handle, $log_contents);
					fclose($handle);				
					
					if ( $rows_affected = $wpdb->insert( $table_name, array( 'time' => current_time('mysql'), 'arq_quarantine_source' => $SRValueSource, 'arq_quarantine_backup' => $SRValueBack, 'arq_quarantine_qpath' => $Qpath.$Qdir_path.'/'.$BasefilenameQ ) ) ) {
						$send_email = 'send';
					}
				}
					
			} else {

				if ( ! is_dir( $Qpath . $Qdir_path ) ) {
					@mkdir( $Qpath . $Qdir_path, 0755, true );
				}
					
				if ( is_dir( $Qpath . $Qdir_path ) ) {

					rename($SRValueSource, $Qpath.$Qdir_path.'/'.$BasefilenameQ);
				}

				$log_contents = "\r\n" . '[BPS Pro '.$bpspro_version.': wp-content File Quarantine Logged: ' . $timestamp . ']' . "\r\n" . 'Quarantined Filename: ' . $BasefilenameQ .  "\r\n" . 'Quarantine Path: '.$Qpath.$Qdir_path.'/'.$BasefilenameQ."\r\n" . 'Restore Path: '.$SRValueSource."\r\n";

				$handle = @fopen( $bpsProLog, 'a' );
				@fwrite($handle, $log_contents);
				fclose($handle);				
					
				if ( $rows_affected = $wpdb->insert( $table_name, array( 'time' => current_time('mysql'), 'arq_quarantine_source' => $SRValueSource, 'arq_quarantine_backup' => $SRValueBack, 'arq_quarantine_qpath' => $Qpath.$Qdir_path.'/'.$BasefilenameQ ) ) ) {
					$send_email = 'send';
				}
			} // end if ( $exclude_plugins_folder == 'exclude' ) {		
		} // end ! file_exists($SRValueBack) && ! in_array($SRValueSourceArray, $ExludedPaths) ) {	
	} // end foreach $resultPathNameSR

	// ONLY Send 1 email alert
	if ( $send_email != '' ) {
		bps_smonitor_autorestore_email();
	}
	} // end if ( file_exists($bpsClassFileARQMB) && file_exists($bpsClassFile) && @filemtime($bpsClassFileARQMB) == @filemtime($bpsClassFile) ) {
	} // end if ARQ Cron is On
}

// AutoRestore|Quarantine Added Files
// 11.2: duplicate file code removed|mkdir code added|Qpath + dir
// 15.6: Complete Rebuild of this function, Added DB Table, Add Folders & Files Form and Quarantine Form processing code.
function bps_ARQ_addedFiles() {
global $wpdb, $bpspro_version;

	// Either someone is not using ARQ Add Folders & Files or they have added folders and files in a previous version of BPS Pro prior to: 15.6.
	if ( ! get_option('bulletproof_security_options_arq_add_hidden') ) {
		exit();
	}
	
	$ARQDir =  WP_CONTENT_DIR . '/bps-backup/autorestore/added-files';
	$bpspro_arq_add_table = $wpdb->prefix . "bpspro_arq_add";
	
	// ARQ Cron FailSafe Shutdown
	if ( ! is_dir($ARQDir) || $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $bpspro_arq_add_table ) ) != $bpspro_arq_add_table ) {
		exit();
	}
		
	$options = get_option('bulletproof_security_options_ARCM');

	if ( $options['bps_autorestore_cron'] == 'On' ) {

		$bpsClassFile = WP_PLUGIN_DIR . '/bulletproof-security/includes/class.php';
		$bpsClassFileARQMB = WP_CONTENT_DIR . '/bps-backup/master-backups/class.php';
	
		// Failsafe for class.php ARQ Exclude rules
		if ( file_exists($bpsClassFileARQMB) && file_exists($bpsClassFile) && @filemtime($bpsClassFileARQMB) == @filemtime($bpsClassFile) ) {
	
			$table_name = $wpdb->prefix . "bpspro_arq_quarantine";
			$bpsProLog = WP_CONTENT_DIR . '/bps-backup/logs/autorestore_log.txt';
			$timeNow = time();
			$gmt_offset = get_option( 'gmt_offset' ) * 3600;
			$timestamp = date_i18n(get_option('date_format'), strtotime("11/15-1976")) . ' ' . date_i18n(get_option('time_format'), $timeNow + $gmt_offset);
			$Qpath = WP_CONTENT_DIR . '/bps-backup/quarantine/';
		
			// 15.6: Add DB table changes: Added new column: arq_add_type: values: top-level-folder, specific-folder, single-file and q-restore.
			$forward_slash = '/';	
			$getARQAddRows = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $bpspro_arq_add_table WHERE arq_add_source LIKE %s", "%$forward_slash%") );
			
			if ( $wpdb->num_rows == 0 ) {
				exit();
			}
	
			$AddedSourcePath = array();
			$AddedDirPath = array();
			$AddedSingleFilePath = array();
			$AddedSourceSize = array();
			$AddedBackupSize = array();	
			
			foreach ( $getARQAddRows as $row ) {
				
				// Removes any single file & restored Quarantine file db rows from the AddedDirPath array.
				// Added single files do not check for any other files in a dir/folder in the Quarantine ONLY section of code below
				// and are only checked by file size if the file exists in the AutoRestore & Quarantine section of code.
				if ( $row->arq_add_type != 'single-file' && $row->arq_add_type != 'q-restore' ) {
					$AddedDirPath[] = $row->arq_add_dir;
				}
				
				$AddedSourcePath[] = $row->arq_add_source;
				$AddedSingleFilePath[] = $row->arq_add_type;
				$AddedSourceSize[] = filesize($row->arq_add_source);
				$AddedBackupSize[] = filesize($row->arq_add_backup);
			}
			
			$send_email = '';
			$result = array_diff($AddedSourceSize, $AddedBackupSize);
			
			$arq_root_hosting_account_path = get_option('bulletproof_security_options_arq_host_root');
			$arq_root_path_replace = str_replace( array( '\\\\', '\\'), '/', $arq_root_hosting_account_path['bps_arq_host_root_folder']);
	
			foreach ( $result as $diffKey => $diffValue ) {
				if ( array_key_exists($diffKey, $AddedSourcePath) ) {
					$sliceValues = array_slice($AddedSourcePath, $diffKey, 1);
					
				foreach ( $sliceValues as $pathKey => $pathValue ) {	
				
					$arq_path_parts = pathinfo($pathValue);
					$Basefilename = basename($pathValue);
					$dirname = $arq_path_parts['dirname'];
					$pathValueARCM_replace = str_replace( dirname($arq_root_path_replace), '', $dirname);
					$pathValueARCM = $ARQDir . $pathValueARCM_replace . '/' . $Basefilename;
					
					// 11.2: duplicate file code removed|mkdir code added|Qpath + added-files dir				
					// AutoRestore & Quarantine: This means an existing file in /autorestore/added-files/ will be both AutoRestored and Quarantined.
					if ( file_exists($pathValueARCM) ) {
						
						if ( ! is_dir( $Qpath . 'added-files' . $pathValueARCM_replace ) ) {
							@mkdir( $Qpath . 'added-files' . $pathValueARCM_replace, 0755, true );
						}
						
						if ( is_dir( $Qpath . 'added-files' . $pathValueARCM_replace ) ) {
							copy($pathValue, $Qpath.'added-files'.$pathValueARCM_replace.'/'.$Basefilename);
						}					
	
						$log_contents = "\r\n" . '[BPS Pro '.$bpspro_version.': non-WordPress Added File AutoRestore Logged: ' . $timestamp . ']' . "\r\n" . 'Quarantined Filename: ' . $Basefilename .  "\r\n" . 'Quarantine Path: '.$Qpath.'added-files'.$pathValueARCM_replace.'/'.$Basefilename."\r\n" . 'Restore Path: '.$pathValue."\r\n";
	
						$handle = @fopen( $bpsProLog, 'a' );
						@fwrite($handle, $log_contents);
						fclose($handle);					
						
						if ( $rows_affected = $wpdb->insert( $table_name, array( 'time' => current_time('mysql'), 'arq_quarantine_source' => $pathValue, 'arq_quarantine_backup' => $pathValueARCM, 'arq_quarantine_qpath' => $Qpath.'added-files'.$pathValueARCM_replace.'/'.$Basefilename ) ) ) {
							$send_email = 'send';	
						}
						
						copy($pathValueARCM, $pathValue);
	
					} // end if file_exists $pathValueARCM
				} // end foreach $sliceValues
			} // end array_key_exists
		} // end foreach $result - end AutoRestore processing
	
		// Start Quarantine ONLY processing
		// This checks if different files exist in a folder than exist in the db rows and quarantines them if they are different and not in the db.
		// This only affects: Top Level and Specific Added Folders & Files and not Added Individual/Single Files.
		// Added Individual/Single files are checked by file size in the AutoRestore & Quarantine section of code above.
		if ( ! empty($AddedDirPath) ) {
			$comma_separated = implode( ',', $AddedDirPath);
			$NoDupeFiles = implode( ',', array_unique( explode( ',', $comma_separated )));
		}
	
		$ExplodeUniqueFiles = explode( ',', $NoDupeFiles);
		
		$filePathIT = array();
	
		foreach ( $ExplodeUniqueFiles as $key => $value ) {
			if ( is_dir($value) ) {
				$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($value), RecursiveIteratorIterator::SELF_FIRST);
			
				foreach ( $objects as $file ) {
					if ( $file->isFile() ) {
						$filePathIT[] = $file->getPathname();
					}
				}
			}	
		}
	
		$filePathIT_str_replace = str_replace( array( '\\\\', '\\'), '/', $filePathIT );
		$resultQ = array_diff($filePathIT_str_replace, $AddedSourcePath);
		
		$arq_root_hosting_account_path = get_option('bulletproof_security_options_arq_host_root');
		$arq_root_path_replace = str_replace( array( '\\\\', '\\'), '/', $arq_root_hosting_account_path['bps_arq_host_root_folder']);
	
		foreach ( $resultQ as $KeyQ => $ValueQ ) {
	
			// 11.2: duplicate file code removed|mkdir code added|Qpath + added-files dir	
			if ( $ValueQ != '' ) {
				
				$Qpath_parts = pathinfo($ValueQ);
				$BasefilenameQ = basename($ValueQ);
				$dirname = $Qpath_parts['dirname'];
				$Qdir_path = str_replace( dirname($arq_root_path_replace), '', $dirname);
				
				if ( ! is_dir( $Qpath . 'added-files' . $Qdir_path ) ) {
					@mkdir( $Qpath . 'added-files' . $Qdir_path, 0755, true );
				}
						
				if ( is_dir( $Qpath . 'added-files' . $Qdir_path ) ) {
					rename($ValueQ, $Qpath.'added-files'.$Qdir_path.'/'.$BasefilenameQ);
				}
	
				$log_contents = "\r\n" . '[BPS Pro '.$bpspro_version.': non-WordPress Added File Quarantine Logged: ' . $timestamp . ']' . "\r\n" . 'Quarantined Filename: ' . $BasefilenameQ .  "\r\n" . 'Quarantine Path: '.$Qpath.'added-files'.$Qdir_path.'/'.$BasefilenameQ."\r\n" . 'Restore Path: '.$ValueQ."\r\n";
				
				$handle = @fopen( $bpsProLog, 'a' );
				@fwrite($handle, $log_contents);
				fclose($handle);
					
				if ( $rows_affected = $wpdb->insert( $table_name, array( 'time' => current_time('mysql'), 'arq_quarantine_source' => $ValueQ, 'arq_quarantine_backup' => $ARQDir.$Qdir_path.'/'.$BasefilenameQ, 'arq_quarantine_qpath' => $Qpath.'added-files'.$Qdir_path.'/'.$BasefilenameQ ) ) ) {
					$send_email = 'send';	
				}
			}
		}
		
		// ONLY Send 1 email alert
		if ( $send_email != '' ) {
			bps_smonitor_autorestore_email();
		}
		} // end if ( file_exists($bpsClassFileARQMB)...
	} // end if ARQ Cron is On
}

?>