get_option( 'patchstack_license_free', 0 ) == 1 ) { return; } $rules = $this->plugin->htaccess->get_firewall_rule_settings(); $settings = json_encode( $rules ); $results = $this->plugin->api->post_firewall_rule( [ 'settings' => $settings ] ); // If no rules returned, we assume all settings are turned off. if ( empty( $results ) ) { $results['rules'] = ''; } // We have rules so apply it to the .htaccess file. if ( isset( $results['rules'] ) ) { $this->plugin->htaccess->write_to_htaccess( $results['rules'] ); return; } } /** * Pull the firewall .htaccess rules from the API. * Then apply it to the .htaccess file after we create a backup. * * @return void */ public function post_firewall_htaccess_rules() { if ( $this->get_option( 'patchstack_license_free', 0 ) == 1 ) { return; } $results = $this->plugin->api->post_firewall_htaccess_rule(); $rules = ! isset( $results['rules'] ) || empty( $results ) ? '' : $results['rules']; // Check if we have to update anything at all. $hash = sha1( $rules ); if ( get_option( 'patchstack_firewall_htaccess_hash', '' ) == $hash || ( get_option( 'patchstack_firewall_htaccess_hash', '' ) == '' && $rules == '' ) ) { return; } // We have rules so apply it to the .htaccess file. update_option( 'patchstack_firewall_htaccess_hash', $hash ); } /** * Pull the firewall/whitelist rules from the API. * * @return void */ public function dynamic_firewall_rules() { if ( $this->get_option( 'patchstack_license_free', 0 ) == 1 ) { return; } // Get the firewall and whitelist rules. $results = $this->plugin->api->post_firewall_rule_json(); if ( ! isset( $results['firewall'] ) ) { return; } // Separate the new firewall engine rules from the old ones. $rules = $results['firewall']; $newRules = []; $oldRules = []; foreach ( $rules as $rule ) { if ( isset( $rule['rule_v2'] ) ) { $rule['rules'] = $rule['rule_v2']; unset($rule['rule_v2']); $newRules[] = $rule; } else { $oldRules[] = $rule; } } // Update firewall rules. update_option( 'patchstack_firewall_rules', json_encode( $oldRules ) ); update_option( 'patchstack_firewall_rules_v3', json_encode( $newRules ) ); // Separate the new firewall engine rules from the old ones. $rules = $results['whitelists']; $newRules = []; $oldRules = []; foreach ( $rules as $rule ) { if ( isset( $rule['rule_v2'] ) ) { $rule['rules'] = $rule['rule_v2']; unset($rule['rule_v2']); $newRules[] = $rule; } else { $oldRules[] = $rule; } } // Update whitelist rules. update_option( 'patchstack_whitelist_rules', json_encode( $oldRules ) ); update_option( 'patchstack_whitelist_rules_v3', json_encode( $newRules ) ); // Update the whitelisted keys. update_option( 'patchstack_whitelist_keys_rules', json_encode( $results['whitelist_keys'] ) ); } }