| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Model\Notification; |
| 4 |
|
| 5 |
use WP_Defender\Controller\Firewall; |
| 6 |
use WP_Defender\Model\Lockout_Log; |
| 7 |
|
| 8 |
/** |
| 9 |
* Class Firewall_Report. |
| 10 |
* @package WP_Defender\Model\Notification |
| 11 |
*/ |
| 12 |
class Firewall_Report extends \WP_Defender\Model\Notification { |
| 13 |
protected $table = 'wd_lockout_report'; |
| 14 |
|
| 15 |
public function before_load() { |
| 16 |
$default = array( |
| 17 |
'slug' => 'firewall-report', |
| 18 |
'title' => __( 'Firewall - Reporting', 'wpdef' ), |
| 19 |
'status' => self::STATUS_DISABLED, |
| 20 |
'description' => __( 'Configure Defender to automatically email you a lockout report for this website.', 'wpdef' ), |
| 21 |
// @since 3.0.0 Fix 'Guest'-line. |
| 22 |
'in_house_recipients' => is_user_logged_in() ? array( $this->get_default_user() ) : array(), |
| 23 |
'out_house_recipients' => array(), |
| 24 |
'type' => 'report', |
| 25 |
'dry_run' => false, |
| 26 |
'frequency' => 'weekly', |
| 27 |
'day' => 'sunday', |
| 28 |
'day_n' => '1', |
| 29 |
'time' => '4:00', |
| 30 |
'configs' => array(), |
| 31 |
); |
| 32 |
$this->import( $default ); |
| 33 |
} |
| 34 |
|
| 35 |
public function send() { |
| 36 |
foreach ( $this->in_house_recipients as $recipient ) { |
| 37 |
if ( self::USER_SUBSCRIBED !== $recipient['status'] ) { |
| 38 |
continue; |
| 39 |
} |
| 40 |
$this->send_to_user( $recipient['name'], $recipient['email'] ); |
| 41 |
} |
| 42 |
foreach ( $this->out_house_recipients as $recipient ) { |
| 43 |
if ( self::USER_SUBSCRIBED !== $recipient['status'] ) { |
| 44 |
continue; |
| 45 |
} |
| 46 |
$this->send_to_user( $recipient['name'], $recipient['email'] ); |
| 47 |
} |
| 48 |
$this->last_sent = $this->est_timestamp; |
| 49 |
$this->est_timestamp = $this->get_next_run()->getTimestamp(); |
| 50 |
$this->save(); |
| 51 |
} |
| 52 |
|
| 53 |
private function send_to_user( $name, $email ) { |
| 54 |
$site_url = network_site_url(); |
| 55 |
/* translators: */ |
| 56 |
$subject = sprintf( __( 'Defender Lockouts Report for %s', 'wpdef' ), $site_url ); |
| 57 |
if ( 'daily' === $this->frequency ) { |
| 58 |
$time_unit = __( 'in the past 24 hours', 'wpdef' ); |
| 59 |
$interval = '-24 hours'; |
| 60 |
} elseif ( 'weekly' === $this->frequency ) { |
| 61 |
$time_unit = __( 'in the past week', 'wpdef' ); |
| 62 |
$interval = '-7 days'; |
| 63 |
} else { |
| 64 |
$time_unit = __( 'in the month', 'wpdef' ); |
| 65 |
$interval = '-30 days'; |
| 66 |
} |
| 67 |
// Number of lockouts. |
| 68 |
$count_lockouts = array( |
| 69 |
'404' => Lockout_Log::count( |
| 70 |
strtotime( $interval ), |
| 71 |
time(), |
| 72 |
array( |
| 73 |
Lockout_Log::LOCKOUT_404, |
| 74 |
) |
| 75 |
), |
| 76 |
'login' => Lockout_Log::count( |
| 77 |
strtotime( $interval ), |
| 78 |
time(), |
| 79 |
array( |
| 80 |
Lockout_Log::AUTH_LOCK, |
| 81 |
) |
| 82 |
), |
| 83 |
// Todo: add UA-lockouts when UA-checkbox appears in the Firewall email configure. |
| 84 |
// Todo: change COUNT-logic for all lockouts when UA-checkbox will be ready, e.g. Lockout_Log::count_lockout_in_24_hours(). |
| 85 |
); |
| 86 |
|
| 87 |
$firewall = wd_di()->get( Firewall::class ); |
| 88 |
$logs_url = network_admin_url( 'admin.php?page=wdf-ip-lockout&view=logs' ); |
| 89 |
// Need for activated Mask Login feature. |
| 90 |
$logs_url = apply_filters( 'report_email_logs_link', $logs_url, $email ); |
| 91 |
$content_body = $firewall->render_partial( |
| 92 |
'email/firewall-report', |
| 93 |
array( |
| 94 |
'name' => $name, |
| 95 |
'count_total' => (int) $count_lockouts['404'] + (int) $count_lockouts['login'], |
| 96 |
'time_unit' => $time_unit, |
| 97 |
'logs_url' => $logs_url, |
| 98 |
'site_url' => $site_url, |
| 99 |
'count_lockouts' => $count_lockouts, |
| 100 |
), |
| 101 |
false |
| 102 |
); |
| 103 |
$content = $firewall->render_partial( |
| 104 |
'email/index', |
| 105 |
array( |
| 106 |
'title' => __( 'Firewall', 'wpdef' ), |
| 107 |
'content_body' => $content_body, |
| 108 |
), |
| 109 |
false |
| 110 |
); |
| 111 |
|
| 112 |
$headers = defender_noreply_html_header( |
| 113 |
defender_noreply_email( 'wd_lockout_noreply_email' ) |
| 114 |
); |
| 115 |
|
| 116 |
$ret = wp_mail( $email, $subject, $content, $headers ); |
| 117 |
if ( $ret ) { |
| 118 |
$this->save_log( $email ); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Define labels for settings key. |
| 124 |
* |
| 125 |
* @param string|null $key |
| 126 |
* |
| 127 |
* @return string|array|null |
| 128 |
*/ |
| 129 |
public function labels( $key = null ) { |
| 130 |
$labels = array( |
| 131 |
'report' => __( 'Firewall - Reporting', 'wpdef' ), |
| 132 |
'day' => __( 'Day of', 'wpdef' ), |
| 133 |
'day_n' => __( 'Day of', 'wpdef' ), |
| 134 |
'report_time' => __( 'Time of day', 'wpdef' ), |
| 135 |
'report_frequency' => __( 'Frequency', 'wpdef' ), |
| 136 |
'report_subscribers' => __( 'Recipients', 'wpdef' ), |
| 137 |
); |
| 138 |
|
| 139 |
if ( ! is_null( $key ) ) { |
| 140 |
return $labels[ $key ] ?? null; |
| 141 |
} |
| 142 |
|
| 143 |
return $labels; |
| 144 |
} |
| 145 |
} |
| 146 |
|