| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles the scheduling and sending of malware scan reports. |
| 4 |
* |
| 5 |
* @package WP_Defender\Model\Notification |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Model\Notification; |
| 9 |
|
| 10 |
use DateTime; |
| 11 |
use Exception; |
| 12 |
use WP_Defender\Controller\Scan; |
| 13 |
use WP_Defender\Model\Notification; |
| 14 |
use WP_Defender\Model\Scan as Model_Scan; |
| 15 |
use WP_Defender\Traits\Scan_Email_Template; |
| 16 |
|
| 17 |
/** |
| 18 |
* Handles the scheduling and sending of malware scan reports. |
| 19 |
*/ |
| 20 |
class Malware_Report extends Notification { |
| 21 |
|
| 22 |
use Scan_Email_Template; |
| 23 |
|
| 24 |
/** |
| 25 |
* Table name. |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
protected $table = 'wd_malware_scanning_report'; |
| 30 |
/** |
| 31 |
* Slug identifier for the malware report. |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
public const SLUG = 'malware-report'; |
| 36 |
|
| 37 |
/** |
| 38 |
* Constructor method. |
| 39 |
* Sets default values for the class. |
| 40 |
*/ |
| 41 |
protected function before_load(): void { |
| 42 |
$default = array( |
| 43 |
'slug' => self::SLUG, |
| 44 |
'title' => esc_html__( 'Malware Scanning - Reporting', 'defender-security' ), |
| 45 |
'status' => self::STATUS_DISABLED, |
| 46 |
'description' => esc_html__( |
| 47 |
'Automatically run regular scans of your website and email you reports.', |
| 48 |
'defender-security' |
| 49 |
), |
| 50 |
// @since 3.0.0 Fix 'Guest'-line. |
| 51 |
'in_house_recipients' => is_user_logged_in() ? array( $this->get_default_user() ) : array(), |
| 52 |
'out_house_recipients' => array(), |
| 53 |
'type' => 'report', |
| 54 |
// @since 2.7.0 Add Scheduled Scanning to Malware settings. |
| 55 |
// Hide Schedule and 'dry_run' fields on UI but no delete for backward compatibility with Scan settings, the config structure and the Hub. |
| 56 |
'frequency' => 'weekly', |
| 57 |
'day' => 'sunday', |
| 58 |
'day_n' => 1, |
| 59 |
'time' => '4:00', |
| 60 |
'dry_run' => false, |
| 61 |
'configs' => array( |
| 62 |
'always_send' => false, |
| 63 |
'error_send' => false, |
| 64 |
'template' => $this->get_email_template(), |
| 65 |
), |
| 66 |
); |
| 67 |
$this->import( $default ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Checks if the "Scheduled Scan" is checked and sends the malware scan report if necessary. |
| 72 |
* |
| 73 |
* @return bool Returns true if the scan report should be sent, false otherwise. |
| 74 |
*/ |
| 75 |
public function maybe_send(): bool { |
| 76 |
// @since 2.7.0 First, check "Scheduled Scan" is checked or not. |
| 77 |
if ( ! ( new \WP_Defender\Model\Setting\Scan() )->scheduled_scanning ) { |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
if ( $this->last_sent <= 0 || $this->est_timestamp <= 0 ) { |
| 82 |
$this->last_sent = $this->est_timestamp; |
| 83 |
$this->est_timestamp = $this->get_next_run()->getTimestamp(); |
| 84 |
$this->save(); |
| 85 |
} |
| 86 |
$time = apply_filters( 'defender_current_time_for_report', new DateTime( 'now', wp_timezone() ) ); |
| 87 |
// Testing. |
| 88 |
if ( defined( 'WP_DEFENDER_TESTING' ) && true === constant( 'WP_DEFENDER_TESTING' ) ) { |
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
// Check by 'always_send' will be after scanning is complete. |
| 93 |
return $time->getTimestamp() >= $this->est_timestamp; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Sends the email. |
| 98 |
* Retrieves data based on logic. Steps: |
| 99 |
* 1) check the current active scan, |
| 100 |
* 2) if there isn't active scan then check the latest scan, |
| 101 |
* 3) save the report settings, |
| 102 |
* 4) run new scan. |
| 103 |
* |
| 104 |
* @return void |
| 105 |
* @throws Exception When an error occurs. |
| 106 |
*/ |
| 107 |
public function send() { |
| 108 |
// The current status. |
| 109 |
$active = Model_Scan::get_active(); |
| 110 |
if ( is_object( $active ) ) { |
| 111 |
// Scan is running. |
| 112 |
return; |
| 113 |
} |
| 114 |
// Check the latest scan. |
| 115 |
$model = Model_Scan::get_last(); |
| 116 |
if ( ! is_object( $model ) ) { |
| 117 |
return; |
| 118 |
} |
| 119 |
// Save the report settings. |
| 120 |
$this->last_sent = $this->est_timestamp; |
| 121 |
$this->est_timestamp = $this->get_next_run()->getTimestamp(); |
| 122 |
$this->save(); |
| 123 |
// Run scan. |
| 124 |
$scan = Model_Scan::create( true ); |
| 125 |
if ( is_object( $scan ) && ! is_wp_error( $scan ) ) { |
| 126 |
$scan_controller = wd_di()->get( Scan::class ); |
| 127 |
$scan_controller->do_async_scan( 'report' ); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Define settings labels. |
| 133 |
* |
| 134 |
* @return array |
| 135 |
*/ |
| 136 |
public function labels(): array { |
| 137 |
return array( |
| 138 |
'report' => esc_html__( 'Malware Scanning - Reporting', 'defender-security' ), |
| 139 |
'always_send' => esc_html__( 'Send notifications when no issues are detected', 'defender-security' ), |
| 140 |
'report_subscribers' => esc_html__( 'Recipients', 'defender-security' ), |
| 141 |
'day' => esc_html__( 'Day of', 'defender-security' ), |
| 142 |
'day_n' => esc_html__( 'Day of', 'defender-security' ), |
| 143 |
'time' => esc_html__( 'Time of day', 'defender-security' ), |
| 144 |
'frequency' => esc_html__( 'Frequency', 'defender-security' ), |
| 145 |
); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Returns the next run date for the malware report in the format suitable for the Hub. |
| 150 |
* |
| 151 |
* @return string The formatted next run date for the malware report, or 'Never' if the report is not active. |
| 152 |
*/ |
| 153 |
public function get_next_run_for_hub(): string { |
| 154 |
return self::STATUS_ACTIVE === $this->status |
| 155 |
? $this->persistent_hub_datetime_format( $this->est_timestamp ) |
| 156 |
: esc_html__( 'Never', 'defender-security' ); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Additional converting rules. |
| 161 |
* |
| 162 |
* @param array $configs The configuration data. |
| 163 |
* |
| 164 |
* @return array The type-casted configuration data. |
| 165 |
* @since 3.1.0 |
| 166 |
*/ |
| 167 |
public function type_casting( $configs ): array { |
| 168 |
$configs['always_send'] = (bool) $configs['always_send']; |
| 169 |
$configs['error_send'] = (bool) $configs['error_send']; |
| 170 |
|
| 171 |
return $configs; |
| 172 |
} |
| 173 |
} |
| 174 |
|