| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles scan settings. |
| 4 |
* |
| 5 |
* @package WP_Defender\Model\Setting |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Model\Setting; |
| 9 |
|
| 10 |
use DateTime; |
| 11 |
use Exception; |
| 12 |
use Calotes\Model\Setting; |
| 13 |
use function wp_timezone; |
| 14 |
|
| 15 |
/** |
| 16 |
* Model for scan settings. |
| 17 |
*/ |
| 18 |
class Scan extends Setting { |
| 19 |
|
| 20 |
/** |
| 21 |
* Option name. |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
protected $table = 'wd_scan_settings'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Enable core/plugin integrity check while perform a scan. |
| 29 |
* |
| 30 |
* @defender_property |
| 31 |
* @var bool |
| 32 |
*/ |
| 33 |
public $integrity_check = true; |
| 34 |
|
| 35 |
/** |
| 36 |
* Enable Scan WP core files. |
| 37 |
* |
| 38 |
* @defender_property |
| 39 |
* @var bool |
| 40 |
*/ |
| 41 |
public $check_core = true; |
| 42 |
|
| 43 |
/** |
| 44 |
* Enable Scan plugin files. |
| 45 |
* |
| 46 |
* @defender_property |
| 47 |
* @var bool |
| 48 |
*/ |
| 49 |
public $check_plugins = true; |
| 50 |
|
| 51 |
/** |
| 52 |
* Check the files inside wp-content by our malware signatures. |
| 53 |
* |
| 54 |
* @defender_property |
| 55 |
* @var bool |
| 56 |
*/ |
| 57 |
public $scan_malware = true; |
| 58 |
|
| 59 |
/** |
| 60 |
* Check if any plugins or themes have a known vulnerability. |
| 61 |
* |
| 62 |
* @defender_property |
| 63 |
* @var bool |
| 64 |
*/ |
| 65 |
public $check_known_vuln = true; |
| 66 |
|
| 67 |
/** |
| 68 |
* If a file is smaller than this, we wil include it to the test. |
| 69 |
* |
| 70 |
* @defender_property |
| 71 |
* @var int |
| 72 |
*/ |
| 73 |
public $filesize = 10; |
| 74 |
|
| 75 |
/** |
| 76 |
* Is scheduled scanning enabled? |
| 77 |
* |
| 78 |
* @var bool |
| 79 |
* @defender_property |
| 80 |
*/ |
| 81 |
public $scheduled_scanning = false; |
| 82 |
|
| 83 |
/** |
| 84 |
* The frequency of scheduled scan. |
| 85 |
* |
| 86 |
* @var string |
| 87 |
* @defender_property |
| 88 |
* @rule in[daily,weekly,monthly] |
| 89 |
*/ |
| 90 |
public $frequency; |
| 91 |
|
| 92 |
/** |
| 93 |
* The day of scheduled scan. |
| 94 |
* |
| 95 |
* @var string |
| 96 |
* @defender_property |
| 97 |
* @sanitize_text_field |
| 98 |
*/ |
| 99 |
public $day; |
| 100 |
|
| 101 |
/** |
| 102 |
* This is for when user select scheduled scan as monthly, we will have the day number, instead of text. |
| 103 |
* |
| 104 |
* @var int |
| 105 |
* @sanitize_text_field |
| 106 |
* @defender_property |
| 107 |
*/ |
| 108 |
public int $day_n = 1; |
| 109 |
|
| 110 |
/** |
| 111 |
* Same as $day. |
| 112 |
* |
| 113 |
* @var string |
| 114 |
* @defender_property |
| 115 |
* @sanitize_text_field |
| 116 |
*/ |
| 117 |
public $time; |
| 118 |
|
| 119 |
/** |
| 120 |
* Quarantine file deletion/expiration cron schedule. |
| 121 |
* |
| 122 |
* @var string |
| 123 |
* @defender_property |
| 124 |
* @sanitize_text_field |
| 125 |
*/ |
| 126 |
public $quarantine_expire_schedule = 'thirty_days'; |
| 127 |
|
| 128 |
/** |
| 129 |
* Enable Abandoned or outdated plugins. |
| 130 |
* |
| 131 |
* @defender_property |
| 132 |
* @var bool |
| 133 |
*/ |
| 134 |
public $check_abandoned_plugin = true; |
| 135 |
|
| 136 |
/** |
| 137 |
* Define settings labels. |
| 138 |
* |
| 139 |
* @return array |
| 140 |
*/ |
| 141 |
public function labels(): array { |
| 142 |
return array( |
| 143 |
'integrity_check' => esc_html__( 'File change detection', 'defender-security' ), |
| 144 |
'check_core' => esc_html__( 'Scan core files', 'defender-security' ), |
| 145 |
'check_plugins' => esc_html__( 'Scan plugin files', 'defender-security' ), |
| 146 |
'check_abandoned_plugin' => esc_html__( 'Outdated & removed plugins', 'defender-security' ), |
| 147 |
'check_known_vuln' => esc_html__( 'Known vulnerabilities', 'defender-security' ), |
| 148 |
'scan_malware' => esc_html__( 'Suspicious code', 'defender-security' ), |
| 149 |
'filesize' => esc_html__( 'Max included file size', 'defender-security' ), |
| 150 |
'scheduled_scanning' => esc_html__( 'Scheduled Scanning', 'defender-security' ), |
| 151 |
'frequency' => esc_html__( 'Frequency', 'defender-security' ), |
| 152 |
'day' => esc_html__( 'Day of the week', 'defender-security' ), |
| 153 |
'day_n' => esc_html__( 'Day of the month', 'defender-security' ), |
| 154 |
'time' => esc_html__( 'Time of day', 'defender-security' ), |
| 155 |
); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Check different cases for 'File change detection' option. |
| 160 |
* |
| 161 |
* @return bool |
| 162 |
*/ |
| 163 |
public function is_checked_any_file_change_types(): bool { |
| 164 |
if ( ! $this->integrity_check ) { |
| 165 |
// Check the parent type. |
| 166 |
return false; |
| 167 |
} elseif ( $this->integrity_check && ! $this->check_core && ! $this->check_plugins ) { |
| 168 |
// Check the parent and child types. |
| 169 |
return false; |
| 170 |
} |
| 171 |
|
| 172 |
return true; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Validates the input after form submission and adds error messages if necessary. |
| 177 |
* |
| 178 |
* @return void |
| 179 |
*/ |
| 180 |
protected function after_validate(): void { |
| 181 |
// Case#1: all child types of File change detection are unchecked BUT parent type is checked. |
| 182 |
if ( $this->integrity_check && ! $this->check_core && ! $this->check_plugins ) { |
| 183 |
$this->errors[] = sprintf( |
| 184 |
/* translators: %s: File change detection. */ |
| 185 |
esc_html__( 'You have not selected a scan type for the %s. Please choose at least one and save the settings again.', 'defender-security' ), |
| 186 |
'<strong>' . esc_html__( 'File change detection', 'defender-security' ) . '</strong>' |
| 187 |
); |
| 188 |
// Case#2: all scan types are unchecked and Scheduled Scanning is checked. |
| 189 |
} elseif ( ! $this->is_enabled_any_scan_type() && $this->scheduled_scanning ) { |
| 190 |
$this->errors[] = esc_html__( |
| 191 |
'You have not selected a scan type. Please enable at least one scan type and save the settings again.', |
| 192 |
'defender-security' |
| 193 |
); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Initializes the object by setting default values for the frequency, day, day_n, and time properties based on the |
| 199 |
* current day and time. |
| 200 |
* |
| 201 |
* @return void |
| 202 |
* @throws Exception Emits Exception in case of an error. |
| 203 |
* @since 4.7.1 Implement Dynamic Scan Scheduling to avoid event spikes on MP. |
| 204 |
*/ |
| 205 |
protected function before_load(): void { |
| 206 |
// Get current day and time. |
| 207 |
$date = new DateTime( 'now', wp_timezone() ); |
| 208 |
$result = explode( '--', $date->format( 'l--H--i' ) ); |
| 209 |
$day = strtolower( $result[0] ); |
| 210 |
$current_hours = (int) $result[1]; |
| 211 |
$current_mins = (int) $result[2]; |
| 212 |
// We have a 30 minute span, so XX:00-15 => XX:00, XX:16-45 => XX:30, XX:46-59 => (XX+1):00. |
| 213 |
if ( $current_mins > 15 && $current_mins <= 45 ) { |
| 214 |
$mins = '30'; |
| 215 |
} elseif ( $current_mins >= 0 && $current_mins < 16 ) { |
| 216 |
$mins = '00'; |
| 217 |
} else { |
| 218 |
++$current_hours; |
| 219 |
$mins = '00'; |
| 220 |
} |
| 221 |
|
| 222 |
$this->frequency = 'weekly'; |
| 223 |
$this->day = $day; |
| 224 |
$this->day_n = 1; |
| 225 |
$this->time = $current_hours . ':' . $mins; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Is enabled any scan type at least? |
| 230 |
* |
| 231 |
* @return bool |
| 232 |
*/ |
| 233 |
private function is_enabled_any_scan_type(): bool { |
| 234 |
return $this->integrity_check |
| 235 |
|| $this->check_known_vuln |
| 236 |
|| $this->scan_malware |
| 237 |
|| $this->check_abandoned_plugin; |
| 238 |
} |
| 239 |
} |
| 240 |
|