Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import and export options metabox #102

Merged
merged 10 commits into from
Jan 26, 2024
73 changes: 63 additions & 10 deletions admin/CF7_AntiSpam_Admin_Customizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use CF7_AntiSpam\Core\CF7_AntiSpam;
use CF7_AntiSpam\Core\CF7_Antispam_Geoip;
use WP_Query;
use function cli\err;

/**
* The plugin settings.
Expand Down Expand Up @@ -1007,17 +1008,67 @@ private function cf7a_input_cron_schedule( $input, $input_name, $cron_task, $sch
}


private function cf7a_clean_agnostic( $value ) {
if ( is_bool( $value ) ) {
$input = boolval( $value );
} elseif ( is_numeric( $value ) ) {
$input = floatval( $value );
} else {
$input = sanitize_text_field( $value );
}
return $input;
}

/**
* Clean and sanitize a value recursively.
*
* @param string $key The key of the value to be cleaned.
* @param mixed $value The value to be cleaned.
*
* @return array|bool|int|string
*/
private function cf7a_clean_recursive( $json_data ) {
$input = array();
foreach ( $json_data as $key => $value ) {
if ( is_array( $value ) || is_object( $value ) ) {
$input[ $key ] = $this->cf7a_clean_recursive( $value );
} else {
$input[ $key ] = $this->cf7a_clean_agnostic( $value );
}
}
return $input;
}

/**
* Sanitize each setting field as needed
*
* @param array $input Contains all settings fields as array keys.
* @return array $options sanitized
*/
public function cf7a_sanitize_options( $input ) {
$new_input['cf7a_enabled'] = isset( $input['cf7a_enabled'] ) ? 1 : 0;
/* get the import options */
$new_input = $this->options;
$import_data = isset( $_POST['to-import'] ) ? $_POST['to-import'] : false;
if ( ! empty( $import_data ) ) {
$json_data = json_decode( wp_unslash( $_POST['to-import'] ) );
$input = $this->cf7a_clean_recursive( $json_data );
// monkey pathing arrays that needs to be imploded
$input['bad_ip_list'] = implode( ',', $input['bad_ip_list'] );
$input['ip_whitelist'] = implode( ',', $input['ip_whitelist'] );
$input['bad_email_strings_list'] = implode( ',', $input['bad_email_strings_list'] );
$input['bad_user_agent_list'] = implode( ',', $input['bad_user_agent_list'] );
$input['dnsbl_list'] = implode( ',', $input['dnsbl_list'] );
$input['honeypot_input_names'] = implode( ',', $input['honeypot_input_names'] );
$input['bad_words_list'] = implode( ',', $input['bad_words_list'] );
$input['languages_locales']['allowed'] = implode( ',', $input['languages_locales']['allowed'] );
$input['languages_locales']['disallowed'] = implode( ',', $input['languages_locales']['disallowed'] );
$input['cf7a_enabled'] = 1;
$input['cf7a_enable'] = 1;
$input['cf7a_version'] = CF7ANTISPAM_VERSION;
}
error_log( print_r( $input, true ) );

/* get the existing options */
$new_input = $this->options;
$new_input['cf7a_enabled'] = isset( $input['cf7a_enabled'] ) ? 1 : 0;

$new_input['cf7a_enable'] = isset( $input['cf7a_enable'] ) ? $input['cf7a_enable'] : $new_input['cf7a_enable'];

Expand All @@ -1036,15 +1087,17 @@ public function cf7a_sanitize_options( $input ) {
* Checking if the enable_geoip_download is not set (note the name is $new_input but actually is the copy of the stored options)
* and the user has chosen to enable the geoip, in this case download the database if needed
*/
if ( empty( $new_input['enable_geoip_download'] ) && isset( $input['enable_geoip_download'] ) ) {
if ( empty( $import_data ) && empty( $new_input['enable_geoip_download'] ) && isset( $input['enable_geoip_download'] ) ) {
$this->cf7a_enable_geo( $new_input['enable_geoip_download'] );
}

$new_input['enable_geoip_download'] = isset( $input['enable_geoip_download'] ) ? 1 : 0;
$new_input['geoip_dbkey'] = isset( $input['geoip_dbkey'] ) ? sanitize_textarea_field( $input['geoip_dbkey'] ) : false;


$new_input['geoip_dbkey'] = isset( $input['geoip_dbkey'] ) ? sanitize_textarea_field( $input['geoip_dbkey'] ) : false;

/* browser language check enabled */
$new_input['check_language'] = isset( $input['check_language'] ) ? 1 : 0;
$new_input['check_language'] = ! empty( $input['check_language'] ) ? 1 : 0;

/* geo-ip location check enabled */
$new_input['check_geo_location'] = isset( $input['check_geo_location'] ) ? 1 : 0;
Expand All @@ -1057,6 +1110,7 @@ public function cf7a_sanitize_options( $input ) {
? $this->cf7a_settings_format_user_input( sanitize_textarea_field( $input['languages_locales']['disallowed'] ) )
: array();


/* max attempts before ban */
$new_input['max_attempts'] = isset( $input['max_attempts'] ) ? intval( $input['max_attempts'] ) : 3;

Expand Down Expand Up @@ -1126,7 +1180,7 @@ public function cf7a_sanitize_options( $input ) {
/* honeyform */
$new_input['check_honeyform'] = isset( $input['check_honeyform'] ) ? 1 : 0;
$new_input['honeyform_position'] = ! empty( $input['honeyform_position'] ) ? sanitize_title( $input['honeyform_position'] ) : 'wp_body_open';
$new_input['honeyform_excluded_pages'] = ! empty( $input['honeyform_excluded_pages'] ) ? cf7a_str_array_to_uint_array( $input['honeyform_excluded_pages'] ) : '';
$new_input['honeyform_excluded_pages'] = ! empty( $input['honeyform_excluded_pages'] ) ? cf7a_str_array_to_uint_array( $input['honeyform_excluded_pages'] ) : array();

/* identity protection */
$new_input['mailbox_protection_multiple_send'] = isset( $input['mailbox_protection_multiple_send'] ) ? 1 : 0;
Expand Down Expand Up @@ -1526,9 +1580,8 @@ public function cf7a_honeyform_excluded_pages_callback() {
}
}

$admin_options = get_option( 'cf7a_options' );
$excluded = isset( $admin_options['honeyform_excluded_pages'] ) ? $admin_options['honeyform_excluded_pages'] : array();
$str_excluded = '';
$excluded = isset( $this->options['honeyform_excluded_pages'] ) ? $this->options['honeyform_excluded_pages'] : array();
$str_excluded = '';
if ( is_array( $excluded ) ) {
foreach ( $excluded as $entry ) {
$str_excluded .= '<option selected="true" value="' . $entry . '">' . get_the_title( $entry ) . '</option>';
Expand Down
32 changes: 32 additions & 0 deletions admin/CF7_AntiSpam_Admin_Display.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ public function cf7a_display_content() {
?>
</form>
</div>

<?php
// Export/Import Options
$this->cf7a_export_options();
}

/**
Expand Down Expand Up @@ -188,6 +191,35 @@ public static function cf7a_get_blacklisted_table() {
}
}

private function cf7a_export_options() {

?>
<div id="cf7a_export_import" class="cf7-antispam card">
<h3><?php esc_html_e( 'Export/Import Options', 'cf7-antispam' ); ?></h3>
<form id="import-export-options" method="post" action="<?php echo admin_url( 'options.php' ); ?>">
<?php
$option_group = 'cf7_antispam_options';
wp_nonce_field( "$option_group-options" );
?>
<input type="hidden" name="option_page" value="cf7_antispam_options">
<input type="hidden" name="action" value="update">
<input type="hidden" name="type" value="import">
<input type="hidden" name="_wp_http_referer" value="<?php echo esc_url( add_query_arg( 'settings-updated', 'true', admin_url( 'admin.php?page=cf7-antispam' ) ) ); ?>">

<!-- Form field -->
<label for="cf7a_options_area"><?php esc_html__( 'Copy or paste here the settings to import it or export it', 'cf7-antispam' ); ?></label>
<textarea id="cf7a_options_area" rows="5"><?php echo wp_json_encode( $this->options, JSON_PRETTY_PRINT ); ?></textarea>

<!-- buttons -->
<div class="cf7a_buttons cf7a_buttons_export_import">
<button type="button" id="cf7a_download_button" class="button button-primary">Download</button>
<button type="submit" id="cf7a_import_button" class="button button-secondary">Import</button>
</div>
</form>
</div>
<?php
}

/**
* It outputs a card with a bunch of buttons that perform various actions on the database
*
Expand Down
2 changes: 1 addition & 1 deletion core/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ function cf7a_str_array_to_uint_array( $str_array ) {
array_filter(
$str_array,
function ( $value ) {
return is_numeric( $value ) && $value > 0 && intval( $value ) == $value;
return is_int( $value ) || is_numeric( $value ) && $value > 0 && intval( $value ) == $value;
}
)
);
Expand Down
108 changes: 58 additions & 50 deletions engine/CF7_AntiSpam_Activator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,53 +39,54 @@ class CF7_AntiSpam_Activator {
*/
public static function init_vars() {
self::$default_cf7a_options = array(
'cf7a_enable' => true,
'cf7a_version' => CF7ANTISPAM_VERSION,
'cf7a_customizations_class' => CF7ANTISPAM_HONEYPOT_CLASS,
'cf7a_customizations_prefix' => CF7ANTISPAM_PREFIX,
'cf7a_cipher' => 'aes-128-cbc',
'cf7a_score_preset' => 'weak',
'cf7a_disable_reload' => true,
'check_bot_fingerprint' => true,
'check_bot_fingerprint_extras' => true,
'append_on_submit' => true,
'check_time' => true,
'check_time_min' => 6,
'check_time_max' => YEAR_IN_SECONDS,
'check_bad_ip' => true,
'autostore_bad_ip' => true,
'max_attempts' => 3,
'unban_after' => 'disabled',
'check_bad_words' => true,
'check_bad_email_strings' => true,
'check_bad_user_agent' => true,
'check_dnsbl' => false,
'check_refer' => true,
'check_honeypot' => true,
'check_honeyform' => false,
'identity_protection_user' => false,
'identity_protection_wp' => false,
'enable_geoip_download' => false,
'geoip_dbkey' => false,
'check_language' => false,
'check_geo_location' => false,
'honeyform_position' => 'the_content',
'enable_b8' => true,
'b8_threshold' => 0.95,
'enable_advanced_settings' => 0,
'bad_words_list' => array(),
'bad_ip_list' => array(),
'ip_whitelist' => array(),
'bad_email_strings_list' => array(),
'bad_user_agent_list' => array(),
'dnsbl_list' => array(),
'honeypot_input_names' => array(),
'honeyform_excluded_pages' => array(),
'languages_locales' => array(
'cf7a_enable' => true,
'cf7a_version' => CF7ANTISPAM_VERSION,
'cf7a_customizations_class' => CF7ANTISPAM_HONEYPOT_CLASS,
'cf7a_customizations_prefix' => CF7ANTISPAM_PREFIX,
'cf7a_cipher' => 'aes-128-cbc',
'cf7a_score_preset' => 'weak',
'cf7a_disable_reload' => true,
'check_bot_fingerprint' => true,
'check_bot_fingerprint_extras' => true,
'append_on_submit' => true,
'check_time' => true,
'check_time_min' => 6,
'check_time_max' => YEAR_IN_SECONDS,
'check_bad_ip' => true,
'autostore_bad_ip' => true,
'max_attempts' => 3,
'unban_after' => 'disabled',
'check_bad_words' => true,
'check_bad_email_strings' => true,
'check_bad_user_agent' => true,
'check_dnsbl' => false,
'check_refer' => true,
'check_honeypot' => true,
'check_honeyform' => false,
'identity_protection_user' => false,
'identity_protection_wp' => false,
'enable_geoip_download' => false,
'geoip_dbkey' => false,
'check_language' => false,
'check_geo_location' => false,
'honeyform_position' => 'the_content',
'enable_b8' => true,
'b8_threshold' => 0.95,
'enable_advanced_settings' => 0,
'mailbox_protection_multiple_send' => 0,
'bad_words_list' => array(),
'bad_ip_list' => array(),
'ip_whitelist' => array(),
'bad_email_strings_list' => array(),
'bad_user_agent_list' => array(),
'dnsbl_list' => array(),
'honeypot_input_names' => array(),
'honeyform_excluded_pages' => array(),
'languages_locales' => array(
'allowed' => array(),
'disallowed' => array(),
),
'score' => array(
'score' => array(
'_fingerprinting' => 0.1,
'_time' => 0.3,
'_bad_string' => 0.5,
Expand Down Expand Up @@ -213,7 +214,19 @@ public static function update_options( $reset_options = false ) {

$options = get_option( 'cf7a_options' );

if ( false !== $options && ! $reset_options ) {
if ( false === $options || $reset_options ) {

// Delete all options
if ( $reset_options === true ) {
delete_option( 'cf7a_options' );
}

/* if the plugin options are missing Init the plugin with the default option + the default settings */
$new_options = array_merge( self::$default_cf7a_options, self::$default_cf7a_options_bootstrap );

add_option( 'cf7a_options', $new_options );

} else {

/* update the plugin options but add the new options automatically */
if ( isset( $options['cf7a_version'] ) ) {
Expand All @@ -226,11 +239,6 @@ public static function update_options( $reset_options = false ) {
cf7a_log( 'CF7-antispam plugin options updated', 1 );

update_option( 'cf7a_options', $new_options );
} else {
/* if the plugin options are missing Init the plugin with the default option + the default settings */
$new_options = array_merge( self::$default_cf7a_options, self::$default_cf7a_options_bootstrap );

add_option( 'cf7a_options', $new_options );
}

cf7a_log( $new_options, 1 );
Expand Down
2 changes: 2 additions & 0 deletions src/admin-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ import './integration/integration.scss';

import './settings/settings.js';
import './settings/settings.scss';

import './settings/importExport.js';
Loading
Loading