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

// ini_set Form: Save DB options & create ini_set code in wp-config.php
// do not use clearstatcache(); it is buggy
if ( isset( $_POST['Submit-iniset-Options'] ) && current_user_can('manage_options') ) {
	check_admin_referer( 'bulletproof_security_options_iniSet' );

	$ini_Set_Options = array(
	'bps_iniSet_ErrorReporting' 			=> esc_html($_POST['bps_iniSet_ErrorReporting']), 
	'bps_iniSet_LogErrors' 					=> esc_html($_POST['bps_iniSet_LogErrors']), 
	'bps_iniSet_ErrorLog' 					=> stripslashes( esc_html($_POST['bps_iniSet_ErrorLog']) ), 
	'bps_iniSet_LogErrorsMaxLen' 			=> esc_html($_POST['bps_iniSet_LogErrorsMaxLen']), 
	'bps_iniSet_MemoryLimit' 				=> esc_html($_POST['bps_iniSet_MemoryLimit']), 
	'bps_iniSet_session_cookie_httponly' 	=> esc_html($_POST['bps_iniSet_session_cookie_httponly']), 
	'bps_iniSet_session_use_only_cookies' 	=> esc_html($_POST['bps_iniSet_session_use_only_cookies']), 
	'bps_iniSet_session_cookie_secure' 		=> esc_html($_POST['bps_iniSet_session_cookie_secure']), 
	'bps_iniSet_IgnoreRepeatedErrors' 		=> esc_html($_POST['bps_iniSet_IgnoreRepeatedErrors']), 
	'bps_iniSet_IgnoreRepeatedSource' 		=> esc_html($_POST['bps_iniSet_IgnoreRepeatedSource']), 
	'bps_iniSet_AllowUrlInclude' 			=> esc_html($_POST['bps_iniSet_AllowUrlInclude']), 
	'bps_iniSet_DefineSyslogVar' 			=> esc_html($_POST['bps_iniSet_DefineSyslogVar']), 
	'bps_iniSet_DisplayErrors' 				=> esc_html($_POST['bps_iniSet_DisplayErrors']), 
	'bps_iniSet_DisplayStartupErrors' 		=> esc_html($_POST['bps_iniSet_DisplayStartupErrors']), 
	'bps_iniSet_ImplicitFlush' 				=> esc_html($_POST['bps_iniSet_ImplicitFlush']), 
	'bps_iniSet_MagicQuotesRuntime' 		=> esc_html($_POST['bps_iniSet_MagicQuotesRuntime']), 
	'bps_iniSet_MaxExecutionTime' 			=> esc_html($_POST['bps_iniSet_MaxExecutionTime']), 
	'bps_iniSet_MysqlConnectTimeout' 		=> esc_html($_POST['bps_iniSet_MysqlConnectTimeout']), 
	'bps_iniSet_MysqlTraceMode' 			=> esc_html($_POST['bps_iniSet_MysqlTraceMode']), 
	'bps_iniSet_ReportMemleaks' 			=> esc_html($_POST['bps_iniSet_ReportMemleaks']) 
	);

	foreach( $ini_Set_Options as $key => $value ) {
		update_option('bulletproof_security_options_iniSet', $ini_Set_Options);
	}

	$options = get_option('bulletproof_security_options_iniSet');
	$Flockoptions = get_option('bulletproof_security_options_flock');
	$filename = ABSPATH . 'wp-config.php';
	$permswpconfig = @substr(sprintf('%o', fileperms($filename)), -4);
	$sapi_type = php_sapi_name();
	$wpconfigBackup = WP_CONTENT_DIR . '/bps-backup/autorestore/root-files/wp-config.php';
	$bpsPHPElog = WP_PLUGIN_DIR . '/bulletproof-security/admin/php/bps_php_error.log';
	$bpsPHPElogFolder = WP_CONTENT_DIR . '/bps-backup/logs/bps_php_error.log';
	$subject = file_get_contents($filename);
	$pattern = '/\/\*\* BEGIN BPS Pro ini_set Settings \*\*\/(.*?)\/\*\* END BPS Pro ini_set Settings \*\*\//s';
	//$pattern2 = '/\* That\'s all, stop editing! Happy blogging. \*/';
	$pattern2 = '/(if\s\(\s!(\s|)defined\((\s|)\'ABSPATH\'(\s|)\)\s\)(.*\s*){1}(\s|){1,}define\((\s|)\'ABSPATH\',\sdirname\((\s|)__FILE__(\s|)\)\s\.\s\'\/\'(\s|)\);)/';
	$successText = '<font color="green"><strong>'.__('Your ini_set Options have been Saved and Enabled successfully.', 'bulletproof-security').'</strong></font><br>';
	
	$replacement = "/** BEGIN BPS Pro ini_set Settings **/
@error_reporting(".$options['bps_iniSet_ErrorReporting'].");
@ini_set('log_errors','".$options['bps_iniSet_LogErrors']."');
@ini_set('error_log','".$options['bps_iniSet_ErrorLog']."'); // add the path to your php error log
@ini_set('log_errors_max_len','".$options['bps_iniSet_LogErrorsMaxLen']."');
@ini_set('memory_limit','".$options['bps_iniSet_MemoryLimit']."');
@ini_set('session.cookie_httponly','".$options['bps_iniSet_session_cookie_httponly']."');
@ini_set('session.use_only_cookies','".$options['bps_iniSet_session_use_only_cookies']."');
@ini_set('session.cookie_secure','".$options['bps_iniSet_session_cookie_secure']."');
@ini_set('ignore_repeated_errors','".$options['bps_iniSet_IgnoreRepeatedErrors']."');
@ini_set('ignore_repeated_source','".$options['bps_iniSet_IgnoreRepeatedSource']."');
@ini_set('allow_url_include','".$options['bps_iniSet_AllowUrlInclude']."');
@ini_set('define_syslog_variables','".$options['bps_iniSet_DefineSyslogVar']."');
@ini_set('display_errors','".$options['bps_iniSet_DisplayErrors']."');
@ini_set('display_startup_errors','".$options['bps_iniSet_DisplayStartupErrors']."');
@ini_set('implicit_flush','".$options['bps_iniSet_ImplicitFlush']."');
@ini_set('magic_quotes_runtime','".$options['bps_iniSet_MagicQuotesRuntime']."');
@ini_set('max_execution_time','".$options['bps_iniSet_MaxExecutionTime']."');
@ini_set('mysql.connect_timeout','".$options['bps_iniSet_MysqlConnectTimeout']."');
@ini_set('mysql.trace_mode','".$options['bps_iniSet_MysqlTraceMode']."');
@ini_set('report_memleaks','".$options['bps_iniSet_ReportMemleaks']."');
/** END BPS Pro ini_set Settings **/";

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

	if ( $options['bps_iniSet_ErrorLog'] == '' || $options['bps_iniSet_LogErrorsMaxLen'] == '' || $options['bps_iniSet_MemoryLimit'] == '' || $options['bps_iniSet_MaxExecutionTime'] == '' || $options['bps_iniSet_MysqlConnectTimeout'] == '' ) {
		echo $bps_topDiv;
		$text = '<font color="#fb0101"><strong>'.__('You cannot have blank values in any text boxes. A blank value in any text box will cause your website to crash.', 'bulletproof-security').'</font><br>'.__('Check these ini_set options text boxes and make sure that none of them have blank values: Error Log Location Set To, Log Errors Max Length, Memory Limit, Max Execution Time and MySQL Connect Timeout. The script has exited so that your website will not crash. Click on the BPS Pro P-Security menu link, go back to the ini_set Options tab page and add values in any text boxes that are blank.', 'bulletproof-security').'</strong>';
		echo $text;	
		echo $bps_bottomDiv;	
	return;	
	}

	if ( ! file_exists($filename) ) {
		echo $bps_topDiv;
		$text = '<font color="#fb0101"><strong>'.__('A wp-config.php file was NOT found in your root folder.', 'bulletproof-security').'</strong></font><br>'.__('If you have moved your wp-config.php file to a protected Server folder then you will need to add the BPS Pro ini_set coding to your wp-config.php file manually.', 'bulletproof-security').'<br>'.__('The BPS ini_set coding can be found in this file /bulletproof-security/admin/php/php-directives-code-for-wp-config.', 'bulletproof-security');
		echo $text;
		echo $bps_bottomDiv;
		
	} else {
	
	// This writes the new ini_set code if old ini_set code already exists
	if ( file_exists($filename) && preg_match($pattern, $subject, $matches) && $options['bps_iniSet_IgnoreRepeatedErrors'] != '' && $options['bps_iniSet_IgnoreRepeatedSource'] != '' ) {
		
		if ( @substr($sapi_type, 0, 6) != 'apache' || @$permswpconfig != '0666' || @$permswpconfig != '0777') { // Windows IIS, XAMPP, etc
			@chmod($filename, 0644);
		}
		
		$stringReplace = @file_get_contents($filename);
		$stringReplace = preg_replace('/\/\*\* BEGIN BPS Pro ini_set Settings \*\*\/(.*?)\/\*\* END BPS Pro ini_set Settings \*\*\//s', $replacement, $stringReplace);
		file_put_contents($filename, $stringReplace);		
		copy($filename, $wpconfigBackup);	
		copy($bpsPHPElog, $bpsPHPElogFolder);

		echo $bps_topDiv;
		echo $successText;
		echo $bps_bottomDiv;
		
		$fl_override_options = get_option('bulletproof_security_options_flock_eol');
		
		if ( $fl_override_options['bps_file_lock_override'] == 'On' ) {

			if ( $Flockoptions['bps_lock_root_htaccess'] == 'yes' ) {	
				@chmod($filename, 0400);		
			}	
		}
	}
	
	// This writes new ini_set code if ini_set code does NOT exist yet.
	if ( file_exists($filename) && ! preg_match($pattern, $subject, $matches) && preg_match($pattern2, $subject, $matches) && $options['bps_iniSet_IgnoreRepeatedErrors'] != '' && $options['bps_iniSet_IgnoreRepeatedSource'] != '' ) {
		
		if ( @substr($sapi_type, 0, 6) != 'apache' || @$permswpconfig != '0666' || @$permswpconfig != '0777') { // Windows IIS, XAMPP, etc
			@chmod($filename, 0644);
		}		
		
		$string = @file_get_contents($filename);

$ini_set_code = "/** BEGIN BPS Pro ini_set Settings **/
@error_reporting(".$options['bps_iniSet_ErrorReporting'].");
@ini_set('log_errors','".$options['bps_iniSet_LogErrors']."');
@ini_set('error_log','".$options['bps_iniSet_ErrorLog']."'); // add the path to your php error log
@ini_set('log_errors_max_len','".$options['bps_iniSet_LogErrorsMaxLen']."');
@ini_set('memory_limit','".$options['bps_iniSet_MemoryLimit']."');
@ini_set('session.cookie_httponly','".$options['bps_iniSet_session_cookie_httponly']."');
@ini_set('session.use_only_cookies','".$options['bps_iniSet_session_use_only_cookies']."');
@ini_set('session.cookie_secure','".$options['bps_iniSet_session_cookie_secure']."');
@ini_set('ignore_repeated_errors','".$options['bps_iniSet_IgnoreRepeatedErrors']."');
@ini_set('ignore_repeated_source','".$options['bps_iniSet_IgnoreRepeatedSource']."');
@ini_set('allow_url_include','".$options['bps_iniSet_AllowUrlInclude']."');
@ini_set('define_syslog_variables','".$options['bps_iniSet_DefineSyslogVar']."');
@ini_set('display_errors','".$options['bps_iniSet_DisplayErrors']."');
@ini_set('display_startup_errors','".$options['bps_iniSet_DisplayStartupErrors']."');
@ini_set('implicit_flush','".$options['bps_iniSet_ImplicitFlush']."');
@ini_set('magic_quotes_runtime','".$options['bps_iniSet_MagicQuotesRuntime']."');
@ini_set('max_execution_time','".$options['bps_iniSet_MaxExecutionTime']."');
@ini_set('mysql.connect_timeout','".$options['bps_iniSet_MysqlConnectTimeout']."');
@ini_set('mysql.trace_mode','".$options['bps_iniSet_MysqlTraceMode']."');
@ini_set('report_memleaks','".$options['bps_iniSet_ReportMemleaks']."');
/** END BPS Pro ini_set Settings **/";
		
		$stringReplace = preg_replace($pattern2, $ini_set_code . "\n\n/** Absolute path to the WordPress directory. */\n$1", $string);

		file_put_contents($filename, $stringReplace);
		copy($filename, $wpconfigBackup);	
		copy($bpsPHPElog, $bpsPHPElogFolder);
	
		echo $bps_topDiv;
		echo $successText;
		echo $bps_bottomDiv;
		
		$fl_override_options = get_option('bulletproof_security_options_flock_eol');
		
		if ( $fl_override_options['bps_file_lock_override'] == 'On' ) {

			if ( $Flockoptions['bps_lock_root_htaccess'] == 'yes' ) {	
				@chmod($filename, 0400);		
			}
		}
	}
	}

	$Elog_file_options = get_option('bulletproof_security_options2');
	$Elog_db_options = get_option('bulletproof_security_options_elog');
	$gmt_offset = get_option( 'gmt_offset' ) * 3600;
	$last_modified = date("F d Y H:i:s", time() + $gmt_offset );
	
	$Elog_set_location = array( 'bps_error_log_location' => stripslashes( esc_html($_POST['bps_iniSet_ErrorLog']) ) );	

	foreach( $Elog_set_location as $key => $value ) {
		update_option('bulletproof_security_options2', $Elog_set_location);
	}	

	$Elog_db_timestamp = array( 'bps_error_log_date_mod' => $last_modified );
	
	foreach( $Elog_db_timestamp as $key => $value ) {
		update_option('bulletproof_security_options_elog', $Elog_db_timestamp);
	}
}

// ini_set Form - delete ini_set code in wp-config.php and delete saved DB Options
if ( isset( $_POST['Submit-bps-iniset-wpconfig-remove'] ) && current_user_can('manage_options') ) {
	check_admin_referer( 'bps-iniset-wpconfig-remove' );
	
	$Flockoptions = get_option('bulletproof_security_options_flock');
	$filename = ABSPATH . 'wp-config.php';
	$wpconfigBackup = WP_CONTENT_DIR . '/bps-backup/autorestore/root-files/wp-config.php';	
	$permswpconfig = @substr( sprintf('%o', fileperms($filename) ), -4);
	$sapi_type = php_sapi_name();
	
	if ( ! file_exists($filename) ) {
		echo $bps_topDiv;
		$text = '<font color="#fb0101">'.__('A wp-config.php file was NOT found in your root folder.', 'bulletproof-security').'</font><br>'.__('If you have moved your wp-config.php file to a protected Server folder then you will need to manually delete the BPS Pro ini_set coding from your wp-config.php file.', 'bulletproof-security');
		echo $text;
		echo $bps_bottomDiv;
		
	} else {
	
	if ( file_exists($filename) ) {
		
		if ( @substr($sapi_type, 0, 6) != 'apache' || @$permswpconfig != '0666' || @$permswpconfig != '0777') { // Windows IIS, XAMPP, etc
			@chmod($filename, 0644);
		}				
		
		$stringReplace = @file_get_contents($filename);
		$stringReplace = preg_replace('/\/\*\* BEGIN BPS Pro ini_set Settings \*\*\/(.*?)\/\*\* END BPS Pro ini_set Settings \*\*\//s', '', $stringReplace);
		file_put_contents($filename, $stringReplace);
		copy($filename, $wpconfigBackup);	
		
		delete_option('bulletproof_security_options_iniSet');
		
		echo $bps_topDiv;
		$text = '<font color="green"><strong>'.__('Your ini_set Saved Options and ini_set code have been Deleted successfully.', 'bulletproof-security').'</strong></font>';
		echo $text;
		echo $bps_bottomDiv;
		
		$fl_override_options = get_option('bulletproof_security_options_flock_eol');
		
		if ( $fl_override_options['bps_file_lock_override'] == 'On' ) {

			if ( $Flockoptions['bps_lock_root_htaccess'] == 'yes' ) {	
				@chmod($filename, 0400);		
			}
		}
	}
}
}

?>
