| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Model\Notification; |
| 4 |
|
| 5 |
use WP_Defender\Model\Scan; |
| 6 |
use WP_Defender\Model\Scan_Item; |
| 7 |
use WP_Defender\Traits\IO; |
| 8 |
use WP_Defender\Model\Notification\Malware_Report; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class Firewall_Notification |
| 12 |
* @package WP_Defender\Model\Notification |
| 13 |
*/ |
| 14 |
class Malware_Notification extends \WP_Defender\Model\Notification { |
| 15 |
use IO; |
| 16 |
|
| 17 |
/** |
| 18 |
* Option name. |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
protected $table = 'wd_malware_scanning_notification'; |
| 22 |
|
| 23 |
/** |
| 24 |
* @return array |
| 25 |
*/ |
| 26 |
public function get_default_values() { |
| 27 |
|
| 28 |
return array( |
| 29 |
'subject_issue_found' => __( 'Scan of {SITE_URL} complete. {ISSUES_COUNT} issues found.', 'wpdef' ), |
| 30 |
'subject_issue_not_found' => __( 'Scan of {SITE_URL} complete. {ISSUES_COUNT} issues found.', 'wpdef' ), |
| 31 |
'subject_error' => __( 'Couldn’t scan {SITE_URL} for vulnerabilities.', 'wpdef' ), |
| 32 |
'content_issue_found' => __( 'Hi {USER_NAME}, |
| 33 |
|
| 34 |
{ISSUES_COUNT} vulnerabilities were identified for {SITE_URL} during a Malware Scan. See details for each issue below. |
| 35 |
|
| 36 |
{ISSUES_LIST}', 'wpdef' ), |
| 37 |
'content_issue_not_found' => __( 'Hi {USER_NAME}, |
| 38 |
|
| 39 |
No vulnerabilities have been found for {SITE_URL}.', 'wpdef' ), |
| 40 |
'content_error' => __( 'Hi {USER_NAME}, |
| 41 |
|
| 42 |
We couldn\'t scan {SITE_URL} for vulnerabilities. Please visit your site and run a manual scan.', 'wpdef' ), |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
public function before_load() { |
| 47 |
$default_values = $this->get_default_values(); |
| 48 |
$default = array( |
| 49 |
'slug' => 'malware-notification', |
| 50 |
'title' => __( 'Malware Scanning - Notification', 'wpdef' ), |
| 51 |
'status' => self::STATUS_DISABLED, |
| 52 |
'description' => __( 'Get email notifications when Defender has finished manual malware scans.', 'wpdef' ), |
| 53 |
// @since 3.0.0 Fix 'Guest'-line. |
| 54 |
'in_house_recipients' => is_user_logged_in() ? array( $this->get_default_user() ) : array(), |
| 55 |
'out_house_recipients' => array(), |
| 56 |
'type' => 'notification', |
| 57 |
'dry_run' => false, |
| 58 |
'configs' => array( |
| 59 |
'always_send' => false, |
| 60 |
'error_send' => false, |
| 61 |
'template' => array( |
| 62 |
'found' => array( |
| 63 |
'subject' => $default_values['subject_issue_found'], |
| 64 |
'body' => $default_values['content_issue_found'], |
| 65 |
), |
| 66 |
'not_found' => array( |
| 67 |
'subject' => $default_values['subject_issue_not_found'], |
| 68 |
'body' => $default_values['content_issue_not_found'], |
| 69 |
), |
| 70 |
'error' => array( |
| 71 |
'subject' => $default_values['subject_error'], |
| 72 |
'body' => $default_values['content_error'], |
| 73 |
), |
| 74 |
), |
| 75 |
), |
| 76 |
); |
| 77 |
$this->import( $default ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* @param Scan $scan |
| 82 |
* @param object $object |
| 83 |
* @param string $subject |
| 84 |
* @param string $content |
| 85 |
* @param array $issues |
| 86 |
*/ |
| 87 |
protected function email_engine( Scan $scan, $object, $subject, $content, $issues ) { |
| 88 |
foreach ( $object->in_house_recipients as $user ) { |
| 89 |
if ( self::USER_SUBSCRIBED !== $user['status'] ) { |
| 90 |
continue; |
| 91 |
} |
| 92 |
$this->send_to_user( $user['email'], $user['name'], $subject, $content, $issues ); |
| 93 |
} |
| 94 |
foreach ( $object->out_house_recipients as $user ) { |
| 95 |
if ( self::USER_SUBSCRIBED !== $user['status'] ) { |
| 96 |
continue; |
| 97 |
} |
| 98 |
$this->send_to_user( $user['email'], $user['name'], $subject, $content, $issues ); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* @param Scan $scan |
| 104 |
* |
| 105 |
* @return void |
| 106 |
*/ |
| 107 |
public function send( Scan $scan ) { |
| 108 |
$object = $this; |
| 109 |
if ( $scan->is_automation ) { |
| 110 |
$object = wd_di()->get( Malware_Report::class ); |
| 111 |
// If Malware Reporting is unchecked, then return. |
| 112 |
if ( self::STATUS_ACTIVE !== $object->status ) { |
| 113 |
return; |
| 114 |
} |
| 115 |
// @since 2.7.0 Remove 'dry_run'-condition. |
| 116 |
} |
| 117 |
$issues = $scan->get_issues( null, Scan_Item::STATUS_ACTIVE ); |
| 118 |
/** |
| 119 |
* When the Defender is unable to scan files for some reason and the 'error_send' option is checked. |
| 120 |
* Possible reasons: |
| 121 |
* - no connection to Scan API, |
| 122 |
* - no checksums received from wp.org, |
| 123 |
* - scan process stuck. |
| 124 |
*/ |
| 125 |
if ( |
| 126 |
isset( $object->configs['error_send'] ) && ! empty( $object->configs['error_send'] |
| 127 |
&& in_array( $scan->status, array( Scan::STATUS_ERROR, Scan::STATUS_IDLE ), true ) ) |
| 128 |
) { |
| 129 |
$subject = $object->configs['template']['error']['subject']; |
| 130 |
$content = nl2br( $object->configs['template']['error']['body'] ); |
| 131 |
|
| 132 |
return $this->email_engine( $scan, $object, $subject, $content, $issues ); |
| 133 |
} |
| 134 |
|
| 135 |
// Sometimes value may be as empty string for disabled 'always_send'. |
| 136 |
if ( empty( $object->configs['always_send'] ) && 0 === count( $issues ) ) { |
| 137 |
|
| 138 |
return; |
| 139 |
} |
| 140 |
// For another case: |
| 141 |
$templates = count( $issues ) ? $object->configs['template']['found'] : $object->configs['template']['not_found']; |
| 142 |
$subject = $templates['subject']; |
| 143 |
$content = nl2br( $templates['body'] ); |
| 144 |
|
| 145 |
return $this->email_engine( $scan, $object, $subject, $content, $issues ); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* @param $email |
| 150 |
* @param $name |
| 151 |
* @param $subject |
| 152 |
* @param $content |
| 153 |
* @param $issues |
| 154 |
* |
| 155 |
* @throws \DI\DependencyException |
| 156 |
* @throws \DI\NotFoundException |
| 157 |
*/ |
| 158 |
private function send_to_user( $email, $name, $subject, $content, $issues ) { |
| 159 |
$controller_scan = wd_di()->get( \WP_Defender\Controller\Scan::class ); |
| 160 |
$replacers = array( |
| 161 |
'issues_count' => is_array($issues) || $issues instanceof \Countable ? count( $issues ) : 0, |
| 162 |
'issues_list' => $controller_scan->render_partial( |
| 163 |
'email/scan-issue-list', |
| 164 |
array( |
| 165 |
'issues' => $issues, |
| 166 |
'email' => $email, |
| 167 |
), |
| 168 |
false |
| 169 |
), |
| 170 |
'site_url' => network_site_url(), |
| 171 |
'user_name' => $name, |
| 172 |
); |
| 173 |
// Replace data. |
| 174 |
$replacers = apply_filters( 'wd_notification_email_params', $replacers ); |
| 175 |
$filter_name = (is_array($issues) || $issues instanceof \Countable ? count( $issues ) : 0) > 0 |
| 176 |
? 'wd_notification_email_subject_issue' |
| 177 |
: 'wd_notification_email_subject'; |
| 178 |
$subject = apply_filters( $filter_name, $subject ); |
| 179 |
$content = apply_filters( 'wd_notification_email_content_before', $content ); |
| 180 |
foreach ( $replacers as $key => $replacer ) { |
| 181 |
$content = str_replace( "{{$key}}", $replacer, $content ); |
| 182 |
$content = str_replace( '{' . strtoupper( $key ) . '}', $replacer, $content ); |
| 183 |
$subject = str_replace( "{{$key}}", $replacer, $subject ); |
| 184 |
$subject = str_replace( '{' . strtoupper( $key ) . '}', $replacer, $subject ); |
| 185 |
} |
| 186 |
$content = apply_filters( 'wd_notification_email_content_after', $content ); |
| 187 |
$template = $controller_scan->render_partial( |
| 188 |
'email/index', |
| 189 |
array( |
| 190 |
'title' => __( 'Malware Scanning', 'wpdef' ), |
| 191 |
'content_body' => $content, |
| 192 |
), |
| 193 |
false |
| 194 |
); |
| 195 |
|
| 196 |
$headers = defender_noreply_html_header( |
| 197 |
defender_noreply_email( 'wd_scan_noreply_email' ) |
| 198 |
); |
| 199 |
|
| 200 |
$ret = wp_mail( $email, $subject, $template, $headers ); |
| 201 |
if ( $ret ) { |
| 202 |
$this->save_log( $email ); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Define labels for settings key. |
| 208 |
* |
| 209 |
* @param string|null $key |
| 210 |
* |
| 211 |
* @return string|array|null |
| 212 |
*/ |
| 213 |
public function labels( $key = null ) { |
| 214 |
$labels = array( |
| 215 |
'notification' => __( 'Malware Scanning - Notification', 'wpdef' ), |
| 216 |
'always_send_notification' => __( 'Send notifications when no issues are detected', 'wpdef' ), |
| 217 |
'error_send' => __( "Send notifications when Defender couldn't scan the files", 'wpdef' ), |
| 218 |
'email_subject_issue_found' => __( 'Email subject when an issue is found', 'wpdef' ), |
| 219 |
'email_subject_issue_not_found' => __( 'Email subject when no issues are found', 'wpdef' ), |
| 220 |
'email_subject_error' => __( 'Email subject when failed to scan', 'wpdef' ), |
| 221 |
'email_content_issue_found' => __( 'Email body when an issue is found', 'wpdef' ), |
| 222 |
'email_content_issue_not_found' => __( 'Email body when no issues are found', 'wpdef' ), |
| 223 |
'email_content_error' => __( 'When failed to scan', 'wpdef' ), |
| 224 |
'notification_subscribers' => __( 'Recipients', 'wpdef' ), |
| 225 |
); |
| 226 |
|
| 227 |
if ( ! is_null( $key ) ) { |
| 228 |
return $labels[ $key ] ?? null; |
| 229 |
} |
| 230 |
|
| 231 |
return $labels; |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Try to send email if: |
| 236 |
* Malware Scanning - Notification is enabled, |
| 237 |
* Malware Scanning - Reporting is enabled or Scan model has is_automation = true. |
| 238 |
* |
| 239 |
* @return bool |
| 240 |
*/ |
| 241 |
public function check_options() { |
| 242 |
|
| 243 |
return self::STATUS_ACTIVE === $this->status |
| 244 |
|| self::STATUS_ACTIVE === wd_di()->get( Malware_Report::class )->status; |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Additional converting rules. |
| 249 |
* |
| 250 |
* @param array $configs |
| 251 |
* |
| 252 |
* @return array |
| 253 |
* @since 3.1.0 |
| 254 |
*/ |
| 255 |
public function type_casting( $configs ) { |
| 256 |
$configs['always_send'] = (bool) $configs['always_send']; |
| 257 |
$configs['error_send'] = (bool) $configs['error_send']; |
| 258 |
|
| 259 |
return $configs; |
| 260 |
} |
| 261 |
} |
| 262 |
|