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

/******************************************************* */
/* Folder Lock Cron schedules, function */
/******************************************************* */

add_action('bpsPro_FolderLock_check', 'bpsPro_FolderLock_scan');

// Folder Lock Cron schedule checks based on DB options saved
// Uses the ARQ Cron Schedule Intervals in arq-cron.php
function bpsPro_schedule_FolderLock_checks() {
$options = get_option('bulletproof_security_options_flock_FL');
$killit = '';
	
	if ( ! get_option('bulletproof_security_options_flock_FL') || ! $options['bps_folder_lock_cron'] || $options['bps_folder_lock_cron'] == '' ) {
		return $killit;
	}	
	
	if ( $options['bps_folder_lock_cron'] == 'On' ) {
	
		$bpsCronCheck = wp_get_schedule('bpsPro_FolderLock_check');

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

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

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

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

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

	}
	elseif ( $options['bps_folder_lock_cron'] == 'Off' ) { 
		wp_clear_scheduled_hook('bpsPro_FolderLock_check');
	}
}

add_action('init', 'bpsPro_schedule_FolderLock_checks');

// Folder Lock Cron checks current Hosting Account Root folders against whitelisted HAR DB folders.
// If a new folder is found it is locked with 400 folder permissions and added to the Folder Lock DB Table.
// Users can unlock legitimate folders using the Folder Lock Tools.
// IMPORTANT: curr_perms should not be inserted and should be left blank. The Unlock Tool uses 755 if this value is blank.
// Since a new folder (either legit or not) will be inserted once then the other check needs to be for Locked status value.
// Automatically relock the folder on each Cron run if the status value is Locked, but do not update the DB or send another email alert.
// Doing a Rescan will get the curr_perms value.
// Still not sure what I am going to do with DSO server types yet.
function bpsPro_FolderLock_scan() {

	$FLoptions = get_option('bulletproof_security_options_flock_FL');	
	
	if ( $FLoptions['bps_folder_lock_cron'] == 'Off' || ! get_option('bulletproof_security_options_flock_FL') ) {
		exit();
	}
	
	if ( time() < $FLoptions['bps_folder_lock_cron_end'] ) {
		exit();
	
	} else {

		global $wpdb;
		$FLtable_name = $wpdb->prefix . "bpspro_folder_lock";
		$searchAll = ''; // logically there should be at least 1 folder name at bare minimum
		$getFolderLockTable = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $FLtable_name WHERE folder_name != %s", "%$searchAll%" ) );
		$source = $_SERVER['DOCUMENT_ROOT'];	
		$FL_Reset_file = WP_CONTENT_DIR . '/bps-backup/master-backups/Folder-Lock-Alert-Reset.txt';
		$send_email = '';

		if ( is_dir($source) ) {
			
			$iterator = new DirectoryIterator($source);
			
			$folder_name = array();
			$folder_mod = array();									
			$folder_path = array();				
			
			foreach ( $iterator as $files ) {
			
				if ( $files->isDir() && ! $files->isDot() ) {

					$folder_name[] = $files->getFilename();
					$folder_path[$files->getFilename()] = $files->getPathname();
					$folder_mod[$files->getFilename()] = $files->getMTime();					
				}
			}			
		}	
		
		$row_path = array();
		
		// Do not add a check for $wpdb->num_rows != 0 in case there are not actually any DB rows
		foreach ( $getFolderLockTable as $row ) {
			
			$row_path[$row->folder_name] = $row->path;
			
			if ( is_dir( $row->path ) && $row->status == 'Locked' ) {
				chmod( $row->path, 0400 );
			}
		}

		$result = array_diff($folder_path, $row_path);

		if ( ! empty($result) ) {

			foreach ( $result as $key => $value ) {
		
				$last_modified = filemtime($value . '/.');
		
				if ( $insert_rows = $wpdb->insert( $FLtable_name, array( 'folder_name' => $key, 'status' => 'Locked', 'path' => $value, 'last_mod' => $last_modified, 'ulock_time' => time(), 'hlock_time' => current_time('mysql') ) ) ) {
						
					chmod( $value, 0400 );
					$send_email = 'send';

					if ( file_exists($FL_Reset_file) ) {			
						file_put_contents($FL_Reset_file, "Folder Lock Alerts");
					}		
				}
			}
		}
		
		// ONLY Send 1 email alert
		if ( $send_email != '' ) {
			bps_smonitor_folder_lock_email();
		}
	} // end if ( time() < $FLoptions['bps_folder_lock_cron_end'] ) {
}

// S-Monitor Folder Lock Email Alert
function bps_smonitor_folder_lock_email() {
global $wpdb;
$options = get_option('bulletproof_security_options_email');
	
	if ( $options['bps_flock_email'] == 'yes' ) {
		$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);

		$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%2Flock%2Fflock.php#bps-tabs-2';
		$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: F-Lock Folder Lock Has Locked A Hosting Account Root Folder - $timestamp ";
	
		$message =  '<p>To view the Hosting Account Root Folder that was locked, log into your website and go to the BPS Pro F-Lock Folder Lock page. Click the Read Me help button on the Folder Lock page for instructions on what to do next.</p>';
		$message .= '<p>Site: '.$justUrl.'</p>'; 

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

?>