| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Model\Notification; |
| 4 |
|
| 5 |
use WP_Defender\Model\Scan as Model_Scan; |
| 6 |
|
| 7 |
class Malware_Report extends \WP_Defender\Model\Notification { |
| 8 |
protected $table = 'wd_malware_scanning_report'; |
| 9 |
|
| 10 |
public function before_load() { |
| 11 |
$default = array( |
| 12 |
'slug' => 'malware-report', |
| 13 |
'title' => __( 'Malware Scanning - Reporting', 'wpdef' ), |
| 14 |
'status' => self::STATUS_DISABLED, |
| 15 |
'description' => __( 'Automatically run regular scans of your website and email you reports.', 'wpdef' ), |
| 16 |
// @since 3.0.0 Fix 'Guest'-line. |
| 17 |
'in_house_recipients' => is_user_logged_in() ? array( $this->get_default_user() ) : array(), |
| 18 |
'out_house_recipients' => array(), |
| 19 |
'type' => 'report', |
| 20 |
// @since 2.7.0 Add Scheduled Scanning to Malware settings. |
| 21 |
// Hide Schedule and 'dry_run' fields on UI but no delete for backward compatibility with Scan settings, the config structure and the Hub. |
| 22 |
'frequency' => 'weekly', |
| 23 |
'day' => 'sunday', |
| 24 |
'day_n' => '1', |
| 25 |
'time' => '4:00', |
| 26 |
'dry_run' => false, |
| 27 |
'configs' => array( |
| 28 |
'always_send' => false, |
| 29 |
'error_send' => false, |
| 30 |
'template' => array( |
| 31 |
'found' => array( |
| 32 |
'subject' => __( 'Scan of {SITE_URL} complete. {ISSUES_COUNT} issues found.', 'wpdef' ), |
| 33 |
'body' => __( 'Hi {USER_NAME}, |
| 34 |
|
| 35 |
WP Defender here, reporting back from the front. |
| 36 |
|
| 37 |
I\'ve finished scanning {SITE_URL} for vulnerabilities and I found {ISSUES_COUNT} issues that you should take a closer look at! |
| 38 |
{ISSUES_LIST} |
| 39 |
|
| 40 |
Stay Safe, |
| 41 |
WP Defender |
| 42 |
WPMU DEV Superhero', 'wpdef' ), |
| 43 |
), |
| 44 |
'not_found' => array( |
| 45 |
'subject' => __( 'Scan of {SITE_URL} complete. {ISSUES_COUNT} issues found.', 'wpdef' ), |
| 46 |
'body' => __( 'Hi {USER_NAME}, |
| 47 |
|
| 48 |
WP Defender here, reporting back from the front. |
| 49 |
|
| 50 |
I\'ve finished scanning {SITE_URL} for vulnerabilities and I found nothing. Well done for running such a tight ship! |
| 51 |
|
| 52 |
Keep up the good work! With regular security scans and a well-hardened installation you\'ll be just fine. |
| 53 |
|
| 54 |
Stay safe, |
| 55 |
WP Defender |
| 56 |
WPMU DEV Superhero', 'wpdef' ), |
| 57 |
), |
| 58 |
'error' => array( |
| 59 |
'subject' => __( 'Couldn’t scan {SITE_URL} for vulnerabilities. ', 'wpdef' ), |
| 60 |
'body' => __( |
| 61 |
'Hi {USER_NAME}, |
| 62 |
|
| 63 |
WP Defender here, reporting back from the front. |
| 64 |
|
| 65 |
I couldn’t scan {SITE_URL} for vulnerabilities. Please visit your site and run a manual scan. |
| 66 |
|
| 67 |
Stay Safe, |
| 68 |
WP Defender |
| 69 |
WPMU DEV Superhero ', 'wpdef' ), |
| 70 |
), |
| 71 |
), |
| 72 |
), |
| 73 |
); |
| 74 |
$this->import( $default ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* @return bool |
| 79 |
* @throws \Exception |
| 80 |
*/ |
| 81 |
public function maybe_send() { |
| 82 |
// @since 2.7.0 First, check "Scheduled Scan" is checked or not. |
| 83 |
if ( ! ( new \WP_Defender\Model\Setting\Scan() )->scheduled_scanning ) { |
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
if ( empty( $this->last_sent ) || empty( $this->est_timestamp ) ) { |
| 88 |
$this->last_sent = $this->est_timestamp; |
| 89 |
$this->est_timestamp = $this->get_next_run()->getTimestamp(); |
| 90 |
$this->save(); |
| 91 |
} |
| 92 |
$time = apply_filters( 'defender_current_time_for_report', new \DateTime( 'now', wp_timezone() ) ); |
| 93 |
// Testing. |
| 94 |
if ( defined( 'WP_DEFENDER_TESTING' ) && true === constant( 'WP_DEFENDER_TESTING' ) ) { |
| 95 |
return true; |
| 96 |
} |
| 97 |
// Check by 'always_send' will be after scanning is complete. |
| 98 |
return $time->getTimestamp() >= $this->est_timestamp; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Steps: |
| 103 |
* 1) check the current active scan, |
| 104 |
* 2) if there isn't active scan then check the latest scan, |
| 105 |
* 3) save the report settings, |
| 106 |
* 4) run new scan. |
| 107 |
*/ |
| 108 |
public function send() { |
| 109 |
// The current status. |
| 110 |
$active = Model_Scan::get_active(); |
| 111 |
if ( is_object( $active ) ) { |
| 112 |
// Scan is running. |
| 113 |
return; |
| 114 |
} |
| 115 |
// Check the latest scan. |
| 116 |
$model = Model_Scan::get_last(); |
| 117 |
if ( ! is_object( $model ) ) { |
| 118 |
return; |
| 119 |
} |
| 120 |
// Save the report settings. |
| 121 |
$this->last_sent = $this->est_timestamp; |
| 122 |
$this->est_timestamp = $this->get_next_run()->getTimestamp(); |
| 123 |
$this->save(); |
| 124 |
// Run scan. |
| 125 |
$scan = Model_Scan::create( true ); |
| 126 |
if ( is_object( $scan ) && ! is_wp_error( $scan ) ) { |
| 127 |
wd_di()->get( \WP_Defender\Controller\Scan::class )->do_async_scan( 'report' ); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Define labels for settings key. |
| 133 |
* |
| 134 |
* @param string|null $key |
| 135 |
* |
| 136 |
* @return string|array|null |
| 137 |
*/ |
| 138 |
public function labels( $key = null ) { |
| 139 |
$labels = array( |
| 140 |
'report' => __( 'Malware Scanning - Reporting', 'wpdef' ), |
| 141 |
'always_send' => __( 'Send notifications when no issues are detected', 'wpdef' ), |
| 142 |
'report_subscribers' => __( 'Recipients', 'wpdef' ), |
| 143 |
'day' => __( 'Day of', 'wpdef' ), |
| 144 |
'day_n' => __( 'Day of', 'wpdef' ), |
| 145 |
'time' => __( 'Time of day', 'wpdef' ), |
| 146 |
'frequency' => __( 'Frequency', 'wpdef' ), |
| 147 |
); |
| 148 |
|
| 149 |
if ( ! is_null( $key ) ) { |
| 150 |
return $labels[ $key ] ?? null; |
| 151 |
} |
| 152 |
|
| 153 |
return $labels; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* @return string |
| 158 |
*/ |
| 159 |
public function get_next_run_for_hub() { |
| 160 |
// Todo: check it because Scheduled Scanning moves to Malware settings on v2.7.0. |
| 161 |
return self::STATUS_ACTIVE === $this->status |
| 162 |
? $this->persistent_hub_datetime_format( $this->est_timestamp ) |
| 163 |
: __( 'Never', 'wpdef' ); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Additional converting rules. |
| 168 |
* |
| 169 |
* @param array $configs |
| 170 |
* |
| 171 |
* @return array |
| 172 |
* @since 3.1.0 |
| 173 |
*/ |
| 174 |
public function type_casting( $configs ) { |
| 175 |
$configs['always_send'] = (bool) $configs['always_send']; |
| 176 |
$configs['error_send'] = (bool) $configs['error_send']; |
| 177 |
|
| 178 |
return $configs; |
| 179 |
} |
| 180 |
} |
| 181 |
|