<?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();
}

/*****************************************/
// S-Monitor Dashboard Alerts
// ARQ, Security Log, PHP Error Log, Login Security
// DB Monitor & DB Backup are in the db-security.php file
/*****************************************/

/*****************************************/
// S-Monitor AutoRestore/Quarantine Alerts
/*****************************************/

// Get the Current / Last Modifed Date of the ARCM Log File - Minutes check
function bps_getARCMLogLastMod_wp() {
$filename = WP_CONTENT_DIR . '/bps-backup/logs/autorestore_log.txt';
$gmt_offset = get_option( 'gmt_offset' ) * 3600;

if ( file_exists($filename) ) {
	$last_modified = date("F d Y H:i", filemtime($filename) + $gmt_offset );
	return $last_modified;
	}
}

// Get the Current / Last Modifed Date of the ARCM Log File - Seconds for Display
function bps_getARCMLogLastMod_wp_secs() {
$filename = WP_CONTENT_DIR . '/bps-backup/logs/autorestore_log.txt';
$gmt_offset = get_option( 'gmt_offset' ) * 3600;

if ( file_exists($filename) ) {
	$last_modified = date("F d Y H:i:s", filemtime($filename) + $gmt_offset );
	return $last_modified;
	}
}

// BPS Dashboard Alerts Admin notices
function bpsPro_dashboard_alerts_admin_notices() {
	
	if ( preg_match( '/page=stories-dashboard/', esc_html($_SERVER['QUERY_STRING']) ) || preg_match( '/page=backwpupbackups/', esc_html( $_SERVER['QUERY_STRING'] ) ) || preg_match( '/post_type=ai1ec_event/', esc_html( $_SERVER['QUERY_STRING'] ) ) ) {
		return;
	}

	if ( current_user_can('manage_options') ) { 
		bps_ARCMModTimeDiff_wp();
		bps_SecurityLogModTimeDiff_wp();
		bps_LoginSecurityResetModTimeDiff_wp();
		bpsPro_FolderLockResetModTimeDiff_wp();
		bps_smonitor_ELogModTimeDiff_wp();
	}
}
add_action('admin_notices', 'bpsPro_dashboard_alerts_admin_notices');

// AutoRestore/Quarantine Alert
// WP Dashboard Always On - No option setting to avoid any human error
// 13.3: ARQ Alerts can now be turned off or displayed in BPS Pro plugin pages only.
function bps_ARCMModTimeDiff_wp() {
	
	if ( current_user_can('manage_options') ) {
	
	// New installations - BPS Pro has NOT been activated & S-Monitor Options have not been saved & / or the Setup Wizard has not been run
	if ( ! get_option('bulletproof_security_options_activation') || ! get_option('bulletproof_security_options_monitor') ) {
		return;
	}
	
	$options = get_option('bulletproof_security_options_ARCM_log');

	if ( ! isset($options['bps_arcm_log_date_mod']) ) {
		
		if ( ! preg_match( '/page=bulletproof-security\/admin\/wizard\//', esc_html( $_SERVER['REQUEST_URI'] ) ) ) {			
		
		$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);">'.__('The AutoRestore|Quarantine Log Last Modified Time in DB Needs To Be Set', 'bulletproof-security').'<br>'.__('Run the ', 'bulletproof-security').'<a href="'.admin_url( 'admin.php?page=bulletproof-security/admin/wizard/wizard.php' ).'">'.__('Pre-Installation Wizard and Setup Wizard', 'bulletproof-security').'</a>'.__(' or...', 'bulletproof-security').'<br><a href="'.admin_url( 'admin.php?page=bulletproof-security/admin/quarantine/quarantine.php#bps-tabs-2' ).'">'.__('Click Here', 'bulletproof-security').'</a>'.__(' to go to the Quarantine Log page and click the Reset Last Modified Time in DB button.', 'bulletproof-security').'<br>'.__('View the Quarantine Log Read Me Help button for additional help info.', 'bulletproof-security').'</div>';
		echo $text;
		}
	}
	
	$Smonitor_options = get_option('bulletproof_security_options_monitor');

	if ( $Smonitor_options['bps_autorestore_alerts'] == 'wpOn' ) {	
	if ( strcmp( bps_getARCMLogLastMod_wp_secs(), $options['bps_arcm_log_date_mod'] ) != 0 && $options['bps_arcm_log_date_mod'] != '' && strcmp( bps_getARCMLogLastMod_wp(), getBPSInstallTime() ) == 0 ) {

		$gmt_offset = get_option( 'gmt_offset' ) * 3600;
		$filename = WP_CONTENT_DIR . '/bps-backup/logs/autorestore_log.txt';

		touch( $filename, strtotime( $options['bps_arcm_log_date_mod'] ) - $gmt_offset );
	}
	
	$db_timestamp_plus_one = strtotime( $options['bps_arcm_log_date_mod'] ) + 3600;
	$db_timestamp_minus_one = strtotime( $options['bps_arcm_log_date_mod'] ) - 3600;	
	
	if ( $options['bps_arcm_log_date_mod'] != '' && strcmp( bps_getARCMLogLastMod_wp_secs(), $options['bps_arcm_log_date_mod'] ) != 0 && $db_timestamp_plus_one != strtotime( bps_getARCMLogLastMod_wp_secs() ) && $db_timestamp_minus_one != strtotime( bps_getARCMLogLastMod_wp_secs() ) ) {
		$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="#fb0101">'.__('AutoRestore|Quarantine Alert', 'bulletproof-security').'</font><br>'.__('A file has been quarantined. ', 'bulletproof-security').'<a href="'.admin_url( 'admin.php?page=bulletproof-security/admin/quarantine/quarantine.php' ).'">'.__('Click Here', 'bulletproof-security').'</a>'.__(' to go to Quarantine to view the quarantined file.', 'bulletproof-security').'<br>'.__('To clear/reset this alert click the Reset Last Modified Time in DB button on the Quarantine Log tab page.', 'bulletproof-security').'</div>';
		echo $text;
	}
	}
	}
}

//************************************//
// S-Monitor Security Log Alerts      //
//************************************//

// S-Monitor - Get the Last Modifed Date of the Security Log File - Seconds
function bps_getSecurityLogLastMod_secs() {
$filename = WP_CONTENT_DIR . '/bps-backup/logs/http_error_log.txt';
$gmt_offset = get_option( 'gmt_offset' ) * 3600;

if ( file_exists($filename) ) {
	$last_modified = date("F d Y H:i:s", filemtime($filename) + $gmt_offset );
	return $last_modified;
	}
}

// S-Monitor - Get the Last Modifed Date of the Security Log File - Minutes
function bps_getSecurityLogLastMod_minutes() {
$filename = WP_CONTENT_DIR . '/bps-backup/logs/http_error_log.txt';
$gmt_offset = get_option( 'gmt_offset' ) * 3600;

if ( file_exists($filename) ) {
	$last_modified = date("F d Y H:i", filemtime($filename) + $gmt_offset );
	return $last_modified;
	}
}

// S-Monitor - String comparison of Security Log DB Last Modified Time and Actual File Last Modified Time
// Security Log reset Last Modified Time on upgrade installation - Always On
function bps_SecurityLogModTimeDiff_wp() {

	if ( current_user_can('manage_options') ) {
	
	global $pagenow;
	// New installations - BPS Pro has NOT been activated & S-Monitor Options have not been saved & / or the Setup Wizard has not been run
	if ( ! get_option('bulletproof_security_options_activation') || ! get_option('bulletproof_security_options_monitor') ) {
		return;
	}
	
	$SecLog_mod_date = get_option('bulletproof_security_options_Security_log');

	if ( ! isset($SecLog_mod_date['bps_security_log_date_mod']) ) {
		
		if ( ! preg_match( '/page=bulletproof-security\/admin\/wizard\//', esc_html( $_SERVER['REQUEST_URI'] ) ) ) {			
		
		$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);">'.__('The Security Log Last Modified Time in DB Needs To Be Set', 'bulletproof-security').'<br>'.__('Run the ', 'bulletproof-security').'<a href="'.admin_url( 'admin.php?page=bulletproof-security/admin/wizard/wizard.php' ).'">'.__('Pre-Installation Wizard and Setup Wizard', 'bulletproof-security').'</a>'.__(' or...', 'bulletproof-security').'<br><a href="'.admin_url( 'admin.php?page=bulletproof-security/admin/security-log/security-log.php' ).'">'.__('Click Here', 'bulletproof-security').'</a>'.__(' to go to the Security Log page and click the Reset Last Modified Time in DB button.', 'bulletproof-security').'<br>'.__('View the Security Log Read Me Help button for additional help info.', 'bulletproof-security').'</div>';
		echo $text;
		}
	}
	
	$options = get_option('bulletproof_security_options_monitor');

	if ( $options['bps_SecLog_entry'] == 'wpOn' ) {
	if ( strcmp( bps_getSecurityLogLastMod_secs(), $SecLog_mod_date['bps_security_log_date_mod'] ) != 0 && $SecLog_mod_date['bps_security_log_date_mod'] != '' && strcmp( bps_getSecurityLogLastMod_minutes(), getBPSInstallTime() ) == 0 ) {

		$gmt_offset = get_option( 'gmt_offset' ) * 3600;
		$filename = WP_CONTENT_DIR . '/bps-backup/logs/http_error_log.txt';

		touch( $filename, strtotime( $SecLog_mod_date['bps_security_log_date_mod'] ) - $gmt_offset );
	}
	
	$db_timestamp_plus_one = strtotime( $SecLog_mod_date['bps_security_log_date_mod'] ) + 3600;
	$db_timestamp_minus_one = strtotime( $SecLog_mod_date['bps_security_log_date_mod'] ) - 3600;
	
	// Daylight Savings: do not display alert when time change occurs
	if ( $SecLog_mod_date['bps_security_log_date_mod'] != '' && strcmp( bps_getSecurityLogLastMod_secs(), $SecLog_mod_date['bps_security_log_date_mod'] ) != 0 && $db_timestamp_plus_one != strtotime( bps_getSecurityLogLastMod_secs() ) && $db_timestamp_minus_one != strtotime( bps_getSecurityLogLastMod_secs() ) ) {
		
		$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="#fb0101">'.__('Security Log Alert', 'bulletproof-security').'</font><br>'.__('A New Security Log Entry Has Been Logged. ', 'bulletproof-security').'<a href="'.admin_url( 'admin.php?page=bulletproof-security/admin/security-log/security-log.php' ).'">'.__('Click Here', 'bulletproof-security').'</a>'.__(' to go to your Security Log.', 'bulletproof-security').'<br>'.__('To remove/clear this alert click the Reset Last Modified Time in DB button on the Security Log page.', 'bulletproof-security').'<br>'.__('To turn Off Security Log Dashboard Alerts ', 'bulletproof-security').'<a href="'.admin_url( 'admin.php?page=bulletproof-security/admin/monitor/monitor.php' ).'">'.__('Click Here', 'bulletproof-security').'</a>'.__(' to go to the S-Monitor page.', 'bulletproof-security').'</div>';
		
		if ( 'update-core.php' != $pagenow && 'about.php' != $pagenow ) {
			echo $text;
		}
	}
	}
	}
}

/*************************************/
// S-Monitor PHP Error Log Alerts
/*************************************/

// S-Monitor - PHP Error Log new errors - Get the Last Modifed Date of the PHP Error Log File
// This is the file path that is saved to DB - can be another path besides the default BPS log file location
function bps_getPhpELogLastMod_smonitor() {
$options = get_option('bulletproof_security_options2');
$filename = $options['bps_error_log_location'];
$gmt_offset = get_option( 'gmt_offset' ) * 3600;

if ( file_exists($filename) ) {
	$last_modified = date("F d Y H:i:s", filemtime($filename) + $gmt_offset );
	return $last_modified;
	}
}

// Get the Last Modifed Date of the Default PHP Error Log File - Minutes check
// IMPORTANT NOTE: This is the default BPS PHP Error log in /admin/ not the active php error log
// filemtime will match for the bulletproof-security.php file and bps_php_error.log
function bps_getPhpELogLastMod_minutes() {
$filename = WP_PLUGIN_DIR . '/bulletproof-security/admin/php/bps_php_error.log';
$gmt_offset = get_option( 'gmt_offset' ) * 3600;

if ( file_exists($filename) ) {
	$last_modified = date("F d Y H:i", filemtime($filename) + $gmt_offset );
	return $last_modified;
	}
}

// S-Monitor - PHP Error Log Alerts
function bps_smonitor_ELogModTimeDiff_wp() {
	
	if ( current_user_can('manage_options') ) {
	
	$options = get_option('bulletproof_security_options_monitor');

	if ( isset($options['bps_PHP_ELog_error']) && $options['bps_PHP_ELog_error'] == 'Off' ) {
		return;	
	}
	
	// New installations - BPS Pro has NOT been activated & S-Monitor Options have not been saved & / or the Setup Wizard has not been run
	if ( ! get_option('bulletproof_security_options_activation') || ! get_option('bulletproof_security_options_monitor') ) {
		return;
	}

	if ( $options['bps_PHP_ELog_error'] == 'wpOn' ) {
	
		$ELog_mod_time = get_option('bulletproof_security_options_elog');	
	
	if ( strcmp( bps_getPhpELogLastMod_smonitor(), $ELog_mod_time['bps_error_log_date_mod'] ) != 0 && $ELog_mod_time['bps_error_log_date_mod'] != '' && strcmp( bps_getPhpELogLastMod_minutes(), getBPSInstallTime() ) == 0 ) {

		$gmt_offset = get_option( 'gmt_offset' ) * 3600;
		$ELog_location = get_option('bulletproof_security_options2');		
		
		touch( $ELog_location['bps_error_log_location'], strtotime( $ELog_mod_time['bps_error_log_date_mod'] ) - $gmt_offset );
	}
	
	$db_timestamp_plus_one = strtotime( $ELog_mod_time['bps_error_log_date_mod'] ) + 3600;
	$db_timestamp_minus_one = strtotime( $ELog_mod_time['bps_error_log_date_mod'] ) - 3600;

	if ( strcmp( bps_getPhpELogLastMod_smonitor(), $ELog_mod_time['bps_error_log_date_mod'] ) != 0 && $db_timestamp_plus_one != strtotime( bps_getPhpELogLastMod_smonitor() ) && $db_timestamp_minus_one != strtotime( bps_getPhpELogLastMod_smonitor() ) ) { // 0 is equal strings
		$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="#fb0101">'.__('A PHP Error has been logged in your PHP Error Log', 'bulletproof-security').'</font><br><a href="'.admin_url( 'admin.php?page=bulletproof-security/admin/php/php-options.php#bps-tabs-5' ).'">'.__('Click Here', 'bulletproof-security').'</a>'.__(' To go to the P-Security PHP Error Log page.', 'bulletproof-security').'<br>'.__('To remove/clear this Alert click on the Reset Last Modified Time in DB button.', 'bulletproof-security').'</div>';
		echo $text;
	}
	}
	}
}

/*************************************/
// S-Monitor Login Security Alerts
/*************************************/

// S-Monitor - Get the Last Modifed Date of the Login Security Reset File - Seconds
function bps_getLoginSecurityResetFileLastMod_secs() {
$filename = WP_CONTENT_DIR . '/bps-backup/master-backups/Login-Security-Alert-Reset.txt';
$gmt_offset = get_option( 'gmt_offset' ) * 3600;

if ( file_exists($filename) ) {
	$last_modified = date("F d Y H:i:s", filemtime($filename) + $gmt_offset );
	return $last_modified;
	}
}

// S-Monitor - Get the Last Modifed Date of the Login Security Reset File - Minutes
function bps_getLoginSecurityResetFileLastMod_minutes() {
$filename = WP_CONTENT_DIR . '/bps-backup/master-backups/Login-Security-Alert-Reset.txt';
$gmt_offset = get_option( 'gmt_offset' ) * 3600;

if ( file_exists($filename) ) {
	$last_modified = date("F d Y H:i", filemtime($filename) + $gmt_offset );
	return $last_modified;
	}
}

// S-Monitor - String comparison of Login Security DB Last Modified Time and Actual File Last Modified Time - WP
function bps_LoginSecurityResetModTimeDiff_wp() {

	if ( current_user_can('manage_options') ) {
	
	$options = get_option('bulletproof_security_options_monitor');

	if ( isset($options['bps_login_security_alerts']) && $options['bps_login_security_alerts'] == 'Off' ) {
		return;	
	}

	global $blog_id;
	if ( is_multisite() && $blog_id != 1 ) {	
		return;
	}

	// New installations - BPS Pro has NOT been activated & S-Monitor Options have not been saved & / or the Setup Wizard has not been run
	if ( ! get_option('bulletproof_security_options_activation') || ! get_option('bulletproof_security_options_monitor') ) {
		return;
	}

	$LSAlertsoptions = get_option('bulletproof_security_options_login_alerts');  

	if ( ! isset($LSAlertsoptions['bps_login_security_db_mod_time']) ) {
		return;
	}

	if ( $options['bps_login_security_alerts'] == 'wpOn' ) {
	if ( strcmp( bps_getLoginSecurityResetFileLastMod_secs(), $LSAlertsoptions['bps_login_security_db_mod_time'] ) != 0 && $LSAlertsoptions['bps_login_security_db_mod_time'] != '' && strcmp( bps_getLoginSecurityResetFileLastMod_minutes(), getBPSInstallTime() ) == 0 ) {
	
		$gmt_offset = get_option( 'gmt_offset' ) * 3600;
		$filename = WP_CONTENT_DIR . '/bps-backup/master-backups/Login-Security-Alert-Reset.txt';
		
		touch( $filename, strtotime( $LSAlertsoptions['bps_login_security_db_mod_time'] ) - $gmt_offset );
	}
	
	$db_timestamp_plus_one = strtotime( $LSAlertsoptions['bps_login_security_db_mod_time'] ) + 3600;
	$db_timestamp_minus_one = strtotime( $LSAlertsoptions['bps_login_security_db_mod_time'] ) - 3600;	
	
	if ( $LSAlertsoptions['bps_login_security_db_mod_time'] != '' && strcmp( bps_getLoginSecurityResetFileLastMod_secs(), $LSAlertsoptions['bps_login_security_db_mod_time'] ) != 0 && $db_timestamp_plus_one != strtotime( bps_getLoginSecurityResetFileLastMod_secs() ) && $db_timestamp_minus_one != strtotime( bps_getLoginSecurityResetFileLastMod_secs() ) ) {
		$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="#fb0101">'.__('Login Security Alert', 'bulletproof-security').'</font><br><a href="'.admin_url( 'admin.php?page=bulletproof-security/admin/login/login.php' ).'">'.__('Click Here', 'bulletproof-security').'</a>'.__(' to go to the Login Security page.', 'bulletproof-security').'<br>'.__('To reset/clear Login Security Alerts click the Reset|Clear Login Security Alerts button on the Login Security page.', 'bulletproof-security').'</div>';
		echo $text;
	}
	}
	}
}

/*************************************/
// S-Monitor Folder Lock Alerts
/*************************************/
// S-Monitor - Get the Last Modifed Date of the Folder Lock Reset File - Seconds
function bps_getFolderLockResetFileLastMod_secs() {
$filename = WP_CONTENT_DIR . '/bps-backup/master-backups/Folder-Lock-Alert-Reset.txt';
$gmt_offset = get_option( 'gmt_offset' ) * 3600;

if ( file_exists($filename) ) {
	$last_modified = date("F d Y H:i:s", filemtime($filename) + $gmt_offset );
	return $last_modified;
	}
}

// S-Monitor - Get the Last Modifed Date of the Folder Lock Reset File - Minutes
function bps_getFolderLockResetFileLastMod_minutes() {
$filename = WP_CONTENT_DIR . '/bps-backup/master-backups/Folder-Lock-Alert-Reset.txt';
$gmt_offset = get_option( 'gmt_offset' ) * 3600;

if ( file_exists($filename) ) {
	$last_modified = date("F d Y H:i", filemtime($filename) + $gmt_offset );
	return $last_modified;
	}
}

// S-Monitor - String comparison of Folder Lock DB Last Modified Time and Actual File Last Modified Time - WP
// 11.5: bpsPro_new_feature_notification() function will do the DB options updates and display a Dismiss Notice for Folder Lock.
function bpsPro_FolderLockResetModTimeDiff_wp() {

	if ( current_user_can('manage_options') ) {
	
	$options = get_option('bulletproof_security_options_monitor');

	if ( isset($options['bps_flock_FL_alerts']) && $options['bps_flock_FL_alerts'] == 'Off' ) {
		return;	
	}

	global $blog_id;
	if ( is_multisite() && $blog_id != 1 ) {	
		return;
	}

	// New installations - BPS Pro has NOT been activated & S-Monitor Options have not been saved & / or the Setup Wizard has not been run
	if ( ! get_option('bulletproof_security_options_activation') || ! get_option('bulletproof_security_options_monitor') ) {
		return;
	}
	
	$FLAlertsoptions = get_option('bulletproof_security_options_flock_FL_alerts');  

	if ( ! isset($FLAlertsoptions['bps_folderlock_db_mod_time']) ) {
		return;
	}

	if ( $options['bps_flock_FL_alerts'] == 'wpOn' ) {
	if ( strcmp( bps_getFolderLockResetFileLastMod_secs(), $FLAlertsoptions['bps_folderlock_db_mod_time'] ) != 0 && $FLAlertsoptions['bps_folderlock_db_mod_time'] != '' && strcmp( bps_getFolderLockResetFileLastMod_minutes(), getBPSInstallTime() ) == 0 ) {
	
		$gmt_offset = get_option( 'gmt_offset' ) * 3600;
		$filename = WP_CONTENT_DIR . '/bps-backup/master-backups/Folder-Lock-Alert-Reset.txt';
		
		touch( $filename, strtotime( $FLAlertsoptions['bps_folderlock_db_mod_time'] ) - $gmt_offset );
	}
	
	$db_timestamp_plus_one = strtotime( $FLAlertsoptions['bps_folderlock_db_mod_time'] ) + 3600;
	$db_timestamp_minus_one = strtotime( $FLAlertsoptions['bps_folderlock_db_mod_time'] ) - 3600;	
	
	if ( $FLAlertsoptions['bps_folderlock_db_mod_time'] != '' && strcmp( bps_getFolderLockResetFileLastMod_secs(), $FLAlertsoptions['bps_folderlock_db_mod_time'] ) != 0 && $db_timestamp_plus_one != strtotime( bps_getFolderLockResetFileLastMod_secs() ) && $db_timestamp_minus_one != strtotime( bps_getFolderLockResetFileLastMod_secs() ) ) {
		$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="#fb0101">'.__('F-Lock Folder Lock Alert', 'bulletproof-security').'</font><br>'.__('A Hosting Account Root Folder has been locked.', 'bulletproof-security').'<br><a href="'.admin_url( 'admin.php?page=bulletproof-security/admin/lock/flock.php#bps-tabs-2' ).'">'.__('Click Here', 'bulletproof-security').'</a>'.__(' to go to the F-Lock Folder Lock page. Click the Folder Lock Help Info Read Me button for help information.', 'bulletproof-security').'<br>'.__('To reset/clear Folder Lock Alerts click the Reset|Clear Folder Lock Alerts button on the Folder Lock page.', 'bulletproof-security').'</div>';
		echo $text;
	}
	}
	}
}

/**************************************************/
// Hourly Cron Email Alerts: Security Log & PHP Error Log 
/**************************************************/

// Add Action for the Security Log Cron job to check for new log entries
add_action('bpsPro_security_log_check', 'bps_smonitor_SecurityLogModTimeDiff_wp_email');

// The Scheduled Cron Job to check the Security Log hourly
function bpsPro_schedule_SecurityLog_check() {
	
$options = get_option('bulletproof_security_options_email');

	if ( isset($options['bps_security_log_email']) && $options['bps_security_log_email'] == 'yes' ) {
	
		if ( ! wp_next_scheduled( 'bpsPro_security_log_check' ) ) {
			wp_schedule_event(time(), 'hourly', 'bpsPro_security_log_check');
		}
	}
}
add_action('init', 'bpsPro_schedule_SecurityLog_check');

// Add Security Log Cron to cron_schedules
function bpsPro_add_hourly_security_log_cron( $schedules ) {
	$schedules['hourly'] = array('interval' => 3600, 'display' => __('Once Hourly'));
	return $schedules;
}
add_filter('cron_schedules', 'bpsPro_add_hourly_security_log_cron');

// S-Monitor Email Alert - The scheduled Cron function to check the Security Log new log entries
// String comparison of DB Last Modified Time and Actual File Last Modified Time
function bps_smonitor_SecurityLogModTimeDiff_wp_email() {
$options1 = get_option('bulletproof_security_options_Security_log');
$options = get_option('bulletproof_security_options_email');
$last_modified_time_secs = bps_getSecurityLogLastMod_secs();
$last_modified_time_db = $options1['bps_security_log_date_mod'];
	
	if ( strcmp( $last_modified_time_secs, $last_modified_time_db ) != 0 ) { // 0 is equal strings
		if ( $options['bps_security_log_email'] == 'yes' ) {
		
		$bps_email = $options['bps_send_email_to'];
		$bps_email_from = $options['bps_send_email_from'];
		$bps_email_cc = $options['bps_send_email_cc'];
		$bps_email_bcc = $options['bps_send_email_bcc'];
		$path = '/wp-admin/admin.php?page=bulletproof-security%2Fadmin%2Fsecurity-log%2Fsecurity-log.php';
		$justUrl = get_site_url(null, $path, null);
		
		$headers = array( 'Content-Type: text/html; charset=UTF-8', 'From: ' . $bps_email_from, 'Cc: ' . $bps_email_cc, 'Bcc: ' . $bps_email_bcc );
		$mail_Subject = " BPS Pro Alert: A New Security Log Entry Has Been Logged ";
	
		$mail_message = '<p><font color="#fb0101"><strong>A New Security Log Entry Has Been Logged in your Security Log File.</strong></font></p>';
		$mail_message .= '<p>Site: '.$justUrl.'</p>'; 
		$mail_message .= '<p>To view the Security Log go to the BPS Security Log page.</p>';
		
		wp_mail( $bps_email, $mail_Subject, $mail_message, $headers );
		
		} elseif ( $options['bps_security_log_email'] == 'no' ) { 
			wp_clear_scheduled_hook('bpsPro_security_log_check');
		}
	}
}

// Add Action for the PHP Error Log Cron job to check for new php errors
add_action('bpsPro_php_elog_check', 'bps_smonitor_ELogModTimeDiff_wp_email');

// The Scheduled Cron Job to check the PHP Error Log hourly
function bpsPro_schedule_Elog_check() {

$options = get_option('bulletproof_security_options_email');

	if ( isset($options['bps_error_log_email']) && $options['bps_error_log_email'] == 'yes' ) {
		
		if ( ! wp_next_scheduled( 'bpsPro_php_elog_check' ) ) {
			wp_schedule_event(time(), 'hourly', 'bpsPro_php_elog_check');
		}
	}
}
add_action('init', 'bpsPro_schedule_Elog_check');

// Add ELog Cron to cron_schedules
function bpsPro_add_hourly_elog_cron( $schedules ) {
	$schedules['hourly'] = array('interval' => 3600, 'display' => __('Once Hourly'));
	return $schedules;
}
add_filter('cron_schedules', 'bpsPro_add_hourly_elog_cron');

// S-Monitor Email Alert - The scheduled Cron function to check the PHP Error Log new errors
// String comparison of DB Last Modified Time and Actual File Last Modified Time
function bps_smonitor_ELogModTimeDiff_wp_email() {
$options2 = get_option('bulletproof_security_options_elog');
$options = get_option('bulletproof_security_options_email');
$last_modified_time = bps_getPhpELogLastMod_smonitor();
$last_modified_time_db = $options2['bps_error_log_date_mod'];
	
	if ( strcmp($last_modified_time, $last_modified_time_db) != 0 ) { // 0 is equal strings
	if ( $options['bps_error_log_email'] == 'yes' ) {
	
	$bps_email = $options['bps_send_email_to'];
	$bps_email_from = $options['bps_send_email_from'];
	$bps_email_cc = $options['bps_send_email_cc'];
	$bps_email_bcc = $options['bps_send_email_bcc'];
	$path = '/wp-admin/admin.php?page=bulletproof-security%2Fadmin%2Fphp%2Fphp-options.php#bps-tabs-5';
	$justUrl = get_site_url(null, $path, null);
	
	$headers = array( 'Content-Type: text/html; charset=UTF-8', 'From: ' . $bps_email_from, 'Cc: ' . $bps_email_cc, 'Bcc: ' . $bps_email_bcc );
	$mail_Subject = " BPS Pro Alert: A PHP Error has been logged ";

	$mail_message = '<p><font color="#fb0101"><strong>A PHP Error has been logged in your PHP Error Log File.</strong></font></p>';
	$mail_message .= '<p>Site: '.$justUrl.'</p>'; 
	$mail_message .= '<p>To view the php error go to the BPS P-Security PHP Error Log page.</p>';
	
	wp_mail( $bps_email, $mail_Subject, $mail_message, $headers );
	
	} elseif ( $options['bps_error_log_email'] == 'no' ) { 
		wp_clear_scheduled_hook('bpsPro_php_elog_check');
	}
	}
}

// S-Monitor AutoRestore Email Alert 
// function fired in ARQ functions in arq-cron.php in real-time
function bps_smonitor_autorestore_email() {
global $wpdb;
$options = get_option('bulletproof_security_options_email');
	
	if ( $options['bps_autorestore_email'] == 'yes' ) {
	
	$timeNow = date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) );
	$timePlusOne = date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) + 60 );
	$Qtable = $wpdb->prefix . "bpspro_arq_quarantine";
	$DB_row_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $Qtable WHERE time >= %s AND time <= %s", $timeNow, $timePlusOne ) );
	$forward_slash = "/";
	$QuarantineRows = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $Qtable WHERE arq_quarantine_source LIKE %s ORDER BY id DESC LIMIT 25", "%$forward_slash%" ) );

	$bps_email_to = $options['bps_send_email_to'];
	$bps_email_from = $options['bps_send_email_from'];
	$bps_email_cc = $options['bps_send_email_cc'];
	$bps_email_bcc = $options['bps_send_email_bcc'];
	$path = '/wp-admin/admin.php?page=bulletproof-security%2Fadmin%2Fquarantine%2Fquarantine.php';
	$justUrl = get_site_url(null, $path, null);

	$headers = array( 'Content-Type: text/html; charset=UTF-8', 'From: ' . $bps_email_from, 'Cc: ' . $bps_email_cc, 'Bcc: ' . $bps_email_bcc );
	$subject = " BPS Pro Alert: AutoRestore|Quarantine Has Quarantined A File ";
	
	$message = '<p><font color="#fb0101"><strong>A file has been sent to Quarantine</strong></font></p>';
	$message .= '<p><strong>Total Number of Files Quarantined: <font color="blue">'.$DB_row_count.'</font></strong></p>';
	$message .= '<p>Site: '.$justUrl.'</p>';
	$message .=  '<p>To view the file that was quarantined, log into your website and go to the BPS Pro Quarantine page. Click the Read Me help button on the Quarantine page for instructions on what to do next. Files that were quarantined are listed below. Note: If you previously had files in Quarantine then those files will also be listed below. The maximum number of quarantined files that will be listed in AutoRestore|Quarantine Alert emails is 25 files. Quarantined files are listed in descending order from newest to oldest quarantined files.</p>';
	$message .= '<p>If you are manually editing or uploading files using FTP or your web host control panel and you do not want those files to be quarantined while you are working on them click this link: http://forum.ait-pro.com/forums/topic/autorestore-quarantine-guide-read-me-first/#procedural-steps<p>';
	
	foreach ( $QuarantineRows as $row ) {
			
		$message .= '<strong>File: </strong>' . $row->arq_quarantine_source . ' -- <strong>Quarantine Time: </strong>' . $row->time . '<br>';
	}

	wp_mail( $bps_email_to, $subject, $message, $headers );
	}
}

// S-Monitor MScan Email Alert - email is only sent when Scheduled scanning is On. 
// function fired in mscan-ajax-functions.php
function bps_smonitor_mscan_email() {
global $wpdb;
	$options = get_option('bulletproof_security_options_email');
	$MScan_options = get_option('bulletproof_security_options_MScan');
	
	if ( isset($options['bps_mscan_email']) && $options['bps_mscan_email'] == 'yes' && isset($MScan_options['mscan_scan_frequency']) && $MScan_options['mscan_scan_frequency'] != 'Off' ) {
	
		$timeNow = date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) );
		$timePlusOne = date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) + 60 );
	
		$bps_email_to = $options['bps_send_email_to'];
		$bps_email_from = $options['bps_send_email_from'];
		$bps_email_cc = $options['bps_send_email_cc'];
		$bps_email_bcc = $options['bps_send_email_bcc'];
		$path = '/wp-admin/admin.php?page=bulletproof-security%2Fadmin%2Fmscan%2Fmscan.php';
		$justUrl = get_site_url(null, $path, null);
	
		$headers = array( 'Content-Type: text/html; charset=UTF-8', 'From: ' . $bps_email_from, 'Cc: ' . $bps_email_cc, 'Bcc: ' . $bps_email_bcc );
		$subject = " BPS Pro Alert: MScan has detected a suspicious file or code or DB Entry ";
		
		$message = '<p><font color="#fb0101"><strong>A suspicious file or code or DB Entry was detected by MScan</strong></font></p>';
		$message .=  '<p>To view the file or DB Entry that was detected as suspicious, log into your website, go to the BPS Pro MScan page and check the View|Ignore|Delete Suspicious Files Form and the View|Ignore Suspicious DB Entries Form. For additional help info click the Read Me help button on the MScan page.</p>';
		$message .= '<p>Site: '.$justUrl.'</p>'; 
	
		wp_mail( $bps_email_to, $subject, $message, $headers );
	}
}

?>