-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-location-watcher.php
146 lines (117 loc) · 4.27 KB
/
class-location-watcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
namespace Myrotvorets\WordPress\SecEnh;
use WildWolf\Utils\Singleton;
use WP_User;
/**
* @psalm-import-type GeolocateResponse from IP_API
*/
final class Location_Watcher {
use Singleton;
public const USER_META_KEY = 'psb_lw';
private string $ip = '';
/** @psalm-var GeolocateResponse|null|false */
private $location = false;
private function __construct() {
$this->init();
}
public function init(): void {
$ip_address = Utils::get_ip();
if ( $ip_address ) {
$this->ip = $ip_address;
add_action( 'wp_login', [ $this, 'wp_login' ], 10, 2 );
add_filter( 'attach_session_information', [ $this, 'attach_session_information' ] );
}
}
/**
* @param string $_user_login Username.
* @param WP_User $user WP_User object of the logged-in user.
*/
public function wp_login( $_user_login, WP_User $user ): void {
if ( false === apply_filters( 'psb_lw_check_user', true, $user ) ) {
return;
}
if ( apply_filters( 'psb_is_whitelisted_ip', false, $this->ip ) ) {
return;
}
if ( false === $this->location ) {
$this->location = IP_API::geolocate( $this->ip );
}
if ( ! $this->location ) {
$message = sprintf( 'Sign-on to %1$s by %3$s from an unknown new location: IP: %2$s', home_url(), $this->ip, $user->user_login );
Utils::log( 'wp-location-watcher', LOG_AUTH, LOG_WARNING, $message );
return;
}
if ( ! $this->check_meta( $user->ID, $this->location ) ) {
$this->send_notification( $user, $this->location );
$message = sprintf( 'Sign-on to %1$s by %4$s from a new location: IP: %2$s, %3$s', home_url(), $this->ip, join( ', ', IP_API::describe( $this->location ) ), $user->user_login );
Utils::log( 'wp-location-watcher', LOG_AUTH, LOG_WARNING, $message );
}
}
/**
* @param mixed[] $info
* @return mixed[]
*/
public function attach_session_information( array $info ): array {
if ( false === $this->location ) {
$this->location = IP_API::geolocate( $this->ip );
}
if ( $this->location ) {
$info['Location'] = join( ', ', IP_API::describe( $this->location ) );
}
return $info;
}
/**
* @psalm-param GeolocateResponse $location
*/
private function check_meta( int $user_id, array $location ): bool {
/** @var mixed */
$arr = get_user_meta( $user_id, self::USER_META_KEY, true );
if ( ! is_array( $arr ) ) {
$arr = [];
}
$key = join( "\0", [ $location['country'], $location['city'], $location['isp'] ] );
$res = isset( $arr[ $key ] );
$arr[ $key ] = time();
update_user_meta( $user_id, self::USER_META_KEY, $arr );
return $res;
}
/**
* @psalm-param GeolocateResponse $location
*/
private function send_notification( WP_User $user, array $location ): void {
if ( false === apply_filters( 'psb_lw_send_notification', true ) ) {
return;
}
$blogname = html_entity_decode( get_bloginfo( 'name' ), ENT_QUOTES );
$ua = Utils::get_ua();
$location = join( ', ', IP_API::describe( $location ) );
$location = (string) apply_filters( 'psb_lw_location', $location, $this->ip );
// translators: 1: site name; 2: user display name
$subject = sprintf( __( '[%1$s] SECURITY: %2$s has logged in from a new location', 'secenh' ), $blogname, $user->display_name );
$subject = (string) apply_filters( 'psb_lw_notification_subject', $subject );
// translators: 1: user disiplay name, 2: site URL, 3: login, 4: IP, 5: location, 6: user-agent
$message = __( 'Hello,
This is an automated email to inform you that %1$s has logged into %2$s from a new location.
It is likely that %1$s has logged in from a new web computer, but there is also a chance that their account has been compromised and someone else has logged into their account.
Username: %3$s
IP Address: %4$s
Guessed Location: %5$s
Browser User Agent: %6$s', 'secenh' );
$message = sprintf(
$message,
$user->display_name,
home_url(),
$user->user_login,
$this->ip,
$location,
$ua
);
$message = (string) apply_filters( 'psb_lw_notification_message', $message );
/** @var string[] */
$to = (array) apply_filters( 'psb_lw_notification_to', [ (string) get_option( 'admin_email' ) ] );
do_action( 'psb_lw_before_notification', $user );
// phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_mail_wp_mail
wp_mail( $to, $subject, $message );
do_action( 'psb_lw_after_notification', $user );
}
}