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