| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Component; |
| 4 |
|
| 5 |
use WP_Defender\Component; |
| 6 |
use WP_Defender\Model\Lockout_Ip; |
| 7 |
|
| 8 |
class Firewall extends Component { |
| 9 |
/** |
| 10 |
* Queue hooks when this class init |
| 11 |
*/ |
| 12 |
public function add_hooks() { |
| 13 |
add_filter( 'defender_ip_lockout_assets', array( &$this, 'output_scripts_data' ) ); |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* @param array $data |
| 18 |
* |
| 19 |
* @return array |
| 20 |
*/ |
| 21 |
public function output_scripts_data( $data ) { |
| 22 |
$model = new \WP_Defender\Model\Setting\Firewall(); |
| 23 |
$data['settings'] = array( |
| 24 |
'storage_days' => $model->storage_days ?? 30, |
| 25 |
'class' => \WP_Defender\Model\Setting\Firewall::class, |
| 26 |
); |
| 27 |
|
| 28 |
return $data; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Cron for delete old log |
| 33 |
*/ |
| 34 |
public function firewall_clean_up_logs() { |
| 35 |
$settings = new \WP_Defender\Model\Setting\Firewall(); |
| 36 |
/** |
| 37 |
* Filter Count days for IP logs to be saved to DB |
| 38 |
* |
| 39 |
* @since 2.3 |
| 40 |
* |
| 41 |
* @param string |
| 42 |
*/ |
| 43 |
$storage_days = apply_filters( 'ip_lockout_logs_store_backward', $settings->storage_days ); |
| 44 |
|
| 45 |
if ( ! is_numeric( $storage_days ) ) { |
| 46 |
return; |
| 47 |
} |
| 48 |
$time_string = '-' . $storage_days . ' days'; |
| 49 |
$timestamp = $this->local_to_utc( $time_string ); |
| 50 |
\WP_Defender\Model\Lockout_Log::remove_logs( $timestamp, 50 ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Cron for clean up temporary IP block list |
| 55 |
*/ |
| 56 |
public function firewall_clean_up_temporary_ip_blocklist() { |
| 57 |
$models = Lockout_Ip::get_bulk( Lockout_Ip::STATUS_BLOCKED ); |
| 58 |
foreach( $models as $model ) { |
| 59 |
$model->status = Lockout_Ip::STATUS_NORMAL; |
| 60 |
$model->save(); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Update the firewall temporary IP blocklist clear cron job |
| 66 |
* Once the interval settings value is updated |
| 67 |
* |
| 68 |
* @param string $new_interval |
| 69 |
*/ |
| 70 |
public function update_cron_schedule_interval( $new_interval ) { |
| 71 |
$settings = new \WP_Defender\Model\Setting\Firewall(); |
| 72 |
// if new interval is different than the saved value then we need to clear the cron job |
| 73 |
if ( $new_interval !== $settings->ip_blocklist_cleanup_interval ) { |
| 74 |
update_site_option( 'wpdef_clear_schedule_firewall_cleanup_temp_blocklist_ips', true ); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
} |
| 79 |
|