| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Model\Notification; |
| 4 |
|
| 5 |
use WP_Defender\Controller\Firewall; |
| 6 |
use WP_Defender\Model\Email_Track; |
| 7 |
use WP_Defender\Model\Lockout_Log; |
| 8 |
use WP_Defender\Model\Setting\Login_Lockout; |
| 9 |
use WP_Defender\Model\Setting\Notfound_Lockout; |
| 10 |
use WP_Defender\Traits\IO; |
| 11 |
|
| 12 |
/** |
| 13 |
* Class Firewall_Notification |
| 14 |
* @package WP_Defender\Model\Notification |
| 15 |
*/ |
| 16 |
class Firewall_Notification extends \WP_Defender\Model\Notification { |
| 17 |
use IO; |
| 18 |
|
| 19 |
public $table = 'wd_malware_firewall_notification'; |
| 20 |
|
| 21 |
public function before_load() { |
| 22 |
$default = array( |
| 23 |
'title' => __( 'Firewall - Notification', 'wpdef' ), |
| 24 |
'slug' => 'firewall-notification', |
| 25 |
'status' => self::STATUS_DISABLED, |
| 26 |
'description' => __( 'Get email when a user or IP is locked out for trying to access your login area.', 'wpdef' ), |
| 27 |
// @since 3.0.0 Fix 'Guest'-line. |
| 28 |
'in_house_recipients' => is_user_logged_in() ? array( $this->get_default_user() ) : array(), |
| 29 |
'out_house_recipients' => array(), |
| 30 |
'type' => 'notification', |
| 31 |
'dry_run' => false, |
| 32 |
'configs' => array( |
| 33 |
'login_lockout' => false, |
| 34 |
'nf_lockout' => false, |
| 35 |
'limit' => false, |
| 36 |
'threshold' => 3, |
| 37 |
'cool_off' => 24, |
| 38 |
), |
| 39 |
); |
| 40 |
$this->import( $default ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* @param Lockout_Log $model |
| 45 |
* |
| 46 |
* @return bool |
| 47 |
*/ |
| 48 |
public function check_options( Lockout_Log $model ) { |
| 49 |
if ( self::STATUS_ACTIVE !== $this->status ) { |
| 50 |
return false; |
| 51 |
} |
| 52 |
// Check 'Login Protection Lockout'. |
| 53 |
if ( Lockout_Log::AUTH_LOCK === $model->type && true === $this->configs['login_lockout'] ) { |
| 54 |
return true; |
| 55 |
} |
| 56 |
// Check '404 Protection Lockout'. |
| 57 |
if ( Lockout_Log::LOCKOUT_404 === $model->type && true === $this->configs['nf_lockout'] ) { |
| 58 |
return true; |
| 59 |
} |
| 60 |
|
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* @param Lockout_Log $model |
| 66 |
*/ |
| 67 |
public function send( Lockout_Log $model ) { |
| 68 |
// Todo: add UA-template when UA-checkbox appears in the Firewall email template. |
| 69 |
if ( |
| 70 |
true === filter_var( $this->configs['login_lockout'], FILTER_VALIDATE_BOOLEAN ) |
| 71 |
&& Lockout_Log::AUTH_LOCK === $model->type |
| 72 |
) { |
| 73 |
$template = 'login-lockout'; |
| 74 |
} else { |
| 75 |
$template = 'lockout-404'; |
| 76 |
} |
| 77 |
|
| 78 |
foreach ( $this->in_house_recipients as $user ) { |
| 79 |
if ( self::USER_SUBSCRIBED !== $user['status'] ) { |
| 80 |
continue; |
| 81 |
} |
| 82 |
$this->send_to_user( $user['email'], $user['name'], $model, $template ); |
| 83 |
} |
| 84 |
|
| 85 |
foreach ( $this->out_house_recipients as $user ) { |
| 86 |
if ( self::USER_SUBSCRIBED !== $user['status'] ) { |
| 87 |
continue; |
| 88 |
} |
| 89 |
$this->send_to_user( $user['email'], $user['name'], $model, $template ); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* @param string $email |
| 95 |
* @param string $name |
| 96 |
* @param Lockout_Log $model |
| 97 |
* @param string $template |
| 98 |
* |
| 99 |
* @throws \DI\DependencyException |
| 100 |
* @throws \DI\NotFoundException |
| 101 |
*/ |
| 102 |
private function send_to_user( $email, $name, $model, $template ) { |
| 103 |
// Check if this meet the threshold. |
| 104 |
if ( true === $this->configs['limit'] ) { |
| 105 |
$count = Email_Track::count( $this->slug, $email, strtotime( '-' . $this->configs['cool_off'] . ' hours' ), time() ); |
| 106 |
if ( $count >= $this->configs['threshold'] ) { |
| 107 |
// No send. |
| 108 |
return; |
| 109 |
} |
| 110 |
} |
| 111 |
$network_site_url = network_site_url(); |
| 112 |
if ( 'login-lockout' === $template ) { |
| 113 |
$subject = sprintf( __( 'Login lockout alert for %s', 'wpdef' ), $network_site_url );// phpcs:ignore |
| 114 |
$settings = wd_di()->get( Login_Lockout::class ); |
| 115 |
$text = __( 'The host <strong>%1$s</strong> has been locked out of <a href="%2$s">%3$s</a> due to more than <strong>%4$s</strong> failed login attempts. %5$s', 'wpdef' );// phpcs:ignore |
| 116 |
if ( 'permanent' === $settings->lockout_type ) { |
| 117 |
$string = __( 'Accordingly, the host has been permanently banned.', 'wpdef' ); |
| 118 |
} else { |
| 119 |
$string = sprintf( __( 'They have been locked out for <strong>%1$s %2$s.</strong>', 'wpdef' ), $settings->duration, $settings->duration_unit );// phpcs:ignore |
| 120 |
} |
| 121 |
$text = sprintf( |
| 122 |
$text, |
| 123 |
$model->ip, |
| 124 |
$network_site_url, |
| 125 |
$network_site_url, |
| 126 |
$settings->attempt, |
| 127 |
$string |
| 128 |
); |
| 129 |
} else { |
| 130 |
$subject = sprintf( __( '404 lockout alert for %s', 'wpdef' ), $network_site_url );// phpcs:ignore |
| 131 |
$settings = wd_di()->get( Notfound_Lockout::class ); |
| 132 |
$text = __( 'The host <strong>%1$s</strong> has been locked out of <a href="%2$s">%3$s</a> due to more than <strong>%4$s</strong> 404 requests for the file <strong>%5$s</strong>. %6$s', 'wpdef' );// phpcs:ignore |
| 133 |
if ( 'permanent' === $settings->lockout_type ) { |
| 134 |
$string = __( 'Accordingly, the host has been permanently banned.', 'wpdef' ); |
| 135 |
} else { |
| 136 |
$string = sprintf( __( 'They have been locked out for <strong>%1$s %2$s.</strong>', 'wpdef' ), $settings->duration, $settings->duration_unit );// phpcs:ignore |
| 137 |
} |
| 138 |
$text = sprintf( |
| 139 |
$text, |
| 140 |
$model->ip, |
| 141 |
$network_site_url, |
| 142 |
$network_site_url, |
| 143 |
$settings->attempt, |
| 144 |
$model->tried, |
| 145 |
$string |
| 146 |
); |
| 147 |
} |
| 148 |
$firewall = wd_di()->get( Firewall::class ); |
| 149 |
$logs_url = network_admin_url( 'admin.php?page=wdf-ip-lockout&view=logs' ); |
| 150 |
// Need for activated Mask Login feature. |
| 151 |
$logs_url = apply_filters( 'report_email_logs_link', $logs_url, $email ); |
| 152 |
$content_body = $firewall->render_partial( |
| 153 |
'email/' . $template, |
| 154 |
array( |
| 155 |
'name' => $name, |
| 156 |
'text' => $text, |
| 157 |
'logs_url' => $logs_url, |
| 158 |
), |
| 159 |
false |
| 160 |
); |
| 161 |
$content = $firewall->render_partial( |
| 162 |
'email/index', |
| 163 |
array( |
| 164 |
'title' => __( 'Firewall', 'wpdef' ), |
| 165 |
'content_body' => $content_body, |
| 166 |
), |
| 167 |
false |
| 168 |
); |
| 169 |
|
| 170 |
$headers = defender_noreply_html_header( |
| 171 |
defender_noreply_email( 'wd_lockout_noreply_email' ) |
| 172 |
); |
| 173 |
|
| 174 |
$ret = wp_mail( $email, $subject, $content, $headers ); |
| 175 |
if ( $ret ) { |
| 176 |
$this->save_log( $email ); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Define labels for settings key. |
| 182 |
* |
| 183 |
* @param string|null $key |
| 184 |
* |
| 185 |
* @return string|array|null |
| 186 |
*/ |
| 187 |
public function labels( $key = null ) { |
| 188 |
$labels = array( |
| 189 |
'notification' => __( 'Firewall - Notification', 'wpdef' ), |
| 190 |
'login_lockout_notification' => __( 'Login Protection Lockout', 'wpdef' ), |
| 191 |
'ip_lockout_notification' => __( '404 Detection Lockout', 'wpdef' ), |
| 192 |
'notification_subscribers' => __( 'Recipients', 'wpdef' ), |
| 193 |
'cooldown_enabled' => __( 'Limit email notifications for repeat lockouts', 'wpdef' ), |
| 194 |
'cooldown_number_lockout' => __( 'Repeat Lockouts Threshold', 'wpdef' ), |
| 195 |
'cooldown_period' => __( 'Repeat Lockouts Period', 'wpdef' ), |
| 196 |
); |
| 197 |
|
| 198 |
if ( ! is_null( $key ) ) { |
| 199 |
return $labels[ $key ] ?? null; |
| 200 |
} |
| 201 |
|
| 202 |
return $labels; |
| 203 |
} |
| 204 |
} |
| 205 |
|