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

	AITpro Software Products License Information:
	BY DOWNLOADING, INSTALLING, COPYING, ACCESSING, OR USING BulletProof Security Pro YOU AGREE TO THE TERMS OF THIS AGREEMENT. 
	IF YOU 	ARE ACCEPTING THESE TERMS ON BEHALF OF ANOTHER PERSON OR A COMPANY OR OTHER LEGAL ENTITY, YOU REPRESENT AND WARRANT
	THAT YOU HAVE FULL AUTHORITY TO BIND THAT PERSON, COMPANY, OR LEGAL ENTITY TO THESE TERMS. IF YOU DO NOT AGREE TO THESE TERMS,
	* DO NOT DOWNLOAD, INSTALL, COPY, ACCESS, OR USE BulletProof Security Pro; AND
	* PROMPTLY RETURN BulletProof Security Pro TO THE PARTY FROM WHOM YOU ACQUIRED IT. IF YOU DOWNLOADED BulletProof Security Pro
	FROM THE AITPRO WEBSITE, CONTACT AITPRO FOR A REFUND IF APPLICABLE.
	
	AITpro Software Products License Information continued:
	You agree to keep the AITpro Software Products License for BulletProof Security Pro, unmodified or altered in any way,
	with the original copy of BulletProof Security Pro that you have and any and all copies or partial copies of BulletProof
	Security Pro that You make. 
*/
// Direct calls to this file are Forbidden when core files are not present 
if ( ! current_user_can('manage_options') ) { 
		header('Status: 403 Forbidden');
		header('HTTP/1.1 403 Forbidden');
		exit();
}
	
/****************************************/
# BEGIN AutoRestore File Backup functions
/****************************************/

// Setup Wizard - Root ARQ Backup - Copy Root files to autorestore/root-files folder & rename root .htaccess to auto_.htaccess
// 13.3: Added a 5MB file size limit condition for files
function bpsSetupWizard_ARQ_root_backup($source, $dest) {
	
	if ( current_user_can('manage_options') ) {
	
	if ( ! is_dir( WP_CONTENT_DIR . '/bps-backup/autorestore/root-files' ) ) {
		mkdir( WP_CONTENT_DIR . '/bps-backup/autorestore/root-files', 0755, true );
		@chmod( WP_CONTENT_DIR . '/bps-backup/autorestore/root-files/', 0755 );
	}

	$bps_wpcontent_dir = str_replace( ABSPATH, '', WP_CONTENT_DIR );
	$source = ABSPATH;
	$dest = WP_CONTENT_DIR . '/bps-backup/autorestore/root-files';
	
	if ( is_dir($source) ) {
		$iterator = new DirectoryIterator($source);

	foreach ( $iterator as $file ) {
		try {
			if ( $file->isFile() ) {
			
				if ( $file->getSize() < 5120000 ) {
				
					if ( $file->getFilename() == '.htaccess' ) {
						copy($file->getPathname(), $dest.DIRECTORY_SEPARATOR.'auto_'.$file->getFilename());
					} else {
						copy($file->getPathname(), $dest.DIRECTORY_SEPARATOR.$file->getFilename());
					}
				}
			}
		} catch (RuntimeException $e) {}
	}		

	$text = '<strong><font color="green">'.__('ARQ Root File Backup - files backed up to /', 'bulletproof-security').$bps_wpcontent_dir.__('/bps-backup/autorestore/root-files Successfully!', 'bulletproof-security').'</font></strong><br>';
	echo $text;
	}
	}
}

// Setup Wizard - wp-admin ARQ Backup
// Copy wp-admin folders and files to autorestore folder - maintain exact directory structure - rename .htaccess to wpadmin.htaccess
// 13.3: Added a 5MB file size limit condition for files
function bpsSetupWizard_ARQ_wpadmin_backup($source, $dest) {
	if ( current_user_can('manage_options') ) {
	
	if ( ! is_dir( WP_CONTENT_DIR . '/bps-backup/autorestore/wp-admin' ) ) {
		mkdir( WP_CONTENT_DIR . '/bps-backup/autorestore/wp-admin', 0755, true );
		@chmod( WP_CONTENT_DIR . '/bps-backup/autorestore/wp-admin/', 0755 );
	}

	$source = ABSPATH.'wp-admin';
	$dest = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-admin/';
	$htaccessARQ = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-admin/.htaccess';
	$htaccessARQRename = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-admin/wpadmin.htaccess';
	$bps_wpcontent_dir = str_replace( ABSPATH, '', WP_CONTENT_DIR );

	if ( is_dir($source) ) {
		$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);	
		
		foreach ( $iterator as $file ) {
			if ( $file->isDir() )	{
				if ( ! is_dir( $dest.DIRECTORY_SEPARATOR.$iterator->getSubPathName() ) ) {
					@mkdir($dest.DIRECTORY_SEPARATOR.$iterator->getSubPathName());
				}
			} else {
				
				if ( $file->getSize() < 5120000 ) {
					copy($file, $dest.DIRECTORY_SEPARATOR.$iterator->getSubPathName());
				}
			}
		}
		$text = '<strong><font color="green">'.__('ARQ wp-admin File Backup - files backed up to /', 'bulletproof-security').$bps_wpcontent_dir.__('/bps-backup/autorestore/wp-admin Successfully!', 'bulletproof-security').'</font></strong><br>';
		echo $text;

	} else {
		copy($source, $dest);
	} // end is_dir
	
	if ( file_exists($htaccessARQ) ) {
		rename($htaccessARQ, $htaccessARQRename);
	}
	}
}

// Setup Wizard - wp-includes ARQ Backup
// Copy wp-includes folders and files to autorestore folder - maintain exact directory structure
// 13.3: Added a 5MB file size limit condition for files
function bpsSetupWizard_ARQ_wpincludes_backup($source, $dest) {
	if ( current_user_can('manage_options') ) {
	
	if ( ! is_dir( WP_CONTENT_DIR . '/bps-backup/autorestore/wp-includes' ) ) {
		mkdir( WP_CONTENT_DIR . '/bps-backup/autorestore/wp-includes', 0755, true );
		@chmod( WP_CONTENT_DIR . '/bps-backup/autorestore/wp-includes/', 0755 );
	}

	$source = ABSPATH.'wp-includes';
	$dest = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-includes/';
	$bps_wpcontent_dir = str_replace( ABSPATH, '', WP_CONTENT_DIR);	
	
	if ( is_dir($source) ) {
		$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
		
		foreach ( $iterator as $file ) {
			if ( $file->isDir() )	{
				if ( ! is_dir( $dest.DIRECTORY_SEPARATOR.$iterator->getSubPathName() ) ) {
					@mkdir($dest.DIRECTORY_SEPARATOR.$iterator->getSubPathName());
				}
			} else {
				
				if ( $file->getSize() < 5120000 ) {
					copy($file, $dest.DIRECTORY_SEPARATOR.$iterator->getSubPathName());
				}
			}
		}
		$text = '<strong><font color="green">'.__('ARQ wp-includes File Backup - files backed up to /', 'bulletproof-security').$bps_wpcontent_dir.__('/bps-backup/autorestore/wp-includes Successfully!', 'bulletproof-security').'</font></strong><br>';
		echo $text;
	} else {
		copy($source, $dest);
	}
	}
}

// Setup Wizard - wp-content ARQ Backup
// Copy wp-content folders and files to autorestore folder - maintain exact directory structure
// Exclude: 'uploads', 'upgrade', 'blogs.dir', 'bps-backup', 'w3tc', 'cache', 'plugins/si-captcha-for-wordpress/captcha/temp', 'plugins/jetpack', 'plugins',
// 13.3: Added a 5MB file size limit condition for files
function bpsSetupWizard_ARQ_wpcontent_backup($source, $dest) {
	if ( current_user_can('manage_options') ) {
		
	if ( ! is_dir( WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content' ) ) {
		mkdir( WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content', 0755, true );
		@chmod( WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/', 0755 );
	}

	$source = WP_CONTENT_DIR;
	$dest = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/';
	$bps_wpcontent_dir = str_replace( ABSPATH, '', WP_CONTENT_DIR);	
	
	if ( is_dir($source) ) {
		$dirItr    = new RecursiveDirectoryIterator($source);		
		$filterItr = new BPSCopyWPCRecursiveFilterIterator($dirItr);
		$iterator  = new RecursiveIteratorIterator($filterItr, RecursiveIteratorIterator::SELF_FIRST); 
		
		foreach ( $iterator as $file ) {
			if ( $file->isDir() ) {
				if ( ! is_dir( $dest.DIRECTORY_SEPARATOR.$iterator->getSubPathName() ) ) {
					@mkdir($dest.DIRECTORY_SEPARATOR.$iterator->getSubPathName());
				}
			} else {
				
				if ( $file->getSize() < 5120000 ) {
					@copy($file, $dest.DIRECTORY_SEPARATOR.$iterator->getSubPathName());
				}
			}
		}
		$text = '<strong><font color="green">'.__('ARQ wp-content File Backup - files backed up to /', 'bulletproof-security').$bps_wpcontent_dir.__('/bps-backup/autorestore/wp-content Successfully!', 'bulletproof-security').'</font></strong><br>';
		echo $text;
	} else {
		@copy($source, $dest);
	}
	}
}

// ARQ Root Backup file counter
// 13.3: Added a 5MB file size limit condition for files
function bpsSetupWizardFileCounterRootARQ($source, $count) {
	$source = WP_CONTENT_DIR . '/bps-backup/autorestore/root-files';
	$count = 0;
	
	if ( ! is_dir($source) ) {
		$text = '<br><font color="#fb0101"><strong>'.__('Error: The ', 'bulletproof-security').$source.__(' folder does NOT exist.', 'bulletproof-security').'</strong></font><br>';
		echo $text;
	}

	if ( is_dir($source) ) {
		$iterator = new DirectoryIterator($source);

		foreach ( $iterator as $files ) {
    		if ( $files->isFile() ) {
    			if ( $files->getSize() < 5120000 ) {
					$count++;
				}
			}
		}
	}
}

// Root website file counter
// 13.3: Added a 5MB file size limit condition for files
function bpsSetupWizardFileCounterRoot($source, $count) {
	$source = ABSPATH;
	$count = 0;
	
	if ( is_dir($source) ) {
		$iterator = new DirectoryIterator($source);

		foreach ( $iterator as $files ) {
 
			try { 
    			if ( $files->isFile() ) {
    				if ( $files->getSize() < 5120000 ) {
						$count++;
					}
				}
			} catch (RuntimeException $e) {}
		}
	}
}

// ARQ wp-admin Backup file counter
// ONLY count the total number of files in the root wp-admin directory & NOT all folders and files
// 13.3: Added a 5MB file size limit condition for files
function bpsSetupWizardFileCounterWpadminARQ($source, $count) {
	$source = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-admin/';
	$count = 0;
	
	if ( ! is_dir($source) ) {
		$text = '<br><font color="#fb0101"><strong>'.__('Error: The ', 'bulletproof-security').$source.__(' folder does NOT exist.', 'bulletproof-security').'</strong></font><br>';
		echo $text;
	}

	if ( is_dir($source) ) {
		$iterator = new DirectoryIterator($source);

		foreach ( $iterator as $files ) {
    		if ( $files->isFile() ) {
    			if ( $files->getSize() < 5120000 ) {
					$count++;
				}
			}
		}
	}
}

// wp-admin root folder website file counter
// 13.3: Added a 5MB file size limit condition for files
function bpsSetupWizardFileCounterWpadmin($source, $count) {
	$source = ABSPATH . 'wp-admin';
	$count = 0;
	
	if ( is_dir($source) ) {
		$iterator = new DirectoryIterator($source);

		foreach ( $iterator as $files ) {
    		if ( $files->isFile() ) {
    			if ( $files->getSize() < 5120000 ) {
					$count++;
				}
			}
		}
	}
}

// ARQ wp-includes Backup file counter
// ONLY count the total number of files in the root wp-includes directory & NOT all folders and files
// 13.3: Added a 5MB file size limit condition for files
function bpsSetupWizardFileCounterWpincludesARQ($source, $count) {
	$source = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-includes/';
	$count = 0;
	
	if ( ! is_dir($source) ) {
		$text = '<br><font color="#fb0101"><strong>'.__('Error: The ', 'bulletproof-security').$source.__(' folder does NOT exist.', 'bulletproof-security').'</strong></font><br>';
		echo $text;
	}

	if ( is_dir($source) ) {
		$iterator = new DirectoryIterator($source);

		foreach ( $iterator as $files ) {
    		if ( $files->isFile() ) {
    			if ( $files->getSize() < 5120000 ) {
					$count++;
				}
			}
		}
	}
}

// wp-includes root folder website file counter
// 13.3: Added a 5MB file size limit condition for files
function bpsSetupWizardFileCounterWpincludes($source, $count) {
	$source = ABSPATH . 'wp-includes';
	$count = 0;
	
	if ( is_dir($source) ) {
		$iterator = new DirectoryIterator($source);

		foreach ( $iterator as $files ) {
    		if ( $files->isFile() ) {
    			if ( $files->getSize() < 5120000 ) {
					$count++;
				}
			}
		}
	}
}

// ARQ wp-content Backup file counter
// ONLY count the total number of files in the root wp-content directory & NOT all folders and files
// 13.3: Added a 5MB file size limit condition for files
function bpsSetupWizardFileCounterWpcontentARQ($source, $count) {
	$source = WP_CONTENT_DIR . '/bps-backup/autorestore/wp-content/';
	$count = 0;
	
	if ( ! is_dir($source) ) {
		$text = '<br><font color="#fb0101"><strong>'.__('Error: The ', 'bulletproof-security').$source.__(' folder does NOT exist.', 'bulletproof-security').'</strong></font><br>';
		echo $text;
	}

	if ( is_dir($source) ) {
		$iterator = new DirectoryIterator($source);

		foreach ( $iterator as $files ) {
    		if ( $files->isFile() ) {
    			if ( $files->getSize() < 5120000 ) {
					$count++;
				}
			}
		}
	}
}

// wp-content root folder website file counter
// 13.3: Added a 5MB file size limit condition for files
function bpsSetupWizardFileCounterWpcontent($source, $count) {
	$source = WP_CONTENT_DIR;
	$count = 0;
	
	if ( is_dir($source) ) {
		$iterator = new DirectoryIterator($source);

		foreach ( $iterator as $files ) {
    		if ( $files->isFile() ) {
    			if ( $files->getSize() < 5120000 ) {
				 $count++;
				}
			}
		}
	}
}
/****************************************/
# END AutoRestore File Backup functions
/****************************************/

?>