<?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 - Zip & Email Log File Cron    //
/*********************************************/
// 262144 bytes = 256KB = .25MB
// 524288 bytes = 512KB = .5MB
// 1048576 bytes = 1024KB = 1MB
// 2097152 bytes = 2048KB = 2MB - if log file is larger than 2MB do not send

add_action('bpsPro_email_log_files', 'bps_Log_File_Processing');

function bpsPro_schedule_Email_Log_Files() {
	if ( ! wp_next_scheduled( 'bpsPro_email_log_files' ) ) {
		wp_schedule_event(time(), 'hourly', 'bpsPro_email_log_files');
	}
}
add_action('init', 'bpsPro_schedule_Email_Log_Files');

function bpsPro_add_hourly_email_log_cron( $schedules ) {
	$schedules['hourly'] = array('interval' => 3600, 'display' => __('Once Hourly'));
	return $schedules;
}
add_filter('cron_schedules', 'bpsPro_add_hourly_email_log_cron');

// S-Monitor - Process Log Files
// FailSafe for log files that exceed the maximum size in a short period of time
function bps_Log_File_Processing() {
$options = get_option('bulletproof_security_options_email');
$options2 = get_option('bulletproof_security_options2'); 
$SecurityLog = WP_CONTENT_DIR . '/bps-backup/logs/http_error_log.txt';
$SecurityLogMaster = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/http_error_log.txt';
$DBMLog = WP_CONTENT_DIR . '/bps-backup/logs/db_monitor_log.txt';
$DBMLogMaster = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/db_monitor_log.txt';
$DBBLog = WP_CONTENT_DIR . '/bps-backup/logs/db_backup_log.txt';
$DBBLogMaster = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/db_backup_log.txt';
$ARQLog = WP_CONTENT_DIR . '/bps-backup/logs/autorestore_log.txt';
$ARQLogMaster = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/autorestore_log.txt';
$PHPLog = ! isset($options2['bps_error_log_location']) ? '' : $options2['bps_error_log_location'];
$PHPLogMaster = WP_PLUGIN_DIR . '/bulletproof-security/admin/php/bps_php_error_master.log';
$MScanLog = WP_CONTENT_DIR . '/bps-backup/logs/mscan_log.txt';
$MScanLogMaster = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/mscan_log.txt';

$bps_security_log_size = isset($options['bps_security_log_size']) ? $options['bps_security_log_size'] : '500KB';

switch ( $bps_security_log_size ) {
	case "256KB":
		if ( file_exists($SecurityLog) && filesize($SecurityLog) >= 262144 && filesize($SecurityLog) < 524288 || file_exists($SecurityLog) && filesize($SecurityLog) > 2097152) {
			if ( $options['bps_security_log_emailL'] == 'email') {
				if ( bps_Zip_Security_Log_File() == true ) {
					bps_Email_Security_Log_File();
				}
			} elseif ( $options['bps_security_log_emailL'] == 'delete') {
				copy($SecurityLogMaster, $SecurityLog);	
			}
		}
		break;
    case "500KB":
		if ( file_exists($SecurityLog) && filesize($SecurityLog) >= 524288 && filesize($SecurityLog) < 1048576 || file_exists($SecurityLog) && filesize($SecurityLog) > 2097152) {
			if ( $options['bps_security_log_emailL'] == 'email') {
				if ( bps_Zip_Security_Log_File() == true ) {
					bps_Email_Security_Log_File();
				}
			} elseif ( $options['bps_security_log_emailL'] == 'delete') {
				copy($SecurityLogMaster, $SecurityLog);	
			}		
		}
		break;
    case "1MB":
		if ( file_exists($SecurityLog) && filesize($SecurityLog) >= 1048576 && filesize($SecurityLog) < 2097152 || file_exists($SecurityLog) && filesize($SecurityLog) > 2097152) {
			if ( $options['bps_security_log_emailL'] == 'email') {
				if ( bps_Zip_Security_Log_File() == true ) {
					bps_Email_Security_Log_File();
				}
			} elseif ( $options['bps_security_log_emailL'] == 'delete') {
				copy($SecurityLogMaster, $SecurityLog);	
			}		
		}    
	break;
}

$bps_dbm_log_size = isset($options['bps_dbm_log_size']) ? $options['bps_dbm_log_size'] : '500KB';

switch ( $bps_dbm_log_size ) {
    case "256KB":
		if ( file_exists($DBMLog) && filesize($DBMLog) >= 262144 && filesize($DBMLog) < 524288 || file_exists($DBMLog) && filesize($DBMLog) > 2097152) {
			if ( $options['bps_dbm_log_email'] == 'email') {
				if ( bps_Zip_DBM_Log_File() == true ) {
					bps_Email_DBM_Log_File();
				}
			} elseif ( $options['bps_dbm_log_email'] == 'delete') {
				copy($DBMLogMaster, $DBMLog);	
			}
		}
		break;
    case "500KB":
		if ( file_exists($DBMLog) && filesize($DBMLog) >= 524288 && filesize($DBMLog) < 1048576 || file_exists($DBMLog) && filesize($DBMLog) > 2097152) {
			if ( $options['bps_dbm_log_email'] == 'email') {
				if ( bps_Zip_DBM_Log_File() == true ) {
					bps_Email_DBM_Log_File();
				}
			} elseif ( $options['bps_dbm_log_email'] == 'delete') {
				copy($DBMLogMaster, $DBMLog);	
			}		
		}
		break;
    case "1MB":
		if ( file_exists($DBMLog) && filesize($DBMLog) >= 1048576 && filesize($DBMLog) < 2097152 || file_exists($DBMLog) && filesize($DBMLog) > 2097152) {
			if ( $options['bps_dbm_log_email'] == 'email') {
				if ( bps_Zip_DBM_Log_File() == true ) {
					bps_Email_DBM_Log_File();
				}
			} elseif ( $options['bps_dbm_log_email'] == 'delete') {
				copy($DBMLogMaster, $DBMLog);	
			}		
		}
	break;
}

$bps_dbb_log_size = isset($options['bps_dbb_log_size']) ? $options['bps_dbb_log_size'] : '500KB';

switch ( $bps_dbb_log_size ) {
    case "256KB":
		if ( file_exists($DBBLog) && filesize($DBBLog) >= 262144 && filesize($DBBLog) < 524288 || file_exists($DBBLog) && filesize($DBBLog) > 2097152) {
			if ( $options['bps_dbb_log_email'] == 'email') {
				if ( bps_Zip_DBB_Log_File() == true ) {
					bps_Email_DBB_Log_File();
				}
			} elseif ( $options['bps_dbb_log_email'] == 'delete') {
				copy($DBBLogMaster, $DBBLog);	
			}
		}
		break;
    case "500KB":
		if ( file_exists($DBBLog) && filesize($DBBLog) >= 524288 && filesize($DBBLog) < 1048576 || file_exists($DBBLog) && filesize($DBBLog) > 2097152) {
			if ( $options['bps_dbb_log_email'] == 'email') {
				if ( bps_Zip_DBB_Log_File() == true ) {
					bps_Email_DBB_Log_File();
				}
			} elseif ( $options['bps_dbb_log_email'] == 'delete') {
				copy($DBBLogMaster, $DBBLog);	
			}		
		}
		break;
    case "1MB":
		if ( file_exists($DBBLog) && filesize($DBBLog) >= 1048576 && filesize($DBBLog) < 2097152 || file_exists($DBBLog) && filesize($DBBLog) > 2097152) {
			if ( $options['bps_dbb_log_email'] == 'email') {
				if ( bps_Zip_DBB_Log_File() == true ) {
					bps_Email_DBB_Log_File();
				}
			} elseif ( $options['bps_dbb_log_email'] == 'delete') {
				copy($DBBLogMaster, $DBBLog);	
			}		
		}
	break;
}

$bps_arq_log_size = isset($options['bps_arq_log_size']) ? $options['bps_arq_log_size'] : '500KB';

switch ( $bps_arq_log_size ) {
    case "256KB":
		if ( file_exists($ARQLog) && filesize($ARQLog) >= 262144 && filesize($ARQLog) < 524288 || file_exists($ARQLog) && filesize($ARQLog) > 2097152) {
			if ( $options['bps_arq_log_email'] == 'email') {
				if ( bps_Zip_ARQ_Log_File() == true ) {
					bps_Email_ARQ_Log_File();
				}
			} elseif ( $options['bps_arq_log_email'] == 'delete') {
				copy($ARQLogMaster, $ARQLog);	
			}
		}
		break;
    case "500KB":
		if ( file_exists($ARQLog) && filesize($ARQLog) >= 524288 && filesize($ARQLog) < 1048576 || file_exists($ARQLog) && filesize($ARQLog) > 2097152) {
			if ( $options['bps_arq_log_email'] == 'email') {
				if ( bps_Zip_ARQ_Log_File() == true ) {
					bps_Email_ARQ_Log_File();
				}
			} elseif ( $options['bps_arq_log_email'] == 'delete') {
				copy($ARQLogMaster, $ARQLog);	
			}		
		}
		break;
    case "1MB":
		if ( file_exists($ARQLog) && filesize($ARQLog) >= 1048576 && filesize($ARQLog) < 2097152 || file_exists($ARQLog) && filesize($ARQLog) > 2097152) {
			if ( $options['bps_arq_log_email'] == 'email') {
				if ( bps_Zip_ARQ_Log_File() == true ) {
					bps_Email_ARQ_Log_File();
				}
			} elseif ( $options['bps_arq_log_email'] == 'delete') {
				copy($ARQLogMaster, $ARQLog);	
			}		
		}
	break;
}

$bps_php_log_size = isset($options['bps_php_log_size']) ? $options['bps_php_log_size'] : '500KB';

switch ( $bps_php_log_size ) {
    case "256KB":
		if ( file_exists($PHPLog) && filesize($PHPLog) >= 262144 && filesize($PHPLog) < 524288 || file_exists($PHPLog) && filesize($PHPLog) > 2097152) {
			if ( $options['bps_php_log_email'] == 'email') {
				if ( bps_Zip_PHP_Log_File() == true ) {
					bps_Email_PHP_Log_File();
				}
			} elseif ( $options['bps_php_log_email'] == 'delete') {
				copy($PHPLogMaster, $PHPLog);	
			}
		}
		break;
    case "500KB":
		if ( file_exists($PHPLog) && filesize($PHPLog) >= 524288 && filesize($PHPLog) < 1048576 || file_exists($PHPLog) && filesize($PHPLog) > 2097152) {
			if ( $options['bps_php_log_email'] == 'email') {
				if ( bps_Zip_PHP_Log_File() == true ) {
					bps_Email_PHP_Log_File();
				}
			} elseif ( $options['bps_php_log_email'] == 'delete') {
				copy($PHPLogMaster, $PHPLog);	
			}		
		}
		break;
    case "1MB":
		if ( file_exists($PHPLog) && filesize($PHPLog) >= 1048576 && filesize($PHPLog) < 2097152 || file_exists($PHPLog) && filesize($PHPLog) > 2097152) {
			if ( $options['bps_php_log_email'] == 'email') {
				if ( bps_Zip_PHP_Log_File() == true ) {
					bps_Email_PHP_Log_File();
				}
			} elseif ( $options['bps_php_log_email'] == 'delete') {
				copy($PHPLogMaster, $PHPLog);	
			}		
		}
	break;
}

$bps_mscan_log_size = isset($options['bps_mscan_log_size']) ? $options['bps_mscan_log_size'] : '500KB';

switch ( $bps_mscan_log_size ) {
    case "256KB":
		if ( file_exists($MScanLog) && filesize($MScanLog) >= 262144 && filesize($MScanLog) < 524288 || file_exists($MScanLog) && filesize($MScanLog) > 2097152) {
			if ( $options['bps_mscan_log_email'] == 'email') {
				if ( bps_Zip_MScan_Log_File() == true ) {
					bps_Email_MScan_Log_File();
				}
			} elseif ( $options['bps_arq_log_email'] == 'delete') {
				copy($MScanLogMaster, $MScanLog);	
			}
		}
		break;
    case "500KB":
		if ( file_exists($MScanLog) && filesize($MScanLog) >= 524288 && filesize($MScanLog) < 1048576 || file_exists($MScanLog) && filesize($MScanLog) > 2097152) {
			if ( $options['bps_mscan_log_email'] == 'email') {
				if ( bps_Zip_MScan_Log_File() == true ) {
					bps_Email_MScan_Log_File();
				}
			} elseif ( $options['bps_arq_log_email'] == 'delete') {
				copy($MScanLogMaster, $MScanLog);	
			}		
		}
		break;
    case "1MB":
		if ( file_exists($MScanLog) && filesize($MScanLog) >= 1048576 && filesize($MScanLog) < 2097152 || file_exists($MScanLog) && filesize($MScanLog) > 2097152) {
			if ( $options['bps_mscan_log_email'] == 'email') {
				if ( bps_Zip_MScan_Log_File() == true ) {
					bps_Email_MScan_Log_File();
				}
			} elseif ( $options['bps_mscan_log_email'] == 'delete') {
				copy($MScanLogMaster, $MScanLog);	
			}		
		}
	break;
}
}

// EMAIL NOTES: You cannot fake a zip file by renaming a file extension 
// The zip file must be a real zip archive or it will not be successfully attached to an email.
// A plain txt file cannot be attached to an email.
// Email Security Log File
// 11.7.1/11.8: Gets the current sec-log-master.txt Log entries and adds it to the message body.
function bps_Email_Security_Log_File() {
$options = get_option('bulletproof_security_options_email');
$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%2Fsecurity-log%2Fsecurity-log.php';
$justUrl = get_site_url(null, $path, null);
$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);
$SecurityLog = WP_CONTENT_DIR . '/bps-backup/logs/http_error_log.txt';
$SecurityLogMaster = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/http_error_log.txt';
$zip_filename = get_option('bulletproof_security_options_zip_filename'); 
$SecurityLogZip = WP_CONTENT_DIR . '/bps-backup/logs/' . $zip_filename['bps_security_log_zip_file'];
$SecLogMasterTXT = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/sec-log-master.txt';
$SecLogMasterTXT_contents = @file_get_contents($SecLogMasterTXT);
$pattern = '/Total/';
$SecLogEntries = preg_replace( $pattern, "<br />Total", $SecLogMasterTXT_contents );

	if ( file_exists($SecurityLogZip) ) {
		$attachments = array( $SecurityLogZip );
		$no_zip_attachment = '';
	} else {
		$attachments = array();
		$no_zip_attachment = '<p><font color="#fb0101"><strong>Error: Unable to attach the zip file. The zip file does not exist.</strong></font></p>';
	}
	
	$headers = array( 'Content-Type: text/html; charset=UTF-8', 'From: ' . $bps_email_from, 'Cc: ' . $bps_email_cc, 'Bcc: ' . $bps_email_bcc );
	$subject = " BPS Pro Security Log - $timestamp ";
	$message = '<p><font color="blue"><strong>Security Log File For:</strong></font></p>';
	$message .= '<p>Site: '.$justUrl.'</p>';
	$message .= '<p><font color="blue"><strong>Total # of Security Log Entries by Type:</strong></font>'. $SecLogEntries .'</p>';
	$message .= $no_zip_attachment;	

	$mailed = wp_mail($bps_email_to, $subject, $message, $headers, $attachments);

	if ( $mailed && file_exists($SecurityLogZip) ) {
		unlink($SecurityLogZip);
	
		if ( copy($SecurityLogMaster, $SecurityLog) ) {
			$last_modified_time_file = date("F d Y H:i:s", filemtime($SecurityLog) + $gmt_offset );
			$log_options = array( 'bps_security_log_date_mod' => $last_modified_time_file );
			
			foreach( $log_options as $key => $value ) {
				update_option('bulletproof_security_options_Security_log', $log_options);
			}			
		}
	}
}

// Email DB Monitor log file
function bps_Email_DBM_Log_File() {
$options = get_option('bulletproof_security_options_email');
$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%2Fdb-monitor%2Fdb-monitor.php';
$justUrl = get_site_url(null, $path, null);
$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);
$DBMLog = WP_CONTENT_DIR . '/bps-backup/logs/db_monitor_log.txt';
$DBMLogMaster = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/db_monitor_log.txt';
$zip_filename = get_option('bulletproof_security_options_zip_filename'); 
$DBMLogZip = WP_CONTENT_DIR . '/bps-backup/logs/' . $zip_filename['bps_db_monitor_log_zip_file'];
	
	if ( file_exists($DBMLogZip) ) {
		$attachments = array( $DBMLogZip );
		$no_zip_attachment = '';
	} else {
		$attachments = array();
		$no_zip_attachment = '<p><font color="#fb0101"><strong>Error: Unable to attach the zip file. The zip file does not exist.</strong></font></p>';
	}

	$headers = array( 'Content-Type: text/html; charset=UTF-8', 'From: ' . $bps_email_from, 'Cc: ' . $bps_email_cc, 'Bcc: ' . $bps_email_bcc );
	$subject = " BPS Pro DB Monitor Log - $timestamp ";
	$message = '<p>This is a regular scheduled automatic log file zip email and is NOT an alert.</p>';	
	$message .= '<p><font color="blue"><strong>DB Monitor Log File is Attached For:</strong></font></p>';
	$message .= '<p>Site: '.$justUrl.'</p>'; 
	$message .= $no_zip_attachment;
			
	$mailed = wp_mail($bps_email_to, $subject, $message, $headers, $attachments);

	if ( $mailed && file_exists($DBMLogZip) ) {
		unlink($DBMLogZip);
	
		if ( copy( $DBMLogMaster, $DBMLog ) ) {
			$last_modified_time_file = date("F d Y H:i:s", filemtime($DBMLog) + $gmt_offset );
			$log_options = array( 'bps_dbm_log_date_mod' => $last_modified_time_file );
			
			foreach( $log_options as $key => $value ) {
				update_option('bulletproof_security_options_DBM_log', $log_options);
			}
		}
	}
}

// Email DB Backup log file
function bps_Email_DBB_Log_File() {
$options = get_option('bulletproof_security_options_email');
$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%2Fdb-backup-security%2Fdb-backup-security.php';
$justUrl = get_site_url(null, $path, null);
$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);
$DBBLog = WP_CONTENT_DIR . '/bps-backup/logs/db_backup_log.txt';
$DBBLogMaster = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/db_backup_log.txt';
$zip_filename = get_option('bulletproof_security_options_zip_filename'); 
$DBBLogZip = WP_CONTENT_DIR . '/bps-backup/logs/' . $zip_filename['bps_db_backup_log_zip_file'];
	
	if ( file_exists($DBBLogZip) ) {
		$attachments = array( $DBBLogZip );
		$no_zip_attachment = '';
	} else {
		$attachments = array();
		$no_zip_attachment = '<p><font color="#fb0101"><strong>Error: Unable to attach the zip file. The zip file does not exist.</strong></font></p>';
	}

	$headers = array( 'Content-Type: text/html; charset=UTF-8', 'From: ' . $bps_email_from, 'Cc: ' . $bps_email_cc, 'Bcc: ' . $bps_email_bcc );
	$subject = " BPS Pro DB Backup Log - $timestamp ";
	$message = '<p>This is a regular scheduled automatic log file zip email and is NOT an alert.</p>';	
	$message .= '<p><font color="blue"><strong>DB Backup Log File is Attached For:</strong></font></p>';
	$message .= '<p>Site: '.$justUrl.'</p>'; 
	$message .= $no_zip_attachment;
			
	$mailed = wp_mail($bps_email_to, $subject, $message, $headers, $attachments);

	if ( $mailed && file_exists($DBBLogZip) ) {
		unlink($DBBLogZip);
	
		if ( copy( $DBBLogMaster, $DBBLog ) ) {
			$last_modified_time_file = date("F d Y H:i:s", filemtime($DBBLog) + $gmt_offset );
			$log_options = array( 'bps_dbb_log_date_mod' => $last_modified_time_file );
			
			foreach( $log_options as $key => $value ) {
				update_option('bulletproof_security_options_DBB_log', $log_options);
			}
		}
	}
}

// Email ARQ Log File
function bps_Email_ARQ_Log_File() {
$options = get_option('bulletproof_security_options_email');
$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);
$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);
$ARQLog = WP_CONTENT_DIR . '/bps-backup/logs/autorestore_log.txt';
$ARQLogMaster = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/autorestore_log.txt';
$zip_filename = get_option('bulletproof_security_options_zip_filename'); 
$ARQLogZip = WP_CONTENT_DIR . '/bps-backup/logs/' . $zip_filename['bps_quarantine_log_zip_file'];

	if ( file_exists($ARQLogZip) ) {
		$attachments = array( $ARQLogZip );
		$no_zip_attachment = '';
	} else {
		$attachments = array();
		$no_zip_attachment = '<p><font color="#fb0101"><strong>Error: Unable to attach the zip file. The zip file does not exist.</strong></font></p>';
	}

	$headers = array( 'Content-Type: text/html; charset=UTF-8', 'From: ' . $bps_email_from, 'Cc: ' . $bps_email_cc, 'Bcc: ' . $bps_email_bcc );
	$subject = " BPS Pro ARQ Log - $timestamp ";
	$message = '<p><font color="blue"><strong>AutoRestore|Quarantine Log File For:</strong></font></p>';
	$message .= '<p>Site: '.$justUrl.'</p>'; 
	$message .= $no_zip_attachment;		

	$mailed = wp_mail($bps_email_to, $subject, $message, $headers, $attachments);

	if ( $mailed && file_exists($ARQLogZip) ) {
		unlink($ARQLogZip); 

		if ( copy($ARQLogMaster, $ARQLog) ) {
			$last_modified_time_file = date("F d Y H:i:s", filemtime($ARQLog) + $gmt_offset );
			$log_options = array( 'bps_arcm_log_date_mod' => $last_modified_time_file );
			
			foreach( $log_options as $key => $value ) {
				update_option('bulletproof_security_options_ARCM_log', $log_options);
			}		
		}	
	}
}

// PHP Error Log File
function bps_Email_PHP_Log_File() {
$options = get_option('bulletproof_security_options_email');
$options2 = get_option('bulletproof_security_options2');
$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%2Fphp%2Fphp-options.php#bps-tabs-5';
$justUrl = get_site_url(null, $path, null);
$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);
$PHPLog = $options2['bps_error_log_location'];
$PHPLogMaster = WP_PLUGIN_DIR . '/bulletproof-security/admin/php/bps_php_error_master.log';
$zip_filename = get_option('bulletproof_security_options_zip_filename'); 
$PHPLogZip = WP_CONTENT_DIR . '/bps-backup/logs/' . $zip_filename['bps_php_error_log_zip_file'];

	if ( file_exists($PHPLogZip) ) {
		$attachments = array( $PHPLogZip );
		$no_zip_attachment = '';
	} else {
		$attachments = array();
		$no_zip_attachment = '<p><font color="#fb0101"><strong>Error: Unable to attach the zip file. The zip file does not exist.</strong></font></p>';
	}

	$headers = array( 'Content-Type: text/html; charset=UTF-8', 'From: ' . $bps_email_from, 'Cc: ' . $bps_email_cc, 'Bcc: ' . $bps_email_bcc );
	$subject = " BPS Pro PHP Error Log - $timestamp ";
	$message = '<p><font color="blue"><strong>PHP Error Log File For:</strong></font></p>';
	$message .= '<p>Site: '.$justUrl.'</p>'; 
	$message .= $no_zip_attachment;		

	$mailed = wp_mail($bps_email_to, $subject, $message, $headers, $attachments);

	if ( $mailed && file_exists($PHPLogZip) ) {
		unlink($PHPLogZip); 

		if ( copy($PHPLogMaster, $PHPLog) ) {
			$last_modified_time_file = date("F d Y H:i:s", filemtime($PHPLog) + $gmt_offset );
			$log_options = array( 'bps_error_log_date_mod' => $last_modified_time_file );
			
			foreach( $log_options as $key => $value ) {
				update_option('bulletproof_security_options_elog', $log_options);
			}
		}
	}
}

// Email MScan Log File
function bps_Email_MScan_Log_File() {
$options = get_option('bulletproof_security_options_email');
$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);
$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);
$MScanLog = WP_CONTENT_DIR . '/bps-backup/logs/mscan_log.txt';
$MScanLogMaster = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/mscan_log.txt';
$zip_filename = get_option('bulletproof_security_options_zip_filename'); 
$MScanLogZip = WP_CONTENT_DIR . '/bps-backup/logs/' . $zip_filename['bps_mscan_log_zip_file'];

	if ( file_exists($MScanLogZip) ) {
		$attachments = array( $MScanLogZip );
		$no_zip_attachment = '';
	} else {
		$attachments = array();
		$no_zip_attachment = '<p><font color="#fb0101"><strong>Error: Unable to attach the zip file. The zip file does not exist.</strong></font></p>';
	}

	$headers = array( 'Content-Type: text/html; charset=UTF-8', 'From: ' . $bps_email_from, 'Cc: ' . $bps_email_cc, 'Bcc: ' . $bps_email_bcc );
	$subject = " BPS Pro MScan Log - $timestamp ";
	$message = '<p><font color="blue"><strong>MScan Log File For:</strong></font></p>';
	$message .= '<p>Site: '.$justUrl.'</p>'; 
	$message .= $no_zip_attachment;		

	$mailed = wp_mail($bps_email_to, $subject, $message, $headers, $attachments);

	if ( $mailed && file_exists($MScanLogZip) ) {
		unlink($MScanLogZip); 

		if ( copy($MScanLogMaster, $MScanLog) ) {
			$last_modified_time_file = date("F d Y H:i:s", filemtime($MScanLog) + $gmt_offset );
			$log_options = array( 'bps_mscan_log_date_mod' => $last_modified_time_file );
			
			foreach( $log_options as $key => $value ) {
				update_option('bulletproof_security_options_MScan_log', $log_options);
			}
		}	
	}
}

// Counts the Total # of Security Log Entries by Type & adds the info to the top of the Security Log text file before it is zipped and emailed.
function bpsPro_SecLog_Entry_Counter_add_to_zip() {
	
	$bpsProLog = WP_CONTENT_DIR . '/bps-backup/logs/http_error_log.txt';
	$SecLogMasterTXT = WP_PLUGIN_DIR . '/bulletproof-security/admin/htaccess/sec-log-master.txt';
	$SecLogMasterTXTARQ = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/plugins/bulletproof-security/admin/htaccess/sec-log-master.txt';

	if ( file_exists($bpsProLog) ) {		

		$check_string = @file_get_contents($bpsProLog);

		// Only creates Total Log entry listings for Log Entry types that match.
		preg_match_all( '/400 POST Bad Request|400 GET Bad Request|403 GET Request|403 POST Request|404 GET Not Found Request|404 POST Not Found Request|405 HEAD Request|410 Gone POST Request|410 Gone GET Request|Idle Session Logout|Maintenance Mode - Visitor Logged|Login Form - POST Request Logged|Login Form - GET, HEAD, OTHER Request Logged|WP Register Form - POST Request Logged|WP Register Form - GET, HEAD, OTHER Request Logged|Lost Password Form - POST Request Logged|Lost Password Form - GET, HEAD, OTHER Request Logged|Comment Form User Is Logged In - POST Request Logged|Comment Form User Is Logged In - GET, HEAD, OTHER Request Logged|Comment Form User NOT Logged In - POST Request Logged|Comment Form User NOT Logged In - GET, HEAD, OTHER Request Logged|BuddyPress Register Form - POST Request Logged|BuddyPress Register Form - GET, HEAD, OTHER Request Logged|Plugin Firewall AutoPilot Mode New Whitelist Rule\(s\) Created|WP Core Automatic Update: ARQ Cron was turned Off|WP Core Automatic Update: Backup performed|WP Core Automatic Update: ARQ Cron was turned On|WP Core Manual Update\|Re-install function: Backup performed|WP Core Manual Update\|Re-install function: ARQ Cron was turned On|AJAX Shiny Plugin Update\|Install: ARQ Cron was turned Off|AJAX Shiny Theme Update\|Install: ARQ Cron was turned Off|AJAX Bulk Theme Update\|New Install: ARQ Cron was turned Off|AJAX Bulk Plugin Update: ARQ Cron was turned Off|AJAX Manual WP Core Update: ARQ Cron was turned Off|upgrader_pre_install Filter: ARQ Cron was turned Off|upgrader_post_install Filter: Backup performed|AFS Cron: ARQ Cron was turned On|WP <= 4.5.3 bpsPro_wordpress_update_core_manual function: ARQ Cron was turned Off/', $check_string, $matches );

		foreach ( $matches[0] as $key => $value ) {
				
		}
			
		$array_count_values = array_count_values($matches[0]);
		$log_contents = array();
		
		if ( ! empty( $array_count_values ) ) {

			ksort($array_count_values);
			
			foreach ( $array_count_values as $key => $value ) {
				
				$log_contents[] = 'Total ' . $key . ' Log Entries: ' . $value . "\n";
				file_put_contents($SecLogMasterTXT, $log_contents);
			}

			$SecLogMasterTXT_contents = @file_get_contents($SecLogMasterTXT);
			$stringReplace = @file_get_contents($bpsProLog);	
			$pattern = '/BPS\sPRO\sSECURITY\sLOG(.*\s*){1}=====================(.*\s*){1}=====================/';			
		
			if ( preg_match( $pattern, $stringReplace, $matches ) ) {
				$stringReplace = preg_replace( $pattern, "BPS PRO SECURITY LOG\r\n=====================\r\n=====================\r\n\n[BEGIN Total # of Security Log Entries by Type:]\r\n" . $SecLogMasterTXT_contents . "[END Total # of Security Log Entries by Type:]", $stringReplace );
		
				// BPS Pro only: for BPS free remove the "if" condition and copy.
				if ( file_put_contents($bpsProLog, $stringReplace) ) {
					@copy($SecLogMasterTXT, $SecLogMasterTXTARQ);
				}
			}
		}
	}
}

// Zip Security Log File - If ZipArchive Class is not available use PCLZip
function bps_Zip_Security_Log_File() {

	bpsPro_SecLog_Entry_Counter_add_to_zip();

	$timeNow = time();
	$gmt_offset = get_option( 'gmt_offset' ) * 3600;
	$date_time = date( 'Y-m-d-\t\i\m\e-g-i-a', $timeNow + $gmt_offset );
	$filename_timestamp = 'security_log-' . $date_time . '.txt';
	$zip_filename_timestamp = 'security-log-' .  $date_time . '.zip';

	$zf_options = array( 
	'bps_security_log_zip_file' 	=> $zip_filename_timestamp, 
	'bps_db_backup_log_zip_file' 	=> '', 
	'bps_mscan_log_zip_file' 		=> '', 
	'bps_db_monitor_log_zip_file' 	=> '',
	'bps_quarantine_log_zip_file' 	=> '', 
	'bps_php_error_log_zip_file' 	=> '' 
	);
	
	foreach( $zf_options as $key => $value ) {
		update_option('bulletproof_security_options_zip_filename', $zf_options);
	}

	// Use ZipArchive
	if ( class_exists('ZipArchive') ) {

		$zip = new ZipArchive();
		$filename = WP_CONTENT_DIR . '/bps-backup/logs/' . $zip_filename_timestamp;
		
		if ( $zip->open($filename, ZIPARCHIVE::CREATE) !== true ) {
			exit("Error: Cannot Open $filename\n");
		}
	
		$zip->addFile( WP_CONTENT_DIR . '/bps-backup/logs/http_error_log.txt', $filename_timestamp );
		$zip->close();

		return true;

	} else {

		// Use PCLZip
		define( 'PCLZIP_TEMPORARY_DIR', WP_CONTENT_DIR . '/bps-backup/logs/' );
		require_once ABSPATH . 'wp-admin/includes/class-pclzip.php';
		
		if ( ini_get( 'mbstring.func_overload' ) && function_exists( 'mb_internal_encoding' ) ) {
			$previous_encoding = mb_internal_encoding();
			mb_internal_encoding( 'ISO-8859-1' );
		}
		
		$archive = new PclZip( WP_CONTENT_DIR . '/bps-backup/logs/' . $zip_filename_timestamp );
		$http_error_log = WP_CONTENT_DIR . '/bps-backup/logs/http_error_log.txt';
		$v_content = file_get_contents( $http_error_log );
		
		$v_list = $archive->create(
    		array(
        		array(
            		PCLZIP_ATT_FILE_NAME => $filename_timestamp,
            		PCLZIP_ATT_FILE_CONTENT => $v_content
        		)
    		)
		);
		
		return true;
	
		if ( $v_list == 0 ) {
			die( "Error: " . $archive->errorInfo(true) );
			return false;	
		}
	}
}

// Zip ARQ Log File - If ZipArchive Class is not available use PCLZip
function bps_Zip_ARQ_Log_File() {
	
	$timeNow = time();
	$gmt_offset = get_option( 'gmt_offset' ) * 3600;
	$date_time = date( 'Y-m-d-\t\i\m\e-g-i-a', $timeNow + $gmt_offset );
	$filename_timestamp = 'autorestore_log-' . $date_time . '.txt';
	$zip_filename_timestamp = 'autorestore-log-' .  $date_time . '.zip';

	$zf_options = array( 
	'bps_security_log_zip_file' 	=> '', 
	'bps_db_backup_log_zip_file' 	=> '', 
	'bps_mscan_log_zip_file' 		=> '', 
	'bps_db_monitor_log_zip_file' 	=> '',
	'bps_quarantine_log_zip_file' 	=> $zip_filename_timestamp, 
	'bps_php_error_log_zip_file' 	=> '' 
	);
	
	foreach( $zf_options as $key => $value ) {
		update_option('bulletproof_security_options_zip_filename', $zf_options);
	}
	
	// Use ZipArchive
	if ( class_exists('ZipArchive') ) {

		$zip = new ZipArchive();
		$filename = WP_CONTENT_DIR . '/bps-backup/logs/' . $zip_filename_timestamp;
		
		if ( $zip->open($filename, ZIPARCHIVE::CREATE) !== true ) {
			exit("Error: Cannot Open $filename\n");
		}
	
		$zip->addFile( WP_CONTENT_DIR . '/bps-backup/logs/autorestore_log.txt', $filename_timestamp );
		$zip->close();
	
		return true;

	} else {

		// Use PCLZip
		define( 'PCLZIP_TEMPORARY_DIR', WP_CONTENT_DIR . '/bps-backup/logs/' );
		require_once ABSPATH . 'wp-admin/includes/class-pclzip.php';
		
		if ( ini_get( 'mbstring.func_overload' ) && function_exists( 'mb_internal_encoding' ) ) {
			$previous_encoding = mb_internal_encoding();
			mb_internal_encoding( 'ISO-8859-1' );
		}
		
		$archive = new PclZip(WP_CONTENT_DIR . '/bps-backup/logs/' . $zip_filename_timestamp);
		$quarantine_log = WP_CONTENT_DIR . '/bps-backup/logs/autorestore_log.txt';
		$v_content = file_get_contents( $quarantine_log );
		
		$v_list = $archive->create(
    		array(
        		array(
            		PCLZIP_ATT_FILE_NAME => $filename_timestamp,
            		PCLZIP_ATT_FILE_CONTENT => $v_content
        		)
    		)
		);		
		
		return true;  	
	
		if ( $v_list == 0 ) {
			die( "Error: " . $archive->errorInfo(true) );
			return false;
		}
	}
}

// Zip the DB Monitor Log File - If ZipArchive Class is not available use PCLZip
function bps_Zip_DBM_Log_File() {
	
	$timeNow = time();
	$gmt_offset = get_option( 'gmt_offset' ) * 3600;
	$date_time = date( 'Y-m-d-\t\i\m\e-g-i-a', $timeNow + $gmt_offset );
	$filename_timestamp = 'db_monitor_log-' . $date_time . '.txt';
	$zip_filename_timestamp = 'db-monitor-log-' .  $date_time . '.zip';

	$zf_options = array( 
	'bps_security_log_zip_file' 	=> '', 
	'bps_db_backup_log_zip_file' 	=> '', 
	'bps_mscan_log_zip_file' 		=> '', 
	'bps_db_monitor_log_zip_file' 	=> $zip_filename_timestamp,
	'bps_quarantine_log_zip_file' 	=> '', 
	'bps_php_error_log_zip_file' 	=> '' 
	);
	
	foreach( $zf_options as $key => $value ) {
		update_option('bulletproof_security_options_zip_filename', $zf_options);
	}

	// Use ZipArchive
	if ( class_exists('ZipArchive') ) {

		$zip = new ZipArchive();
		$filename = WP_CONTENT_DIR . '/bps-backup/logs/' . $zip_filename_timestamp;
		
		if ( $zip->open( $filename, ZIPARCHIVE::CREATE ) !== true ) {
			exit("Error: Cannot Open $filename\n");
		}
	
		$zip->addFile( WP_CONTENT_DIR . '/bps-backup/logs/db_monitor_log.txt', $filename_timestamp );
		$zip->close();
	
		return true;

	} else {

		// Use PCLZip
		define( 'PCLZIP_TEMPORARY_DIR', WP_CONTENT_DIR . '/bps-backup/logs/' );
		require_once ABSPATH . 'wp-admin/includes/class-pclzip.php';
		
		if ( ini_get( 'mbstring.func_overload' ) && function_exists( 'mb_internal_encoding' ) ) {
			$previous_encoding = mb_internal_encoding();
			mb_internal_encoding( 'ISO-8859-1' );
		}
		
		$archive = new PclZip( WP_CONTENT_DIR . '/bps-backup/logs/' . $zip_filename_timestamp );
		$db_monitor_log = WP_CONTENT_DIR . '/bps-backup/logs/db_monitor_log.txt';
		$v_content = file_get_contents( $db_monitor_log );
		
		$v_list = $archive->create(
    		array(
        		array(
            		PCLZIP_ATT_FILE_NAME => $filename_timestamp,
            		PCLZIP_ATT_FILE_CONTENT => $v_content
        		)
    		)
		);

		return true;
	
		if ( $v_list == 0 ) {
			die( "Error: " . $archive->errorInfo(true) );
			return false;	
		}
	}
}

// Zip the DB Backup Log File - If ZipArchive Class is not available use PCLZip
function bps_Zip_DBB_Log_File() {
	
	$timeNow = time();
	$gmt_offset = get_option( 'gmt_offset' ) * 3600;
	$date_time = date( 'Y-m-d-\t\i\m\e-g-i-a', $timeNow + $gmt_offset );
	$filename_timestamp = 'db_backup_log-' . $date_time . '.txt';
	$zip_filename_timestamp = 'db-backup-log-' .  $date_time . '.zip';

	$zf_options = array( 
	'bps_security_log_zip_file' 	=> '', 
	'bps_db_backup_log_zip_file' 	=> $zip_filename_timestamp, 
	'bps_mscan_log_zip_file' 		=> '', 
	'bps_db_monitor_log_zip_file' 	=> '',
	'bps_quarantine_log_zip_file' 	=> '', 
	'bps_php_error_log_zip_file' 	=> '' 
	);
	
	foreach( $zf_options as $key => $value ) {
		update_option('bulletproof_security_options_zip_filename', $zf_options);
	}

	// Use ZipArchive
	if ( class_exists('ZipArchive') ) {

		$zip = new ZipArchive();
		$filename = WP_CONTENT_DIR . '/bps-backup/logs/' . $zip_filename_timestamp;
		
		if ( $zip->open( $filename, ZIPARCHIVE::CREATE ) !== true ) {
			exit("Error: Cannot Open $filename\n");
		}
	
		$zip->addFile( WP_CONTENT_DIR . '/bps-backup/logs/db_backup_log.txt', $filename_timestamp );
		$zip->close();
	
		return true;

	} else {

		// Use PCLZip
		define( 'PCLZIP_TEMPORARY_DIR', WP_CONTENT_DIR . '/bps-backup/logs/' );
		require_once ABSPATH . 'wp-admin/includes/class-pclzip.php';
		
		if ( ini_get( 'mbstring.func_overload' ) && function_exists( 'mb_internal_encoding' ) ) {
			$previous_encoding = mb_internal_encoding();
			mb_internal_encoding( 'ISO-8859-1' );
		}
		
		$archive = new PclZip( WP_CONTENT_DIR . '/bps-backup/logs/' . $zip_filename_timestamp );
		$db_backup_log = WP_CONTENT_DIR . '/bps-backup/logs/db_backup_log.txt';
		$v_content = file_get_contents( $db_backup_log );
		
		$v_list = $archive->create(
    		array(
        		array(
            		PCLZIP_ATT_FILE_NAME => $filename_timestamp,
            		PCLZIP_ATT_FILE_CONTENT => $v_content
        		)
    		)
		);

		return true;
	
		if ( $v_list == 0 ) {
			die( "Error: " . $archive->errorInfo(true) );
			return false;	
		}
	}
}

// Zip PHP Error Log File - If ZipArchive Class is not available use PCLZip
function bps_Zip_PHP_Log_File() {
	
	$timeNow = time();
	$gmt_offset = get_option( 'gmt_offset' ) * 3600;
	$date_time = date( 'Y-m-d-\t\i\m\e-g-i-a', $timeNow + $gmt_offset );
	$filename_timestamp = 'php_error_log-' . $date_time . '.txt';
	$zip_filename_timestamp = 'php-error-log-' .  $date_time . '.zip';
	$options = get_option('bulletproof_security_options2');

	$zf_options = array( 
	'bps_security_log_zip_file' 	=> '', 
	'bps_db_backup_log_zip_file' 	=> '', 
	'bps_mscan_log_zip_file' 		=> '', 
	'bps_db_monitor_log_zip_file' 	=> '',
	'bps_quarantine_log_zip_file' 	=> '', 
	'bps_php_error_log_zip_file' 	=> $zip_filename_timestamp 
	);
	
	foreach( $zf_options as $key => $value ) {
		update_option('bulletproof_security_options_zip_filename', $zf_options);
	}

	// Use ZipArchive
	if ( class_exists('ZipArchive') ) {
	
		$zip = new ZipArchive();
		$filename = WP_CONTENT_DIR . '/bps-backup/logs/' . $zip_filename_timestamp;
		
		if ( $zip->open($filename, ZIPARCHIVE::CREATE) !== true ) {
			exit("Error: Cannot Open $filename\n");
		}
	
		$zip->addFile( $options['bps_error_log_location'], $filename_timestamp );
		$zip->close();
	
		return true;

	} else {

		// Use PCLZip
		define( 'PCLZIP_TEMPORARY_DIR', WP_CONTENT_DIR . '/bps-backup/logs/' );
		require_once ABSPATH . 'wp-admin/includes/class-pclzip.php';
		
		if ( ini_get( 'mbstring.func_overload' ) && function_exists( 'mb_internal_encoding' ) ) {
			$previous_encoding = mb_internal_encoding();
			mb_internal_encoding( 'ISO-8859-1' );
		}
		
		$archive = new PclZip(WP_CONTENT_DIR . '/bps-backup/logs/' . $zip_filename_timestamp);
		$php_error_log = $options['bps_error_log_location'];
		$v_content = file_get_contents( $php_error_log );
		
		$v_list = $archive->create(
    		array(
        		array(
            		PCLZIP_ATT_FILE_NAME => $filename_timestamp,
            		PCLZIP_ATT_FILE_CONTENT => $v_content
        		)
    		)
		);

		return true;
	
		if ( $v_list == 0 ) {
			die( "Error: " . $archive->errorInfo(true) );
			return false;
		}
	}
}

// Zip MScan Log File - If ZipArchive Class is not available use PclZip
function bps_Zip_MScan_Log_File() {
	
	$timeNow = time();
	$gmt_offset = get_option( 'gmt_offset' ) * 3600;
	$date_time = date( 'Y-m-d-\t\i\m\e-g-i-a', $timeNow + $gmt_offset );
	$filename_timestamp = 'mscan_log-' . $date_time . '.txt';
	$zip_filename_timestamp = 'mscan-log-' .  $date_time . '.zip';

	$zf_options = array( 
	'bps_security_log_zip_file' 	=> '', 
	'bps_db_backup_log_zip_file' 	=> '', 
	'bps_mscan_log_zip_file' 		=> $zip_filename_timestamp, 
	'bps_db_monitor_log_zip_file' 	=> '',
	'bps_quarantine_log_zip_file' 	=> '', 
	'bps_php_error_log_zip_file' 	=> '' 
	);
	
	foreach( $zf_options as $key => $value ) {
		update_option('bulletproof_security_options_zip_filename', $zf_options);
	}

	// Use ZipArchive
	if ( class_exists('ZipArchive') ) {

		$zip = new ZipArchive();
		$filename = WP_CONTENT_DIR . '/bps-backup/logs/' . $zip_filename_timestamp;
		
		if ( $zip->open($filename, ZIPARCHIVE::CREATE) !== true ) {
			exit("Error: Cannot Open $filename\n");
		}
	
		$zip->addFile( WP_CONTENT_DIR . '/bps-backup/logs/mscan_log.txt', $filename_timestamp );
		$zip->close();
	
		return true;

	} else {

		// Use PCLZip
		define( 'PCLZIP_TEMPORARY_DIR', WP_CONTENT_DIR . '/bps-backup/logs/' );
		require_once ABSPATH . 'wp-admin/includes/class-pclzip.php';
		
		if ( ini_get( 'mbstring.func_overload' ) && function_exists( 'mb_internal_encoding' ) ) {
			$previous_encoding = mb_internal_encoding();
			mb_internal_encoding( 'ISO-8859-1' );
		}
		
		$archive = new PclZip(WP_CONTENT_DIR . '/bps-backup/logs/' . $zip_filename_timestamp);
		$mscan_log = WP_CONTENT_DIR . '/bps-backup/logs/mscan_log.txt';
		$v_content = file_get_contents( $mscan_log );
		
		$v_list = $archive->create(
    		array(
        		array(
            		PCLZIP_ATT_FILE_NAME => $filename_timestamp,
            		PCLZIP_ATT_FILE_CONTENT => $v_content
        		)
    		)
		);

		return true;  	
	
		if ( $v_list == 0 ) {
			die( "Error: " . $archive->errorInfo(true) );
			return false;
		}
	}
}

/***********************************************************/
//  ARQ Turned Off - Cron Schedules, Check & Email Alerts  //
/***********************************************************/

add_action('bpsPro_ARQ_Off_check', 'bpsPro_ARQ_turned_Off_check');

// ARQ Off Cron schedule checks based on DB options saved
// Uses the ARQ Cron Schedule Intervals in arq-cron.php
function bpsPro_schedule_ARQ_Off_checks() {
$options = get_option('bulletproof_security_options_email');
$bpsARQOffCronCheck = wp_get_schedule('bpsPro_ARQ_Off_check');
$killit = '';
	
	if ( ! isset($options['bps_arq_cron_frequency_email']) || isset($options['bps_arq_cron_frequency_email']) && $options['bps_arq_cron_frequency_email'] == 'Off' ) {
		wp_clear_scheduled_hook('bpsPro_ARQ_Off_check');
		return $killit;
	}
	
	$ARQCronOptions = get_option('bulletproof_security_options_ARCM');

	if ( $ARQCronOptions['bps_autorestore_cron'] == 'Off' ) {
	
	if ( $options['bps_arq_cron_frequency_email'] == '5' ) {
	if ( $bpsARQOffCronCheck == 'minutes_10' || $bpsARQOffCronCheck == 'minutes_15' || $bpsARQOffCronCheck == 'minutes_30' || $bpsARQOffCronCheck == 'minutes_60' ) {
		wp_clear_scheduled_hook('bpsPro_ARQ_Off_check');
	}
	
	if ( ! wp_next_scheduled('bpsPro_ARQ_Off_check' ) ) {
		wp_schedule_event( time(), 'minutes_5', 'bpsPro_ARQ_Off_check' );
	}
	}
	
	if ( $options['bps_arq_cron_frequency_email'] == '10' ) {
	if ( $bpsARQOffCronCheck == 'minutes_5' || $bpsARQOffCronCheck == 'minutes_15' || $bpsARQOffCronCheck == 'minutes_30' || $bpsARQOffCronCheck == 'minutes_60' ) {
		wp_clear_scheduled_hook('bpsPro_ARQ_Off_check');
	}
	
	if ( ! wp_next_scheduled('bpsPro_ARQ_Off_check' ) ) {
		wp_schedule_event( time(), 'minutes_10', 'bpsPro_ARQ_Off_check' );
	}
	}
	
	if ( $options['bps_arq_cron_frequency_email'] == '15' ) {
	if ( $bpsARQOffCronCheck == 'minutes_5' || $bpsARQOffCronCheck == 'minutes_10' || $bpsARQOffCronCheck == 'minutes_30' || $bpsARQOffCronCheck == 'minutes_60' ) {
		wp_clear_scheduled_hook('bpsPro_ARQ_Off_check');
	}
	
	if ( ! wp_next_scheduled('bpsPro_ARQ_Off_check' ) ) {
		wp_schedule_event( time(), 'minutes_15', 'bpsPro_ARQ_Off_check' );
	}
	}
	
	if ( $options['bps_arq_cron_frequency_email'] == '30' ) {
	if ( $bpsARQOffCronCheck == 'minutes_5' || $bpsARQOffCronCheck == 'minutes_10' || $bpsARQOffCronCheck == 'minutes_15' || $bpsARQOffCronCheck == 'minutes_60' ) {
		wp_clear_scheduled_hook('bpsPro_ARQ_Off_check');
	}
	
	if ( ! wp_next_scheduled('bpsPro_ARQ_Off_check' ) ) {
		wp_schedule_event( time(), 'minutes_30', 'bpsPro_ARQ_Off_check' );
	}
	}

	if ( $options['bps_arq_cron_frequency_email'] == '60' ) {
	if ( $bpsARQOffCronCheck == 'minutes_5' || $bpsARQOffCronCheck == 'minutes_10' || $bpsARQOffCronCheck == 'minutes_15' || $bpsARQOffCronCheck == 'minutes_30' ) {
		wp_clear_scheduled_hook('bpsPro_ARQ_Off_check');
	}
	
	if ( ! wp_next_scheduled('bpsPro_ARQ_Off_check' ) ) {
		wp_schedule_event( time(), 'minutes_60', 'bpsPro_ARQ_Off_check' );
	}
	}

	}
	elseif ( $ARQCronOptions['bps_autorestore_cron'] == 'On' ) { 
		wp_clear_scheduled_hook('bpsPro_ARQ_Off_check');
	}
}

add_action('init', 'bpsPro_schedule_ARQ_Off_checks');

// ARQ Off Check: Cron with Time Restriction
// Sends email at Cron Intervals if ARQ is turned Off & logs the timestamp in the Security Log 
## 14.6: increase time interval from 120 to 300 for ARQ Off check. This will allow plenty of time for ARQ to complete and the first ARQ cron On condition will exit if ARQ is On.
function bpsPro_ARQ_turned_Off_check() {	
$ARQEmailOptions = get_option('bulletproof_security_options_email');
$ARQCronOptions = get_option('bulletproof_security_options_ARCM');	

	if ( ! $ARQEmailOptions['bps_arq_cron_frequency_email'] || $ARQEmailOptions['bps_arq_cron_frequency_email'] == 'Off' || $ARQCronOptions['bps_autorestore_cron'] == 'On' ) {
		exit();
	}
	
	if ( time() < $ARQEmailOptions['bps_arq_cron_next_email'] ) {
		exit();
	
	} else {

		$ARQ_status = get_option('bulletproof_security_options_ARQ_status');
		
		if ( $ARQ_status['bps_arq_status'] == 'On' && $ARQ_status['bps_arq_status_timestamp'] + 300 >= time() ) {
			exit();
			
		} else {

			// Send ARQ Off email alert
			$bps_email_to = $ARQEmailOptions['bps_send_email_to'];
			$bps_email_from = $ARQEmailOptions['bps_send_email_from'];
			$bps_email_cc = $ARQEmailOptions['bps_send_email_cc'];
			$bps_email_bcc = $ARQEmailOptions['bps_send_email_bcc'];
			$path = '/wp-admin/admin.php?page=bulletproof-security%2Fadmin%2Fautorestore%2Fautorestore.php';
			$justUrl = get_site_url(null, $path, null);
			$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);
	
			$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 is turned Off - $timestamp ";
			$message = '<p>When AutoRestore is turned Off on your website you will receive this BPS Pro email Alert as a reminder that AutoRestore is currently turned Off on your website. This BPS Pro email Alert is sent via a Cron Job and another email alert will be sent every 15 minutes (default setting) if ARQ is still turned Off.</p>';	
			$message .= '<p>To change or turn Off this BPS Pro email Alert, login to your website, go to the BPS Pro S-Monitor page, under Email Alerting & Log File Options change the ARQ: When AutoRestore|Quarantine is Turned Off option setting to whatever setting you would like to use instead.</p>';
			$message .= '<p>Site: '.$justUrl.'</p>'; 
		
			$mailed = wp_mail($bps_email_to, $subject, $message, $headers);	
	
			// Log a Security Log entry
			$LogTimestamp = date_i18n(get_option('date_format'), strtotime("11/15-1976")) . ' - ' . date_i18n(get_option('time_format'), $timeNow + $gmt_offset);
			$bpsProLog = WP_CONTENT_DIR . '/bps-backup/logs/http_error_log.txt';

			$log_contents = "\r\n" . '[AutoRestore Turned Off Cron Check: ' . $LogTimestamp . ']' . "\r\n" . 'This Security Log entry is created when AutoRestore is turned Off on your website. To change or turn Off this setting go to the BPS Pro S-Monitor page, under Email Alerting & Log File Options change the ARQ: When AutoRestore|Quarantine is Turned Off option setting to whatever setting you would like to use instead.' . "\r\n";

			if ( is_writable( $bpsProLog ) ) {
			if ( ! $handle = fopen( $bpsProLog, 'a' ) ) {
				exit;
			}
			if ( fwrite( $handle, $log_contents) === FALSE ) {
				exit;
			}
			fclose($handle);
			}

			// Resets the new ARQ Off Cron check time - next scheduled check time
			if ( $ARQEmailOptions['bps_arq_cron_frequency_email'] == '5' ) {
				$bps_arq_cron_next_email = time() + 300;	
			}	
			if ( $ARQEmailOptions['bps_arq_cron_frequency_email'] == '10' ) {
				$bps_arq_cron_next_email = time() + 600;	
			}
			if ( $ARQEmailOptions['bps_arq_cron_frequency_email'] == '15' ) {
				$bps_arq_cron_next_email = time() + 900;	
			}	
			if ( $ARQEmailOptions['bps_arq_cron_frequency_email'] == '30' ) {
				$bps_arq_cron_next_email = time() + 1800;	
			}	
			if ( $ARQEmailOptions['bps_arq_cron_frequency_email'] == '60' ) {
				$bps_arq_cron_next_email = time() + 3600;	
			}

			$BPS_Options = array(
			'bps_send_email_to' 			=> $ARQEmailOptions['bps_send_email_to'], 
			'bps_send_email_from' 			=> $ARQEmailOptions['bps_send_email_from'], 
			'bps_send_email_cc' 			=> $ARQEmailOptions['bps_send_email_cc'], 
			'bps_send_email_bcc' 			=> $ARQEmailOptions['bps_send_email_bcc'], 
			'bps_login_security_email' 		=> $ARQEmailOptions['bps_login_security_email'], 
			'bps_autorestore_email' 		=> $ARQEmailOptions['bps_autorestore_email'], 
			'bps_arq_log_size' 				=> $ARQEmailOptions['bps_arq_log_size'], 
			'bps_arq_log_email' 			=> $ARQEmailOptions['bps_arq_log_email'], 
			'bps_security_log_email' 		=> $ARQEmailOptions['bps_security_log_email'], 
			'bps_security_log_size' 		=> $ARQEmailOptions['bps_security_log_size'], 
			'bps_security_log_emailL' 		=> $ARQEmailOptions['bps_security_log_emailL'], 
			'bps_error_log_email' 			=> $ARQEmailOptions['bps_error_log_email'],  
			'bps_php_log_size' 				=> $ARQEmailOptions['bps_php_log_size'], 
			'bps_php_log_email' 			=> $ARQEmailOptions['bps_php_log_email'],  
			'bps_upgrade_email' 			=> $ARQEmailOptions['bps_upgrade_email'],  
			'bps_dbm_email' 				=> $ARQEmailOptions['bps_dbm_email'], 
			'bps_dbm_log_email' 			=> $ARQEmailOptions['bps_dbm_log_email'], 
			'bps_dbm_log_size' 				=> $ARQEmailOptions['bps_dbm_log_size'],  
			'bps_dbb_log_email' 			=> $ARQEmailOptions['bps_dbb_log_email'], 
			'bps_dbb_log_size' 				=> $ARQEmailOptions['bps_dbb_log_size'], 
			'bps_flock_email' 				=> $ARQEmailOptions['bps_flock_email'], 
			'bps_arq_cron_frequency_email' 	=> $ARQEmailOptions['bps_arq_cron_frequency_email'], 
			'bps_arq_cron_next_email' 		=> $bps_arq_cron_next_email, 
			'bps_hpf_email' 				=> $ARQEmailOptions['bps_hpf_email'], 
			'bps_mscan_email' 				=> $ARQEmailOptions['bps_mscan_email'], 
			'bps_mscan_log_size' 			=> $ARQEmailOptions['bps_mscan_log_size'], 
			'bps_mscan_log_email' 			=> $ARQEmailOptions['bps_mscan_log_email'], 
			'bps_plugin_updates_frequency' 	=> $ARQEmailOptions['bps_plugin_updates_frequency'], 
			'bps_plugin_updates_email' 		=> $ARQEmailOptions['bps_plugin_updates_email'],
			'bps_theme_updates_frequency' 	=> $ARQEmailOptions['bps_theme_updates_frequency'],
			'bps_theme_updates_email' 		=> $ARQEmailOptions['bps_theme_updates_email'] 
			);	
	
			foreach ( $BPS_Options as $key => $value ) {
				update_option('bulletproof_security_options_email', $BPS_Options);
			}
		}
	} // end if ( time() < $ARQEmailOptions['bps_arq_cron_next_email'] ) {
}

add_action('bpsPro_plugin_updates_cron', 'bpsPro_plugin_updates_alert');

// Plugin Update Alert Cron Schedules
// Uses the ARQ Cron Schedule Intervals in arq-cron.php
function bpsPro_plugin_updates_alert_schedule() {
	
	$options = get_option('bulletproof_security_options_email');
	$PluginUpdateCron = wp_get_schedule('bpsPro_plugin_updates_cron');	
	
	if ( ! isset($options['bps_plugin_updates_email']) || isset($options['bps_plugin_updates_email']) && $options['bps_plugin_updates_email'] == 'no' ) {
		wp_clear_scheduled_hook('bpsPro_plugin_updates_cron');
		return;
	}

	if ( isset($options['bps_plugin_updates_frequency']) ) {
	
		if ( $options['bps_plugin_updates_frequency'] == '1Hour' ) {
			if ( $PluginUpdateCron == 'hours_12' || $PluginUpdateCron == 'daily' ) {
				wp_clear_scheduled_hook('bpsPro_plugin_updates_cron');
			}
			
			if ( ! wp_next_scheduled('bpsPro_plugin_updates_cron' ) ) {
				wp_schedule_event( time(), 'minutes_60', 'bpsPro_plugin_updates_cron' );
			}
		}	
	
		if ( $options['bps_plugin_updates_frequency'] == '12Hours' ) {
			if ( $PluginUpdateCron == 'minutes_60' || $PluginUpdateCron == 'daily' ) {
				wp_clear_scheduled_hook('bpsPro_plugin_updates_cron');
			}
			
			if ( ! wp_next_scheduled('bpsPro_plugin_updates_cron' ) ) {
				wp_schedule_event( time(), 'hours_12', 'bpsPro_plugin_updates_cron' );
			}
		}
		
		if ( $options['bps_plugin_updates_frequency'] == '1Day' ) {
			if ( $PluginUpdateCron == 'minutes_60' || $PluginUpdateCron == 'hours_12' ) {
				wp_clear_scheduled_hook('bpsPro_plugin_updates_cron');
			}
			
			if ( ! wp_next_scheduled('bpsPro_plugin_updates_cron' ) ) {
				wp_schedule_event( time(), 'daily', 'bpsPro_plugin_updates_cron' );
			}
		}
	}
}

add_action('init', 'bpsPro_plugin_updates_alert_schedule');

// S-Monitor: Plugin Updates Available Alert
function bpsPro_plugin_updates_alert() {

	$options = get_option('bulletproof_security_options_email');

	if ( ! isset($options['bps_plugin_updates_email']) || isset($options['bps_plugin_updates_email']) && $options['bps_plugin_updates_email'] == 'no' ) {
		return;
	}
	
	$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/plugins.php';
	$justUrl = get_site_url(null, $path, null);
	$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);
	$headers = array( 'Content-Type: text/html; charset=UTF-8', 'From: ' . $bps_email_from, 'Cc: ' . $bps_email_cc, 'Bcc: ' . $bps_email_bcc );
	$subject = " BPS Pro Plugin and Theme Updates Alert - $timestamp ";

	if ( ! function_exists( 'get_plugin_updates' ) ) {
		require_once ABSPATH . '/wp-admin/includes/update.php';
	}

	$plugin_updates = get_plugin_updates();
	$plugin_data_array = array();

	foreach ( $plugin_updates as $key => $data ) {
		
		if ( $options['bps_plugin_updates_email'] == 'yes_all' ) {
			
			$plugin_data_array[] = 'Update Available for the ' . $data->Name . ' Plugin' . ' | Version: '. $data->update->new_version;
		
		} elseif ( $options['bps_plugin_updates_email'] == 'yes_active' ) {
		
			$active_plugins = in_array( $data->update->plugin, apply_filters('active_plugins', get_option('active_plugins')));
		
			if ( 1 == $active_plugins || is_plugin_active_for_network( $data->update->plugin ) ) {
		
				$plugin_data_array[] = 'Update Available for the ' . $data->Name . ' Plugin' . ' | Version: '. $data->update->new_version;
			}
		}
	}

	$message = '<p>Site: '.$justUrl.'</p>';
	
	if ( $options['bps_plugin_updates_email'] != 'no' ) {
		$message .= '<p><font color="blue"><strong>Plugin Updates Available:</strong></font></p>';
		
		if ( empty($plugin_data_array) ) {
			$message .= '<p><strong>No New Plugin Updates Are Available</strong></p>';
		} else {
			
			foreach ( $plugin_data_array as $key => $value ) {
				$message .= $value . '<br>';
			}
		}
	}

	if ( ! empty($plugin_data_array) ) {
		wp_mail($bps_email_to, $subject, $message, $headers);
	}
}

add_action('bpsPro_theme_updates_cron', 'bpsPro_theme_updates_alert');

// Theme Update Alert Cron Schedules
// Uses the ARQ Cron Schedule Intervals in arq-cron.php
function bpsPro_theme_updates_alert_schedule() {
	
	$options = get_option('bulletproof_security_options_email');
	$ThemeUpdateCron = wp_get_schedule('bpsPro_theme_updates_cron');	
	
	if ( ! isset($options['bps_theme_updates_email']) || isset($options['bps_theme_updates_email']) && $options['bps_theme_updates_email'] == 'no' ) {
		wp_clear_scheduled_hook('bpsPro_theme_updates_cron');
		return;
	}

	if ( isset($options['bps_theme_updates_frequency']) ) {
	
		if ( $options['bps_theme_updates_frequency'] == '1Hour' ) {
			if ( $ThemeUpdateCron == 'hours_12' || $ThemeUpdateCron == 'daily' ) {
				wp_clear_scheduled_hook('bpsPro_theme_updates_cron');
			}
			
			if ( ! wp_next_scheduled('bpsPro_theme_updates_cron' ) ) {
				wp_schedule_event( time(), 'minutes_60', 'bpsPro_theme_updates_cron' );
			}
		}	
	
		if ( $options['bps_theme_updates_frequency'] == '12Hours' ) {
			if ( $ThemeUpdateCron == 'minutes_60' || $ThemeUpdateCron == 'daily' ) {
				wp_clear_scheduled_hook('bpsPro_theme_updates_cron');
			}
			
			if ( ! wp_next_scheduled('bpsPro_theme_updates_cron' ) ) {
				wp_schedule_event( time(), 'hours_12', 'bpsPro_theme_updates_cron' );
			}
		}
		
		if ( $options['bps_theme_updates_frequency'] == '1Day' ) {
			if ( $ThemeUpdateCron == 'minutes_60' || $ThemeUpdateCron == 'hours_12' ) {
				wp_clear_scheduled_hook('bpsPro_theme_updates_cron');
			}
			
			if ( ! wp_next_scheduled('bpsPro_theme_updates_cron' ) ) {
				wp_schedule_event( time(), 'daily', 'bpsPro_theme_updates_cron' );
			}
		}
	}
}

add_action('init', 'bpsPro_theme_updates_alert_schedule');

// S-Monitor: Theme Updates Available Alert
function bpsPro_theme_updates_alert() {

	$options = get_option('bulletproof_security_options_email');

	if ( ! isset($options['bps_theme_updates_email']) || isset($options['bps_theme_updates_email']) && $options['bps_theme_updates_email'] == 'no' ) {
		return;
	}
	
	$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/themes.php';
	$justUrl = get_site_url(null, $path, null);
	$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);
	$headers = array( 'Content-Type: text/html; charset=UTF-8', 'From: ' . $bps_email_from, 'Cc: ' . $bps_email_cc, 'Bcc: ' . $bps_email_bcc );
	$subject = " BPS Pro Theme Updates Alert - $timestamp ";

	$message = '<p>Site: '.$justUrl.'</p>';
	
	if ( ! function_exists( 'get_theme_updates' ) ) {
		require_once ABSPATH . '/wp-admin/includes/update.php';
	}

	$active_theme = wp_get_theme();
	$theme_updates = get_theme_updates();
	$theme_data_array = array();

	foreach ( $theme_updates as $theme_update ) {
		
		if ( $options['bps_theme_updates_email'] == 'yes_all' ) {
			
			$theme_data_array[] = 'Update Available for the ' . $theme_update->get('Name') . ' Theme' . ' | Version: ' . $theme_update->update['new_version'];
		
		} elseif ( $options['bps_theme_updates_email'] == 'yes_active' ) {
		
			if ( $active_theme->get( 'Name' ) == $theme_update->get('Name') && $active_theme->get( 'Version' ) != $theme_update->update['new_version'] ) {
		
				$theme_data_array[] = 'Update Available for the ' . $theme_update->get('Name') . ' Theme' . ' | Version: ' . $theme_update->update['new_version'];
			}
		}		
	}
	
	if ( $options['bps_theme_updates_email'] != 'no' ) {
			$message .= '<p><font color="blue"><strong>Theme Updates Available:</strong></font></p>';
				
		if ( empty($theme_data_array) ) {
			$message .= '<p><strong>No New Theme Updates Are Available</strong></p>';
		} else {
			
			foreach ( $theme_data_array as $key => $value ) {
				$message .= $value . '<br>';
			}
		}
	}

	if ( ! empty($theme_data_array) ) {
		wp_mail($bps_email_to, $subject, $message, $headers);
	}
}

?>