| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles the scheduling and sending of firewall-related reports. |
| 4 |
* |
| 5 |
* @package WP_Defender\Model\Notification |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Model\Notification; |
| 9 |
|
| 10 |
use WP_Defender\Component\Mail; |
| 11 |
use WP_Defender\Model\Lockout_Log; |
| 12 |
use WP_Defender\Controller\Firewall; |
| 13 |
use WP_Defender\Component\Notification; |
| 14 |
|
| 15 |
/** |
| 16 |
* Handles the scheduling and sending of firewall-related reports. |
| 17 |
*/ |
| 18 |
class Firewall_Report extends \WP_Defender\Model\Notification { |
| 19 |
|
| 20 |
/** |
| 21 |
* Table name. |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
protected $table = 'wd_lockout_report'; |
| 26 |
|
| 27 |
public const SLUG = 'firewall-report'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Constructor method. |
| 31 |
* Sets default values for the firewall report. |
| 32 |
*/ |
| 33 |
protected function before_load(): void { |
| 34 |
$default = array( |
| 35 |
'slug' => self::SLUG, |
| 36 |
'title' => esc_html__( 'Firewall - Reporting', 'defender-security' ), |
| 37 |
'status' => self::STATUS_DISABLED, |
| 38 |
'description' => esc_html__( |
| 39 |
'Configure Defender to automatically email you a lockout report for this website.', |
| 40 |
'defender-security' |
| 41 |
), |
| 42 |
// @since 3.0.0 Fix 'Guest'-line. |
| 43 |
'in_house_recipients' => is_user_logged_in() ? array( $this->get_default_user() ) : array(), |
| 44 |
'out_house_recipients' => array(), |
| 45 |
'type' => 'report', |
| 46 |
'dry_run' => false, |
| 47 |
'frequency' => 'weekly', |
| 48 |
'day' => 'sunday', |
| 49 |
'day_n' => 1, |
| 50 |
'time' => '4:00', |
| 51 |
'configs' => array(), |
| 52 |
); |
| 53 |
$this->import( $default ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Sends the firewall report email. |
| 58 |
* Constructs and sends the firewall report email to the recipients. |
| 59 |
* |
| 60 |
* @return void |
| 61 |
*/ |
| 62 |
public function send() { |
| 63 |
$service = wd_di()->get( Notification::class ); |
| 64 |
foreach ( $this->in_house_recipients as $recipient ) { |
| 65 |
if ( self::USER_SUBSCRIBED !== $recipient['status'] ) { |
| 66 |
continue; |
| 67 |
} |
| 68 |
$this->send_to_user( $recipient['name'], $recipient['email'], $service ); |
| 69 |
} |
| 70 |
foreach ( $this->out_house_recipients as $recipient ) { |
| 71 |
if ( self::USER_SUBSCRIBED !== $recipient['status'] ) { |
| 72 |
continue; |
| 73 |
} |
| 74 |
$this->send_to_user( $recipient['name'], $recipient['email'], $service ); |
| 75 |
} |
| 76 |
$this->last_sent = $this->est_timestamp; |
| 77 |
$this->est_timestamp = $this->get_next_run()->getTimestamp(); |
| 78 |
$this->save(); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Sends the firewall report email to a specific user. |
| 83 |
* Constructs and sends the firewall report email to the specified recipient. |
| 84 |
* |
| 85 |
* @param string $name The recipient's name. |
| 86 |
* @param string $email The recipient's email address. |
| 87 |
* @param object $service The notification service object. |
| 88 |
* |
| 89 |
* @return void |
| 90 |
*/ |
| 91 |
private function send_to_user( $name, $email, $service ) { |
| 92 |
$site_url = network_site_url(); |
| 93 |
$mail_object = wd_di()->get( Mail::class ); |
| 94 |
$plugin_label = $mail_object->get_sender_name( self::SLUG ); |
| 95 |
$subject = sprintf( |
| 96 |
/* translators: 1. Plugin label. 2. Site URL. */ |
| 97 |
esc_html__( '%1$s Lockouts Report for %2$s', 'defender-security' ), |
| 98 |
$plugin_label, |
| 99 |
$site_url |
| 100 |
); |
| 101 |
// Frequency. |
| 102 |
if ( 'daily' === $this->frequency ) { |
| 103 |
$time_unit = esc_html__( 'in the past 24 hours', 'defender-security' ); |
| 104 |
$interval = '-24 hours'; |
| 105 |
} elseif ( 'weekly' === $this->frequency ) { |
| 106 |
$time_unit = esc_html__( 'in the past week', 'defender-security' ); |
| 107 |
$interval = '-7 days'; |
| 108 |
} else { |
| 109 |
$time_unit = esc_html__( 'in the month', 'defender-security' ); |
| 110 |
$interval = '-30 days'; |
| 111 |
} |
| 112 |
// Number of lockouts. |
| 113 |
$count_lockouts = array( |
| 114 |
'404' => Lockout_Log::count( |
| 115 |
strtotime( $interval ), |
| 116 |
time(), |
| 117 |
array( |
| 118 |
Lockout_Log::LOCKOUT_404, |
| 119 |
) |
| 120 |
), |
| 121 |
'login' => Lockout_Log::count( |
| 122 |
strtotime( $interval ), |
| 123 |
time(), |
| 124 |
array( |
| 125 |
Lockout_Log::AUTH_LOCK, |
| 126 |
) |
| 127 |
), |
| 128 |
'ua' => Lockout_Log::count( |
| 129 |
strtotime( $interval ), |
| 130 |
time(), |
| 131 |
array( |
| 132 |
Lockout_Log::LOCKOUT_UA, |
| 133 |
) |
| 134 |
), |
| 135 |
); |
| 136 |
|
| 137 |
$firewall = wd_di()->get( Firewall::class ); |
| 138 |
$logs_url = network_admin_url( 'admin.php?page=wdf-ip-lockout&view=logs' ); |
| 139 |
// Need for activated Mask Login feature. |
| 140 |
$logs_url = apply_filters( 'report_email_logs_link', $logs_url, $email ); |
| 141 |
$content_body = $firewall->render_partial( |
| 142 |
'email/firewall-report', |
| 143 |
array( |
| 144 |
'name' => $name, |
| 145 |
'count_total' => (int) $count_lockouts['404'] + (int) $count_lockouts['login'] + (int) $count_lockouts['ua'], |
| 146 |
'time_unit' => $time_unit, |
| 147 |
'logs_url' => $logs_url, |
| 148 |
'site_url' => $site_url, |
| 149 |
'count_lockouts' => $count_lockouts, |
| 150 |
), |
| 151 |
false |
| 152 |
); |
| 153 |
$unsubscribe_link = $service->create_unsubscribe_url( $this->slug, $email ); |
| 154 |
$content = $firewall->render_partial( |
| 155 |
'email/index', |
| 156 |
array( |
| 157 |
'title' => esc_html__( 'Firewall', 'defender-security' ), |
| 158 |
'content_body' => $content_body, |
| 159 |
'unsubscribe_link' => $unsubscribe_link, |
| 160 |
), |
| 161 |
false |
| 162 |
); |
| 163 |
|
| 164 |
$headers = $mail_object->get_headers( |
| 165 |
defender_noreply_email( 'wd_lockout_noreply_email' ), |
| 166 |
self::SLUG |
| 167 |
); |
| 168 |
|
| 169 |
$ret = wp_mail( $email, $subject, $content, $headers ); |
| 170 |
if ( $ret ) { |
| 171 |
$this->save_log( $email ); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Define settings labels. |
| 177 |
* |
| 178 |
* @return array |
| 179 |
*/ |
| 180 |
public function labels(): array { |
| 181 |
return array( |
| 182 |
'report' => esc_html__( 'Firewall - Reporting', 'defender-security' ), |
| 183 |
'day' => esc_html__( 'Day of', 'defender-security' ), |
| 184 |
'day_n' => esc_html__( 'Day of', 'defender-security' ), |
| 185 |
'report_time' => esc_html__( 'Time of day', 'defender-security' ), |
| 186 |
'report_frequency' => esc_html__( 'Frequency', 'defender-security' ), |
| 187 |
'report_subscribers' => esc_html__( 'Recipients', 'defender-security' ), |
| 188 |
); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Additional converting rules. |
| 193 |
* |
| 194 |
* @param array $configs The configuration data. |
| 195 |
* |
| 196 |
* @return array The type-casted configuration data. |
| 197 |
* @since 3.1.0 |
| 198 |
*/ |
| 199 |
public function type_casting( $configs ): array { |
| 200 |
return is_array( $configs ) ? $configs : array(); |
| 201 |
} |
| 202 |
} |
| 203 |
|